-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay-ncurses.c
1840 lines (1662 loc) · 41.1 KB
/
display-ncurses.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright Neil Brown ©2015-2023 <[email protected]>
* May be distributed under terms of GPLv2 - see file:COPYING
*
* ncurses front end for edlib.
*
* There is currently only support for a single terminal window
* which provides a single pane.
*
* Rendering operations are:
* draw text with attributes at location
* erase with attributes in rectangle
*/
#define RECORD_REPLAY
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE
#endif
#define _XOPEN_SOURCE_EXTENDED
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE
#endif
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <curses.h>
#include <panel.h>
#include <string.h>
#include <locale.h>
#include <ctype.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <netdb.h>
#include <wand/MagickWand.h>
#ifdef __CHECKER__
// enums confuse sparse...
#define MagickBooleanType int
#endif
#include <term.h>
#define PANE_DATA_TYPE struct display_data
#include "core.h"
#ifdef RECORD_REPLAY
#include <unistd.h>
#include <stdio.h>
#include "md5.h"
typedef char hash_t[MD5_DIGEST_SIZE*2+1];
#endif
#ifdef __CHECKER__
#undef NCURSES_OK_ADDR
#define NCURSES_OK_ADDR(p) ((void*)0 != NCURSES_CAST(const void *, (p)))
#endif
struct col_hash;
struct display_data {
SCREEN *scr;
FILE *scr_file;
int is_xterm;
struct col_hash *col_hash;
int report_position;
long last_event;
bool did_close;
bool suspended;
struct buf paste_buf;
time_t paste_start;
char *paste_latest;
int paste_pending;
struct pids {
pid_t pid;
struct pids *next;
} *pids;
char *rs1, *rs2, *rs3, *clear;
char attr_buf[1024];
#ifdef RECORD_REPLAY
FILE *log;
FILE *input;
int input_sleeping;
/* Sometimes I get duplicate Display lines, but not consistently.
* To avoid these, record last, filter repeats.
*/
int last_cx, last_cy;
char last_screen[MD5_DIGEST_SIZE*2+1];
char next_screen[MD5_DIGEST_SIZE*2+1];
/* The next event to generate when idle */
enum { DoNil, DoMouse, DoKey, DoCheck, DoClose} next_event;
char event_info[30];
struct xy event_pos;
int clears; /* counts of Draw:clear events */
#endif
};
#include "core-pane.h"
static SCREEN *current_screen;
static void ncurses_text(struct pane *p safe, struct pane *display safe,
wchar_t ch, int attr, int pair,
short x, short y, short cursor);
static PANEL * safe pane_panel(struct pane *p safe, struct pane *home);
DEF_CMD(input_handle);
DEF_CMD(handle_winch);
static struct map *nc_map;
DEF_LOOKUP_CMD(ncurses_handle, nc_map);
static struct display_data *current_dd;
static void set_screen(struct pane *p)
{
struct display_data *dd;
extern void *_nc_globals[100];
int i;
static int index = -1, offset=0;
if (!p) {
if (current_screen && index >= 0)
_nc_globals[index] = NULL;
current_screen = NULL;
return;
}
dd = p->data;
current_dd = dd;
if (!dd)
return;
if (dd->scr == current_screen)
return;
if (index == -1) {
index = -2;
for (i=0; i<100; i++)
if (_nc_globals[i] < (void*)stdscr &&
_nc_globals[i]+4*(sizeof(void*)) >= (void*)stdscr) {
/* This is _nc_windowlist */
index = i;
offset = ((void*)stdscr) - _nc_globals[i];
}
}
set_term(dd->scr);
current_screen = dd->scr;
if (index >= 0) {
_nc_globals[index] = ((void*)stdscr) - offset;
}
}
#ifdef RECORD_REPLAY
DEF_CMD(next_evt);
static bool parse_event(struct pane *p safe);
static bool prepare_recrep(struct pane *p safe)
{
struct display_data *dd = p->data;
char *name;
name = getenv("EDLIB_RECORD");
if (name)
dd->log = fopen(name, "w");
name = getenv("EDLIB_REPLAY");
if (name)
dd->input = fopen(name, "r");
if (getenv("EDLIB_PAUSE"))
sleep(atoi(getenv("EDLIB_PAUSE")));
if (dd->input) {
parse_event(p);
return True;
}
return False;
}
static void close_recrep(struct pane *p safe)
{
struct display_data *dd = p->data;
if (dd->log) {
fprintf(dd->log, "Close %d\n", dd->clears);
fclose(dd->log);
}
}
static void record_key(struct pane *p safe, char *key safe)
{
struct display_data *dd = p->data;
char q;
if (!dd->log)
return;
if (!strchr(key, '"'))
q = '"';
else if (!strchr(key, '\''))
q = '\'';
else if (!strchr(key, '/'))
q = '/';
else
return;
fprintf(dd->log, "Key %c%s%c\n", q,key,q);
dd->last_cx = -2; /* Force next Display to be shown */
fflush(dd->log);
}
static void record_mouse(struct pane *p safe, char *key safe, int x, int y)
{
struct display_data *dd = p->data;
char q;
if (!dd->log)
return;
if (!strchr(key, '"'))
q = '"';
else if (!strchr(key, '\''))
q = '\'';
else if (!strchr(key, '/'))
q = '/';
else
return;
fprintf(dd->log, "Mouse %c%s%c %d,%d\n", q,key,q, x, y);
dd->last_cx = -2; /* Force next Display to be shown */
fflush(dd->log);
}
static void record_screen(struct pane *p safe)
{
struct display_data *dd = p->data;
struct md5_state ctx;
uint16_t buf[CCHARW_MAX+5];
char out[MD5_DIGEST_SIZE*2+1];
int r,c;
if (!dd->log && !(dd->input && dd->next_event == DoCheck))
return;
set_screen(p);
md5_init(&ctx);
for (r = 0; r < p->h; r++)
for (c = 0; c < p->w; c++) {
cchar_t cc;
wchar_t wc[CCHARW_MAX+2];
attr_t a;
short color, fg, bg;
int l;
mvwin_wch(stdscr, r, c, &cc);
getcchar(&cc, wc, &a, &color, NULL);
pair_content(color, &fg, &bg);
buf[0] = htole16(fg);
buf[1] = htole16(bg);
for (l = 0; l < CCHARW_MAX && wc[l]; l++)
buf[l+3] = htole16(wc[l]);
buf[2] = htole16(l);
LOG("%d,%d %d:%d:%d:%d %lc", c,r,fg,bg,l,wc[0], wc[0] > ' ' ? wc[0] : '?');
md5_update(&ctx, (uint8_t*)buf,
(l+3) * sizeof(uint16_t));
}
md5_final_txt(&ctx, out);
if (strcmp(out, dd->last_screen) == 0 &&
p->cx == dd->last_cx && p->cy == dd->last_cy) {
/* No change - filter it */
dd->clears -= 1;
} else if (dd->log) {
fprintf(dd->log, "Display %d,%d %s", p->w, p->h, out);
if (p->cx >= 0)
fprintf(dd->log, " %d,%d", p->cx, p->cy);
fprintf(dd->log, "\n");
fflush(dd->log);
strcpy(dd->last_screen, out);
dd->last_cx = p->cx; dd->last_cy = p->cy;
}
if (dd->input && dd->input_sleeping) {
char *delay = getenv("EDLIB_REPLAY_DELAY");
call_comm("event:free", p, &next_evt);
if (delay)
call_comm("event:timer", p, &next_evt, atoi(delay));
else
call_comm("event:on-idle", p, &next_evt);
}
}
static char *copy_quote(char *line safe, char *buf safe)
{
char q;
while (*line == ' ')
line++;
q = *line++;
if (q != '"' && q != '\'' && q != '/')
return NULL;
while (*line != q && *line)
*buf++ = *line++;
if (!*line)
return NULL;
*buf = '\0';
return line+1;
}
static char *get_coord(char *line safe, struct xy *co safe)
{
long v;
char *ep;
while (*line == ' ')
line ++;
v = strtol(line, &ep, 10);
if (!ep || ep == line || *ep != ',')
return NULL;
co->x = v;
line = ep+1;
v = strtol(line, &ep, 10);
if (!ep || ep == line || (*ep && *ep != ' ' && *ep != '\n'))
return NULL;
co->y = v;
return ep;
}
static char *get_hash(char *line safe, hash_t hash safe)
{
int i;
while (*line == ' ')
line++;
for (i = 0; i < MD5_DIGEST_SIZE*2 && *line; i++)
hash[i] = *line++;
if (!*line)
return NULL;
return line;
}
static bool parse_event(struct pane *p safe)
{
struct display_data *dd = p->data;
char line[80];
line[79] = 0;
dd->next_event = DoNil;
if (!dd->input ||
fgets(line, sizeof(line)-1, dd->input) == NULL)
line[0]=0;
else if (strstarts(line, "Key ")) {
if (!copy_quote(line+4, dd->event_info))
return False;
dd->next_event = DoKey;
} else if (strstarts(line, "Mouse ")) {
char *f = copy_quote(line+6, dd->event_info);
if (!f)
return False;
f = get_coord(f, &dd->event_pos);
if (!f)
return False;
dd->next_event = DoMouse;
} else if (strstarts(line, "Display ")) {
char *f = get_coord(line+8, &dd->event_pos);
if (!f)
return False;
f = get_hash(f, dd->next_screen);
dd->next_event = DoCheck;
} else if (strstarts(line, "Close")) {
dd->next_event = DoClose;
}
LOG("parse %s", line);
dd->input_sleeping = 1;
if (dd->next_event != DoCheck) {
char *delay = getenv("EDLIB_REPLAY_DELAY");
if (delay)
call_comm("event:timer", p, &next_evt, atoi(delay));
else
call_comm("event:on-idle", p, &next_evt);
} else
call_comm("event:timer", p, &next_evt, 10*1000);
return True;
}
REDEF_CMD(next_evt)
{
struct pane *p = ci->home;
struct display_data *dd = p->data;
int button = 0, type = 0;
dd->input_sleeping = 0;
switch(dd->next_event) {
case DoKey:
record_key(p, dd->event_info);
call("Keystroke", p, 0, NULL, dd->event_info);
break;
case DoMouse:
record_mouse(p, dd->event_info, dd->event_pos.x,
dd->event_pos.y);
if (strstr(dd->event_info, ":Press"))
type = 1;
else if (strstr(dd->event_info, ":Release"))
type = 2;
else if (strstr(dd->event_info, ":Motion"))
type = 3;
if (type == 1 || type == 2) {
char *e = dd->event_info + strlen(dd->event_info) - 1;
button = atoi(e);
}
call("Mouse-event", p, button, NULL, dd->event_info,
type, NULL, NULL,
dd->event_pos.x, dd->event_pos.y);
break;
case DoCheck:
/* No point checking, just do a diff against new trace log. */
/* not; (strcmp(dd->next_screen, dd->last_screen) != 0) */
break;
case DoClose:
call("event:deactivate", p);
pane_close(p);
return 1;
case DoNil:
call_comm("event:read", p, &input_handle, 0);
call_comm("event:signal", p, &handle_winch, SIGWINCH);
return 1;
}
parse_event(p);
return 1;
}
#else
static inline bool prepare_recrep(struct pane *p safe) {return False;}
static inline void record_key(struct pane *p safe, char *key) {}
static inline void record_mouse(struct pane *p safe, char *key safe,
int x, int y) {}
static inline void record_screen(struct pane *p safe) {}
static inline void close_recrep(struct pane *p safe) {}
#endif
DEF_CB(cnt_disp)
{
struct call_return *cr = container_of(ci->comm, struct call_return, c);
cr->i += 1;
return 1;
}
static void ncurses_end(struct pane *p safe);
DEF_CMD(nc_close_display)
{
/* If this is only display, then refuse to close this one */
struct call_return cr;
char *nc = pane_attr_get(ci->home, "no-close");
if (nc) {
call("Message", ci->focus, 0, NULL, nc);
return 1;
}
cr.c = cnt_disp;
cr.i = 0;
call_comm("editor:notify:all-displays", ci->focus, &cr.c);
if (cr.i > 1) {
/* Need to call ncurses_end() before we send a Notify:Close
* notification, else server exits too early
*/
ncurses_end(ci->home);
return Efallthrough;
} else
call("Message", ci->focus, 0, NULL,
"Cannot close only window.");
return 1;
}
static int nc_putc(int ch)
{
if (current_dd)
fputc(ch, current_dd->scr_file);
return 1;
}
static char *fnormalize(struct pane *p safe, const char *str) safe
{
char *ret = strsave(p, str);
char *cp;
for (cp = ret ; cp && *cp ; cp++)
if (!isalnum(*cp) &&
!strchr("/_-+=.,@#", *cp))
/* Don't like this char */
*cp = '_';
return ret ?: "_";
}
static void wait_for(struct display_data *dd safe)
{
struct pids **pp = &dd->pids;
while (*pp) {
struct pids *p = *pp;
if (waitpid(p->pid, NULL, WNOHANG) > 0) {
*pp = p->next;
free(p);
} else
pp = &p->next;
}
}
DEF_CB(ns_resume)
{
struct display_data *dd = ci->home->data;
if (dd->suspended) {
dd->suspended = False;
set_screen(ci->home);
doupdate();
}
return 1;
}
DEF_CMD(nc_external_viewer)
{
struct pane *p = ci->home;
struct display_data *dd = p->data;
char *disp = pane_attr_get(p, "DISPLAY");
char *disp_auth = pane_attr_get(p, "XAUTHORITY");
char *remote = pane_attr_get(p, "REMOTE_SESSION");
char *fqdn = NULL;
const char *path = ci->str;
int pid;
char buf[100];
int n;
int fd;
if (!path)
return Enoarg;
if (disp && *disp) {
struct pids *pds;
switch (pid = fork()) {
case -1:
return Efail;
case 0: /* Child */
setenv("DISPLAY", disp, 1);
if (disp_auth)
setenv("XAUTHORITY", disp_auth, 1);
fd = open("/dev/null", O_RDWR);
if (fd) {
dup2(fd, 0);
dup2(fd, 1);
dup2(fd, 2);
if (fd > 2)
close(fd);
}
execlp("xdg-open", "xdg-open", path, NULL);
exit(1);
default: /* parent */
pds = malloc(sizeof(*pds));
pds->pid = pid;
pds->next = dd->pids;
dd->pids = pds;
break;
}
wait_for(dd);
return 1;
}
/* handle no-display case */
if (remote && strcmp(remote, "yes") == 0 &&
path[0] == '/' &&
gethostname(buf, sizeof(buf)) == 0) {
struct addrinfo *res;
const struct addrinfo hints = {
.ai_flags = AI_CANONNAME,
};
if (getaddrinfo(buf, NULL, &hints, &res) == 0 &&
res && res->ai_canonname)
fqdn = strdup(res->ai_canonname);
freeaddrinfo(res);
}
set_screen(p);
n = 0;
ioctl(fileno(dd->scr_file), FIONREAD, &n);
if (n)
n -= read(fileno(dd->scr_file), buf,
n <= (int)sizeof(buf) ? n : (int)sizeof(buf));
endwin();
/* stay in raw mode */
raw();
noecho();
/* Endwin doesn't seem to reset properly, at least on xfce-terminal.
* So do it manually
*/
if (dd->rs1)
tputs(dd->rs1, 1, nc_putc);
if (dd->rs2)
tputs(dd->rs2, 1, nc_putc);
if (dd->rs3)
tputs(dd->rs3, 1, nc_putc);
if (dd->clear)
tputs(dd->clear, 1, nc_putc);
fflush(dd->scr_file);
fprintf(dd->scr_file, "# Consider copy-pasting following\r\n");
if (fqdn && path[0] == '/') {
/* File will not be local for the user, so help them copy it. */
const char *tmp = fnormalize(p, ci->str2 ?: "XXXXXX");
const char *fname = fnormalize(p, ci->str);
if (strcmp(fname, ci->str) != 0)
/* file name had unusuable chars, need to create safe name */
link(ci->str, fname);
fprintf(dd->scr_file, "f=`mktemp --tmpdir %s`; scp %s:%s $f ; ",
tmp, fqdn, fname);
path = "$f";
}
free(fqdn);
fprintf(dd->scr_file, "xdg-open %s\r\n", path);
fprintf(dd->scr_file, "# Press Enter to continue\r\n");
dd->suspended = True;
call_comm("event:timer", p, &ns_resume, 30*1000);
return 1;
}
static void ncurses_stop(struct pane *p safe)
{
struct display_data *dd = p->data;
if (dd->is_xterm) {
/* disable bracketed-paste */
fprintf(dd->scr_file, "\033[?2004l");
fflush(dd->scr_file);
}
if (dd->paste_start)
free(buf_final(&dd->paste_buf));
dd->paste_start = 0;
free(dd->paste_latest);
dd->paste_latest = NULL;
nl();
endwin();
if (dd->rs1)
tputs(dd->rs1, 1, nc_putc);
if (dd->rs2)
tputs(dd->rs2, 1, nc_putc);
if (dd->rs3)
tputs(dd->rs3, 1, nc_putc);
fflush(dd->scr_file);
}
static void ncurses_end(struct pane *p safe)
{
struct display_data *dd = p->data;
if (dd->did_close)
return;
dd->did_close = True;
set_screen(p);
close_recrep(p);
ncurses_stop(p);
}
/*
* hash table for colours and pairs
* key is r,g,b (0-1000) in 10bit fields,
* or fg,bg in 16 bit fields with bit 31 set
* content is colour number of colour pair number.
* We never delete entries, unless we delete everything.
*/
struct chash {
struct chash *next;
int key, content;
};
#define COL_KEY(r,g,b) ((r<<20) | (g<<10) | b)
#define PAIR_KEY(fg, bg) ((1<<31) | (fg<<16) | bg)
#define hash_key(k) ((((k) * 0x61C88647) >> 20) & 0xff)
struct col_hash {
int next_col, next_pair;
struct chash *tbl[256];
};
static struct col_hash *safe hash_init(struct display_data *dd safe)
{
if (!dd->col_hash) {
dd->col_hash = malloc(sizeof(*dd->col_hash));
memset(dd->col_hash, 0, sizeof(*dd->col_hash));
dd->col_hash->next_col = 16;
dd->col_hash->next_pair = 1;
}
return dd->col_hash;
}
static void hash_free(struct display_data *dd safe)
{
int h;
struct chash *c;
struct col_hash *ch;
ch = dd->col_hash;
if (!ch)
return;
for (h = 0; h < 255; h++)
while ((c = ch->tbl[h]) != NULL) {
ch->tbl[h] = c->next;
free(c);
}
free(ch);
dd->col_hash = NULL;
}
static int find_col(struct display_data *dd safe, int rgb[])
{
if (0 /* dynamic colours */) {
struct col_hash *ch = hash_init(dd);
int k = COL_KEY(rgb[0], rgb[1], rgb[2]);
int h = hash_key(k);
struct chash *c;
for (c = ch->tbl[h]; c; c = c->next)
if (c->key == k)
return c->content;
c = malloc(sizeof(*c));
c->key = k;
c->content = ch->next_col++;
c->next = ch->tbl[h];
ch->tbl[h] = c;
init_color(c->content, rgb[0], rgb[1], rgb[2]);
return c->content;
} else {
/* If colour is grey, map to 1 of 26 for 0, 232 to 255, 15
* The 24 grey shades have bit values from 8 to 238, so the
* gap to white is a little bigger, but that probably doesn't
* matter.
* Otherwise map to 6x6x6 rgb cube from 16
* Actual colours are biased bright, at 0,95,135,175,215,255
* with a 95 gap at bottom and 40 elsewhere.
* So we divide 5 and 2 half ranges, and merge bottom 2.
*/
int c = 0;
int h;
//printf("want %d,%d,%d\n", rgb[0], rgb[1], rgb[2]);
if (abs(rgb[0] - rgb[1]) < 10 &&
abs(rgb[1] - rgb[2]) < 10) {
/* grey - within 1% */
int v = (rgb[0] + rgb[1] + rgb[2]) / 3;
/* We divide the space in 24 ranges surrounding
* the grey values, and 2 half-ranges near black
* and white. So add half a range - 1000/50 -
* then divide by 1000/25 to get a number from 0 to 25.
*/
v = (v + 1000/50) / (1000/25);
if (v == 0)
return 0; /* black */
if (v >= 25)
return 15; /* white */
//printf(" grey %d\n", v + 231);
/* grey shades are from 232 to 255 inclusive */
return v + 231;
}
for (h = 0; h < 3; h++) {
int v = rgb[h];
v = (v + 1000/12) / (1000/6);
/* v is from 0 to 6, we want up to 5
* with 0 and 1 merged
*/
if (v)
v -= 1;
c = c * 6 + v;
}
//printf(" color %d\n", c + 16);
return c + 16;
}
}
static int to_pair(struct display_data *dd safe, int fg, int bg)
{
struct col_hash *ch = hash_init(dd);
int k = PAIR_KEY(fg, bg);
int h = hash_key(k);
struct chash *c;
for (c = ch->tbl[h]; c; c = c->next)
if (c->key == k)
return c->content;
c = malloc(sizeof(*c));
c->key = k;
c->content = ch->next_pair++;
c->next = ch->tbl[h];
ch->tbl[h] = c;
init_pair(c->content, fg, bg);
return c->content;
}
static int cvt_attrs(struct pane *p safe, struct pane *home safe,
const char *attrs, int *pairp safe)
{
struct display_data *dd = home->data;
int attr = 0;
const char *a, *v;
char *col = NULL;
PANEL *pan = NULL;
int fg = COLOR_BLACK;
int bg = COLOR_WHITE+8;
set_screen(home);
while (p->parent != p &&(pan = pane_panel(p, NULL)) == NULL)
p = p->parent;
if (pan) {
/* Get 'default colours for this pane - set at clear */
int at = getbkgd(panel_window(pan));
int pair = PAIR_NUMBER(at);
short dfg, dbg;
pair_content(pair, &dfg, &dbg);
if (dfg >= 0)
fg = dfg;
if (dbg >= 0)
bg = dbg;
}
foreach_attr(a, v, attrs, NULL) {
if (amatch(a, "inverse"))
attr |= A_STANDOUT;
else if (amatch(a, "noinverse"))
attr &= ~A_STANDOUT;
else if (amatch(a, "bold"))
attr |= A_BOLD;
else if (amatch(a, "nobold"))
attr &= ~A_BOLD;
else if (amatch(a, "underline"))
attr |= A_UNDERLINE;
else if (amatch(a, "nounderline"))
attr &= ~A_UNDERLINE;
else if (amatch(a, "fg") && v) {
struct call_return cr =
call_ret(all, "colour:map", home,
0, NULL, aupdate(&col, v));
int rgb[3] = {cr.i, cr.i2, cr.x};
fg = find_col(dd, rgb);
} else if (amatch(a, "bg") && v) {
struct call_return cr =
call_ret(all, "colour:map", home,
0, NULL, aupdate(&col, v));
int rgb[3] = {cr.i, cr.i2, cr.x};
bg = find_col(dd, rgb);
}
}
free(col);
if (fg != COLOR_BLACK || bg != COLOR_WHITE+8)
*pairp = to_pair(dd, fg, bg);
return attr;
}
static int make_cursor(int attr)
{
return attr ^ A_UNDERLINE;
}
DEF_CMD(nc_notify_display)
{
struct display_data *dd = ci->home->data;
comm_call(ci->comm2, "callback:display", ci->home, dd->last_event);
return 1;
}
DEF_CMD_CLOSED(nc_close)
{
struct pane *p = ci->home;
struct display_data *dd = p->data;
ncurses_end(p);
hash_free(dd);
fclose(dd->scr_file);
return 1;
}
DEF_CMD(nc_pane_close)
{
PANEL *pan = NULL;
set_screen(ci->home);
while ((pan = panel_above(pan)) != NULL)
if (panel_userptr(pan) == ci->focus)
break;
if (pan) {
WINDOW *win = panel_window(pan);
del_panel(pan);
delwin(win);
pane_damaged(ci->home, DAMAGED_POSTORDER);
}
return 1;
}
static PANEL * safe pane_panel(struct pane *p safe, struct pane *home)
{
PANEL *pan = NULL;
while ((pan = panel_above(pan)) != NULL)
if (panel_userptr(pan) == p)
return pan;
if (!home)
return pan;
pan = new_panel(newwin(p->h, p->w, 0, 0));
set_panel_userptr(pan, p);
pane_add_notify(home, p, "Notify:Close");
return pan;
}
DEF_CMD(nc_clear)
{
struct pane *p = ci->home;
struct display_data *dd = p->data;
cchar_t cc = {};
int pair = 0;
/* default come from parent when clearing pane */
int attr = cvt_attrs(ci->focus->parent, p, ci->str, &pair);
PANEL *panel;
WINDOW *win;
int w, h;
set_screen(p);
panel = pane_panel(ci->focus, p);
if (!panel)
return Efail;
win = panel_window(panel);
getmaxyx(win, h, w);
if (h != ci->focus->h || w != ci->focus->w) {
wresize(win, ci->focus->h, ci->focus->w);
replace_panel(panel, win);
}
cc.attr = attr;
cc.ext_color = pair;
cc.chars[0] = ' ';
wbkgrndset(win, &cc);
werase(win);
dd->clears += 1;
pane_damaged(p, DAMAGED_POSTORDER);
return 1;
}
DEF_CMD(nc_text_size)
{
int max_space = ci->num;
int max_bytes = 0;
int size = 0;
const char *str = ci->str;
if (!str)
return Enoarg;
while (str[0] != 0) {
wint_t wc = get_utf8(&str, NULL);
int width;
if (wc >= WERR)
break;
width = wcwidth(wc);
if (width < 0)
break;
size += width;
if (size <= max_space)
max_bytes = str - ci->str;
}
return comm_call(ci->comm2, "callback:size", ci->focus,
max_bytes, NULL, NULL,
0, NULL, NULL, size, 1);
}
DEF_CMD(nc_draw_text)
{
struct pane *p = ci->home;
int pair = 0;
int attr = cvt_attrs(ci->focus, p, ci->str2, &pair);
int cursor_offset = ci->num;
short x = ci->x, y = ci->y;
const char *str = ci->str;
if (!str)
return Enoarg;
set_screen(p);
while (str[0] != 0) {
int precurs = str <= ci->str + cursor_offset;
wint_t wc = get_utf8(&str, NULL);
int width;
if (wc == WEOF || wc == WERR)
break;
width = wcwidth(wc);
if (width < 0)
break;
if (precurs && str > ci->str + cursor_offset)
ncurses_text(ci->focus, p, wc, attr, pair, x, y, 1);
else
ncurses_text(ci->focus, p, wc, attr, pair, x, y, 0);
x += width;
}
if (str == ci->str + cursor_offset)
ncurses_text(ci->focus, p, ' ', 0, 0, x, y, 1);
pane_damaged(p, DAMAGED_POSTORDER);
return 1;
}
struct di_info {
struct command c;
MagickWand *wd safe;