aboutsummaryrefslogtreecommitdiff
path: root/examples/foreign-toplevel.c
blob: 2d16f77b28f3fd580f055135d10a4dd28d911fe5 (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
#define _POSIX_C_SOURCE 200809L
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wayland-client.h>
#include "wlr-foreign-toplevel-management-unstable-v1-client-protocol.h"

#define WLR_FOREIGN_TOPLEVEL_MANAGEMENT_VERSION 3


static void print_help(void) {
	static const char usage[] =
		"Usage: foreign-toplevel [OPTIONS] ...\n"
		"Manage and view information about toplevel windows.\n"
		"\n"
		"  -f <id>         focus\n"
		"  -s <id>         fullscreen\n"
		"  -o <output_id>  select output for fullscreen toplevel to appear on. Use this\n"
		"                  option with -s. View available outputs with wayland-info.\n"
		"  -S <id>         unfullscreen\n"
		"  -a <id>         maximize\n"
		"  -u <id>         unmaximize\n"
		"  -i <id>         minimize\n"
		"  -r <id>         restore(unminimize)\n"
		"  -c <id>         close\n"
		"  -m              continuously print changes to the list of opened toplevels\n"
		"                  Can be used together with some of the previous options.\n"
		"  -h              print help message and quit\n";
	fprintf(stderr, "%s", usage);
}

enum toplevel_state_field {
	TOPLEVEL_STATE_MAXIMIZED = (1 << 0),
	TOPLEVEL_STATE_MINIMIZED = (1 << 1),
	TOPLEVEL_STATE_ACTIVATED = (1 << 2),
	TOPLEVEL_STATE_FULLSCREEN = (1 << 3),
	TOPLEVEL_STATE_INVALID = (1 << 4),
};

static const uint32_t no_parent = (uint32_t)-1;
static struct wl_output *pref_output = NULL;
static uint32_t pref_output_id = UINT32_MAX;

struct toplevel_state {
	char *title;
	char *app_id;

	uint32_t state;
	uint32_t parent_id;
};

static void copy_state(struct toplevel_state *current,
		struct toplevel_state *pending) {
	if (current->title && pending->title) {
		free(current->title);
	}
	if (current->app_id && pending->app_id) {
		free(current->app_id);
	}

	if (pending->title) {
		current->title = pending->title;
		pending->title = NULL;
	}
	if (pending->app_id) {
		current->app_id = pending->app_id;
		pending->app_id = NULL;
	}

	if (!(pending->state & TOPLEVEL_STATE_INVALID)) {
		current->state = pending->state;
	}

	current->parent_id = pending->parent_id;

	pending->state = TOPLEVEL_STATE_INVALID;
}

static uint32_t global_id = 0;
struct toplevel_v1 {
	struct wl_list link;
	struct zwlr_foreign_toplevel_handle_v1 *zwlr_toplevel;

	uint32_t id;
	struct toplevel_state current, pending;
};

static void print_toplevel(struct toplevel_v1 *toplevel, bool print_endl) {
	printf("-> %d. title=%s app_id=%s", toplevel->id,
			toplevel->current.title ?: "(nil)",
			toplevel->current.app_id ?: "(nil)");

	if (toplevel->current.parent_id != no_parent) {
		printf(" parent=%u", toplevel->current.parent_id);
	} else {
		printf(" no parent");
	}

	if (print_endl) {
		printf("\n");
	}
}

static void print_toplevel_state(struct toplevel_v1 *toplevel, bool print_endl) {
	if (toplevel->current.state & TOPLEVEL_STATE_MAXIMIZED) {
		printf(" maximized");
	} else {
		printf(" unmaximized");
	}
	if (toplevel->current.state & TOPLEVEL_STATE_MINIMIZED) {
		printf(" minimized");
	} else {
		printf(" unminimized");
	}
	if (toplevel->current.state & TOPLEVEL_STATE_ACTIVATED) {
		printf(" active");
	} else {
		printf(" inactive");
	}
	if (toplevel->current.state & TOPLEVEL_STATE_FULLSCREEN) {
		printf(" fullscreen");
	}

	if (print_endl) {
		printf("\n");
	}
}

static void finish_toplevel_state(struct toplevel_state *state) {
	free(state->title);
	free(state->app_id);
}

static void toplevel_handle_title(void *data,
		struct zwlr_foreign_toplevel_handle_v1 *zwlr_toplevel,
		const char *title) {
	struct toplevel_v1 *toplevel = data;
	free(toplevel->pending.title);
	toplevel->pending.title = strdup(title);
}

static void toplevel_handle_app_id(void *data,
		struct zwlr_foreign_toplevel_handle_v1 *zwlr_toplevel,
		const char *app_id) {
	struct toplevel_v1 *toplevel = data;
	free(toplevel->pending.app_id);
	toplevel->pending.app_id = strdup(app_id);
}

static void toplevel_handle_output_enter(void *data,
		struct zwlr_foreign_toplevel_handle_v1 *zwlr_toplevel,
		struct wl_output *output) {
	struct toplevel_v1 *toplevel = data;
	print_toplevel(toplevel, false);
	printf(" enter output %u\n",
		(uint32_t)(size_t)wl_output_get_user_data(output));
}

static void toplevel_handle_output_leave(void *data,
		struct zwlr_foreign_toplevel_handle_v1 *zwlr_toplevel,
		struct wl_output *output) {
	struct toplevel_v1 *toplevel = data;
	print_toplevel(toplevel, false);
	printf(" leave output %u\n",
		(uint32_t)(size_t)wl_output_get_user_data(output));
}

static uint32_t array_to_state(struct wl_array *array) {
	uint32_t state = 0;
	uint32_t *entry;
	wl_array_for_each(entry, array) {
		if (*entry == ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_MAXIMIZED)
			state |= TOPLEVEL_STATE_MAXIMIZED;
		if (*entry == ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_MINIMIZED)
			state |= TOPLEVEL_STATE_MINIMIZED;
		if (*entry == ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_ACTIVATED)
			state |= TOPLEVEL_STATE_ACTIVATED;
		if (*entry == ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_FULLSCREEN)
			state |= TOPLEVEL_STATE_FULLSCREEN;
	}

	return state;
}

static void toplevel_handle_state(void *data,
		struct zwlr_foreign_toplevel_handle_v1 *zwlr_toplevel,
		struct wl_array *state) {
	struct toplevel_v1 *toplevel = data;
	toplevel->pending.state = array_to_state(state);
}

static struct zwlr_foreign_toplevel_manager_v1 *toplevel_manager = NULL;
static struct wl_list toplevel_list;

static void toplevel_handle_parent(void *data,
		struct zwlr_foreign_toplevel_handle_v1 *zwlr_toplevel,
		struct zwlr_foreign_toplevel_handle_v1 *zwlr_parent) {
	struct toplevel_v1 *toplevel = data;
	toplevel->pending.parent_id = no_parent;
	if (zwlr_parent) {
		struct toplevel_v1 *toplevel_tmp;
		wl_list_for_each(toplevel_tmp, &toplevel_list, link) {
			if (toplevel_tmp->zwlr_toplevel == zwlr_parent) {
				toplevel->pending.parent_id = toplevel_tmp->id;
				break;
			}
		}
		if (toplevel->pending.parent_id == no_parent) {
			fprintf(stderr, "Cannot find parent toplevel!\n");
		}
	}
}

static void toplevel_handle_done(void *data,
		struct zwlr_foreign_toplevel_handle_v1 *zwlr_toplevel) {
	struct toplevel_v1 *toplevel = data;
	bool state_changed = toplevel->current.state != toplevel->pending.state;

	copy_state(&toplevel->current, &toplevel->pending);

	print_toplevel(toplevel, !state_changed);
	if (state_changed) {
		print_toplevel_state(toplevel, true);
	}
}

static void toplevel_handle_closed(void *data,
		struct zwlr_foreign_toplevel_handle_v1 *zwlr_toplevel) {
	struct toplevel_v1 *toplevel = data;
	print_toplevel(toplevel, false);
	printf(" closed\n");

	zwlr_foreign_toplevel_handle_v1_destroy(zwlr_toplevel);
	finish_toplevel_state(&toplevel->current);
	finish_toplevel_state(&toplevel->pending);
	free(toplevel);
}

static const struct zwlr_foreign_toplevel_handle_v1_listener toplevel_impl = {
	.title = toplevel_handle_title,
	.app_id = toplevel_handle_app_id,
	.output_enter = toplevel_handle_output_enter,
	.output_leave = toplevel_handle_output_leave,
	.state = toplevel_handle_state,
	.done = toplevel_handle_done,
	.closed = toplevel_handle_closed,
	.parent = toplevel_handle_parent
};

static void toplevel_manager_handle_toplevel(void *data,
		struct zwlr_foreign_toplevel_manager_v1 *toplevel_manager,
		struct zwlr_foreign_toplevel_handle_v1 *zwlr_toplevel) {
	struct toplevel_v1 *toplevel = calloc(1, sizeof(struct toplevel_v1));
	if (!toplevel) {
		fprintf(stderr, "Failed to allocate memory for toplevel\n");
		return;
	}

	toplevel->id = global_id++;
	toplevel->zwlr_toplevel = zwlr_toplevel;
	toplevel->current.parent_id = no_parent;
	toplevel->pending.parent_id = no_parent;
	wl_list_insert(&toplevel_list, &toplevel->link);

	zwlr_foreign_toplevel_handle_v1_add_listener(zwlr_toplevel, &toplevel_impl,
		toplevel);
}

static void toplevel_manager_handle_finished(void *data,
		struct zwlr_foreign_toplevel_manager_v1 *toplevel_manager) {
	zwlr_foreign_toplevel_manager_v1_destroy(toplevel_manager);
}

static const struct zwlr_foreign_toplevel_manager_v1_listener toplevel_manager_impl = {
	.toplevel = toplevel_manager_handle_toplevel,
	.finished = toplevel_manager_handle_finished,
};

struct wl_seat *seat = NULL;
static void handle_global(void *data, struct wl_registry *registry,
		uint32_t name, const char *interface, uint32_t version) {
	if (strcmp(interface, wl_output_interface.name) == 0) {
		if (name == pref_output_id) {
			pref_output = wl_registry_bind(registry, name,
					&wl_output_interface, version);

		}
	} else if (strcmp(interface,
			zwlr_foreign_toplevel_manager_v1_interface.name) == 0) {
		toplevel_manager = wl_registry_bind(registry, name,
				&zwlr_foreign_toplevel_manager_v1_interface,
				WLR_FOREIGN_TOPLEVEL_MANAGEMENT_VERSION);

		wl_list_init(&toplevel_list);
		zwlr_foreign_toplevel_manager_v1_add_listener(toplevel_manager,
				&toplevel_manager_impl, NULL);
	} else if (strcmp(interface, wl_seat_interface.name) == 0 && seat == NULL) {
		seat = wl_registry_bind(registry, name, &wl_seat_interface, version);
	}
}

static void handle_global_remove(void *data, struct wl_registry *registry,
		uint32_t name) {
	// who cares
}

static const struct wl_registry_listener registry_listener = {
	.global = handle_global,
	.global_remove = handle_global_remove,
};

/* return NULL when id == -1
 * exit if the given ID cannot be found in the list of toplevels */
static struct toplevel_v1 *toplevel_by_id_or_bail(int32_t id) {
	if (id == -1) {
		return NULL;
	}

	struct toplevel_v1 *toplevel;
	wl_list_for_each(toplevel, &toplevel_list, link) {
		if (toplevel->id == (uint32_t)id) {
			return toplevel;
		}
	}

	fprintf(stderr, "No toplevel with the given id: %d\n", id);
	exit(EXIT_FAILURE);
}

int main(int argc, char **argv) {
	int focus_id = -1, close_id = -1;
	int maximize_id = -1, unmaximize_id = -1;
	int minimize_id = -1, restore_id = -1;
	int fullscreen_id = -1, unfullscreen_id = -1;
	int one_shot = 1;
	int c;

	while ((c = getopt(argc, argv, "f:a:u:i:r:c:s:S:mo:h")) != -1) {
		switch (c) {
		case 'f':
			focus_id = atoi(optarg);
			break;
		case 'a':
			maximize_id = atoi(optarg);
			break;
		case 'u':
			unmaximize_id = atoi(optarg);
			break;
		case 'i':
			minimize_id = atoi(optarg);
			break;
		case 'r':
			restore_id = atoi(optarg);
			break;
		case 'c':
			close_id = atoi(optarg);
			break;
		case 's':
			fullscreen_id = atoi(optarg);
			break;
		case 'S':
			unfullscreen_id = atoi(optarg);
			break;
		case 'm':
			one_shot = 0;
			break;
		case 'o':
			pref_output_id = atoi(optarg);
			break;
		case '?':
			print_help();
			return EXIT_FAILURE;
			break;
		case 'h':
			print_help();
			return EXIT_SUCCESS;
			break;
		}
	}

	struct wl_display *display = wl_display_connect(NULL);
	if (display == NULL) {
		fprintf(stderr, "Failed to create display\n");
		return EXIT_FAILURE;
	}

	struct wl_registry *registry = wl_display_get_registry(display);
	wl_registry_add_listener(registry, &registry_listener, NULL);
	wl_display_roundtrip(display);

	if (toplevel_manager == NULL) {
		fprintf(stderr, "wlr-foreign-toplevel not available\n");
		return EXIT_FAILURE;
	}
	wl_display_roundtrip(display); // load list of toplevels
	wl_display_roundtrip(display); // load toplevel details

	struct toplevel_v1 *toplevel;
	if ((toplevel = toplevel_by_id_or_bail(focus_id))) {
		zwlr_foreign_toplevel_handle_v1_activate(toplevel->zwlr_toplevel, seat);
	}
	if ((toplevel = toplevel_by_id_or_bail(maximize_id))) {
		zwlr_foreign_toplevel_handle_v1_set_maximized(toplevel->zwlr_toplevel);
	}
	if ((toplevel = toplevel_by_id_or_bail(unmaximize_id))) {
		zwlr_foreign_toplevel_handle_v1_unset_maximized(toplevel->zwlr_toplevel);
	}
	if ((toplevel = toplevel_by_id_or_bail(minimize_id))) {
		zwlr_foreign_toplevel_handle_v1_set_minimized(toplevel->zwlr_toplevel);
	}
	if ((toplevel = toplevel_by_id_or_bail(restore_id))) {
		zwlr_foreign_toplevel_handle_v1_unset_minimized(toplevel->zwlr_toplevel);
	}
	if ((toplevel = toplevel_by_id_or_bail(fullscreen_id))) {
		if (pref_output_id != UINT32_MAX && pref_output == NULL) {
			fprintf(stderr, "Could not find output %i\n", pref_output_id);
		}

		zwlr_foreign_toplevel_handle_v1_set_fullscreen(toplevel->zwlr_toplevel, pref_output);
	}
	if ((toplevel = toplevel_by_id_or_bail(unfullscreen_id))) {
		zwlr_foreign_toplevel_handle_v1_unset_fullscreen(toplevel->zwlr_toplevel);
	}
	if ((toplevel = toplevel_by_id_or_bail(close_id))) {
		zwlr_foreign_toplevel_handle_v1_close(toplevel->zwlr_toplevel);
	}

	wl_display_flush(display);

	if (one_shot == 0) {
		while (wl_display_dispatch(display) != -1) {
			// This space intentionally left blank
		}
	}

	return EXIT_SUCCESS;
}