-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvex.c
509 lines (432 loc) · 13.9 KB
/
vex.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
#define _XOPEN_SOURCE 600
#include <ctype.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <pty.h>
#include <X11/Xft/Xft.h>
#include <vterm.h>
#include "unicode.h"
/* Launching /bin/sh may launch a GNU Bash and that can have nasty side
* effects. On my system, it clobbers ~/.bash_history because it doesn't
* respect $HISTSIZE from my ~/.bashrc. That's very annoying. So, launch
* /bin/dash which does nothing of the sort. */
#define SHELL "/bin/sh"
struct PTY
{
int master, slave;
};
struct X11
{
int fd;
Display *dpy;
int screen;
Window root;
Window termwin;
GC termgc;
unsigned long col_fg, col_bg;
int w, h;
XftFont* font;
XftDraw* fdraw;
int font_width, font_height;
XftColor fcol_fg, fcol_bg;
int buf_w, buf_h;
};
struct X11 x11;
struct PTY pty;
VTerm *vt;
VTermScreen *vts;
VTermState *vtstate;
bool
term_set_size(struct PTY *pty, struct X11 *x11)
{
struct winsize ws = {
.ws_col = x11->buf_w,
.ws_row = x11->buf_h,
};
/* This is the very same ioctl that normal programs use to query the
* window size. Normal programs are actually able to do this, too,
* but it makes little sense: Setting the size has no effect on the
* PTY driver in the kernel (it just keeps a record of it) or the
* terminal emulator. IIUC, all that's happening is that subsequent
* ioctls will report the new size -- until another ioctl sets a new
* size.
*
* I didn't see any response to ioctls of normal programs in any of
* the popular terminals (XTerm, VTE, st). They are not informed by
* the kernel when a normal program issues an ioctl like that.
*
* On the other hand, if we were to issue this ioctl during runtime
* and the size actually changed, child programs would get a
* SIGWINCH. */
if (ioctl(pty->master, TIOCSWINSZ, &ws) == -1)
{
perror("ioctl(TIOCSWINSZ)");
return false;
}
return true;
}
bool
pt_pair(struct PTY *pty)
{
int err = openpty(&(pty->master), &(pty->slave), NULL, NULL, NULL);
if (err == -1) {
return false;
}
return true;
}
void
vt_output_callback(const char* s, size_t len, void *user)
{
(void)user;
for (size_t i = 0; i < len; i++)
write(pty.master, &s[i], 1);
}
int
x11_get_selection(struct X11* x11, char** s){
Atom PRIMARY = XInternAtom(x11->dpy, "PRIMARY", 0);
Atom XSEL_DATA = XInternAtom(x11->dpy, "XSEL_DATA", 0);
Atom UTF8_STRING = XInternAtom(x11->dpy, "UTF8_STRING", True);
XEvent event;
Atom target;
int format;
unsigned long N, size;
char *data;
XConvertSelection(x11->dpy, PRIMARY, UTF8_STRING, XSEL_DATA, x11->termwin, CurrentTime);
XSync(x11->dpy, False);
XNextEvent(x11->dpy, &event);
if (event.type != SelectionNotify) {
printf("Incorrect event received!");
return 0;
}
if (event.xselection.selection != PRIMARY) {
printf("Wrong type of selection received!");
return 0;
}
if (!event.xselection.property) {
printf("Selection property not received!");
return 0;
}
XGetWindowProperty(x11->dpy, x11->termwin, event.xselection.property,
0L, (~0L), 0, AnyPropertyType, &target,
&format, &size, &N, (unsigned char**) &data);
if (target != UTF8_STRING) {
printf("Selection target incorrect!");
return 0;
}
*s = (char *)malloc(size+1);
memcpy(*s, data, size);
(*s)[size] = '\0';
XFree(data);
XDeleteProperty(x11->dpy, x11->termwin, event.xselection.property);
return size;
}
void
x11_button(XButtonEvent *ev)
{
// if middle click - paste
if (ev->button == Button2) {
char *data;
int size = x11_get_selection(&x11, &data);
if (size == 0) {
printf("No data found when trying to paste.\n");
return;
}
// now we have the clipboard content in utf-8 format
// we need to convert this into ucs-4/utf-32 to feed into vterm
// This circular methodology is ugly but unavoidable:
// 1. We cannnot exfil data from X11 in any other format than utf-8. Apart from ASCII, utf-8 is the only one supported. This kinda makes sense from a standardization/space-saving sense.
// 2. We need to interface with the libvterm lib using ucs-4 aka utf32. So, we need to do this conversion.
// 3. libvterm internally actually has the same code we have here, to be able to convert utf-8 data coming from the actual terminal process (which is then exposed as a part of the screen cell api). But this stuff is internal to libvterm, so we can't re-use this function.
// 4. There is no standard conversion implementation. I have lifted code here as-is from suckless st's codebase.
Rune* data4;
size_t size4 = utf8_to_ucs4(data, &data4, size);
vterm_keyboard_start_paste(vt);
// send in the characters
for(size_t i = 0; i < size4; i++) {
vterm_keyboard_unichar(vt, data4[i], VTERM_MOD_NONE);
}
vterm_keyboard_end_paste(vt);
free(data);
free(data4);
}
}
void
x11_key(XKeyEvent *ev)
{
char buf[32];
int i, num;
KeySym ksym;
num = XLookupString(ev, buf, sizeof buf, &ksym, 0);
if (ksym == XK_Left)
vterm_keyboard_key(vt, VTERM_KEY_LEFT, VTERM_MOD_NONE);
else if (ksym == XK_Right)
vterm_keyboard_key(vt, VTERM_KEY_RIGHT, VTERM_MOD_NONE);
else if (ksym == XK_Up)
vterm_keyboard_key(vt, VTERM_KEY_UP, VTERM_MOD_NONE);
else if (ksym == XK_Down)
vterm_keyboard_key(vt, VTERM_KEY_DOWN, VTERM_MOD_NONE);
else if (ksym == XK_Prior)
vterm_keyboard_key(vt, VTERM_KEY_PAGEUP, VTERM_MOD_NONE);
else if (ksym == XK_Next)
vterm_keyboard_key(vt, VTERM_KEY_PAGEDOWN, VTERM_MOD_NONE);
else
for (i = 0; i < num; i++)
vterm_keyboard_unichar(vt, buf[i], VTERM_MOD_NONE);
}
void
x11_resize(struct X11 *x11, XExposeEvent* ev)
{
x11->w = ev->width;
x11->h = ev->height;
x11->buf_w = ev->width / x11->font_width;
x11->buf_h = ev->height / x11->font_height;
vterm_set_size(vt, x11->buf_h, x11->buf_w);
term_set_size(&pty, x11);
}
void
x11_redraw(struct X11 *x11)
{
int x, y;
VTermScreenCell cell;
XftColor *fg;
XSetForeground(x11->dpy, x11->termgc, x11->col_bg);
XFillRectangle(x11->dpy, x11->termwin, x11->termgc, 0, 0, x11->w, x11->h);
for (y = 0; y < x11->buf_h; y++)
{
for (x = 0; x < x11->buf_w; x++)
{
vterm_screen_get_cell(vts, (VTermPos){.row = y, .col = x}, &cell);
// default color
fg = &x11->fcol_fg;
if (cell.attrs.reverse) {
// draw background of cell
XSetForeground(x11->dpy, x11->termgc, x11->col_fg);
XFillRectangle(x11->dpy, x11->termwin, x11->termgc,
x * x11->font_width,
y * x11->font_height,
x11->font_width, x11->font_height);
fg = &x11->fcol_bg;
}
XftDrawString32(x11->fdraw, fg, x11->font,
x * x11->font_width,
y * x11->font_height + x11->font->ascent,
(XftChar32 *) cell.chars, 1);
// note that we only use the first char instead of cell.width
// we we don't have the tech (read: harfbuzz) for combining chars
}
}
XSetForeground(x11->dpy, x11->termgc, x11->col_fg);
VTermPos cursor;
vterm_state_get_cursorpos(vtstate, &cursor);
XFillRectangle(x11->dpy, x11->termwin, x11->termgc,
cursor.col * x11->font_width,
cursor.row * x11->font_height,
x11->font_width, x11->font_height);
XSync(x11->dpy, False);
}
bool
x11_setup(struct X11 *x11)
{
Colormap cmap;
XColor color;
x11->dpy = XOpenDisplay(NULL);
if (x11->dpy == NULL)
{
fprintf(stderr, "Cannot open display\n");
return false;
}
x11->screen = DefaultScreen(x11->dpy);
x11->root = RootWindow(x11->dpy, x11->screen);
x11->fd = ConnectionNumber(x11->dpy);
x11->font = XftFontOpenName(x11->dpy, x11->screen,
"Monospace:size=22");
if (x11->font == NULL)
{
fprintf(stderr, "Could not load font\n");
return false;
}
x11->font_height = x11->font->height;
XGlyphInfo ext;
XftTextExtents8(x11->dpy, x11->font, (FcChar8 *)"m", 1, &ext);
x11->font_width = ext.width + 2;
cmap = DefaultColormap(x11->dpy, x11->screen);
if (!XAllocNamedColor(x11->dpy, cmap, "white", &color, &color))
{
fprintf(stderr, "Could not load bg color\n");
return false;
}
x11->col_bg = color.pixel;
if (!XAllocNamedColor(x11->dpy, cmap, "black", &color, &color))
{
fprintf(stderr, "Could not load fg color\n");
return false;
}
x11->col_fg = color.pixel;
// init XftColor for use with text
if (XftColorAllocName(x11->dpy,
DefaultVisual(x11->dpy, x11->screen),
cmap,
"black", &x11->fcol_fg) == False)
{
fprintf(stderr, "Could not load font fg color\n");
return false;
}
if (XftColorAllocName(x11->dpy,
DefaultVisual(x11->dpy, x11->screen),
cmap,
"white", &x11->fcol_bg) == False)
{
fprintf(stderr, "Could not load font bg color\n");
return false;
}
/* The terminal will have a fixed size of 80x25 cells. This is an
* arbitrary number. No resizing has been implemented and child
* processes can't even ask us for the current size (for now).
*/
x11->buf_w = 80;
x11->buf_h = 25;
x11->w = x11->buf_w * x11->font_width;
x11->h = x11->buf_h * x11->font_height;
x11->termwin = XCreateSimpleWindow(x11->dpy, x11->root,
0, 0,
x11->w, x11->h,
2, x11->col_fg, x11->col_bg);
/* allow receiving mouse events */
XSelectInput(x11->dpy, x11->termwin,
KeyReleaseMask|KeyPressMask|ExposureMask|ButtonPressMask);
XStoreName(x11->dpy, x11->termwin, "vex");
XMapWindow(x11->dpy, x11->termwin);
x11->termgc = XCreateGC(x11->dpy, x11->termwin, 0, NULL);
// init draw for xft drawing
x11->fdraw = XftDrawCreate(x11->dpy, x11->termwin,
DefaultVisual(x11->dpy, x11->screen), cmap);
if (x11->fdraw == NULL)
{
fprintf(stderr, "Could not create xft draw \n");
return false;
}
XSync(x11->dpy, False);
return true;
}
bool
spawn(struct PTY *pty)
{
pid_t p;
setenv("TERM", "xterm", 1);
p = fork();
if (p == 0)
{
close(pty->master);
/* Create a new session and make our terminal this process'
* controlling terminal. The shell that we'll spawn in a second
* will inherit the status of session leader. */
setsid();
if (ioctl(pty->slave, TIOCSCTTY, NULL) == -1)
{
perror("ioctl(TIOCSCTTY)");
return false;
}
dup2(pty->slave, 0);
dup2(pty->slave, 1);
dup2(pty->slave, 2);
close(pty->slave);
execl(SHELL, "-" SHELL, (char *)NULL);
return false;
}
else if (p > 0)
{
close(pty->slave);
return true;
}
perror("fork");
return false;
}
int
run(struct PTY *pty, struct X11 *x11)
{
int maxfd;
fd_set readable;
XEvent ev;
char* buf = (char *)malloc(100*sizeof(char));
size_t bread;
maxfd = pty->master > x11->fd ? pty->master : x11->fd;
for (;;)
{
FD_ZERO(&readable);
FD_SET(pty->master, &readable);
FD_SET(x11->fd, &readable);
if (select(maxfd + 1, &readable, NULL, NULL, NULL) == -1)
{
perror("select");
return 1;
}
if (FD_ISSET(pty->master, &readable))
{
bread = read(pty->master, buf, 100);
if (bread <= 0)
{
/* This is not necessarily an error but also happens
* when the child exits normally. */
fprintf(stderr, "Nothing to read from child: ");
perror(NULL);
return 1;
}
vterm_input_write(vt, buf, bread);
x11_redraw(x11);
}
if (FD_ISSET(x11->fd, &readable))
{
while (XPending(x11->dpy))
{
XNextEvent(x11->dpy, &ev);
switch (ev.type)
{
case Expose:
x11_resize(x11, &ev.xexpose);
x11_redraw(x11);
break;
case KeyPress:
x11_key(&ev.xkey);
break;
case ButtonPress:
x11_button(&ev.xbutton);
break;
}
}
}
}
return 0;
}
int
main()
{
if (!x11_setup(&x11))
return 1;
if (!pt_pair(&pty))
return 1;
vt = vterm_new(x11.buf_h, x11.buf_w);
if (vt == NULL)
return 1;
vterm_set_utf8(vt, 1);
vtstate = vterm_obtain_state(vt);
vterm_state_reset(vtstate, 1);
vts = vterm_obtain_screen(vt);
vterm_output_set_callback(vt, &vt_output_callback, NULL);
vterm_screen_enable_altscreen(vts, 1);
if (!term_set_size(&pty, &x11))
return 1;
if (!spawn(&pty))
return 1;
return run(&pty, &x11);
}