-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptylist.c
541 lines (455 loc) · 12.2 KB
/
ptylist.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
/*
* vtsh - A mashup of virtual terminal and shell
* Copyright (c) 2021-2022, Tommi Leino <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "ptylist.h"
#include "dpy.h"
#include "pty.h"
#include "widget.h"
#include "layout.h"
#include "editor.h"
#include "xevent.h"
#include "font.h"
#include "button.h"
#include "util.h"
#include "event.h"
#include <X11/Xutil.h>
#include <X11/XKBlib.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include <assert.h>
#include <stdio.h>
struct ptylist {
struct dpy *dpy;
struct pty *ptys[100];
int n_ptys;
int i;
struct widget *widget;
struct layout *vbox;
struct ptylist *first;
struct ptylist *next;
struct widget *context_menu;
struct pty *context_pty;
char *context_s;
};
static int ptylist_keypress(XKeyEvent *, void *);
static void ptylist_focus_change(int, void *);
static struct pty *ptylist_add_pty(struct ptylist *, struct pty *);
static int ptylist_find_pty(struct ptylist *, struct widget *);
static void ptylist_destroy(void *);
static void ptylist_create_new_window(void);
static void ptylist_close_pty(struct ptylist *, struct pty *);
static void ptylist_context_open(struct ptylist *,
struct pty *, const char *, int, int);
static void ptylist_context_close(struct ptylist *);
static void ptylist_ptyaction(struct pty *, PtyAction,
const char *, int, int, void *);
static int n_ptylist;
static int ptylist_i = 1;
struct ptylist *ptylist_root;
struct ptylist *
ptylist_create(const char *name, struct widget *parent)
{
struct ptylist *ptylist;
extern struct dpy *dpy;
if ((ptylist = calloc(1, sizeof(*ptylist))) == NULL)
return NULL;
if ((WIDGET(ptylist) = widget_create(name, parent)) == NULL)
goto fail;
if ((ptylist->vbox = layout_create_vbox("vbox", WIDGET(ptylist)))
== NULL)
goto fail;
ptylist->dpy = dpy;
widget_set_keypress_callback(WIDGET(ptylist), ptylist_keypress,
ptylist);
widget_set_focus_change_callback(WIDGET(ptylist),
ptylist_focus_change, ptylist);
ptylist_add_pty(ptylist, NULL);
widget_show(WIDGET(ptylist->vbox));
widget_show(WIDGET(ptylist));
add_destroy_handler(WIDGET(ptylist)->window, ptylist_destroy, ptylist);
if (n_ptylist == 0)
ptylist_root = ptylist;
n_ptylist++;
return ptylist;
fail:
ptylist_free(ptylist);
return NULL;
}
static void
ptylist_destroy(void *udata)
{
struct ptylist *ptylist = udata;
extern struct dpy *dpy;
XSync(DPY(dpy), False);
font_close();
ptylist_free(ptylist);
}
void
ptylist_free_all()
{
struct ptylist *np, *next;
if (ptylist_root) {
for (np = ptylist_root->first; np != NULL; np = next) {
next = np->next;
free(np);
n_ptylist--;
}
free(ptylist_root);
ptylist_root = NULL;
n_ptylist--;
}
}
void
ptylist_free(struct ptylist *ptylist)
{
int i;
struct ptylist *np;
extern int running;
if (ptylist == ptylist_root) {
ptylist_root = ptylist_root->first;
if (ptylist_root != NULL && ptylist_root->first != NULL)
ptylist_root->first = ptylist_root->first->next;
} else if (ptylist_root != NULL && ptylist_root->first == ptylist) {
ptylist_root->first = ptylist_root->first->next;
} else if (ptylist_root != NULL) {
np = ptylist_root->first;
while (np) {
if (np->next == ptylist) {
np->next = np->next->next;
break;
}
np = np->next;
}
}
for (i = 0; i < ptylist->n_ptys; i++)
pty_free(ptylist->ptys[i]);
if (ptylist->vbox != NULL)
layout_free(ptylist->vbox);
if (ptylist->context_menu != NULL)
widget_free(ptylist->context_menu);
widget_free(WIDGET(ptylist));
free(ptylist);
n_ptylist--;
if (n_ptylist == 0)
running = 0;
}
static void
ptylist_context_close(struct ptylist *ptylist)
{
if (ptylist->context_menu == NULL)
return;
widget_hide(ptylist->context_menu);
ptylist->context_pty = NULL;
if (ptylist->context_s != NULL) {
free(ptylist->context_s);
ptylist->context_s = NULL;
}
}
static void
ptylist_open_button(struct button *button, void *udata)
{
struct ptylist *ptylist = udata;
struct pty *pty;
char *s;
if (ptylist->context_menu == NULL)
return;
s = malloc(strlen(ptylist->context_s) + 2);
if (s != NULL) {
snprintf(s, strlen(ptylist->context_s) + 2,
":%s", ptylist->context_s);
pty = ptylist_add_pty(ptylist, NULL);
pty_run_command(pty, s);
free(s);
}
ptylist_context_close(ptylist);
}
static void
ptylist_exec_button(struct button *button, void *udata)
{
struct ptylist *ptylist = udata;
struct pty *pty;
if (ptylist->context_menu == NULL)
return;
pty = ptylist_add_pty(ptylist, NULL);
pty_run_command(pty, ptylist->context_s);
ptylist_context_close(ptylist);
}
static void
ptylist_context_open(struct ptylist *ptylist, struct pty *pty,
const char *s, int x, int y)
{
struct button *open_button, *exec_button;
struct layout *vbox;
extern struct dpy *dpy;
if (ptylist->context_menu != NULL) {
XMoveWindow(DPY(dpy), ptylist->context_menu->window, x, y);
XRaiseWindow(DPY(dpy), ptylist->context_menu->window);
widget_show(ptylist->context_menu);
} else {
ptylist->context_menu = widget_create_transient("context_menu",
widget_find_root(WIDGET(ptylist)));
if (ptylist->context_menu == NULL)
return;
}
ptylist->context_pty = pty;
ptylist->context_s = strdup(s);
XMoveWindow(DPY(dpy), ptylist->context_menu->window, x, y);
vbox = layout_create_vbox("context_vbox", ptylist->context_menu);
open_button = button_create("open", ptylist_open_button, ptylist,
"open", WIDGET(vbox));
open_button->act_on_release = 1;
exec_button = button_create("exec", ptylist_exec_button, ptylist,
"exec", WIDGET(vbox));
exec_button->act_on_release = 1;
XResizeWindow(DPY(dpy), ptylist->context_menu->window,
MAX(
WIDGET(open_button)->prefer_size[0],
WIDGET(exec_button)->prefer_size[0]),
font_height() * 2);
widget_show(ptylist->context_menu);
}
static void
ptylist_ptyaction(struct pty *pty, PtyAction ptyaction, const char *s,
int x, int y, void *udata)
{
struct ptylist *ptylist = udata;
extern struct dpy *dpy;
XEvent e;
switch (ptyaction) {
case PtyActionOpen:
ptylist_context_open(ptylist, pty, s, x, y);
XGrabPointer(DPY(dpy), ptylist->context_menu->window, False,
ButtonPressMask | ButtonReleaseMask | ButtonMotionMask,
GrabModeAsync, GrabModeSync,
None, None, CurrentTime);
XSync(DPY(dpy), False);
event_dispatch_xevents(1);
XMaskEvent(DPY(dpy), ButtonReleaseMask, &e);
if (e.xbutton.button == 3 &&
e.xbutton.window == ptylist->context_menu->window &&
e.xbutton.subwindow != 0) {
e.xbutton.window = e.xbutton.subwindow;
XSendEvent(DPY(dpy), e.xbutton.subwindow,
True, ButtonReleaseMask, &e);
} else
ptylist_context_close(ptylist);
XUngrabPointer(DPY(dpy), CurrentTime);
event_dispatch_xevents(1);
break;
case PtyActionClose:
ptylist_close_pty(ptylist, pty);
break;
case PtyActionToggleHide:
pty_toggle_hide_output(pty);
break;
}
}
static void
ptylist_close_pty(struct ptylist *ptylist, struct pty *pty)
{
struct widget *root, *ptywidget;
int i;
root = widget_find_root(WIDGET(ptylist));
/*
* TODO: Remove this hack. Focusing cmd_editor here is needed
* only because the rest of this function knows only the keyboard
* focus and keyboard focus can be only on an editor, while in
* fact what we care here is the pty itself. We get pty pointer
* when we're handling mouse presses e.g. to pty's buttons.
*/
if (pty != NULL) {
widget_focus(WIDGET(pty->cmd_editor));
}
ptywidget = root->focus;
/*
* Ensure focus can land on a new pty, refuse otherwise.
*/
widget_focus_prev(ptywidget, root->level);
if (root->focus == ptywidget) {
widget_focus_next(ptywidget, root->level);
if (root->focus == ptywidget) {
widget_focus(ptywidget);
return;
}
}
/*
* Remove it.
*/
i = ptylist_find_pty(ptylist, ptywidget);
if (i >= 0) {
pty_free(ptylist->ptys[i]);
if (i+1 < ptylist->n_ptys)
memmove(&ptylist->ptys[i],
&ptylist->ptys[i+1],
(ptylist->n_ptys-i-1) *
sizeof(struct pty *));
ptylist->n_ptys--;
} else
assert(0);
}
static struct pty *
ptylist_add_pty(struct ptylist *ptylist, struct pty *master)
{
int i;
struct widget *root, *ptywidget = NULL;
char name[256];
struct pty *pty;
root = widget_find_root(WIDGET(ptylist));
ptywidget = root->focus;
i = ptylist_find_pty(ptylist, ptywidget);
if (i == -1) {
i = ptylist->n_ptys;
ptywidget = NULL;
} else
ptywidget = WIDGET(ptylist->ptys[i]);
memmove(&ptylist->ptys[i+1], &ptylist->ptys[i],
(ptylist->n_ptys-i) * sizeof(struct pty *));
snprintf(name, sizeof(name), "pty%d", ++ptylist->i);
pty = pty_create(master, name, WIDGET(ptylist->vbox));
if (pty == NULL) {
warn("creating pty");
return NULL;
}
pty_set_action_callback(pty, ptylist_ptyaction, ptylist);
ptylist->ptys[i] = pty;
ptylist->n_ptys++;
if (ptywidget != NULL)
widget_move_after(WIDGET(ptylist->ptys[i]), ptywidget);
widget_focus(WIDGET(ptylist->ptys[i]));
return pty;
}
static int
ptylist_find_pty(struct ptylist *ptylist, struct widget *widget)
{
int i;
struct widget *np;
for (i = 0; i < ptylist->n_ptys; i++) {
np = widget;
while (np && np->parent != NULL) {
np = np->parent;
if (WIDGET(ptylist->ptys[i]) == np)
return i;
}
}
return -1;
}
struct pty *
ptylist_find_focus(struct ptylist *ptylist)
{
struct widget *root, *ptywidget;
int i;
root = widget_find_root(WIDGET(ptylist));
ptywidget = root->focus;
i = ptylist_find_pty(ptylist, ptywidget);
if (i >= 0)
return ptylist->ptys[i];
return NULL;
}
static void
ptylist_focus_change(int state, void *udata)
{
struct ptylist *ptylist = udata;
struct pty *pty;
pty = ptylist_find_focus(ptylist);
if (pty != NULL)
editor_shrink(pty->ts_editor);
}
static void
ptylist_create_new_window()
{
struct ptylist *np;
XEvent e;
extern struct dpy *dpy;
char buf[16];
snprintf(buf, sizeof(buf), "vtsh%d", ++ptylist_i);
np = ptylist_create(buf, NULL);
np->next = ptylist_root->first;
ptylist_root->first = np;
XSync(DPY(dpy), False);
do {
XMaskEvent(DPY(dpy), StructureNotifyMask, &e);
} while (e.type != MapNotify);
}
void
ptylist_toggle_focus_level(struct ptylist *ptylist)
{
struct widget *root;
root = widget_find_root(WIDGET(ptylist));
root->level ^= 1;
if (root->level == 0)
widget_focus_prev(root->focus, root->level);
else
widget_focus_next(root->focus, root->level);
}
static int
ptylist_keypress(XKeyEvent *xkey, void *udata)
{
struct ptylist *ptylist = udata;
KeySym sym;
int i;
struct pty *pty;
extern struct dpy *dpy;
sym = XkbKeycodeToKeysym(DPY(ptylist->dpy), xkey->keycode, 0,
(xkey->state & ShiftMask) ? 1 : 0);
if (sym == XK_s && xkey->state & ControlMask) {
pty = ptylist_find_focus(ptylist);
pty_save(pty);
warnx("saved");
}
if (!(xkey->state & Mod1Mask) && sym != XK_Escape)
return 0;
switch (sym) {
case XK_n:
ptylist_create_new_window();
return 1;
case XK_space:
case XK_Insert:
pty = ptylist_add_pty(ptylist, NULL);
return 1;
case XK_s:
pty = ptylist_find_focus(ptylist);
if (pty != NULL) {
if (pty->ptyfd != -1)
ptylist_add_pty(ptylist, pty);
else if (pty->master != NULL)
ptylist_add_pty(ptylist, pty->master);
}
return 1;
case XK_H:
pty = ptylist_find_focus(ptylist);
pty_show_output(pty);
for (i = 0; i < ptylist->n_ptys; i++) {
if (ptylist->ptys[i] == pty)
continue;
pty_hide_output(ptylist->ptys[i]);
}
return 1;
case XK_h:
pty = ptylist_find_focus(ptylist);
if (pty != NULL)
pty_toggle_hide_output(pty);
return 1;
case XK_Escape:
case XK_Return:
ptylist_toggle_focus_level(ptylist);
return 1;
case XK_BackSpace:
ptylist_close_pty(ptylist, NULL);
return 1;
}
return 0;
}