getch.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. /* PDCurses */
  2. #include <curspriv.h>
  3. /*man-start**************************************************************
  4. getch
  5. -----
  6. ### Synopsis
  7. int getch(void);
  8. int wgetch(WINDOW *win);
  9. int mvgetch(int y, int x);
  10. int mvwgetch(WINDOW *win, int y, int x);
  11. int ungetch(int ch);
  12. int flushinp(void);
  13. int get_wch(wint_t *wch);
  14. int wget_wch(WINDOW *win, wint_t *wch);
  15. int mvget_wch(int y, int x, wint_t *wch);
  16. int mvwget_wch(WINDOW *win, int y, int x, wint_t *wch);
  17. int unget_wch(const wchar_t wch);
  18. unsigned long PDC_get_key_modifiers(void);
  19. int PDC_return_key_modifiers(bool flag);
  20. ### Description
  21. With the getch(), wgetch(), mvgetch(), and mvwgetch() functions, a
  22. character is read from the terminal associated with the window. In
  23. nodelay mode, if there is no input waiting, the value ERR is
  24. returned. In delay mode, the program will hang until the system
  25. passes text through to the program. Depending on the setting of
  26. cbreak(), this will be after one character or after the first
  27. newline. Unless noecho() has been set, the character will also be
  28. echoed into the designated window.
  29. If keypad() is TRUE, and a function key is pressed, the token for
  30. that function key will be returned instead of the raw characters.
  31. Possible function keys are defined in <curses.h> with integers
  32. beginning with 0401, whose names begin with KEY_.
  33. If nodelay(win, TRUE) has been called on the window and no input is
  34. waiting, the value ERR is returned.
  35. ungetch() places ch back onto the input queue to be returned by the
  36. next call to wgetch().
  37. flushinp() throws away any type-ahead that has been typed by the user
  38. and has not yet been read by the program.
  39. wget_wch() is the wide-character version of wgetch(), available when
  40. PDCurses is built with the PDC_WIDE option. It takes a pointer to a
  41. wint_t rather than returning the key as an int, and instead returns
  42. KEY_CODE_YES if the key is a function key. Otherwise, it returns OK
  43. or ERR. It's important to check for KEY_CODE_YES, since regular wide
  44. characters can have the same values as function key codes.
  45. unget_wch() puts a wide character on the input queue.
  46. PDC_get_key_modifiers() returns the keyboard modifiers (shift,
  47. control, alt, numlock) effective at the time of the last getch()
  48. call. Use the macros PDC_KEY_MODIFIER_* to determine which
  49. modifier(s) were set. PDC_return_key_modifiers() tells getch() to
  50. return modifier keys pressed alone as keystrokes (KEY_ALT_L, etc.).
  51. These may not work on all platforms.
  52. NOTE: getch() and ungetch() are implemented as macros, to avoid
  53. conflict with many DOS compiler's runtime libraries.
  54. ### Return Value
  55. These functions return ERR or the value of the character, meta
  56. character or function key token.
  57. ### Portability
  58. X/Open ncurses NetBSD
  59. getch Y Y Y
  60. wgetch Y Y Y
  61. mvgetch Y Y Y
  62. mvwgetch Y Y Y
  63. ungetch Y Y Y
  64. flushinp Y Y Y
  65. get_wch Y Y Y
  66. wget_wch Y Y Y
  67. mvget_wch Y Y Y
  68. mvwget_wch Y Y Y
  69. unget_wch Y Y Y
  70. PDC_get_key_modifiers - - -
  71. **man-end****************************************************************/
  72. #include <stdlib.h>
  73. static int _get_box(int *y_start, int *y_end, int *x_start, int *x_end)
  74. {
  75. int start, end;
  76. if (SP->sel_start < SP->sel_end)
  77. {
  78. start = SP->sel_start;
  79. end = SP->sel_end;
  80. }
  81. else
  82. {
  83. start = SP->sel_end;
  84. end = SP->sel_start;
  85. }
  86. *y_start = start / COLS;
  87. *x_start = start % COLS;
  88. *y_end = end / COLS;
  89. *x_end = end % COLS;
  90. return (end - start) + (*y_end - *y_start);
  91. }
  92. static void _highlight(void)
  93. {
  94. int i, j, y_start, y_end, x_start, x_end;
  95. if (-1 == SP->sel_start)
  96. return;
  97. _get_box(&y_start, &y_end, &x_start, &x_end);
  98. for (j = y_start; j <= y_end; j++)
  99. for (i = (j == y_start ? x_start : 0);
  100. i < (j == y_end ? x_end : COLS); i++)
  101. curscr->_y[j][i] ^= A_REVERSE;
  102. wrefresh(curscr);
  103. }
  104. static void _copy(void)
  105. {
  106. #ifdef PDC_WIDE
  107. wchar_t *wtmp;
  108. # define TMP wtmp
  109. # define MASK A_CHARTEXT
  110. #else
  111. # define TMP tmp
  112. # define MASK 0xff
  113. #endif
  114. char *tmp;
  115. long pos;
  116. int i, j, y_start, y_end, x_start, x_end, len;
  117. if (-1 == SP->sel_start)
  118. return;
  119. len = _get_box(&y_start, &y_end, &x_start, &x_end);
  120. if (!len)
  121. return;
  122. #ifdef PDC_WIDE
  123. wtmp = malloc((len + 1) * sizeof(wchar_t));
  124. len *= 3;
  125. #endif
  126. tmp = malloc(len + 1);
  127. for (j = y_start, pos = 0; j <= y_end; j++)
  128. {
  129. for (i = (j == y_start ? x_start : 0);
  130. i < (j == y_end ? x_end : COLS); i++)
  131. TMP[pos++] = curscr->_y[j][i] & MASK;
  132. while (y_start != y_end && pos > 0 && TMP[pos - 1] == 32)
  133. pos--;
  134. if (j < y_end)
  135. TMP[pos++] = 10;
  136. }
  137. TMP[pos] = 0;
  138. #ifdef PDC_WIDE
  139. pos = PDC_wcstombs(tmp, wtmp, len);
  140. #endif
  141. PDC_setclipboard(tmp, pos);
  142. free(tmp);
  143. #ifdef PDC_WIDE
  144. free(wtmp);
  145. #endif
  146. }
  147. static int _paste(void)
  148. {
  149. #ifdef PDC_WIDE
  150. wchar_t *wpaste;
  151. # define PASTE wpaste
  152. #else
  153. # define PASTE paste
  154. #endif
  155. char *paste;
  156. long len, newmax;
  157. int key;
  158. key = PDC_getclipboard(&paste, &len);
  159. if (PDC_CLIP_SUCCESS != key || !len)
  160. return -1;
  161. #ifdef PDC_WIDE
  162. wpaste = malloc(len * sizeof(wchar_t));
  163. len = PDC_mbstowcs(wpaste, paste, len);
  164. #endif
  165. newmax = len + SP->c_ungind;
  166. if (newmax > SP->c_ungmax)
  167. {
  168. SP->c_ungch = realloc(SP->c_ungch, newmax * sizeof(int));
  169. if (!SP->c_ungch)
  170. return -1;
  171. SP->c_ungmax = newmax;
  172. }
  173. while (len > 1)
  174. PDC_ungetch(PASTE[--len]);
  175. key = *PASTE;
  176. #ifdef PDC_WIDE
  177. free(wpaste);
  178. #endif
  179. PDC_freeclipboard(paste);
  180. SP->key_modifiers = 0;
  181. return key;
  182. }
  183. static int _mouse_key(void)
  184. {
  185. int i, key = KEY_MOUSE, changes = SP->mouse_status.changes;
  186. unsigned long mbe = SP->_trap_mbe;
  187. /* Selection highlighting? */
  188. if ((!mbe || SP->mouse_status.button[0] & BUTTON_SHIFT) && changes & 1)
  189. {
  190. i = SP->mouse_status.y * COLS + SP->mouse_status.x;
  191. switch (SP->mouse_status.button[0] & BUTTON_ACTION_MASK)
  192. {
  193. case BUTTON_PRESSED:
  194. _highlight();
  195. SP->sel_start = SP->sel_end = i;
  196. return -1;
  197. case BUTTON_MOVED:
  198. _highlight();
  199. SP->sel_end = i;
  200. _highlight();
  201. return -1;
  202. case BUTTON_RELEASED:
  203. _copy();
  204. return -1;
  205. }
  206. }
  207. else if ((!mbe || SP->mouse_status.button[1] & BUTTON_SHIFT) &&
  208. changes & 2 && (SP->mouse_status.button[1] &
  209. BUTTON_ACTION_MASK) == BUTTON_CLICKED)
  210. {
  211. SP->key_code = FALSE;
  212. return _paste();
  213. }
  214. /* Filter unwanted mouse events */
  215. for (i = 0; i < 3; i++)
  216. {
  217. if (changes & (1 << i))
  218. {
  219. int shf = i * 5;
  220. short button = SP->mouse_status.button[i] & BUTTON_ACTION_MASK;
  221. if ( (!(mbe & (BUTTON1_PRESSED << shf)) &&
  222. (button == BUTTON_PRESSED))
  223. || (!(mbe & (BUTTON1_CLICKED << shf)) &&
  224. (button == BUTTON_CLICKED))
  225. || (!(mbe & (BUTTON1_DOUBLE_CLICKED << shf)) &&
  226. (button == BUTTON_DOUBLE_CLICKED))
  227. || (!(mbe & (BUTTON1_MOVED << shf)) &&
  228. (button == BUTTON_MOVED))
  229. || (!(mbe & (BUTTON1_RELEASED << shf)) &&
  230. (button == BUTTON_RELEASED))
  231. )
  232. SP->mouse_status.changes ^= (1 << i);
  233. }
  234. }
  235. if (changes & PDC_MOUSE_MOVED)
  236. {
  237. if (!(mbe & (BUTTON1_MOVED|BUTTON2_MOVED|BUTTON3_MOVED)))
  238. SP->mouse_status.changes ^= PDC_MOUSE_MOVED;
  239. }
  240. if (changes & (PDC_MOUSE_WHEEL_UP|PDC_MOUSE_WHEEL_DOWN))
  241. {
  242. if (!(mbe & MOUSE_WHEEL_SCROLL))
  243. SP->mouse_status.changes &=
  244. ~(PDC_MOUSE_WHEEL_UP|PDC_MOUSE_WHEEL_DOWN);
  245. }
  246. if (!changes)
  247. return -1;
  248. /* Check for click in slk area */
  249. i = PDC_mouse_in_slk(SP->mouse_status.y, SP->mouse_status.x);
  250. if (i)
  251. {
  252. if (SP->mouse_status.button[0] & (BUTTON_PRESSED|BUTTON_CLICKED))
  253. key = KEY_F(i);
  254. else
  255. key = -1;
  256. }
  257. return key;
  258. }
  259. int wgetch(WINDOW *win)
  260. {
  261. int key, waitcount;
  262. PDC_LOG(("wgetch() - called\n"));
  263. if (!win || !SP)
  264. return ERR;
  265. waitcount = 0;
  266. /* set the number of 1/20th second napms() calls */
  267. if (SP->delaytenths)
  268. waitcount = 2 * SP->delaytenths;
  269. else
  270. if (win->_delayms)
  271. {
  272. /* Can't really do millisecond intervals, so delay in
  273. 1/20ths of a second (50ms) */
  274. waitcount = win->_delayms / 50;
  275. if (!waitcount)
  276. waitcount = 1;
  277. }
  278. /* refresh window when wgetch is called if there have been changes
  279. to it and it is not a pad */
  280. if (!(win->_flags & _PAD) && ((!win->_leaveit &&
  281. (win->_begx + win->_curx != SP->curscol ||
  282. win->_begy + win->_cury != SP->cursrow)) || is_wintouched(win)))
  283. wrefresh(win);
  284. /* if ungotten char exists, remove and return it */
  285. if (SP->c_ungind)
  286. return SP->c_ungch[--(SP->c_ungind)];
  287. /* if normal and data in buffer */
  288. if ((!SP->raw_inp && !SP->cbreak) && (SP->c_gindex < SP->c_pindex))
  289. return SP->c_buffer[SP->c_gindex++];
  290. /* prepare to buffer data */
  291. SP->c_pindex = 0;
  292. SP->c_gindex = 0;
  293. /* to get here, no keys are buffered. go and get one. */
  294. for (;;) /* loop for any buffering */
  295. {
  296. /* is there a keystroke ready? */
  297. if (!PDC_check_key())
  298. {
  299. /* if not, handle timeout() and halfdelay() */
  300. if (SP->delaytenths || win->_delayms)
  301. {
  302. if (!waitcount)
  303. return ERR;
  304. waitcount--;
  305. }
  306. else
  307. if (win->_nodelay)
  308. return ERR;
  309. napms(50); /* sleep for 1/20th second */
  310. continue; /* then check again */
  311. }
  312. /* if there is, fetch it */
  313. key = PDC_get_key();
  314. /* copy or paste? */
  315. if (SP->key_modifiers & PDC_KEY_MODIFIER_SHIFT)
  316. {
  317. if (0x03 == key)
  318. {
  319. _copy();
  320. continue;
  321. }
  322. else if (0x16 == key)
  323. key = _paste();
  324. }
  325. /* filter mouse events; translate mouse clicks in the slk
  326. area to function keys */
  327. if (SP->key_code && key == KEY_MOUSE)
  328. key = _mouse_key();
  329. /* filter special keys if not in keypad mode */
  330. if (SP->key_code && !win->_use_keypad)
  331. key = -1;
  332. /* unwanted key? loop back */
  333. if (key == -1)
  334. continue;
  335. _highlight();
  336. SP->sel_start = SP->sel_end = -1;
  337. /* translate CR */
  338. if (key == '\r' && SP->autocr && !SP->raw_inp)
  339. key = '\n';
  340. /* if echo is enabled */
  341. if (SP->echo && !SP->key_code)
  342. {
  343. waddch(win, key);
  344. wrefresh(win);
  345. }
  346. /* if no buffering */
  347. if (SP->raw_inp || SP->cbreak)
  348. return key;
  349. /* if no overflow, put data in buffer */
  350. if (key == '\b')
  351. {
  352. if (SP->c_pindex > SP->c_gindex)
  353. SP->c_pindex--;
  354. }
  355. else
  356. if (SP->c_pindex < _INBUFSIZ - 2)
  357. SP->c_buffer[SP->c_pindex++] = key;
  358. /* if we got a line */
  359. if (key == '\n' || key == '\r')
  360. return SP->c_buffer[SP->c_gindex++];
  361. }
  362. }
  363. int mvgetch(int y, int x)
  364. {
  365. PDC_LOG(("mvgetch() - called\n"));
  366. if (move(y, x) == ERR)
  367. return ERR;
  368. return wgetch(stdscr);
  369. }
  370. int mvwgetch(WINDOW *win, int y, int x)
  371. {
  372. PDC_LOG(("mvwgetch() - called\n"));
  373. if (wmove(win, y, x) == ERR)
  374. return ERR;
  375. return wgetch(win);
  376. }
  377. int PDC_ungetch(int ch)
  378. {
  379. PDC_LOG(("ungetch() - called\n"));
  380. if (SP->c_ungind >= SP->c_ungmax) /* pushback stack full */
  381. return ERR;
  382. SP->c_ungch[SP->c_ungind++] = ch;
  383. return OK;
  384. }
  385. int flushinp(void)
  386. {
  387. PDC_LOG(("flushinp() - called\n"));
  388. if (!SP)
  389. return ERR;
  390. PDC_flushinp();
  391. SP->c_gindex = 1; /* set indices to kill buffer */
  392. SP->c_pindex = 0;
  393. SP->c_ungind = 0; /* clear SP->c_ungch array */
  394. return OK;
  395. }
  396. unsigned long PDC_get_key_modifiers(void)
  397. {
  398. PDC_LOG(("PDC_get_key_modifiers() - called\n"));
  399. if (!SP)
  400. return ERR;
  401. return SP->key_modifiers;
  402. }
  403. int PDC_return_key_modifiers(bool flag)
  404. {
  405. PDC_LOG(("PDC_return_key_modifiers() - called\n"));
  406. if (!SP)
  407. return ERR;
  408. SP->return_key_modifiers = flag;
  409. return PDC_modifiers_set();
  410. }
  411. #ifdef PDC_WIDE
  412. int wget_wch(WINDOW *win, wint_t *wch)
  413. {
  414. int key;
  415. PDC_LOG(("wget_wch() - called\n"));
  416. if (!wch)
  417. return ERR;
  418. key = wgetch(win);
  419. if (key == ERR)
  420. return ERR;
  421. *wch = key;
  422. return SP->key_code ? KEY_CODE_YES : OK;
  423. }
  424. int get_wch(wint_t *wch)
  425. {
  426. PDC_LOG(("get_wch() - called\n"));
  427. return wget_wch(stdscr, wch);
  428. }
  429. int mvget_wch(int y, int x, wint_t *wch)
  430. {
  431. PDC_LOG(("mvget_wch() - called\n"));
  432. if (move(y, x) == ERR)
  433. return ERR;
  434. return wget_wch(stdscr, wch);
  435. }
  436. int mvwget_wch(WINDOW *win, int y, int x, wint_t *wch)
  437. {
  438. PDC_LOG(("mvwget_wch() - called\n"));
  439. if (wmove(win, y, x) == ERR)
  440. return ERR;
  441. return wget_wch(win, wch);
  442. }
  443. int unget_wch(const wchar_t wch)
  444. {
  445. return PDC_ungetch(wch);
  446. }
  447. #endif