您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

278 行
7.9 KiB

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