File: new.c

package info (click to toggle)
evilwm 1.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid, stretch
  • size: 332 kB
  • ctags: 426
  • sloc: ansic: 2,997; makefile: 76; sh: 30
file content (429 lines) | stat: -rw-r--r-- 12,149 bytes parent folder | download
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/* evilwm - Minimalist Window Manager for X
 * Copyright (C) 1999-2015 Ciaran Anscomb <evilwm@6809.org.uk>
 * see README for license and other details. */

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "evilwm.h"
#include "log.h"

static void init_geometry(Client *c);
static void reparent(Client *c);
static void update_window_type_flags(Client *c, unsigned int type);
#ifdef XDEBUG
static const char *map_state_string(int map_state);
static const char *gravity_string(int gravity);
static void debug_wm_normal_hints(XSizeHints *size);
#else
# define debug_wm_normal_hints(s)
#endif

void make_new_client(Window w, ScreenInfo *s) {
	Client *c;
	char *name;
	XClassHint *class;
	unsigned int window_type;

	LOG_ENTER("make_new_client(window=%lx)", w);

	XGrabServer(dpy);

	/* First a bit of interaction with the error handler due to X's
	 * tendency to batch event notifications.  We set a global variable to
	 * the id of the window we're initialising then do simple X call on
	 * that window.  If an error is raised by this (and nothing else should
	 * do so as we've grabbed the server), the error handler resets the
	 * variable indicating the window has already disappeared, so we stop
	 * trying to manage it. */
	initialising = w;
	XFetchName(dpy, w, &name);
	XSync(dpy, False);
	/* If 'initialising' is now set to None, that means doing the
	 * XFetchName raised BadWindow - the window has been removed before
	 * we got a chance to grab the server. */
	if (initialising == None) {
		LOG_DEBUG("XError occurred for initialising window - aborting...\n");
		XUngrabServer(dpy);
		LOG_LEAVE();
		return;
	}
	initialising = None;
	LOG_DEBUG("screen=%d\n", s->screen);
	LOG_DEBUG("name=%s\n", name ? name : "Untitled");
	if (name)
		XFree(name);

	window_type = ewmh_get_net_wm_window_type(w);
	/* Don't manage DESKTOP type windows */
	if (window_type & EWMH_WINDOW_TYPE_DESKTOP) {
		XMapWindow(dpy, w);
		XUngrabServer(dpy);
		return;
	}

	c = malloc(sizeof(Client));
	/* Don't crash the window manager, just fail the operation. */
	if (!c) {
		LOG_ERROR("out of memory in new_client; limping onward\n");
		LOG_LEAVE();
		XUngrabServer(dpy);
		return;
	}
	clients_tab_order = list_prepend(clients_tab_order, c);
	clients_mapping_order = list_append(clients_mapping_order, c);
	clients_stacking_order = list_append(clients_stacking_order, c);

	c->screen = s;
	c->window = w;
	c->ignore_unmap = 0;
	c->remove = 0;

	/* Ungrab the X server as soon as possible. Now that the client is
	 * malloc()ed and attached to the list, it is safe for any subsequent
	 * X calls to raise an X error and thus flag it for removal. */
	XUngrabServer(dpy);

	c->border = opt_bw;

	update_window_type_flags(c, window_type);
	init_geometry(c);

#ifdef DEBUG
	{
		struct list *iter;
		int i = 0;
		for (iter = clients_tab_order; iter; iter = iter->next)
			i++;
		LOG_DEBUG("new window %dx%d+%d+%d, wincount=%d\n", c->width, c->height, c->x, c->y, i);
	}
#endif

	XSelectInput(dpy, c->window, ColormapChangeMask | EnterWindowMask | PropertyChangeMask);

	reparent(c);

#ifdef SHAPE
	if (have_shape) {
		XShapeSelectInput(dpy, c->window, ShapeNotifyMask);
		set_shape(c);
	}
#endif

	/* Read instance/class information for client and check against list
	 * built with -app options */
	class = XAllocClassHint();
	if (class) {
		struct list *aiter = applications;
		XGetClassHint(dpy, w, class);
		while (aiter) {
			Application *a = aiter->data;
			if ((!a->res_name || (class->res_name && !strcmp(class->res_name, a->res_name)))
					&& (!a->res_class || (class->res_class && !strcmp(class->res_class, a->res_class)))) {
				if (a->geometry_mask & WidthValue)
					c->width = a->width * c->width_inc;
				if (a->geometry_mask & HeightValue)
					c->height = a->height * c->height_inc;
				if (a->geometry_mask & XValue) {
					if (a->geometry_mask & XNegative)
						c->x = a->x + DisplayWidth(dpy, s->screen)-c->width-c->border;
					else
						c->x = a->x + c->border;
				}
				if (a->geometry_mask & YValue) {
					if (a->geometry_mask & YNegative)
						c->y = a->y + DisplayHeight(dpy, s->screen)-c->height-c->border;
					else
						c->y = a->y + c->border;
				}
				moveresize(c);
				if (a->is_dock) c->is_dock = 1;
#ifdef VWM
				if (a->vdesk != VDESK_NONE) c->vdesk = a->vdesk;
#endif
			}
			aiter = aiter->next;
		}
		XFree(class->res_name);
		XFree(class->res_class);
		XFree(class);
	}
	ewmh_init_client(c);
	ewmh_set_net_client_list(c->screen);
	ewmh_set_net_client_list_stacking(c->screen);

	/* Only map the window frame (and thus the window) if it's supposed
	 * to be visible on this virtual desktop. */
#ifdef VWM
	if (is_fixed(c) || c->vdesk == s->vdesk)
#endif
	{
		client_show(c);
		client_raise(c);
		if (!(window_type & (EWMH_WINDOW_TYPE_DOCK|EWMH_WINDOW_TYPE_NOTIFICATION))) {
			select_client(c);
#ifdef WARP_POINTER
			setmouse(c->window, c->width + c->border - 1,
				 c->height + c->border - 1);
#endif
			discard_enter_events(c);
		}
	}
#ifdef VWM
	else {
		set_wm_state(c, IconicState);
	}
	ewmh_set_net_wm_desktop(c);
#endif
	LOG_LEAVE();
}

