Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

153 righe
4.4 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 "cucul.h"
  18. #include "caca.h"
  19. static cucul_canvas_t *cv;
  20. static caca_display_t *dp;
  21. static void print_event(int, int, caca_event_t *);
  22. int main(int argc, char **argv)
  23. {
  24. caca_event_t *events;
  25. int i, h, quit;
  26. cv = cucul_create_canvas(0, 0);
  27. if(!cv)
  28. return 1;
  29. dp = caca_create_display(cv);
  30. if(!dp)
  31. return 1;
  32. h = cucul_get_canvas_height(cv) - 1;
  33. cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
  34. cucul_draw_line(cv, 0, 0, cucul_get_canvas_width(cv) - 1, 0, " ");
  35. cucul_draw_line(cv, 0, h, cucul_get_canvas_width(cv) - 1, h, " ");
  36. cucul_putstr(cv, 0, h, "type \"quit\" to exit");
  37. caca_refresh_display(dp);
  38. events = malloc(h * sizeof(caca_event_t));
  39. memset(events, 0, h * sizeof(caca_event_t));
  40. for(quit = 0; quit < 4; )
  41. {
  42. caca_event_t ev;
  43. static char const * quit_string[] = { "", "q", "qu", "qui", "quit" };
  44. int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, -1);
  45. if(!ret)
  46. continue;
  47. do
  48. {
  49. /* "quit" quits */
  50. if(ev.type & CACA_EVENT_KEY_PRESS)
  51. {
  52. int key = ev.data.key.ch;
  53. if((key == 'q' && quit == 0) || (key == 'u' && quit == 1)
  54. || (key == 'i' && quit == 2) || (key == 't' && quit == 3))
  55. quit++;
  56. else if(key == 'q')
  57. quit = 1;
  58. else
  59. quit = 0;
  60. }
  61. memmove(events + 1, events, (h - 1) * sizeof(caca_event_t));
  62. events[0] = ev;
  63. ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0);
  64. }
  65. while(ret);
  66. cucul_set_color(cv, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK);
  67. cucul_clear_canvas(cv);
  68. /* Print current event */
  69. cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
  70. cucul_draw_line(cv, 0, 0, cucul_get_canvas_width(cv) - 1, 0, " ");
  71. print_event(0, 0, events);
  72. cucul_draw_line(cv, 0, h, cucul_get_canvas_width(cv) - 1, h, " ");
  73. cucul_printf(cv, 0, h, "type \"quit\" to exit: %s", quit_string[quit]);
  74. /* Print previous events */
  75. cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLACK);
  76. for(i = 1; i < h && events[i].type; i++)
  77. print_event(0, i, events + i);
  78. caca_refresh_display(dp);
  79. }
  80. /* Clean up */
  81. caca_free_display(dp);
  82. cucul_free_canvas(cv);
  83. return 0;
  84. }
  85. static void print_event(int x, int y, caca_event_t *ev)
  86. {
  87. int character;
  88. switch(ev->type)
  89. {
  90. case CACA_EVENT_NONE:
  91. cucul_printf(cv, x, y, "CACA_EVENT_NONE");
  92. break;
  93. case CACA_EVENT_KEY_PRESS:
  94. character = ev->data.key.ch;
  95. cucul_printf(cv, x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%cv)", character,
  96. (character > 0x1f && character < 0x80) ? character : '?');
  97. break;
  98. case CACA_EVENT_KEY_RELEASE:
  99. character = ev->data.key.ch;
  100. cucul_printf(cv, x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%cv)", character,
  101. (character > 0x1f && character < 0x80) ? character : '?');
  102. break;
  103. case CACA_EVENT_MOUSE_MOTION:
  104. cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_MOTION %u %u",
  105. ev->data.mouse.x, ev->data.mouse.y);
  106. break;
  107. case CACA_EVENT_MOUSE_PRESS:
  108. cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_PRESS %u",
  109. ev->data.mouse.button);
  110. break;
  111. case CACA_EVENT_MOUSE_RELEASE:
  112. cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_RELEASE %u",
  113. ev->data.mouse.button);
  114. break;
  115. case CACA_EVENT_RESIZE:
  116. cucul_printf(cv, x, y, "CACA_EVENT_RESIZE %u %u",
  117. ev->data.resize.w, ev->data.resize.h);
  118. break;
  119. case CACA_EVENT_QUIT:
  120. cucul_printf(cv, x, y, "CACA_EVENT_QUIT");
  121. break;
  122. default:
  123. cucul_printf(cv, x, y, "CACA_EVENT_UNKNOWN");
  124. }
  125. }