From fec8a5db3d0f6d1fc4aa9dabe4d80bafb1e90638 Mon Sep 17 00:00:00 2001 From: Leon Henrik Plickat Date: Mon, 21 Jun 2021 16:05:36 +0200 Subject: [PATCH] Accept * as parameter to operate on all outputs --- wlopm.1 | 7 ++++ wlopm.c | 100 ++++++++++++++++++++++++++++++++------------------------ 2 files changed, 64 insertions(+), 43 deletions(-) diff --git a/wlopm.1 b/wlopm.1 index 8f65f3d..c97e84e 100644 --- a/wlopm.1 +++ b/wlopm.1 @@ -41,6 +41,13 @@ Toggle the power mode of the output. .RE +.SH OUTPUT NAMES +Output names are compositor dependand. +.P +If instead of an output name * is given as parameter to an operation, wlopm will +do that operation for all discovered outputs. + + .SH AUTHOR Leon Henrik Plickat diff --git a/wlopm.c b/wlopm.c index 3b597b8..5e171c0 100644 --- a/wlopm.c +++ b/wlopm.c @@ -173,11 +173,67 @@ static struct Output *output_from_name (const char *str) 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 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) + { + fprintf(stdout, + "%s\n {\n" + " \"output\": \"%s\",\n" + " \"error\": \"output does not exist\"\n" + " }", + json_prev ? "," : "", + operation->name); + json_prev = true; + } + 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); @@ -264,49 +320,7 @@ static void sync_handle_done (void *data, struct wl_callback *wl_callback, uint3 struct Operation *operation; wl_list_for_each(operation, &operations, link) - { - const struct Output *output = output_from_name(operation->name); - if ( output == NULL ) - { - if (json) - { - fprintf(stdout, - "%s\n {\n" - " \"output\": \"%s\",\n" - " \"error\": \"output does not exist\"\n" - " }", - json_prev ? "," : "", - operation->name); - json_prev = true; - } - else - fprintf(stderr, "ERROR: Output '%s' does not exist.\n", - operation->name); - continue; - } - - enum zwlr_output_power_v1_mode new_mode; - switch (operation->power_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); - } - + do_operation(operation); /* We need to sync yet another time because setting the * power mode might fail and we want to display those