Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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