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.
 
 
 
 
 
 

145 line
4.1 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; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  21. * 02111-1307 USA
  22. */
  23. #include "config.h"
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include "caca.h"
  28. static void print_event(int, int, unsigned int);
  29. int main(int argc, char **argv)
  30. {
  31. int *events;
  32. int i, h, quit;
  33. if(caca_init())
  34. return 1;
  35. h = caca_get_height() - 1;
  36. caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
  37. caca_draw_line(0, 0, caca_get_width() - 1, 0, ' ');
  38. caca_draw_line(0, h, caca_get_width() - 1, h, ' ');
  39. caca_putstr(0, h, "type \"quit\" to exit");
  40. caca_refresh();
  41. events = malloc(h * sizeof(int));
  42. memset(events, 0, h * sizeof(int));
  43. for(quit = 0; quit < 4; )
  44. {
  45. static char const * quit_string[] = { "", "q", "qu", "qui", "quit" };
  46. unsigned int event = caca_wait_event(CACA_EVENT_ANY);
  47. if(!event)
  48. continue;
  49. do
  50. {
  51. /* "quit" quits */
  52. if(event & CACA_EVENT_KEY_PRESS)
  53. {
  54. int key = event & ~CACA_EVENT_KEY_PRESS;
  55. if((key == 'q' && quit == 0) || (key == 'u' && quit == 1)
  56. || (key == 'i' && quit == 2) || (key == 't' && quit == 3))
  57. quit++;
  58. else if(key == 'q')
  59. quit = 1;
  60. else
  61. quit = 0;
  62. }
  63. memmove(events + 1, events, (h - 1) * sizeof(int));
  64. events[0] = event;
  65. event = caca_get_event(CACA_EVENT_ANY);
  66. }
  67. while(event);
  68. caca_clear();
  69. /* Print current event */
  70. caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
  71. caca_draw_line(0, 0, caca_get_width() - 1, 0, ' ');
  72. print_event(0, 0, events[0]);
  73. caca_draw_line(0, h, caca_get_width() - 1, h, ' ');
  74. caca_printf(0, h, "type \"quit\" to exit: %s", quit_string[quit]);
  75. /* Print previous events */
  76. caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLACK);
  77. for(i = 1; i < h && events[i]; i++)
  78. print_event(0, i, events[i]);
  79. caca_refresh();
  80. }
  81. /* Clean up */
  82. caca_end();
  83. return 0;
  84. }
  85. static void print_event(int x, int y, unsigned int event)
  86. {
  87. int character;
  88. switch(event & 0xff000000)
  89. {
  90. case CACA_EVENT_NONE:
  91. caca_printf(x, y, "CACA_EVENT_NONE");
  92. break;
  93. case CACA_EVENT_KEY_PRESS:
  94. character = event & 0x00ffffff;
  95. caca_printf(x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%c)", character,
  96. (character > 0x20 && character < 0x80) ? character : '?');
  97. break;
  98. case CACA_EVENT_KEY_RELEASE:
  99. character = event & 0x00ffffff;
  100. caca_printf(x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%c)", character,
  101. (character > 0x20 && character < 0x80) ? character : '?');
  102. break;
  103. case CACA_EVENT_MOUSE_MOTION:
  104. caca_printf(x, y, "CACA_EVENT_MOUSE_MOTION %u %u",
  105. (event & 0x00fff000) >> 12, event & 0x00000fff);
  106. break;
  107. case CACA_EVENT_MOUSE_PRESS:
  108. caca_printf(x, y, "CACA_EVENT_MOUSE_PRESS %u",
  109. event & 0x00ffffff);
  110. break;
  111. case CACA_EVENT_MOUSE_RELEASE:
  112. caca_printf(x, y, "CACA_EVENT_MOUSE_RELEASE %u",
  113. event & 0x00ffffff);
  114. break;
  115. default:
  116. caca_printf(x, y, "CACA_EVENT_UNKNOWN");
  117. }
  118. }