-
Notifications
You must be signed in to change notification settings - Fork 2
/
vswm.c
224 lines (181 loc) · 4.77 KB
/
vswm.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
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <X11/keysym.h>
#include <X11/XF86keysym.h>
#include <X11/XKBlib.h>
#include <X11/Xlib.h>
typedef struct Key Key;
typedef void (*Events)(XEvent *event);
struct Key {
unsigned int modifier;
KeySym keysym;
void (*function)(XEvent *event, char *command);
char *command;
};
static void size(void);
static void grab(void);
static void scan(void);
static void loop(void);
static void enter(XEvent *event);
static void configure(XEvent *event);
static void key(XEvent *event);
static void map(XEvent *event);
static void focus(XEvent *event, char *command);
static void launch(XEvent *event, char *command);
static void destroy(XEvent *event, char *command);
static void refresh(XEvent *event, char *command);
static int ignore(Display *display, XErrorEvent *event);
static Display *display;
static Window root;
static int screen, width, height;
static Key keys[] = {
{ Mod4Mask, XK_Return, launch, "xterm" },
{ Mod4Mask, XK_d, launch, "dmn" },
{ Mod4Mask, XK_b, launch, "firefox" },
{ Mod4Mask, XK_p, launch, "scr" },
{ Mod4Mask | ShiftMask, XK_q, destroy, 0 },
{ Mod4Mask | ShiftMask, XK_r, refresh, 0 },
{ Mod4Mask, XK_Tab, focus, "next" },
{ Mod4Mask | ShiftMask, XK_Tab, focus, "prev" },
{ 0, XF86XK_AudioMute, launch, "pamixer -t" },
{ 0, XF86XK_AudioLowerVolume, launch, "pamixer -d 5" },
{ 0, XF86XK_AudioRaiseVolume, launch, "pamixer -i 5" },
{ 0, XF86XK_MonBrightnessDown, launch, "xbacklight -dec 5" },
{ 0, XF86XK_MonBrightnessUp, launch, "xbacklight -inc 5" },
};
static const Events events[LASTEvent] = {
[ConfigureRequest] = configure,
[EnterNotify] = enter,
[KeyPress] = key,
[MapRequest] = map,
};
void size(void)
{
XWindowAttributes attributes;
if (XGetWindowAttributes(display, root, &attributes)) {
width = attributes.width;
height = attributes.height;
} else {
width = XDisplayWidth(display, screen);
height = XDisplayHeight(display, screen);
}
}
void grab(void)
{
unsigned int i;
for (i = 0; i < sizeof(keys) / sizeof(struct Key); i++)
XGrabKey(display, XKeysymToKeycode(display, keys[i].keysym),
keys[i].modifier, root, True, GrabModeAsync, GrabModeAsync);
}
void scan()
{
unsigned int i, n;
Window r, p, *c;
if (XQueryTree(display, root, &r, &p, &c, &n)) {
for (i = 0; i < n; i++)
XMoveResizeWindow(display, c[i], 0, 0, width, height);
if (c)
XFree(c);
}
}
void loop(void)
{
XEvent event;
while (1 && !XNextEvent(display, &event))
if (events[event.type])
events[event.type](&event);
}
void enter(XEvent *event)
{
Window window = event->xcrossing.window;
XSetInputFocus(display, window, RevertToParent, CurrentTime);
XRaiseWindow(display, window);
}
void configure(XEvent *event)
{
XConfigureRequestEvent *request = &event->xconfigurerequest;
XWindowChanges changes = {
.x = request->x,
.y = request->y,
.width = request->width,
.height = request->height,
.border_width = request->border_width,
.sibling = request->above,
.stack_mode = request->detail,
};
XConfigureWindow(display, request->window, request->value_mask, &changes);
}
void key(XEvent *event)
{
unsigned int i;
KeySym keysym = XkbKeycodeToKeysym(display, event->xkey.keycode, 0, 0);
for (i = 0; i < sizeof(keys) / sizeof(struct Key); i++)
if (keysym == keys[i].keysym && keys[i].modifier == event->xkey.state)
keys[i].function(event, keys[i].command);
}
void map(XEvent *event)
{
Window window = event->xmaprequest.window;
XWindowChanges changes = { .border_width = 0 };
XSelectInput(display, window, StructureNotifyMask | EnterWindowMask);
XConfigureWindow(display, window, CWBorderWidth, &changes);
XMoveResizeWindow(display, window, 0, 0, width, height);
XMapWindow(display, window);
}
void focus(XEvent *event, char *command)
{
(void)event;
int next = command[0] == 'n';
XCirculateSubwindows(display, root, next ? RaiseLowest : LowerHighest);
}
void launch(XEvent *event, char *command)
{
(void)event;
if (fork() == 0) {
if (fork() == 0) {
if (display)
close(XConnectionNumber(display));
setsid();
execl("/bin/sh", "sh", "-c", command, 0);
exit(1);
}
else {
exit(0);
}
}
}
void destroy(XEvent *event, char *command)
{
(void)command;
XSetCloseDownMode(display, DestroyAll);
XKillClient(display, event->xkey.subwindow);
}
void refresh(XEvent *event, char *command)
{
(void)event;
(void)command;
size();
scan();
}
int ignore(Display *display, XErrorEvent *event)
{
(void)display;
(void)event;
return 0;
}
int main(void)
{
if (!(display = XOpenDisplay(0)))
exit(1);
signal(SIGCHLD, SIG_IGN);
XSetErrorHandler(ignore);
screen = XDefaultScreen(display);
root = XDefaultRootWindow(display);
XSelectInput(display, root, SubstructureRedirectMask);
XDefineCursor(display, root, XCreateFontCursor(display, 68));
size();
grab();
scan();
loop();
}