~leon_plickat/wlopm

ref: a385b761bd0854c41264e6a543894368a2191d6f wlopm/wlopm.c -rw-r--r-- 12.9 KiB
a385b761Daniel Eklöf Append to CFLAGS, instead of replacing them 3 months ago
                                                                                
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
 * wlopm - Wayland output power manager
 *
 * Copyright (C) 2021 Leon Henrik Plickat
 *
 * 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 3 of the License, or
 * (at your option) 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, see <https://www.gnu.org/licenses/>.
 */

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>

#include <wayland-client.h>

#include "xdg-output-unstable-v1.h"
#include "wlr-output-power-management-unstable-v1.h"

#define VERSION "0.0.1"

const char usage[] =
	"Usage: wlopm [options...]\n"
	"  -j, --json                  Use JSON format.\n"
	"  -h, --help                  Print this help text and exit.\n"
	"      --on     <output-name>  Set the power mode of the specified output to on.\n"
	"      --off    <output-name>  Set the power mode of the specified output to off.\n"
	"      --toggle <output-name>  Toggle the power mode of the specified output.\n"
	"\n";

struct Output
{
	struct wl_list link;
	struct wl_output *wl_output;
	struct zxdg_output_v1 *xdg_output;
	struct zwlr_output_power_v1 *wlr_output_power;
	enum zwlr_output_power_v1_mode mode;
	char *name;
	bool operation_failed;
	uint32_t global_name;
};

enum Action
{
	LIST,
	OPERATIONS,
};

enum Power_mode
{
	ON,
	OFF,
	TOGGLE,
};

struct Operation
{
	struct wl_list link;
	char *name;
	enum Power_mode power_mode;
};

bool json = false;
bool json_prev = false;

struct wl_display *wl_display = NULL;
struct wl_registry *wl_registry = NULL;
struct wl_callback *sync_callback = NULL;

struct wl_list outputs;
struct wl_list operations;

struct zxdg_output_manager_v1 *xdg_output_manager = NULL;
struct zwlr_output_power_manager_v1 *wlr_output_power_manager = NULL;

int ret = EXIT_SUCCESS;
bool loop = true;

static void noop () {}

static void wlr_output_power_handle_mode (void *data, struct zwlr_output_power_v1 *wlr_output_power,
		enum zwlr_output_power_v1_mode mode)
{
	struct Output *output = (struct Output *)data;
	output->mode = mode;
}

static void wlr_output_power_handle_failed (void *data, struct zwlr_output_power_v1 *wlr_output_power)
{
	struct Output *output = (struct Output *)data;
	output->operation_failed = true;
}

static const struct zwlr_output_power_v1_listener wlr_output_power_listener = {
	.mode   = wlr_output_power_handle_mode,
	.failed = wlr_output_power_handle_failed,
};

static void xdg_output_handle_name (void *data, struct zxdg_output_v1 *xdg_output,
		const char *name)
{
	struct Output *output = (struct Output *)data;
	if ( output->name != NULL )
		free(output->name);
	output->name = strdup(name);
}

static const struct zxdg_output_v1_listener xdg_output_listener = {
	.logical_size     = noop,
	.name             = xdg_output_handle_name,
	.logical_position = noop,
	.description      = noop,
	.done             = noop, /* Deprecated since version 3. */
};

static void registry_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 )
	{
		struct Output *output = calloc(1, sizeof(struct Output));
		if ( output == NULL )
		{
			fputs("ERROR: Failed to allocate.\n", stderr);
			return;
		}

		output->wl_output = wl_registry_bind(registry, name, 
				&wl_output_interface, version);
		output->xdg_output = NULL;
		output->wlr_output_power = NULL;
		output->name = NULL;
		output->global_name = name;

		wl_list_insert(&outputs, &output->link);
	}
	else if ( strcmp(interface, zxdg_output_manager_v1_interface.name) == 0 )
		xdg_output_manager = wl_registry_bind(registry, name,
				&zxdg_output_manager_v1_interface, version);
	else if ( strcmp(interface, zwlr_output_power_manager_v1_interface.name) == 0 )
		wlr_output_power_manager = wl_registry_bind(registry, name,
				&zwlr_output_power_manager_v1_interface, version);
}

static const struct wl_registry_listener registry_listener = {
	.global        = registry_handle_global,
	.global_remove = noop, /* We don't run long enough to care. */
};

static void sync_handle_done (void *data, struct wl_callback *wl_callback, uint32_t other_data);
static const struct wl_callback_listener sync_callback_listener = {
	.done = sync_handle_done,
};

static struct Output *output_from_name (const char *str)
{
	struct Output *output;
	wl_list_for_each(output, &outputs, link)
		if ( strcmp(output->name, str) == 0 )
			return output;
	return NULL;
}