/* Calls XGetWindowAttributes, XGetWMHints and XGetWMNormalHints to determine
 * window's initial geometry. */
static void init_geometry(Client *c) {
	long size_flags;
	XWindowAttributes attr;
	unsigned long *eprop;
	unsigned long nitems;
	PropMwmHints *mprop;
#ifdef VWM
	unsigned long *lprop;
#endif

	if ( (mprop = get_property(c->window, mwm_hints, mwm_hints, &nitems)) ) {
		if (nitems >= PROP_MWM_HINTS_ELEMENTS
				&& (mprop->flags & MWM_HINTS_DECORATIONS)
				&& !(mprop->decorations & MWM_DECOR_ALL)
				&& !(mprop->decorations & MWM_DECOR_BORDER)) {
			c->border = 0;
		}
		XFree(mprop);
	}

#ifdef VWM
	c->vdesk = c->screen->vdesk;
	if ( (lprop = get_property(c->window, xa_net_wm_desktop, XA_CARDINAL, &nitems)) ) {
		/* NB, Xlib not only returns a 32bit value in a long (which may
		 * not be 32bits), it also sign extends the 32bit value */
		if (nitems && valid_vdesk(lprop[0] & UINT32_MAX)) {
			c->vdesk = lprop[0] & UINT32_MAX;
		}
		XFree(lprop);
	}
#endif

	get_window_type(c);

	/* Get current window attributes */
	LOG_XENTER("XGetWindowAttributes(window=%lx)", c->window);
	XGetWindowAttributes(dpy, c->window, &attr);
	LOG_XDEBUG("(%s) %dx%d+%d+%d, bw = %d\n", map_state_string(attr.map_state), attr.width, attr.height, attr.x, attr.y, attr.border_width);
	LOG_XLEAVE();
	c->old_border = attr.border_width;
	c->oldw = c->oldh = 0;
	c->cmap = attr.colormap;

	if ( (eprop = get_property(c->window, xa_evilwm_unmaximised_horz, XA_CARDINAL, &nitems)) ) {
		if (nitems == 2) {
			c->oldx = eprop[0];
			c->oldw = eprop[1];
		}
		XFree(eprop);
	}
	if ( (eprop = get_property(c->window, xa_evilwm_unmaximised_vert, XA_CARDINAL, &nitems)) ) {
		if (nitems == 2) {
			c->oldy = eprop[0];
			c->oldh = eprop[1];
		}
		XFree(eprop);
	}

	size_flags = get_wm_normal_hints(c);

	if ((attr.width >= c->min_width) && (attr.height >= c->min_height)) {
	/* if (attr.map_state == IsViewable || (size_flags & (PSize | USSize))) { */
		c->width = attr.width;
		c->height = attr.height;
	} else {
		c->width = c->min_width;
		c->height = c->min_height;
		send_config(c);
	}
	if ((attr.map_state == IsViewable)
			|| (size_flags & (/*PPosition |*/ USPosition))) {
		c->x = attr.x;
		c->y = attr.y;
	} else {
		int xmax = DisplayWidth(dpy, c->screen->screen);
		int ymax = DisplayHeight(dpy, c->screen->screen);
		int x, y;
		get_mouse_position(&x, &y, c->screen->root);
		c->x = (x * (xmax - c->border - c->width)) / xmax;
		c->y = (y * (ymax - c->border - c->height)) / ymax;
		send_config(c);
	}

	LOG_DEBUG("window started as %dx%d +%d+%d\n", c->width, c->height, c->x, c->y);
	if (attr.map_state == IsViewable) {
		/* The reparent that is to come would trigger an unmap event */
		c->ignore_unmap++;
	}
	c->x += c->old_border;
	c->y += c->old_border;
	gravitate_border(c, -c->old_border);
	gravitate_border(c, c->border);
}

static void reparent(Client *c) {
	XSetWindowAttributes p_attr;

	p_attr.border_pixel = c->screen->bg.pixel;
	p_attr.override_redirect = True;
	p_attr.event_mask = ChildMask | ButtonPressMask | EnterWindowMask;
	c->parent = XCreateWindow(dpy, c->screen->root, c->x-c->border, c->y-c->border,
		c->width, c->height, c->border,
		DefaultDepth(dpy, c->screen->screen), CopyFromParent,
		DefaultVisual(dpy, c->screen->screen),
		CWOverrideRedirect | CWBorderPixel | CWEventMask, &p_attr);

	XAddToSaveSet(dpy, c->window);
	XSetWindowBorderWidth(dpy, c->window, 0);
	XReparentWindow(dpy, c->window, c->parent, 0, 0);
	XMapWindow(dpy, c->window);
	grab_button(c->parent, grabmask2, AnyButton);
	grab_button(c->parent, grabmask2 | altmask, AnyButton);
}

/* Get WM_NORMAL_HINTS property */
long get_wm_normal_hints(Client *c) {
	XSizeHints *size;
	long flags;
	long dummy;
	size = XAllocSizeHints();

	LOG_XENTER("XGetWMNormalHints(window=%lx)", c->window);
	XGetWMNormalHints(dpy, c->window, size, &dummy);
	debug_wm_normal_hints(size);
	LOG_XLEAVE();
	flags = size->flags;
	if (flags & PMinSize) {
		c->min_width = size->min_width;
		c->min_height = size->min_height;
	} else {
		c->min_width = c->min_height = 0;
	}
	if (flags & PMaxSize) {
		c->max_width = size->max_width;
		c->max_height = size->max_height;
	} else {
		c->max_width = c->max_height = 0;
	}
	if (flags & PBaseSize) {
		c->base_width = size->base_width;
		c->base_height = size->base_height;
	} else {
		c->base_width = c->min_width;
		c->base_height = c->min_height;
	}
	c->width_inc = c->height_inc = 1;
	if (flags & PResizeInc) {
		c->width_inc = size->width_inc ? size->width_inc : 1;
		c->height_inc = size->height_inc ? size->height_inc : 1;
	}
	if (!(flags & PMinSize)) {
		c->min_width = c->base_width + c->width_inc;
		c->min_height = c->base_height + c->height_inc;
	}
	if (flags & PWinGravity) {
		c->win_gravity_hint = size->win_gravity;
	} else {
		c->win_gravity_hint = NorthWestGravity;
	}
	c->win_gravity = c->win_gravity_hint;
	XFree(size);
	return flags;
}

