Skip to content

Commit

Permalink
Use execvpe to execute zenity and kdialog
Browse files Browse the repository at this point in the history
This removes the hard-coding of zenity or kdialog paths. Zenity is preferred still.
  • Loading branch information
paulfd authored and redtide committed May 16, 2023
1 parent 3cf80b6 commit 2116eac
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions vstgui/lib/platform/linux/x11fileselector.cpp
Expand Up @@ -3,6 +3,7 @@
// distribution and at http://github.com/steinbergmedia/vstgui/LICENSE

#include "x11fileselector.h"
#include "vstgui/lib/vstguibase.h"
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
Expand All @@ -18,13 +19,13 @@ extern "C" { extern char **environ; }
namespace VSTGUI {
namespace X11 {

static constexpr auto kdialogpath = "/usr/bin/kdialog";
static constexpr auto zenitypath = "/usr/bin/zenity";
static constexpr auto kdialogpath = "kdialog";
static constexpr auto zenitypath = "zenity";

//------------------------------------------------------------------------
struct FileSelector : IPlatformFileSelector
{
FileSelector (PlatformFileSelectorStyle style) : style (style) { identifiyExDialogType (); }
FileSelector (PlatformFileSelectorStyle style) : style (style) {}

~FileSelector () noexcept { closeProcess (); }

Expand All @@ -36,15 +37,12 @@ struct FileSelector : IPlatformFileSelector

bool runDialog (const PlatformFileSelectorConfig& config)
{
switch (exDialogType)
{
case ExDialogType::kdialog:
return runKDialog (config);
case ExDialogType::zenity:
return runZenity (config);
case ExDialogType::none:
break;
}
if (runZenity (config))
return true;

if (runKDialog (config))
return true;

return false;
}

Expand Down Expand Up @@ -89,14 +87,6 @@ struct FileSelector : IPlatformFileSelector
zenity
};

void identifiyExDialogType ()
{
if (access (zenitypath, X_OK) != -1)
exDialogType = ExDialogType::zenity;
if (access (kdialogpath, X_OK) != -1)
exDialogType = ExDialogType::kdialog;
}

bool runKDialog (const PlatformFileSelectorConfig& config)
{
std::vector<std::string> args;
Expand Down Expand Up @@ -199,7 +189,9 @@ struct FileSelector : IPlatformFileSelector
return false;

if (forkPid == 0) {
execute (argv, envp, rw.fd);
if (!execute (argv, envp, rw.fd))
return false;

assert (false);
}

Expand All @@ -213,15 +205,21 @@ struct FileSelector : IPlatformFileSelector
return true;
}

[[noreturn]]
static void execute (char* argv[], char* envp[], const int pipeFd[2])
static bool execute (char* argv[], char* envp[], const int pipeFd[2])
{
close (pipeFd[0]);
if (dup2 (pipeFd[1], STDOUT_FILENO) == -1)
_exit (1);
close (pipeFd[1]);
execve (argv[0], argv, envp);
#if LINUX
if (execvpe (argv[0], argv, envp) == -1)
#else // BSD
if (execve (argv[0], argv, envp) == -1)
#endif
return false;

_exit (1);
return true; // not reachable
}

void closeProcess ()
Expand Down

0 comments on commit 2116eac

Please sign in to comment.