Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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