Added support for local sessions (Closes: #1234).
[mp-5.x.git] / mpv_curses.c
blob5770744026f7099eb45a02aa3057ab2408d6a09f
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 /*******************
48 Data
49 ********************/
51 /* the curses attributes */
52 int * nc_attrs = NULL;
54 /* code for the 'normal' attribute */
55 static int normal_attr = 0;
57 /* current window */
58 static WINDOW * cw = NULL;
60 /* stack of windows */
61 static int n_stack = 0;
62 static WINDOW ** w_stack = NULL;
64 /* last attr set */
65 static int last_attr = 0;
67 /* timer function */
68 static int timer_msecs = 0;
69 static mpdm_t timer_func = NULL;
71 /*******************
72 Code
73 ********************/
75 static void set_attr(void)
76 /* set the current and fill attributes */
78 wattrset(cw, nc_attrs[last_attr]);
79 wbkgdset(cw, ' ' | nc_attrs[last_attr]);
83 static void nc_sigwinch(int s)
84 /* SIGWINCH signal handler */
86 mpdm_t v;
88 #ifdef NCURSES_VERSION
89 /* Make sure that window size changes... */
90 struct winsize ws;
92 int fd = open("/dev/tty", O_RDWR);
94 if (fd == -1) return; /* This should never have to happen! */
96 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
97 resizeterm(ws.ws_row, ws.ws_col);
99 close(fd);
100 #else
101 /* restart curses */
102 /* ... */
103 #endif
105 /* invalidate main window */
106 clearok(stdscr, 1);
107 refresh();
109 /* re-set dimensions */
110 v = mpdm_hget_s(mp, L"window");
111 mpdm_hset_s(v, L"tx", MPDM_I(COLS));
112 mpdm_hset_s(v, L"ty", MPDM_I(LINES));
114 /* reattach */
115 signal(SIGWINCH, nc_sigwinch);
119 #ifdef CONFOPT_WGET_WCH
120 int wget_wch(WINDOW * w, wint_t *ch);
121 #endif
123 static wchar_t * nc_getwch(void)
124 /* gets a key as a wchar_t */
126 static wchar_t c[2];
128 #ifdef CONFOPT_WGET_WCH
130 /* set timer period */
131 if(timer_msecs > 0)
132 timeout(timer_msecs);
134 if(wget_wch(stdscr, (wint_t *)c) == -1)
135 c[0] = (wchar_t) -1;
137 #else
138 char tmp[MB_CUR_MAX + 1];
139 int cc, n = 0;
141 /* read one byte */
142 cc = wgetch(cw);
143 if(has_key(cc))
145 c[0] = cc;
146 return(c);
149 /* set to non-blocking */
150 nodelay(cw, 1);
152 /* read all possible following characters */
153 tmp[n++] = cc;
154 while(n < sizeof(tmp) - 1 && (cc = getch()) != ERR)
155 tmp[n++] = cc;
157 /* sets input as blocking */
158 nodelay(cw, 0);
160 tmp[n] = '\0';
161 mbstowcs(c, tmp, n);
162 #endif
164 c[1] = '\0';
165 return(c);
169 #define ctrl(k) ((k) & 31)
171 static mpdm_t nc_getkey(mpdm_t args)
172 /* reads a key and converts to an action */
174 static int shift = 0;
175 wchar_t * f = NULL;
176 mpdm_t k = NULL;
178 /* any pending key? return it */
179 if((k = mp_pending_key()) != NULL)
180 return(k);
182 f = nc_getwch();
184 if(f[0] == -1)
186 mpdm_exec(timer_func, NULL);
187 return(NULL);
190 if(shift)
192 switch(f[0]) {
193 case L'0': f = L"f10"; break;
194 case L'1': f = L"f1"; break;
195 case L'2': f = L"f2"; break;
196 case L'3': f = L"f3"; break;
197 case L'4': f = L"f4"; break;
198 case L'5': f = L"f5"; break;
199 case L'6': f = L"f6"; break;
200 case L'7': f = L"f7"; break;
201 case L'8': f = L"f8"; break;
202 case L'9': f = L"f9"; break;
203 case KEY_LEFT: f = L"ctrl-cursor-left"; break;
204 case KEY_RIGHT: f = L"ctrl-cursor-right"; break;
205 case KEY_DOWN: f = L"ctrl-cursor-down"; break;
206 case KEY_UP: f = L"ctrl-cursor-up"; break;
207 case KEY_END: f = L"ctrl-end"; break;
208 case KEY_HOME: f = L"ctrl-home"; break;
209 case L'\r': f = L"ctrl-enter"; break;
210 case L'\e': f = L"escape"; break;
211 case KEY_ENTER: f = L"ctrl-enter"; break;
212 case L' ': f = L"ctrl-space"; break;
213 case L'a': f = L"ctrl-a"; break;
214 case L'b': f = L"ctrl-b"; break;
215 case L'c': f = L"ctrl-c"; break;
216 case L'd': f = L"ctrl-d"; break;
217 case L'e': f = L"ctrl-e"; break;
218 case L'f': f = L"ctrl-f"; break;
219 case L'g': f = L"ctrl-g"; break;
220 case L'h': f = L"ctrl-h"; break;
221 case L'i': f = L"ctrl-i"; break;
222 case L'j': f = L"ctrl-j"; break;
223 case L'k': f = L"ctrl-k"; break;
224 case L'l': f = L"ctrl-l"; break;
225 case L'm': f = L"ctrl-m"; break;
226 case L'n': f = L"ctrl-n"; break;
227 case L'o': f = L"ctrl-o"; break;
228 case L'p': f = L"ctrl-p"; break;
229 case L'q': f = L"ctrl-q"; break;
230 case L'r': f = L"ctrl-r"; break;
231 case L's': f = L"ctrl-s"; break;
232 case L't': f = L"ctrl-t"; break;
233 case L'u': f = L"ctrl-u"; break;
234 case L'v': f = L"ctrl-v"; break;
235 case L'w': f = L"ctrl-w"; break;
236 case L'x': f = L"ctrl-x"; break;
237 case L'y': f = L"ctrl-y"; break;
238 case L'z': f = L"ctrl-z"; break;
241 shift = 0;
243 else
245 switch(f[0]) {
246 case KEY_LEFT: f = L"cursor-left"; break;
247 case KEY_RIGHT: f = L"cursor-right"; break;
248 case KEY_UP: f = L"cursor-up"; break;
249 case KEY_DOWN: f = L"cursor-down"; break;
250 case KEY_PPAGE: f = L"page-up"; break;
251 case KEY_NPAGE: f = L"page-down"; break;
252 case KEY_HOME: f = L"home"; break;
253 case KEY_END: f = L"end"; break;
254 case KEY_IC: f = L"insert"; break;
255 case KEY_DC: f = L"delete"; break;
256 case 0x7f:
257 case KEY_BACKSPACE:
258 case L'\b': f = L"backspace"; break;
259 case L'\r':
260 case KEY_ENTER: f = L"enter"; break;
261 case L'\t': f = L"tab"; break;
262 case L' ': f = L"space"; break;
263 case KEY_F(1): f = L"f1"; break;
264 case KEY_F(2): f = L"f2"; break;
265 case KEY_F(3): f = L"f3"; break;
266 case KEY_F(4): f = L"f4"; break;
267 case KEY_F(5): f = L"f5"; break;
268 case KEY_F(6): f = L"f6"; break;
269 case KEY_F(7): f = L"f7"; break;
270 case KEY_F(8): f = L"f8"; break;
271 case KEY_F(9): f = L"f9"; break;
272 case KEY_F(10): f = L"f10"; break;
273 case ctrl(' '): f = L"ctrl-space"; break;
274 case ctrl('a'): f = L"ctrl-a"; break;
275 case ctrl('b'): f = L"ctrl-b"; break;
276 case ctrl('c'): f = L"ctrl-c"; break;
277 case ctrl('d'): f = L"ctrl-d"; break;
278 case ctrl('e'): f = L"ctrl-e"; break;
279 case ctrl('f'): f = L"ctrl-f"; break;
280 case ctrl('g'): f = L"ctrl-g"; break;
281 case ctrl('j'): f = L"ctrl-j"; break;
282 case ctrl('k'): f = L"ctrl-k"; break;
283 case ctrl('l'): f = L"ctrl-l"; break;
284 case ctrl('n'): f = L"ctrl-n"; break;
285 case ctrl('o'): f = L"ctrl-o"; break;
286 case ctrl('p'): f = L"ctrl-p"; break;
287 case ctrl('q'): f = L"ctrl-q"; break;
288 case ctrl('r'): f = L"ctrl-r"; break;
289 case ctrl('s'): f = L"ctrl-s"; break;
290 case ctrl('t'): f = L"ctrl-t"; break;
291 case ctrl('u'): f = L"ctrl-u"; break;
292 case ctrl('v'): f = L"ctrl-v"; break;
293 case ctrl('w'): f = L"ctrl-w"; break;
294 case ctrl('x'): f = L"ctrl-x"; break;
295 case ctrl('y'): f = L"ctrl-y"; break;
296 case ctrl('z'): f = L"ctrl-z"; break;
297 case L'\e': shift = 1; f = NULL; break;
301 /* no known key? do nothing */
302 if(f == NULL) return(NULL);
304 return(mp_process_keyseq(MPDM_S(f)));
308 static mpdm_t nc_addwstr(mpdm_t str)
309 /* draws a string */
311 wchar_t * wptr = mpdm_string(str);
313 #ifndef CONFOPT_ADDWSTR
314 char * cptr;
316 cptr = mpdm_wcstombs(wptr, NULL);
317 waddstr(cw, cptr);
318 free(cptr);
320 #else
321 waddwstr(cw, wptr);
322 #endif /* CONFOPT_ADDWSTR */
324 return(NULL);
328 static void draw_status(void)
329 /* draws the status bar */
331 mpdm_t t;
333 t = mp_build_status_line();
335 /* move to the last line, clear it and draw there */
336 wmove(cw, LINES - 1, 0);
337 wattrset(cw, nc_attrs[normal_attr]);
338 wclrtoeol(cw);
339 nc_addwstr(t);
341 /* draw the menu key hint, right-aligned */
342 if((t = mpdm_hget_s(mp, L"menu_key_hint")) != NULL)
344 wmove(cw, LINES - 1, COLS - mpdm_size(t));
345 nc_addwstr(t);
350 static void nc_draw(mpdm_t doc)
351 /* driver drawing function for cursesw */
353 mpdm_t d;
354 int n, m;
356 werase(cw);
358 d = mp_draw(doc, 0);
360 for(n = 0;n < mpdm_size(d);n++)
362 mpdm_t l = mpdm_aget(d, n);
364 wmove(cw, n, 0);
366 for(m = 0;m < mpdm_size(l);m++)
368 int attr;
369 mpdm_t s;
371 /* get the attribute and the string */
372 attr = mpdm_ival(mpdm_aget(l, m++));
373 s = mpdm_aget(l, m);
375 wattrset(cw, nc_attrs[attr]);
376 nc_addwstr(s);
380 draw_status();
382 wrefresh(cw);
386 static void build_colors(void)
387 /* builds the colors */
389 mpdm_t colors;
390 mpdm_t color_names;
391 mpdm_t l;
392 mpdm_t c;
393 int n, s;
395 #ifdef CONFOPT_TRANSPARENCY
396 use_default_colors();
398 #define DEFAULT_INK -1
399 #define DEFAULT_PAPER -1
401 #else /* CONFOPT_TRANSPARENCY */
403 #define DEFAULT_INK COLOR_BLACK
404 #define DEFAULT_PAPER COLOR_WHITE
406 #endif
408 /* gets the color definitions and attribute names */
409 colors = mpdm_hget_s(mp, L"colors");
410 color_names = mpdm_hget_s(mp, L"color_names");
411 l = mpdm_keys(colors);
412 s = mpdm_size(l);
414 /* redim the structures */
415 nc_attrs = realloc(nc_attrs, sizeof(int) * s);
417 /* loop the colors */
418 for(n = 0;n < s && (c = mpdm_aget(l, n)) != NULL;n++)
420 mpdm_t d = mpdm_hget(colors, c);
421 mpdm_t v = mpdm_hget_s(d, L"text");
422 int cp, c0, c1;
424 /* store the 'normal' attribute */
425 if(wcscmp(mpdm_string(c), L"normal") == 0)
426 normal_attr = n;
428 /* store the attr */
429 mpdm_hset_s(d, L"attr", MPDM_I(n));
431 /* get color indexes */
432 if((c0 = mpdm_seek(color_names, mpdm_aget(v, 0), 1)) == -1 ||
433 (c1 = mpdm_seek(color_names, mpdm_aget(v, 1), 1)) == -1)
434 continue;
436 init_pair(n + 1, c0 - 1, c1 - 1);
437 cp = COLOR_PAIR(n + 1);
439 /* flags */
440 v = mpdm_hget_s(d, L"flags");
441 if(mpdm_seek_s(v, L"reverse", 1) != -1) cp |= A_REVERSE;
442 if(mpdm_seek_s(v, L"bright", 1) != -1) cp |= A_BOLD;
443 if(mpdm_seek_s(v, L"underline", 1) != -1) cp |= A_UNDERLINE;
445 nc_attrs[n] = cp;
448 /* set the background filler */
449 wbkgdset(cw, ' ' | nc_attrs[normal_attr]);
453 static mpdm_t ncursesw_drv_timer(mpdm_t a)
455 mpdm_t func = mpdm_aget(a, 1);
456 mpdm_t r;
458 timer_msecs = mpdm_ival(mpdm_aget(a, 0));
460 r = mpdm_unref(timer_func);
461 timer_func = mpdm_ref(func);
463 return(r);
467 static mpdm_t ncursesw_drv_main_loop(mpdm_t a)
468 /* curses driver main loop */
470 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 endwin();
486 return(NULL);
490 /* TUI */
492 static mpdm_t tui_addstr(mpdm_t a)
493 /* TUI: add a string */
495 return(nc_addwstr(mpdm_aget(a, 0)));
499 static mpdm_t tui_move(mpdm_t a)
500 /* TUI: move to a screen position */
502 /* curses' move() use y, x */
503 wmove(cw, mpdm_ival(mpdm_aget(a, 1)), mpdm_ival(mpdm_aget(a, 0)));
505 /* if third argument is not NULL, clear line */
506 if(mpdm_aget(a, 2) != NULL)
507 wclrtoeol(cw);
509 return(NULL);
513 static mpdm_t tui_attr(mpdm_t a)
514 /* TUI: set attribute for next string */
516 last_attr = mpdm_ival(mpdm_aget(a, 0));
518 set_attr();
520 return(NULL);
524 static mpdm_t tui_refresh(mpdm_t a)
525 /* TUI: refresh the screen */
527 wrefresh(cw);
528 return(NULL);
532 static mpdm_t tui_getxy(mpdm_t a)
533 /* TUI: returns the x and y cursor position */
535 mpdm_t v;
536 int x, y;
538 getyx(cw, y, x);
540 v = MPDM_A(2);
541 mpdm_aset(v, MPDM_I(x), 0);
542 mpdm_aset(v, MPDM_I(y), 1);
543 return(v);
547 static mpdm_t tui_openpanel(mpdm_t a)
548 /* opens a panel (creates new window) */
550 n_stack++;
551 w_stack = realloc(w_stack, n_stack * sizeof(WINDOW *));
552 cw = w_stack[n_stack - 1] = newwin(mpdm_ival(mpdm_aget(a, 3)),
553 mpdm_ival(mpdm_aget(a, 2)),
554 mpdm_ival(mpdm_aget(a, 1)),
555 mpdm_ival(mpdm_aget(a, 0)));
557 set_attr();
558 wclrtobot(cw);
559 box(cw, 0, 0);
561 return(NULL);
565 static mpdm_t tui_closepanel(mpdm_t a)
566 /* closes a panel (deletes last window) */
568 n_stack--;
569 delwin(w_stack[n_stack]);
571 w_stack = realloc(w_stack, n_stack * sizeof(WINDOW *));
572 cw = n_stack == 0 ? stdscr : w_stack[n_stack - 1];
574 touchwin(cw);
575 wrefresh(cw);
577 return(NULL);
581 static void register_functions(void)
583 mpdm_t drv;
584 mpdm_t tui;
586 drv = mpdm_hget_s(mp, L"drv");
587 mpdm_hset_s(drv, L"timer", MPDM_X(ncursesw_drv_timer));
588 mpdm_hset_s(drv, L"main_loop", MPDM_X(ncursesw_drv_main_loop));
589 mpdm_hset_s(drv, L"shutdown", MPDM_X(ncursesw_drv_shutdown));
591 tui = mpsl_eval(MPDM_LS(L"load('mp_tui.mpsl');"), NULL);
593 /* FIXME: if tui failed, a fatal error must be shown */
595 /* if((e = mpdm_hget_s(mpdm_root(), L"ERROR")) != NULL)
597 mpdm_write_wcs(stdout, mpdm_string(e));
598 printf("\n");
600 return(0);
603 /* execute tui */
604 mpdm_hset_s(tui, L"getkey", MPDM_X(nc_getkey));
605 mpdm_hset_s(tui, L"addstr", MPDM_X(tui_addstr));
606 mpdm_hset_s(tui, L"move", MPDM_X(tui_move));
607 mpdm_hset_s(tui, L"attr", MPDM_X(tui_attr));
608 mpdm_hset_s(tui, L"refresh", MPDM_X(tui_refresh));
609 mpdm_hset_s(tui, L"getxy", MPDM_X(tui_getxy));
610 mpdm_hset_s(tui, L"openpanel", MPDM_X(tui_openpanel));
611 mpdm_hset_s(tui, L"closepanel", MPDM_X(tui_closepanel));
615 static mpdm_t ncursesw_drv_startup(mpdm_t a)
617 mpdm_t v;
619 register_functions();
621 initscr();
622 start_color();
623 keypad(stdscr, TRUE);
624 nonl();
625 raw();
626 noecho();
628 build_colors();
630 v = mpdm_hget_s(mp, L"window");
631 mpdm_hset_s(v, L"tx", MPDM_I(COLS));
632 mpdm_hset_s(v, L"ty", MPDM_I(LINES));
634 signal(SIGWINCH, nc_sigwinch);
636 cw = stdscr;
638 return(NULL);
642 int ncursesw_drv_detect(int * argc, char *** argv)
644 mpdm_t drv;
646 drv = mpdm_hget_s(mp, L"drv");
647 mpdm_hset_s(drv, L"id", MPDM_LS(L"curses"));
648 mpdm_hset_s(drv, L"startup", MPDM_X(ncursesw_drv_startup));
650 return(1);
653 #endif /* CONFOPT_CURSES */