Add "toggle" operation

This commit is contained in:
Leon Henrik Plickat 2021-06-14 23:47:49 +00:00
parent e3f245394a
commit 22f87adf6c
2 changed files with 55 additions and 14 deletions

View File

@ -26,6 +26,12 @@ Set output power mode to off.
.P .P
.RE .RE
\fBwlopm toggle <output-name>\fR
.RS 4
Toggle the output power mode.
.P
.RE
.SH AUTHOR .SH AUTHOR
Leon Henrik Plickat Leon Henrik Plickat

49
wlopm.c
View File

@ -35,6 +35,7 @@ const char usage[] =
"\twlopm List outputs and their power modes.\n" "\twlopm List outputs and their power modes.\n"
"\twlopm on <output-name> Set output power mode to on.\n" "\twlopm on <output-name> Set output power mode to on.\n"
"\twlopm off <output-name> Set output power mode to off.\n" "\twlopm off <output-name> Set output power mode to off.\n"
"\twlopm toggle <output-name> Toggle output power mode.\n"
"\n"; "\n";
enum Action enum Action
@ -42,6 +43,7 @@ enum Action
LIST, LIST,
ON, ON,
OFF, OFF,
TOGGLE,
}; };
enum Action action = LIST; enum Action action = LIST;
@ -148,6 +150,15 @@ static const struct wl_callback_listener sync_callback_listener = {
.done = sync_handle_done, .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) static void sync_handle_done (void *data, struct wl_callback *wl_callback, uint32_t other_data)
{ {
wl_callback_destroy(wl_callback); wl_callback_destroy(wl_callback);
@ -201,17 +212,39 @@ static void sync_handle_done (void *data, struct wl_callback *wl_callback, uint3
} }
else else
{ {
struct Output *output; const struct Output *output = output_from_name(name);
wl_list_for_each(output, &outputs, link) if ( output == NULL )
if ( strcmp(output->name, name) == 0 ) {
goto found;
fprintf(stdout, "ERROR: No output with name \"%s\".\n", name); fprintf(stdout, "ERROR: No output with name \"%s\".\n", name);
ret = EXIT_FAILURE; ret = EXIT_FAILURE;
loop = false; loop = false;
return; 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); 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 /* We need to sync yet another time because setting the
* power mode might fail. * power mode might fail.
@ -233,6 +266,8 @@ int main(int argc, char *argv[])
action = ON; action = ON;
else if ( strcmp(argv[1], "off") == 0 ) else if ( strcmp(argv[1], "off") == 0 )
action = OFF; action = OFF;
else if ( strcmp(argv[1], "toggle") == 0 )
action = TOGGLE;
else else
{ {
fputs(usage, stderr); fputs(usage, stderr);