You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

241 line
6.7 KiB

  1. /*
  2. * libcaca ASCII-Art library
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the Do What The Fuck You Want To
  8. * Public License, Version 2, as published by Sam Hocevar. See
  9. * http://sam.zoy.org/wtfpl/COPYING for more details.
  10. */
  11. /** \file event.c
  12. * \version \$Id$
  13. * \author Sam Hocevar <sam@zoy.org>
  14. * \brief Event handling
  15. *
  16. * This file contains event handling functions for keyboard and mouse input.
  17. */
  18. #include "config.h"
  19. #include "cucul.h"
  20. #include "cucul_internals.h"
  21. #include "caca.h"
  22. #include "caca_internals.h"
  23. static unsigned int _get_next_event(caca_t *);
  24. static unsigned int _lowlevel_event(caca_t *);
  25. #if !defined(_DOXYGEN_SKIP_ME)
  26. /* If no new key was pressed after AUTOREPEAT_THRESHOLD usec, assume the
  27. * key was released */
  28. #define AUTOREPEAT_THRESHOLD 200000
  29. /* Start repeating key after AUTOREPEAT_TRIGGER usec and send keypress
  30. * events every AUTOREPEAT_RATE usec. */
  31. #define AUTOREPEAT_TRIGGER 300000
  32. #define AUTOREPEAT_RATE 100000
  33. #endif
  34. /** \brief Get the next mouse or keyboard input event.
  35. *
  36. * This function polls the event queue for mouse or keyboard events matching
  37. * the event mask and returns the first matching event. Non-matching events
  38. * are discarded. \c event_mask must have a non-zero value. This function is
  39. * non-blocking and returns zero if no more events are pending in the queue.
  40. * See also caca_wait_event() for a blocking version of this function.
  41. *
  42. * \param event_mask Bitmask of requested events.
  43. * \return The next matching event in the queue, or 0 if no event is pending.
  44. */
  45. unsigned int caca_get_event(caca_t *kk, unsigned int event_mask)
  46. {
  47. if(!event_mask)
  48. return CACA_EVENT_NONE;
  49. for( ; ; )
  50. {
  51. unsigned int event = _get_next_event(kk);
  52. if(!event || event & event_mask)
  53. return event;
  54. }
  55. }
  56. /** \brief Wait for the next mouse or keyboard input event.
  57. *
  58. * This function returns the first mouse or keyboard event in the queue
  59. * that matches the event mask. If no event is pending, it blocks until a
  60. * matching event is received. \c event_mask must have a non-zero value.
  61. * See also caca_get_event() for a non-blocking version of this function.
  62. *
  63. * \param event_mask Bitmask of requested events.
  64. * \return The next event in the queue.
  65. */
  66. unsigned int caca_wait_event(caca_t *kk, unsigned int event_mask)
  67. {
  68. if(!event_mask)
  69. return CACA_EVENT_NONE;
  70. for( ; ; )
  71. {
  72. unsigned int event = _get_next_event(kk);
  73. if(event & event_mask)
  74. return event;
  75. _caca_sleep(10000);
  76. }
  77. }
  78. /** \brief Return the X mouse coordinate.
  79. *
  80. * This function returns the X coordinate of the mouse position last time
  81. * it was detected. This function is not reliable if the ncurses or S-Lang
  82. * drivers are being used, because mouse position is only detected when
  83. * the mouse is clicked. Other drivers such as X11 work well.
  84. *
  85. * \return The X mouse coordinate.
  86. */
  87. unsigned int caca_get_mouse_x(caca_t *kk)
  88. {
  89. if(kk->mouse.x >= kk->qq->width)
  90. kk->mouse.x = kk->qq->width - 1;
  91. return kk->mouse.x;
  92. }
  93. /** \brief Return the Y mouse coordinate.
  94. *
  95. * This function returns the Y coordinate of the mouse position last time
  96. * it was detected. This function is not reliable if the ncurses or S-Lang
  97. * drivers are being used, because mouse position is only detected when
  98. * the mouse is clicked. Other drivers such as X11 work well.
  99. *
  100. * \return The Y mouse coordinate.
  101. */
  102. unsigned int caca_get_mouse_y(caca_t *kk)
  103. {
  104. if(kk->mouse.y >= kk->qq->height)
  105. kk->mouse.y = kk->qq->height - 1;
  106. return kk->mouse.y;
  107. }
  108. /*
  109. * XXX: The following functions are local.
  110. */
  111. static unsigned int _get_next_event(caca_t *kk)
  112. {
  113. #if defined(USE_SLANG) || defined(USE_NCURSES)
  114. unsigned int ticks;
  115. #endif
  116. unsigned int event;
  117. /* If we are about to return a resize event, acknowledge it */
  118. if(kk->resize.resized)
  119. {
  120. kk->resize.resized = 0;
  121. _caca_handle_resize(kk);
  122. return CACA_EVENT_RESIZE;
  123. }
  124. event = _lowlevel_event(kk);
  125. #if defined(USE_SLANG)
  126. if(kk->drv.driver != CACA_DRIVER_SLANG)
  127. #endif
  128. #if defined(USE_NCURSES)
  129. if(kk->drv.driver != CACA_DRIVER_NCURSES)
  130. #endif
  131. return event;
  132. #if defined(USE_SLANG) || defined(USE_NCURSES)
  133. /* Simulate long keypresses using autorepeat features */
  134. ticks = _caca_getticks(&kk->events.key_timer);
  135. kk->events.last_key_ticks += ticks;
  136. kk->events.autorepeat_ticks += ticks;
  137. /* Handle autorepeat */
  138. if(kk->events.last_key
  139. && kk->events.autorepeat_ticks > AUTOREPEAT_TRIGGER
  140. && kk->events.autorepeat_ticks > AUTOREPEAT_THRESHOLD
  141. && kk->events.autorepeat_ticks > AUTOREPEAT_RATE)
  142. {
  143. _push_event(kk, event);
  144. kk->events.autorepeat_ticks -= AUTOREPEAT_RATE;
  145. return CACA_EVENT_KEY_PRESS | kk->events.last_key;
  146. }
  147. /* We are in autorepeat mode and the same key was just pressed, ignore
  148. * this event and return the next one by calling ourselves. */
  149. if(event == (CACA_EVENT_KEY_PRESS | kk->events.last_key))
  150. {
  151. kk->events.last_key_ticks = 0;
  152. return _get_next_event(kk);
  153. }
  154. /* We are in autorepeat mode, but key has expired or a new key was
  155. * pressed - store our event and return a key release event first */
  156. if(kk->events.last_key
  157. && (kk->events.last_key_ticks > AUTOREPEAT_THRESHOLD
  158. || (event & CACA_EVENT_KEY_PRESS)))
  159. {
  160. _push_event(kk, event);
  161. event = CACA_EVENT_KEY_RELEASE | kk->events.last_key;
  162. kk->events.last_key = 0;
  163. return event;
  164. }
  165. /* A new key was pressed, enter autorepeat mode */
  166. if(event & CACA_EVENT_KEY_PRESS)
  167. {
  168. kk->events.last_key_ticks = 0;
  169. kk->events.autorepeat_ticks = 0;
  170. kk->events.last_key = event & 0x00ffffff;
  171. }
  172. return event;
  173. #endif
  174. }
  175. static unsigned int _lowlevel_event(caca_t *kk)
  176. {
  177. #if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO)
  178. unsigned int event = _pop_event(kk);
  179. if(event)
  180. return event;
  181. #endif
  182. return kk->drv.get_event(kk);
  183. }
  184. #if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO)
  185. void _push_event(caca_t *kk, unsigned int event)
  186. {
  187. if(!event || kk->events.queue == EVENTBUF_LEN)
  188. return;
  189. kk->events.buf[kk->events.queue] = event;
  190. kk->events.queue++;
  191. }
  192. unsigned int _pop_event(caca_t *kk)
  193. {
  194. int i;
  195. unsigned int event;
  196. if(kk->events.queue == 0)
  197. return CACA_EVENT_NONE;
  198. event = kk->events.buf[0];
  199. for(i = 1; i < kk->events.queue; i++)
  200. kk->events.buf[i - 1] = kk->events.buf[i];
  201. kk->events.queue--;
  202. return event;
  203. }
  204. #endif