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

156 рядки
4.5 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 commes 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(0, 0);
  31. if(!cv)
  32. return 1;
  33. dp = caca_create_display(cv);
  34. if(!dp)
  35. return 1;
  36. h = cucul_get_canvas_height(cv) - 1;
  37. cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);
  38. cucul_draw_line(cv, 0, 0, cucul_get_canvas_width(cv) - 1, 0, ' ');
  39. cucul_draw_line(cv, 0, h, cucul_get_canvas_width(cv) - 1, h, ' ');
  40. cucul_put_str(cv, 0, h, "type \"quit\" to exit");
  41. caca_refresh_display(dp);
  42. events = malloc(h * sizeof(caca_event_t));
  43. memset(events, 0, h * sizeof(caca_event_t));
  44. for(quit = 0; quit < 4; )
  45. {
  46. caca_event_t ev;
  47. static char const * quit_string[] = { "", "q", "qu", "qui", "quit" };
  48. int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, -1);
  49. if(!ret)
  50. continue;
  51. do
  52. {
  53. /* "quit" quits */
  54. if(ev.type & CACA_EVENT_KEY_PRESS)
  55. {
  56. int key = ev.data.key.ch;
  57. if((key == 'q' && quit == 0) || (key == 'u' && quit == 1)
  58. || (key == 'i' && quit == 2) || (key == 't' && quit == 3))
  59. quit++;
  60. else if(key == 'q')
  61. quit = 1;
  62. else
  63. quit = 0;
  64. }
  65. memmove(events + 1, events, (h - 1) * sizeof(caca_event_t));
  66. events[0] = ev;
  67. ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0);
  68. }
  69. while(ret);
  70. cucul_set_color_ansi(cv, CUCUL_LIGHTGRAY, CUCUL_BLACK);
  71. cucul_clear_canvas(cv);
  72. /* Print current event */
  73. cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);
  74. cucul_draw_line(cv, 0, 0, cucul_get_canvas_width(cv) - 1, 0, ' ');
  75. print_event(0, 0, events);
  76. cucul_draw_line(cv, 0, h, cucul_get_canvas_width(cv) - 1, h, ' ');
  77. cucul_printf(cv, 0, h, "type \"quit\" to exit: %s", quit_string[quit]);
  78. /* Print previous events */
  79. cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLACK);
  80. for(i = 1; i < h && events[i].type; i++)
  81. print_event(0, i, events + i);
  82. caca_refresh_display(dp);
  83. }
  84. /* Clean up */
  85. caca_free_display(dp);
  86. cucul_free_canvas(cv);
  87. return 0;
  88. }
  89. static void print_event(int x, int y, caca_event_t *ev)
  90. {
  91. int character;
  92. switch(ev->type)
  93. {
  94. case CACA_EVENT_NONE:
  95. cucul_printf(cv, x, y, "CACA_EVENT_NONE");
  96. break;
  97. case CACA_EVENT_KEY_PRESS:
  98. character = ev->data.key.ch;
  99. cucul_printf(cv, x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%c)", character,
  100. (character > 0x1f && character < 0x80) ? character : '?');
  101. break;
  102. case CACA_EVENT_KEY_RELEASE:
  103. character = ev->data.key.ch;
  104. cucul_printf(cv, x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%c)", character,
  105. (character > 0x1f && character < 0x80) ? character : '?');
  106. break;
  107. case CACA_EVENT_MOUSE_MOTION:
  108. cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_MOTION %u %u",
  109. ev->data.mouse.x, ev->data.mouse.y);
  110. break;
  111. case CACA_EVENT_MOUSE_PRESS:
  112. cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_PRESS %u",
  113. ev->data.mouse.button);
  114. break;
  115. case CACA_EVENT_MOUSE_RELEASE:
  116. cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_RELEASE %u",
  117. ev->data.mouse.button);
  118. break;
  119. case CACA_EVENT_RESIZE:
  120. cucul_printf(cv, x, y, "CACA_EVENT_RESIZE %u %u",
  121. ev->data.resize.w, ev->data.resize.h);
  122. break;
  123. case CACA_EVENT_QUIT:
  124. cucul_printf(cv, x, y, "CACA_EVENT_QUIT");
  125. break;
  126. default:
  127. cucul_printf(cv, x, y, "CACA_EVENT_UNKNOWN");
  128. }
  129. }