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.
 
 
 
 
 
 

446 líneas
14 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. 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. * \param ev The libcaca event.
  226. * \return The event's mouse button.
  227. */
  228. int caca_get_event_mouse_button(caca_event_t const *ev)
  229. {
  230. return ((caca_privevent_t const *)ev)->data.mouse.button;
  231. }
  232. /** \brief Return a mouse motion event's X coordinate.
  233. *
  234. * Return the X coordinate for a mouse motion event.
  235. *
  236. * This function never fails, but must only be called with a valid event of
  237. * type \c CACA_EVENT_MOUSE_MOTION, or the results will be undefined. See
  238. * caca_get_event_type() for more information.
  239. *
  240. * \param ev The libcaca event.
  241. * \return The event's X mouse coordinate.
  242. */
  243. int caca_get_event_mouse_x(caca_event_t const *ev)
  244. {
  245. return ((caca_privevent_t const *)ev)->data.mouse.x;
  246. }
  247. /** \brief Return a mouse motion event's Y coordinate.
  248. *
  249. * Return the Y coordinate for a mouse motion event.
  250. *
  251. * This function never fails, but must only be called with a valid event of
  252. * type \c CACA_EVENT_MOUSE_MOTION, or the results will be undefined. See
  253. * caca_get_event_type() for more information.
  254. *
  255. * \param ev The libcaca event.
  256. * \return The event's Y mouse coordinate.
  257. */
  258. int caca_get_event_mouse_y(caca_event_t const *ev)
  259. {
  260. return ((caca_privevent_t const *)ev)->data.mouse.y;
  261. }
  262. /** \brief Return a resize event's display width value.
  263. *
  264. * Return the width value for a display resize event.
  265. *
  266. * This function never fails, but must only be called with a valid event of
  267. * type \c CACA_EVENT_RESIZE, or the results will be undefined. See
  268. * caca_get_event_type() for more information.
  269. *
  270. * \param ev The libcaca event.
  271. * \return The event's new display width value.
  272. */
  273. int caca_get_event_resize_width(caca_event_t const *ev)
  274. {
  275. return ((caca_privevent_t const *)ev)->data.resize.w;
  276. }
  277. /** \brief Return a resize event's display height value.
  278. *
  279. * Return the height value for a display resize event.
  280. *
  281. * This function never fails, but must only be called with a valid event of
  282. * type \c CACA_EVENT_RESIZE, or the results will be undefined. See
  283. * caca_get_event_type() for more information.
  284. *
  285. * \param ev The libcaca event.
  286. * \return The event's new display height value.
  287. */
  288. int caca_get_event_resize_height(caca_event_t const *ev)
  289. {
  290. return ((caca_privevent_t const *)ev)->data.resize.h;
  291. }
  292. /*
  293. * XXX: The following functions are local.
  294. */
  295. static int _get_next_event(caca_display_t *dp, caca_privevent_t *ev)
  296. {
  297. #if defined(USE_SLANG) || defined(USE_NCURSES)
  298. int ticks;
  299. #endif
  300. int ret;
  301. /* If we are about to return a resize event, acknowledge it */
  302. if(dp->resize.resized)
  303. {
  304. dp->resize.resized = 0;
  305. _caca_handle_resize(dp);
  306. ev->type = CACA_EVENT_RESIZE;
  307. ev->data.resize.w = caca_get_canvas_width(dp->cv);
  308. ev->data.resize.h = caca_get_canvas_height(dp->cv);
  309. return 1;
  310. }
  311. ret = _lowlevel_event(dp, ev);
  312. #if defined(USE_SLANG)
  313. if(dp->drv.id != CACA_DRIVER_SLANG)
  314. #endif
  315. #if defined(USE_NCURSES)
  316. if(dp->drv.id != CACA_DRIVER_NCURSES)
  317. #endif
  318. return ret;
  319. #if defined(USE_SLANG) || defined(USE_NCURSES)
  320. /* Simulate long keypresses using autorepeat features */
  321. ticks = _caca_getticks(&dp->events.key_timer);
  322. dp->events.last_key_ticks += ticks;
  323. dp->events.autorepeat_ticks += ticks;
  324. /* Handle autorepeat */
  325. if(dp->events.last_key_event.type
  326. && dp->events.autorepeat_ticks > AUTOREPEAT_TRIGGER
  327. && dp->events.autorepeat_ticks > AUTOREPEAT_THRESHOLD
  328. && dp->events.autorepeat_ticks > AUTOREPEAT_RATE)
  329. {
  330. _push_event(dp, ev);
  331. dp->events.autorepeat_ticks -= AUTOREPEAT_RATE;
  332. *ev = dp->events.last_key_event;
  333. return 1;
  334. }
  335. /* We are in autorepeat mode and the same key was just pressed, ignore
  336. * this event and return the next one by calling ourselves. */
  337. if(ev->type == CACA_EVENT_KEY_PRESS
  338. && dp->events.last_key_event.type
  339. && ev->data.key.ch == dp->events.last_key_event.data.key.ch
  340. && ev->data.key.utf32 == dp->events.last_key_event.data.key.utf32)
  341. {
  342. dp->events.last_key_ticks = 0;
  343. return _get_next_event(dp, ev);
  344. }
  345. /* We are in autorepeat mode, but key has expired or a new key was
  346. * pressed - store our event and return a key release event first */
  347. if(dp->events.last_key_event.type
  348. && (dp->events.last_key_ticks > AUTOREPEAT_THRESHOLD
  349. || (ev->type & CACA_EVENT_KEY_PRESS)))
  350. {
  351. _push_event(dp, ev);
  352. *ev = dp->events.last_key_event;
  353. ev->type = CACA_EVENT_KEY_RELEASE;
  354. dp->events.last_key_event.type = CACA_EVENT_NONE;
  355. return 1;
  356. }
  357. /* A new key was pressed, enter autorepeat mode */
  358. if(ev->type & CACA_EVENT_KEY_PRESS)
  359. {
  360. dp->events.last_key_ticks = 0;
  361. dp->events.autorepeat_ticks = 0;
  362. dp->events.last_key_event = *ev;
  363. }
  364. return ev->type ? 1 : 0;
  365. #endif
  366. }
  367. static int _lowlevel_event(caca_display_t *dp, caca_privevent_t *ev)
  368. {
  369. #if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO)
  370. int ret = _pop_event(dp, ev);
  371. if(ret)
  372. return ret;
  373. #endif
  374. return dp->drv.get_event(dp, ev);
  375. }
  376. #if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL)
  377. void _push_event(caca_display_t *dp, caca_privevent_t *ev)
  378. {
  379. if(!ev->type || dp->events.queue == EVENTBUF_LEN)
  380. return;
  381. dp->events.buf[dp->events.queue] = *ev;
  382. dp->events.queue++;
  383. }
  384. int _pop_event(caca_display_t *dp, caca_privevent_t *ev)
  385. {
  386. int i;
  387. if(dp->events.queue == 0)
  388. return 0;
  389. *ev = dp->events.buf[0];
  390. for(i = 1; i < dp->events.queue; i++)
  391. dp->events.buf[i - 1] = dp->events.buf[i];
  392. dp->events.queue--;
  393. return 1;
  394. }
  395. #endif