forked from nick-gravgaard/windowlab
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.c
404 lines (363 loc) · 11.9 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
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
/* WindowLab - an X11 window manager
* Copyright (c) 2001-2010 Nick Gravgaard
* me at nickgravgaard.com
* http://nickgravgaard.com/windowlab/
*
* 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 2
* of the License, or 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <X11/Xatom.h>
#include "windowlab.h"
Client *find_client(Window w, int mode)
{
Client *c = head_client;
if (mode == FRAME)
{
while (c != NULL)
{
if (c->frame == w)
{
return c;
}
c = c->next;
}
}
else // WINDOW
{
while (c != NULL)
{
if (c->window == w)
{
return c;
}
c = c->next;
}
}
return NULL;
}
/* Attempt to follow the ICCCM by explicitly specifying 32 bits for
* this property. Does this goof up on 64 bit systems? */
void set_wm_state(Client *c, int state)
{
CARD32 data[2];
data[0] = state;
data[1] = None; //Icon? We don't need no steenking icon.
XChangeProperty(dsply, c->window, wm_state, wm_state, 32, PropModeReplace, (unsigned char *)data, 2);
}
/* If we can't find a WM_STATE we're going to have to assume
* Withdrawn. This is not exactly optimal, since we can't really
* distinguish between the case where no WM has run yet and when the
* state was explicitly removed (Clients are allowed to either set the
* atom to Withdrawn or just remove it... yuck.) */
long get_wm_state(Client *c)
{
Atom real_type;
int real_format;
long state = WithdrawnState;
unsigned long items_read, items_left;
unsigned char *data;
if (XGetWindowProperty(dsply, c->window, wm_state, 0L, 2L, False, wm_state, &real_type, &real_format, &items_read, &items_left, &data) == Success && items_read)
{
state = *(long *)data;
XFree(data);
}
return state;
}
/* This will need to be called whenever we update our Client stuff.
* Yeah, yeah, stop yelling at me about OO. */
void send_config(Client *c)
{
XConfigureEvent ce;
ce.type = ConfigureNotify;
ce.event = c->window;
ce.window = c->window;
ce.x = c->x;
ce.y = c->y;
ce.width = c->width;
ce.height = c->height;
ce.border_width = 0;
ce.above = None;
ce.override_redirect = 0;
XSendEvent(dsply, c->window, False, StructureNotifyMask, (XEvent *)&ce);
}
/* After pulling my hair out trying to find some way to tell if a
* window is still valid, I've decided to instead carefully ignore any
* errors raised by this function. We know that the X calls are, and
* we know the only reason why they could fail -- a window has removed
* itself completely before the Unmap and Destroy events get through
* the queue to us. It's not absolutely perfect, but it works.
*
* The 'withdrawing' argument specifies if the client is actually
* (destroying itself||being destroyed by us) or if we are merely
* cleaning up its data structures when we exit mid-session. */
void remove_client(Client *c, int mode)
{
Client *p;
XGrabServer(dsply);
XSetErrorHandler(ignore_xerror);
#ifdef DEBUG
err("removing %s, %d: %d left", c->name, mode, XPending(dsply));
#endif
if (mode == WITHDRAW)
{
set_wm_state(c, WithdrawnState);
}
else //REMAP
{
XMapWindow(dsply, c->window);
}
gravitate(c, REMOVE_GRAVITY);
XReparentWindow(dsply, c->window, root, c->x, c->y);
#ifdef MWM_HINTS
if (c->has_border)
{
XSetWindowBorderWidth(dsply, c->window, 1);
}
#else
XSetWindowBorderWidth(dsply, c->window, 1);
#endif
#ifdef XFT
XftDrawDestroy(c->xftdraw);
#endif
XRemoveFromSaveSet(dsply, c->window);
XDestroyWindow(dsply, c->frame);
if (head_client == c)
{
head_client = c->next;
}
else
{
for (p = head_client; p && p->next; p = p->next)
{
if (p->next == c)
{
p->next = c->next;
}
}
}
if (c->size)
{
XFree(c->size);
}
if (c == fullscreen_client)
{
fullscreen_client = NULL;
}
if (c == focused_client)
{
focused_client = NULL;
check_focus(get_prev_focused());
}
free(c);
XSync(dsply, False);
XSetErrorHandler(handle_xerror);
XUngrabServer(dsply);
redraw_taskbar();
}
void redraw(Client *c)
{
if (c == fullscreen_client)
{
return;
}
#ifdef MWM_HINTS
if (!c->has_title)
{
return;
}
#endif
XDrawLine(dsply, c->frame, border_gc, 0, BARHEIGHT() - DEF_BORDERWIDTH + DEF_BORDERWIDTH / 2, c->width, BARHEIGHT() - DEF_BORDERWIDTH + DEF_BORDERWIDTH / 2);
// clear text part of bar
if (c == focused_client)
{
XFillRectangle(dsply, c->frame, active_gc, 0, 0, c->width - ((BARHEIGHT() - DEF_BORDERWIDTH) * 3), BARHEIGHT() - DEF_BORDERWIDTH);
}
else
{
XFillRectangle(dsply, c->frame, inactive_gc, 0, 0, c->width - ((BARHEIGHT() - DEF_BORDERWIDTH) * 3), BARHEIGHT() - DEF_BORDERWIDTH);
}
if (!c->trans && c->name != NULL)
{
#ifdef XFT
XftDrawStringUtf8(c->xftdraw, &xft_detail, xftfont, SPACE, SPACE + xftfont->ascent, (XftChar8 *)c->name, strlen(c->name));
#else
XDrawString(dsply, c->frame, text_gc, SPACE, SPACE + font->ascent, c->name, strlen(c->name));
#endif
}
if (c == focused_client)
{
draw_hide_button(c, &text_gc, &active_gc);
draw_toggledepth_button(c, &text_gc, &active_gc);
draw_close_button(c, &text_gc, &active_gc);
}
else
{
draw_hide_button(c, &text_gc, &inactive_gc);
draw_toggledepth_button(c, &text_gc, &inactive_gc);
draw_close_button(c, &text_gc, &inactive_gc);
}
}
/* Window gravity is a mess to explain, but we don't need to do much
* about it since we're using X borders. For NorthWest et al, the top
* left corner of the window when there is no WM needs to match up
* with the top left of our fram once we manage it, and likewise with
* SouthWest and the bottom right (these are the only values I ever
* use, but the others should be obvious). Our titlebar is on the top
* so we only have to adjust in the first case. */
void gravitate(Client *c, int multiplier)
{
int dy = 0;
int gravity = (c->size->flags & PWinGravity) ? c->size->win_gravity : NorthWestGravity;
switch (gravity)
{
case NorthWestGravity:
case NorthEastGravity:
case NorthGravity:
dy = BARHEIGHT();
break;
case CenterGravity:
dy = BARHEIGHT()/2;
break;
}
c->y += multiplier * dy;
}
/* Well, the man pages for the shape extension say nothing, but I was
* able to find a shape.PS.Z on the x.org FTP site. What we want to do
* here is make the window shape be a boolean OR (or union, if you
* prefer) of the client's shape and our titlebar. The titlebar
* requires both a bound and a clip because it has a border -- the X
* server will paint the border in the region between the two. (I knew
* that using X borders would get me eventually... ;-)) */
#ifdef SHAPE
void set_shape(Client *c)
{
int n, order;
XRectangle temp, *dummy;
dummy = XShapeGetRectangles(dsply, c->window, ShapeBounding, &n, &order);
if (n > 1)
{
XShapeCombineShape(dsply, c->frame, ShapeBounding, 0, BARHEIGHT(), c->window, ShapeBounding, ShapeSet);
temp.x = -BORDERWIDTH(c);
temp.y = -BORDERWIDTH(c);
temp.width = c->width + (2 * BORDERWIDTH(c));
temp.height = BARHEIGHT() + BORDERWIDTH(c);
XShapeCombineRectangles(dsply, c->frame, ShapeBounding, 0, 0, &temp, 1, ShapeUnion, YXBanded);
temp.x = 0;
temp.y = 0;
temp.width = c->width;
temp.height = BARHEIGHT() - BORDERWIDTH(c);
XShapeCombineRectangles(dsply, c->frame, ShapeClip, 0, BARHEIGHT(), &temp, 1, ShapeUnion, YXBanded);
c->has_been_shaped = 1;
}
else
{
if (c->has_been_shaped)
{
// I can't find a 'remove all shaping' function...
temp.x = -BORDERWIDTH(c);
temp.y = -BORDERWIDTH(c);
temp.width = c->width + (2 * BORDERWIDTH(c));
temp.height = c->height + BARHEIGHT() + (2 * BORDERWIDTH(c));
XShapeCombineRectangles(dsply, c->frame, ShapeBounding, 0, 0, &temp, 1, ShapeSet, YXBanded);
}
}
XFree(dummy);
}
#endif
void check_focus(Client *c)
{
if (c != NULL)
{
XSetInputFocus(dsply, c->window, RevertToNone, CurrentTime);
XInstallColormap(dsply, c->cmap);
} else {
XSetInputFocus(dsply, PointerRoot, RevertToPointerRoot, CurrentTime);
}
if (c != focused_client)
{
Client *old_focused = focused_client;
focused_client = c;
focus_count++;
if (c != NULL)
{
c->focus_order = focus_count;
redraw(c);
}
if (old_focused != NULL)
{
redraw(old_focused);
}
redraw_taskbar();
}
}
Client *get_prev_focused(void)
{
Client *c = head_client;
Client *prev_focused = NULL;
unsigned int highest = 0;
while (c != NULL)
{
if (!c->hidden && c->focus_order > highest)
{
highest = c->focus_order;
prev_focused = c;
}
c = c->next;
}
return prev_focused;
}
void draw_hide_button(Client *c, GC *detail_gc, GC *background_gc)
{
int x, topleft_offset;
x = c->width - ((BARHEIGHT() - DEF_BORDERWIDTH) * 3);
topleft_offset = (BARHEIGHT() / 2) - 5; // 5 being ~half of 9
XFillRectangle(dsply, c->frame, *background_gc, x, 0, BARHEIGHT() - DEF_BORDERWIDTH, BARHEIGHT() - DEF_BORDERWIDTH);
XDrawLine(dsply, c->frame, *detail_gc, x + topleft_offset + 4, topleft_offset + 2, x + topleft_offset + 4, topleft_offset + 0);
XDrawLine(dsply, c->frame, *detail_gc, x + topleft_offset + 6, topleft_offset + 2, x + topleft_offset + 7, topleft_offset + 1);
XDrawLine(dsply, c->frame, *detail_gc, x + topleft_offset + 6, topleft_offset + 4, x + topleft_offset + 8, topleft_offset + 4);
XDrawLine(dsply, c->frame, *detail_gc, x + topleft_offset + 6, topleft_offset + 6, x + topleft_offset + 7, topleft_offset + 7);
XDrawLine(dsply, c->frame, *detail_gc, x + topleft_offset + 4, topleft_offset + 6, x + topleft_offset + 4, topleft_offset + 8);
XDrawLine(dsply, c->frame, *detail_gc, x + topleft_offset + 2, topleft_offset + 6, x + topleft_offset + 1, topleft_offset + 7);
XDrawLine(dsply, c->frame, *detail_gc, x + topleft_offset + 2, topleft_offset + 4, x + topleft_offset + 0, topleft_offset + 4);
XDrawLine(dsply, c->frame, *detail_gc, x + topleft_offset + 2, topleft_offset + 2, x + topleft_offset + 1, topleft_offset + 1);
}
void draw_toggledepth_button(Client *c, GC *detail_gc, GC *background_gc)
{
int x, topleft_offset;
x = c->width - ((BARHEIGHT() - DEF_BORDERWIDTH) * 2);
topleft_offset = (BARHEIGHT() / 2) - 6; // 6 being ~half of 11
XFillRectangle(dsply, c->frame, *background_gc, x, 0, BARHEIGHT() - DEF_BORDERWIDTH, BARHEIGHT() - DEF_BORDERWIDTH);
XDrawRectangle(dsply, c->frame, *detail_gc, x + topleft_offset, topleft_offset, 7, 7);
XDrawRectangle(dsply, c->frame, *detail_gc, x + topleft_offset + 3, topleft_offset + 3, 7, 7);
}
void draw_close_button(Client *c, GC *detail_gc, GC *background_gc)
{
int x, topleft_offset;
x = c->width - (BARHEIGHT() - DEF_BORDERWIDTH);
topleft_offset = (BARHEIGHT() / 2) - 5; // 5 being ~half of 9
XFillRectangle(dsply, c->frame, *background_gc, x, 0, BARHEIGHT() - DEF_BORDERWIDTH, BARHEIGHT() - DEF_BORDERWIDTH);
XDrawLine(dsply, c->frame, *detail_gc, x + topleft_offset + 1, topleft_offset, x + topleft_offset + 8, topleft_offset + 7);
XDrawLine(dsply, c->frame, *detail_gc, x + topleft_offset + 1, topleft_offset + 1, x + topleft_offset + 7, topleft_offset + 7);
XDrawLine(dsply, c->frame, *detail_gc, x + topleft_offset, topleft_offset + 1, x + topleft_offset + 7, topleft_offset + 8);
XDrawLine(dsply, c->frame, *detail_gc, x + topleft_offset, topleft_offset + 7, x + topleft_offset + 7, topleft_offset);
XDrawLine(dsply, c->frame, *detail_gc, x + topleft_offset + 1, topleft_offset + 7, x + topleft_offset + 7, topleft_offset + 1);
XDrawLine(dsply, c->frame, *detail_gc, x + topleft_offset + 1, topleft_offset + 8, x + topleft_offset + 8, topleft_offset + 1);
}
void
update_title(Client *c) {
if (!gettextprop(c->window, net_wm_name, c->name, sizeof c->name))
gettextprop(c->window, XA_WM_NAME, c->name, sizeof c->name);
if (c->name[0] == '\0') /* hack to mark broken clients */
strcpy(c->name, "broken");
}