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.
 
 
 
 
 
 

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