You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

165 lines
4.8 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 "cucul.h"
  21. #include "caca.h"
  22. static cucul_canvas_t *cv;
  23. static caca_display_t *dp;
  24. static void print_event(int, int, caca_event_t *);
  25. int main(int argc, char **argv)
  26. {
  27. caca_event_t *events;
  28. int i, h, quit;
  29. cv = cucul_create_canvas(80, 24);
  30. if(cv == NULL)
  31. {
  32. printf("Failed to create canvas\n");
  33. return 1;
  34. }
  35. dp = caca_create_display(cv);
  36. if(dp == NULL)
  37. {
  38. printf("Failed to create display\n");
  39. return 1;
  40. }
  41. h = cucul_get_canvas_height(cv) - 1;
  42. cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);
  43. cucul_draw_line(cv, 0, 0, cucul_get_canvas_width(cv) - 1, 0, ' ');
  44. cucul_draw_line(cv, 0, h, cucul_get_canvas_width(cv) - 1, h, ' ');
  45. cucul_put_str(cv, 0, h, "type \"quit\" to exit");
  46. caca_refresh_display(dp);
  47. events = malloc(h * sizeof(caca_event_t));
  48. memset(events, 0, h * sizeof(caca_event_t));
  49. for(quit = 0; quit < 4; )
  50. {
  51. caca_event_t ev;
  52. static char const * quit_string[] = { "", "q", "qu", "qui", "quit" };
  53. int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, -1);
  54. if(!ret)
  55. continue;
  56. do
  57. {
  58. /* "quit" quits */
  59. if(caca_get_event_type(&ev) & CACA_EVENT_KEY_PRESS)
  60. {
  61. int key = caca_get_event_key_ch(&ev);
  62. if((key == 'q' && quit == 0) || (key == 'u' && quit == 1)
  63. || (key == 'i' && quit == 2) || (key == 't' && quit == 3))
  64. quit++;
  65. else if(key == 'q')
  66. quit = 1;
  67. else
  68. quit = 0;
  69. }
  70. memmove(events + 1, events, (h - 1) * sizeof(caca_event_t));
  71. events[0] = ev;
  72. ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0);
  73. }
  74. while(ret);
  75. cucul_set_color_ansi(cv, CUCUL_LIGHTGRAY, CUCUL_BLACK);
  76. cucul_clear_canvas(cv);
  77. /* Print current event */
  78. cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);
  79. cucul_draw_line(cv, 0, 0, cucul_get_canvas_width(cv) - 1, 0, ' ');
  80. print_event(0, 0, events);
  81. cucul_draw_line(cv, 0, h, cucul_get_canvas_width(cv) - 1, h, ' ');
  82. cucul_printf(cv, 0, h, "type \"quit\" to exit: %s", quit_string[quit]);
  83. /* Print previous events */
  84. cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLACK);
  85. for(i = 1; i < h && caca_get_event_type(&events[i]); i++)
  86. print_event(0, i, events + i);
  87. caca_refresh_display(dp);
  88. }
  89. /* Clean up */
  90. free(events);
  91. caca_free_display(dp);
  92. cucul_free_canvas(cv);
  93. return 0;
  94. }
  95. static void print_event(int x, int y, caca_event_t *ev)
  96. {
  97. int character;
  98. switch(caca_get_event_type(ev))
  99. {
  100. case CACA_EVENT_NONE:
  101. cucul_printf(cv, x, y, "CACA_EVENT_NONE");
  102. break;
  103. case CACA_EVENT_KEY_PRESS:
  104. character = caca_get_event_key_ch(ev);
  105. cucul_printf(cv, x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%c)", character,
  106. (character > 0x1f && character < 0x80) ? character : '?');
  107. break;
  108. case CACA_EVENT_KEY_RELEASE:
  109. character = caca_get_event_key_ch(ev);
  110. cucul_printf(cv, x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%c)", character,
  111. (character > 0x1f && character < 0x80) ? character : '?');
  112. break;
  113. case CACA_EVENT_MOUSE_MOTION:
  114. cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_MOTION %u %u",
  115. caca_get_event_mouse_x(ev), caca_get_event_mouse_y(ev));
  116. break;
  117. case CACA_EVENT_MOUSE_PRESS:
  118. cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_PRESS %u",
  119. caca_get_event_mouse_button(ev));
  120. break;
  121. case CACA_EVENT_MOUSE_RELEASE:
  122. cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_RELEASE %u",
  123. caca_get_event_mouse_button(ev));
  124. break;
  125. case CACA_EVENT_RESIZE:
  126. cucul_printf(cv, x, y, "CACA_EVENT_RESIZE %u %u",
  127. caca_get_event_resize_width(ev),
  128. caca_get_event_resize_height(ev));
  129. break;
  130. case CACA_EVENT_QUIT:
  131. cucul_printf(cv, x, y, "CACA_EVENT_QUIT");
  132. break;
  133. default:
  134. cucul_printf(cv, x, y, "CACA_EVENT_UNKNOWN");
  135. }
  136. }