-
Notifications
You must be signed in to change notification settings - Fork 0
/
dvtm.c
1935 lines (1734 loc) · 38.7 KB
/
dvtm.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
/*
* The initial "port" of dwm to curses was done by
*
* © 2007-2016 Marc André Tanner <mat at brain-dump dot org>
*
* It is highly inspired by the original X11 dwm and
* reuses some code of it which is mostly
*
* © 2006-2007 Anselm R. Garbe <garbeam at gmail dot com>
*
* See LICENSE for details.
*/
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <wchar.h>
#include <limits.h>
#include <libgen.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/types.h>
#include <fcntl.h>
#include <curses.h>
#include <stdio.h>
#include <stdarg.h>
#include <signal.h>
#include <locale.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <errno.h>
#include <pwd.h>
#if defined __CYGWIN__ || defined __sun
# include <termios.h>
#endif
#include "vt.h"
#ifdef PDCURSES
int ESCDELAY;
#endif
#ifndef NCURSES_REENTRANT
# define set_escdelay(d) (ESCDELAY = (d))
#endif
typedef struct {
float mfact;
unsigned int nmaster;
int history;
int w;
int h;
volatile sig_atomic_t need_resize;
} Screen;
typedef struct {
const char *symbol;
void (*arrange)(void);
} Layout;
typedef struct Client Client;
struct Client {
WINDOW *window;
Vt *term;
Vt *editor, *app;
int editor_fds[2];
volatile sig_atomic_t editor_died;
const char *cmd;
char title[255];
int order;
pid_t pid;
unsigned short int id;
unsigned short int x;
unsigned short int y;
unsigned short int w;
unsigned short int h;
bool has_title_line;
bool minimized;
bool urgent;
volatile sig_atomic_t died;
Client *next;
Client *prev;
Client *snext;
unsigned int tags;
};
typedef struct {
short fg;
short bg;
short fg256;
short bg256;
short pair;
} Color;
typedef struct {
const char *title;
attr_t attrs;
Color *color;
} ColorRule;
#define ALT(k) ((k) + (161 - 'a'))
#if defined CTRL && defined _AIX
#undef CTRL
#endif
#ifndef CTRL
#define CTRL(k) ((k) & 0x1F)
#endif
#define CTRL_ALT(k) ((k) + (129 - 'a'))
#define MAX_ARGS 8
typedef struct {
void (*cmd)(const char *args[]);
const char *args[3];
} Action;
#define MAX_KEYS 3
typedef unsigned int KeyCombo[MAX_KEYS];
typedef struct {
KeyCombo keys;
Action action;
} KeyBinding;
typedef struct {
mmask_t mask;
Action action;
} Button;
typedef struct {
const char *name;
Action action;
} Cmd;
enum { BAR_TOP, BAR_BOTTOM, BAR_OFF };
typedef struct {
int fd;
int pos, lastpos;
bool autohide;
unsigned short int h;
unsigned short int y;
char text[512];
const char *file;
} StatusBar;
typedef struct {
int fd;
const char *file;
unsigned short int id;
} CmdFifo;
typedef struct {
char *data;
size_t len;
size_t size;
} Register;
typedef struct {
char *name;
const char *argv[4];
bool filter;
bool color;
} Editor;
#define LENGTH(arr) (sizeof(arr) / sizeof((arr)[0]))
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#define TAGMASK ((1 << LENGTH(tags)) - 1)
#ifdef NDEBUG
#define debug(format, args...)
#else
#define debug eprint
#endif
/* commands for use by keybindings */
static void create(const char *args[]);
static void copymode(const char *args[]);
static void focusn(const char *args[]);
static void focusid(const char *args[]);
static void focusnext(const char *args[]);
static void focusnextnm(const char *args[]);
static void focusprev(const char *args[]);
static void focusprevnm(const char *args[]);
static void focuslast(const char *args[]);
static void focusup(const char *args[]);
static void focusdown(const char *args[]);
static void focusleft(const char *args[]);
static void focusright(const char *args[]);
static void killclient(const char *args[]);
static void paste(const char *args[]);
static void quit(const char *args[]);
static void redraw(const char *args[]);
static void scrollback(const char *args[]);
static void send(const char *args[]);
static void setlayout(const char *args[]);
static void incnmaster(const char *args[]);
static void setmfact(const char *args[]);
static void startup(const char *args[]);
static void tag(const char *args[]);
static void tagid(const char *args[]);
static void togglebar(const char *args[]);
static void togglebarpos(const char *args[]);
static void toggleminimize(const char *args[]);
static void togglemouse(const char *args[]);
static void togglerunall(const char *args[]);
static void toggletag(const char *args[]);
static void toggleview(const char *args[]);
static void viewprevtag(const char *args[]);
static void view(const char *args[]);
static void zoom(const char *args[]);
/* commands for use by mouse bindings */
static void mouse_focus(const char *args[]);
static void mouse_fullscreen(const char *args[]);
static void mouse_minimize(const char *args[]);
static void mouse_zoom(const char *args[]);
/* functions and variables available to layouts via config.h */
static Client* nextvisible(Client *c);
static void focus(Client *c);
static void resize(Client *c, int x, int y, int w, int h);
extern Screen screen;
static unsigned int waw, wah, wax, way;
static Client *clients = NULL;
static char *title;
#include "config.h"
/* global variables */
static const char *dvtm_name = "dvtm";
Screen screen = { .mfact = MFACT, .nmaster = NMASTER, .history = SCROLL_HISTORY };
static Client *stack = NULL;
static Client *sel = NULL;
static Client *lastsel = NULL;
static Client *msel = NULL;
static unsigned int seltags;
static unsigned int tagset[2] = { 1, 1 };
static bool mouse_events_enabled = ENABLE_MOUSE;
static Layout *layout = layouts;
static StatusBar bar = { .fd = -1, .lastpos = BAR_POS, .pos = BAR_POS, .autohide = BAR_AUTOHIDE, .h = 1 };
static CmdFifo cmdfifo = { .fd = -1 };
static const char *shell;
static Register copyreg;
static volatile sig_atomic_t running = true;
static bool runinall = false;
static void
eprint(const char *errstr, ...) {
va_list ap;
va_start(ap, errstr);
vfprintf(stderr, errstr, ap);
va_end(ap);
}
static void
error(const char *errstr, ...) {
va_list ap;
va_start(ap, errstr);
vfprintf(stderr, errstr, ap);
va_end(ap);
exit(EXIT_FAILURE);
}
static bool
isarrange(void (*func)()) {
return func == layout->arrange;
}
static bool
isvisible(Client *c) {
return c->tags & tagset[seltags];
}
static bool
is_content_visible(Client *c) {
if (!c)
return false;
if (isarrange(fullscreen))
return sel == c;
return isvisible(c) && !c->minimized;
}
static Client*
nextvisible(Client *c) {
for (; c && !isvisible(c); c = c->next);
return c;
}
static void
updatebarpos(void) {
bar.y = 0;
wax = 0;
way = 0;
wah = screen.h;
waw = screen.w;
if (bar.pos == BAR_TOP) {
wah -= bar.h;
way += bar.h;
} else if (bar.pos == BAR_BOTTOM) {
wah -= bar.h;
bar.y = wah;
}
}
static void
hidebar(void) {
if (bar.pos != BAR_OFF) {
bar.lastpos = bar.pos;
bar.pos = BAR_OFF;
}
}
static void
showbar(void) {
if (bar.pos == BAR_OFF)
bar.pos = bar.lastpos;
}
static void
drawbar(void) {
int sx, sy, x, y, width;
unsigned int occupied = 0, urgent = 0;
if (bar.pos == BAR_OFF)
return;
for (Client *c = clients; c; c = c->next) {
occupied |= c->tags;
if (c->urgent)
urgent |= c->tags;
}
getyx(stdscr, sy, sx);
attrset(BAR_ATTR);
move(bar.y, 0);
for (unsigned int i = 0; i < LENGTH(tags); i++){
if (tagset[seltags] & (1 << i))
attrset(TAG_SEL);
else if (urgent & (1 << i))
attrset(TAG_URGENT);
else if (occupied & (1 << i))
attrset(TAG_OCCUPIED);
else
attrset(TAG_NORMAL);
printw(TAG_SYMBOL, tags[i]);
}
attrset(runinall ? TAG_SEL : TAG_NORMAL);
addstr(layout->symbol);
attrset(TAG_NORMAL);
getyx(stdscr, y, x);
(void)y;
int maxwidth = screen.w - x - 2;
addch(BAR_BEGIN);
attrset(BAR_ATTR);
wchar_t wbuf[sizeof bar.text];
size_t numchars = mbstowcs(wbuf, bar.text, sizeof bar.text);
if (numchars != (size_t)-1 && (width = wcswidth(wbuf, maxwidth)) != -1) {
int pos;
for (pos = 0; pos + width < maxwidth; pos++)
addch(' ');
for (size_t i = 0; i < numchars; i++) {
pos += wcwidth(wbuf[i]);
if (pos > maxwidth)
break;
addnwstr(wbuf+i, 1);
}
clrtoeol();
}
attrset(TAG_NORMAL);
mvaddch(bar.y, screen.w - 1, BAR_END);
attrset(NORMAL_ATTR);
move(sy, sx);
wnoutrefresh(stdscr);
}
static int
show_border(void) {
return (bar.pos != BAR_OFF) || (clients && clients->next);
}
static void
draw_border(Client *c) {
char t = '\0';
int x, y, maxlen, attrs = NORMAL_ATTR;
if (!show_border())
return;
if (sel != c && c->urgent)
attrs = URGENT_ATTR;
if (sel == c || (runinall && !c->minimized))
attrs = SELECTED_ATTR;
wattrset(c->window, attrs);
getyx(c->window, y, x);
mvwhline(c->window, 0, 0, ACS_HLINE, c->w);
maxlen = c->w - 10;
if (maxlen < 0)
maxlen = 0;
if ((size_t)maxlen < sizeof(c->title)) {
t = c->title[maxlen];
c->title[maxlen] = '\0';
}
mvwprintw(c->window, 0, 2, "[%s%s#%d]",
*c->title ? c->title : "",
*c->title ? " | " : "",
c->order);
if (t)
c->title[maxlen] = t;
wmove(c->window, y, x);
}
static void
draw_content(Client *c) {
vt_draw(c->term, c->window, c->has_title_line, 0);
}
static void
draw(Client *c) {
if (is_content_visible(c)) {
redrawwin(c->window);
draw_content(c);
}
if (!isarrange(fullscreen) || sel == c)
draw_border(c);
wnoutrefresh(c->window);
}
static void
draw_all(void) {
if (!nextvisible(clients)) {
sel = NULL;
curs_set(0);
erase();
drawbar();
doupdate();
return;
}
if (!isarrange(fullscreen)) {
for (Client *c = nextvisible(clients); c; c = nextvisible(c->next)) {
if (c != sel)
draw(c);
}
}
/* as a last step the selected window is redrawn,
* this has the effect that the cursor position is
* accurate
*/
if (sel)
draw(sel);
}
static void
arrange(void) {
unsigned int m = 0, n = 0;
for (Client *c = nextvisible(clients); c; c = nextvisible(c->next)) {
c->order = ++n;
if (c->minimized)
m++;
}
erase();
attrset(NORMAL_ATTR);
if (bar.fd == -1 && bar.autohide) {
if ((!clients || !clients->next) && n == 1)
hidebar();
else
showbar();
updatebarpos();
}
if (m && !isarrange(fullscreen))
wah--;
layout->arrange();
if (m && !isarrange(fullscreen)) {
unsigned int i = 0, nw = waw / m, nx = wax;
for (Client *c = nextvisible(clients); c; c = nextvisible(c->next)) {
if (c->minimized) {
resize(c, nx, way+wah, ++i == m ? waw - nx : nw, 1);
nx += nw;
}
}
wah++;
}
focus(NULL);
wnoutrefresh(stdscr);
drawbar();
draw_all();
}
static void
attach(Client *c) {
if (clients)
clients->prev = c;
c->next = clients;
c->prev = NULL;
clients = c;
for (int o = 1; c; c = nextvisible(c->next), o++)
c->order = o;
}
static void
attachafter(Client *c, Client *a) { /* attach c after a */
if (c == a)
return;
if (!a)
for (a = clients; a && a->next; a = a->next);
if (a) {
if (a->next)
a->next->prev = c;
c->next = a->next;
c->prev = a;
a->next = c;
for (int o = a->order; c; c = nextvisible(c->next))
c->order = ++o;
}
}
static void
attachstack(Client *c) {
c->snext = stack;
stack = c;
}
static void
detach(Client *c) {
Client *d;
if (c->prev)
c->prev->next = c->next;
if (c->next) {
c->next->prev = c->prev;
for (d = nextvisible(c->next); d; d = nextvisible(d->next))
--d->order;
}
if (c == clients)
clients = c->next;
c->next = c->prev = NULL;
}
static void
settitle(Client *c) {
char *term, *t = title;
if (!t && sel == c && *c->title)
t = c->title;
if (t && (term = getenv("TERM")) && !strstr(term, "linux")) {
printf("\033]0;%s\007", t);
fflush(stdout);
}
}
static void
detachstack(Client *c) {
Client **tc;
for (tc = &stack; *tc && *tc != c; tc = &(*tc)->snext);
*tc = c->snext;
}
static void
focus(Client *c) {
if (!c)
for (c = stack; c && !isvisible(c); c = c->snext);
if (sel == c)
return;
lastsel = sel;
sel = c;
if (lastsel) {
lastsel->urgent = false;
if (!isarrange(fullscreen)) {
draw_border(lastsel);
wnoutrefresh(lastsel->window);
}
}
if (c) {
detachstack(c);
attachstack(c);
settitle(c);
c->urgent = false;
if (isarrange(fullscreen)) {
draw(c);
} else {
draw_border(c);
wnoutrefresh(c->window);
}
}
curs_set(c && !c->minimized && vt_cursor_visible(c->term));
}
static void
applycolorrules(Client *c) {
const ColorRule *r = colorrules;
short fg = r->color->fg, bg = r->color->bg;
attr_t attrs = r->attrs;
for (unsigned int i = 1; i < LENGTH(colorrules); i++) {
r = &colorrules[i];
if (strstr(c->title, r->title)) {
attrs = r->attrs;
fg = r->color->fg;
bg = r->color->bg;
break;
}
}
vt_default_colors_set(c->term, attrs, fg, bg);
}
static void
term_title_handler(Vt *term, const char *title) {
Client *c = (Client *)vt_data_get(term);
if (title)
strncpy(c->title, title, sizeof(c->title) - 1);
c->title[title ? sizeof(c->title) - 1 : 0] = '\0';
settitle(c);
if (!isarrange(fullscreen) || sel == c)
draw_border(c);
applycolorrules(c);
}
static void
term_urgent_handler(Vt *term) {
Client *c = (Client *)vt_data_get(term);
c->urgent = true;
printf("\a");
fflush(stdout);
drawbar();
if (!isarrange(fullscreen) && sel != c && isvisible(c))
draw_border(c);
}
static void
move_client(Client *c, int x, int y) {
if (c->x == x && c->y == y)
return;
debug("moving, x: %d y: %d\n", x, y);
if (mvwin(c->window, y, x) == ERR) {
eprint("error moving, x: %d y: %d\n", x, y);
} else {
c->x = x;
c->y = y;
}
}
static void
resize_client(Client *c, int w, int h) {
bool has_title_line = show_border();
bool resize_window = c->w != w || c->h != h;
if (resize_window) {
debug("resizing, w: %d h: %d\n", w, h);
if (wresize(c->window, h, w) == ERR) {
eprint("error resizing, w: %d h: %d\n", w, h);
} else {
c->w = w;
c->h = h;
}
}
if (resize_window || c->has_title_line != has_title_line) {
c->has_title_line = has_title_line;
vt_resize(c->app, h - has_title_line, w);
if (c->editor)
vt_resize(c->editor, h - has_title_line, w);
}
}
static void
resize(Client *c, int x, int y, int w, int h) {
resize_client(c, w, h);
move_client(c, x, y);
}
static Client*
get_client_by_coord(unsigned int x, unsigned int y) {
if (y < way || y >= way+wah)
return NULL;
if (isarrange(fullscreen))
return sel;
for (Client *c = nextvisible(clients); c; c = nextvisible(c->next)) {
if (x >= c->x && x < c->x + c->w && y >= c->y && y < c->y + c->h) {
debug("mouse event, x: %d y: %d client: %d\n", x, y, c->order);
return c;
}
}
return NULL;
}
static void
sigchld_handler(int sig) {
int errsv = errno;
int status;
pid_t pid;
while ((pid = waitpid(-1, &status, WNOHANG)) != 0) {
if (pid == -1) {
if (errno == ECHILD) {
/* no more child processes */
break;
}
eprint("waitpid: %s\n", strerror(errno));
break;
}
debug("child with pid %d died\n", pid);
for (Client *c = clients; c; c = c->next) {
if (c->pid == pid) {
c->died = true;
break;
}
if (c->editor && vt_pid_get(c->editor) == pid) {
c->editor_died = true;
break;
}
}
}
errno = errsv;
}
static void
sigwinch_handler(int sig) {
screen.need_resize = true;
}
static void
sigterm_handler(int sig) {
running = false;
}
static void
resize_screen(void) {
struct winsize ws;
if (ioctl(0, TIOCGWINSZ, &ws) == -1) {
getmaxyx(stdscr, screen.h, screen.w);
} else {
screen.w = ws.ws_col;
screen.h = ws.ws_row;
}
debug("resize_screen(), w: %d h: %d\n", screen.w, screen.h);
resizeterm(screen.h, screen.w);
wresize(stdscr, screen.h, screen.w);
updatebarpos();
clear();
arrange();
}
static KeyBinding*
keybinding(KeyCombo keys, unsigned int keycount) {
for (unsigned int b = 0; b < LENGTH(bindings); b++) {
for (unsigned int k = 0; k < keycount; k++) {
if (keys[k] != bindings[b].keys[k])
break;
if (k == keycount - 1)
return &bindings[b];
}
}
return NULL;
}
static unsigned int
bitoftag(const char *tag) {
unsigned int i;
if (!tag)
return ~0;
for (i = 0; (i < LENGTH(tags)) && strcmp(tags[i], tag); i++);
return (i < LENGTH(tags)) ? (1 << i) : 0;
}
static void
tagschanged() {
bool allminimized = true;
for (Client *c = nextvisible(clients); c; c = nextvisible(c->next)) {
if (!c->minimized) {
allminimized = false;
break;
}
}
if (allminimized && nextvisible(clients)) {
focus(NULL);
toggleminimize(NULL);
}
arrange();
}
static void
tag(const char *args[]) {
if (!sel)
return;
sel->tags = bitoftag(args[0]) & TAGMASK;
tagschanged();
}
static void
tagid(const char *args[]) {
if (!args[0] || !args[1])
return;
const int win_id = atoi(args[0]);
for (Client *c = clients; c; c = c->next) {
if (c->id == win_id) {
unsigned int ntags = c->tags;
for (unsigned int i = 1; i < MAX_ARGS && args[i]; i++) {
if (args[i][0] == '+')
ntags |= bitoftag(args[i]+1);
else if (args[i][0] == '-')
ntags &= ~bitoftag(args[i]+1);
else
ntags = bitoftag(args[i]);
}
ntags &= TAGMASK;
if (ntags) {
c->tags = ntags;
tagschanged();
}
return;
}
}
}
static void
toggletag(const char *args[]) {
if (!sel)
return;
unsigned int newtags = sel->tags ^ (bitoftag(args[0]) & TAGMASK);
if (newtags) {
sel->tags = newtags;
tagschanged();
}
}
static void
toggleview(const char *args[]) {
unsigned int newtagset = tagset[seltags] ^ (bitoftag(args[0]) & TAGMASK);
if (newtagset) {
tagset[seltags] = newtagset;
tagschanged();
}
}
static void
view(const char *args[]) {
unsigned int newtagset = bitoftag(args[0]) & TAGMASK;
if (tagset[seltags] != newtagset && newtagset) {
seltags ^= 1; /* toggle sel tagset */
tagset[seltags] = newtagset;
tagschanged();
}
}
static void
viewprevtag(const char *args[]) {
seltags ^= 1;
tagschanged();
}
static void
keypress(int code) {
int key = -1;
unsigned int len = 1;
char buf[8] = { '\e' };
if (code == '\e') {
/* pass characters following escape to the underlying app */
nodelay(stdscr, TRUE);
for (int t; len < sizeof(buf) && (t = getch()) != ERR; len++) {
if (t > 255) {
key = t;
break;
}
buf[len] = t;
}
nodelay(stdscr, FALSE);
}
for (Client *c = runinall ? nextvisible(clients) : sel; c; c = nextvisible(c->next)) {
if (is_content_visible(c)) {
c->urgent = false;
if (code == '\e')
vt_write(c->term, buf, len);
else
vt_keypress(c->term, code);
if (key != -1)
vt_keypress(c->term, key);
}
if (!runinall)
break;
}
}
static void
mouse_setup(void) {
#ifdef CONFIG_MOUSE
mmask_t mask = 0;
if (mouse_events_enabled) {
mask = BUTTON1_CLICKED | BUTTON2_CLICKED;
for (unsigned int i = 0; i < LENGTH(buttons); i++)
mask |= buttons[i].mask;
}
mousemask(mask, NULL);
#endif /* CONFIG_MOUSE */
}
static bool
checkshell(const char *shell) {
if (shell == NULL || *shell == '\0' || *shell != '/')
return false;
if (!strcmp(strrchr(shell, '/')+1, dvtm_name))
return false;
if (access(shell, X_OK))
return false;
return true;
}
static const char *
getshell(void) {
const char *shell = getenv("SHELL");
struct passwd *pw;
if (checkshell(shell))
return shell;
if ((pw = getpwuid(getuid())) && checkshell(pw->pw_shell))
return pw->pw_shell;
return "/bin/sh";
}
static void
setup(void) {
shell = getshell();
setlocale(LC_CTYPE, "");
initscr();
start_color();
noecho();
nonl();
keypad(stdscr, TRUE);
mouse_setup();
raw();
vt_init();
vt_keytable_set(keytable, LENGTH(keytable));
for (unsigned int i = 0; i < LENGTH(colors); i++) {
if (COLORS == 256) {
if (colors[i].fg256)
colors[i].fg = colors[i].fg256;
if (colors[i].bg256)
colors[i].bg = colors[i].bg256;
}
colors[i].pair = vt_color_reserve(colors[i].fg, colors[i].bg);
}
resize_screen();
struct sigaction sa;
memset(&sa, 0, sizeof sa);
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sa.sa_handler = sigwinch_handler;
sigaction(SIGWINCH, &sa, NULL);
sa.sa_handler = sigchld_handler;
sigaction(SIGCHLD, &sa, NULL);
sa.sa_handler = sigterm_handler;
sigaction(SIGTERM, &sa, NULL);
sa.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &sa, NULL);
}
static void
destroy(Client *c) {
if (sel == c)
focusnextnm(NULL);
detach(c);
detachstack(c);
if (sel == c) {
Client *next = nextvisible(clients);
if (next) {
focus(next);
toggleminimize(NULL);
} else {
sel = NULL;
}
}
if (lastsel == c)
lastsel = NULL;
werase(c->window);
wnoutrefresh(c->window);
vt_destroy(c->term);
delwin(c->window);
if (!clients && LENGTH(actions)) {