static void output_set_power_mode (struct Output *output, enum Power_mode mode)
{
	enum zwlr_output_power_v1_mode new_mode;
	switch (mode)
	{
		case ON:
			new_mode = ZWLR_OUTPUT_POWER_V1_MODE_ON;
			break;

		case OFF:
			new_mode = ZWLR_OUTPUT_POWER_V1_MODE_OFF;
			break;

		case TOGGLE:
			if ( output->mode == ZWLR_OUTPUT_POWER_V1_MODE_ON )
				new_mode = ZWLR_OUTPUT_POWER_V1_MODE_OFF;
			else
				new_mode = ZWLR_OUTPUT_POWER_V1_MODE_ON;
			break;
	}
	zwlr_output_power_v1_set_mode(output->wlr_output_power, new_mode);
}

static char *power_mode_to_string (enum zwlr_output_power_v1_mode mode)
{
	return mode == ZWLR_OUTPUT_POWER_V1_MODE_ON ? "on" : "off";
}

static void print_json_error (const char *output_name, const char *msg)
{
	fprintf(stdout,
			"%s\n    {\n"
			"      \"output\": \"%s\",\n"
			"      \"error\": \"%s\"\n"
			"    }",
			json_prev ? "," : "", output_name, msg);
	json_prev = true;
}

static void do_operation (struct Operation *operation)
{
	if ( *operation->name == '*' )
	{
		struct Output *output;
		wl_list_for_each(output, &outputs, link)
			output_set_power_mode(output, operation->power_mode);
	}
	else
	{
		struct Output *output = output_from_name(operation->name);
		if ( output == NULL )
		{
			if (json)
				print_json_error(operation->name, "output does not exist");
			else
				fprintf(stderr, "ERROR: Output '%s' does not exist.\n",
						operation->name);
			return;
		}
		output_set_power_mode(output, operation->power_mode);
	}
}

static void sync_handle_done (void *data, struct wl_callback *wl_callback, uint32_t other_data)
{
	wl_callback_destroy(wl_callback);
	sync_callback = NULL;

	static int sync = 0;
	if ( sync == 0 )
	{
		if ( wlr_output_power_manager == NULL )
		{
			fputs("ERROR: Wayland server does not support wlr-output-power-management-v1.\n", stderr);
			loop = false;
			ret = EXIT_FAILURE;
			return;
		}

		if ( xdg_output_manager == NULL )
		{
			fputs("ERROR: Wayland server does not support xdg-output.\n", stderr);
			loop = false;
			ret = EXIT_FAILURE;
			return;
		}

		struct Output *output;
		wl_list_for_each(output, &outputs, link)
		{
			output->xdg_output = zxdg_output_manager_v1_get_xdg_output(
					xdg_output_manager, output->wl_output);
			zxdg_output_v1_add_listener(output->xdg_output,
					&xdg_output_listener, output);

			output->wlr_output_power = zwlr_output_power_manager_v1_get_output_power(
					wlr_output_power_manager, output->wl_output);
			zwlr_output_power_v1_add_listener(output->wlr_output_power,
					&wlr_output_power_listener, output);
		}

		sync_callback = wl_display_sync(wl_display);
		wl_callback_add_listener(sync_callback, &sync_callback_listener, NULL);
	}
	else if ( sync == 1 )
	{
		if (wl_list_empty(&operations))
		{
			/* The operations list is empty, so let's just list all
			 * outputs and their current power mode.
			 */
			struct Output *output;
			if (json)
			{
				fputs("[", stdout);
				wl_list_for_each(output, &outputs, link)
				{
					fprintf(stdout,
							"%s\n  {\n"
							"    \"output\": \"%s\",\n"
							"    \"power-mode\": \"%s\"\n"
							"  }",
							json_prev ? "," : "",
							output->name,
							power_mode_to_string(output->mode));
					json_prev = true;
				}
				fputs("\n]\n", stdout);
			}
			else
				wl_list_for_each(output, &outputs, link)
					fprintf(stdout, "%s %s\n", output->name,
							power_mode_to_string(output->mode));
			loop = false;
		}
		else
		{
			/* There are operations in the operations list. We have
			 * things to do!
			 */

			if (json)
				fputs(
						"{\n"
						"  \"errors\": [",
						stdout);

			struct Operation *operation;
			wl_list_for_each(operation, &operations, link)
				do_operation(operation);

			/* We need to sync yet another time because setting the
			 * power mode might fail and we want to display those
			 * error messages.
			 */
			sync_callback = wl_display_sync(wl_display);
			wl_callback_add_listener(sync_callback, &sync_callback_listener, NULL);
		}
	}
	else
	{
		struct Output *output;
		wl_list_for_each(output, &outputs, link)
			if (output->operation_failed)
			{
				if (json)
					print_json_error(output->name, "setting power mode failed");
				else
					fprintf(stderr, "ERROR: Setting power mode for output '%s' failed.\n",
							output->name);
			}

		if (json)
			fputs(
					"\n  ]\n"
					"}\n",
					stdout);
		loop = false;
	}
	sync++;
}

