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ů.
 
 
 
 
 
 

355 řádky
11 KiB

  1. /*
  2. * libcaca ASCII-Art library
  3. * Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  19. * 02111-1307 USA
  20. */
  21. /** \file io.c
  22. * \version \$Id$
  23. * \author Sam Hocevar <sam@zoy.org>
  24. * \brief Event handling
  25. *
  26. * This file contains event handling functions for keyboard and mouse input.
  27. */
  28. #include "config.h"
  29. #if defined(USE_SLANG)
  30. # include <slang.h>
  31. #endif
  32. #if defined(USE_NCURSES)
  33. # include <curses.h>
  34. #endif
  35. #if defined(USE_CONIO)
  36. # include <conio.h>
  37. #endif
  38. #if defined(USE_X11)
  39. # include <X11/Xlib.h>
  40. # include <X11/Xutil.h>
  41. # include <X11/keysym.h>
  42. #endif
  43. #include <unistd.h>
  44. #include "caca.h"
  45. #include "caca_internals.h"
  46. static void _push_key(unsigned int);
  47. static unsigned int _pop_key(void);
  48. static unsigned int _read_key(void);
  49. #define KEY_BUFLEN 10
  50. static unsigned int keybuf[KEY_BUFLEN + 1]; /* zero-terminated */
  51. static int keys = 0;
  52. /** \brief Get the next mouse or keyboard input event.
  53. *
  54. * This function polls the event queue for mouse or keyboard events and
  55. * returns the event. It is non-blocking and returns zero if no event is
  56. * pending in the queue. See also caca_wait_event() for a blocking version
  57. * of this function.
  58. *
  59. * \return The next event in the queue, or 0 if no event is pending.
  60. */
  61. unsigned int caca_get_event(void)
  62. {
  63. unsigned int event = 0;
  64. #if defined(USE_X11)
  65. if(_caca_driver == CACA_DRIVER_X11)
  66. {
  67. XEvent xevent;
  68. static unsigned int x11_x = 0, x11_y = 0;
  69. long int xevent_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask
  70. | ButtonReleaseMask | PointerMotionMask;
  71. char key;
  72. while(XCheckWindowEvent(x11_dpy, x11_window, xevent_mask, &xevent)
  73. == True)
  74. {
  75. KeySym keysym;
  76. if(xevent.type == MotionNotify)
  77. {
  78. unsigned int newx = xevent.xmotion.x / x11_font_width;
  79. unsigned int newy = xevent.xmotion.y / x11_font_height;
  80. if(x11_x == newx && x11_y == newy)
  81. continue;
  82. if(newx >= _caca_width)
  83. newx = _caca_width - 1;
  84. if(newy >= _caca_height)
  85. newy = _caca_height - 1;
  86. x11_x = newx & 0xfff;
  87. x11_y = newy & 0xfff;
  88. return CACA_EVENT_MOUSE_MOTION | (newx << 12) | (newy << 0);
  89. }
  90. if(xevent.type == ButtonPress)
  91. return CACA_EVENT_MOUSE_PRESS | 1;
  92. if(xevent.type == ButtonPress)
  93. return CACA_EVENT_MOUSE_RELEASE | 1;
  94. if(xevent.type == KeyPress)
  95. event |= CACA_EVENT_KEY_PRESS;
  96. else if(xevent.type == KeyRelease)
  97. event |= CACA_EVENT_KEY_RELEASE;
  98. else
  99. return 0;
  100. if(XLookupString(&xevent.xkey, &key, 1, NULL, NULL))
  101. return event | key;
  102. keysym = XKeycodeToKeysym(x11_dpy, xevent.xkey.keycode, 0);
  103. switch(keysym)
  104. {
  105. case XK_F1: return event | CACA_KEY_F1;
  106. case XK_F2: return event | CACA_KEY_F2;
  107. case XK_F3: return event | CACA_KEY_F3;
  108. case XK_F4: return event | CACA_KEY_F4;
  109. case XK_F5: return event | CACA_KEY_F5;
  110. case XK_F6: return event | CACA_KEY_F6;
  111. case XK_F7: return event | CACA_KEY_F7;
  112. case XK_F8: return event | CACA_KEY_F8;
  113. case XK_F9: return event | CACA_KEY_F9;
  114. case XK_F10: return event | CACA_KEY_F10;
  115. case XK_F11: return event | CACA_KEY_F11;
  116. case XK_F12: return event | CACA_KEY_F12;
  117. case XK_F13: return event | CACA_KEY_F13;
  118. case XK_F14: return event | CACA_KEY_F14;
  119. case XK_F15: return event | CACA_KEY_F15;
  120. case XK_Left: return event | CACA_KEY_LEFT;
  121. case XK_Right: return event | CACA_KEY_RIGHT;
  122. case XK_Up: return event | CACA_KEY_UP;
  123. case XK_Down: return event | CACA_KEY_DOWN;
  124. default: return 0;
  125. }
  126. }
  127. return 0;
  128. }
  129. #endif
  130. /* Read all available key events and push them into the buffer */
  131. while(keys < KEY_BUFLEN)
  132. {
  133. unsigned int key = _read_key();
  134. if(!key)
  135. break;
  136. _push_key(key);
  137. }
  138. /* If no keys were read, return */
  139. if(!keys)
  140. return 0;
  141. #if defined(USE_NCURSES)
  142. if(_caca_driver == CACA_DRIVER_NCURSES)
  143. {
  144. if(keybuf[0] == KEY_MOUSE)
  145. {
  146. MEVENT mevent;
  147. _pop_key();
  148. getmouse(&mevent);
  149. _push_key(CACA_EVENT_MOUSE_PRESS | 1);
  150. return CACA_EVENT_MOUSE_MOTION
  151. | (mevent.x << 12) | (mevent.y << 0);
  152. }
  153. switch(keybuf[0])
  154. {
  155. case KEY_UP: event = CACA_EVENT_KEY_PRESS | CACA_KEY_UP; break;
  156. case KEY_DOWN: event = CACA_EVENT_KEY_PRESS | CACA_KEY_DOWN; break;
  157. case KEY_LEFT: event = CACA_EVENT_KEY_PRESS | CACA_KEY_LEFT; break;
  158. case KEY_RIGHT: event = CACA_EVENT_KEY_PRESS | CACA_KEY_RIGHT; break;
  159. case KEY_F(1): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F1; break;
  160. case KEY_F(2): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F2; break;
  161. case KEY_F(3): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F3; break;
  162. case KEY_F(4): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F4; break;
  163. case KEY_F(5): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F5; break;
  164. case KEY_F(6): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F6; break;
  165. case KEY_F(7): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F7; break;
  166. case KEY_F(8): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F8; break;
  167. case KEY_F(9): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F9; break;
  168. case KEY_F(10): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F10; break;
  169. case KEY_F(11): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F11; break;
  170. case KEY_F(12): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F12; break;
  171. }
  172. if(event)
  173. {
  174. _pop_key();
  175. return event;
  176. }
  177. }
  178. #endif
  179. /* If it's already a special event, return it */
  180. if((keybuf[0] & ~0xff) != 0)
  181. return _pop_key();
  182. /* If it's not an escape sequence, return the key */
  183. if(keybuf[0] != '\x1b')
  184. return CACA_EVENT_KEY_PRESS | _pop_key();
  185. /*
  186. * Handle known escape sequences
  187. */
  188. _pop_key();
  189. if(keybuf[0] == 'O' && keybuf[1] >= 'P' && keybuf[1] <= 'S')
  190. {
  191. /* ^[OP ^[OQ ^[OR ^[OS */
  192. static unsigned int keylist[] =
  193. { CACA_KEY_F1, CACA_KEY_F2, CACA_KEY_F3, CACA_KEY_F4 };
  194. _pop_key();
  195. return CACA_EVENT_KEY_PRESS | keylist[_pop_key() - 'P'];
  196. }
  197. else if(keybuf[0] == '[' && keybuf[1] >= 'A' && keybuf[1] <= 'D')
  198. {
  199. /* ^[[A ^[[B ^[[C ^[[D */
  200. static unsigned int keylist[] =
  201. { CACA_KEY_UP, CACA_KEY_DOWN, CACA_KEY_RIGHT, CACA_KEY_LEFT };
  202. _pop_key();
  203. return CACA_EVENT_KEY_PRESS | keylist[_pop_key() - 'A'];
  204. }
  205. else if(keybuf[0] == '[' && keybuf[1] == 'M' &&
  206. keybuf[2] && keybuf[3] && keybuf[3])
  207. {
  208. /* ^[[Mxxx */
  209. _pop_key();
  210. _pop_key();
  211. _push_key(CACA_EVENT_MOUSE_PRESS | (_pop_key() - ' '));
  212. return CACA_EVENT_MOUSE_MOTION
  213. | ((_pop_key() - '!') << 12) | ((_pop_key() - '!') << 0);
  214. }
  215. else if(keybuf[0] == '[' && keybuf[1] == '1' && keybuf[3] == '~' &&
  216. keybuf[2] >= '5' && keybuf[2] != '6' && keybuf[2] <= '9')
  217. {
  218. /* ^[[15~ ^[[17~ ^[[18~ ^[[19~ */
  219. static unsigned int keylist[] =
  220. { CACA_KEY_F5, 0, CACA_KEY_F6, CACA_KEY_F7, CACA_KEY_F8 };
  221. _pop_key();
  222. _pop_key();
  223. event = CACA_EVENT_KEY_PRESS | keylist[_pop_key() - '5'];
  224. _pop_key();
  225. return event;
  226. }
  227. else if(keybuf[0] == '[' && keybuf[1] == '2' && keybuf[3] == '~' &&
  228. keybuf[2] >= '0' && keybuf[2] != '2' && keybuf[2] <= '4')
  229. {
  230. /* ^[[20~ ^[[21~ ^[[23~ ^[[24~ */
  231. static unsigned int keylist[] =
  232. { CACA_KEY_F9, CACA_KEY_F10, 0, CACA_KEY_F11, CACA_KEY_F12 };
  233. _pop_key();
  234. _pop_key();
  235. event = CACA_EVENT_KEY_PRESS | keylist[_pop_key() - '0'];
  236. _pop_key();
  237. return event;
  238. }
  239. /* Unknown escape sequence: return the ESC key */
  240. return CACA_EVENT_KEY_PRESS | '\x1b';
  241. }
  242. /** \brief Wait for the next mouse or keyboard input event.
  243. *
  244. * This function returns the first mouse or keyboard event in the queue. If
  245. * no event is pending, it blocks until an event is received. See also
  246. * caca_get_event() for a non-blocking version of this function.
  247. *
  248. * \return The next event in the queue.
  249. */
  250. unsigned int caca_wait_event(void)
  251. {
  252. for( ; ; )
  253. {
  254. unsigned int event = caca_get_event();
  255. if(event)
  256. return event;
  257. usleep(1000);
  258. }
  259. }
  260. /*
  261. * XXX: The following functions are local.
  262. */
  263. static void _push_key(unsigned int key)
  264. {
  265. if(keys == KEY_BUFLEN)
  266. return;
  267. keybuf[keys] = key;
  268. keys++;
  269. keybuf[keys] = 0;
  270. }
  271. static unsigned int _pop_key(void)
  272. {
  273. int i;
  274. unsigned int key = keybuf[0];
  275. keys--;
  276. for(i = 0; i < keys; i++)
  277. keybuf[i] = keybuf[i + 1];
  278. keybuf[keys] = 0;
  279. return key;
  280. }
  281. static unsigned int _read_key(void)
  282. {
  283. #if defined(USE_NCURSES)
  284. int intkey;
  285. #endif
  286. #if defined(USE_X11)
  287. #endif
  288. switch(_caca_driver)
  289. {
  290. #if defined(USE_SLANG)
  291. case CACA_DRIVER_SLANG:
  292. return SLang_input_pending(0) ? SLang_getkey() : 0;
  293. #endif
  294. #if defined(USE_NCURSES)
  295. case CACA_DRIVER_NCURSES:
  296. intkey = getch();
  297. return (intkey == ERR) ? 0 : intkey;
  298. #endif
  299. #if defined(USE_CONIO)
  300. case CACA_DRIVER_CONIO:
  301. return _conio_kbhit() ? getch() : 0;
  302. #endif
  303. #if defined(USE_X11)
  304. case CACA_DRIVER_X11:
  305. #endif
  306. default:
  307. break;
  308. }
  309. return 0;
  310. }