Add json output mode

This commit is contained in:
Leon Henrik Plickat 2021-06-15 11:48:55 +00:00
parent 22f87adf6c
commit cea679c1a3
2 changed files with 61 additions and 20 deletions

View File

@ -14,6 +14,12 @@ List outputs and their power modes.
.P .P
.RE .RE
\fBwlopm --json\fR
.RS 4
List outputs and their power modes, formatted in JSON.
.P
.RE
\fBwlopm on <output-name>\fR \fBwlopm on <output-name>\fR
.RS 4 .RS 4
Set output power mode to on. Set output power mode to on.

75
wlopm.c
View File

@ -33,6 +33,7 @@
const char usage[] = const char usage[] =
"Usage:\n" "Usage:\n"
"\twlopm List outputs and their power modes.\n" "\twlopm List outputs and their power modes.\n"
"\twlopm --json Format the list as JSON.\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" "\twlopm toggle <output-name> Toggle output power mode.\n"
@ -47,6 +48,7 @@ enum Action
}; };
enum Action action = LIST; enum Action action = LIST;
bool json = false;
char *name = NULL; char *name = NULL;
struct Output struct Output
@ -159,6 +161,11 @@ static struct Output *output_from_name (const char *str)
return NULL; return NULL;
} }
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 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);
@ -205,9 +212,24 @@ static void sync_handle_done (void *data, struct wl_callback *wl_callback, uint3
if ( action == LIST ) if ( action == LIST )
{ {
struct Output *output; struct Output *output;
wl_list_for_each(output, &outputs, link) if (json)
fprintf(stdout, "%s %s\n", output->name, {
output->mode == ZWLR_OUTPUT_POWER_V1_MODE_ON ? "on" : "off"); fputs("[\n", stdout);
uint32_t i = 0, len = (uint32_t)wl_list_length(&outputs);
wl_list_for_each(output, &outputs, link)
{
fprintf(stdout, " {\n \"output\": \"%s\",\n \"power-mode\": \"%s\"\n }%s\n",
output->name,
power_mode_to_string(output->mode),
i < (len - 1) ? "," : "");
i++;
}
fputs("]\n", stdout);
}
else
wl_list_for_each(output, &outputs, link)
fprintf(stdout, "%s %s\n", output->name,
power_mode_to_string(output->mode));
loop = false; loop = false;
} }
else else
@ -260,26 +282,39 @@ static void sync_handle_done (void *data, struct wl_callback *wl_callback, uint3
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if ( argc == 3 ) switch (argc)
{ {
if ( strcmp(argv[1], "on") == 0 ) case 1:
action = ON; break;
else if ( strcmp(argv[1], "off") == 0 )
action = OFF; case 2:
else if ( strcmp(argv[1], "toggle") == 0 ) if ( strcmp(argv[1], "--json") == 0 )
action = TOGGLE; json = true;
else else
{ {
fputs(usage, stderr);
return EXIT_FAILURE;
}
break;
case 3:
if ( strcmp(argv[1], "on") == 0 )
action = ON;
else if ( strcmp(argv[1], "off") == 0 )
action = OFF;
else if ( strcmp(argv[1], "toggle") == 0 )
action = TOGGLE;
else
{
fputs(usage, stderr);
return EXIT_FAILURE;
}
name = strdup(argv[2]);
break;
default:
fputs(usage, stderr); fputs(usage, stderr);
return EXIT_FAILURE; return EXIT_FAILURE;
}
name = strdup(argv[2]);
}
else if ( argc != 1 )
{
fputs(usage, stderr);
return EXIT_FAILURE;
} }
/* We query the display name here instead of letting wl_display_connect() /* We query the display name here instead of letting wl_display_connect()