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.
 
 
 
 
 
 

444 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 "cucul.h"
  23. #include "caca.h"
  24. #include "caca_internals.h"
  25. static int _get_next_event(caca_display_t *, caca_privevent_t *);
  26. static int _lowlevel_event(caca_display_t *, caca_privevent_t *);
  27. #if !defined(_DOXYGEN_SKIP_ME)
  28. /* If no new key was pressed after AUTOREPEAT_THRESHOLD usec, assume the
  29. * key was released */
  30. #define AUTOREPEAT_THRESHOLD 100000
  31. /* Start repeating key after AUTOREPEAT_TRIGGER usec and send keypress
  32. * events every AUTOREPEAT_RATE usec. */
  33. #define AUTOREPEAT_TRIGGER 300000
  34. #define AUTOREPEAT_RATE 100000
  35. #endif
  36. /** \brief Get the next mouse or keyboard input event.
  37. *
  38. * Poll the event queue for mouse or keyboard events matching the event
  39. * mask and return the first matching event. Non-matching events are
  40. * discarded. If \c event_mask is zero, the function returns immediately.
  41. *
  42. * The timeout value tells how long this function needs to wait for an
  43. * event. A value of zero returns immediately and the function returns zero
  44. * if no more events are pending in the queue. A negative value causes the
  45. * function to wait indefinitely until a matching event is received.
  46. *
  47. * If not null, \c ev will be filled with information about the event
  48. * received. If null, the function will return but no information about
  49. * the event will be sent.
  50. *
  51. * This function never fails.
  52. *
  53. * \param dp The libcaca graphical context.
  54. * \param event_mask Bitmask of requested events.
  55. * \param timeout A timeout value in microseconds, -1 for blocking behaviour
  56. * \param ev A pointer to a caca_event structure, or NULL.
  57. * \return 1 if a matching event was received, or 0 if the wait timeouted.
  58. */
  59. int caca_get_event(caca_display_t *dp, int event_mask,
  60. caca_event_t *ev, int timeout)
  61. {
  62. caca_privevent_t privevent;
  63. caca_timer_t timer = {0, 0};
  64. int usec = 0;
  65. if(!event_mask)
  66. return 0;
  67. if(timeout > 0)
  68. _caca_getticks(&timer);
  69. for( ; ; )
  70. {
  71. int ret = _get_next_event(dp, &privevent);
  72. /* If we got the event we wanted, return */
  73. if(privevent.type & event_mask)
  74. {
  75. if(ev)
  76. memcpy(ev, &privevent, sizeof(privevent));
  77. return ret;
  78. }
  79. /* If there is no timeout, sleep and try again. */
  80. if(timeout < 0)
  81. {
  82. _caca_sleep(10000);
  83. continue;
  84. }
  85. /* If we timeouted, return an empty event */
  86. if(usec >= timeout)
  87. {
  88. privevent.type = CACA_EVENT_NONE;
  89. if(ev)
  90. memcpy(ev, &privevent, sizeof(privevent));
  91. return 0;
  92. }
  93. /* Otherwise sleep a bit. Our granularity is far too high for values
  94. * below 10000 microseconds so we cheat a bit. */
  95. if(usec > 10000)
  96. _caca_sleep(10000);
  97. else
  98. _caca_sleep(1000);
  99. usec += _caca_getticks(&timer);
  100. }
  101. }
  102. /** \brief Return the X mouse coordinate.
  103. *
  104. * Return the X coordinate of the mouse position last time
  105. * it was detected. This function is not reliable if the ncurses or S-Lang
  106. * drivers are being used, because mouse position is only detected when
  107. * the mouse is clicked. Other drivers such as X11 work well.
  108. *
  109. * This function never fails.
  110. *
  111. * \param dp The libcaca graphical context.
  112. * \return The X mouse coordinate.
  113. */
  114. int caca_get_mouse_x(caca_display_t const *dp)
  115. {
  116. int width = cucul_get_canvas_width(dp->cv);
  117. if(dp->mouse.x >= width)
  118. return width - 1;
  119. return dp->mouse.x;
  120. }
  121. /** \brief Return the Y mouse coordinate.
  122. *
  123. * Return the Y coordinate of the mouse position last time
  124. * it was detected. This function is not reliable if the ncurses or S-Lang
  125. * drivers are being used, because mouse position is only detected when
  126. * the mouse is clicked. Other drivers such as X11 work well.
  127. *
  128. * This function never fails.
  129. *
  130. * \param dp The libcaca graphical context.
  131. * \return The Y mouse coordinate.
  132. */
  133. int caca_get_mouse_y(caca_display_t const *dp)
  134. {
  135. int height = cucul_get_canvas_height(dp->cv);
  136. if(dp->mouse.y >= height)
  137. return 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. 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. uint32_t 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. 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. 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. 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. 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. 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. 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 = cucul_get_canvas_width(dp->cv);
  306. ev->data.resize.h = cucul_get_canvas_height(dp->cv);
  307. return 1;
  308. }
  309. ret = _lowlevel_event(dp, ev);
  310. #if defined(USE_SLANG)
  311. if(dp->drv.id != CACA_DRIVER_SLANG)
  312. #endif
  313. #if defined(USE_NCURSES)
  314. if(dp->drv.id != 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