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.
 
 
 
 
 
 

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