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
.RE
\fBwlopm toggle <output-name>\fR
.RS 4
Toggle the output power mode.
.P
.RE
.SH AUTHOR
Leon Henrik Plickat

63
wlopm.c
View File

@ -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);