static void update_window_type_flags(Client *c, unsigned int type) {
	c->is_dock = (type & EWMH_WINDOW_TYPE_DOCK) ? 1 : 0;
}

/* Determine window type and update flags accordingly */
void get_window_type(Client *c) {
	unsigned int type = ewmh_get_net_wm_window_type(c->window);
	update_window_type_flags(c, type);
}

#ifdef XDEBUG
static const char *map_state_string(int map_state) {
	const char *map_states[4] = {
		"IsUnmapped",
		"IsUnviewable",
		"IsViewable",
		"Unknown"
	};
	return ((unsigned int)map_state < 3)
		? map_states[map_state]
		: map_states[3];
}

static const char *gravity_string(int gravity) {
	const char *gravities[12] = {
		"ForgetGravity",
		"NorthWestGravity",
		"NorthGravity",
		"NorthEastGravity",
		"WestGravity",
		"CenterGravity",
		"EastGravity",
		"SouthWestGravity",
		"SouthGravity",
		"SouthEastGravity",
		"StaticGravity",
		"Unknown"
	};
	return ((unsigned int)gravity < 11) ? gravities[gravity] : gravities[11];
}

static void debug_wm_normal_hints(XSizeHints *size) {
	if (size->flags & 15) {
		LOG_XDEBUG("");
		if (size->flags & USPosition) {
			LOG_XDEBUG_("USPosition ");
		}
		if (size->flags & USSize) {
			LOG_XDEBUG_("USSize ");
		}
		if (size->flags & PPosition) {
			LOG_XDEBUG_("PPosition ");
		}
		if (size->flags & PSize) {
			LOG_XDEBUG_("PSize");
		}
		LOG_XDEBUG_("\n");
	}
	if (size->flags & PMinSize) {
		LOG_XDEBUG("PMinSize: min_width = %d, min_height = %d\n", size->min_width, size->min_height);
	}
	if (size->flags & PMaxSize) {
		LOG_XDEBUG("PMaxSize: max_width = %d, max_height = %d\n", size->max_width, size->max_height);
	}
	if (size->flags & PResizeInc) {
		LOG_XDEBUG("PResizeInc: width_inc = %d, height_inc = %d\n",
				size->width_inc, size->height_inc);
	}
	if (size->flags & PAspect) {
		LOG_XDEBUG("PAspect: min_aspect = %d/%d, max_aspect = %d/%d\n",
				size->min_aspect.x, size->min_aspect.y,
				size->max_aspect.x, size->max_aspect.y);
	}
	if (size->flags & PBaseSize) {
		LOG_XDEBUG("PBaseSize: base_width = %d, base_height = %d\n",
				size->base_width, size->base_height);
	}
	if (size->flags & PWinGravity) {
		LOG_XDEBUG("PWinGravity: %s\n", gravity_string(size->win_gravity));
	}
}
#endif