From 32b6d151769b9642a0d6d96929272f7221e7ab53 Mon Sep 17 00:00:00 2001 From: columbarius Date: Thu, 8 Oct 2020 14:47:18 +0200 Subject: [PATCH] Cleanup dmenu launcher in wlr_screencast.c --- src/screencast/wlr_screencast.c | 104 +++++++++++++++++++------------- 1 file changed, 62 insertions(+), 42 deletions(-) diff --git a/src/screencast/wlr_screencast.c b/src/screencast/wlr_screencast.c index 1caa475e..54c9baf3 100644 --- a/src/screencast/wlr_screencast.c +++ b/src/screencast/wlr_screencast.c @@ -381,26 +381,25 @@ 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]); @@ -408,37 +407,37 @@ int xdpw_wlr_output_chooser_dmenu_launcher(char *buffer, unsigned long buffer_ma 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; @@ -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; }