Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

event.c 7.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library 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. /*
  14. * This file contains event handling functions for keyboard and mouse input.
  15. */
  16. #include "config.h"
  17. #if !defined(__KERNEL__)
  18. # include <stdio.h>
  19. #endif
  20. #include "cucul.h"
  21. #include "cucul_internals.h"
  22. #include "caca.h"
  23. #include "caca_internals.h"
  24. static int _get_next_event(caca_display_t *, caca_event_t *);
  25. static int _lowlevel_event(caca_display_t *, caca_event_t *);
  26. #if !defined(_DOXYGEN_SKIP_ME)
  27. /* If no new key was pressed after AUTOREPEAT_THRESHOLD usec, assume the
  28. * key was released */
  29. #define AUTOREPEAT_THRESHOLD 200000
  30. /* Start repeating key after AUTOREPEAT_TRIGGER usec and send keypress
  31. * events every AUTOREPEAT_RATE usec. */
  32. #define AUTOREPEAT_TRIGGER 300000
  33. #define AUTOREPEAT_RATE 100000
  34. #endif
  35. /** \brief Get the next mouse or keyboard input event.
  36. *
  37. * This function polls the event queue for mouse or keyboard events matching
  38. * the event mask and returns the first matching event. Non-matching events
  39. * are discarded. \c event_mask must have a non-zero value.
  40. *
  41. * The timeout value tells how long this function needs to wait for an
  42. * event. A value of zero returns immediately and the function returns zero
  43. * if no more events are pending in the queue. A negative value causes the
  44. * function to wait indefinitely until a matching event is received.
  45. *
  46. * \param dp The libcaca graphical context.
  47. * \param event_mask Bitmask of requested events.
  48. * \param timeout A timeout value in microseconds
  49. * \param ev A pointer to a caca_event structure.
  50. * \return The next matching event in the queue, or 0 if no event is pending.
  51. */
  52. int caca_get_event(caca_display_t *dp, unsigned int event_mask,
  53. caca_event_t *ev, int timeout)
  54. {
  55. caca_timer_t timer;
  56. int usec = 0;
  57. if(!event_mask)
  58. return 0;
  59. if(timeout > 0)
  60. _caca_getticks(&timer);
  61. for( ; ; )
  62. {
  63. int ret = _get_next_event(dp, ev);
  64. /* If we got the event we wanted, return */
  65. if(ev->type & event_mask)
  66. return ret;
  67. /* If there is no timeout, sleep and try again. */
  68. if(timeout < 0)
  69. {
  70. _caca_sleep(10000);
  71. continue;
  72. }
  73. /* If we timeouted, return an empty event */
  74. if(usec >= timeout)
  75. {
  76. ev->type = CACA_EVENT_NONE;
  77. return 0;
  78. }
  79. /* Otherwise sleep a bit. Our granularity is far too high for values
  80. * below 10000 microseconds so we cheat a bit. */
  81. if(usec > 10000)
  82. _caca_sleep(10000);
  83. else
  84. _caca_sleep(1000);
  85. usec += _caca_getticks(&timer);
  86. }
  87. }
  88. /** \brief Return the X mouse coordinate.
  89. *
  90. * This function returns the X coordinate of the mouse position last time
  91. * it was detected. This function is not reliable if the ncurses or S-Lang
  92. * drivers are being used, because mouse position is only detected when
  93. * the mouse is clicked. Other drivers such as X11 work well.
  94. *
  95. * \param dp The libcaca graphical context.
  96. * \return The X mouse coordinate.
  97. */
  98. unsigned int caca_get_mouse_x(caca_display_t *dp)
  99. {
  100. if(dp->mouse.x >= dp->cv->width)
  101. dp->mouse.x = dp->cv->width - 1;
  102. return dp->mouse.x;
  103. }
  104. /** \brief Return the Y mouse coordinate.
  105. *
  106. * This function returns the Y coordinate of the mouse position last time
  107. * it was detected. This function is not reliable if the ncurses or S-Lang
  108. * drivers are being used, because mouse position is only detected when
  109. * the mouse is clicked. Other drivers such as X11 work well.
  110. *
  111. * \param dp The libcaca graphical context.
  112. * \return The Y mouse coordinate.
  113. */
  114. unsigned int caca_get_mouse_y(caca_display_t *dp)
  115. {
  116. if(dp->mouse.y >= dp->cv->height)
  117. dp->mouse.y = dp->cv->height - 1;
  118. return dp->mouse.y;
  119. }
  120. /*
  121. * XXX: The following functions are local.
  122. */
  123. static int _get_next_event(caca_display_t *dp, caca_event_t *ev)
  124. {
  125. #if defined(USE_SLANG) || defined(USE_NCURSES)
  126. unsigned int ticks;
  127. #endif
  128. int ret;
  129. /* If we are about to return a resize event, acknowledge it */
  130. if(dp->resize.resized)
  131. {
  132. dp->resize.resized = 0;
  133. _caca_handle_resize(dp);
  134. ev->type = CACA_EVENT_RESIZE;
  135. ev->data.resize.w = dp->cv->width;
  136. ev->data.resize.h = dp->cv->height;
  137. return 1;
  138. }
  139. ret = _lowlevel_event(dp, ev);
  140. #if defined(USE_SLANG)
  141. if(dp->drv.driver != CACA_DRIVER_SLANG)
  142. #endif
  143. #if defined(USE_NCURSES)
  144. if(dp->drv.driver != CACA_DRIVER_NCURSES)
  145. #endif
  146. return ret;
  147. #if defined(USE_SLANG) || defined(USE_NCURSES)
  148. /* Simulate long keypresses using autorepeat features */
  149. ticks = _caca_getticks(&dp->events.key_timer);
  150. dp->events.last_key_ticks += ticks;
  151. dp->events.autorepeat_ticks += ticks;
  152. /* Handle autorepeat */
  153. if(dp->events.last_key_event.type
  154. && dp->events.autorepeat_ticks > AUTOREPEAT_TRIGGER
  155. && dp->events.autorepeat_ticks > AUTOREPEAT_THRESHOLD
  156. && dp->events.autorepeat_ticks > AUTOREPEAT_RATE)
  157. {
  158. _push_event(dp, ev);
  159. dp->events.autorepeat_ticks -= AUTOREPEAT_RATE;
  160. *ev = dp->events.last_key_event;
  161. return 1;
  162. }
  163. /* We are in autorepeat mode and the same key was just pressed, ignore
  164. * this event and return the next one by calling ourselves. */
  165. if(ev->type == CACA_EVENT_KEY_PRESS
  166. && dp->events.last_key_event.type
  167. && ev->data.key.ch == dp->events.last_key_event.data.key.ch
  168. && ev->data.key.ucs4 == dp->events.last_key_event.data.key.ucs4)
  169. {
  170. dp->events.last_key_ticks = 0;
  171. return _get_next_event(dp, ev);
  172. }
  173. /* We are in autorepeat mode, but key has expired or a new key was
  174. * pressed - store our event and return a key release event first */
  175. if(dp->events.last_key_event.type
  176. && (dp->events.last_key_ticks > AUTOREPEAT_THRESHOLD
  177. || (ev->type & CACA_EVENT_KEY_PRESS)))
  178. {
  179. _push_event(dp, ev);
  180. *ev = dp->events.last_key_event;
  181. ev->type = CACA_EVENT_KEY_RELEASE;
  182. dp->events.last_key_event.type = CACA_EVENT_NONE;
  183. return 1;
  184. }
  185. /* A new key was pressed, enter autorepeat mode */
  186. if(ev->type & CACA_EVENT_KEY_PRESS)
  187. {
  188. dp->events.last_key_ticks = 0;
  189. dp->events.autorepeat_ticks = 0;
  190. dp->events.last_key_event = *ev;
  191. }
  192. return ev->type ? 1 : 0;
  193. #endif
  194. }
  195. static int _lowlevel_event(caca_display_t *dp, caca_event_t *ev)
  196. {
  197. #if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO)
  198. int ret = _pop_event(dp, ev);
  199. if(ret)
  200. return ret;
  201. #endif
  202. return dp->drv.get_event(dp, ev);
  203. }
  204. #if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL)
  205. void _push_event(caca_display_t *dp, caca_event_t *ev)
  206. {
  207. if(!ev->type || dp->events.queue == EVENTBUF_LEN)
  208. return;
  209. dp->events.buf[dp->events.queue] = *ev;
  210. dp->events.queue++;
  211. }
  212. int _pop_event(caca_display_t *dp, caca_event_t *ev)
  213. {
  214. int i;
  215. if(dp->events.queue == 0)
  216. return 0;
  217. *ev = dp->events.buf[0];
  218. for(i = 1; i < dp->events.queue; i++)
  219. dp->events.buf[i - 1] = dp->events.buf[i];
  220. dp->events.queue--;
  221. return 1;
  222. }
  223. #endif