Skip to content

Commit

Permalink
Cleanup dmenu launcher in wlr_screencast.c
Browse files Browse the repository at this point in the history
  • Loading branch information
columbarius committed Oct 8, 2020
1 parent e24e913 commit 32b6d15
Showing 1 changed file with 62 additions and 42 deletions.
104 changes: 62 additions & 42 deletions src/screencast/wlr_screencast.c
Original file line number Diff line number Diff line change
Expand Up @@ -381,64 +381,63 @@ struct xdpw_wlr_output *wlr_output_chooser_slurp(struct wl_list *output_list) {
return NULL;
}

int xdpw_wlr_output_chooser_dmenu_launcher(char *buffer, unsigned long buffer_maxlength, char *name, unsigned long name_maxlength) {
static bool exec_chooser_dmenu(char *const argv[], char *buffer, unsigned long buffer_maxlength, char *name, unsigned long name_maxlength) {
int p1[2]; //p -> c
int p2[2]; //c -> p

if (pipe(p1) == -1) {
printf("Failed to open pipe\n");
return -1;
logprint(ERROR,"Failed to open pipe");
return false;
}
if (pipe(p2) == -1) {
printf("Failed to open pipe\n");
return -1;
logprint(ERROR,"Failed to open pipe");
return false;
}

int id = fork();
pid_t pid = fork();

if (id == -1) {
printf("Failed to fork\n");
return 2;
}
if (id == 0) {
if (pid < 0) {
perror("fork");
return false;
} else if (pid == 0) {
close(p1[1]);
close(p2[0]);

dup2(p1[0], STDIN_FILENO);
dup2(p2[1], STDOUT_FILENO);
close(p1[0]);
close(p2[1]);

int err;
char *wofiargs[] = {"wofi" , "-d", "-n", NULL};
err = execvp(wofiargs[0], wofiargs);
char *bmenuargs[] = {"bemenu", NULL};
err = execvp(bmenuargs[0], bmenuargs);
err = execvp(argv[0], argv);
if (err == -1) {
printf("Could not find external chooser\n");
return -1;
}
abort();
} else {
close(p1[0]);
close(p2[1]);
if (write(p1[1],buffer,buffer_maxlength*sizeof(char)) == -1) {
return 2;
}
close(p1[1]);
if (read(p2[0],name,name_maxlength*sizeof(char)) == -1) {
return 3;
}
buffer[name_maxlength] = '\0';
char *p = strchr(name, '\n');
if (p != NULL) {
*p = '\0';
perror("execvp");
logprint(WARN,"Failed to execute %s",argv[0]);
return false;
}
close(p2[0]);
return 0;
exit(127);
}
close(p1[0]);
close(p2[1]);

if (write(p1[1],buffer,buffer_maxlength*sizeof(char)) == -1) {
return false;
}
close(p1[1]);
if (read(p2[0],name,name_maxlength*sizeof(char)) == -1) {
return false;
}

buffer[name_maxlength -1] = '\0';
char *p = strchr(name, '\n');
if (p != NULL) {
*p = '\0';
}
close(p2[0]);
return true;
}

struct xdpw_wlr_output *xdpw_wlr_output_chooser_dmenu(struct wl_list *output_list) {
struct xdpw_wlr_output *wlr_output_chooser_dmenu(struct wl_list *output_list) {

logprint(DEBUG, "wlroots: output chooser dmenu called");
struct xdpw_wlr_output *output, *tmp;
Expand Down Expand Up @@ -468,13 +467,34 @@ struct xdpw_wlr_output *xdpw_wlr_output_chooser_dmenu(struct wl_list *output_lis
}
buffer[maxlength - 1] = '\0';

logprint(TRACE, "wlroots: call dmenu launcher");
xdpw_wlr_output_chooser_dmenu_launcher(buffer, maxlength, name, namelength);

wl_list_for_each_safe(output, tmp, output_list, link) {
if (strcmp(output->name, name) == 0) {
return output;
char *argv_wofi[] = {
"wofi",
"-d",
"-n",
NULL,
};

char *argv_bemenu[] = {
"bemenu",
NULL,
};

void *argvs[] = {
argv_wofi,
argv_bemenu,
NULL
};

i = 0;
while (argvs[i] != NULL) {
if (exec_chooser_dmenu(argvs[i], buffer, maxlength, name, namelength)) {
wl_list_for_each_safe(output, tmp, output_list, link) {
if (strcmp(output->name, name) == 0) {
return output;
}
}
}
i++;
}
return NULL;
}
Expand Down

0 comments on commit 32b6d15

Please sign in to comment.