Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

162 rindas
4.7 KiB

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