/
mpv_curses.c
852 lines (697 loc) · 17.5 KB
/
mpv_curses.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
/*
Minimum Profit - Programmer Text Editor
Curses driver.
Copyright (C) 1991-2010 Angel Ortega <angel@triptico.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
http://www.triptico.com
*/
#include "config.h"
#ifdef CONFOPT_CURSES
#include <stdio.h>
#include <wchar.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <ncursesw/ncurses.h>
#include "mpdm.h"
#include "mpsl.h"
#include "mp.h"
/** data **/
/* the curses attributes */
int *nc_attrs = NULL;
/* code for the 'normal' attribute */
static int normal_attr = 0;
/* current window */
static WINDOW *cw = NULL;
/* stack of windows */
static int n_stack = 0;
static WINDOW **w_stack = NULL;
/* last attr set */
static int last_attr = 0;
/* timer function */
static int timer_msecs = 0;
static mpdm_t timer_func = NULL;
/** code **/
static void set_attr(void)
/* set the current and fill attributes */
{
wattrset(cw, nc_attrs[last_attr]);
wbkgdset(cw, ' ' | nc_attrs[last_attr]);
}
static void nc_sigwinch(int s)
/* SIGWINCH signal handler */
{
mpdm_t v;
#ifdef NCURSES_VERSION
/* Make sure that window size changes... */
struct winsize ws;
int fd = open("/dev/tty", O_RDWR);
if (fd == -1)
return; /* This should never have to happen! */
if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
resizeterm(ws.ws_row, ws.ws_col);
close(fd);
#else
/* restart curses */
/* ... */
#endif
/* invalidate main window */
clearok(stdscr, 1);
refresh();
/* re-set dimensions */
v = mpdm_hget_s(mp, L"window");
mpdm_hset_s(v, L"tx", MPDM_I(COLS));
mpdm_hset_s(v, L"ty", MPDM_I(LINES));
/* reattach */
signal(SIGWINCH, nc_sigwinch);
}
#ifdef CONFOPT_WGET_WCH
int wget_wch(WINDOW * w, wint_t * ch);
#endif
static wchar_t *nc_getwch(void)
/* gets a key as a wchar_t */
{
static wchar_t c[2];
#ifdef CONFOPT_WGET_WCH
/* set timer period */
if (timer_msecs > 0)
timeout(timer_msecs);
if (wget_wch(stdscr, (wint_t *) c) == -1)
c[0] = (wchar_t) - 1;
#else
char tmp[MB_CUR_MAX + 1];
int cc, n = 0;
/* read one byte */
cc = wgetch(cw);
if (has_key(cc)) {
c[0] = cc;
return c;
}
/* set to non-blocking */
nodelay(cw, 1);
/* read all possible following characters */
tmp[n++] = cc;
while (n < sizeof(tmp) - 1 && (cc = getch()) != ERR)
tmp[n++] = cc;
/* sets input as blocking */
nodelay(cw, 0);
tmp[n] = '\0';
mbstowcs(c, tmp, n);
#endif
c[1] = '\0';
return c;
}
#define ctrl(k) ((k) & 31)
static mpdm_t nc_getkey(mpdm_t args, mpdm_t ctxt)
/* reads a key and converts to an action */
{
static int shift = 0;
wchar_t *f = NULL;
mpdm_t k = NULL;
/* any pending key? return it */
if ((k = mp_pending_key()) != NULL)
return k;
f = nc_getwch();
if (f[0] == -1) {
mpdm_void(mpdm_exec(timer_func, NULL, NULL));
return NULL;
}
if (shift) {
switch (f[0]) {
case L'0':
f = L"f10";
break;
case L'1':
f = L"f1";
break;
case L'2':
f = L"f2";
break;
case L'3':
f = L"f3";
break;
case L'4':
f = L"f4";
break;
case L'5':
f = L"f5";
break;
case L'6':
f = L"f6";
break;
case L'7':
f = L"f7";
break;
case L'8':
f = L"f8";
break;
case L'9':
f = L"f9";
break;
case KEY_LEFT:
f = L"alt-cursor-left";
break;
case KEY_RIGHT:
f = L"alt-cursor-right";
break;
case KEY_DOWN:
f = L"alt-cursor-down";
break;
case KEY_UP:
f = L"alt-cursor-up";
break;
case KEY_END:
f = L"alt-end";
break;
case KEY_HOME:
f = L"alt-home";
break;
case L'\r':
f = L"alt-enter";
break;
case L'\e':
f = L"escape";
break;
case KEY_ENTER:
f = L"alt-enter";
break;
case L' ':
f = L"alt-space";
break;
case L'a':
f = L"alt-a";
break;
case L'b':
f = L"alt-b";
break;
case L'c':
f = L"alt-c";
break;
case L'd':
f = L"alt-d";
break;
case L'e':
f = L"alt-e";
break;
case L'f':
f = L"alt-f";
break;
case L'g':
f = L"alt-g";
break;
case L'h':
f = L"alt-h";
break;
case L'i':
f = L"alt-i";
break;
case L'j':
f = L"alt-j";
break;
case L'k':
f = L"alt-k";
break;
case L'l':
f = L"alt-l";
break;
case L'm':
f = L"alt-m";
break;
case L'n':
f = L"alt-n";
break;
case L'o':
f = L"alt-o";
break;
case L'p':
f = L"alt-p";
break;
case L'q':
f = L"alt-q";
break;
case L'r':
f = L"alt-r";
break;
case L's':
f = L"alt-s";
break;
case L't':
f = L"alt-t";
break;
case L'u':
f = L"alt-u";
break;
case L'v':
f = L"alt-v";
break;
case L'w':
f = L"alt-w";
break;
case L'x':
f = L"alt-x";
break;
case L'y':
f = L"alt-y";
break;
case L'z':
f = L"alt-z";
break;
case L'\'':
f = L"alt-'";
break;
case L',':
f = L"alt-,";
break;
case L'-':
f = L"alt--";
break;
case L'.':
f = L"alt-.";
break;
case L'/':
f = L"alt-/";
break;
case L'=':
f = L"alt-=";
break;
}
shift = 0;
}
else {
switch (f[0]) {
case KEY_LEFT:
f = L"cursor-left";
break;
case KEY_RIGHT:
f = L"cursor-right";
break;
case KEY_UP:
f = L"cursor-up";
break;
case KEY_DOWN:
f = L"cursor-down";
break;
case KEY_PPAGE:
f = L"page-up";
break;
case KEY_NPAGE:
f = L"page-down";
break;
case KEY_HOME:
f = L"home";
break;
case KEY_END:
f = L"end";
break;
case KEY_LL:
f = L"end";
break;
case KEY_IC:
f = L"insert";
break;
case KEY_DC:
f = L"delete";
break;
case 0x7f:
case KEY_BACKSPACE:
case L'\b':
f = L"backspace";
break;
case L'\r':
case KEY_ENTER:
f = L"enter";
break;
case L'\t':
f = L"tab";
break;
case KEY_BTAB:
f = L"shift-tab";
break;
case L' ':
f = L"space";
break;
case KEY_F(1):
f = L"f1";
break;
case KEY_F(2):
f = L"f2";
break;
case KEY_F(3):
f = L"f3";
break;
case KEY_F(4):
f = L"f4";
break;
case KEY_F(5):
f = L"f5";
break;
case KEY_F(6):
f = L"f6";
break;
case KEY_F(7):
f = L"f7";
break;
case KEY_F(8):
f = L"f8";
break;
case KEY_F(9):
f = L"f9";
break;
case KEY_F(10):
f = L"f10";
break;
case ctrl(' '):
f = L"ctrl-space";
break;
case ctrl('a'):
f = L"ctrl-a";
break;
case ctrl('b'):
f = L"ctrl-b";
break;
case ctrl('c'):
f = L"ctrl-c";
break;
case ctrl('d'):
f = L"ctrl-d";
break;
case ctrl('e'):
f = L"ctrl-e";
break;
case ctrl('f'):
f = L"ctrl-f";
break;
case ctrl('g'):
f = L"ctrl-g";
break;
case ctrl('j'):
f = L"ctrl-j";
break;
case ctrl('k'):
f = L"ctrl-k";
break;
case ctrl('l'):
f = L"ctrl-l";
break;
case ctrl('n'):
f = L"ctrl-n";
break;
case ctrl('o'):
f = L"ctrl-o";
break;
case ctrl('p'):
f = L"ctrl-p";
break;
case ctrl('q'):
f = L"ctrl-q";
break;
case ctrl('r'):
f = L"ctrl-r";
break;
case ctrl('s'):
f = L"ctrl-s";
break;
case ctrl('t'):
f = L"ctrl-t";
break;
case ctrl('u'):
f = L"ctrl-u";
break;
case ctrl('v'):
f = L"ctrl-v";
break;
case ctrl('w'):
f = L"ctrl-w";
break;
case ctrl('x'):
f = L"ctrl-x";
break;
case ctrl('y'):
f = L"ctrl-y";
break;
case ctrl('z'):
f = L"ctrl-z";
break;
case L'\e':
shift = 1;
f = NULL;
break;
}
}
if (f != NULL) {
mpdm_t t;
k = mpdm_ref(MPDM_S(f));
if ((t = mp_process_keyseq(k)) != k) {
mpdm_unref(k);
k = t;
}
else
mpdm_unrefnd(t);
}
return k;
}
static mpdm_t nc_addwstr(mpdm_t str)
/* draws a string */
{
wchar_t *wptr = mpdm_string(str);
#ifndef CONFOPT_ADDWSTR
char *cptr;
cptr = mpdm_wcstombs(wptr, NULL);
waddstr(cw, cptr);
free(cptr);
#else
waddwstr(cw, wptr);
#endif /* CONFOPT_ADDWSTR */
return NULL;
}
static mpdm_t nc_doc_draw(mpdm_t args, mpdm_t ctxt)
/* draws the document part */
{
mpdm_t d;
int n, m;
werase(cw);
d = mpdm_aget(args, 0);
d = mpdm_ref(mp_draw(d, 0));
for (n = 0; n < mpdm_size(d); n++) {
mpdm_t l = mpdm_aget(d, n);
wmove(cw, n, 0);
for (m = 0; m < mpdm_size(l); m++) {
int attr;
mpdm_t s;
/* get the attribute and the string */
attr = mpdm_ival(mpdm_aget(l, m++));
s = mpdm_aget(l, m);
wattrset(cw, nc_attrs[attr]);
nc_addwstr(s);
}
}
mpdm_unref(d);
return NULL;
}
static void build_colors(void)
/* builds the colors */
{
mpdm_t colors;
mpdm_t color_names;
mpdm_t l;
mpdm_t c;
int n, s;
#ifdef CONFOPT_TRANSPARENCY
use_default_colors();
#define DEFAULT_INK -1
#define DEFAULT_PAPER -1
#else /* CONFOPT_TRANSPARENCY */
#define DEFAULT_INK COLOR_BLACK
#define DEFAULT_PAPER COLOR_WHITE
#endif
/* gets the color definitions and attribute names */
colors = mpdm_hget_s(mp, L"colors");
color_names = mpdm_hget_s(mp, L"color_names");
l = mpdm_ref(mpdm_keys(colors));
s = mpdm_size(l);
/* redim the structures */
nc_attrs = realloc(nc_attrs, sizeof(int) * s);
/* loop the colors */
for (n = 0; n < s && (c = mpdm_aget(l, n)) != NULL; n++) {
mpdm_t d = mpdm_hget(colors, c);
mpdm_t v = mpdm_hget_s(d, L"text");
int cp, c0, c1;
/* store the 'normal' attribute */
if (wcscmp(mpdm_string(c), L"normal") == 0)
normal_attr = n;
/* store the attr */
mpdm_hset_s(d, L"attr", MPDM_I(n));
/* get color indexes */
if ((c0 = mpdm_seek(color_names, mpdm_aget(v, 0), 1)) == -1 ||
(c1 = mpdm_seek(color_names, mpdm_aget(v, 1), 1)) == -1)
continue;
init_pair(n + 1, c0 - 1, c1 - 1);
cp = COLOR_PAIR(n + 1);
/* flags */
v = mpdm_hget_s(d, L"flags");
if (mpdm_seek_s(v, L"reverse", 1) != -1)
cp |= A_REVERSE;
if (mpdm_seek_s(v, L"bright", 1) != -1)
cp |= A_BOLD;
if (mpdm_seek_s(v, L"underline", 1) != -1)
cp |= A_UNDERLINE;
nc_attrs[n] = cp;
}
/* set the background filler */
wbkgdset(cw, ' ' | nc_attrs[normal_attr]);
mpdm_unref(l);
}
/** driver functions **/
static mpdm_t ncursesw_drv_timer(mpdm_t a, mpdm_t ctxt)
{
mpdm_t func = mpdm_aget(a, 1);
timer_msecs = mpdm_ival(mpdm_aget(a, 0));
mpdm_ref(func);
mpdm_unref(timer_func);
timer_func = func;
return NULL;
}
static mpdm_t ncursesw_drv_shutdown(mpdm_t a, mpdm_t ctxt)
{
mpdm_t v;
endwin();
if ((v = mpdm_hget_s(mp, L"exit_message")) != NULL) {
mpdm_write_wcs(stdout, mpdm_string(v));
printf("\n");
}
return NULL;
}
/** TUI **/
static mpdm_t tui_addstr(mpdm_t a, mpdm_t ctxt)
/* TUI: add a string */
{
return nc_addwstr(mpdm_aget(a, 0));
}
static mpdm_t tui_move(mpdm_t a, mpdm_t ctxt)
/* TUI: move to a screen position */
{
/* curses' move() use y, x */
wmove(cw, mpdm_ival(mpdm_aget(a, 1)), mpdm_ival(mpdm_aget(a, 0)));
/* if third argument is not NULL, clear line */
if (mpdm_aget(a, 2) != NULL)
wclrtoeol(cw);
return NULL;
}
static mpdm_t tui_attr(mpdm_t a, mpdm_t ctxt)
/* TUI: set attribute for next string */
{
last_attr = mpdm_ival(mpdm_aget(a, 0));
set_attr();
return NULL;
}
static mpdm_t tui_refresh(mpdm_t a, mpdm_t ctxt)
/* TUI: refresh the screen */
{
wrefresh(cw);
return NULL;
}
static mpdm_t tui_getxy(mpdm_t a, mpdm_t ctxt)
/* TUI: returns the x and y cursor position */
{
mpdm_t v;
int x, y;
getyx(cw, y, x);
v = MPDM_A(2);
mpdm_ref(v);
mpdm_aset(v, MPDM_I(x), 0);
mpdm_aset(v, MPDM_I(y), 1);
mpdm_unrefnd(v);
return v;
}
static mpdm_t tui_openpanel(mpdm_t a, mpdm_t ctxt)
/* opens a panel (creates new window) */
{
n_stack++;
w_stack = realloc(w_stack, n_stack * sizeof(WINDOW *));
cw = w_stack[n_stack - 1] = newwin(mpdm_ival(mpdm_aget(a, 3)),
mpdm_ival(mpdm_aget(a, 2)),
mpdm_ival(mpdm_aget(a, 1)),
mpdm_ival(mpdm_aget(a, 0)));
set_attr();
wclrtobot(cw);
box(cw, 0, 0);
return NULL;
}
static mpdm_t tui_closepanel(mpdm_t a, mpdm_t ctxt)
/* closes a panel (deletes last window) */
{
n_stack--;
delwin(w_stack[n_stack]);
w_stack = realloc(w_stack, n_stack * sizeof(WINDOW *));
cw = n_stack == 0 ? stdscr : w_stack[n_stack - 1];
touchwin(cw);
wrefresh(cw);
return NULL;
}
static void register_functions(void)
{
mpdm_t drv;
mpdm_t tui;
drv = mpdm_hget_s(mp, L"drv");
mpdm_hset_s(drv, L"timer", MPDM_X(ncursesw_drv_timer));
mpdm_hset_s(drv, L"shutdown", MPDM_X(ncursesw_drv_shutdown));
tui = mpsl_eval(MPDM_LS(L"load('mp_tui.mpsl');"), NULL, NULL);
/* FIXME: if tui failed, a fatal error must be shown */
/* if((e = mpdm_hget_s(mpdm_root(), L"ERROR")) != NULL)
{
mpdm_write_wcs(stdout, mpdm_string(e));
printf("\n");
return(0);
}*/
/* execute tui */
mpdm_hset_s(tui, L"getkey", MPDM_X(nc_getkey));
mpdm_hset_s(tui, L"addstr", MPDM_X(tui_addstr));
mpdm_hset_s(tui, L"move", MPDM_X(tui_move));
mpdm_hset_s(tui, L"attr", MPDM_X(tui_attr));
mpdm_hset_s(tui, L"refresh", MPDM_X(tui_refresh));
mpdm_hset_s(tui, L"getxy", MPDM_X(tui_getxy));
mpdm_hset_s(tui, L"openpanel", MPDM_X(tui_openpanel));
mpdm_hset_s(tui, L"closepanel", MPDM_X(tui_closepanel));
mpdm_hset_s(tui, L"doc_draw", MPDM_X(nc_doc_draw));
}
static mpdm_t ncursesw_drv_startup(mpdm_t a)
{
mpdm_t v;
register_functions();
initscr();
start_color();
keypad(stdscr, TRUE);
nonl();
raw();
noecho();
build_colors();
v = mpdm_hget_s(mp, L"window");
mpdm_hset_s(v, L"tx", MPDM_I(COLS));
mpdm_hset_s(v, L"ty", MPDM_I(LINES));
signal(SIGWINCH, nc_sigwinch);
cw = stdscr;
return NULL;
}
int ncursesw_drv_detect(int *argc, char ***argv)
{
mpdm_t drv;
drv = mpdm_hget_s(mp, L"drv");
mpdm_hset_s(drv, L"id", MPDM_LS(L"curses"));
mpdm_hset_s(drv, L"startup", MPDM_X(ncursesw_drv_startup));
return 1;
}
#endif /* CONFOPT_CURSES */