/
client.c
352 lines (287 loc) · 9.91 KB
/
client.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
/*
* Copyright (c) 2012 Chris Diamand
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include "policy.h"
#include "wm.h"
#include "xatoms.h"
static void print_clients(struct WM_t *W)
{
int i;
msg("%d clients:\n", W->nclients);
for (i = 0; i < W->nclients; i++)
msg(" -> \'%s\'\n", W->clients[i]->name);
}
static void set_net_wm_status(struct WM_t *W, struct wmclient *C)
{
Atom props[2];
props[0] = get_atom(W, "_NET_WM_STATE_MAXIMIZED_HORZ");
props[1] = get_atom(W, "_NET_WM_STATE_MAXIMIZED_VERT");
XChangeProperty(W->XDisplay, C->win, get_atom(W, "_NET_WM_STATE"),
XA_ATOM, 32, PropModeReplace, (unsigned char *) props,
C->fullscreen ? sizeof(props) / sizeof(Atom) : 0);
}
/* Move all clients with focus position between start end down the list,
overwriting the one at end */
static void move_down_client_list(struct WM_t *W, int start, int end)
{
int i;
for (i = end - 1; i >= start; i--)
W->clients[i + 1] = W->clients[i];
W->clients[start] = NULL;
}
static void grabkey(struct WM_t *W, struct wmclient *C, KeySym sym, unsigned int mods)
{
KeyCode code = XKeysymToKeycode(W->XDisplay, sym);
XGrabKey(W->XDisplay, code, mods, C->win,
0, GrabModeAsync, GrabModeAsync);
XGrabKey(W->XDisplay, code, mods | LockMask, C->win,
0, GrabModeAsync, GrabModeAsync);
}
void client_select_events(struct WM_t *W, struct wmclient *C)
{
XSelectInput(W->XDisplay, C->win, StructureNotifyMask);
/* Grab ALT+click events for moving windows */
XGrabButton(W->XDisplay, Button1, Mod1Mask, C->win, 0,
ButtonPressMask | ButtonReleaseMask | ButtonMotionMask,
GrabModeAsync, GrabModeSync, None, None);
/* Grab Shift+alt+click events for moving windows */
XGrabButton(W->XDisplay, Button1, Mod1Mask | ShiftMask, C->win, 0,
ButtonPressMask | ButtonReleaseMask | ButtonMotionMask,
GrabModeAsync, GrabModeSync, None, None);
/* Grab for any click so if it is clicked on it can be refocused */
XGrabButton(W->XDisplay, Button1, 0, C->win, 0, ButtonPressMask,
GrabModeAsync, GrabModeSync, None, None);
/* Alt-Tab */
grabkey(W, C, XK_Tab, Mod1Mask);
/* Shift-alt-enter for menu */
grabkey(W, C, XK_Return, ShiftMask | Mod1Mask);
/* Shift-alt-F for fullscreen */
grabkey(W, C, XK_f, ShiftMask | Mod1Mask);
/* Shift-alt-arrows for tiling */
grabkey(W, C, XK_Up, ShiftMask | Mod1Mask);
grabkey(W, C, XK_Down, ShiftMask | Mod1Mask);
grabkey(W, C, XK_Left, ShiftMask | Mod1Mask);
grabkey(W, C, XK_Right, ShiftMask | Mod1Mask);
}
static void set_border_colour(struct WM_t *W, struct wmclient *C, int focus)
{
if (focus)
XSetWindowBorder(W->XDisplay, C->win, W->focus_border_col);
else
XSetWindowBorder(W->XDisplay, C->win, W->unfocus_border_col);
}
/* Set a 1-pixel border and position it at the size/pos in C */
static void set_size_pos_border(struct WM_t *W, struct wmclient *C)
{
/* Add a border */
XSetWindowBorderWidth(W->XDisplay, C->win, W->prefs.bw);
/* Set the colour differently if it's at the top of the focus stack */
if (W->clients[0] == C)
set_border_colour(W, C, 1);
else
set_border_colour(W, C, 0);
XMoveResizeWindow(W->XDisplay, C->win, C->x, C->y, C->w, C->h);
/* XChangeProperty? */
}
static int get_client_index(struct WM_t *W, Window id)
{
int i;
for (i = 0; i < W->nclients; i++)
{
struct wmclient *C = W->clients[i];
/* Don't test null entries */
if (C != NULL)
{
if (C->win == id)
return i;
}
}
return -1;
}
void client_focus(struct WM_t *W, struct wmclient *C)
{
int oldidx;
struct wmclient *old;
/* Don't do anything if there's nothing to focus */
if (W->nclients == 0)
return;
oldidx = get_client_index(W, C->win);
old = W->clients[0];
move_down_client_list(W, 0, oldidx);
W->clients[0] = C;
/* Unfocus the old window */
/* Re-enable grabbing for click events */
XGrabButton(W->XDisplay, Button1, 0, old->win, 0, ButtonPressMask|ButtonReleaseMask|PointerMotionMask,
GrabModeAsync, GrabModeSync, None, None);
/* Make the border boring */
set_border_colour(W, old, 0);
set_border_colour(W, C, 1);
XRaiseWindow(W->XDisplay, C->win);
XSetInputFocus(W->XDisplay, C->win, RevertToPointerRoot, CurrentTime);
XUngrabButton(W->XDisplay, Button1, 0, C->win);
refresh_current_head(W);
}
/* Move and resize a window, saving the new dimensions. Negative sizes mean
* maximise but still with a border */
void client_moveresize(struct WM_t *W, struct wmclient *C, int x, int y, int w, int h)
{
/* We can't move fullscreen windows so set it to its original size */
if (C->fullscreen)
{
C->fullscreen = 0;
set_size_pos_border(W, C);
}
/* Make sure it's smaller than the screen */
if (w > curr_width(W) || w < 0)
w = curr_width(W) - 2 * W->prefs.bw;
if (h > curr_height(W) || h < 0)
h = curr_height(W) - 2 * W->prefs.bw;
C->x = x;
C->y = y;
C->w = w;
C->h = h;
XMoveResizeWindow(W->XDisplay, C->win, x, y, w, h);
}
static void maximise(struct WM_t *W, struct wmclient *C)
{
msg("client_MAX: %s\n", C->name);
C->fullscreen = 1;
XSetWindowBorderWidth(W->XDisplay, C->win, 0);
XMoveResizeWindow(W->XDisplay, C->win,
curr_head_x(W), curr_head_y(W),
curr_width(W), curr_height(W));
set_net_wm_status(W, C);
}
static void unmaximise(struct WM_t *W, struct wmclient *C)
{
msg("client_unMAX: %s\n", C->name);
C->fullscreen = 0;
set_size_pos_border(W, C);
set_net_wm_status(W, C);
}
void client_togglefullscreen(struct WM_t *W, struct wmclient *C)
{
if (!C->fullscreen)
maximise(W, C);
else
unmaximise(W, C);
}
static void get_pid(struct WM_t *W, struct wmclient *C)
{
Atom NetWM_PID = XInternAtom(W->XDisplay, "_NET_WM_PID", 0);
Atom actual_type_return;
int actual_format_return;
unsigned long nitems_return, bytes_after_return;
unsigned char *prop_return;
if (NetWM_PID == None)
{
msg("get_pid(): Couldn't get _NET_WM_PID atom\n");
C->pid = 0;
return;
}
XGetWindowProperty(W->XDisplay, C->win, NetWM_PID, /* Get the _NET_WM_PID property */
0, /* Data offset 0 */
1, /* Length 1x32 bits */
0, /* Don't delete it afterwards */
AnyPropertyType, /* Atom reg_type, whatever that is */
&actual_type_return, &actual_format_return,
&nitems_return, &bytes_after_return,
&prop_return); /* The actual data */
if (nitems_return > 0 && prop_return)
{
C->pid = *( (int *) prop_return );
XFree(prop_return);
}
else /* It doesn't have a PID property */
C->pid = 0;
}
static int registering_existing_windows = 0;
/* Register a client, steal its border, grap events, etc */
void client_register(struct WM_t *W, Window xwindow_id)
{
struct wmclient *C = malloc(sizeof(*C));
XFetchName(W->XDisplay, xwindow_id, &(C->name));
if (!(C->name))
C->name = strdup("<notitle>");
msg("Registering \'%s\':\n", C->name);
C->win = xwindow_id;
decide_new_window_size_pos(W, C);
/* Unless it's a window found at startup, move it
* to the current head/screen */
if (!registering_existing_windows)
{
C->x += curr_head_x(W);
C->y += curr_head_y(W);
}
C->fullscreen = 0;
XMapWindow(W->XDisplay, C->win);
set_size_pos_border(W, C);
client_select_events(W, C);
get_pid(W, C);
W->clients[W->nclients++] = C;
client_focus(W, C);
XFlush(W->XDisplay);
print_clients(W);
}
void client_find_open_windows(struct WM_t *W)
{
Window *children, root_ret, par_ret;
unsigned int n, i;
XQueryTree(W->XDisplay, W->rootWindow, &root_ret, &par_ret, &children, &n);
registering_existing_windows = 1;
for (i = 0; i < n; i++)
client_register(W, children[i]);
registering_existing_windows = 0;
XFree(children);
}
/* Find a wmclient structure from its window ID */
struct wmclient *client_from_window(struct WM_t *W, Window id)
{
int idx = get_client_index(W, id);
if (idx >= 0)
return W->clients[idx];
return NULL;
}
void client_remove(struct WM_t *W, struct wmclient *C)
{
int idx = get_client_index(W, C->win), i;
if (W->clients[idx] != C)
{
msg("client_remove: somehow get_client_index failed! :o\n");
return;
}
W->clients[idx] = NULL;
/* Update all the focus numbers, i.e. decrease (bring forward) all the windows
with bigger focus numbers. */
for (i = idx; i < W->nclients; i++)
W->clients[i] = W->clients[i + 1];
W->nclients--;
msg("Removing client \'%s\'\n", C->name);
free(C->name);
free(C);
msg("About to print\n");
print_clients(W);
msg("Printed, about to focus\n");
client_focus(W, W->clients[0]);
msg("Focused\n");
}