-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtmuxwm.c
176 lines (160 loc) · 6.51 KB
/
tmuxwm.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
/* TMUXWM.C
* Copyright Jesse McClure 2015
* License: GPLv3
* Compile: gcc -o tmuxwm tmuxwm.c -lX11
*/
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/XKBlib.h>
#include <X11/keysym.h>
#include <X11/Xutil.h>
#include <X11/XF86keysym.h>
typedef struct Bind { KeySym sym; int mod; const char * const *args; } Bind;
typedef const char * const Arg[];
/* The next ~30 lines could be separated into a "config.h", but this is BMFM
* code: By Me For Me. So I don't care to separate it for your convenience.
*/
Arg tmux_client = (Arg) { "termite", "-e", "tmux", "new-session", NULL };
static Bind bind[] = {
{ XK_Return, Mod4Mask, (Arg) { "tmux", "rename-window", NULL } },
{ XK_Tab, Mod4Mask, (Arg) { "tmux", "last-pane", NULL } },
{ XK_space, Mod4Mask, (Arg) { "tmux", "new-window", NULL } },
{ XK_1, Mod4Mask, (Arg) { "tmux", "select-window", "-t", "1", NULL } },
{ XK_2, Mod4Mask, (Arg) { "tmux", "select-window", "-t", "2", NULL } },
{ XK_3, Mod4Mask, (Arg) { "tmux", "select-window", "-t", "3", NULL } },
{ XK_4, Mod4Mask, (Arg) { "tmux", "select-window", "-t", "4", NULL } },
{ XK_5, Mod4Mask, (Arg) { "tmux", "select-window", "-t", "5", NULL } },
{ XK_6, Mod4Mask, (Arg) { "tmux", "select-window", "-t", "6", NULL } },
{ XK_7, Mod4Mask, (Arg) { "tmux", "select-window", "-t", "7", NULL } },
{ XK_8, Mod4Mask, (Arg) { "tmux", "select-window", "-t", "8", NULL } },
{ XK_9, Mod4Mask, (Arg) { "tmux", "select-window", "-t", "9", NULL } },
{ XK_h, Mod4Mask, (Arg) { "tmux", "split-window", "-h", NULL} },
{ XK_v, Mod4Mask, (Arg) { "tmux", "split-window", "-v", NULL} },
{ XK_w, Mod4Mask, (Arg) { "chromium", NULL} },
{ XK_Menu, 0, (Arg) { "interrobang", NULL } },
};
/* End of potential "config.h" content
*/
static void buttonpress(XEvent *);
static void configurerequest(XEvent *);
static void enternotify(XEvent *);
static void keypress(XEvent *);
static void maprequest(XEvent *);
static void unmapnotify(XEvent *);
static Display *dpy;
static Window root;
static int sw, sh;
static void (*handler[LASTEvent])(XEvent *) = {
[ButtonPress] = buttonpress,
[ConfigureRequest] = configurerequest,
[EnterNotify] = enternotify,
[KeyPress] = keypress,
[MapRequest] = maprequest,
};
static int xerror(Display *d, XErrorEvent *e) {
char msg[256];
XGetErrorText(d, e->error_code, msg, sizeof(msg));
fprintf(stderr,"[X11 %d:%d] %s\n", e->request_code, e->error_code, msg);
}
static inline int spawn(Arg args) {
if (fork() != 0) return 1;
close(ConnectionNumber(dpy));
setsid();
execvp(args[0], args);
}
int main(int argc, const char **argv) {
signal(SIGCHLD, SIG_IGN);
if(!(dpy = XOpenDisplay(0x0))) return 1;
XSetErrorHandler(xerror);
root = DefaultRootWindow(dpy);
sw = DisplayWidth(dpy, DefaultScreen(dpy));
sh = DisplayHeight(dpy, DefaultScreen(dpy));
XGrabKey(dpy, XKeysymToKeycode(dpy, XK_Tab), Mod1Mask, root, True, GrabModeAsync, GrabModeAsync);
XGrabKey(dpy, XKeysymToKeycode(dpy, XK_Return), Mod4Mask|ShiftMask, root, True, GrabModeAsync, GrabModeAsync);
int i;
for (i = 0; i < sizeof(bind)/sizeof(bind[0]); ++i)
XGrabKey(dpy, XKeysymToKeycode(dpy, bind[i].sym), bind[i].mod, root, True, GrabModeAsync, GrabModeAsync);
XGrabButton(dpy, AnyButton, Mod4Mask, root, True, ButtonPressMask, GrabModeAsync, GrabModeAsync, None, None);
XSelectInput(dpy, root, SubstructureNotifyMask | SubstructureRedirectMask);
XEvent ev;
spawn(tmux_client);
while (!XNextEvent(dpy,&ev))
if (handler[ev.type]) handler[ev.type](&ev);
return 0;
}
void buttonpress(XEvent *ev) {
XButtonEvent *e = &ev->xbutton;
XRaiseWindow(dpy, e->subwindow);
if (e->button == 2)
XMoveResizeWindow(dpy, e->subwindow, 0, 0, sw, sh);
if (e->button != 1 && e->button !=3)
return;
XGrabPointer(dpy, root, True, PointerMotionMask | ButtonReleaseMask,
GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
XWindowAttributes attr;
XGetWindowAttributes(dpy, e->subwindow, &attr);
int xx = e->x_root, yy = e->y_root, px, py;
XEvent ee;
while (!XMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask, &ee)) {
if (ee.type == ButtonRelease) break;
px = xx; py = yy; xx = ee.xbutton.x_root; yy = ee.xbutton.y_root;
if (e->button == 1) { attr.x += xx - px; attr.y += yy - py; }
else if (e->button == 3) {
attr.width += xx - px; attr.height += yy - py;
if (attr.width < 12) attr.width = 12;
else if (attr.height < 12) attr.height = 12;
}
XMoveResizeWindow(dpy, e->subwindow, attr.x, attr.y, attr.width, attr.height);
}
XUngrabPointer(dpy, CurrentTime);
}
void configurerequest(XEvent *ev) {
XConfigureRequestEvent *e = &ev->xconfigurerequest;
XWindowChanges wc;
wc.x = e->x; wc.y = e->y; wc.width = e->width; wc.height = e->height;
wc.sibling = Above; wc.stack_mode = e->detail; wc.border_width = e->border_width;
XConfigureWindow(dpy, e->window, e->value_mask, &wc);
}
void enternotify(XEvent *ev) {
XSetInputFocus(dpy, ev->xcrossing.window, RevertToPointerRoot, CurrentTime);
}
void keypress(XEvent *ev) {
XKeyEvent *e = &ev->xkey;
KeySym sym = XkbKeycodeToKeysym(dpy, e->keycode, 0, 0);
int i;
if (e->state == Mod1Mask && sym == XK_Tab)
XCirculateSubwindowsUp(dpy, root);
else if (e->state == (Mod4Mask|ShiftMask) && sym == XK_Return)
spawn(tmux_client);
else for (i = 0; i < sizeof(bind)/sizeof(bind[0]); ++i)
if (bind[i].sym == sym && bind[i].mod == e->state && bind[i].args)
spawn(bind[i].args);
}
#define MapFocus 0x01
#define MapFull 0x02
void maprequest(XEvent *ev) {
XMapRequestEvent *e = &ev->xmaprequest;
XWindowAttributes wa;
if (!XGetWindowAttributes(dpy, e->window, &wa) || wa.override_redirect)
return;
int flags = MapFocus | MapFull;
XWMHints *wmHint;
if ( (wmHint=XGetWMHints(dpy,e->window)) ) {
if (wmHint->flags & InputHint && !wmHint->input) flags &= ~MapFocus;
XFree(wmHint);
}
XClassHint csHint;
XGetClassHint(dpy, e->window, &csHint);
if (strncmp(csHint.res_name,"Float",5) == 0) flags &= ~MapFull;
if (strncmp(csHint.res_class,"Float",5) == 0) flags &= ~MapFull;
Window parent;
if (XGetTransientForHint(dpy, e->window, &parent)) flags &= ~MapFull;
if (flags & MapFocus) XSelectInput(dpy, e->window, EnterWindowMask);
if (flags & MapFull) XMoveResizeWindow(dpy, e->window, 0, 0, sw, sh);
else XMoveResizeWindow(dpy, e->window, wa.x, wa.y, wa.width, wa.height);
XMapWindow(dpy, e->window);
}