-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.c
2136 lines (1800 loc) · 48.1 KB
/
main.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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <time.h>
#include <sys/mman.h>
#include <sys/epoll.h>
#include <sys/timerfd.h>
#include <fcntl.h>
#include <unistd.h>
#include <pty.h>
#include <xkbcommon/xkbcommon-compose.h>
#include <wayland-client-core.h>
#include <wayland-client-protocol.h>
#include <wayland-cursor.h>
#include "tsm/libtsm.h"
#include "xdg-shell.h"
#include "primary-selection-unstable-v1.h"
#include "xdg-decoration-unstable-v1.h"
#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
int font_init(int, char *, int *, int *);
void font_deinit(void);
unsigned char *get_glyph(uint32_t, uint32_t, int);
static void noop()
{
}
enum deco {
DECO_AUTO,
DECO_SERVER,
DECO_NONE
};
static struct {
bool die;
bool configured;
bool need_redraw;
bool can_redraw;
int resize;
int fd, master_fd;
struct wl_display *display;
struct wl_registry *registry;
struct wl_compositor *cp;
struct wl_shm *shm;
bool shm_argb;
struct xdg_wm_base *wm_base;
struct wl_seat *seat;
struct wl_surface *surf;
struct xdg_surface *xdgsurf;
struct xdg_toplevel *toplvl;
struct buffer {
struct wl_buffer *b;
void *data;
int size;
bool busy;
tsm_age_t age;
} buf[2];
struct wl_callback *cb;
int col, row;
int cwidth, cheight;
int width, height;
int confwidth, confheight;
struct {
int top, left;
} margin;
struct tsm_screen *screen;
struct tsm_vte *vte;
unsigned int mods;
struct xkb_context *xkb_ctx;
struct xkb_state *xkb_state;
struct xkb_keymap *xkb_keymap;
struct xkb_compose_table *xkb_compose_table;
struct xkb_compose_state *xkb_compose_state;
xkb_mod_index_t xkb_alt;
xkb_mod_index_t xkb_ctrl;
xkb_mod_index_t xkb_shift;
struct wl_keyboard *kbd;
struct wl_pointer *ptr;
wl_fixed_t ptr_x, ptr_y;
int select;
struct {
struct wl_cursor_theme *theme;
struct wl_cursor *text;
struct wl_cursor *current;
long long anim_start;
struct wl_surface *surface;
struct wl_callback *callback;
uint32_t enter_serial;
} cursor;
struct {
int fd;
uint32_t key;
xkb_keysym_t sym;
uint32_t unicode;
void (*action)(void);
struct itimerspec its;
} repeat;
struct wl_data_device_manager *d_dm;
struct wl_data_device *d_d;
struct zwp_primary_selection_device_manager_v1 *ps_dm;
struct zwp_primary_selection_device_v1 *ps_d;
struct {
struct wl_data_source *source;
char *data;
} d_copy;
struct {
struct zwp_primary_selection_source_v1 *source;
char *data;
} ps_copy;
struct {
struct wl_data_offer *d_offer;
struct zwp_primary_selection_offer_v1 *ps_offer;
char *d_mime;
char *ps_mime;
int fd[2];
char buf[200];
size_t len;
bool active;
} paste;
struct {
struct zxdg_decoration_manager_v1 *manager;
struct zxdg_toplevel_decoration_v1 *deco;
} deco;
struct {
bool linger;
char *config;
char *display;
char *app_id;
} opt;
struct binding {
struct binding *next;
unsigned int mods;
xkb_keysym_t sym;
void (*action)(void);
} *binding;
struct {
char shell[32];
int col, row;
int scrollback;
bool scroll_to_bottom_on_input;
bool margin;
unsigned char opacity;
enum deco decorations;
int font_size;
char font_path[512];
uint8_t colors[TSM_COLOR_NUM][3];
} cfg;
} term = {
.cfg.shell = "/bin/sh",
.cfg.col = 80,
.cfg.row = 24,
.cfg.scrollback = 0,
.cfg.scroll_to_bottom_on_input = false,
.cfg.margin = false,
.cfg.opacity = 0xff,
.cfg.decorations = DECO_AUTO,
.cfg.font_size = 18,
.cfg.font_path = "",
.cfg.colors = {
[TSM_COLOR_BLACK] = { 0, 0, 0 },
[TSM_COLOR_RED] = { 205, 0, 0 },
[TSM_COLOR_GREEN] = { 0, 205, 0 },
[TSM_COLOR_YELLOW] = { 205, 205, 0 },
[TSM_COLOR_BLUE] = { 0, 0, 238 },
[TSM_COLOR_MAGENTA] = { 205, 0, 205 },
[TSM_COLOR_CYAN] = { 0, 205, 205 },
[TSM_COLOR_LIGHT_GREY] = { 229, 229, 229 },
[TSM_COLOR_DARK_GREY] = { 127, 127, 127 },
[TSM_COLOR_LIGHT_RED] = { 255, 0, 0 },
[TSM_COLOR_LIGHT_GREEN] = { 0, 255, 0 },
[TSM_COLOR_LIGHT_YELLOW] = { 255, 255, 0 },
[TSM_COLOR_LIGHT_BLUE] = { 92, 92, 255 },
[TSM_COLOR_LIGHT_MAGENTA] = { 255, 0, 255 },
[TSM_COLOR_LIGHT_CYAN] = { 0, 255, 255 },
[TSM_COLOR_WHITE] = { 255, 255, 255 },
[TSM_COLOR_FOREGROUND] = { 229, 229, 229 },
[TSM_COLOR_BACKGROUND] = { 0, 0, 0 },
},
.opt.app_id = "havoc"
};
static void wcb(struct tsm_vte *vte, const char *u8, size_t len, void *data)
{
assert(len <= PIPE_BUF);
if (term.master_fd >= 0 && write(term.master_fd, u8, len) < 0) {
fprintf(stderr, "could not write to pty master: %m\n");
abort();
}
}
static void handle_display(int ev)
{
if (ev & EPOLLHUP) {
term.die = true;
} else if (ev & EPOLLIN) {
if (wl_display_dispatch(term.display) < 0) {
fprintf(stderr, "could not dispatch events: %m\n");
abort();
}
}
}
static void handle_tty(int ev)
{
char data[256];
int len;
if (ev & EPOLLIN) {
term.need_redraw = true;
len = read(term.master_fd, data, sizeof(data));
assert(len);
if (len < 0) {
fprintf(stderr, "could not read from pty: %m\n");
abort();
} else {
tsm_vte_input(term.vte, data, len);
}
} else if (ev & EPOLLHUP) {
epoll_ctl(term.fd, EPOLL_CTL_DEL, term.master_fd, NULL);
close(term.master_fd);
term.master_fd = -1;
if (!term.opt.linger)
term.die = true;
}
}
static void handle_repeat(int ev)
{
uint64_t exp;
if (read(term.repeat.fd, &exp, sizeof exp) < 0)
return;
if (term.repeat.action) {
term.repeat.action();
return;
}
tsm_vte_handle_keyboard(term.vte, term.repeat.sym, XKB_KEY_NoSymbol,
term.mods, term.repeat.unicode);
}
static long long now(void)
{
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
return (long long)t.tv_sec * 1000 + t.tv_nsec / 1000000;
}
static void cursor_draw(int frame)
{
struct wl_buffer *buffer;
struct wl_cursor_image *image;
if ((int)term.cursor.current->image_count <= frame) {
fprintf(stderr, "cursor frame index out of range\n");
return;
}
image = term.cursor.current->images[frame];
buffer = wl_cursor_image_get_buffer(image);
wl_surface_attach(term.cursor.surface, buffer, 0, 0);
wl_surface_damage(term.cursor.surface, 0, 0,
image->width, image->height);
wl_surface_commit(term.cursor.surface);
wl_pointer_set_cursor(term.ptr, term.cursor.enter_serial,
term.cursor.surface,
image->hotspot_x, image->hotspot_y);
}
static void cursor_request_frame_callback(void);
static void cursor_frame_callback(void *data, struct wl_callback *cb,
uint32_t time)
{
int frame = wl_cursor_frame(term.cursor.current,
now() - term.cursor.anim_start);
assert(cb == term.cursor.callback);
wl_callback_destroy(term.cursor.callback);
cursor_request_frame_callback();
cursor_draw(frame);
}
static const struct wl_callback_listener cursor_frame_listener = {
cursor_frame_callback
};
static void cursor_request_frame_callback(void)
{
term.cursor.callback = wl_surface_frame(term.cursor.surface);
wl_callback_add_listener(term.cursor.callback, &cursor_frame_listener,
NULL);
}
static void cursor_unset(void)
{
if (term.cursor.callback) {
wl_callback_destroy(term.cursor.callback);
term.cursor.callback = NULL;
}
term.cursor.current = NULL;
}
static void cursor_set(struct wl_cursor *cursor)
{
uint32_t duration;
int frame;
cursor_unset();
if (term.ptr == NULL)
return;
if (cursor == NULL)
goto hide;
term.cursor.current = cursor;
frame = wl_cursor_frame_and_duration(term.cursor.current, 0, &duration);
if (duration) {
term.cursor.anim_start = now();
cursor_request_frame_callback();
}
cursor_draw(frame);
return;
hide:
wl_pointer_set_cursor(term.ptr, term.cursor.enter_serial, NULL, 0, 0);
}
static void cursor_init(void)
{
int size = 32;
char *size_str = getenv("XCURSOR_SIZE");
struct wl_cursor *text = NULL;
if (size_str && *size_str) {
char *end;
long s;
errno = 0;
s = strtol(size_str, &end, 10);
if (errno == 0 && *end == '\0' && s > 0)
size = s;
}
term.cursor.theme = wl_cursor_theme_load(getenv("XCURSOR_THEME"), size,
term.shm);
if (term.cursor.theme == NULL)
return;
text = wl_cursor_theme_get_cursor(term.cursor.theme, "text");
if (text == NULL)
text = wl_cursor_theme_get_cursor(term.cursor.theme, "ibeam");
if (text == NULL)
text = wl_cursor_theme_get_cursor(term.cursor.theme, "xterm");
term.cursor.surface = wl_compositor_create_surface(term.cp);
if (term.cursor.surface == NULL) {
wl_cursor_theme_destroy(term.cursor.theme);
term.cursor.theme = NULL;
return;
}
term.cursor.text = text;
}
static void cursor_free(void)
{
if (term.cursor.callback)
wl_callback_destroy(term.cursor.callback);
if (term.cursor.surface)
wl_surface_destroy(term.cursor.surface);
if (term.cursor.theme)
wl_cursor_theme_destroy(term.cursor.theme);
}
#define REPLACEMENT_CHAR 0x0000fffd
static int utf8_to_utf32(char const **utf8, size_t *len, uint32_t *r)
{
static unsigned char const tail_len[128] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0
};
unsigned char tail, c = *(*utf8)++;
--(*len);
if (c < 0x80) {
*r = c;
return 0;
}
tail = tail_len[c - 0x80];
if (!tail) {
/* middle of a character or tail too long */
*r = REPLACEMENT_CHAR;
return 0;
} else if (tail > *len) {
/* need more input for a complete character */
++(*len), --(*utf8);
return 1;
}
/* remove length specification bits */
*r = c & 0x3f >> tail;
while (tail--) {
c = *(*utf8)++;
--(*len);
if ((c & 0xc0) != 0x80) {
*r = REPLACEMENT_CHAR;
++(*len), --(*utf8);
return 0;
}
*r = (*r << 6) + (c & 0x3f);
}
return 0;
}
static void end_paste(void)
{
tsm_vte_paste_end(term.vte);
epoll_ctl(term.fd, EPOLL_CTL_DEL, term.paste.fd[0], NULL);
close(term.paste.fd[0]);
term.paste.len = 0;
term.paste.active = false;
}
static void handle_paste(int ev)
{
if (ev & EPOLLIN) {
uint32_t code;
ssize_t len;
char const *p = &term.paste.buf[0];
len = read(term.paste.fd[0],
term.paste.buf + term.paste.len,
sizeof(term.paste.buf) - term.paste.len);
if (len <= 0) {
end_paste();
return;
}
if (term.cfg.scroll_to_bottom_on_input)
tsm_screen_sb_reset(term.screen);
term.need_redraw = true;
term.paste.len += len;
while (term.paste.len > 0) {
if (utf8_to_utf32(&p, &term.paste.len, &code)) {
memcpy(&term.paste.buf, p, term.paste.len);
break;
}
tsm_vte_handle_keyboard(term.vte, XKB_KEY_NoSymbol,
XKB_KEY_NoSymbol, 0, code);
}
} else if (ev & EPOLLHUP) {
end_paste();
}
}
static struct epcb {
void (*f)(int);
} dfp = { handle_display }
, tfp = { handle_tty }
, rfp = { handle_repeat }
, pfp = { handle_paste };
static void buffer_release(void *data, struct wl_buffer *b)
{
struct buffer *buffer = data;
buffer->busy = false;
}
static const struct wl_buffer_listener buffer_listener = {
buffer_release
};
static int buffer_init(struct buffer *buf)
{
struct wl_shm_pool *pool;
char shm_name[14];
int fd, stride;
int max = 100;
assert(!buf->busy);
stride = term.width * 4;
buf->size = stride * term.height;
srand(time(NULL));
do {
sprintf(shm_name, "/havoc-%d", rand() % 1000000);
fd = shm_open(shm_name, O_RDWR | O_CREAT | O_EXCL, 0600);
} while (fd < 0 && errno == EEXIST && --max);
if (fd < 0) {
fprintf(stderr, "shm_open failed: %m\n");
return -1;
}
shm_unlink(shm_name);
if (ftruncate(fd, buf->size) < 0) {
fprintf(stderr, "ftruncate failed: %m\n");
close(fd);
return -1;
}
buf->data = mmap(NULL, buf->size, PROT_READ | PROT_WRITE, MAP_SHARED,
fd, 0);
if (buf->data == MAP_FAILED) {
fprintf(stderr, "mmap failed: %m\n");
close(fd);
return -1;
}
pool = wl_shm_create_pool(term.shm, fd, buf->size);
buf->b = wl_shm_pool_create_buffer(pool, 0, term.width, term.height,
stride, WL_SHM_FORMAT_ARGB8888);
wl_buffer_add_listener(buf->b, &buffer_listener, buf);
wl_shm_pool_destroy(pool);
close(fd);
buf->age = 0;
return 0;
}
static void buffer_unmap(struct buffer *buf)
{
if (buf->b) {
wl_buffer_destroy(buf->b);
munmap(buf->data, buf->size);
}
}
static struct buffer *swap_buffers(void)
{
struct buffer *buf;
assert(term.configured);
if (!term.buf[0].busy) {
buf = &term.buf[0];
} else if (!term.buf[1].busy) {
buf = &term.buf[1];
} else {
fprintf(stderr, "both surface content buffers are busy\n");
return NULL;
}
if (term.resize) {
buffer_unmap(buf);
if (buffer_init(buf) < 0)
abort();
}
return buf;
}
#define mul(a, b) (((a) * (b) + 255) >> 8)
#define join(a, r, g, b) ((a) << 24 | (r) << 16 | (g) << 8 | (b))
typedef uint_fast8_t uf8;
static void blank(uint32_t *dst, int w, uf8 br, uf8 bg, uf8 bb, uf8 ba)
{
int i;
uint32_t b;
int h = term.cheight;
b = join(ba, mul(br, ba), mul(bg, ba), mul(bb, ba));
w *= term.cwidth;
while (h--) {
for (i = 0; i < w; ++i)
dst[i] = b;
dst += term.width;
}
}
static void print(uint32_t *dst, int w,
uf8 br, uf8 bg, uf8 bb,
uf8 fr, uf8 fg, uf8 fb,
uf8 ba, unsigned char *glyph)
{
int i;
int h = term.cheight;
w *= term.cwidth;
br = mul(br, ba);
bg = mul(bg, ba);
bb = mul(bb, ba);
while (h--) {
for (i = 0; i < w; ++i) {
uf8 fa = glyph[i];
if (fa == 0) {
dst[i] = join(ba, br, bg, bb);
} else if (fa == 0xff) {
dst[i] = join(0xff, fr, fg, fb);
} else {
uf8 ca = 255 - fa;
dst[i] = join(fa + mul(ba, ca),
mul(fr, fa) + mul(br, ca),
mul(fg, fa) + mul(bg, ca),
mul(fb, fa) + mul(bb, ca));
}
}
glyph += w;
dst += term.width;
}
}
static int draw_cell(struct tsm_screen *tsm, uint32_t id, const uint32_t *ch,
size_t len, int char_width, int x, int y,
const struct tsm_screen_attr *a, tsm_age_t age,
void *data)
{
struct buffer *buffer = data;
uint32_t *dst = buffer->data;
if (age && age <= buffer->age)
return 0;
dst += term.margin.top * term.width + term.margin.left;
dst = &dst[y * term.cheight * term.width + x * term.cwidth];
if (len == 0) {
if (a->inverse)
blank(dst, char_width,
~a->br, ~a->bg, ~a->bb, term.cfg.opacity);
else
blank(dst, char_width,
a->br, a->bg, a->bb, term.cfg.opacity);
} else {
/* todo, combining marks */
unsigned char *g = get_glyph(id, ch[0], char_width);
if (a->inverse)
print(dst, char_width,
~a->br, ~a->bg, ~a->bb,
~a->fr, ~a->fg, ~a->fb,
term.cfg.opacity, g);
else
print(dst, char_width,
a->br, a->bg, a->bb,
a->fr, a->fg, a->fb,
term.cfg.opacity, g);
}
return 0;
}
static void draw_margin(struct buffer *buffer)
{
uint32_t *dst = buffer->data;
uint8_t a = term.cfg.opacity;
uint8_t *rgb = term.cfg.colors[TSM_COLOR_BACKGROUND];
uint32_t c = join(a, mul(rgb[0], a), mul(rgb[1], a), mul(rgb[2], a));
int inw = term.col * term.cwidth;
int inh = term.row * term.cheight;
int i, j;
for (i = 0; i < term.width * term.margin.top; ++i)
dst[i] = c;
for (i = (term.margin.top + inh) * term.width;
i < term.height * term.width;
++i)
dst[i] = c;
for (i = term.margin.top; i < term.margin.top + inh; ++i) {
for (j = 0; j < term.margin.left; ++j)
dst[i * term.width + j] = c;
for (j = term.margin.left + inw; j < term.width; ++j)
dst[i * term.width + j] = c;
}
}
static void frame_callback(void *data, struct wl_callback *cb, uint32_t time)
{
assert(term.cb == cb);
wl_callback_destroy(cb);
term.cb = NULL;
term.can_redraw = true;
}
static const struct wl_callback_listener frame_listener = {
frame_callback
};
static void redraw(void)
{
struct buffer *buffer = swap_buffers();
if (buffer == NULL) {
fprintf(stderr, "no buffer available, cannot redraw\n");
return;
}
wl_surface_attach(term.surf, buffer->b, 0, 0);
buffer->age = tsm_screen_draw(term.screen, draw_cell, buffer);
if (buffer->age == 0)
term.buf[0].age = term.buf[1].age = 0;
wl_surface_damage(term.surf, 0, 0, term.width, term.height);
term.cb = wl_surface_frame(term.surf);
wl_callback_add_listener(term.cb, &frame_listener, NULL);
wl_surface_commit(term.surf);
buffer->busy = true;
term.can_redraw = false;
term.need_redraw = false;
if (term.resize) {
--term.resize;
if (term.cfg.margin)
draw_margin(buffer);
}
}
static void paste(bool primary)
{
struct epoll_event ee;
if (primary) {
if (!term.ps_dm || !term.paste.ps_mime)
return;
} else {
if (!term.d_dm || !term.paste.d_mime)
return;
}
if (term.paste.active)
end_paste();
if (pipe(term.paste.fd) < 0)
return;
if (primary) {
zwp_primary_selection_offer_v1_receive(term.paste.ps_offer,
term.paste.ps_mime,
term.paste.fd[1]);
} else {
wl_data_offer_receive(term.paste.d_offer, term.paste.d_mime,
term.paste.fd[1]);
}
close(term.paste.fd[1]);
ee.events = EPOLLIN;
ee.data.ptr = &pfp;
epoll_ctl(term.fd, EPOLL_CTL_ADD, term.paste.fd[0], &ee);
term.paste.active = true;
tsm_vte_paste_begin(term.vte);
}
static void action_paste(void)
{
paste(false);
}
static void ds_target(void *d, struct wl_data_source *ds, const char *mt)
{
}
static void ds_send(void *data, struct wl_data_source *ds,
const char *mime_type, int32_t fd)
{
write(fd, term.d_copy.data, strlen(term.d_copy.data));
close(fd);
}
static void ds_cancelled(void *data, struct wl_data_source *source)
{
wl_data_source_destroy(term.d_copy.source);
term.d_copy.source = NULL;
free(term.d_copy.data);
}
static void ds_dnd_drop_performed(void *data, struct wl_data_source *ds)
{
}
static void ds_dnd_finished(void *data, struct wl_data_source *ds)
{
}
static void ds_action(void *data, struct wl_data_source *ds, uint32_t a)
{
}
static struct wl_data_source_listener ds_listener = {
ds_target,
ds_send,
ds_cancelled,
ds_dnd_drop_performed,
ds_dnd_finished,
ds_action
};
static void d_copy(uint32_t serial)
{
if (!term.d_dm)
return;
if (tsm_screen_selection_copy(term.screen, &term.d_copy.data) < 0)
return;
term.d_copy.source =
wl_data_device_manager_create_data_source(term.d_dm);
wl_data_source_offer(term.d_copy.source, "UTF8_STRING");
wl_data_source_offer(term.d_copy.source, "text/plain");
wl_data_source_add_listener(term.d_copy.source, &ds_listener, NULL);
wl_data_device_set_selection(term.d_d, term.d_copy.source, serial);
}
static void d_uncopy(void)
{
if (!term.d_dm)
return;
if (!term.d_copy.source)
return;
ds_cancelled(NULL, term.d_copy.source);
}
static uint32_t action_copy_serial;
static void action_copy(void)
{
d_uncopy();
if (term.select == 3)
d_copy(action_copy_serial);
}
static void reset_repeat(void)
{
struct itimerspec its = {
{ 0, 0 }, { 0, 0 }
};
timerfd_settime(term.repeat.fd, 0, &its, NULL);
}
static void setup_compose(void)
{
struct xkb_compose_table *compose_table;
struct xkb_compose_state *compose_state;
char *lang = getenv("LANG");
if (lang == NULL)
return;
compose_table =
xkb_compose_table_new_from_locale(term.xkb_ctx,
lang,
XKB_COMPOSE_COMPILE_NO_FLAGS);
if (!compose_table) {
fprintf(stderr, "could not create XKB compose table "
"for locale '%s'.\n", lang);
return;
}
compose_state = xkb_compose_state_new(compose_table,
XKB_COMPOSE_STATE_NO_FLAGS);
if (!compose_state) {
fprintf(stderr, "could not create XKB compose state. "
"Disabling compose.\n");
xkb_compose_table_unref(compose_table);
return;
}
xkb_compose_table_unref(term.xkb_compose_table);
xkb_compose_state_unref(term.xkb_compose_state);
term.xkb_compose_table = compose_table;
term.xkb_compose_state = compose_state;
}
static void kbd_keymap(void *data, struct wl_keyboard *k, uint32_t fmt,
int32_t fd, uint32_t size)
{
struct xkb_keymap *keymap;
struct xkb_state *state;
char *map;
if (fmt != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
close(fd);
return;
}
map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
if (map == MAP_FAILED) {
close(fd);
return;
}
keymap = xkb_keymap_new_from_string(term.xkb_ctx, map,
XKB_KEYMAP_FORMAT_TEXT_V1,
XKB_KEYMAP_COMPILE_NO_FLAGS);
munmap(map, size);
close(fd);
if (!keymap) {
fprintf(stderr, "failed to compile keymap\n");
return;
}
state = xkb_state_new(keymap);
if (!state) {
fprintf(stderr, "failed to create XKB state\n");
xkb_keymap_unref(keymap);
return;
}
xkb_keymap_unref(term.xkb_keymap);
xkb_state_unref(term.xkb_state);
term.xkb_keymap = keymap;
term.xkb_state = state;
setup_compose();
term.xkb_ctrl = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_CTRL);
term.xkb_alt = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_ALT);
term.xkb_shift = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_SHIFT);
}
static void kbd_enter(void *data, struct wl_keyboard *k, uint32_t serial,
struct wl_surface *surf, struct wl_array *keys)
{
}
static void kbd_leave(void *data, struct wl_keyboard *k, uint32_t serial,
struct wl_surface *surf)
{
reset_repeat();
}
static xkb_keysym_t compose(xkb_keysym_t sym)
{
if (!term.xkb_compose_state)
return sym;
if (sym == XKB_KEY_NoSymbol)
return sym;
if (xkb_compose_state_feed(term.xkb_compose_state,
sym) != XKB_COMPOSE_FEED_ACCEPTED)
return sym;
switch (xkb_compose_state_get_status(term.xkb_compose_state)) {
case XKB_COMPOSE_COMPOSED:
return xkb_compose_state_get_one_sym(term.xkb_compose_state);
case XKB_COMPOSE_COMPOSING:
case XKB_COMPOSE_CANCELLED:
return XKB_KEY_NoSymbol;
case XKB_COMPOSE_NOTHING:
default: