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.

18 jaren geleden
18 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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(32, 16);
  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(ev.type & CACA_EVENT_KEY_PRESS)
  61. {
  62. int key = ev.data.key.ch;
  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 && events[i].type; 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(ev->type)
  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 = ev->data.key.ch;
  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 = ev->data.key.ch;
  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. ev->data.mouse.x, ev->data.mouse.y);
  116. break;
  117. case CACA_EVENT_MOUSE_PRESS:
  118. cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_PRESS %u",
  119. ev->data.mouse.button);
  120. break;
  121. case CACA_EVENT_MOUSE_RELEASE:
  122. cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_RELEASE %u",
  123. ev->data.mouse.button);
  124. break;
  125. case CACA_EVENT_RESIZE:
  126. cucul_printf(cv, x, y, "CACA_EVENT_RESIZE %u %u",
  127. ev->data.resize.w, ev->data.resize.h);
  128. break;
  129. case CACA_EVENT_QUIT:
  130. cucul_printf(cv, x, y, "CACA_EVENT_QUIT");
  131. break;
  132. default:
  133. cucul_printf(cv, x, y, "CACA_EVENT_UNKNOWN");
  134. }
  135. }