Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

149 řádky
4.1 KiB

  1. /*
  2. * event event lister for libcaca
  3. * Copyright (c) 2004 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. #include "config.h"
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include "cucul.h"
  18. #include "caca.h"
  19. static cucul_t *qq;
  20. static caca_t *kk;
  21. static void print_event(int, int, struct caca_event *);
  22. int main(int argc, char **argv)
  23. {
  24. struct caca_event *events;
  25. int i, h, quit;
  26. qq = cucul_create(0, 0);
  27. if(!qq)
  28. return 1;
  29. kk = caca_attach(qq);
  30. if(!kk)
  31. return 1;
  32. h = cucul_get_height(qq) - 1;
  33. cucul_set_color(qq, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
  34. cucul_draw_line(qq, 0, 0, cucul_get_width(qq) - 1, 0, " ");
  35. cucul_draw_line(qq, 0, h, cucul_get_width(qq) - 1, h, " ");
  36. cucul_putstr(qq, 0, h, "type \"quit\" to exit");
  37. caca_display(kk);
  38. events = malloc(h * sizeof(struct caca_event));
  39. memset(events, 0, h * sizeof(struct caca_event));
  40. for(quit = 0; quit < 4; )
  41. {
  42. struct caca_event ev;
  43. static char const * quit_string[] = { "", "q", "qu", "qui", "quit" };
  44. int ret = caca_wait_event(kk, CACA_EVENT_ANY, &ev);
  45. if(!ret)
  46. continue;
  47. do
  48. {
  49. /* "quit" quits */
  50. if(ev.type & CACA_EVENT_KEY_PRESS)
  51. {
  52. int key = ev.data.key.c;
  53. if((key == 'q' && quit == 0) || (key == 'u' && quit == 1)
  54. || (key == 'i' && quit == 2) || (key == 't' && quit == 3))
  55. quit++;
  56. else if(key == 'q')
  57. quit = 1;
  58. else
  59. quit = 0;
  60. }
  61. memmove(events + 1, events, (h - 1) * sizeof(struct caca_event));
  62. events[0] = ev;
  63. ret = caca_get_event(kk, CACA_EVENT_ANY, &ev);
  64. }
  65. while(ret);
  66. cucul_clear(qq);
  67. /* Print current event */
  68. cucul_set_color(qq, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
  69. cucul_draw_line(qq, 0, 0, cucul_get_width(qq) - 1, 0, " ");
  70. print_event(0, 0, events);
  71. cucul_draw_line(qq, 0, h, cucul_get_width(qq) - 1, h, " ");
  72. cucul_printf(qq, 0, h, "type \"quit\" to exit: %s", quit_string[quit]);
  73. /* Print previous events */
  74. cucul_set_color(qq, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLACK);
  75. for(i = 1; i < h && events[i].type; i++)
  76. print_event(0, i, events + i);
  77. caca_display(kk);
  78. }
  79. /* Clean up */
  80. caca_detach(kk);
  81. cucul_free(qq);
  82. return 0;
  83. }
  84. static void print_event(int x, int y, struct caca_event *ev)
  85. {
  86. int character;
  87. switch(ev->type)
  88. {
  89. case CACA_EVENT_NONE:
  90. cucul_printf(qq, x, y, "CACA_EVENT_NONE");
  91. break;
  92. case CACA_EVENT_KEY_PRESS:
  93. character = ev->data.key.c;
  94. cucul_printf(qq, x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%c)", character,
  95. (character > 0x20 && character < 0x80) ? character : '?');
  96. break;
  97. case CACA_EVENT_KEY_RELEASE:
  98. character = ev->data.key.c;
  99. cucul_printf(qq, x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%c)", character,
  100. (character > 0x20 && character < 0x80) ? character : '?');
  101. break;
  102. case CACA_EVENT_MOUSE_MOTION:
  103. cucul_printf(qq, x, y, "CACA_EVENT_MOUSE_MOTION %u %u",
  104. ev->data.mouse.x, ev->data.mouse.y);
  105. break;
  106. case CACA_EVENT_MOUSE_PRESS:
  107. cucul_printf(qq, x, y, "CACA_EVENT_MOUSE_PRESS %u",
  108. ev->data.mouse.button);
  109. break;
  110. case CACA_EVENT_MOUSE_RELEASE:
  111. cucul_printf(qq, x, y, "CACA_EVENT_MOUSE_RELEASE %u",
  112. ev->data.mouse.button);
  113. break;
  114. case CACA_EVENT_RESIZE:
  115. cucul_printf(qq, x, y, "CACA_EVENT_RESIZE %u %u",
  116. ev->data.resize.w, ev->data.resize.h);
  117. break;
  118. default:
  119. cucul_printf(qq, x, y, "CACA_EVENT_UNKNOWN");
  120. }
  121. }