選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

event.c 14 KiB

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