-
Notifications
You must be signed in to change notification settings - Fork 10
/
newt.c
1172 lines (994 loc) · 31.8 KB
/
newt.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
#include "config.h"
#include <slang.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/signal.h>
#include <sys/time.h>
#include <sys/types.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
#include <wchar.h>
#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif
#include "newt.h"
#include "newt_pr.h"
struct Window {
int height, width, top, left;
SLsmg_Char_Type * buffer;
char * title;
};
struct keymap {
char * str;
int code;
char * tc;
};
static struct Window windowStack[20];
static struct Window * currentWindow = NULL;
static char * helplineStack[20];
static char ** currentHelpline = NULL;
static int cursorRow, cursorCol;
static int cursorOn = 1;
static int noFlowCtrl = 0;
static int trashScreen = 0;
extern int needResize;
static const char * const defaultHelpLine =
" <Tab>/<Alt-Tab> between elements | <Space> selects | <F12> next screen"
;
const struct newtColors newtDefaultColorPalette = {
"white", "blue", /* root fg, bg */
"black", "lightgray", /* border fg, bg */
"black", "lightgray", /* window fg, bg */
"white", "black", /* shadow fg, bg */
"red", "lightgray", /* title fg, bg */
"lightgray", "red", /* button fg, bg */
"red", "lightgray", /* active button fg, bg */
"lightgray", "blue", /* checkbox fg, bg */
"lightgray", "red", /* active checkbox fg, bg */
"lightgray", "blue", /* entry box fg, bg */
"blue", "lightgray", /* label fg, bg */
"black", "lightgray", /* listbox fg, bg */
"lightgray", "blue", /* active listbox fg, bg */
"black", "lightgray", /* textbox fg, bg */
"lightgray", "red", /* active textbox fg, bg */
"white", "blue", /* help line */
"lightgray", "blue", /* root text */
"blue", /* scale full */
"red", /* scale empty */
"blue", "lightgray", /* disabled entry fg, bg */
"black", "lightgray", /* compact button fg, bg */
"lightgray", "red", /* active & sel listbox */
"black", "brown" /* selected listbox */
};
static const struct keymap keymap[] = {
{ "\033OA", NEWT_KEY_UP, "ku" },
{ "\020", NEWT_KEY_UP, NULL }, /* emacs ^P */
{ "\033OB", NEWT_KEY_DOWN, "kd" },
{ "\016", NEWT_KEY_DOWN, NULL }, /* emacs ^N */
{ "\033OC", NEWT_KEY_RIGHT, "kr" },
{ "\006", NEWT_KEY_RIGHT, NULL }, /* emacs ^F */
{ "\033OD", NEWT_KEY_LEFT, "kl" },
{ "\002", NEWT_KEY_LEFT, NULL }, /* emacs ^B */
{ "\033OH", NEWT_KEY_HOME, "kh" },
{ "\033[1~", NEWT_KEY_HOME, NULL },
{ "\001", NEWT_KEY_HOME, NULL }, /* emacs ^A */
{ "\033Ow", NEWT_KEY_END, "kH" },
{ "\033[4~", NEWT_KEY_END, "@7" },
{ "\005", NEWT_KEY_END, NULL }, /* emacs ^E */
{ "\033[3~", NEWT_KEY_DELETE, "kD" },
{ "\004", NEWT_KEY_DELETE, NULL }, /* emacs ^D */
{ "\033[2~", NEWT_KEY_INSERT, "kI" },
{ "\033\t", NEWT_KEY_UNTAB, "kB" },
{ "\033[Z", NEWT_KEY_UNTAB, NULL },
{ "\033[5~", NEWT_KEY_PGUP, "kP" },
{ "\033[6~", NEWT_KEY_PGDN, "kN" },
{ "\033V", NEWT_KEY_PGUP, NULL },
{ "\033v", NEWT_KEY_PGUP, NULL },
{ "\026", NEWT_KEY_PGDN, NULL },
{ "\033[[A", NEWT_KEY_F1, NULL },
{ "\033[[B", NEWT_KEY_F2, NULL },
{ "\033[[C", NEWT_KEY_F3, NULL },
{ "\033[[D", NEWT_KEY_F4, NULL },
{ "\033[[E", NEWT_KEY_F5, NULL },
{ "\033OP", NEWT_KEY_F1, NULL },
{ "\033OQ", NEWT_KEY_F2, NULL },
{ "\033OR", NEWT_KEY_F3, NULL },
{ "\033OS", NEWT_KEY_F4, NULL },
{ "\033[11~", NEWT_KEY_F1, "k1" },
{ "\033[12~", NEWT_KEY_F2, "k2" },
{ "\033[13~", NEWT_KEY_F3, "k3" },
{ "\033[14~", NEWT_KEY_F4, "k4" },
{ "\033[15~", NEWT_KEY_F5, "k5" },
{ "\033[17~", NEWT_KEY_F6, "k6" },
{ "\033[18~", NEWT_KEY_F7, "k7" },
{ "\033[19~", NEWT_KEY_F8, "k8" },
{ "\033[20~", NEWT_KEY_F9, "k9" },
{ "\033[21~", NEWT_KEY_F10, "k;" },
{ "\033[23~", NEWT_KEY_F11, "F1" },
{ "\033[24~", NEWT_KEY_F12, "F2" },
{ "\033", NEWT_KEY_ESCAPE, "@2" },
{ "\033", NEWT_KEY_ESCAPE, "@9" },
{ "\177", NEWT_KEY_BKSPC, NULL },
{ "\010", NEWT_KEY_BKSPC, NULL },
{ 0 }, /* LEAVE this one */
};
static void initKeymap();
static void freeKeymap();
static newtSuspendCallback suspendCallback = NULL;
static void * suspendCallbackData = NULL;
void newtSetSuspendCallback(newtSuspendCallback cb, void * data) {
suspendCallback = cb;
suspendCallbackData = data;
}
static void handleSigwinch(int signum) {
needResize = 1;
}
static int getkeyInterruptHook(void) {
return -1;
}
int _newt_wstrlen(const char *str, int len) {
mbstate_t ps;
wchar_t tmp;
int nchars = 0;
if (!str) return 0;
if (!len) return 0;
if (len < 0) len = strlen(str);
memset(&ps,0,sizeof(mbstate_t));
while (len > 0) {
int x,y;
x = mbrtowc(&tmp,str,len,&ps);
if (x >0) {
str += x;
len -= x;
y = wcwidth(tmp);
if (y>0)
nchars+=y;
} else break;
}
return nchars;
}
/** Trim a string to fit
* @param title - string. NULL will be inserted if necessary
* @param chrs - available space. (character cells)
*/
void trim_string(char *title, int chrs)
{
char *p = title;
int ln;
int x = 0,y = 0;
wchar_t tmp;
mbstate_t ps;
memset(&ps, 0, sizeof(ps));
ln = strlen(title);
while (*p) {
x = mbrtowc(&tmp, p, ln, &ps);
if (x < 0) { // error
*p = '\0';
return;
}
y = wcwidth(tmp);
if (y > chrs) {
*p = '\0';
return;
} else {
p += x;
ln -= x;
chrs -= y;
}
}
}
static int getkey() {
int c;
while ((c = SLang_getkey()) == '\xC') { /* if Ctrl-L redraw whole screen */
SLsmg_touch_lines(0, SLtt_Screen_Rows);
SLsmg_refresh();
}
return c;
}
static void updateColorset(char *fg, char *bg, char **fg_p, char **bg_p)
{
if (*fg && fg_p)
*fg_p = fg;
if (*bg && bg_p)
*bg_p = bg;
}
/* parse color specifications (e.g. root=,black:border=red,blue)
* and update the palette
*/
static void parseColors(char *s, struct newtColors *palette)
{
char *name, *str, *fg, *bg;
for (str = s; (s = strtok(str, ";:\n\r\t ")); str = NULL) {
name = s;
if (!(s = strchr(s, '=')) || !*s)
continue;
*s = '\0';
fg = ++s;
if (!(s = strchr(s, ',')) || !*s)
continue;
*s = '\0';
bg = ++s;
if (!strcmp(name, "root"))
updateColorset(fg, bg, &palette->rootFg, &palette->rootBg);
else if (!strcmp(name, "border"))
updateColorset(fg, bg, &palette->borderFg, &palette->borderBg);
else if (!strcmp(name, "window"))
updateColorset(fg, bg, &palette->windowFg, &palette->windowBg);
else if (!strcmp(name, "shadow"))
updateColorset(fg, bg, &palette->shadowFg, &palette->shadowBg);
else if (!strcmp(name, "title"))
updateColorset(fg, bg, &palette->titleFg, &palette->titleBg);
else if (!strcmp(name, "button"))
updateColorset(fg, bg, &palette->buttonFg, &palette->buttonBg);
else if (!strcmp(name, "actbutton"))
updateColorset(fg, bg, &palette->actButtonFg, &palette->actButtonBg);
else if (!strcmp(name, "checkbox"))
updateColorset(fg, bg, &palette->checkboxFg, &palette->checkboxBg);
else if (!strcmp(name, "actcheckbox"))
updateColorset(fg, bg, &palette->actCheckboxFg, &palette->actCheckboxBg);
else if (!strcmp(name, "entry"))
updateColorset(fg, bg, &palette->entryFg, &palette->entryBg);
else if (!strcmp(name, "label"))
updateColorset(fg, bg, &palette->labelFg, &palette->labelBg);
else if (!strcmp(name, "listbox"))
updateColorset(fg, bg, &palette->listboxFg, &palette->listboxBg);
else if (!strcmp(name, "actlistbox"))
updateColorset(fg, bg, &palette->actListboxFg, &palette->actListboxBg);
else if (!strcmp(name, "textbox"))
updateColorset(fg, bg, &palette->textboxFg, &palette->textboxBg);
else if (!strcmp(name, "acttextbox"))
updateColorset(fg, bg, &palette->actTextboxFg, &palette->actTextboxBg);
else if (!strcmp(name, "helpline"))
updateColorset(fg, bg, &palette->helpLineFg, &palette->helpLineBg);
else if (!strcmp(name, "roottext"))
updateColorset(fg, bg, &palette->rootTextFg, &palette->rootTextBg);
else if (!strcmp(name, "emptyscale"))
updateColorset(fg, bg, NULL, &palette->emptyScale);
else if (!strcmp(name, "fullscale"))
updateColorset(fg, bg, NULL, &palette->fullScale);
else if (!strcmp(name, "disentry"))
updateColorset(fg, bg, &palette->disabledEntryFg, &palette->disabledEntryBg);
else if (!strcmp(name, "compactbutton"))
updateColorset(fg, bg, &palette->compactButtonFg, &palette->compactButtonBg);
else if (!strcmp(name, "actsellistbox"))
updateColorset(fg, bg, &palette->actSelListboxFg, &palette->actSelListboxBg);
else if (!strcmp(name, "sellistbox"))
updateColorset(fg, bg, &palette->selListboxFg, &palette->selListboxBg);
}
}
static void initColors(void)
{
char *colors, *colors_file, buf[16384];
FILE *f;
struct newtColors palette;
palette = newtDefaultColorPalette;
colors_file = getenv("NEWT_COLORS_FILE");
#ifdef NEWT_COLORS_FILE
if (colors_file == NULL)
colors_file = NEWT_COLORS_FILE;
#endif
if ((colors = getenv("NEWT_COLORS"))) {
strncpy(buf, colors, sizeof (buf));
buf[sizeof (buf) - 1] = '\0';
parseColors(buf, &palette);
} else if (colors_file && *colors_file && (f = fopen(colors_file, "r"))) {
size_t r;
if ((r = fread(buf, 1, sizeof (buf) - 1, f)) > 0) {
buf[r] = '\0';
parseColors(buf, &palette);
}
fclose(f);
}
newtSetColors(palette);
}
void newtFlushInput(void) {
while (SLang_input_pending(0)) {
getkey();
}
}
/**
* @brief Refresh the screen
*/
void newtRefresh(void) {
SLsmg_refresh();
}
void newtSuspend(void) {
SLtt_set_cursor_visibility (1);
SLsmg_suspend_smg();
SLang_reset_tty();
SLtt_set_cursor_visibility (cursorOn);
}
/**
* @brief Return after suspension.
* @return 0 on success.
*/
int newtResume(void) {
SLsmg_resume_smg ();
SLsmg_refresh();
return SLang_init_tty(0, noFlowCtrl, 0);
}
void newtCls(void) {
SLsmg_set_color(NEWT_COLORSET_ROOT);
SLsmg_gotorc(0, 0);
SLsmg_erase_eos();
newtRefresh();
}
/**
* @brief Resize the screen
* @param redraw - boolean - should we redraw the screen?
*/
void newtResizeScreen(int redraw) {
/* we can't redraw from scratch, just redisplay SLang screen */
SLtt_get_screen_size();
/* SLsmg_reinit_smg(); */
if (redraw) {
SLsmg_touch_lines(0, SLtt_Screen_Rows);
newtRefresh();
}
}
/**
* @brief Initialize the newt library
* @return int - 0 for success, else < 0
*/
int newtInit(void) {
char * MonoValue, * MonoEnv = "NEWT_MONO";
char * NoFlowCtrlValue, * NoFlowCtrlEnv = "NEWT_NOFLOWCTRL";
const char *lang;
int ret;
if ((lang = getenv("LC_ALL")) == NULL)
if ((lang = getenv("LC_CTYPE")) == NULL)
if ((lang = getenv("LANG")) == NULL)
lang = "";
/* slang doesn't support multibyte encodings except UTF-8,
avoid character corruption by redrawing the screen */
if (strstr (lang, ".euc") != NULL)
trashScreen = 1;
SLutf8_enable(-1);
SLtt_get_terminfo();
SLtt_get_screen_size();
MonoValue = getenv(MonoEnv);
if ( MonoValue != NULL )
SLtt_Use_Ansi_Colors = 0;
NoFlowCtrlValue = getenv(NoFlowCtrlEnv);
if ( NoFlowCtrlValue != NULL )
noFlowCtrl = 1;
if ((ret = SLsmg_init_smg()) < 0)
return ret;
if ((ret = SLang_init_tty(0, noFlowCtrl, 0)) < 0)
return ret;
initColors();
newtCursorOff();
initKeymap();
SLsignal_intr(SIGWINCH, handleSigwinch);
SLang_getkey_intr_hook = getkeyInterruptHook;
return 0;
}
/**
* @brief Closedown the newt library, tidying screen.
* @returns int , 0. (no errors reported)
*/
int newtFinished(void) {
if (currentWindow) {
for (; currentWindow >= windowStack; currentWindow--) {
free(currentWindow->buffer);
free(currentWindow->title);
}
currentWindow = NULL;
}
if (currentHelpline) {
for (; currentHelpline >= helplineStack; currentHelpline--)
free(*currentHelpline);
currentHelpline = NULL;
}
freeKeymap();
SLsmg_gotorc(SLtt_Screen_Rows - 1, 0);
newtCursorOn();
SLsmg_refresh();
SLsmg_reset_smg();
SLang_reset_tty();
return 0;
}
/**
* @brief Set the colors used.
* @param colors - newtColor struct used.
*/
void newtSetColors(struct newtColors colors) {
if (!SLtt_Use_Ansi_Colors) {
int i;
for (i = 2; i < 25; i++)
SLtt_set_mono(i, NULL, 0);
SLtt_set_mono(NEWT_COLORSET_SELLISTBOX, NULL, SLTT_BOLD_MASK);
SLtt_set_mono(NEWT_COLORSET_ACTBUTTON, NULL, SLTT_REV_MASK);
SLtt_set_mono(NEWT_COLORSET_ACTCHECKBOX, NULL, SLTT_REV_MASK);
SLtt_set_mono(NEWT_COLORSET_ACTLISTBOX, NULL, SLTT_REV_MASK);
SLtt_set_mono(NEWT_COLORSET_ACTTEXTBOX, NULL, SLTT_REV_MASK);
SLtt_set_mono(NEWT_COLORSET_ACTSELLISTBOX, NULL, SLTT_REV_MASK | SLTT_BOLD_MASK);
SLtt_set_mono(NEWT_COLORSET_DISENTRY, NULL, 0); // FIXME
SLtt_set_mono(NEWT_COLORSET_FULLSCALE, NULL, SLTT_ULINE_MASK | SLTT_REV_MASK);
SLtt_set_mono(NEWT_COLORSET_EMPTYSCALE, NULL, SLTT_ULINE_MASK);
return;
}
SLtt_set_color(NEWT_COLORSET_ROOT, "", colors.rootFg, colors.rootBg);
SLtt_set_color(NEWT_COLORSET_BORDER, "", colors.borderFg, colors.borderBg);
SLtt_set_color(NEWT_COLORSET_WINDOW, "", colors.windowFg, colors.windowBg);
SLtt_set_color(NEWT_COLORSET_SHADOW, "", colors.shadowFg, colors.shadowBg);
SLtt_set_color(NEWT_COLORSET_TITLE, "", colors.titleFg, colors.titleBg);
SLtt_set_color(NEWT_COLORSET_BUTTON, "", colors.buttonFg, colors.buttonBg);
SLtt_set_color(NEWT_COLORSET_ACTBUTTON, "", colors.actButtonFg,
colors.actButtonBg);
SLtt_set_color(NEWT_COLORSET_CHECKBOX, "", colors.checkboxFg,
colors.checkboxBg);
SLtt_set_color(NEWT_COLORSET_ACTCHECKBOX, "", colors.actCheckboxFg,
colors.actCheckboxBg);
SLtt_set_color(NEWT_COLORSET_ENTRY, "", colors.entryFg, colors.entryBg);
SLtt_set_color(NEWT_COLORSET_LABEL, "", colors.labelFg, colors.labelBg);
SLtt_set_color(NEWT_COLORSET_LISTBOX, "", colors.listboxFg,
colors.listboxBg);
SLtt_set_color(NEWT_COLORSET_ACTLISTBOX, "", colors.actListboxFg,
colors.actListboxBg);
SLtt_set_color(NEWT_COLORSET_TEXTBOX, "", colors.textboxFg,
colors.textboxBg);
SLtt_set_color(NEWT_COLORSET_ACTTEXTBOX, "", colors.actTextboxFg,
colors.actTextboxBg);
SLtt_set_color(NEWT_COLORSET_HELPLINE, "", colors.helpLineFg,
colors.helpLineBg);
SLtt_set_color(NEWT_COLORSET_ROOTTEXT, "", colors.rootTextFg,
colors.rootTextBg);
SLtt_set_color(NEWT_COLORSET_EMPTYSCALE, "", "white",
colors.emptyScale);
SLtt_set_color(NEWT_COLORSET_FULLSCALE, "", "white",
colors.fullScale);
SLtt_set_color(NEWT_COLORSET_DISENTRY, "", colors.disabledEntryFg,
colors.disabledEntryBg);
SLtt_set_color(NEWT_COLORSET_COMPACTBUTTON, "", colors.compactButtonFg,
colors.compactButtonBg);
SLtt_set_color(NEWT_COLORSET_ACTSELLISTBOX, "", colors.actSelListboxFg,
colors.actSelListboxBg);
SLtt_set_color(NEWT_COLORSET_SELLISTBOX, "", colors.selListboxFg,
colors.selListboxBg);
}
void newtSetColor(int colorset, char *fg, char *bg) {
if (colorset < NEWT_COLORSET_ROOT ||
(colorset > NEWT_COLORSET_SELLISTBOX && colorset < NEWT_COLORSET_CUSTOM(0)) ||
!SLtt_Use_Ansi_Colors)
return;
SLtt_set_color(colorset, "", fg, bg);
}
/* Keymap handling - rewritten by Henning Makholm <[email protected]>,
* November 2003.
*/
struct kmap_trie_entry {
char alloced; /* alloced/not first element in array */
char c ; /* character got from terminal */
int code; /* newt key, or 0 if c does not make a complete sequence */
struct kmap_trie_entry *contseq; /* sub-trie for character following c */
struct kmap_trie_entry *next; /* try this if char received != c */
};
static struct kmap_trie_entry *kmap_trie_root = NULL;
static int keyreader_buf_len = 10 ;
static unsigned char default_keyreader_buf[10];
static unsigned char *keyreader_buf = default_keyreader_buf;
#if 0 /* for testing of the keymap manipulation code */
static void dumpkeys_recursive(struct kmap_trie_entry *curr, int i, FILE *f) {
int j, ps ;
char seen[256]={0};
if( curr && i >= keyreader_buf_len ) {
fprintf(f,"ARGH! Too long sequence!\n") ;
return ;
}
for(;curr;curr=curr->next) {
keyreader_buf[i] = curr->c ;
ps = seen[(unsigned char)curr->c]++ ;
if( ps || curr->code || (!curr->code && !curr->contseq) ) {
for(j=0;j<=i;j++) {
if( keyreader_buf[j] > 32 && keyreader_buf[j]<127 &&
keyreader_buf[j] != '^' && keyreader_buf[j] != '\\' )
fprintf(f,"%c",keyreader_buf[j]);
else if( keyreader_buf[j] > 0 && keyreader_buf[j]<=32 )
fprintf(f,"^%c",keyreader_buf[j] + 0x40);
else
fprintf(f,"\\%03o",
(unsigned)(unsigned char)keyreader_buf[j]);
}
if( curr->code )
fprintf(f,": 0x%X\n",curr->code);
else
fprintf(f,": (just keymap)\n");
}
dumpkeys_recursive(curr->contseq,i+1,f);
}
}
static void dump_keymap(void) {
FILE *f = fopen("newt.keydump","wt");
if (f) {
dumpkeys_recursive(kmap_trie_root, 0, f);
fclose(f);
}
}
#endif
/* newtBindKey may overwrite a binding that is there already */
static void newtBindKey(char *keyseq, int meaning) {
struct kmap_trie_entry *root = kmap_trie_root ;
struct kmap_trie_entry **curptr = &root ;
/* Try to make sure the common matching buffer is long enough. */
if( strlen(keyseq) > keyreader_buf_len ) {
int i = strlen(keyseq)+10;
unsigned char *newbuf = malloc(i);
if (newbuf) {
if (keyreader_buf != default_keyreader_buf)
free(keyreader_buf);
keyreader_buf = newbuf;
keyreader_buf_len = i;
}
}
if (*keyseq == 0) return; /* binding the empty sequence is meaningless */
while(1) {
while ((*curptr) && (*curptr)->c != *keyseq)
curptr = &(*curptr)->next;
if ((*curptr)==0) {
struct kmap_trie_entry* fresh
= calloc(strlen(keyseq),sizeof(struct kmap_trie_entry));
if (fresh == 0) return; /* despair! */
fresh->alloced = 1;
*curptr = fresh;
while (keyseq[1]) {
fresh->contseq = fresh+1;
(fresh++)->c = *(keyseq++);
}
fresh->c = *keyseq;
fresh->code = meaning;
return;
}
if (keyseq[1]==0) {
(*curptr)->code = meaning;
return;
} else {
curptr = &(*curptr)->contseq;
keyseq++;
}
}
}
/* This function recursively inserts all entries in the "to" trie into
corresponding positions in the "from" trie, except positions that
are already defined in the "from" trie. */
static void kmap_trie_fallback(struct kmap_trie_entry *to,
struct kmap_trie_entry **from) {
if (*from == NULL)
*from = to ;
if (*from == to)
return ;
for (;to!=NULL;to=to->next) {
struct kmap_trie_entry **fromcopy = from ;
while ((*fromcopy) && (*fromcopy)->c != to->c)
fromcopy = &(*fromcopy)->next ;
if (*fromcopy) {
if ((*fromcopy)->code == 0)
(*fromcopy)->code = to->code;
kmap_trie_fallback(to->contseq, &(*fromcopy)->contseq);
} else {
*fromcopy = malloc(sizeof(struct kmap_trie_entry));
if (*fromcopy) {
**fromcopy = *to ;
(*fromcopy)->alloced = 1;
(*fromcopy)->next = 0 ;
}
}
}
}
int newtGetKey(void) {
int key, lastcode, errors = 0;
unsigned char *chptr = keyreader_buf, *lastmatch;
struct kmap_trie_entry *curr = kmap_trie_root;
do {
key = getkey();
if (key == SLANG_GETKEY_ERROR) {
if (needResize) {
needResize = 0;
return NEWT_KEY_RESIZE;
}
/* Ignore other signals, but assume that stdin disappeared (the
* parent terminal was proably closed) if the error persists.
*/
if (errors++ > 10)
return NEWT_KEY_ERROR;
continue;
}
if (key == NEWT_KEY_SUSPEND && suspendCallback)
suspendCallback(suspendCallbackData);
} while (key == NEWT_KEY_SUSPEND || key == SLANG_GETKEY_ERROR);
/* Read more characters, matching against the trie as we go */
lastcode = *chptr = key;
lastmatch = chptr ;
while(1) {
while (curr->c != key) {
curr = curr->next ;
if (curr==NULL) goto break2levels;
}
if (curr->code) {
lastcode = curr->code;
lastmatch = chptr;
}
curr = curr->contseq;
if (curr==NULL) break;
if (SLang_input_pending(5) <= 0)
break;
if (chptr==keyreader_buf+keyreader_buf_len-1) break;
*++chptr = key = getkey();
}
break2levels:
/* The last time the trie matched was at position lastmatch. Back
* up if we have read too many characters. */
while (chptr > lastmatch)
SLang_ungetkey(*chptr--);
return lastcode;
}
/**
* @brief Wait for a keystroke
*/
void newtWaitForKey(void) {
newtRefresh();
getkey();
newtClearKeyBuffer();
}
/**
* @brief Clear the keybuffer
*/
void newtClearKeyBuffer(void) {
while (SLang_input_pending(1)) {
getkey();
}
}
/**
* Open a new window.
* @param left. int Size; _not_ including border
* @param top: int size, _not_ including border
* @param width unsigned int
* @param height unsigned int
* @param title - title string
* @return zero on success
*/
int newtOpenWindow(int left, int top,
unsigned int width, unsigned int height,
const char * title) {
int j, row, col;
int n;
int i;
newtFlushInput();
if (currentWindow && currentWindow - windowStack + 1
>= sizeof (windowStack) / sizeof (struct Window))
return 1;
if (!currentWindow) {
currentWindow = windowStack;
} else {
currentWindow++;
}
currentWindow->left = left;
currentWindow->top = top;
currentWindow->width = width;
currentWindow->height = height;
currentWindow->title = title ? strdup(title) : NULL;
currentWindow->buffer = malloc(sizeof(SLsmg_Char_Type) * (width + 5) * (height + 3));
row = top - 1;
col = left - 2;
/* clip to the current screen bounds - msw */
if (row < 0)
row = 0;
if (col < 0)
col = 0;
if (left + width > SLtt_Screen_Cols)
width = SLtt_Screen_Cols - left;
if (top + height > SLtt_Screen_Rows)
height = SLtt_Screen_Rows - top;
n = 0;
for (j = 0; j < height + 3; j++, row++) {
SLsmg_gotorc(row, col);
SLsmg_read_raw(currentWindow->buffer + n,
currentWindow->width + 5);
n += currentWindow->width + 5;
}
newtTrashScreen();
SLsmg_set_color(NEWT_COLORSET_BORDER);
SLsmg_set_char_set(1);
SLsmg_draw_box(top - 1, left - 1, height + 2, width + 2);
SLsmg_set_char_set(0);
if (currentWindow->title) {
trim_string (currentWindow->title, width-4);
i = wstrlen(currentWindow->title,-1) + 4;
i = ((width - i) / 2) + left;
SLsmg_gotorc(top - 1, i);
SLsmg_set_char_set(1);
SLsmg_write_char(SLSMG_RTEE_CHAR);
SLsmg_set_char_set(0);
SLsmg_write_char(' ');
SLsmg_set_color(NEWT_COLORSET_TITLE);
SLsmg_write_string((char *)currentWindow->title);
SLsmg_set_color(NEWT_COLORSET_BORDER);
SLsmg_write_char(' ');
SLsmg_set_char_set(1);
SLsmg_write_char(SLSMG_LTEE_CHAR);
SLsmg_set_char_set(0);
}
SLsmg_set_color(NEWT_COLORSET_WINDOW);
SLsmg_fill_region(top, left, height, width, ' ');
SLsmg_set_color(NEWT_COLORSET_SHADOW);
SLsmg_fill_region(top + height + 1, left, 1, width + 2, ' ');
SLsmg_fill_region(top, left + width + 1, height + 1, 1, ' ');
for (i = top; i < (top + height + 1); i++) {
SLsmg_gotorc(i, left + width + 1);
SLsmg_write_string(" ");
}
return 0;
}
/**
* @brief Draw a centered window.
* @param width - width in char cells
* @param height - no. of char cells.
* @param title - fixed title
* @returns zero on success
*/
int newtCenteredWindow(unsigned int width,unsigned int height,
const char * title) {
int top, left;
top = (int)(SLtt_Screen_Rows - height) / 2;
/* I don't know why, but this seems to look better */
if ((SLtt_Screen_Rows % 2) && (top % 2)) top--;
left = (int)(SLtt_Screen_Cols - width) / 2;
return newtOpenWindow(left, top, width, height, title);
}
/**
* @brief Remove the top window
*/
void newtPopWindow(void) {
newtPopWindowNoRefresh();
newtRefresh();
}
void newtPopWindowNoRefresh(void) {
int j, row, col;
int n = 0;
if (currentWindow == NULL)
return;
row = currentWindow->top - 1;
col = currentWindow->left - 2;
if (row < 0)
row = 0;
if (col < 0)
col = 0;
for (j = 0; j < currentWindow->height + 3; j++, row++) {
SLsmg_gotorc(row, col);
SLsmg_write_raw(currentWindow->buffer + n,
currentWindow->width + 5);
n += currentWindow->width + 5;
}
free(currentWindow->buffer);
free(currentWindow->title);
if (currentWindow == windowStack)
currentWindow = NULL;
else
currentWindow--;
SLsmg_set_char_set(0);
newtTrashScreen();
}
void newtGetWindowPos(int * x, int * y) {
if (currentWindow) {
*x = currentWindow->left;
*y = currentWindow->top;
} else
*x = *y = 0;
}
void newtGetrc(int * row, int * col) {
*row = cursorRow;
*col = cursorCol;
if (currentWindow) {
*row -= currentWindow->top;
*col -= currentWindow->left;
}
}
void newtGotorc(int newRow, int newCol) {
if (currentWindow) {
newRow += currentWindow->top;
newCol += currentWindow->left;
}
cursorRow = newRow;
cursorCol = newCol;
SLsmg_gotorc(cursorRow, cursorCol);
}
void newtDrawBox(int left, int top, int width, int height, int shadow) {
if (currentWindow) {
top += currentWindow->top;
left += currentWindow->left;
}
SLsmg_draw_box(top, left, height, width);
if (shadow) {
SLsmg_set_color(NEWT_COLORSET_SHADOW);
SLsmg_fill_region(top + height, left + 1, 1, width - 1, ' ');
SLsmg_fill_region(top + 1, left + width, height, 1, ' ');
}
}
void newtClearBox(int left, int top, int width, int height) {
if (currentWindow) {
top += currentWindow->top;
left += currentWindow->left;
}
SLsmg_fill_region(top, left, height, width, ' ');
}
static void initKeymap(void) {
const struct keymap * curr;
struct kmap_trie_entry *kmap_trie_escBrack, *kmap_trie_escO;
/* Here are some entries that will help in handling esc O foo and
esc [ foo as variants of each other. */
kmap_trie_root = calloc(3, sizeof (struct kmap_trie_entry));
kmap_trie_escBrack = kmap_trie_root + 1;
kmap_trie_escO = kmap_trie_root + 2;
kmap_trie_root->alloced = 1;
kmap_trie_root->c = '\033';
kmap_trie_root->contseq = kmap_trie_escBrack;
kmap_trie_escBrack->c = '[';
kmap_trie_escBrack->next = kmap_trie_escO;
kmap_trie_escO->c = 'O';
/* First bind built-in default bindings. They may be shadowed by
the termcap entries that get bound later. */
for (curr = keymap; curr->code; curr++) {
if (curr->str)
newtBindKey(curr->str,curr->code);
}
/* Then bind strings from termcap entries */
for (curr = keymap; curr->code; curr++) {
if (curr->tc) {
char *pc = SLtt_tgetstr(curr->tc);
if (pc) {
newtBindKey(pc,curr->code);
}
}
}
/* Finally, invent lowest-priority keybindings that correspond to
searching for esc-O-foo if esc-[-foo was not found and vice
versa. That is needed because of strong confusion among
different emulators of VTxxx terminals; some terminfo/termcap
descriptions are apparently written by people who were not
aware of the differences between "applicataion" and "terminal"
keypad modes. Or perhaps they were, but tried to make their
description work with a program that puts the keyboard in the
wrong emulation mode. In short, one needs this: */
kmap_trie_fallback(kmap_trie_escO->contseq, &kmap_trie_escBrack->contseq);
kmap_trie_fallback(kmap_trie_escBrack->contseq, &kmap_trie_escO->contseq);
}
static void free_keys(struct kmap_trie_entry *kmap, struct kmap_trie_entry *parent, int prepare) {
if (kmap == NULL)
return;