ng-jackspa.1: examples controlling from another process or another network host
[ng-jackspa.git] / curses.c
bloba8f2ce924fad808f01fb44c71259ed1f640d4f49
1 /* curses.c - helper functions related to curses
2 * Copyright © 2013 Géraud Meyer <graud@gmx.com>
4 * This file is part of ng-jackspa.
6 * ng-jackspa is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License version 2 as published by the
8 * Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
15 * You should have received a copy of the GNU General Public License along
16 * with ng-jackspa. If not, see <http://www.gnu.org/licenses/>.
19 #define CTRL(key) ((key)-64)
21 /* Read user text input from a curses window with line editing keys.
22 * The result is obtained from the window itself, not from the keyboard; a
23 * space marks the end of the input; if the text contains a ^O character, the
24 * input is cancelled (as soon as the ^O is processed).
25 * The terminal must not echo, and it should be in cbreak mode (rather than in
26 * line editing mode, aka cooked mode); a pseudo line editing terminal mode is
27 * emulated, with additional emacs-like edit/move keys.
29 int wtextentry(WINDOW *win, char *result, size_t size)
31 short s; /* state varibale */
32 int k; /* keyboard input */
33 char c; /* input char */
34 int row, col;
36 wclear(win);
38 for (s = 1; s > 0; )
39 switch(k = wgetch(win)) {
40 case KEY_CANCEL:
41 case CTRL('O'): /* cancel */
42 case '\e':
43 s = 2;
44 ungetch('\n');
45 break;
46 case KEY_ENTER:
47 case '\n': /* eof */
48 if (s != 1) s = -1;
49 else s = 0;
50 break;
51 case KEY_BACKSPACE: /* erase */
52 case '\b':
53 getyx(win, row, col), wmove(win, row, col-1), wdelch(win);
54 break;
55 case KEY_DC: /* delete */
56 case CTRL('D'):
57 wdelch(win);
58 break;
59 case CTRL('U'): /* kill */
60 wmove(win, 0, 0), wclrtoeol(win);
61 break;
62 case CTRL('K'):
63 wclrtoeol(win);
64 break;
65 case KEY_UP: /* default/previous input field */
66 case CTRL('P'):
67 wmove(win, 0, 0), wclrtoeol(win);
68 wmove(win, 0, 0), waddnstr(win, result, size);
69 break;
70 case CTRL('B'):
71 case KEY_LEFT:
72 getyx(win, row, col), wmove(win, row, col-1);
73 break;
74 case CTRL('F'):
75 case KEY_RIGHT:
76 getyx(win, row, col);
77 if (col+1 < size) wmove(win, row, col+1);
78 break;
79 case KEY_HOME:
80 case CTRL('A'):
81 wmove(win, 0, 0);
82 break;
83 case KEY_END:
84 case CTRL('E'):
85 getyx(win, row, col);
86 for (; col < size-1 && (c = mvwinch(win, row, col)&A_CHARTEXT) != ' '; col++);
87 break;
88 case ERR:
89 break;
90 default:
91 getyx(win, row, col);
92 waddch(win, k);
93 break;
96 if (s < 0)
97 return (result[0] = '\0', 1);
98 else {
99 for (col = 0; col < size-1 && (c = mvwinch(win, 0, col)&A_CHARTEXT) != ' '; col++)
100 result[col] = c;
101 result[col] = '\0';
104 return 0;