25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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