forked from msokalski/ascii-patrol
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec_nix.cpp
2049 lines (1674 loc) · 36.2 KB
/
spec_nix.cpp
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
#ifdef NIX
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xresource.h>
#include <X11/XKBlib.h>
#include <pthread.h>
#include <X11/extensions/XInput2.h>
#include <pulse/pulseaudio.h>
static bool raw_keyboard = false;
static bool use_xterm_256 = true;
static bool use_xi = false;
static int xi_opcode = -1;
static Display* dpy = 0;
static Window win; // current focus
static Window root; // default root
static Window victim; // our victim
#include <string.h>
#include <signal.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef FIONREAD
// CYGWIN
#include <sys/socket.h>
#elif defined(__APPLE__)
#include <fcntl.h>
#else
// LINUX
#include <linux/kd.h>
#include <fcntl.h>
#define LINUX
#endif
// pollution from <pulse/pulseaudio.h>
#ifdef MIN
#undef MIN
#endif
// pollution from <pulse/pulseaudio.h>
#ifdef MAX
#undef MAX
#endif
#include "spec.h"
#include "conf.h"
#include "menu.h"
void DBG(const char* str)
{
}
bool has_key_releases()
{
return dpy!=0 || raw_keyboard;
}
int fopen_s(FILE** f, const char* path, const char* mode)
{
if (!f)
return -1;
*f = fopen(path,mode);
return *f==0;
}
unsigned int get_time()
{
struct timeval now;
gettimeofday(&now, NULL);
unsigned int tm = now.tv_sec*1000 + now.tv_usec/1000;
if (!tm)
tm=1;
return tm;
}
static char read_buf[3];
static int read_len=0;
bool get_input_len(int* r)
{
if (!dpy)
{
ioctl( fileno(stdin), FIONREAD, r);
r+=read_len;
return true;
}
*r = XPending(dpy);
return true;
}
char read_char()
{
while (1)
{
if (!read_len)
{
int nm=0;
get_input_len(&nm);
if (!nm)
return 0;
read_buf[0] = getchar();
read_len++;
}
if (read_buf[0]==27)
{
if (read_len==1)
{
int nm=0;
get_input_len(&nm);
if (!nm)
{
read_len=0;
return 27;
}
read_buf[1] = getchar();
read_len=2;
}
if (read_buf[1]!='[')
{
read_len--;
if (read_len)
memmove(read_buf, read_buf+1, read_len);
return 27;
}
if (read_len==2)
{
int nm=0;
get_input_len(&nm);
if (!nm)
return 0;
read_buf[2]=getchar();
read_len=3;
}
char ch=0;
switch (read_buf[2])
{
case 'A':
ch = KBD_UP;
break;
case 'B':
ch = KBD_DN;
break;
case 'C':
ch = KBD_RT;
break;
case 'D':
ch = KBD_LT;
break;
case 72: // LX
ch = KBD_HOM;
break;
case 70: // LX
ch = KBD_END;
break;
}
if (ch)
{
read_len=0;
return ch;
}
int nm=0;
get_input_len(&nm);
if (!nm)
return 0;
char tilde = getchar();
switch (read_buf[2])
{
case 55: // rxvt
case 49:
ch = KBD_HOM;
break;
case 50:
ch = KBD_INS;
break;
case 51:
ch = KBD_DEL;
break;
case 56: // rxvt
case 52:
ch = KBD_END;
break;
case 53:
ch = KBD_PUP;
break;
case 54:
ch = KBD_PDN;
break;
}
read_len=0;
if (tilde!='~')
{
read_buf[0] = tilde;
read_len = 1;
}
return ch;
}
else
{
char ch = read_buf[0];
read_len--;
if (read_len)
memmove(read_buf, read_buf+1, read_len);
if (ch==10)
ch=13; // enter
if (ch==127)
ch=8; // bk space
return ch;
}
}
}
static const int key_map[]=
{
0,0,0,0,0,0,0,0,
0,27,'!1','@2','#3','$4','%5','^6','&7','*8','(9',')0','_-','+=',8,
'\t','Qq','Ww','Ee','Rr','Tt','Yy','Uu','Ii','Oo','Pp','{[','}]',13,
0,'Aa','Ss','Dd','Ff','Gg','Hh','Jj','Kk','Ll',':;','\"\'',
'~`',0,'|\\','Zz','Xx','Cc','Vv','Bb','Nn','Mm','<,','>.','?/',0,
};
bool spec_read_input(CON_INPUT* ir, int n, int* r)
{
int nm = 0;
get_input_len(&nm);
n = n>nm ? nm : n;
static int caps=0;
static int l_shift=0;
static int r_shift=0;
static int ctrl=0;
if (raw_keyboard)
{
int i;
for (i=0; i<n; i++)
{
int res;
unsigned char scan;
res = read(0, &scan, 1);
if (!res)
{
break;
}
switch (scan & 0x7F)
{
case 0xe0: // escape code
scan=0;
break;
case 0x2a:
l_shift = scan<0x80;
scan=0;
break;
case 0x36:
r_shift = scan<0x80;
scan=0;
break;
case 0x1d:
// l-ctrl is 0x1d
// r-ctrl is prefixed with 0xe0
ctrl = scan<0x80;
scan=0;
break;
case 66:
// capslock
scan=0;
break;
}
if (scan)
{
ir[i].EventType = CON_INPUT_KBD;
ir[i].Event.KeyEvent.bKeyDown = (scan & 0x80)==0;
ir[i].Event.KeyEvent.uChar.AsciiChar = 0;
int c = (scan+8)&0x7f;
switch (c)
{
case 65: ir[i].Event.KeyEvent.uChar.AsciiChar = ' '; break;
case 'n': ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_HOM; break;
case 'o': ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_UP; break;
case 'p': ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_PUP; break;
case 'q': ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_LT; break;
case 'r': ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_RT; break;
case 's': ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_END; break;
case 't': ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_DN; break;
case 'u': ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_PDN; break;
case 'v': ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_INS; break;
case 'w': ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_DEL; break;
default:
{
if (c*sizeof(int)<sizeof(key_map))
{
int s = (l_shift+r_shift) > 0;
if ((key_map[c]&0xFF) >='a' && (key_map[c]&0xFF) <='z')
s ^= caps;
if (s && (key_map[c]&0xFF00))
ir[i].Event.KeyEvent.uChar.AsciiChar = (key_map[c]>>8)&0xFF;
else
ir[i].Event.KeyEvent.uChar.AsciiChar = key_map[c]&0xFF;
}
}
}
if ( ctrl &&
(ir[i].Event.KeyEvent.uChar.AsciiChar == 'c' ||
ir[i].Event.KeyEvent.uChar.AsciiChar == 'C') )
{
// do not put ^C
i--;
raise(SIGINT);
}
}
else
{
// skip
i--;
}
}
if (r)
*r=i;
return true;
}
if (!dpy)
{
for (int i=0; i<n; i++)
{
int ch = read_char();
if (ch<=0)
{
if (r)
*r = i;
return true;
}
ir[i].EventType = CON_INPUT_KBD;
ir[i].Event.KeyEvent.bKeyDown = true;
ir[i].Event.KeyEvent.uChar.AsciiChar = ch;
}
if (r)
*r = n;
return true;
}
// in non grabbed way: eat all from stdin if submitted
int discard;
ioctl( fileno(stdin), FIONREAD, &discard);
while (discard)
{
char chunk[256];
int bytes = discard > 256 ? 256 : discard;
discard -= fread(chunk,1,bytes,stdin);
}
int i=0;
for (int j=0; j<nm && i<n; j++)
{
XEvent ev;
XGenericEventCookie *cookie = &ev.xcookie;
XNextEvent(dpy, &ev);
if (use_xi)
{
if (cookie->type != GenericEvent ||
cookie->extension != xi_opcode)
continue;
if (XGetEventData(dpy, cookie))
{
if (cookie->evtype == 2 || cookie->evtype == 3)
{
XIDeviceEvent* key = (XIDeviceEvent*)cookie->data;
ir[i].EventType = CON_INPUT_KBD;
ir[i].Event.KeyEvent.bKeyDown = cookie->evtype == 2;
ir[i].Event.KeyEvent.uChar.AsciiChar = 0;
int c = key->detail;
switch (c&0x7f)
{
case 0xe0+8: // escape code
break;
case 0x2a+8:
l_shift = ir[i].Event.KeyEvent.bKeyDown;
break;
case 0x36+8:
r_shift = ir[i].Event.KeyEvent.bKeyDown;
break;
case 66:
{
//if (ir[i].Event.KeyEvent.bKeyDown)
{
unsigned int n=0;
XkbGetIndicatorState(dpy, XkbUseCoreKbd, &n);
caps = n&1;
}
break;
}
case 65: ir[i].Event.KeyEvent.uChar.AsciiChar = ' '; break;
case 110: ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_HOM; break;
case 111: ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_UP; break;
case 112: ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_PUP; break;
case 113: ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_LT; break;
case 114: ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_RT; break;
case 115: ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_END; break;
case 116: ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_DN; break;
case 117: ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_PDN; break;
case 118: ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_INS; break;
case 119: ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_DEL; break;
default:
if (c*sizeof(int)<sizeof(key_map))
{
int s = (l_shift+r_shift) > 0;
if ((key_map[c]&0xFF) >='a' && (key_map[c]&0xFF) <='z')
s ^= caps;
if (s && (key_map[c]&0xFF00))
ir[i].Event.KeyEvent.uChar.AsciiChar = (key_map[c]>>8)&0xFF;
else
ir[i].Event.KeyEvent.uChar.AsciiChar = key_map[c]&0xFF;
}
}
i++;
break;
}
XFreeEventData(dpy, &ev.xcookie);
continue;
}
// continue;
}
switch(ev.type)
{
case FocusIn:
{
unsigned int n=0;
XkbGetIndicatorState(dpy, XkbUseCoreKbd, &n);
caps = n&1;
}
break;
case FocusOut:
int revert;
XGetInputFocus(dpy, &win, &revert);
if (win == PointerRoot)
win = root;
XSelectInput(dpy, win, KeyPressMask|KeyReleaseMask|FocusChangeMask);
if (ev.xfocus.window == victim)
{
ir[i].EventType = CON_INPUT_FOC;
ir[i].Event.FocusEvent.bSetFocus = 0;
i++;
}
break;
case KeyPress:
case KeyRelease:
{
if (ev.xkey.window != victim)
break;
ir[i].EventType = CON_INPUT_KBD;
ir[i].Event.KeyEvent.bKeyDown = ev.type == KeyPress;
ir[i].Event.KeyEvent.uChar.AsciiChar = 0;
int c = ev.xkey.keycode;
switch (c&0x7f)
{
case 0xe0+8: // escape code
break;
case 0x2a+8:
l_shift = ir[i].Event.KeyEvent.bKeyDown;
break;
case 0x36+8:
r_shift = ir[i].Event.KeyEvent.bKeyDown;
break;
case 66:
{
//if (ir[i].Event.KeyEvent.bKeyDown)
{
unsigned int n=0;
XkbGetIndicatorState(dpy, XkbUseCoreKbd, &n);
caps = n&1;
}
break;
}
case 65: ir[i].Event.KeyEvent.uChar.AsciiChar = ' '; break;
case 110: ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_HOM; break;
case 111: ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_UP; break;
case 112: ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_PUP; break;
case 113: ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_LT; break;
case 114: ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_RT; break;
case 115: ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_END; break;
case 116: ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_DN; break;
case 117: ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_PDN; break;
case 118: ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_INS; break;
case 119: ir[i].Event.KeyEvent.uChar.AsciiChar = KBD_DEL; break;
default:
if (c*sizeof(int)<sizeof(key_map))
{
int s = (l_shift+r_shift) > 0;
if ((key_map[c]&0xFF) >='a' && (key_map[c]&0xFF) <='z')
s ^= caps;
if (s && (key_map[c]&0xFF00))
ir[i].Event.KeyEvent.uChar.AsciiChar = (key_map[c]>>8)&0xFF;
else
ir[i].Event.KeyEvent.uChar.AsciiChar = key_map[c]&0xFF;
}
}
i++;
break;
}
}
}
if (r)
*r = i;
return true;
}
void sleep_ms(int ms)
{
usleep(ms*1000);
}
void vsync_wait()
{
sleep_ms(8);
}
static struct termios old, cur;
static int old_keyboard_mode;
/* Initialize new terminal i/o settings */
static void initTermios()
{
tcgetattr(0, &old); /* grab old terminal i/o settings */
#ifdef LINUX
struct termios tty_attr;
int flags;
/* save old keyboard mode */
if (ioctl(0, KDGKBMODE, &old_keyboard_mode) >= 0)
{
/* make stdin non-blocking */
// originaly before (save old keyboard mode)
flags = fcntl(0, F_GETFL);
flags |= O_NONBLOCK;
fcntl(0, F_SETFL, flags);
/* turn off buffering, echo and key processing */
tty_attr = old;
tty_attr.c_lflag &= ~(ICANON | ECHO | ISIG);
tty_attr.c_iflag &= ~(ISTRIP | INLCR | ICRNL | IGNCR | IXON | IXOFF);
tcsetattr(0, TCSANOW, &tty_attr);
ioctl(0, KDSKBMODE, K_MEDIUMRAW);
raw_keyboard = true;
}
else
#endif
{
raw_keyboard = false;
setbuf(stdin, NULL);
cur = old;
cur.c_lflag |= ISIG;
cur.c_iflag &= ~IGNBRK;
cur.c_iflag |= BRKINT;
cur.c_lflag &= ~ICANON; /* disable buffered i/o */
cur.c_lflag &= ~ECHO; /* disable echo mode */
tcsetattr(0, TCSANOW, &cur);
}
}
/* Restore old terminal i/o settings */
static void resetTermios(void)
{
tcsetattr(0, TCSAFLUSH, &old);
#ifndef __APPLE__
if (raw_keyboard)
ioctl(0, KDSKBMODE, old_keyboard_mode);
#endif
}
static int ansi_key_autorep(bool on)
{
if (on)
return printf("\x1B[?8h");
else
return printf("\x1B[?8l");
}
static int ansi_set_wrap(bool on)
{
if (on)
return printf("\x1B[?7h");
else
return printf("\x1B[?7l");
}
static int ansi_show_cursor(bool show)
{
if (show)
return printf("\x1B[?25h");
else
return printf("\x1B[?25l");
}
static int ansi_goto_xy(int x, int y)
{
return printf("\x1B[%d;%df",y+1,x+1);
}
static int ansi_set_color(int fg, int bg)
{
// 8 color pal
return printf("\x1B[%d;%dm",fg%8+30,bg%8+40);
}
static int ansi_set_color_ex(int fg, int bg)
{
// LINUX text console
if (use_xterm_256)
return printf("\x1B[38;5;%d;48;5;%dm",fg%256,bg%256);
// bold & blink
return printf("\x1B[%d;%d;%s;%sm",fg%8+30,bg%8+40,fg<8?"22":"1",bg<8?"25":"5");
}
static int ansi_set_color_rgb(int fg, int bg)
{
// RGB color
return printf("\x1B[38;2;%d;%d;%d;48;2;%d;%d;%d;m",
fg&0xff,(fg>>8)&0xff,(fg>>16)&0xff,
bg&0xff,(bg>>8)&0xff,(bg>>16)&0xff);
}
static int ansi_def_color()
{
return printf("\x1B[0m");
}
static int ansi_screen(bool alt)
{
if (alt)
return printf("\x1B[?1049h");
else
return printf("\x1B[?1049l");
}
static void get_wh(int* w, int* h)
{
winsize ws;
ioctl(0, TIOCGWINSZ, &ws);
if (w)
*w = ws.ws_col;
if (h)
*h = ws.ws_row;
}
void free_sound();
void terminal_done()
{
if (dpy)
{
XCloseDisplay(dpy);
dpy = 0;
}
resetTermios();
ansi_def_color();
ansi_show_cursor(true);
ansi_set_wrap(true);
ansi_key_autorep(true);
ansi_screen(false);
fflush(stdout);
// most likely will fail, so put it at the end.
free_sound();
}
static void terminal_signal(int signum)
{
terminal_done();
_exit(128 + signum);
}
static void select_events(Display *dpy, Window win)
{
XIEventMask evmasks[2];
unsigned char mask1[(XI_LASTEVENT + 7)/8];
unsigned char mask2[(XI_LASTEVENT + 7)/8];
memset(mask1, 0, sizeof(mask1));
/* select for button and key events from all master devices */
// XISetMask(mask1, XI_ButtonPress);
// XISetMask(mask1, XI_ButtonRelease);
XISetMask(mask1, XI_KeyPress);
XISetMask(mask1, XI_KeyRelease);
evmasks[0].deviceid = XIAllMasterDevices;
evmasks[0].mask_len = sizeof(mask1);
evmasks[0].mask = mask1;
memset(mask2, 0, sizeof(mask2));
/* Select for motion from the default cursor */
XISetMask(mask2, XI_Motion);
evmasks[1].deviceid = 2; /* the default cursor */
evmasks[1].mask_len = sizeof(mask2);
evmasks[1].mask = mask2;
XISelectEvents(dpy, win, evmasks, 2);
XFlush(dpy);
}
int terminal_init(int argc, char* argv[], int* dw, int* dh)
{
// todo:
// try to setup ansi palette
// try to enable mouse events (enough to track moves when left button is down only)
const char* term = getenv("TERM");
if (strcmp(term,"linux")==0)
use_xterm_256 = false;
initTermios();
LoadConf();
struct sigaction act;
sigemptyset(&act.sa_mask);
act.sa_handler = terminal_signal;
act.sa_flags = 0;
sigaction(SIGHUP, &act, NULL);
sigaction(SIGINT, &act, NULL);
sigaction(SIGQUIT, &act, NULL);
sigaction(SIGTERM, &act, NULL);
sigaction(SIGXCPU, &act, NULL);
sigaction(SIGXFSZ, &act, NULL);
sigaction(SIGIO, &act, NULL);
sigaction(SIGPIPE, &act, NULL);
sigaction(SIGALRM, &act, NULL);
int cols,rows;
get_wh(&cols,&rows);
printf("W=%d, H=%d\n",cols,rows);
if (raw_keyboard)
{
printf("Using RAW Keyboard... excelent!\n");
}
else
{
printf("Falling back to X11 input\n");
dpy = XOpenDisplay(NULL);
if (!dpy)
{
printf("unable to connect to X11 display\nfalling back to sticky stdin!\n");
}
else
{
Bool supported = false;
XkbSetDetectableAutoRepeat(dpy, True, &supported);
int revert;
XGetInputFocus(dpy, &win, &revert);
if (win == PointerRoot)
win = root;
if (win)
{
int event, error;
if (XQueryExtension(dpy, "XInputExtension", &xi_opcode,&event,&error))
{
select_events(dpy,win);
use_xi = true;
}
else
{
printf("connecting input to window = %lu\n",win);
victim = win;
root = DefaultRootWindow(dpy);
XSelectInput(dpy, win, FocusChangeMask|KeyPressMask|KeyReleaseMask);
/*
XGrabKeyboard(dpy, win,
False,
GrabModeAsync,
GrabModeAsync,
CurrentTime);
*/
XFlush(dpy);
}
}
else
{
XCloseDisplay(dpy);
dpy = 0;
printf("unable to get x11 active window\nfalling back to sticky stdin!\n");
}
}
}
ansi_screen(true);
ansi_set_wrap(false);
ansi_show_cursor(false);
ansi_key_autorep(false);
*dw=cols;
*dh=rows;
return 0;
}
void get_terminal_wh(int* dw, int* dh)
{
get_wh(dw,dh);
}
void free_con_output(CON_OUTPUT* screen)
{
char* arr = (char*)screen->arr;
if (arr)
delete [] arr;
screen->arr = 0;
}
int screen_write(CON_OUTPUT* screen, int dw, int dh, int sx, int sy, int sw, int sh)
{
int w = screen->w;
int h = screen->h;
char* buf = screen->buf;
char* color = screen->color;
char* arr = (char*)screen->arr;
ansi_goto_xy(0,0);
if (!color)
ansi_set_color_ex(0,15);
else
{
for (int y=0; y<sh; y++)
color[(w+1)*y+w] = color[(w+1)*y+w-1];
if (arr)
for (int y=0; y<sh; y++)
arr[(w+1)*(y+h)+w] = color[(w+1)*y+w];
}
int size = 0;
if (arr)
{
// compare line by line (winner)
char cr[51];
memset(cr,'\n',50);
cr[50]=0;
int i=0;
int j=0;
while (i<sh)
{
while (i<sh &&
memcmp(buf+(w+1)*i,arr+(w+1)*i,w+1)==0 &&
(!color || memcmp(color+(w+1)*i,arr+(w+1)*(i+h),w+1)==0))
i++;
if (i>=sh)
break;
if (i>j)
size += printf("%s",cr + (50 - (i-j)) - (j>0));
memcpy(arr+(w+1)*i,buf+(w+1)*i,w+1);
if (color)
memcpy(arr+(w+1)*(i+h),color+(w+1)*i,w+1);
j=i+1;
while (j<sh)
{
if (memcmp(buf+(w+1)*j,arr+(w+1)*j,w+1)==0 &&
(!color|| memcmp(color+(w+1)*j,arr+(w+1)*(j+h),w+1)==0))
{
break;
}
else
{
memcpy(arr+(w+1)*j,buf+(w+1)*j,w+1);
if (color)
memcpy(arr+(w+1)*(j+h),color+(w+1)*j,w+1);
}
j++;
}
// clip
if (!color)
{
char swap = buf[(w+1)*j-1];
buf[(w+1)*j-1] = 0;
size += printf("%s",buf+(w+1)*i);
// unclip
buf[(w+1)*j-1] = swap;