Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

263 rader
7.4 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_t *, caca_event_t *);
  25. static int _lowlevel_event(caca_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 kk 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_t *kk, 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(kk, 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 kk The libcaca graphical context.
  96. * \return The X mouse coordinate.
  97. */
  98. unsigned int caca_get_mouse_x(caca_t *kk)
  99. {
  100. if(kk->mouse.x >= kk->qq->width)
  101. kk->mouse.x = kk->qq->width - 1;
  102. return kk->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 kk The libcaca graphical context.
  112. * \return The Y mouse coordinate.
  113. */
  114. unsigned int caca_get_mouse_y(caca_t *kk)
  115. {
  116. if(kk->mouse.y >= kk->qq->height)
  117. kk->mouse.y = kk->qq->height - 1;
  118. return kk->mouse.y;
  119. }
  120. /*
  121. * XXX: The following functions are local.
  122. */
  123. static int _get_next_event(caca_t *kk, 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(kk->resize.resized)
  131. {
  132. kk->resize.resized = 0;
  133. _caca_handle_resize(kk);
  134. ev->type = CACA_EVENT_RESIZE;
  135. ev->data.resize.w = kk->qq->width;
  136. ev->data.resize.h = kk->qq->height;
  137. return 1;
  138. }
  139. ret = _lowlevel_event(kk, ev);
  140. #if defined(USE_SLANG)
  141. if(kk->drv.driver != CACA_DRIVER_SLANG)
  142. #endif
  143. #if defined(USE_NCURSES)
  144. if(kk->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(&kk->events.key_timer);
  150. kk->events.last_key_ticks += ticks;
  151. kk->events.autorepeat_ticks += ticks;
  152. /* Handle autorepeat */
  153. if(kk->events.last_key_event.type
  154. && kk->events.autorepeat_ticks > AUTOREPEAT_TRIGGER
  155. && kk->events.autorepeat_ticks > AUTOREPEAT_THRESHOLD
  156. && kk->events.autorepeat_ticks > AUTOREPEAT_RATE)
  157. {
  158. _push_event(kk, ev);
  159. kk->events.autorepeat_ticks -= AUTOREPEAT_RATE;
  160. *ev = kk->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. && kk->events.last_key_event.type
  167. && ev->data.key.c == kk->events.last_key_event.data.key.c
  168. && ev->data.key.ucs4 == kk->events.last_key_event.data.key.ucs4)
  169. {
  170. kk->events.last_key_ticks = 0;
  171. return _get_next_event(kk, 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(kk->events.last_key_event.type
  176. && (kk->events.last_key_ticks > AUTOREPEAT_THRESHOLD
  177. || (ev->type & CACA_EVENT_KEY_PRESS)))
  178. {
  179. _push_event(kk, ev);
  180. *ev = kk->events.last_key_event;
  181. ev->type = CACA_EVENT_KEY_RELEASE;
  182. kk->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. kk->events.last_key_ticks = 0;
  189. kk->events.autorepeat_ticks = 0;
  190. kk->events.last_key_event = *ev;
  191. }
  192. return ev->type ? 1 : 0;
  193. #endif
  194. }
  195. static int _lowlevel_event(caca_t *kk, caca_event_t *ev)
  196. {
  197. #if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO)
  198. int ret = _pop_event(kk, ev);
  199. if(ret)
  200. return ret;
  201. #endif
  202. return kk->drv.get_event(kk, ev);
  203. }
  204. #if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL)
  205. void _push_event(caca_t *kk, caca_event_t *ev)
  206. {
  207. if(!ev->type || kk->events.queue == EVENTBUF_LEN)
  208. return;
  209. kk->events.buf[kk->events.queue] = *ev;
  210. kk->events.queue++;
  211. }
  212. int _pop_event(caca_t *kk, caca_event_t *ev)
  213. {
  214. int i;
  215. if(kk->events.queue == 0)
  216. return 0;
  217. *ev = kk->events.buf[0];
  218. for(i = 1; i < kk->events.queue; i++)
  219. kk->events.buf[i - 1] = kk->events.buf[i];
  220. kk->events.queue--;
  221. return 1;
  222. }
  223. #endif