Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

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