Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json chooser #105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions contrib/dmenu-chooser-json.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

chooser=$1
jq '.monitor | .[] | .name + ": " + .make + " " + .model' | tr -d '"' | ${chooser:="wofi -d"} | cut -d : -f 1
1 change: 1 addition & 0 deletions include/screencast_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ enum xdpw_chooser_types {
XDPW_CHOOSER_NONE,
XDPW_CHOOSER_SIMPLE,
XDPW_CHOOSER_DMENU,
XDPW_CHOOSER_JSON,
};

struct xdpw_output_chooser {
Expand Down
4 changes: 4 additions & 0 deletions src/screencast/screencast_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ enum xdpw_chooser_types get_chooser_type(const char *chooser_type) {
return XDPW_CHOOSER_SIMPLE;
} else if (strcmp(chooser_type, "dmenu") == 0) {
return XDPW_CHOOSER_DMENU;
} else if (strcmp(chooser_type, "json") == 0) {
return XDPW_CHOOSER_JSON;
}
fprintf(stderr, "Could not understand chooser type %s\n", chooser_type);
exit(1);
Expand All @@ -77,6 +79,8 @@ const char *chooser_type_str(enum xdpw_chooser_types chooser_type) {
return "simple";
case XDPW_CHOOSER_DMENU:
return "dmenu";
case XDPW_CHOOSER_JSON:
return "json";
}
fprintf(stderr, "Could not find chooser type %d\n", chooser_type);
abort();
Expand Down
36 changes: 33 additions & 3 deletions src/screencast/wlr_screencast.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,10 @@ static bool wlr_output_chooser(struct xdpw_output_chooser *chooser,
goto error_fork;
}

FILE *f;
switch (chooser->type) {
case XDPW_CHOOSER_DMENU:;
FILE *f = fdopen(chooser_in[1], "w");
f = fdopen(chooser_in[1], "w");
if (f == NULL) {
perror("fdopen pipe chooser_in");
logprint(ERROR, "Failed to create stream writing to pipe chooser_in");
Expand All @@ -392,6 +393,34 @@ static bool wlr_output_chooser(struct xdpw_output_chooser *chooser,
}
fclose(f);
break;
case XDPW_CHOOSER_JSON:;
f = fdopen(chooser_in[1], "w");
if (f == NULL) {
perror("fdopen pipe chooser_in");
logprint(ERROR, "Failed to create stream writing to pipe chooser_in");
goto error_fork;
}
fprintf(f,"{");
fprintf(f, "\"monitor\":");
fprintf(f,"[");
bool first = true;
wl_list_for_each(out, output_list, link) {
if (first) {
fprintf(f,"{");
first = false;
} else {
fprintf(f,",{");
}
fprintf(f, "\"name\": \"%s\",", out->name);
fprintf(f, "\"make\": \"%s\",", out->make);
fprintf(f, "\"model\": \"%s\",", out->model);
fprintf(f, "\"id\": \"%u\"", out->id);
fprintf(f,"}");
}
fprintf(f,"]");
fprintf(f,"}");
fclose(f);
break;
default:
close(chooser_in[1]);
}
Expand All @@ -401,7 +430,7 @@ static bool wlr_output_chooser(struct xdpw_output_chooser *chooser,
goto end;
}

FILE *f = fdopen(chooser_out[0], "r");
f = fdopen(chooser_out[0], "r");
if (f == NULL) {
perror("fdopen pipe chooser_out");
logprint(ERROR, "Failed to create stream reading from pipe chooser_out");
Expand Down Expand Up @@ -485,7 +514,8 @@ struct xdpw_wlr_output *xdpw_wlr_output_chooser(struct xdpw_screencast_context *
return xdpw_wlr_output_first(&ctx->output_list);
}
case XDPW_CHOOSER_DMENU:
case XDPW_CHOOSER_SIMPLE:;
case XDPW_CHOOSER_SIMPLE:
case XDPW_CHOOSER_JSON:;
struct xdpw_wlr_output *output = NULL;
if (!ctx->state->config->screencast_conf.chooser_cmd) {
logprint(ERROR, "wlroots: no output chooser given");
Expand Down
4 changes: 3 additions & 1 deletion xdg-desktop-portal-wlr.5.scd
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ These options need to be placed under the **[screencast]** section.
(slurp, wofi, bemenu) and will fallback to an arbitrary output if none of those were found.
- none: xdpw will allow screencast either on the output given by **output_name**, or if empty
an arbitrary output without further interaction.
- simple, dmenu: xdpw will launch the chooser given by **chooser_cmd**. For more details
- simple, dmenu, json: xdpw will launch the chooser given by **chooser_cmd**. For more details
see **OUTPUT CHOOSER**.

## OUTPUT CHOOSER
Expand All @@ -83,6 +83,8 @@ The chooser can be any program or script with the following behaviour:
Supported types of choosers via the **chooser_type** option:
- simple: the chooser is just called without anything further on stdin.
- dmenu: the chooser receives a newline separated list (dmenu style) of outputs on stdin.
- json: the chooser receives a list of outputs formated as a json stdin ++
{"monitor":[{"name": "eDP-1","make": "<manufacturer>","model": "<model>","id": "42"}]}

# SEE ALSO

Expand Down