Added a crude function detector in the section regex for C code.
[mp-5.x.git] / mpv_curses.c
blob2f471ff2002462efdd24100599d20dc31b962adc
1 /*
3 Minimum Profit - Programmer Text Editor
5 Curses driver.
7 Copyright (C) 1991-2007 Angel Ortega <angel@triptico.com>
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 http://www.triptico.com
27 #include "config.h"
29 #ifdef CONFOPT_CURSES
31 #include <stdio.h>
32 #include <wchar.h>
33 #include <curses.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <signal.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39 #include <sys/stat.h>
40 #include <sys/ioctl.h>
42 #include "mpdm.h"
43 #include "mpsl.h"
45 #include "mp.h"
47 /** data **/
49 /* the curses attributes */
50 int * nc_attrs = NULL;
52 /* code for the 'normal' attribute */
53 static int normal_attr = 0;
55 /* current window */
56 static WINDOW * cw = NULL;
58 /* stack of windows */
59 static int n_stack = 0;
60 static WINDOW ** w_stack = NULL;
62 /* last attr set */
63 static int last_attr = 0;
65 /* timer function */
66 static int timer_msecs = 0;
67 static mpdm_t timer_func = NULL;
69 /** code **/
71 static void set_attr(void)
72 /* set the current and fill attributes */
74 wattrset(cw, nc_attrs[last_attr]);
75 wbkgdset(cw, ' ' | nc_attrs[last_attr]);
79 static void nc_sigwinch(int s)
80 /* SIGWINCH signal handler */
82 mpdm_t v;
84 #ifdef NCURSES_VERSION
85 /* Make sure that window size changes... */
86 struct winsize ws;
88 int fd = open("/dev/tty", O_RDWR);
90 if (fd == -1)
91 return; /* This should never have to happen! */
93 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
94 resizeterm(ws.ws_row, ws.ws_col);
96 close(fd);
97 #else
98 /* restart curses */
99 /* ... */
100 #endif
102 /* invalidate main window */
103 clearok(stdscr, 1);
104 refresh();
106 /* re-set dimensions */
107 v = mpdm_hget_s(mp, L"window");
108 mpdm_hset_s(v, L"tx", MPDM_I(COLS));
109 mpdm_hset_s(v, L"ty", MPDM_I(LINES));
111 /* reattach */
112 signal(SIGWINCH, nc_sigwinch);
116 #ifdef CONFOPT_WGET_WCH
117 int wget_wch(WINDOW * w, wint_t *ch);
118 #endif
120 static wchar_t * nc_getwch(void)
121 /* gets a key as a wchar_t */
123 static wchar_t c[2];
125 #ifdef CONFOPT_WGET_WCH
127 /* set timer period */
128 if (timer_msecs > 0)
129 timeout(timer_msecs);
131 if (wget_wch(stdscr, (wint_t *)c) == -1)
132 c[0] = (wchar_t) -1;
134 #else
135 char tmp[MB_CUR_MAX + 1];
136 int cc, n = 0;
138 /* read one byte */
139 cc = wgetch(cw);
140 if (has_key(cc)) {
141 c[0] = cc;
142 return c;
145 /* set to non-blocking */
146 nodelay(cw, 1);
148 /* read all possible following characters */
149 tmp[n++] = cc;
150 while (n < sizeof(tmp) - 1 && (cc = getch()) != ERR)
151 tmp[n++] = cc;
153 /* sets input as blocking */
154 nodelay(cw, 0);
156 tmp[n] = '\0';
157 mbstowcs(c, tmp, n);
158 #endif
160 c[1] = '\0';
161 return c;
165 #define ctrl(k) ((k) & 31)
167 static mpdm_t nc_getkey(mpdm_t args)
168 /* reads a key and converts to an action */
170 static int shift = 0;
171 wchar_t * f = NULL;
172 mpdm_t k = NULL;
174 /* any pending key? return it */
175 if ((k = mp_pending_key()) != NULL)
176 return k;
178 f = nc_getwch();
180 if (f[0] == -1) {
181 mpdm_exec(timer_func, NULL);
182 return NULL;
185 if (shift) {
186 switch (f[0]) {
187 case L'0': f = L"f10"; break;
188 case L'1': f = L"f1"; break;
189 case L'2': f = L"f2"; break;
190 case L'3': f = L"f3"; break;
191 case L'4': f = L"f4"; break;
192 case L'5': f = L"f5"; break;
193 case L'6': f = L"f6"; break;
194 case L'7': f = L"f7"; break;
195 case L'8': f = L"f8"; break;
196 case L'9': f = L"f9"; break;
197 case KEY_LEFT: f = L"ctrl-cursor-left"; break;
198 case KEY_RIGHT: f = L"ctrl-cursor-right"; break;
199 case KEY_DOWN: f = L"ctrl-cursor-down"; break;
200 case KEY_UP: f = L"ctrl-cursor-up"; break;
201 case KEY_END: f = L"ctrl-end"; break;
202 case KEY_HOME: f = L"ctrl-home"; break;
203 case L'\r': f = L"ctrl-enter"; break;
204 case L'\e': f = L"escape"; break;
205 case KEY_ENTER: f = L"ctrl-enter"; break;
206 case L' ': f = L"ctrl-space"; break;
207 case L'a': f = L"ctrl-a"; break;
208 case L'b': f = L"ctrl-b"; break;
209 case L'c': f = L"ctrl-c"; break;
210 case L'd': f = L"ctrl-d"; break;
211 case L'e': f = L"ctrl-e"; break;
212 case L'f': f = L"ctrl-f"; break;
213 case L'g': f = L"ctrl-g"; break;
214 case L'h': f = L"ctrl-h"; break;
215 case L'i': f = L"ctrl-i"; break;
216 case L'j': f = L"ctrl-j"; break;
217 case L'k': f = L"ctrl-k"; break;
218 case L'l': f = L"ctrl-l"; break;
219 case L'm': f = L"ctrl-m"; break;
220 case L'n': f = L"ctrl-n"; break;
221 case L'o': f = L"ctrl-o"; break;
222 case L'p': f = L"ctrl-p"; break;
223 case L'q': f = L"ctrl-q"; break;
224 case L'r': f = L"ctrl-r"; break;
225 case L's': f = L"ctrl-s"; break;
226 case L't': f = L"ctrl-t"; break;
227 case L'u': f = L"ctrl-u"; break;
228 case L'v': f = L"ctrl-v"; break;
229 case L'w': f = L"ctrl-w"; break;
230 case L'x': f = L"ctrl-x"; break;
231 case L'y': f = L"ctrl-y"; break;
232 case L'z': f = L"ctrl-z"; break;
235 shift = 0;
237 else {
238 switch (f[0]) {
239 case KEY_LEFT: f = L"cursor-left"; break;
240 case KEY_RIGHT: f = L"cursor-right"; break;
241 case KEY_UP: f = L"cursor-up"; break;
242 case KEY_DOWN: f = L"cursor-down"; break;
243 case KEY_PPAGE: f = L"page-up"; break;
244 case KEY_NPAGE: f = L"page-down"; break;
245 case KEY_HOME: f = L"home"; break;
246 case KEY_END: f = L"end"; break;
247 case KEY_LL: f = L"end"; break;
248 case KEY_IC: f = L"insert"; break;
249 case KEY_DC: f = L"delete"; break;
250 case 0x7f:
251 case KEY_BACKSPACE:
252 case L'\b': f = L"backspace"; break;
253 case L'\r':
254 case KEY_ENTER: f = L"enter"; break;
255 case L'\t': f = L"tab"; break;
256 case KEY_BTAB: f = L"shift-tab"; break;
257 case L' ': f = L"space"; break;
258 case KEY_F(1): f = L"f1"; break;
259 case KEY_F(2): f = L"f2"; break;
260 case KEY_F(3): f = L"f3"; break;
261 case KEY_F(4): f = L"f4"; break;
262 case KEY_F(5): f = L"f5"; break;
263 case KEY_F(6): f = L"f6"; break;
264 case KEY_F(7): f = L"f7"; break;
265 case KEY_F(8): f = L"f8"; break;
266 case KEY_F(9): f = L"f9"; break;
267 case KEY_F(10): f = L"f10"; break;
268 case ctrl(' '): f = L"ctrl-space"; break;
269 case ctrl('a'): f = L"ctrl-a"; break;
270 case ctrl('b'): f = L"ctrl-b"; break;
271 case ctrl('c'): f = L"ctrl-c"; break;
272 case ctrl('d'): f = L"ctrl-d"; break;
273 case ctrl('e'): f = L"ctrl-e"; break;
274 case ctrl('f'): f = L"ctrl-f"; break;
275 case ctrl('g'): f = L"ctrl-g"; break;
276 case ctrl('j'): f = L"ctrl-j"; break;
277 case ctrl('k'): f = L"ctrl-k"; break;
278 case ctrl('l'): f = L"ctrl-l"; break;
279 case ctrl('n'): f = L"ctrl-n"; break;
280 case ctrl('o'): f = L"ctrl-o"; break;
281 case ctrl('p'): f = L"ctrl-p"; break;
282 case ctrl('q'): f = L"ctrl-q"; break;
283 case ctrl('r'): f = L"ctrl-r"; break;
284 case ctrl('s'): f = L"ctrl-s"; break;
285 case ctrl('t'): f = L"ctrl-t"; break;
286 case ctrl('u'): f = L"ctrl-u"; break;
287 case ctrl('v'): f = L"ctrl-v"; break;
288 case ctrl('w'): f = L"ctrl-w"; break;
289 case ctrl('x'): f = L"ctrl-x"; break;
290 case ctrl('y'): f = L"ctrl-y"; break;
291 case ctrl('z'): f = L"ctrl-z"; break;
292 case L'\e': shift = 1; f = NULL; break;
296 /* no known key? do nothing */
297 if(f == NULL)
298 return NULL;
300 return mp_process_keyseq(MPDM_S(f));
304 static mpdm_t nc_addwstr(mpdm_t str)
305 /* draws a string */
307 wchar_t * wptr = mpdm_string(str);
309 #ifndef CONFOPT_ADDWSTR
310 char * cptr;
312 cptr = mpdm_wcstombs(wptr, NULL);
313 waddstr(cw, cptr);
314 free(cptr);
316 #else
317 waddwstr(cw, wptr);
318 #endif /* CONFOPT_ADDWSTR */
320 return NULL;
324 static void draw_status(void)
325 /* draws the status bar */
327 mpdm_t t;
329 t = mp_build_status_line();
331 /* move to the last line, clear it and draw there */
332 wmove(cw, LINES - 1, 0);
333 wattrset(cw, nc_attrs[normal_attr]);
334 wclrtoeol(cw);
335 nc_addwstr(t);
337 /* draw the menu key hint, right-aligned */
338 if ((t = mpdm_hget_s(mp, L"menu_key_hint")) == NULL) {
339 t = MPDM_LS(L"ctrl-a: ");
340 t = mpdm_strcat(t, mpdm_gettext(MPDM_LS(L"Menu")));
341 mpdm_hset_s(mp, L"menu_key_hint", t);
344 wmove(cw, LINES - 1, COLS - mpdm_size(t));
345 nc_addwstr(t);
349 static void nc_draw(mpdm_t doc)
350 /* driver drawing function for cursesw */
352 mpdm_t d;
353 int n, m;
355 werase(cw);
357 d = mp_draw(doc, 0);
359 for (n = 0; n < mpdm_size(d); n++) {
360 mpdm_t l = mpdm_aget(d, n);
362 wmove(cw, n, 0);
364 for (m = 0; m < mpdm_size(l); m++) {
365 int attr;
366 mpdm_t s;
368 /* get the attribute and the string */
369 attr = mpdm_ival(mpdm_aget(l, m++));
370 s = mpdm_aget(l, m);
372 wattrset(cw, nc_attrs[attr]);
373 nc_addwstr(s);
377 draw_status();
379 wrefresh(cw);
383 static void build_colors(void)
384 /* builds the colors */
386 mpdm_t colors;
387 mpdm_t color_names;
388 mpdm_t l;
389 mpdm_t c;
390 int n, s;
392 #ifdef CONFOPT_TRANSPARENCY
393 use_default_colors();
395 #define DEFAULT_INK -1
396 #define DEFAULT_PAPER -1
398 #else /* CONFOPT_TRANSPARENCY */
400 #define DEFAULT_INK COLOR_BLACK
401 #define DEFAULT_PAPER COLOR_WHITE
403 #endif
405 /* gets the color definitions and attribute names */
406 colors = mpdm_hget_s(mp, L"colors");
407 color_names = mpdm_hget_s(mp, L"color_names");
408 l = mpdm_keys(colors);
409 s = mpdm_size(l);
411 /* redim the structures */
412 nc_attrs = realloc(nc_attrs, sizeof(int) * s);
414 /* loop the colors */
415 for (n = 0; n < s && (c = mpdm_aget(l, n)) != NULL; n++) {
416 mpdm_t d = mpdm_hget(colors, c);
417 mpdm_t v = mpdm_hget_s(d, L"text");
418 int cp, c0, c1;
420 /* store the 'normal' attribute */
421 if (wcscmp(mpdm_string(c), L"normal") == 0)
422 normal_attr = n;
424 /* store the attr */
425 mpdm_hset_s(d, L"attr", MPDM_I(n));
427 /* get color indexes */
428 if ((c0 = mpdm_seek(color_names, mpdm_aget(v, 0), 1)) == -1 ||
429 (c1 = mpdm_seek(color_names, mpdm_aget(v, 1), 1)) == -1)
430 continue;
432 init_pair(n + 1, c0 - 1, c1 - 1);
433 cp = COLOR_PAIR(n + 1);
435 /* flags */
436 v = mpdm_hget_s(d, L"flags");
437 if (mpdm_seek_s(v, L"reverse", 1) != -1)
438 cp |= A_REVERSE;
439 if (mpdm_seek_s(v, L"bright", 1) != -1)
440 cp |= A_BOLD;
441 if (mpdm_seek_s(v, L"underline", 1) != -1)
442 cp |= A_UNDERLINE;
444 nc_attrs[n] = cp;
447 /* set the background filler */
448 wbkgdset(cw, ' ' | nc_attrs[normal_attr]);
452 /** driver functions **/
454 static mpdm_t ncursesw_drv_timer(mpdm_t a)
456 mpdm_t func = mpdm_aget(a, 1);
457 mpdm_t r;
459 timer_msecs = mpdm_ival(mpdm_aget(a, 0));
461 r = mpdm_unref(timer_func);
462 timer_func = mpdm_ref(func);
464 return r;
468 static mpdm_t ncursesw_drv_main_loop(mpdm_t a)
469 /* curses driver main loop */
471 while (! mp_exit_requested) {
472 /* get current document and draw it */
473 nc_draw(mp_active());
475 /* get key and process it */
476 mp_process_event(nc_getkey(NULL));
479 return NULL;
483 static mpdm_t ncursesw_drv_shutdown(mpdm_t a)
485 mpdm_t v;
487 endwin();
489 if ((v = mpdm_hget_s(mp, L"exit_message")) != NULL) {
490 mpdm_write_wcs(stdout, mpdm_string(v));
491 printf("\n");
494 return NULL;
498 /** TUI **/
500 static mpdm_t tui_addstr(mpdm_t a)
501 /* TUI: add a string */
503 return nc_addwstr(mpdm_aget(a, 0));
507 static mpdm_t tui_move(mpdm_t a)
508 /* TUI: move to a screen position */
510 /* curses' move() use y, x */
511 wmove(cw, mpdm_ival(mpdm_aget(a, 1)), mpdm_ival(mpdm_aget(a, 0)));
513 /* if third argument is not NULL, clear line */
514 if (mpdm_aget(a, 2) != NULL)
515 wclrtoeol(cw);
517 return NULL;
521 static mpdm_t tui_attr(mpdm_t a)
522 /* TUI: set attribute for next string */
524 last_attr = mpdm_ival(mpdm_aget(a, 0));
526 set_attr();
528 return NULL;
532 static mpdm_t tui_refresh(mpdm_t a)
533 /* TUI: refresh the screen */
535 wrefresh(cw);
536 return NULL;
540 static mpdm_t tui_getxy(mpdm_t a)
541 /* TUI: returns the x and y cursor position */
543 mpdm_t v;
544 int x, y;
546 getyx(cw, y, x);
548 v = MPDM_A(2);
549 mpdm_aset(v, MPDM_I(x), 0);
550 mpdm_aset(v, MPDM_I(y), 1);
552 return v;
556 static mpdm_t tui_openpanel(mpdm_t a)
557 /* opens a panel (creates new window) */
559 n_stack++;
560 w_stack = realloc(w_stack, n_stack * sizeof(WINDOW *));
561 cw = w_stack[n_stack - 1] = newwin(mpdm_ival(mpdm_aget(a, 3)),
562 mpdm_ival(mpdm_aget(a, 2)),
563 mpdm_ival(mpdm_aget(a, 1)),
564 mpdm_ival(mpdm_aget(a, 0)));
566 set_attr();
567 wclrtobot(cw);
568 box(cw, 0, 0);
570 return NULL;
574 static mpdm_t tui_closepanel(mpdm_t a)
575 /* closes a panel (deletes last window) */
577 n_stack--;
578 delwin(w_stack[n_stack]);
580 w_stack = realloc(w_stack, n_stack * sizeof(WINDOW *));
581 cw = n_stack == 0 ? stdscr : w_stack[n_stack - 1];
583 touchwin(cw);
584 wrefresh(cw);
586 return NULL;
590 static void register_functions(void)
592 mpdm_t drv;
593 mpdm_t tui;
595 drv = mpdm_hget_s(mp, L"drv");
596 mpdm_hset_s(drv, L"timer", MPDM_X(ncursesw_drv_timer));
597 mpdm_hset_s(drv, L"main_loop", MPDM_X(ncursesw_drv_main_loop));
598 mpdm_hset_s(drv, L"shutdown", MPDM_X(ncursesw_drv_shutdown));
600 tui = mpsl_eval(MPDM_LS(L"load('mp_tui.mpsl');"), NULL);
602 /* FIXME: if tui failed, a fatal error must be shown */
604 /* if((e = mpdm_hget_s(mpdm_root(), L"ERROR")) != NULL)
606 mpdm_write_wcs(stdout, mpdm_string(e));
607 printf("\n");
609 return(0);
612 /* execute tui */
613 mpdm_hset_s(tui, L"getkey", MPDM_X(nc_getkey));
614 mpdm_hset_s(tui, L"addstr", MPDM_X(tui_addstr));
615 mpdm_hset_s(tui, L"move", MPDM_X(tui_move));
616 mpdm_hset_s(tui, L"attr", MPDM_X(tui_attr));
617 mpdm_hset_s(tui, L"refresh", MPDM_X(tui_refresh));
618 mpdm_hset_s(tui, L"getxy", MPDM_X(tui_getxy));
619 mpdm_hset_s(tui, L"openpanel", MPDM_X(tui_openpanel));
620 mpdm_hset_s(tui, L"closepanel", MPDM_X(tui_closepanel));
624 static mpdm_t ncursesw_drv_startup(mpdm_t a)
626 mpdm_t v;
628 register_functions();
630 initscr();
631 start_color();
632 keypad(stdscr, TRUE);
633 nonl();
634 raw();
635 noecho();
637 build_colors();
639 v = mpdm_hget_s(mp, L"window");
640 mpdm_hset_s(v, L"tx", MPDM_I(COLS));
641 mpdm_hset_s(v, L"ty", MPDM_I(LINES));
643 signal(SIGWINCH, nc_sigwinch);
645 cw = stdscr;
647 return NULL;
651 int ncursesw_drv_detect(int * argc, char *** argv)
653 mpdm_t drv;
655 drv = mpdm_hget_s(mp, L"drv");
656 mpdm_hset_s(drv, L"id", MPDM_LS(L"curses"));
657 mpdm_hset_s(drv, L"startup", MPDM_X(ncursesw_drv_startup));
659 return 1;
662 #endif /* CONFOPT_CURSES */