No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * This file contains event handling functions for keyboard and mouse input.
  16. */
  17. #include "config.h"
  18. #if !defined(__KERNEL__)
  19. # include <stdio.h>
  20. # include <string.h>
  21. #endif
  22. #include "caca.h"
  23. #include "caca_internals.h"
  24. static int _get_next_event(caca_display_t *, caca_privevent_t *);
  25. static int _lowlevel_event(caca_display_t *, caca_privevent_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 100000
  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 20000
  34. #endif
  35. /** \brief Get the next mouse or keyboard input event.
  36. *
  37. * Poll the event queue for mouse or keyboard events matching the event
  38. * mask and return the first matching event. Non-matching events are
  39. * discarded. If \c event_mask is zero, the function returns immediately.
  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. * This function never fails.
  51. *
  52. * \param dp The libcaca graphical context.
  53. * \param event_mask Bitmask of requested events.
  54. * \param timeout A timeout value in microseconds, -1 for blocking behaviour
  55. * \param ev A pointer to a caca_event structure, or NULL.
  56. * \return 1 if a matching event was received, or 0 if the wait timeouted.
  57. */
  58. int caca_get_event(caca_display_t *dp, int event_mask,
  59. caca_event_t *ev, int timeout)
  60. {
  61. caca_privevent_t privevent;
  62. caca_timer_t timer = {0, 0};
  63. int usec = 0;
  64. if(!event_mask)
  65. return 0;
  66. if(timeout > 0)
  67. _caca_getticks(&timer);
  68. for( ; ; )
  69. {
  70. int ret = _get_next_event(dp, &privevent);
  71. /* If we got the event we wanted, return */
  72. if(privevent.type & event_mask)
  73. {
  74. if(ev)
  75. memcpy(ev, &privevent, sizeof(privevent));
  76. return ret;
  77. }
  78. /* If there is no timeout, sleep and try again. */
  79. if(timeout < 0)
  80. {
  81. _caca_sleep(1000);
  82. continue;
  83. }
  84. /* If we timeouted, return an empty event */
  85. if(usec >= timeout)
  86. {
  87. privevent.type = CACA_EVENT_NONE;
  88. if(ev)
  89. memcpy(ev, &privevent, sizeof(privevent));
  90. return 0;
  91. }
  92. /* Otherwise sleep a bit. Our granularity is far too high for values
  93. * below 10000 microseconds so we cheat a bit. */
  94. if(usec > 10000)
  95. _caca_sleep(10000);
  96. else
  97. _caca_sleep(1000);
  98. usec += _caca_getticks(&timer);
  99. }
  100. }
  101. /** \brief Return the X mouse coordinate.
  102. *
  103. * Return the X 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. * This function never fails.
  109. *
  110. * \param dp The libcaca graphical context.
  111. * \return The X mouse coordinate.
  112. */
  113. int caca_get_mouse_x(caca_display_t const *dp)
  114. {
  115. int width = caca_get_canvas_width(dp->cv);
  116. if(dp->mouse.x >= width)
  117. return width - 1;
  118. return dp->mouse.x;
  119. }
  120. /** \brief Return the Y mouse coordinate.
  121. *
  122. * Return the Y coordinate of the mouse position last time
  123. * it was detected. This function is not reliable if the ncurses or S-Lang
  124. * drivers are being used, because mouse position is only detected when
  125. * the mouse is clicked. Other drivers such as X11 work well.
  126. *
  127. * This function never fails.
  128. *
  129. * \param dp The libcaca graphical context.
  130. * \return The Y mouse coordinate.
  131. */
  132. int caca_get_mouse_y(caca_display_t const *dp)
  133. {
  134. int height = caca_get_canvas_height(dp->cv);
  135. if(dp->mouse.y >= height)
  136. return height - 1;
  137. return dp->mouse.y;
  138. }
  139. /** \brief Return an event's type.
  140. *
  141. * Return the type of an event. This function may always be called on an
  142. * event after caca_get_event() was called, and its return value indicates
  143. * which other functions may be called:
  144. * - \c CACA_EVENT_NONE: no other function may be called.
  145. * - \c CACA_EVENT_KEY_PRESS, \c CACA_EVENT_KEY_RELEASE:
  146. * caca_get_event_key_ch(), caca_get_event_key_utf32() and
  147. * caca_get_event_key_utf8() may be called.
  148. * - \c CACA_EVENT_MOUSE_PRESS, \c CACA_EVENT_MOUSE_RELEASE:
  149. * caca_get_event_mouse_button() may be called.
  150. * - \c CACA_EVENT_MOUSE_MOTION: caca_get_event_mouse_x() and
  151. * caca_get_event_mouse_y() may be called.
  152. * - \c CACA_EVENT_RESIZE: caca_get_event_resize_width() and
  153. * caca_get_event_resize_height() may be called.
  154. * - \c CACA_EVENT_QUIT: no other function may be called.
  155. *
  156. * This function never fails.
  157. *
  158. * \param ev The libcaca event.
  159. * \return The event's type.
  160. */
  161. enum caca_event_type caca_get_event_type(caca_event_t const *ev)
  162. {
  163. return ((caca_privevent_t const *)ev)->type;
  164. }
  165. /** \brief Return a key press or key release event's value
  166. *
  167. * Return either the ASCII value for an event's key, or if the key is not
  168. * an ASCII character, an appropriate \e enum \e caca_key value.
  169. *
  170. * This function never fails, but must only be called with a valid event of
  171. * type \c CACA_EVENT_KEY_PRESS or \c CACA_EVENT_KEY_RELEASE, or the results
  172. * will be undefined. See caca_get_event_type() for more information.
  173. *
  174. * \param ev The libcaca event.
  175. * \return The key value.
  176. */
  177. int caca_get_event_key_ch(caca_event_t const *ev)
  178. {
  179. return ((caca_privevent_t const *)ev)->data.key.ch;
  180. }
  181. /** \brief Return a key press or key release event's Unicode value
  182. *
  183. * Return the UTF-32/UCS-4 value for an event's key if it resolves to a
  184. * printable character.
  185. *
  186. * This function never fails, but must only be called with a valid event of
  187. * type \c CACA_EVENT_KEY_PRESS or \c CACA_EVENT_KEY_RELEASE, or the results
  188. * will be undefined. See caca_get_event_type() for more information.
  189. *
  190. * \param ev The libcaca event.
  191. * \return The key's Unicode value.
  192. */
  193. uint32_t caca_get_event_key_utf32(caca_event_t const *ev)
  194. {
  195. return ((caca_privevent_t const *)ev)->data.key.utf32;
  196. }
  197. /** \brief Return a key press or key release event's UTF-8 value
  198. *
  199. * Write the UTF-8 value for an event's key if it resolves to a printable
  200. * character. Up to 6 UTF-8 bytes and a null termination are written.
  201. *
  202. * This function never fails, but must only be called with a valid event of
  203. * type \c CACA_EVENT_KEY_PRESS or \c CACA_EVENT_KEY_RELEASE, or the results
  204. * will be undefined. See caca_get_event_type() for more information.
  205. *
  206. * \param ev The libcaca event.
  207. * \param utf8 A string buffer with enough bytes to hold the pressed
  208. * key value in UTF-8. Though fewer bytes may be written to
  209. * it, 7 bytes is the minimum safe size.
  210. * \return This function always returns 0.
  211. */
  212. int caca_get_event_key_utf8(caca_event_t const *ev, char *utf8)
  213. {
  214. memcpy(utf8, ((caca_privevent_t const *)ev)->data.key.utf8, 8);
  215. return 0;
  216. }
  217. /** \brief Return a mouse press or mouse release event's button
  218. *
  219. * Return the mouse button index for an event.
  220. *
  221. * This function never fails, but must only be called with a valid event of
  222. * type \c CACA_EVENT_MOUSE_PRESS or \c CACA_EVENT_MOUSE_RELEASE, or the
  223. * results will be undefined. See caca_get_event_type() for more information.
  224. *
  225. * This function returns 1 for the left mouse button, 2 for the right mouse
  226. * button, and 3 for the middle mouse button.
  227. *
  228. * \param ev The libcaca event.
  229. * \return The event's mouse button.
  230. */
  231. int caca_get_event_mouse_button(caca_event_t const *ev)
  232. {
  233. return ((caca_privevent_t const *)ev)->data.mouse.button;
  234. }
  235. /** \brief Return a mouse motion event's X coordinate.
  236. *
  237. * Return the X coordinate for a mouse motion event.
  238. *
  239. * This function never fails, but must only be called with a valid event of
  240. * type \c CACA_EVENT_MOUSE_MOTION, or the results will be undefined. See
  241. * caca_get_event_type() for more information.
  242. *
  243. * \param ev The libcaca event.
  244. * \return The event's X mouse coordinate.
  245. */
  246. int caca_get_event_mouse_x(caca_event_t const *ev)
  247. {
  248. return ((caca_privevent_t const *)ev)->data.mouse.x;
  249. }
  250. /** \brief Return a mouse motion event's Y coordinate.
  251. *
  252. * Return the Y coordinate for a mouse motion event.
  253. *
  254. * This function never fails, but must only be called with a valid event of
  255. * type \c CACA_EVENT_MOUSE_MOTION, or the results will be undefined. See
  256. * caca_get_event_type() for more information.
  257. *
  258. * \param ev The libcaca event.
  259. * \return The event's Y mouse coordinate.
  260. */
  261. int caca_get_event_mouse_y(caca_event_t const *ev)
  262. {
  263. return ((caca_privevent_t const *)ev)->data.mouse.y;
  264. }
  265. /** \brief Return a resize event's display width value.
  266. *
  267. * Return the width value for a display resize event.
  268. *
  269. * This function never fails, but must only be called with a valid event of
  270. * type \c CACA_EVENT_RESIZE, or the results will be undefined. See
  271. * caca_get_event_type() for more information.
  272. *
  273. * \param ev The libcaca event.
  274. * \return The event's new display width value.
  275. */
  276. int caca_get_event_resize_width(caca_event_t const *ev)
  277. {
  278. return ((caca_privevent_t const *)ev)->data.resize.w;
  279. }
  280. /** \brief Return a resize event's display height value.
  281. *
  282. * Return the height value for a display resize event.
  283. *
  284. * This function never fails, but must only be called with a valid event of
  285. * type \c CACA_EVENT_RESIZE, or the results will be undefined. See
  286. * caca_get_event_type() for more information.
  287. *
  288. * \param ev The libcaca event.
  289. * \return The event's new display height value.
  290. */
  291. int caca_get_event_resize_height(caca_event_t const *ev)
  292. {
  293. return ((caca_privevent_t const *)ev)->data.resize.h;
  294. }
  295. /*
  296. * XXX: The following functions are local.
  297. */
  298. static int _get_next_event(caca_display_t *dp, caca_privevent_t *ev)
  299. {
  300. #if defined(USE_SLANG) || defined(USE_NCURSES)
  301. int ticks;
  302. #endif
  303. int ret;
  304. /* If we are about to return a resize event, acknowledge it */
  305. if(dp->resize.resized)
  306. {
  307. dp->resize.resized = 0;
  308. _caca_handle_resize(dp);
  309. ev->type = CACA_EVENT_RESIZE;
  310. ev->data.resize.w = caca_get_canvas_width(dp->cv);
  311. ev->data.resize.h = caca_get_canvas_height(dp->cv);
  312. return 1;
  313. }
  314. ret = _lowlevel_event(dp, ev);
  315. #if defined(USE_SLANG)
  316. if(dp->drv.id != CACA_DRIVER_SLANG)
  317. #endif
  318. #if defined(USE_NCURSES)
  319. if(dp->drv.id != CACA_DRIVER_NCURSES)
  320. #endif
  321. return ret;
  322. #if defined(USE_SLANG) || defined(USE_NCURSES)
  323. /* Simulate long keypresses using autorepeat features */
  324. ticks = _caca_getticks(&dp->events.key_timer);
  325. dp->events.last_key_ticks += ticks;
  326. dp->events.autorepeat_ticks += ticks;
  327. /* Handle autorepeat */
  328. if(dp->events.last_key_event.type
  329. && dp->events.autorepeat_ticks > AUTOREPEAT_TRIGGER
  330. && dp->events.autorepeat_ticks > AUTOREPEAT_THRESHOLD
  331. && dp->events.autorepeat_ticks > AUTOREPEAT_RATE)
  332. {
  333. _push_event(dp, ev);
  334. dp->events.autorepeat_ticks -= AUTOREPEAT_RATE;
  335. *ev = dp->events.last_key_event;
  336. return 1;
  337. }
  338. /* We are in autorepeat mode and the same key was just pressed, ignore
  339. * this event and return the next one by calling ourselves. */
  340. if(ev->type == CACA_EVENT_KEY_PRESS
  341. && dp->events.last_key_event.type
  342. && ev->data.key.ch == dp->events.last_key_event.data.key.ch
  343. && ev->data.key.utf32 == dp->events.last_key_event.data.key.utf32)
  344. {
  345. dp->events.last_key_ticks = 0;
  346. return _get_next_event(dp, ev);
  347. }
  348. /* We are in autorepeat mode, but key has expired or a new key was
  349. * pressed - store our event and return a key release event first */
  350. if(dp->events.last_key_event.type
  351. && (dp->events.last_key_ticks > AUTOREPEAT_THRESHOLD
  352. || (ev->type & CACA_EVENT_KEY_PRESS)))
  353. {
  354. _push_event(dp, ev);
  355. *ev = dp->events.last_key_event;
  356. ev->type = CACA_EVENT_KEY_RELEASE;
  357. dp->events.last_key_event.type = CACA_EVENT_NONE;
  358. return 1;
  359. }
  360. /* A new key was pressed, enter autorepeat mode */
  361. if(ev->type & CACA_EVENT_KEY_PRESS)
  362. {
  363. dp->events.last_key_ticks = 0;
  364. dp->events.autorepeat_ticks = 0;
  365. dp->events.last_key_event = *ev;
  366. }
  367. return ev->type ? 1 : 0;
  368. #endif
  369. }
  370. static int _lowlevel_event(caca_display_t *dp, caca_privevent_t *ev)
  371. {
  372. #if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO)
  373. int ret = _pop_event(dp, ev);
  374. if(ret)
  375. return ret;
  376. #endif
  377. return dp->drv.get_event(dp, ev);
  378. }
  379. #if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL)
  380. void _push_event(caca_display_t *dp, caca_privevent_t *ev)
  381. {
  382. if(!ev->type || dp->events.queue == EVENTBUF_LEN)
  383. return;
  384. dp->events.buf[dp->events.queue] = *ev;
  385. dp->events.queue++;
  386. }
  387. int _pop_event(caca_display_t *dp, caca_privevent_t *ev)
  388. {
  389. int i;
  390. if(dp->events.queue == 0)
  391. return 0;
  392. *ev = dp->events.buf[0];
  393. for(i = 1; i < dp->events.queue; i++)
  394. dp->events.buf[i - 1] = dp->events.buf[i];
  395. dp->events.queue--;
  396. return 1;
  397. }
  398. #endif