3 Minimum Profit - Programmer Text Editor
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
40 #include <sys/ioctl.h>
51 /* the curses attributes */
52 int * nc_attrs
= NULL
;
54 /* code for the 'normal' attribute */
55 static int normal_attr
= 0;
58 static WINDOW
* cw
= NULL
;
60 /* stack of windows */
61 static int n_stack
= 0;
62 static WINDOW
** w_stack
= NULL
;
65 static int last_attr
= 0;
68 static int timer_msecs
= 0;
69 static mpdm_t timer_func
= NULL
;
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 */
88 #ifdef NCURSES_VERSION
89 /* Make sure that window size changes... */
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
);
105 /* invalidate main window */
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
));
115 signal(SIGWINCH
, nc_sigwinch
);
119 #ifdef CONFOPT_WGET_WCH
120 int wget_wch(WINDOW
* w
, wint_t *ch
);
123 static wchar_t * nc_getwch(void)
124 /* gets a key as a wchar_t */
128 #ifdef CONFOPT_WGET_WCH
130 /* set timer period */
132 timeout(timer_msecs
);
134 if(wget_wch(stdscr
, (wint_t *)c
) == -1)
138 char tmp
[MB_CUR_MAX
+ 1];
149 /* set to non-blocking */
152 /* read all possible following characters */
154 while(n
< sizeof(tmp
) - 1 && (cc
= getch()) != ERR
)
157 /* sets input as blocking */
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;
178 /* any pending key? return it */
179 if((k
= mp_pending_key()) != NULL
)
186 mpdm_exec(timer_func
, NULL
);
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;
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;
258 case L
'\b': f
= L
"backspace"; break;
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
)
311 wchar_t * wptr
= mpdm_string(str
);
313 #ifndef CONFOPT_ADDWSTR
316 cptr
= mpdm_wcstombs(wptr
, NULL
);
322 #endif /* CONFOPT_ADDWSTR */
328 static void draw_status(void)
329 /* draws the status bar */
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
]);
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
));
350 static void nc_draw(mpdm_t doc
)
351 /* driver drawing function for cursesw */
360 for(n
= 0;n
< mpdm_size(d
);n
++)
362 mpdm_t l
= mpdm_aget(d
, n
);
366 for(m
= 0;m
< mpdm_size(l
);m
++)
371 /* get the attribute and the string */
372 attr
= mpdm_ival(mpdm_aget(l
, m
++));
375 wattrset(cw
, nc_attrs
[attr
]);
386 static void build_colors(void)
387 /* builds the colors */
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
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
);
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");
424 /* store the 'normal' attribute */
425 if(wcscmp(mpdm_string(c
), L
"normal") == 0)
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)
436 init_pair(n
+ 1, c0
- 1, c1
- 1);
437 cp
= COLOR_PAIR(n
+ 1);
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
;
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);
458 timer_msecs
= mpdm_ival(mpdm_aget(a
, 0));
460 r
= mpdm_unref(timer_func
);
461 timer_func
= mpdm_ref(func
);
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
));
483 static mpdm_t
ncursesw_drv_shutdown(mpdm_t a
)
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
)
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));
524 static mpdm_t
tui_refresh(mpdm_t a
)
525 /* TUI: refresh the screen */
532 static mpdm_t
tui_getxy(mpdm_t a
)
533 /* TUI: returns the x and y cursor position */
541 mpdm_aset(v
, MPDM_I(x
), 0);
542 mpdm_aset(v
, MPDM_I(y
), 1);
547 static mpdm_t
tui_openpanel(mpdm_t a
)
548 /* opens a panel (creates new window) */
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)));
565 static mpdm_t
tui_closepanel(mpdm_t a
)
566 /* closes a panel (deletes last window) */
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];
581 static void register_functions(void)
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));
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
)
619 register_functions();
623 keypad(stdscr
, TRUE
);
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
);
642 int ncursesw_drv_detect(int * argc
, char *** argv
)
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
));
653 #endif /* CONFOPT_CURSES */