2 * Copyright Neil Brown ©2015-2021 <neil@brown.name>
3 * May be distributed under terms of GPLv2 - see file:COPYING
5 * ncurses front end for edlib.
7 * There is currently only support for a single terminal window
8 * which provides a single pane.
10 * Rendering operations are:
11 * draw text with attributes at location
12 * erase with attributes in rectangle
20 #define _XOPEN_SOURCE_EXTENDED
21 #ifndef _DEFAULT_SOURCE
22 #define _DEFAULT_SOURCE
33 #include <sys/ioctl.h>
41 typedef char hash_t[MD5_DIGEST_SIZE*2+1];
45 #undef NCURSES_OK_ADDR
46 #define NCURSES_OK_ADDR(p) ((void*)0 != NCURSES_CAST(const void *, (p)))
56 struct col_hash *col_hash;
62 char last_screen[MD5_DIGEST_SIZE*2+1];
63 char next_screen[MD5_DIGEST_SIZE*2+1];
64 /* The next event to generate when idle */
65 enum { DoNil, DoMouse, DoKey, DoCheck, DoClose} next_event;
71 static SCREEN *current_screen;
72 static void ncurses_text(struct pane *p safe, struct pane *display safe,
73 wchar_t ch, int attr, short x, short y, short cursor);
74 static PANEL * safe pane_panel(struct pane *p safe, struct pane *home);
75 DEF_CMD(input_handle);
76 DEF_CMD(handle_winch);
77 static struct map *nc_map;
78 DEF_LOOKUP_CMD(ncurses_handle, nc_map);
80 static void set_screen(struct pane *p)
82 struct display_data *dd;
83 extern void *_nc_globals[100];
85 static int index = -1, offset=0;
88 if (current_screen && index >= 0)
89 _nc_globals[index] = NULL;
90 current_screen = NULL;
96 if (dd->scr == current_screen)
101 for (i=0; i<100; i++)
102 if (_nc_globals[i] < (void*)stdscr &&
103 _nc_globals[i]+4*(sizeof(void*)) >= (void*)stdscr) {
104 /* This is _nc_windowlist */
106 offset = ((void*)stdscr) - _nc_globals[i];
111 current_screen = dd->scr;
113 _nc_globals[index] = ((void*)stdscr) - offset;
119 DEF_CMD(abort_replay);
121 static bool parse_event(struct pane *p safe);
122 static bool prepare_recrep(struct pane *p safe)
124 struct display_data *dd = p->data;
127 name = getenv("EDLIB_RECORD");
129 dd->log = fopen(name, "w");
130 name = getenv("EDLIB_REPLAY");
132 dd->input = fopen(name, "r");
133 if (getenv("EDLIB_PAUSE"))
134 sleep(atoi(getenv("EDLIB_PAUSE")));
142 static void close_recrep(struct pane *p safe)
144 struct display_data *dd = p->data;
147 fprintf(dd->log, "Close\n");
152 static void record_key(struct pane *p safe, char *key safe)
154 struct display_data *dd = p->data;
159 if (!strchr(key, '"'))
161 else if (!strchr(key, '\''))
163 else if (!strchr(key, '/'))
167 fprintf(dd->log, "Key %c%s%c\n", q,key,q);
170 static void record_mouse(struct pane *p safe, char *key safe, int x, int y)
172 struct display_data *dd = p->data;
176 if (!strchr(key, '"'))
178 else if (!strchr(key, '\''))
180 else if (!strchr(key, '/'))
184 fprintf(dd->log, "Mouse %c%s%c %d,%d\n", q,key,q, x, y);
187 static void record_screen(struct pane *p safe)
189 struct display_data *dd = p->data;
190 struct md5_state ctx;
191 uint16_t buf[CCHARW_MAX+5];
192 char out[MD5_DIGEST_SIZE*2+1];
195 if (!dd->log && !(dd->input && dd->next_event == DoCheck))
199 for (r = 0; r < p->h; r++)
200 for (c = 0; c < p->w; c++) {
202 wchar_t wc[CCHARW_MAX+2];
207 mvwin_wch(stdscr, r, c, &cc);
208 getcchar(&cc, wc, &a, &color, NULL);
209 pair_content(color, &fg, &bg);
210 buf[0] = htole16(fg);
211 buf[1] = htole16(bg);
212 for (l = 0; l < CCHARW_MAX && wc[l]; l++)
213 buf[l+3] = htole16(wc[l]);
215 LOG("%d,%d %d:%d:%d:%d %lc", c,r,fg,bg,l,wc[0], wc[0] > ' ' ? wc[0] : '?');
216 md5_update(&ctx, (uint8_t*)buf,
217 (l+3) * sizeof(uint16_t));
219 md5_final_txt(&ctx, out);
221 fprintf(dd->log, "Display %d,%d %s", p->w, p->h, out);
222 strcpy(dd->last_screen, out);
224 fprintf(dd->log, " %d,%d", p->cx, p->cy);
225 fprintf(dd->log, "\n");
227 if (dd->input && dd->next_event == DoCheck) {
228 call_comm("event:free", p, &abort_replay);
229 // if (strcmp(dd->last_screen, dd->next_screen) != 0)
230 // dd->next_event = DoClose;
231 call_comm("editor-on-idle", p, &next_evt);
235 static inline int match(char *line safe, char *w safe)
237 return strncmp(line, w, strlen(w)) == 0;
240 static char *copy_quote(char *line safe, char *buf safe)
246 if (q != '"' && q != '\'' && q != '/')
248 while (*line != q && *line)
256 static char *get_coord(char *line safe, struct xy *co safe)
263 v = strtol(line, &ep, 10);
264 if (!ep || ep == line || *ep != ',')
268 v = strtol(line, &ep, 10);
269 if (!ep || ep == line || (*ep && *ep != ' ' && *ep != '\n'))
275 static char *get_hash(char *line safe, hash_t hash safe)
280 for (i = 0; i < MD5_DIGEST_SIZE*2 && *line; i++)
287 REDEF_CMD(abort_replay)
289 struct display_data *dd = ci->home->data;
291 dd->next_event = DoClose;
292 return next_evt_func(ci);
295 static bool parse_event(struct pane *p safe)
297 struct display_data *dd = p->data;
301 dd->next_event = DoNil;
303 fgets(line, sizeof(line)-1, dd->input) == NULL)
305 else if (match(line, "Key ")) {
306 if (!copy_quote(line+4, dd->event_info))
308 dd->next_event = DoKey;
309 } else if (match(line, "Mouse ")) {
310 char *f = copy_quote(line+6, dd->event_info);
313 f = get_coord(f, &dd->event_pos);
316 dd->next_event = DoMouse;
317 } else if (match(line, "Display ")) {
318 char *f = get_coord(line+8, &dd->event_pos);
321 f = get_hash(f, dd->next_screen);
322 dd->next_event = DoCheck;
323 } else if (match(line, "Close")) {
324 dd->next_event = DoClose;
326 LOG("parse %s", line);
328 if (dd->next_event != DoCheck)
329 call_comm("editor-on-idle", p, &next_evt);
331 call_comm("event:timer", p, &abort_replay, 10*1000);
337 struct pane *p = ci->home;
338 struct display_data *dd = p->data;
339 int button = 0, type = 0;
342 delay = getenv("EDLIB_REPLAY_DELAY");
343 if (delay && dd->next_event != DoCheck)
344 usleep(atoi(delay)*1000);
346 switch(dd->next_event) {
348 record_key(p, dd->event_info);
349 call("Keystroke", p, 0, NULL, dd->event_info);
352 record_mouse(p, dd->event_info, dd->event_pos.x,
354 if (strstr(dd->event_info, ":Press"))
356 else if (strstr(dd->event_info, ":Release"))
358 else if (strstr(dd->event_info, ":Motion"))
360 if (type == 1 || type == 2) {
361 char *e = dd->event_info + strlen(dd->event_info) - 1;
364 call("Mouse-event", p, button, NULL, dd->event_info,
366 dd->event_pos.x, dd->event_pos.y);
369 /* No point checking, just do a diff against new trace log. */
370 /* not; (strcmp(dd->next_screen, dd->last_screen) != 0) */
373 call("event:deactivate", p);
377 call_comm("event:read", p, &input_handle, 0);
378 call_comm("event:signal", p, &handle_winch, SIGWINCH);
385 static inline bool prepare_recrep(struct pane *p safe) {return False;}
386 static inline void record_key(struct pane *p safe, char *key) {}
387 static inline void record_mouse(struct pane *p safe, char *key safe,
389 static inline void record_screen(struct pane *p safe) {}
390 static inline void close_recrep(struct pane *p safe) {}
395 struct call_return *cr = container_of(ci->comm, struct call_return, c);
401 DEF_CMD(nc_close_display)
403 /* If this is only display, then refuse to close this one */
404 struct call_return cr;
405 struct display_data *dd = ci->home->data;
408 call("Message", ci->focus, 0, NULL, dd->noclose);
414 call_comm("editor:notify:all-displays", ci->focus, &cr.c);
416 pane_close(ci->home);
418 call("Message", ci->focus, 0, NULL,
419 "Cannot close only window.");
423 DEF_CMD(nc_set_noclose)
425 struct display_data *dd = ci->home->data;
430 dd->noclose = strdup(ci->str);
434 static void ncurses_end(struct pane *p safe)
443 * hash table for colours and pairs
444 * key is r,g,b (0-1000) in 10bit fields,
445 * or fg,bg in 16 bit fields with bit 31 set
446 * content is colour number of colour pair number.
447 * We never delete entries, unless we delete everything.
454 #define COL_KEY(r,g,b) ((r<<20) | (g<<10) | b)
455 #define PAIR_KEY(fg, bg) ((1<<31) | (fg<<16) | bg)
456 #define hash_key(k) ((((k) * 0x61C88647) >> 20) & 0xff)
459 int next_col, next_pair;
460 struct chash *tbl[256];
463 static struct col_hash *safe hash_init(struct display_data *dd safe)
466 dd->col_hash = malloc(sizeof(*dd->col_hash));
467 memset(dd->col_hash, 0, sizeof(*dd->col_hash));
468 dd->col_hash->next_col = 16;
469 dd->col_hash->next_pair = 1;
474 static void hash_free(struct display_data *dd safe)
483 for (h = 0; h < 255; h++)
484 while ((c = ch->tbl[h]) != NULL) {
485 ch->tbl[h] = c->next;
492 static int find_col(struct display_data *dd safe, int rgb[])
494 if (0 /* dynamic colours */) {
495 struct col_hash *ch = hash_init(dd);
496 int k = COL_KEY(rgb[0], rgb[1], rgb[2]);
500 for (c = ch->tbl[h]; c; c = c->next)
503 c = malloc(sizeof(*c));
505 c->content = ch->next_col++;
506 c->next = ch->tbl[h];
508 init_color(c->content, rgb[0], rgb[1], rgb[2]);
511 /* If colour is grey, map to 1 of 26 for 0, 232 to 255, 15
512 * The 24 grey shades have bit values from 8 to 238, so the
513 * gap to white is a little bigger, but that probably doesn't
515 * Otherwise map to 6x6x6 rgb cube from 16
516 * Actual colours are biased bright, at 0,95,135,175,215,255
517 * with a 95 gap at bottom and 40 elsewhere.
518 * So we divide 5 and 2 half ranges, and merge bottom 2.
523 //printf("want %d,%d,%d\n", rgb[0], rgb[1], rgb[2]);
524 if (abs(rgb[0] - rgb[1]) < 10 &&
525 abs(rgb[1] - rgb[2]) < 10) {
526 /* grey - within 1% */
527 int v = (rgb[0] + rgb[1] + rgb[2]) / 3;
529 /* We divide the space in 24 ranges surrounding
530 * the grey values, and 2 half-ranges near black
531 * and white. So add half a range - 1000/50 -
532 * then divide by 1000/25 to get a number from 0 to 25.
534 v = (v + 1000/50) / (1000/25);
536 return 0; /* black */
538 return 15; /* white */
539 //printf(" grey %d\n", v + 231);
540 /* grey shades are from 232 to 255 inclusive */
543 for (h = 0; h < 3; h++) {
546 v = (v + 1000/12) / (1000/6);
547 /* v is from 0 to 6, we want up to 5
548 * with 0 and 1 merged
555 //printf(" color %d\n", c + 16);
560 static int to_pair(struct display_data *dd safe, int fg, int bg)
562 struct col_hash *ch = hash_init(dd);
563 int k = PAIR_KEY(fg, bg);
567 for (c = ch->tbl[h]; c; c = c->next)
570 c = malloc(sizeof(*c));
572 c->content = ch->next_pair++;
573 c->next = ch->tbl[h];
575 init_pair(c->content, fg, bg);
580 static int cvt_attrs(struct pane *p safe, struct pane *home safe,
583 struct display_data *dd = home->data;
588 int fg = COLOR_BLACK;
589 int bg = COLOR_WHITE+8;
594 } while (p->parent != p &&(pan = pane_panel(p, NULL)) == NULL);
596 /* Get 'default colours for this pane - set at clear */
597 int at = getbkgd(panel_window(pan));
598 int pair = PAIR_NUMBER(at);
600 pair_content(pair, &dfg, &dbg);
616 strncpy(tmp, a, c-a);
618 if (strcmp(tmp, "inverse")==0) attr |= A_STANDOUT;
619 else if (strcmp(tmp, "noinverse")==0) attr &= ~A_STANDOUT;
620 else if (strcmp(tmp, "bold")==0) attr |= A_BOLD;
621 else if (strcmp(tmp, "nobold")==0) attr &= ~A_BOLD;
622 else if (strcmp(tmp, "underline")==0) attr |= A_UNDERLINE;
623 else if (strcmp(tmp, "nounderline")==0) attr &= ~A_UNDERLINE;
624 else if (strncmp(tmp, "fg:", 3) == 0) {
625 struct call_return cr =
626 call_ret(all, "colour:map", home,
628 int rgb[3] = {cr.i, cr.i2, cr.x};
629 fg = find_col(dd, rgb);
630 } else if (strncmp(tmp, "bg:", 3) == 0) {
631 struct call_return cr =
632 call_ret(all, "colour:map", home,
634 int rgb[3] = {cr.i, cr.i2, cr.x};
635 bg = find_col(dd, rgb);
639 if (fg != COLOR_BLACK || bg != COLOR_WHITE+8)
640 attr |= COLOR_PAIR(to_pair(dd, fg, bg));
644 static int make_cursor(int attr)
646 return attr ^ A_UNDERLINE;
649 DEF_CMD(nc_notify_display)
651 struct display_data *dd = ci->home->data;
652 comm_call(ci->comm2, "callback:display", ci->home, dd->last_event);
658 struct pane *p = ci->home;
659 struct display_data *dd = p->data;
662 fclose(dd->scr_file);
666 DEF_CMD(nc_pane_close)
670 set_screen(ci->home);
671 while ((pan = panel_above(pan)) != NULL)
672 if (panel_userptr(pan) == ci->focus)
675 WINDOW *win = panel_window(pan);
678 pane_damaged(ci->home, DAMAGED_POSTORDER);
683 static PANEL * safe pane_panel(struct pane *p safe, struct pane *home)
687 while ((pan = panel_above(pan)) != NULL)
688 if (panel_userptr(pan) == p)
694 pan = new_panel(newwin(p->h, p->w, 0, 0));
695 set_panel_userptr(pan, p);
696 pane_add_notify(home, p, "Notify:Close");
703 struct pane *p = ci->home;
704 int attr = cvt_attrs(ci->focus, p, ci->str2?:ci->str);
710 panel = pane_panel(ci->focus, p);
713 win = panel_window(panel);
715 if (h != ci->focus->h || w != ci->focus->w) {
716 wresize(win, ci->focus->h, ci->focus->w);
717 replace_panel(panel, win);
722 pane_damaged(p, DAMAGED_POSTORDER);
726 DEF_CMD(nc_text_size)
728 int max_space = ci->num;
731 const char *str = ci->str;
735 while (str[0] != 0) {
736 wint_t wc = get_utf8(&str, NULL);
738 if (wc == WEOF || wc == WERR)
744 if (size <= max_space)
745 max_bytes = str - ci->str;
747 return comm_call(ci->comm2, "callback:size", ci->focus,
748 max_bytes, NULL, NULL,
749 0, NULL, NULL, size, 1);
752 DEF_CMD(nc_draw_text)
754 struct pane *p = ci->home;
755 int attr = cvt_attrs(ci->focus, p, ci->str2);
756 int cursor_offset = ci->num;
757 short x = ci->x, y = ci->y;
758 const char *str = ci->str;
763 while (str[0] != 0) {
764 int precurs = str <= ci->str + cursor_offset;
765 wint_t wc = get_utf8(&str, NULL);
767 if (wc == WEOF || wc == WERR)
772 if (precurs && str > ci->str + cursor_offset)
773 ncurses_text(ci->focus, p, wc, attr, x, y, 1);
775 ncurses_text(ci->focus, p, wc, attr, x, y, 0);
778 if (str == ci->str + cursor_offset)
779 ncurses_text(ci->focus, p, ' ', 0, x, y, 1);
780 pane_damaged(p, DAMAGED_POSTORDER);
784 DEF_CMD(nc_refresh_size)
786 struct pane *p = ci->home;
789 getmaxyx(stdscr, p->h, p->w);
794 DEF_CMD(nc_refresh_post)
796 struct pane *p = ci->home;
802 /* Need to ensure stacking order and panel y,x position
803 * is correct. FIXME it would be good if we could skip this
806 pan = panel_above(NULL);
809 p1 = (struct pane*) panel_userptr(pan);
810 for (;(pan2 = panel_above(pan)) != NULL; pan = pan2) {
811 struct pane *p2 = (struct pane*)panel_userptr(pan2);
812 p1 = (struct pane*)panel_userptr(pan);
816 if (p1->abs_z < p2->abs_z)
818 if (p1->abs_z == p2->abs_z &&
821 /* pan needs to be above pan2. All we can do is move it to
822 * the top. Anything that needs to be above it will eventually
826 /* Now the panel below pan might need to be over pan2 too... */
827 pan = panel_below(pan2);
832 /* As we need to crop pane against their parents, we cannot simply
833 * use update_panels(). Instead we copy each to stdscr and refresh
836 for (pan = NULL; (pan = panel_above(pan)) != NULL; ) {
838 struct xy src, dest, destend;
841 p1 = (void*)panel_userptr(pan);
844 dest = pane_mapxy(p1, p, 0, 0, True);
845 destend = pane_mapxy(p1, p, p1->w, p1->h, True);
846 src = pane_mapxy(p1, p, 0, 0, False);
847 src.x = dest.x - src.x;
848 src.y = dest.y - src.y;
849 win = panel_window(pan);
851 /* guard again accessing beyond boundary of win */
852 if (destend.x > dest.x + (w - src.x))
853 destend.x = dest.x + (w - src.x);
854 if (destend.y > dest.y + (h - src.y))
855 destend.y = dest.y - (h - src.y);
856 copywin(win, stdscr, src.y, src.x,
857 dest.y, dest.x, destend.y-1, destend.x-1, 0);
859 /* place the cursor */
862 while (p1 != p && (pan = pane_panel(p1, NULL)) == NULL)
864 if (pan && p1->cx >= 0) {
865 struct xy curs = pane_mapxy(p1, p, p1->cx, p1->cy, False);
866 wmove(stdscr, curs.y, curs.x);
867 } else if (p->cx >= 0)
868 wmove(stdscr, p->cy, p->cx);
870 record_screen(ci->home);
874 static struct pane *ncurses_init(struct pane *ed,
875 const char *tty, const char *term)
879 struct display_data *dd;
885 f = fopen(tty, "r+");
890 scr = newterm(term, f, f);
897 dd->is_xterm = (term && strncmp(term, "xterm", 5) == 0);
899 p = pane_register(ed, 1, &ncurses_handle.c, dd);
907 use_default_colors();
913 intrflush(stdscr, FALSE);
914 keypad(stdscr, TRUE);
915 mousemask(BUTTON1_PRESSED | BUTTON1_RELEASED |
916 BUTTON2_PRESSED | BUTTON2_RELEASED |
917 BUTTON3_PRESSED | BUTTON3_RELEASED |
918 BUTTON4_PRESSED | BUTTON4_RELEASED |
919 BUTTON5_PRESSED | BUTTON5_RELEASED |
920 BUTTON_CTRL | BUTTON_SHIFT | BUTTON_ALT |
921 REPORT_MOUSE_POSITION, NULL);
924 getmaxyx(stdscr, rows, cols);
925 pane_resize(p, 0, 0, cols, rows);
927 call("editor:request:all-displays", p);
928 if (!prepare_recrep(p)) {
929 call_comm("event:read", p, &input_handle, fileno(f));
931 call_comm("event:signal", p, &handle_winch, SIGWINCH);
936 REDEF_CMD(handle_winch)
938 struct pane *p = ci->home;
939 struct display_data *dd = p->data;
941 ioctl(fileno(dd->scr_file), TIOCGWINSZ, &size);
943 resize_term(size.ws_row, size.ws_col);
945 pane_resize(p, 0, 0, size.ws_row, size.ws_col);
949 DEF_CMD(force_redraw)
951 struct pane *p = ci->home;
958 static void ncurses_text(struct pane *p safe, struct pane *display safe,
959 wchar_t ch, int attr, short x, short y, short cursor)
970 if (pane_has_focus(p->z < 0 ? p->parent : p)) {
971 /* Cursor is in-focus */
972 struct xy curs = pane_mapxy(p, display, x, y, False);
973 display->cx = curs.x;
974 display->cy = curs.y;
976 /* Cursor here, but not focus */
977 attr = make_cursor(attr);
983 pan = pane_panel(p2, NULL);
984 while (!pan && p2->parent != p2) {
986 pan = pane_panel(p2, NULL);
989 struct xy xy = pane_mapxy(p, p2, x, y, False);
990 mvwadd_wch(panel_window(pan), xy.y, xy.x, &cc);
994 static struct namelist {
1000 {KEY_LEFT, ":Left"},
1001 {KEY_RIGHT, ":Right"},
1002 {KEY_HOME, ":Home"},
1003 {KEY_BACKSPACE, ":Backspace"},
1004 {KEY_DL, ":DelLine"},
1005 {KEY_IL, ":InsLine"},
1008 {KEY_ENTER, ":Enter"},
1011 {KEY_NPAGE, ":Next"},
1012 {KEY_PPAGE, ":Prior"},
1014 {KEY_SDC, ":S:Del"},
1015 {KEY_SDL, ":S:DelLine"},
1016 {KEY_SEND, ":S:End"},
1017 {KEY_SHOME, ":S:Home"},
1018 {KEY_SLEFT, ":S:Left"},
1019 {KEY_SRIGHT, ":S:Right"},
1020 {KEY_BTAB, ":S:Tab"},
1024 { 0616, ":S:Prior"},
1026 { 01041, ":S:Home"},
1028 { 01066, ":S:Prior"},
1029 { 01015, ":S:Next"},
1031 { 01027, ":A:S:Home"},
1032 { 01022, ":A:S:End"},
1033 { 01046, ":A:S:Prior"},
1034 { 01047, ":A:S:Next"}, // ??
1036 { 01052, ":A:Prior"},
1037 { 01045, ":A:Next"},
1038 { 01026, ":A:Home"},
1041 { 01014, ":A:Down"},
1042 { 01040, ":A:Left"},
1043 { 01057, ":A:Right"},
1074 {'\177', ":Delete"},
1079 static char *find_name (struct namelist *l safe, wint_t c)
1082 for (i = 0; l[i].name; i++)
1088 static void send_key(int keytype, wint_t c, int alt, struct pane *p safe)
1090 struct display_data *dd = p->data;
1092 char buf[100];/* FIXME */
1094 char *a = alt ? ":A" : "";
1096 if (keytype == KEY_CODE_YES) {
1097 n = find_name(key_names, c);
1099 sprintf(buf, "%sNcurs-%o", a, c);
1101 strcat(strcpy(buf, a), n);
1103 n = find_name(char_names, c);
1105 sprintf(buf, "%s%s", a, n);
1106 else if (c < ' ' || c == 0x7f)
1107 sprintf(buf, "%s:C-%c",
1110 sprintf(buf, "%s-%s", a, put_utf8(t, c));
1113 dd->last_event = time(NULL);
1115 call("Keystroke", p, 0, NULL, buf);
1118 static void do_send_mouse(struct pane *p safe, int x, int y, char *cmd safe,
1119 int button, char *mod, int type)
1122 struct display_data *dd = p->data;
1124 record_mouse(p, cmd, x, y);
1125 ret = call("Mouse-event", p, button, NULL, cmd, type, NULL, mod, x, y);
1126 if (type == 1 && !dd->report_position) {
1128 fprintf(dd->scr_file, "\033[?1002h");
1129 fflush(dd->scr_file);
1131 dd->report_position = 1;
1132 } else if (type == 3 && !ret) {
1134 fprintf(dd->scr_file, "\033[?1002l");
1135 fflush(dd->scr_file);
1137 dd->report_position = 0;
1141 static void send_mouse(MEVENT *mev safe, struct pane *p safe)
1143 struct display_data *dd = p->data;
1149 /* MEVENT has lots of bits. We want a few numbers */
1150 for (b = 1 ; b <= (NCURSES_MOUSE_VERSION <= 1 ? 3 : 5); b++) {
1151 mmask_t s = mev->bstate;
1156 if (s & BUTTON_SHIFT) modf |= 1;
1157 if (s & BUTTON_CTRL) modf |= 2;
1158 if (s & BUTTON_ALT) modf |= 4;
1160 case 0: mod = ""; break;
1161 case 1: mod = ":S"; break;
1162 case 2: mod = ":C"; break;
1163 case 3: mod = ":C:S"; break;
1164 case 4: mod = ":A"; break;
1165 case 5: mod = ":A:S"; break;
1166 case 6: mod = ":A:C"; break;
1167 case 7: mod = ":A:C:S"; break;
1169 if (BUTTON_PRESS(s, b))
1170 action = "%s:Press-%d";
1171 else if (BUTTON_RELEASE(s, b)) {
1172 action = "%s:Release-%d";
1173 /* Modifiers only reported on button Press */
1177 snprintf(buf, sizeof(buf), action, mod, b);
1178 dd->last_event = time(NULL);
1179 do_send_mouse(p, x, y, buf, b, mod, BUTTON_PRESS(s,b) ? 1 : 2);
1181 if ((mev->bstate & REPORT_MOUSE_POSITION) &&
1182 dd->report_position)
1183 /* Motion doesn't update last_event */
1184 do_send_mouse(p, x, y, ":Motion", 0, "", 3);
1187 REDEF_CMD(input_handle)
1189 struct pane *p = ci->home;
1193 int have_escape = 0;
1195 if (!(void*)p->data)
1196 /* already closed */
1199 while ((is_keycode = get_wch(&c)) != ERR) {
1200 if (c == KEY_MOUSE) {
1202 while (getmouse(&mev) != ERR)
1203 send_mouse(&mev, p);
1204 } else if (have_escape) {
1205 send_key(is_keycode, c, 1, p);
1207 } else if (c == '\e')
1210 send_key(is_keycode, c, 0, p);
1211 /* Don't know what other code might have done,
1212 * so re-set the screen
1217 send_key(is_keycode, '\e', 0, p);
1221 DEF_CMD(display_ncurses)
1226 term = pane_attr_get(ci->focus, "TERM");
1228 term = getenv("TERM");
1230 term = "xterm-256color";
1232 p = ncurses_init(ci->focus, ci->str, term);
1234 struct pane *p2 = call_ret(pane, "attach-x11selection", p);
1237 return comm_call(ci->comm2, "callback:display", p);
1242 void edlib_init(struct pane *ed safe)
1244 call_comm("global-set-command", ed, &display_ncurses, 0, NULL,
1245 "attach-display-ncurses");
1247 nc_map = key_alloc();
1248 key_add(nc_map, "Display:refresh", &force_redraw);
1249 key_add(nc_map, "Display:close", &nc_close_display);
1250 key_add(nc_map, "Display:set-noclose", &nc_set_noclose);
1251 key_add(nc_map, "Close", &nc_close);
1252 key_add(nc_map, "Free", &edlib_do_free);
1253 key_add(nc_map, "pane-clear", &nc_clear);
1254 key_add(nc_map, "text-size", &nc_text_size);
1255 key_add(nc_map, "Draw:text", &nc_draw_text);
1256 key_add(nc_map, "Refresh:size", &nc_refresh_size);
1257 key_add(nc_map, "Refresh:postorder", &nc_refresh_post);
1258 key_add(nc_map, "all-displays", &nc_notify_display);
1259 key_add(nc_map, "Sig:Winch", &handle_winch);
1260 key_add(nc_map, "Notify:Close", &nc_pane_close);