~leon_plickat/wlopm

22f87adf6c89d607df7995e4e4e174028e89e6f2 — Leon Henrik Plickat 7 months ago e3f2453
Add "toggle" operation
2 files changed, 55 insertions(+), 14 deletions(-)

M wlopm.1
M wlopm.c
M wlopm.1 => wlopm.1 +6 -0
@@ 26,6 26,12 @@ Set output power mode to off.
.P
.RE

\fBwlopm toggle <output-name>\fR
.RS 4
Toggle the output power mode.
.P
.RE

.SH AUTHOR
Leon Henrik Plickat


M wlopm.c => wlopm.c +49 -14
@@ 32,9 32,10 @@

const char usage[] =
	"Usage:\n"
	"\twlopm                     List outputs and their power modes.\n"
	"\twlopm on  <output-name>   Set output power mode to on.\n"
	"\twlopm off <output-name>   Set output power mode to off.\n"
	"\twlopm                        List outputs and their power modes.\n"
	"\twlopm on     <output-name>   Set output power mode to on.\n"
	"\twlopm off    <output-name>   Set output power mode to off.\n"
	"\twlopm toggle <output-name>   Toggle output power mode.\n"
	"\n";

enum Action


@@ 42,6 43,7 @@ enum Action
	LIST,
	ON,
	OFF,
	TOGGLE,
};

enum Action action = LIST;


@@ 148,6 150,15 @@ 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, name) == 0 )
			return output;
	return NULL;
}

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


@@ 201,17 212,39 @@ static void sync_handle_done (void *data, struct wl_callback *wl_callback, uint3
		}
		else
		{
			struct Output *output;
			wl_list_for_each(output, &outputs, link)
				if ( strcmp(output->name, name) == 0 )
					goto found;
			fprintf(stdout, "ERROR: No output with name \"%s\".\n", name);
			ret = EXIT_FAILURE;
			loop = false;
			return;
found:
			zwlr_output_power_v1_set_mode(output->wlr_output_power,
					action == ON ? ZWLR_OUTPUT_POWER_V1_MODE_ON : ZWLR_OUTPUT_POWER_V1_MODE_OFF);
			const struct Output *output = output_from_name(name);
			if ( output == NULL )
			{
				fprintf(stdout, "ERROR: No output with name \"%s\".\n", name);
				ret = EXIT_FAILURE;
				loop = false;
				return;
			}

			enum zwlr_output_power_v1_mode new_mode;
			switch (action)
			{
				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;

				case LIST:
					/* unreachable */
					break;
			}

			zwlr_output_power_v1_set_mode(output->wlr_output_power, new_mode);

			/* We need to sync yet another time because setting the
			 * power mode might fail.


@@ 233,6 266,8 @@ int main(int argc, char *argv[])
			action = ON;
		else if ( strcmp(argv[1], "off") == 0 )
			action = OFF;
		else if ( strcmp(argv[1], "toggle") == 0 )
			action = TOGGLE;
		else
		{
			fputs(usage, stderr);