summaryrefslogtreecommitdiff
path: root/src/propmon.c
blob: 30b980225d82deb9a5bb2ac167aa07c03d622cab (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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#include "app.h"


static xcb_connection_t *conn;
static GtkTreeStore *store;
xcb_window_t root;

static void
setup_x(const gchar *display)
{
    const xcb_setup_t *setup;
    xcb_screen_iterator_t iter;
    uint32_t mask, list;

    make_connection(display, &conn, NULL);

    setup = xcb_get_setup(conn);
    iter = xcb_setup_roots_iterator(setup);
    root = iter.data->root;

    atom_init(conn);

    /* Select for root window events */
    mask = XCB_CW_EVENT_MASK;
    list = XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_PROPERTY_CHANGE;
    xcb_change_window_attributes(conn, root, mask, &list);

    xcb_flush(conn);
}

static char *
get_prop_text(xcb_get_property_reply_t *reply)
{
    size_t size;
    char *data;

    if (!atom_is_string_type(reply->type))
	return NULL;

    size = (reply->format >> 3) * xcb_get_property_value_length(reply);

    data = malloc(size + 1);
    if (!data)
	APP_ERR(NULL, "Out of memory");

    memcpy(data, xcb_get_property_value(reply), size);
    data[size] = '\0';

    return scrub_text(data);
}

static gboolean
find_window(xcb_window_t window, GtkTreeIter *iter)
{
    GtkTreeModel *tree = GTK_TREE_MODEL(store);
    xcb_window_t cur;

    if (gtk_tree_model_get_iter_first(tree, iter)) {
	do {
	    gtk_tree_model_get(tree, iter, COL_WIN, &cur, -1);
	    if (cur == window)
		return TRUE;

	} while (gtk_tree_model_iter_next(tree, iter));
    }

    return FALSE;
}

static void
update_window_name(xcb_window_t window, const char *text)
{
    GtkTreeIter iter;
    char buf[128];

    if (find_window(window, &iter)) {
	snprintf(buf, sizeof(buf), "%s (%08x)", text, window);
	gtk_tree_store_set(store, &iter, COL_NAME, buf, -1);
    }
}

static void
add_item(GtkTreeIter *parent, GtkTreeIter *sibling,
	 xcb_window_t window, xcb_atom_t atom,
	 security_context_t octx, security_context_t dctx,
	 xcb_atom_t type, const char *data)
{
    GtkTreeIter iter;
    char *color;

    color = get_colors(octx, dctx);

    gtk_tree_store_insert_before(store, &iter, parent, sibling);
    gtk_tree_store_set(store, &iter,
		       COL_ICON, GTK_STOCK_FILE,
		       COL_ATOM, atom,
		       COL_WIN, window,
		       COL_DATA, data,
		       COL_NAME, atom_lookup(conn, atom),
		       COL_TYPE, atom_lookup(conn, type),
		       COL_OCTX, octx,
		       COL_DCTX, dctx,
		       COL_OFG, color,
		       COL_OBG, color + 8,
		       COL_DFG, color + 16,
		       COL_DBG, color + 24,
		       -1);

    free(color);
}

static void
remove_items(xcb_window_t window, xcb_atom_t atom)
{
    GtkTreeModel *tree = GTK_TREE_MODEL(store);
    GtkTreeIter iter, child;
    xcb_atom_t iatom;
    int more;

    if (!find_window(window, &iter))
	return;

    if (gtk_tree_model_iter_children(tree, &child, &iter)) {
	do {
	    gtk_tree_model_get(tree, &child, COL_ATOM, &iatom, -1);
	    if (atom == iatom)
		more = gtk_tree_store_remove(store, &child);
	    else
		more = gtk_tree_model_iter_next(tree, &child);
	} while (more);
    }
}

static int
update_item_matches(GtkTreeIter *iter, xcb_atom_t atom, security_context_t ctx)
{
    GtkTreeModel *tree = GTK_TREE_MODEL(store);
    xcb_atom_t iatom;
    security_context_t ictx;
    int result;

    gtk_tree_model_get(tree, iter, COL_ATOM, &iatom, COL_OCTX, &ictx, -1);
    result = (atom == iatom) && !strcmp(ctx, ictx);
    g_free(ictx);
    return result;
}

static gboolean
update_item_next(xcb_atom_t atom, GtkTreeIter *iter, GtkTreeIter **sibling)
{
    GtkTreeModel *tree = GTK_TREE_MODEL(store);
    xcb_atom_t iatom;
    const char *name, *iname;

    gtk_tree_model_get(tree, iter, COL_ATOM, &iatom, -1);
    name = atom_lookup(conn, atom);
    iname = atom_lookup(conn, iatom);

    if (strcasecmp(name, iname) < 0) {
	*sibling = iter;
	return FALSE;
    }

    *sibling = NULL;
    return gtk_tree_model_iter_next(tree, iter);
}

static void
update_item(xcb_window_t window, xcb_atom_t atom,
	    security_context_t octx, security_context_t dctx,
	    xcb_atom_t type, const char *data)
{
    GtkTreeModel *tree = GTK_TREE_MODEL(store);
    GtkTreeIter iter, child, *sibling = NULL;
    char *color;

    if (!find_window(window, &iter))
	return;

    if (gtk_tree_model_iter_children(tree, &child, &iter)) {
	do {
	    if (update_item_matches(&child, atom, octx)) {
		color = get_colors(NULL, dctx);
		gtk_tree_store_set(store, &child,
				   COL_DATA, data,
				   COL_TYPE, atom_lookup(conn, type),
				   COL_DCTX, dctx,
				   COL_DFG, color + 16,
				   COL_DBG, color + 24,
				   -1);
		free(color);
		return;
	    }
	} while (update_item_next(atom, &child, &sibling));
    }

    add_item(&iter, sibling, window, atom, octx, dctx, type, data);
}

static gboolean
update_is_managed(xcb_atom_t atom, xcb_atom_t wildcard)
{
    return (wildcard == XCB_NONE || wildcard == atom);
}

static void
update(xcb_window_t window, xcb_atom_t atom)
{
    xcb_selinux_list_properties_cookie_t cookie;
    xcb_selinux_list_properties_reply_t *reply;
    xcb_get_property_cookie_t *cookies;
    xcb_get_property_reply_t *propinfo;
    xcb_generic_error_t *error;
    xcb_selinux_list_item_iterator_t iter;
    security_context_t octx, dctx;
    char *text;
    int i;

    /* remove existing items if updating */
    if (atom != XCB_NONE)
	remove_items(window, atom);

    /* list all property instances on the window */
    cookie = xcb_selinux_list_properties(conn, window);
    reply = xcb_selinux_list_properties_reply(conn, cookie, &error);
    if (error) {
	/* window might have gone away */
	if (error->error_code == XCB_WINDOW) {
	    free(error);
	    return;
	}
	APP_ERR(error, "An error was received during ListProperties.\n");
    }

    iter = xcb_selinux_list_properties_properties_iterator(reply);
    cookies = malloc(iter.rem * sizeof(*cookies));
    if (!cookies)
	APP_ERR(NULL, "Out of memory\n");

    /* get the property type and text content */
    i = 0;
    while (iter.rem) {
	if (update_is_managed(iter.data->name, atom)) {
	    octx = xcb_selinux_list_item_object_context(iter.data);
	    xcb_selinux_set_property_use_context(conn, strlen(octx) + 1, octx);
	    cookies[i] = xcb_get_property(conn, FALSE, window, iter.data->name,
					  XCB_GET_PROPERTY_TYPE_ANY, 0, 1024);
	}
	xcb_selinux_list_item_next(&iter);
	++i;
    }

    /* process the replies */
    i = 0;
    iter = xcb_selinux_list_properties_properties_iterator(reply);
    while (iter.rem) {
	if (update_is_managed(iter.data->name, atom)) {
	    octx = xcb_selinux_list_item_object_context(iter.data);
	    dctx = xcb_selinux_list_item_data_context(iter.data);
	    propinfo = xcb_get_property_reply(conn, cookies[i], &error);
	    if (error)
		APP_ERR(error, "An error was received during GetProperty.\n");

	    text = get_prop_text(propinfo);
	    update_item(window, iter.data->name, octx, dctx, propinfo->type, text);

	    /* special window updates */
	    if (iter.data->name == wm_name)
		update_window_name(window, text);

	    free(text);
	    free(propinfo);
	}
	xcb_selinux_list_item_next(&iter);
	++i;
    }

    free(reply);
    free(cookies);
    xcb_flush(conn);
}

static void
add_window(xcb_window_t window)
{
    xcb_selinux_get_window_context_cookie_t cookie;
    xcb_selinux_get_window_context_reply_t *reply;
    xcb_generic_error_t *error;
    security_context_t octx;
    uint32_t mask, list;
    GtkTreeIter iter;
    char *color, buf[24];

    /* Get window context */
    cookie = xcb_selinux_get_window_context(conn, window);
    reply = xcb_selinux_get_window_context_reply(conn, cookie, &error);
    if (error) {
	/* window might have gone away */
	if (error->error_code == XCB_WINDOW) {
	    free(error);
	    return;
	}
	APP_ERR(error, "An error was received during GetWindowContext.\n");
    }

    octx = xcb_selinux_get_window_context_context(reply);

    /* Add window to list */
    color = get_colors(octx, NULL);
    if (window == root)
	snprintf(buf, sizeof(buf), "root (%08x)", window);
    else
	snprintf(buf, sizeof(buf), "unnamed (%08x)", window);

    gtk_tree_store_append(store, &iter, NULL);
    gtk_tree_store_set(store, &iter,
		       COL_ICON, GTK_STOCK_DIRECTORY,
		       COL_WIN, window,
		       COL_NAME, buf,
		       COL_OCTX, octx,
		       COL_OFG, color,
		       COL_OBG, color + 8,
		       -1);

    /* Select for window events */
    if (window != root) {
	mask = XCB_CW_EVENT_MASK;
	list = XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_PROPERTY_CHANGE;
	xcb_change_window_attributes(conn, window, mask, &list);
    }

    update(window, XCB_NONE);
    free(color);
    free(reply);
}

static void
add_windows(void)
{
    xcb_query_tree_cookie_t cookie;
    xcb_query_tree_reply_t *reply;
    xcb_generic_error_t *error;
    xcb_window_t *children;
    int i;

    cookie = xcb_query_tree(conn, root);
    reply = xcb_query_tree_reply(conn, cookie, &error);
    if (error)
	APP_ERR(error, "An error was received during QueryTree.\n");

    children = xcb_query_tree_children(reply);

    add_window(root);
    for (i = 0; i < reply->children_len; i++)
	add_window(children[i]);

    free(reply);
}

static void
del_window_helper(xcb_window_t window, GtkTreeIter *iter)
{
    GtkTreeModel *tree = GTK_TREE_MODEL(store);
    GtkTreeIter child;
    xcb_window_t cur;
    int more;

    do {
	gtk_tree_model_get(tree, iter, COL_WIN, &cur, -1);
	if (cur == window) {
	    if (gtk_tree_model_iter_children(tree, &child, iter))
		del_window_helper(window, &child);
	    more = gtk_tree_store_remove(store, iter);
	} else
	    more = gtk_tree_model_iter_next(tree, iter);
    } while (more);
}

static void
del_window(xcb_window_t window)
{
    GtkTreeModel *tree = GTK_TREE_MODEL(store);
    GtkTreeIter iter;

    if (gtk_tree_model_get_iter_first(tree, &iter))
	del_window_helper(window, &iter);
}

static void
handle_property(xcb_property_notify_event_t *event)
{
    update(event->window, event->atom);
}

static void
handle_create(xcb_create_notify_event_t *event)
{
    add_window(event->window);
}

static void
handle_destroy(xcb_destroy_notify_event_t *event)
{
    del_window(event->window);
}

static gboolean
handle_event(GIOChannel *source, GIOCondition condition, gpointer data)
{
    xcb_generic_event_t *event;

    while ((event = xcb_poll_for_event(conn))) {
	switch (event->response_type) {
	case XCB_PROPERTY_NOTIFY:
	    handle_property((xcb_property_notify_event_t *)event);
	    break;

	case XCB_CREATE_NOTIFY:
	    handle_create((xcb_create_notify_event_t *)event);
	    break;

	case XCB_DESTROY_NOTIFY:
	    handle_destroy((xcb_destroy_notify_event_t *)event);
	    break;

	case XCB_MAP_NOTIFY:
	case XCB_UNMAP_NOTIFY:
	case XCB_UNMAP_NOTIFY | 0x80:
	case XCB_CONFIGURE_NOTIFY:
	case XCB_CONFIGURE_NOTIFY | 0x80:
	case XCB_REPARENT_NOTIFY:
	case XCB_MAPPING_NOTIFY:
	case XCB_CLIENT_MESSAGE:
	case XCB_CLIENT_MESSAGE | 0x80:
	    /* do nothing */
	    break;

	default:
	    fprintf(stderr, "Monitor: %d event\n", event->response_type);
	    break;
	}
	free(event);
	xcb_flush(conn);
    }

    return TRUE;
}

int
prop_monitor_init(struct thread_data *data)
{
    int fd;
    GIOChannel *channel;

    store = GTK_TREE_STORE(data->store);

    setup_x(data->display);

    fd = xcb_get_file_descriptor(conn);
    channel = g_io_channel_unix_new(fd);
    g_io_channel_set_encoding(channel, NULL, NULL);
    g_io_add_watch(channel, G_IO_IN|G_IO_ERR|G_IO_HUP, handle_event, NULL);

    add_windows();
    handle_event(channel, 0, NULL);

    return 0;
}