You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

470 lines
14 KiB

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