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.

event.c 4.4 KiB

18 years ago
18 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. #include "config.h"
  14. #include "common.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(0, 0);
  30. if(!cv)
  31. return 1;
  32. dp = caca_create_display(cv);
  33. if(!dp)
  34. return 1;
  35. h = cucul_get_canvas_height(cv) - 1;
  36. cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);
  37. cucul_draw_line(cv, 0, 0, cucul_get_canvas_width(cv) - 1, 0, ' ');
  38. cucul_draw_line(cv, 0, h, cucul_get_canvas_width(cv) - 1, h, ' ');
  39. cucul_put_str(cv, 0, h, "type \"quit\" to exit");
  40. caca_refresh_display(dp);
  41. events = malloc(h * sizeof(caca_event_t));
  42. memset(events, 0, h * sizeof(caca_event_t));
  43. for(quit = 0; quit < 4; )
  44. {
  45. caca_event_t ev;
  46. static char const * quit_string[] = { "", "q", "qu", "qui", "quit" };
  47. int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, -1);
  48. if(!ret)
  49. continue;
  50. do
  51. {
  52. /* "quit" quits */
  53. if(ev.type & CACA_EVENT_KEY_PRESS)
  54. {
  55. int key = ev.data.key.ch;
  56. if((key == 'q' && quit == 0) || (key == 'u' && quit == 1)
  57. || (key == 'i' && quit == 2) || (key == 't' && quit == 3))
  58. quit++;
  59. else if(key == 'q')
  60. quit = 1;
  61. else
  62. quit = 0;
  63. }
  64. memmove(events + 1, events, (h - 1) * sizeof(caca_event_t));
  65. events[0] = ev;
  66. ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0);
  67. }
  68. while(ret);
  69. cucul_set_color_ansi(cv, CUCUL_LIGHTGRAY, CUCUL_BLACK);
  70. cucul_clear_canvas(cv);
  71. /* Print current event */
  72. cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);
  73. cucul_draw_line(cv, 0, 0, cucul_get_canvas_width(cv) - 1, 0, ' ');
  74. print_event(0, 0, events);
  75. cucul_draw_line(cv, 0, h, cucul_get_canvas_width(cv) - 1, h, ' ');
  76. cucul_printf(cv, 0, h, "type \"quit\" to exit: %s", quit_string[quit]);
  77. /* Print previous events */
  78. cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLACK);
  79. for(i = 1; i < h && events[i].type; i++)
  80. print_event(0, i, events + i);
  81. caca_refresh_display(dp);
  82. }
  83. /* Clean up */
  84. caca_free_display(dp);
  85. cucul_free_canvas(cv);
  86. return 0;
  87. }
  88. static void print_event(int x, int y, caca_event_t *ev)
  89. {
  90. int character;
  91. switch(ev->type)
  92. {
  93. case CACA_EVENT_NONE:
  94. cucul_printf(cv, x, y, "CACA_EVENT_NONE");
  95. break;
  96. case CACA_EVENT_KEY_PRESS:
  97. character = ev->data.key.ch;
  98. cucul_printf(cv, x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%c)", character,
  99. (character > 0x1f && character < 0x80) ? character : '?');
  100. break;
  101. case CACA_EVENT_KEY_RELEASE:
  102. character = ev->data.key.ch;
  103. cucul_printf(cv, x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%c)", character,
  104. (character > 0x1f && character < 0x80) ? character : '?');
  105. break;
  106. case CACA_EVENT_MOUSE_MOTION:
  107. cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_MOTION %u %u",
  108. ev->data.mouse.x, ev->data.mouse.y);
  109. break;
  110. case CACA_EVENT_MOUSE_PRESS:
  111. cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_PRESS %u",
  112. ev->data.mouse.button);
  113. break;
  114. case CACA_EVENT_MOUSE_RELEASE:
  115. cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_RELEASE %u",
  116. ev->data.mouse.button);
  117. break;
  118. case CACA_EVENT_RESIZE:
  119. cucul_printf(cv, x, y, "CACA_EVENT_RESIZE %u %u",
  120. ev->data.resize.w, ev->data.resize.h);
  121. break;
  122. case CACA_EVENT_QUIT:
  123. cucul_printf(cv, x, y, "CACA_EVENT_QUIT");
  124. break;
  125. default:
  126. cucul_printf(cv, x, y, "CACA_EVENT_UNKNOWN");
  127. }
  128. }