summaryrefslogtreecommitdiff
path: root/wm/manage.c
blob: 68d85a8bbf7a1572a40f265b80102550c6dd3c60 (plain)
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
#include <stdlib.h>
#include <stdio.h>
#include "xcb_wm.h"

Table *byChild = 0;
Table *byParent = 0;

void manageWindow(PropertyHandlers *prophs, XCBConnection *c, XCBWINDOW window, WindowAttributes wa)
{
	XCBDRAWABLE d = { window };
	XCBGetGeometryCookie geomc;
	XCBGetGeometryRep *geom;
	XCBGetWindowAttributesRep *attr = 0;
	if(wa.tag == TAG_COOKIE)
	{
		attr = XCBGetWindowAttributesReply(c, wa.u.cookie, 0);
		if(!attr)
			return;
		if(attr->map_state != XCBMapStateViewable)
		{
			printf("Window 0x%08lx is not mapped. Ignoring.\n", window.xid);
			free(attr);
			return;
		}
		wa.tag = TAG_VALUE;
		wa.u.override_redirect = attr->override_redirect;
	}
	if(!wa.u.override_redirect && TableGet(byChild, window.xid))
	{
		printf("Window 0x%08lx already managed. Ignoring.\n", window.xid);
		free(attr);
		return;
	}
	if(wa.u.override_redirect)
	{
		printf("Window 0x%08lx has override-redirect set. Ignoring.\n", window.xid);
		free(attr);
		return;
	}
	geomc = XCBGetGeometry(c, d);
	if(!attr)
	{
		wa.tag = TAG_COOKIE;
		wa.u.cookie = XCBGetWindowAttributes(c, window);
		attr = XCBGetWindowAttributesReply(c, wa.u.cookie, 0);
	}
	geom = XCBGetGeometryReply(c, geomc, 0);
	if(attr && geom)
	{
		reparentWindow(c, window, attr->visual, geom->root, geom->depth, geom->x, geom->y, geom->width, geom->height);
		PropertyChanged(prophs, XCBPropertyNewValue, window, WM_NAME);
	}
	free(attr);
	free(geom);
}

int handleMapNotifyEvent(void *prophs, XCBConnection *c, XCBMapNotifyEvent *e)
{
	WindowAttributes wa = { TAG_VALUE };
	wa.u.override_redirect = e->override_redirect;
	printf("MapNotify for 0x%08lx.\n", e->window.xid);
	manageWindow(prophs, c, e->window, wa);
	return 1;
}

int handleUnmapNotifyEvent(void *data, XCBConnection *c, XCBUnmapNotifyEvent *e)
{
	ClientWindow *client = TableRemove(byChild, e->event.xid);
	XCBWINDOW root;
	printf("UnmapNotify for 0x%08lx (received from 0x%08lx): ", e->window.xid, e->event.xid);
	if(!client)
	{
		printf("not a managed window. Ignoring.\n");
		return 0;
	}

	root = XCBConnSetupSuccessRepRootsIter(XCBGetSetup(c)).data->root;
	printf("child of 0x%08lx.\n", client->parent.xid);
	XCBReparentWindow(c, client->child, root, 0, 0);
	XCBDestroyWindow(c, client->parent);
	XCBFlush(c);
	TableRemove(byParent, client->parent.xid);
	free(client);
	return 1;
}

void manageExistingWindows(XCBConnection *c, PropertyHandlers *prophs, XCBWINDOW root)
{
	XCBQueryTreeCookie wintree;
	XCBQueryTreeRep *rep;
	int i, len;
	XCBWINDOW *children;
	XCBGetWindowAttributesCookie *cookies;

	wintree = XCBQueryTree(c, root);
	rep = XCBQueryTreeReply(c, wintree, 0);
	if(!rep)
		return;
	len = XCBQueryTreeChildrenLength(rep);
	cookies = malloc(len * sizeof(*cookies));
	if(!cookies)
	{
		free(rep);
		return;
	}
	children = XCBQueryTreeChildren(rep);
	for(i = 0; i < len; ++i)
		cookies[i] = XCBGetWindowAttributes(c, children[i]);
	for(i = 0; i < len; ++i)
	{
		WindowAttributes wa = { TAG_COOKIE, { cookies[i] } };
		manageWindow(prophs, c, children[i], wa);
	}
	free(rep);
}