From cea679c1a3592047db4c49b8ac61d917f202a2ff Mon Sep 17 00:00:00 2001 From: Leon Henrik Plickat Date: Tue, 15 Jun 2021 11:48:55 +0000 Subject: [PATCH] Add json output mode --- wlopm.1 | 6 +++++ wlopm.c | 75 ++++++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 61 insertions(+), 20 deletions(-) diff --git a/wlopm.1 b/wlopm.1 index 1880d24..5a446f2 100644 --- a/wlopm.1 +++ b/wlopm.1 @@ -14,6 +14,12 @@ List outputs and their power modes. .P .RE +\fBwlopm --json\fR +.RS 4 +List outputs and their power modes, formatted in JSON. +.P +.RE + \fBwlopm on \fR .RS 4 Set output power mode to on. diff --git a/wlopm.c b/wlopm.c index facec8a..a582c89 100644 --- a/wlopm.c +++ b/wlopm.c @@ -33,6 +33,7 @@ const char usage[] = "Usage:\n" "\twlopm List outputs and their power modes.\n" + "\twlopm --json Format the list as JSON.\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" @@ -47,6 +48,7 @@ enum Action }; enum Action action = LIST; +bool json = false; char *name = NULL; struct Output @@ -159,6 +161,11 @@ static struct Output *output_from_name (const char *str) 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) { 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 ) { struct Output *output; - wl_list_for_each(output, &outputs, link) - fprintf(stdout, "%s %s\n", output->name, - output->mode == ZWLR_OUTPUT_POWER_V1_MODE_ON ? "on" : "off"); + if (json) + { + 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; } else @@ -260,26 +282,39 @@ static void sync_handle_done (void *data, struct wl_callback *wl_callback, uint3 int main(int argc, char *argv[]) { - if ( argc == 3 ) + switch (argc) { - 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 - { + case 1: + break; + + case 2: + if ( strcmp(argv[1], "--json") == 0 ) + json = true; + 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); 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()