Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

164 рядки
4.7 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. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. #include "config.h"
  15. #if !defined(__KERNEL__)
  16. # include <stdio.h>
  17. # include <string.h>
  18. # include <stdlib.h>
  19. #endif
  20. #include "caca.h"
  21. static caca_canvas_t *cv;
  22. static caca_display_t *dp;
  23. static void print_event(int, int, caca_event_t *);
  24. int main(int argc, char **argv)
  25. {
  26. caca_event_t *events;
  27. int i, h, quit;
  28. cv = caca_create_canvas(80, 24);
  29. if(cv == NULL)
  30. {
  31. printf("Failed to create canvas\n");
  32. return 1;
  33. }
  34. dp = caca_create_display(cv);
  35. if(dp == NULL)
  36. {
  37. printf("Failed to create display\n");
  38. return 1;
  39. }
  40. h = caca_get_canvas_height(cv) - 1;
  41. caca_set_color_ansi(cv, CACA_WHITE, CACA_BLUE);
  42. caca_draw_line(cv, 0, 0, caca_get_canvas_width(cv) - 1, 0, ' ');
  43. caca_draw_line(cv, 0, h, caca_get_canvas_width(cv) - 1, h, ' ');
  44. caca_put_str(cv, 0, h, "type \"quit\" to exit");
  45. caca_refresh_display(dp);
  46. events = malloc(h * sizeof(caca_event_t));
  47. memset(events, 0, h * sizeof(caca_event_t));
  48. for(quit = 0; quit < 4; )
  49. {
  50. caca_event_t ev;
  51. static char const * quit_string[] = { "", "q", "qu", "qui", "quit" };
  52. int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, -1);
  53. if(!ret)
  54. continue;
  55. do
  56. {
  57. /* "quit" quits */
  58. if(caca_get_event_type(&ev) & CACA_EVENT_KEY_PRESS)
  59. {
  60. int key = caca_get_event_key_ch(&ev);
  61. if((key == 'q' && quit == 0) || (key == 'u' && quit == 1)
  62. || (key == 'i' && quit == 2) || (key == 't' && quit == 3))
  63. quit++;
  64. else if(key == 'q')
  65. quit = 1;
  66. else
  67. quit = 0;
  68. }
  69. memmove(events + 1, events, (h - 1) * sizeof(caca_event_t));
  70. events[0] = ev;
  71. ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0);
  72. }
  73. while(ret);
  74. caca_set_color_ansi(cv, CACA_LIGHTGRAY, CACA_BLACK);
  75. caca_clear_canvas(cv);
  76. /* Print current event */
  77. caca_set_color_ansi(cv, CACA_WHITE, CACA_BLUE);
  78. caca_draw_line(cv, 0, 0, caca_get_canvas_width(cv) - 1, 0, ' ');
  79. print_event(0, 0, events);
  80. caca_draw_line(cv, 0, h, caca_get_canvas_width(cv) - 1, h, ' ');
  81. caca_printf(cv, 0, h, "type \"quit\" to exit: %s", quit_string[quit]);
  82. /* Print previous events */
  83. caca_set_color_ansi(cv, CACA_WHITE, CACA_BLACK);
  84. for(i = 1; i < h && caca_get_event_type(&events[i]); i++)
  85. print_event(0, i, events + i);
  86. caca_refresh_display(dp);
  87. }
  88. /* Clean up */
  89. free(events);
  90. caca_free_display(dp);
  91. caca_free_canvas(cv);
  92. return 0;
  93. }
  94. static void print_event(int x, int y, caca_event_t *ev)
  95. {
  96. int character;
  97. switch(caca_get_event_type(ev))
  98. {
  99. case CACA_EVENT_NONE:
  100. caca_printf(cv, x, y, "CACA_EVENT_NONE");
  101. break;
  102. case CACA_EVENT_KEY_PRESS:
  103. character = caca_get_event_key_ch(ev);
  104. caca_printf(cv, x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%c)", character,
  105. (character > 0x1f && character < 0x80) ? character : '?');
  106. break;
  107. case CACA_EVENT_KEY_RELEASE:
  108. character = caca_get_event_key_ch(ev);
  109. caca_printf(cv, x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%c)", character,
  110. (character > 0x1f && character < 0x80) ? character : '?');
  111. break;
  112. case CACA_EVENT_MOUSE_MOTION:
  113. caca_printf(cv, x, y, "CACA_EVENT_MOUSE_MOTION %u %u",
  114. caca_get_event_mouse_x(ev), caca_get_event_mouse_y(ev));
  115. break;
  116. case CACA_EVENT_MOUSE_PRESS:
  117. caca_printf(cv, x, y, "CACA_EVENT_MOUSE_PRESS %u",
  118. caca_get_event_mouse_button(ev));
  119. break;
  120. case CACA_EVENT_MOUSE_RELEASE:
  121. caca_printf(cv, x, y, "CACA_EVENT_MOUSE_RELEASE %u",
  122. caca_get_event_mouse_button(ev));
  123. break;
  124. case CACA_EVENT_RESIZE:
  125. caca_printf(cv, x, y, "CACA_EVENT_RESIZE %u %u",
  126. caca_get_event_resize_width(ev),
  127. caca_get_event_resize_height(ev));
  128. break;
  129. case CACA_EVENT_QUIT:
  130. caca_printf(cv, x, y, "CACA_EVENT_QUIT");
  131. break;
  132. default:
  133. caca_printf(cv, x, y, "CACA_EVENT_UNKNOWN");
  134. }
  135. }