@@ 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 <output-name>\fR
.RS 4
Set output power mode to on.
@@ 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 <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"
@@ 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()