-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreewm.cc
270 lines (227 loc) · 6.34 KB
/
threewm.cc
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
#include "threewm.h"
#include "events.h"
#include "screen.h"
#include "keys.h"
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <X11/cursorfont.h>
#include <X11/Xlib.h>
#include <X11/Xproto.h>
Display *dpy;
Client *current = NULL;
int screen;
Window root;
GC invert_gc;
XFontStruct *font;
Client *head_client;
Atom xa_wm_state;
Atom xa_wm_change_state;
Atom xa_wm_protos;
Atom xa_wm_delete;
Atom xa_wm_cmapwins;
Cursor move_curs;
Cursor resize_curs;
int opt_vd = DEF_VD;
int opt_bw = DEF_BW;
XColor fg, bg, fc;
int vdesk = 1;
char throwUnmaps;
Client **prev_focused;
static const char *opt_display = "";
static const char *opt_font = DEF_FONT;
static const char *opt_fg = DEF_FG;
static const char *opt_bg = DEF_BG;
static const char *opt_fc = DEF_FC;
static int wm_running;
static void cleanup(void)
{
while(head_client)
remove_client(head_client, QUITTING);
XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
XInstallColormap(dpy, DefaultColormap(dpy, screen));
XCloseDisplay(dpy);
}
void quit_nicely(void)
{
cleanup();
exit(0);
}
static void handle_signal(int signo)
{
if (signo == SIGCHLD) {
wait(NULL);
return;
}
quit_nicely();
}
static void throwAllUnmapEvent()
{
XEvent ev;
/* throw all event */
while (XCheckTypedEvent(dpy, UnmapNotify, &ev)) {}
}
int handle_xerror(Display *d, XErrorEvent *e)
{
Client *c = find_client(e->resourceid);
if (e->error_code == BadAccess &&
e->request_code == X_ChangeWindowAttributes) {
exit(1);
}
fprintf(stderr, "XError %x %d ", e->error_code, e->request_code);
if (c && e->error_code != BadWindow)
remove_client(c, NOT_QUITTING);
return 0;
}
int ignore_xerror(Display *d, XErrorEvent *e)
{
return 0;
}
static void setup_display(void)
{
XGCValues gv;
XSetWindowAttributes attr;
XColor dummy;
dpy = XOpenDisplay(opt_display);
if (!dpy) {
fprintf(stderr, "XOpenDisplay() failed\n");
exit(1);
}
XSetErrorHandler(handle_xerror);
screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen);
xa_wm_state = XInternAtom(dpy, "WM_STATE", False);
xa_wm_change_state = XInternAtom(dpy, "WM_CHANGE_STATE", False);
xa_wm_protos = XInternAtom(dpy, "WM_PROTOCOLS", False);
xa_wm_delete = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
XAllocNamedColor(dpy, DefaultColormap(dpy, screen), opt_fg, &fg, &dummy);
XAllocNamedColor(dpy, DefaultColormap(dpy, screen), opt_bg, &bg, &dummy);
XAllocNamedColor(dpy, DefaultColormap(dpy, screen), opt_fc, &fc, &dummy);
font = XLoadQueryFont(dpy, opt_font);
if (!font)
font = XLoadQueryFont(dpy, "*");
move_curs = XCreateFontCursor(dpy, XC_fleur);
resize_curs = XCreateFontCursor(dpy, XC_plus);
gv.function = GXinvert;
gv.subwindow_mode = IncludeInferiors;
gv.line_width = 1;
gv.font = font->fid;
invert_gc = XCreateGC(dpy, root,
GCFunction | GCSubwindowMode | GCLineWidth | GCFont, &gv);
attr.event_mask = ChildMask | PropertyChangeMask | ButtonMask;
XChangeWindowAttributes(dpy, root, CWEventMask, &attr);
/* Unfortunately grabbing AnyKey under Solaris seems not to work */
/* XGrabKey(dpy, AnyKey, ControlMask|Mod1Mask, root, True, GrabModeAsync, GrabModeAsync); */
/* So now I grab each and every one. */
XGrabKey(dpy, AnyKey, KEY_EVENT_MODIFIERS,
root, True, GrabModeAsync, GrabModeAsync);
XGrabKey(dpy, AnyKey, KEY_EVENT_MODIFIERS | ShiftMask,
root, True, GrabModeAsync, GrabModeAsync);
XGrabKey(dpy, XKeysymToKeycode(dpy, XK_Tab), Mod1Mask, root, True, GrabModeAsync, GrabModeAsync);
}
static void scan_windows(void)
{
unsigned int nwins;
Window dw1, dw2, *wins;
XWindowAttributes attr;
XQueryTree(dpy, root, &dw1, &dw2, &wins, &nwins);
for (unsigned int i = 0; i < nwins; i++) {
XGetWindowAttributes(dpy, wins[i], &attr);
if (!attr.override_redirect && attr.map_state == IsViewable)
make_new_client(wins[i]);
}
XFree(wins);
}
int main(int argc, char *argv[])
{
struct sigaction act;
XEvent ev;
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-fn") && i+1<argc)
opt_font = argv[++i];
else if (!strcmp(argv[i], "-display") && i+1<argc)
opt_display = argv[++i];
else if (!strcmp(argv[i], "-fg") && i+1<argc)
opt_fg = argv[++i];
else if (!strcmp(argv[i], "-bg") && i+1<argc)
opt_bg = argv[++i];
else if (!strcmp(argv[i], "-fc") && i+1<argc)
opt_fc = argv[++i];
else if (!strcmp(argv[i], "-bw") && i+1<argc)
opt_bw = atoi(argv[++i]);
else if (!strcmp(argv[i], "-vd") && i+1<argc) {
opt_vd = atoi(argv[++i]);
} else {
printf("usage: threewm [-display display] [-vd num]\n");
printf("\t[-fg colour] [-bg colour] [-bw borderwidth]\n");
exit(2);
}
}
prev_focused = (Client **)calloc(opt_vd, sizeof(Client *));
if (!prev_focused) {
fprintf(stderr, "error at allocating prev_focused\n");
exit(1);
}
act.sa_handler = handle_signal;
sigemptyset(&act.sa_mask);
#ifdef SA_NOCLDSTOP
act.sa_flags = SA_NOCLDSTOP; /* don't care about STOP, CONT */
#else
act.sa_flags = 0;
#endif
sigaction(SIGTERM, &act, NULL);
sigaction(SIGINT, &act, NULL);
sigaction(SIGHUP, &act, NULL);
sigaction(SIGCHLD, &act, NULL);
signal(SIGPIPE, SIG_IGN);
setup_display();
scan_windows();
throwAllUnmapEvent();
throwUnmaps = 0;
wm_running = 1;
/* main event loop here */
while (wm_running) {
XNextEvent(dpy, &ev);
switch (ev.type) {
case KeyPress:
handle_key_event(&ev.xkey);
break;
case ButtonPress:
handle_button_event(&ev);
break;
case ButtonRelease:
XSync(dpy, False);
XAllowEvents(dpy, ReplayPointer, CurrentTime);
XSync(dpy, False);
break;
case ConfigureRequest:
handle_configure_request(&ev.xconfigurerequest);
break;
case MapRequest:
handle_map_request(&ev.xmaprequest);
break;
case ClientMessage:
handle_client_message(&ev.xclient);
break;
case EnterNotify:
handle_enter_event(&ev.xcrossing);
break;
case PropertyNotify:
handle_property_change(&ev.xproperty);
break;
case UnmapNotify:
if (!throwUnmaps)
handle_unmap_event(&ev.xunmap);
break;
case DestroyNotify:
handle_destroy_event(&ev.xdestroywindow);
break;
}
force_set_focus();
}
printf("quitting threewm\n");
return 1;
}