From 8de978df9130a72b954846df86c41bf463c43e7c Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Mon, 22 Feb 2021 09:11:16 +0100 Subject: [PATCH] Add native helper for the question dialog --- plugins/editor/src/editor/NativeHelpers.cpp | 77 ++++++++++++++++++++- plugins/editor/src/editor/NativeHelpers.h | 1 + plugins/editor/src/editor/NativeHelpers.mm | 10 +++ 3 files changed, 86 insertions(+), 2 deletions(-) diff --git a/plugins/editor/src/editor/NativeHelpers.cpp b/plugins/editor/src/editor/NativeHelpers.cpp index a8cbb255..0a3ac516 100644 --- a/plugins/editor/src/editor/NativeHelpers.cpp +++ b/plugins/editor/src/editor/NativeHelpers.cpp @@ -11,9 +11,20 @@ #include #include +static WCHAR *stringToWideChar(const char *str, int strCch = -1) +{ + unsigned strSize = MultiByteToWideChar(CP_UTF8, 0, str, strCch, nullptr, 0); + if (strSize == 0) + return {}; + std::unique_ptr strW(new WCHAR[strSize]); + if (MultiByteToWideChar(CP_UTF8, 0, str, strCch, strW.get(), strSize) == 0) + return {}; + return strW.release(); +} + bool openFileInExternalEditor(const char *filename) { - std::wstring path = fs::u8path(filename).wstring(); + std::wstring path = stringToWideChar(filename); SHELLEXECUTEINFOW info; memset(&info, 0, sizeof(info)); @@ -30,7 +41,7 @@ bool openFileInExternalEditor(const char *filename) bool openDirectoryInExplorer(const char *filename) { - std::wstring path = fs::u8path(filename).wstring(); + std::wstring path = stringToWideChar(filename); SHELLEXECUTEINFOW info; memset(&info, 0, sizeof(info)); @@ -42,10 +53,23 @@ bool openDirectoryInExplorer(const char *filename) return ShellExecuteExW(&info); } + +bool askQuestion(const char *text) +{ + int ret = MessageBoxW(nullptr, stringToWideChar(text), L"Question", MB_YESNO); + return ret == IDYES; +} #elif defined(__APPLE__) // implemented in NativeHelpers.mm #else #include +#include +#include +#include +#include +#include +#include +extern "C" { extern char **environ; } static bool openFileByMimeType(const char *filename, const char *mimetype) { @@ -72,4 +96,53 @@ bool openDirectoryInExplorer(const char *filename) { return openFileByMimeType(filename, "inode/directory"); } + +static std::vector createForkEnviron() +{ + std::vector newEnv; + newEnv.reserve(256); + for (char **envp = environ; *envp; ++envp) { + // ensure the process will link with system libraries, + // and not these from the Ardour bundle. + if (strncmp(*envp, "LD_LIBRARY_PATH=", 16) == 0) + continue; + newEnv.push_back(*envp); + } + newEnv.push_back(nullptr); + return newEnv; +} + +bool askQuestion(const char *text) +{ + char *argv[] = { + const_cast("/usr/bin/zenity"), + const_cast("--question"), + const_cast("--text"), + const_cast(text), + nullptr, + }; + + std::vector newEnv = createForkEnviron(); + char **envp = newEnv.data(); + + pid_t forkPid = vfork(); + if (forkPid == -1) + return false; + + if (forkPid == 0) { + execve(argv[0], argv, envp); + _exit(1); + } + + int wret; + int wstatus; + do { + wret = waitpid(forkPid, &wstatus, 0); + } while (wret == -1 && errno == EINTR); + + if (wret == -1 || !WIFEXITED(wstatus)) + return false; + + return WEXITSTATUS(wstatus) == 0; +} #endif diff --git a/plugins/editor/src/editor/NativeHelpers.h b/plugins/editor/src/editor/NativeHelpers.h index 3ca310f7..9ccb5410 100644 --- a/plugins/editor/src/editor/NativeHelpers.h +++ b/plugins/editor/src/editor/NativeHelpers.h @@ -8,3 +8,4 @@ bool openFileInExternalEditor(const char *filename); bool openDirectoryInExplorer(const char *filename); +bool askQuestion(const char *text); diff --git a/plugins/editor/src/editor/NativeHelpers.mm b/plugins/editor/src/editor/NativeHelpers.mm index 7fea74e8..56c1143d 100644 --- a/plugins/editor/src/editor/NativeHelpers.mm +++ b/plugins/editor/src/editor/NativeHelpers.mm @@ -31,4 +31,14 @@ bool openDirectoryInExplorer(const char *fileName) { return openFileWithApplication(fileName, @"Finder"); } + +bool askQuestion(const char *text) +{ + NSAlert *alert = [[NSAlert alloc] init]; + [alert setMessageText:[NSString stringWithUTF8String:text]]; + [alert addButtonWithTitle:@"OK"]; + [alert addButtonWithTitle:@"Cancel"]; + NSInteger button = [alert runModal]; + return button == NSAlertFirstButtonReturn; +} #endif