Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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