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
.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.

47
wlopm.c
View File

@ -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;
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,
output->mode == ZWLR_OUTPUT_POWER_V1_MODE_ON ? "on" : "off");
power_mode_to_string(output->mode));
loop = false;
}
else
@ -260,8 +282,22 @@ static void sync_handle_done (void *data, struct wl_callback *wl_callback, uint3
int main(int argc, char *argv[])
{
if ( argc == 3 )
switch (argc)
{
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 )
@ -273,11 +309,10 @@ int main(int argc, char *argv[])
fputs(usage, stderr);
return EXIT_FAILURE;
}
name = strdup(argv[2]);
}
else if ( argc != 1 )
{
break;
default:
fputs(usage, stderr);
return EXIT_FAILURE;
}