diff --git a/wlopm.1 b/wlopm.1 index 3f6aff8..1880d24 100644 --- a/wlopm.1 +++ b/wlopm.1 @@ -26,6 +26,12 @@ Set output power mode to off. .P .RE +\fBwlopm toggle \fR +.RS 4 +Toggle the output power mode. +.P +.RE + .SH AUTHOR Leon Henrik Plickat diff --git a/wlopm.c b/wlopm.c index 2803e9e..facec8a 100644 --- a/wlopm.c +++ b/wlopm.c @@ -32,9 +32,10 @@ const char usage[] = "Usage:\n" - "\twlopm List outputs and their power modes.\n" - "\twlopm on Set output power mode to on.\n" - "\twlopm off Set output power mode to off.\n" + "\twlopm List outputs and their power modes.\n" + "\twlopm on Set output power mode to on.\n" + "\twlopm off Set output power mode to off.\n" + "\twlopm toggle 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);