您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

164 行
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. #include "common.h"
  16. #if !defined(__KERNEL__)
  17. # include <stdio.h>
  18. # include <string.h>
  19. # include <stdlib.h>
  20. #endif
  21. #include "cucul.h"
  22. #include "caca.h"
  23. static cucul_canvas_t *cv;
  24. static caca_display_t *dp;
  25. static void print_event(int, int, caca_event_t *);
  26. int main(int argc, char **argv)
  27. {
  28. caca_event_t *events;
  29. int i, h, quit;
  30. cv = cucul_create_canvas(80, 24);
  31. if(cv == NULL)
  32. {
  33. printf("Failed to create canvas\n");
  34. return 1;
  35. }
  36. dp = caca_create_display(cv);
  37. if(dp == NULL)
  38. {
  39. printf("Failed to create display\n");
  40. return 1;
  41. }
  42. h = cucul_get_canvas_height(cv) - 1;
  43. cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);
  44. cucul_draw_line(cv, 0, 0, cucul_get_canvas_width(cv) - 1, 0, ' ');
  45. cucul_draw_line(cv, 0, h, cucul_get_canvas_width(cv) - 1, h, ' ');
  46. cucul_put_str(cv, 0, h, "type \"quit\" to exit");
  47. caca_refresh_display(dp);
  48. events = malloc(h * sizeof(caca_event_t));
  49. memset(events, 0, h * sizeof(caca_event_t));
  50. for(quit = 0; quit < 4; )
  51. {
  52. caca_event_t ev;
  53. static char const * quit_string[] = { "", "q", "qu", "qui", "quit" };
  54. int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, -1);
  55. if(!ret)
  56. continue;
  57. do
  58. {
  59. /* "quit" quits */
  60. if(caca_get_event_type(&ev) & CACA_EVENT_KEY_PRESS)
  61. {
  62. int key = caca_get_event_key_ch(&ev);
  63. if((key == 'q' && quit == 0) || (key == 'u' && quit == 1)
  64. || (key == 'i' && quit == 2) || (key == 't' && quit == 3))
  65. quit++;
  66. else if(key == 'q')
  67. quit = 1;
  68. else
  69. quit = 0;
  70. }
  71. memmove(events + 1, events, (h - 1) * sizeof(caca_event_t));
  72. events[0] = ev;
  73. ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0);
  74. }
  75. while(ret);
  76. cucul_set_color_ansi(cv, CUCUL_LIGHTGRAY, CUCUL_BLACK);
  77. cucul_clear_canvas(cv);
  78. /* Print current event */
  79. cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);
  80. cucul_draw_line(cv, 0, 0, cucul_get_canvas_width(cv) - 1, 0, ' ');
  81. print_event(0, 0, events);
  82. cucul_draw_line(cv, 0, h, cucul_get_canvas_width(cv) - 1, h, ' ');
  83. cucul_printf(cv, 0, h, "type \"quit\" to exit: %s", quit_string[quit]);
  84. /* Print previous events */
  85. cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLACK);
  86. for(i = 1; i < h && caca_get_event_type(&events[i]); i++)
  87. print_event(0, i, events + i);
  88. caca_refresh_display(dp);
  89. }
  90. /* Clean up */
  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. }