Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

514 righe
17 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. # if defined(HAVE_SLANG_SLANG_H)
  31. # include <slang/slang.h>
  32. # else
  33. # include <slang.h>
  34. # endif
  35. #endif
  36. #if defined(USE_NCURSES)
  37. # include <curses.h>
  38. #endif
  39. #if defined(USE_CONIO)
  40. # include <conio.h>
  41. #endif
  42. #if defined(USE_X11)
  43. # include <X11/Xlib.h>
  44. # include <X11/Xutil.h>
  45. # include <X11/keysym.h>
  46. #endif
  47. #include <unistd.h>
  48. #include "caca.h"
  49. #include "caca_internals.h"
  50. static unsigned int _get_next_event(void);
  51. static void _push_key(unsigned int);
  52. static unsigned int _pop_key(void);
  53. static unsigned int _read_key(void);
  54. #if !defined(_DOXYGEN_SKIP_ME)
  55. #define KEY_BUFLEN 10
  56. #endif
  57. static unsigned int keybuf[KEY_BUFLEN + 1]; /* zero-terminated */
  58. static int keys = 0;
  59. /** \brief Get the next mouse or keyboard input event.
  60. *
  61. * This function polls the event queue for mouse or keyboard events matching
  62. * the event mask and returns the first matching event. Non-matching events
  63. * are discarded. It is non-blocking and returns zero if no more events are
  64. * pending in the queue. See also caca_wait_event() for a blocking version
  65. * of this function.
  66. *
  67. * \param event_mask Bitmask of requested events.
  68. * \return The next matching event in the queue, or 0 if no event is pending.
  69. */
  70. unsigned int caca_get_event(unsigned int event_mask)
  71. {
  72. for( ; ; )
  73. {
  74. unsigned int event = _get_next_event();
  75. if(!event || event & event_mask)
  76. return event;
  77. }
  78. }
  79. /** \brief Wait for the next mouse or keyboard input event.
  80. *
  81. * This function returns the first mouse or keyboard event in the queue
  82. * that matches the event mask. If no event is pending, it blocks until a
  83. * matching event is received. See also caca_get_event() for a non-blocking
  84. * version of this function.
  85. *
  86. * \param event_mask Bitmask of requested events.
  87. * \return The next event in the queue.
  88. */
  89. unsigned int caca_wait_event(unsigned int event_mask)
  90. {
  91. for( ; ; )
  92. {
  93. unsigned int event = _get_next_event();
  94. if(event & event_mask)
  95. return event;
  96. usleep(1000);
  97. }
  98. }
  99. /*
  100. * XXX: The following functions are local.
  101. */
  102. static unsigned int _get_next_event(void)
  103. {
  104. unsigned int event = 0;
  105. #if defined(USE_X11)
  106. /* The X11 event check routine */
  107. if(_caca_driver == CACA_DRIVER_X11)
  108. {
  109. XEvent xevent;
  110. static unsigned int x11_x = 0, x11_y = 0;
  111. long int xevent_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask
  112. | ButtonReleaseMask | PointerMotionMask;
  113. char key;
  114. while(XCheckWindowEvent(x11_dpy, x11_window, xevent_mask, &xevent)
  115. == True)
  116. {
  117. KeySym keysym;
  118. /* Check for mouse motion events */
  119. if(xevent.type == MotionNotify)
  120. {
  121. unsigned int newx = xevent.xmotion.x / x11_font_width;
  122. unsigned int newy = xevent.xmotion.y / x11_font_height;
  123. if(x11_x == newx && x11_y == newy)
  124. continue;
  125. if(newx >= _caca_width)
  126. newx = _caca_width - 1;
  127. if(newy >= _caca_height)
  128. newy = _caca_height - 1;
  129. x11_x = newx & 0xfff;
  130. x11_y = newy & 0xfff;
  131. return CACA_EVENT_MOUSE_MOTION | (newx << 12) | (newy << 0);
  132. }
  133. /* Check for mouse press and release events */
  134. if(xevent.type == ButtonPress)
  135. return CACA_EVENT_MOUSE_PRESS
  136. | ((XButtonEvent *)&xevent)->button;
  137. if(xevent.type == ButtonRelease)
  138. return CACA_EVENT_MOUSE_RELEASE
  139. | ((XButtonEvent *)&xevent)->button;
  140. /* Check for key press and release events */
  141. if(xevent.type == KeyPress)
  142. event |= CACA_EVENT_KEY_PRESS;
  143. else if(xevent.type == KeyRelease)
  144. event |= CACA_EVENT_KEY_RELEASE;
  145. else
  146. continue;
  147. if(XLookupString(&xevent.xkey, &key, 1, NULL, NULL))
  148. return event | key;
  149. keysym = XKeycodeToKeysym(x11_dpy, xevent.xkey.keycode, 0);
  150. switch(keysym)
  151. {
  152. case XK_F1: return event | CACA_KEY_F1;
  153. case XK_F2: return event | CACA_KEY_F2;
  154. case XK_F3: return event | CACA_KEY_F3;
  155. case XK_F4: return event | CACA_KEY_F4;
  156. case XK_F5: return event | CACA_KEY_F5;
  157. case XK_F6: return event | CACA_KEY_F6;
  158. case XK_F7: return event | CACA_KEY_F7;
  159. case XK_F8: return event | CACA_KEY_F8;
  160. case XK_F9: return event | CACA_KEY_F9;
  161. case XK_F10: return event | CACA_KEY_F10;
  162. case XK_F11: return event | CACA_KEY_F11;
  163. case XK_F12: return event | CACA_KEY_F12;
  164. case XK_F13: return event | CACA_KEY_F13;
  165. case XK_F14: return event | CACA_KEY_F14;
  166. case XK_F15: return event | CACA_KEY_F15;
  167. case XK_Left: return event | CACA_KEY_LEFT;
  168. case XK_Right: return event | CACA_KEY_RIGHT;
  169. case XK_Up: return event | CACA_KEY_UP;
  170. case XK_Down: return event | CACA_KEY_DOWN;
  171. default: return 0;
  172. }
  173. }
  174. return 0;
  175. }
  176. #endif
  177. /* Read all available key events and push them into the buffer */
  178. while(keys < KEY_BUFLEN)
  179. {
  180. unsigned int key = _read_key();
  181. if(!key)
  182. break;
  183. _push_key(key);
  184. }
  185. /* If no keys were read, return */
  186. if(!keys)
  187. return 0;
  188. #if defined(USE_NCURSES)
  189. if(_caca_driver == CACA_DRIVER_NCURSES)
  190. {
  191. if(keybuf[0] == KEY_MOUSE)
  192. {
  193. MEVENT mevent;
  194. _pop_key();
  195. getmouse(&mevent);
  196. switch(mevent.bstate)
  197. {
  198. case BUTTON1_PRESSED:
  199. _push_key(CACA_EVENT_MOUSE_PRESS | 1);
  200. break;
  201. case BUTTON1_RELEASED:
  202. _push_key(CACA_EVENT_MOUSE_RELEASE | 1);
  203. break;
  204. case BUTTON1_CLICKED:
  205. _push_key(CACA_EVENT_MOUSE_PRESS | 1);
  206. _push_key(CACA_EVENT_MOUSE_RELEASE | 1);
  207. break;
  208. case BUTTON1_DOUBLE_CLICKED:
  209. _push_key(CACA_EVENT_MOUSE_PRESS | 1);
  210. _push_key(CACA_EVENT_MOUSE_RELEASE | 1);
  211. _push_key(CACA_EVENT_MOUSE_PRESS | 1);
  212. _push_key(CACA_EVENT_MOUSE_RELEASE | 1);
  213. break;
  214. case BUTTON1_TRIPLE_CLICKED:
  215. _push_key(CACA_EVENT_MOUSE_PRESS | 1);
  216. _push_key(CACA_EVENT_MOUSE_RELEASE | 1);
  217. _push_key(CACA_EVENT_MOUSE_PRESS | 1);
  218. _push_key(CACA_EVENT_MOUSE_RELEASE | 1);
  219. _push_key(CACA_EVENT_MOUSE_PRESS | 1);
  220. _push_key(CACA_EVENT_MOUSE_RELEASE | 1);
  221. break;
  222. case BUTTON1_RESERVED_EVENT:
  223. break;
  224. case BUTTON2_PRESSED:
  225. _push_key(CACA_EVENT_MOUSE_PRESS | 2);
  226. break;
  227. case BUTTON2_RELEASED:
  228. _push_key(CACA_EVENT_MOUSE_RELEASE | 2);
  229. break;
  230. case BUTTON2_CLICKED:
  231. _push_key(CACA_EVENT_MOUSE_PRESS | 2);
  232. _push_key(CACA_EVENT_MOUSE_RELEASE | 2);
  233. break;
  234. case BUTTON2_DOUBLE_CLICKED:
  235. _push_key(CACA_EVENT_MOUSE_PRESS | 2);
  236. _push_key(CACA_EVENT_MOUSE_RELEASE | 2);
  237. _push_key(CACA_EVENT_MOUSE_PRESS | 2);
  238. _push_key(CACA_EVENT_MOUSE_RELEASE | 2);
  239. break;
  240. case BUTTON2_TRIPLE_CLICKED:
  241. _push_key(CACA_EVENT_MOUSE_PRESS | 2);
  242. _push_key(CACA_EVENT_MOUSE_RELEASE | 2);
  243. _push_key(CACA_EVENT_MOUSE_PRESS | 2);
  244. _push_key(CACA_EVENT_MOUSE_RELEASE | 2);
  245. _push_key(CACA_EVENT_MOUSE_PRESS | 2);
  246. _push_key(CACA_EVENT_MOUSE_RELEASE | 2);
  247. break;
  248. case BUTTON2_RESERVED_EVENT:
  249. break;
  250. case BUTTON3_PRESSED:
  251. _push_key(CACA_EVENT_MOUSE_PRESS | 3);
  252. break;
  253. case BUTTON3_RELEASED:
  254. _push_key(CACA_EVENT_MOUSE_RELEASE | 3);
  255. break;
  256. case BUTTON3_CLICKED:
  257. _push_key(CACA_EVENT_MOUSE_PRESS | 3);
  258. _push_key(CACA_EVENT_MOUSE_RELEASE | 3);
  259. break;
  260. case BUTTON3_DOUBLE_CLICKED:
  261. _push_key(CACA_EVENT_MOUSE_PRESS | 3);
  262. _push_key(CACA_EVENT_MOUSE_RELEASE | 3);
  263. _push_key(CACA_EVENT_MOUSE_PRESS | 3);
  264. _push_key(CACA_EVENT_MOUSE_RELEASE | 3);
  265. break;
  266. case BUTTON3_TRIPLE_CLICKED:
  267. _push_key(CACA_EVENT_MOUSE_PRESS | 3);
  268. _push_key(CACA_EVENT_MOUSE_RELEASE | 3);
  269. _push_key(CACA_EVENT_MOUSE_PRESS | 3);
  270. _push_key(CACA_EVENT_MOUSE_RELEASE | 3);
  271. _push_key(CACA_EVENT_MOUSE_PRESS | 3);
  272. _push_key(CACA_EVENT_MOUSE_RELEASE | 3);
  273. break;
  274. case BUTTON3_RESERVED_EVENT:
  275. break;
  276. case BUTTON4_PRESSED:
  277. _push_key(CACA_EVENT_MOUSE_PRESS | 4);
  278. break;
  279. case BUTTON4_RELEASED:
  280. _push_key(CACA_EVENT_MOUSE_RELEASE | 4);
  281. break;
  282. case BUTTON4_CLICKED:
  283. _push_key(CACA_EVENT_MOUSE_PRESS | 4);
  284. _push_key(CACA_EVENT_MOUSE_RELEASE | 4);
  285. break;
  286. case BUTTON4_DOUBLE_CLICKED:
  287. _push_key(CACA_EVENT_MOUSE_PRESS | 4);
  288. _push_key(CACA_EVENT_MOUSE_RELEASE | 4);
  289. _push_key(CACA_EVENT_MOUSE_PRESS | 4);
  290. _push_key(CACA_EVENT_MOUSE_RELEASE | 4);
  291. break;
  292. case BUTTON4_TRIPLE_CLICKED:
  293. _push_key(CACA_EVENT_MOUSE_PRESS | 4);
  294. _push_key(CACA_EVENT_MOUSE_RELEASE | 4);
  295. _push_key(CACA_EVENT_MOUSE_PRESS | 4);
  296. _push_key(CACA_EVENT_MOUSE_RELEASE | 4);
  297. _push_key(CACA_EVENT_MOUSE_PRESS | 4);
  298. _push_key(CACA_EVENT_MOUSE_RELEASE | 4);
  299. break;
  300. case BUTTON4_RESERVED_EVENT:
  301. break;
  302. default:
  303. break;
  304. }
  305. return CACA_EVENT_MOUSE_MOTION | (mevent.x << 12) | mevent.y;
  306. }
  307. switch(keybuf[0])
  308. {
  309. case KEY_UP: event = CACA_EVENT_KEY_PRESS | CACA_KEY_UP; break;
  310. case KEY_DOWN: event = CACA_EVENT_KEY_PRESS | CACA_KEY_DOWN; break;
  311. case KEY_LEFT: event = CACA_EVENT_KEY_PRESS | CACA_KEY_LEFT; break;
  312. case KEY_RIGHT: event = CACA_EVENT_KEY_PRESS | CACA_KEY_RIGHT; break;
  313. case KEY_F(1): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F1; break;
  314. case KEY_F(2): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F2; break;
  315. case KEY_F(3): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F3; break;
  316. case KEY_F(4): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F4; break;
  317. case KEY_F(5): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F5; break;
  318. case KEY_F(6): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F6; break;
  319. case KEY_F(7): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F7; break;
  320. case KEY_F(8): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F8; break;
  321. case KEY_F(9): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F9; break;
  322. case KEY_F(10): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F10; break;
  323. case KEY_F(11): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F11; break;
  324. case KEY_F(12): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F12; break;
  325. }
  326. if(event)
  327. {
  328. _pop_key();
  329. if(event & CACA_EVENT_KEY_PRESS)
  330. _push_key(CACA_EVENT_KEY_RELEASE
  331. | (event & ~CACA_EVENT_KEY_PRESS));
  332. return event;
  333. }
  334. }
  335. #endif
  336. /* If it's already a special event, return it */
  337. if((keybuf[0] & ~0xff) != 0)
  338. return _pop_key();
  339. /* If it's not an escape sequence, return the key */
  340. if(keybuf[0] != '\x1b')
  341. {
  342. event = _pop_key();
  343. _push_key(CACA_EVENT_KEY_RELEASE | event);
  344. return CACA_EVENT_KEY_PRESS | event;
  345. }
  346. /*
  347. * Handle known escape sequences
  348. */
  349. _pop_key();
  350. if(keybuf[0] == 'O' && keybuf[1] >= 'P' && keybuf[1] <= 'S')
  351. {
  352. /* ^[OP ^[OQ ^[OR ^[OS */
  353. static unsigned int keylist[] =
  354. { CACA_KEY_F1, CACA_KEY_F2, CACA_KEY_F3, CACA_KEY_F4 };
  355. _pop_key();
  356. event = keylist[_pop_key() - 'P'];
  357. _push_key(CACA_EVENT_KEY_RELEASE | event);
  358. return CACA_EVENT_KEY_PRESS | event;
  359. }
  360. else if(keybuf[0] == '[' && keybuf[1] >= 'A' && keybuf[1] <= 'D')
  361. {
  362. /* ^[[A ^[[B ^[[C ^[[D */
  363. static unsigned int keylist[] =
  364. { CACA_KEY_UP, CACA_KEY_DOWN, CACA_KEY_RIGHT, CACA_KEY_LEFT };
  365. _pop_key();
  366. event = keylist[_pop_key() - 'A'];
  367. _push_key(CACA_EVENT_KEY_RELEASE | event);
  368. return CACA_EVENT_KEY_PRESS | event;
  369. }
  370. else if(keybuf[0] == '[' && keybuf[1] == 'M' &&
  371. keybuf[2] && keybuf[3] && keybuf[3])
  372. {
  373. int button;
  374. /* ^[[Mxxx */
  375. _pop_key();
  376. _pop_key();
  377. button = (1 + _pop_key() - ' ') & 0xf;
  378. _push_key(CACA_EVENT_MOUSE_PRESS | button);
  379. _push_key(CACA_EVENT_MOUSE_RELEASE | button);
  380. return CACA_EVENT_MOUSE_MOTION
  381. | ((_pop_key() - '!') << 12) | ((_pop_key() - '!') << 0);
  382. }
  383. else if(keybuf[0] == '[' && keybuf[1] == '1' && keybuf[3] == '~' &&
  384. keybuf[2] >= '5' && keybuf[2] != '6' && keybuf[2] <= '9')
  385. {
  386. /* ^[[15~ ^[[17~ ^[[18~ ^[[19~ */
  387. static unsigned int keylist[] =
  388. { CACA_KEY_F5, 0, CACA_KEY_F6, CACA_KEY_F7, CACA_KEY_F8 };
  389. _pop_key();
  390. _pop_key();
  391. event = keylist[_pop_key() - '5'];
  392. _pop_key();
  393. _push_key(CACA_EVENT_KEY_RELEASE | event);
  394. return CACA_EVENT_KEY_PRESS | event;
  395. }
  396. else if(keybuf[0] == '[' && keybuf[1] == '2' && keybuf[3] == '~' &&
  397. keybuf[2] >= '0' && keybuf[2] != '2' && keybuf[2] <= '4')
  398. {
  399. /* ^[[20~ ^[[21~ ^[[23~ ^[[24~ */
  400. static unsigned int keylist[] =
  401. { CACA_KEY_F9, CACA_KEY_F10, 0, CACA_KEY_F11, CACA_KEY_F12 };
  402. _pop_key();
  403. _pop_key();
  404. event = keylist[_pop_key() - '0'];
  405. _pop_key();
  406. _push_key(CACA_EVENT_KEY_RELEASE | event);
  407. return CACA_EVENT_KEY_PRESS | event;
  408. }
  409. /* Unknown escape sequence: return the ESC key */
  410. _push_key(CACA_EVENT_KEY_RELEASE | '\x1b');
  411. return CACA_EVENT_KEY_PRESS | '\x1b';
  412. }
  413. static void _push_key(unsigned int key)
  414. {
  415. if(keys == KEY_BUFLEN)
  416. return;
  417. keybuf[keys] = key;
  418. keys++;
  419. keybuf[keys] = 0;
  420. }
  421. static unsigned int _pop_key(void)
  422. {
  423. int i;
  424. unsigned int key = keybuf[0];
  425. keys--;
  426. for(i = 0; i < keys; i++)
  427. keybuf[i] = keybuf[i + 1];
  428. keybuf[keys] = 0;
  429. return key;
  430. }
  431. static unsigned int _read_key(void)
  432. {
  433. #if defined(USE_NCURSES)
  434. int intkey;
  435. #endif
  436. #if defined(USE_X11)
  437. #endif
  438. switch(_caca_driver)
  439. {
  440. #if defined(USE_SLANG)
  441. case CACA_DRIVER_SLANG:
  442. return SLang_input_pending(0) ? SLang_getkey() : 0;
  443. #endif
  444. #if defined(USE_NCURSES)
  445. case CACA_DRIVER_NCURSES:
  446. intkey = getch();
  447. return (intkey == ERR) ? 0 : intkey;
  448. #endif
  449. #if defined(USE_CONIO)
  450. case CACA_DRIVER_CONIO:
  451. return _conio_kbhit() ? getch() : 0;
  452. #endif
  453. #if defined(USE_X11)
  454. case CACA_DRIVER_X11:
  455. #endif
  456. default:
  457. break;
  458. }
  459. return 0;
  460. }