Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

event.c 4.8 KiB

18 år sedan
18 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. free(events);
  92. caca_free_display(dp);
  93. cucul_free_canvas(cv);
  94. return 0;
  95. }
  96. static void print_event(int x, int y, caca_event_t *ev)
  97. {
  98. int character;
  99. switch(caca_get_event_type(ev))
  100. {
  101. case CACA_EVENT_NONE:
  102. cucul_printf(cv, x, y, "CACA_EVENT_NONE");
  103. break;
  104. case CACA_EVENT_KEY_PRESS:
  105. character = caca_get_event_key_ch(ev);
  106. cucul_printf(cv, x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%c)", character,
  107. (character > 0x1f && character < 0x80) ? character : '?');
  108. break;
  109. case CACA_EVENT_KEY_RELEASE:
  110. character = caca_get_event_key_ch(ev);
  111. cucul_printf(cv, x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%c)", character,
  112. (character > 0x1f && character < 0x80) ? character : '?');
  113. break;
  114. case CACA_EVENT_MOUSE_MOTION:
  115. cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_MOTION %u %u",
  116. caca_get_event_mouse_x(ev), caca_get_event_mouse_y(ev));
  117. break;
  118. case CACA_EVENT_MOUSE_PRESS:
  119. cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_PRESS %u",
  120. caca_get_event_mouse_button(ev));
  121. break;
  122. case CACA_EVENT_MOUSE_RELEASE:
  123. cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_RELEASE %u",
  124. caca_get_event_mouse_button(ev));
  125. break;
  126. case CACA_EVENT_RESIZE:
  127. cucul_printf(cv, x, y, "CACA_EVENT_RESIZE %u %u",
  128. caca_get_event_resize_width(ev),
  129. caca_get_event_resize_height(ev));
  130. break;
  131. case CACA_EVENT_QUIT:
  132. cucul_printf(cv, x, y, "CACA_EVENT_QUIT");
  133. break;
  134. default:
  135. cucul_printf(cv, x, y, "CACA_EVENT_UNKNOWN");
  136. }
  137. }