Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

144 linhas
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 "caca.h"
  27. static void print_event(int, int, unsigned int);
  28. int main(int argc, char **argv)
  29. {
  30. int *events;
  31. int i, h, quit;
  32. if(caca_init())
  33. return 1;
  34. h = caca_get_height() - 1;
  35. caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
  36. caca_draw_line(0, 0, caca_get_width() - 1, 0, ' ');
  37. caca_draw_line(0, h, caca_get_width() - 1, h, ' ');
  38. caca_putstr(0, h, "type \"quit\" to exit");
  39. caca_refresh();
  40. events = malloc(h * sizeof(int));
  41. memset(events, 0, h * sizeof(int));
  42. for(quit = 0; quit < 4; )
  43. {
  44. static char const * quit_string[] = { "", "q", "qu", "qui", "quit" };
  45. unsigned int event = caca_wait_event(CACA_EVENT_ANY);
  46. if(!event)
  47. continue;
  48. do
  49. {
  50. /* "quit" quits */
  51. if(event & CACA_EVENT_KEY_PRESS)
  52. {
  53. int key = event & ~CACA_EVENT_KEY_PRESS;
  54. if((key == 'q' && quit == 0) || (key == 'u' && quit == 1)
  55. || (key == 'i' && quit == 2) || (key == 't' && quit == 3))
  56. quit++;
  57. else if(key == 'q')
  58. quit = 1;
  59. else
  60. quit = 0;
  61. }
  62. memmove(events + 1, events, (h - 1) * sizeof(int));
  63. events[0] = event;
  64. event = caca_get_event(CACA_EVENT_ANY);
  65. }
  66. while(event);
  67. caca_clear();
  68. /* Print current event */
  69. caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
  70. caca_draw_line(0, 0, caca_get_width() - 1, 0, ' ');
  71. print_event(0, 0, events[0]);
  72. caca_draw_line(0, h, caca_get_width() - 1, h, ' ');
  73. caca_printf(0, h, "type \"quit\" to exit: %s", quit_string[quit]);
  74. /* Print previous events */
  75. caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLACK);
  76. for(i = 1; i < h && events[i]; i++)
  77. print_event(0, i, events[i]);
  78. caca_refresh();
  79. }
  80. /* Clean up */
  81. caca_end();
  82. return 0;
  83. }
  84. static void print_event(int x, int y, unsigned int event)
  85. {
  86. int character;
  87. switch(event & 0xff000000)
  88. {
  89. case CACA_EVENT_NONE:
  90. caca_printf(x, y, "CACA_EVENT_NONE");
  91. break;
  92. case CACA_EVENT_KEY_PRESS:
  93. character = event & 0x00ffffff;
  94. caca_printf(x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%c)", character,
  95. (character > 0x20 && character < 0x80) ? character : '?');
  96. break;
  97. case CACA_EVENT_KEY_RELEASE:
  98. character = event & 0x00ffffff;
  99. caca_printf(x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%c)", character,
  100. (character > 0x20 && character < 0x80) ? character : '?');
  101. break;
  102. case CACA_EVENT_MOUSE_MOTION:
  103. caca_printf(x, y, "CACA_EVENT_MOUSE_MOTION %u %u",
  104. (event & 0x00fff000) >> 12, event & 0x00000fff);
  105. break;
  106. case CACA_EVENT_MOUSE_PRESS:
  107. caca_printf(x, y, "CACA_EVENT_MOUSE_PRESS %u",
  108. event & 0x00ffffff);
  109. break;
  110. case CACA_EVENT_MOUSE_RELEASE:
  111. caca_printf(x, y, "CACA_EVENT_MOUSE_RELEASE %u",
  112. event & 0x00ffffff);
  113. break;
  114. default:
  115. caca_printf(x, y, "CACA_EVENT_UNKNOWN");
  116. }
  117. }