static void destroy_all_outputs (void)
{
	struct Output *output, *tmp;
	wl_list_for_each_safe(output, tmp, &outputs, link)
	{
		if ( output->wlr_output_power != NULL )
			zwlr_output_power_v1_destroy(output->wlr_output_power);
		if ( output->xdg_output != NULL )
			zxdg_output_v1_destroy(output->xdg_output);
		wl_output_destroy(output->wl_output);
		wl_list_remove(&output->link);
		free(output->name);
		free(output);
	}
}

static void destroy_all_operations (void)
{
	struct Operation *operation, *tmp;
	wl_list_for_each_safe(operation, tmp, &operations, link)
	{
		wl_list_remove(&operation->link);
		free(operation->name);
		free(operation);
	}
}

static bool create_operation (const char *name, enum Power_mode power_mode)
{
	struct Operation *operation = calloc(1, sizeof(struct Operation));
	if ( operation == NULL )
	{
		fprintf(stderr, "ERROR: calloc: %s\n", strerror(errno));
		return false;
	}

	operation->name = strdup(name);
	if ( operation->name == NULL )
	{
		fprintf(stderr, "ERROR: calloc: %s\n", strerror(errno));
		free(operation);
		return false;
	}

	operation->power_mode = power_mode;

	wl_list_insert(&operations, &operation->link);
	return true;
}

int main(int argc, char *argv[])
{
	wl_list_init(&operations);
	for (int i = 1; i < argc; i++)
	{
		if ( strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0 )
		{
			destroy_all_operations();
			fputs(usage, stderr);
			return EXIT_SUCCESS;
		}
		else if ( strcmp(argv[i], "-j") == 0 || strcmp(argv[i], "--json") == 0 )
			json = true;
		else if ( strcmp(argv[i], "--on") == 0 )
		{
			if ( i == argc - 1 )
			{
				fputs("ERROR: '--on' needs an output name.\n", stderr);
				destroy_all_operations();
				return EXIT_FAILURE;
			}
			if (! create_operation(argv[i+1], ON))
			{
				destroy_all_operations();
				return EXIT_FAILURE;
			}
			i++;
		}
		else if ( strcmp(argv[i], "--off") == 0 )
		{
			if ( i == argc - 1 )
			{
				fputs("ERROR: '--off' needs an output name.\n", stderr);
				destroy_all_operations();
				return EXIT_FAILURE;
			}
			if (! create_operation(argv[i+1], OFF))
			{
				destroy_all_operations();
				return EXIT_FAILURE;
			}
			i++;
		}
		else if ( strcmp(argv[i], "--toggle") == 0 )
		{
			if ( i == argc - 1 )
			{
				fputs("ERROR: '--toggle' needs an output name.\n", stderr);
				destroy_all_operations();
				return EXIT_FAILURE;
			}
			if (! create_operation(argv[i+1], TOGGLE))
			{
				destroy_all_operations();
				return EXIT_FAILURE;
			}
			i++;
		}
		else
		{
			fprintf(stderr, "ERROR: Unknown option '%s'\n", argv[i]);
			destroy_all_operations();
			return EXIT_FAILURE;
		}
	}

	/* We query the display name here instead of letting wl_display_connect()
	 * figure it out itself, because libwayland (for legacy reasons) falls
	 * back to using "wayland-0" when $WAYLAND_DISPLAY is not set, which is
	 * generally not desirable.
	 */
	const char *display_name = getenv("WAYLAND_DISPLAY");
	if ( display_name == NULL )
	{
		fputs("ERROR: WAYLAND_DISPLAY is not set.\n", stderr);
		return EXIT_FAILURE;
	}

	wl_display = wl_display_connect(display_name);
	if ( wl_display == NULL )
	{
		fputs("ERROR: Can not connect to wayland display.\n", stderr);
		return EXIT_FAILURE;
	}

	wl_list_init(&outputs);

	wl_registry = wl_display_get_registry(wl_display);
	wl_registry_add_listener(wl_registry, &registry_listener, NULL);

	sync_callback = wl_display_sync(wl_display);
	wl_callback_add_listener(sync_callback, &sync_callback_listener, NULL);

	while ( loop && wl_display_dispatch(wl_display) > 0 );

	destroy_all_operations();
	destroy_all_outputs();

	if ( sync_callback != NULL )
		wl_callback_destroy(sync_callback);
	if ( wlr_output_power_manager != NULL )
		zwlr_output_power_manager_v1_destroy(wlr_output_power_manager);
	if ( xdg_output_manager != NULL )
		zxdg_output_manager_v1_destroy(xdg_output_manager);
	if ( wl_registry != NULL )
		wl_registry_destroy(wl_registry);
	wl_display_disconnect(wl_display);

	return ret;
}