Files
OpenRCT2-Web/src/openrct2-ui/UiContext.Linux.cpp
T

426 lines
16 KiB
C++
Raw Normal View History

2017-04-01 13:38:52 +01:00
/*****************************************************************************
2020-07-21 15:04:34 +02:00
* Copyright (c) 2014-2020 OpenRCT2 developers
2018-06-15 14:07:34 +02:00
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
2017-04-01 13:38:52 +01:00
#if (defined(__unix__) || defined(__EMSCRIPTEN__)) && !defined(__ANDROID__) && !defined(__APPLE__)
2017-04-01 13:38:52 +01:00
2018-07-21 16:17:06 +02:00
# include "UiContext.h"
2018-06-22 23:22:29 +02:00
2018-07-21 16:17:06 +02:00
# include <SDL.h>
# include <dlfcn.h>
# include <openrct2/common.h>
# include <openrct2/core/Path.hpp>
# include <openrct2/core/String.hpp>
2020-10-21 10:53:22 -07:00
# include <openrct2/core/StringBuilder.h>
2018-07-21 16:17:06 +02:00
# include <openrct2/localisation/Localisation.h>
2022-02-18 21:57:00 +01:00
# include <openrct2/platform/Platform.h>
2018-07-21 16:17:06 +02:00
# include <openrct2/ui/UiContext.h>
# include <sstream>
# include <stdexcept>
2018-02-16 00:43:16 +01:00
namespace OpenRCT2::Ui
2017-04-01 13:38:52 +01:00
{
enum class DIALOG_TYPE
{
NONE,
KDIALOG,
ZENITY,
};
2017-05-08 18:11:42 +01:00
class LinuxContext final : public IPlatformUiContext
2017-04-01 13:38:52 +01:00
{
private:
2021-03-20 14:51:15 +01:00
mutable std::optional<bool> _hasFilePicker = std::nullopt;
2017-04-01 13:38:52 +01:00
public:
LinuxContext()
{
}
2018-06-22 23:22:29 +02:00
void SetWindowIcon(SDL_Window* window) override
2017-04-01 13:38:52 +01:00
{
}
bool IsSteamOverlayAttached() override
{
2018-07-21 16:17:06 +02:00
# ifdef __linux__
2017-04-01 13:38:52 +01:00
// See http://syprog.blogspot.ru/2011/12/listing-loaded-shared-objects-in-linux.html
struct lmap
{
2018-06-22 23:22:29 +02:00
void* base_address;
char* path;
void* unused;
lmap* next;
lmap* prev;
2017-04-01 13:38:52 +01:00
};
struct dummy
{
2018-06-22 23:22:29 +02:00
void* pointers[3];
dummy* ptr;
2017-04-01 13:38:52 +01:00
};
bool result = false;
2018-06-22 23:22:29 +02:00
void* processHandle = dlopen(nullptr, RTLD_NOW);
2017-04-01 13:38:52 +01:00
if (processHandle != nullptr)
{
dummy* p = (static_cast<dummy*>(processHandle))->ptr;
lmap* pl = reinterpret_cast<lmap*>(p->ptr);
2017-04-01 13:38:52 +01:00
while (pl != nullptr)
{
if (strstr(pl->path, "gameoverlayrenderer.so") != nullptr)
{
result = true;
break;
}
pl = pl->next;
}
dlclose(processHandle);
}
return result;
2018-07-21 16:17:06 +02:00
# else
2017-06-25 15:25:55 -04:00
return false; // Needed for OpenBSD, likely all other Unixes.
2018-07-21 16:17:06 +02:00
# endif
2017-04-01 13:38:52 +01:00
}
2018-06-22 23:22:29 +02:00
void ShowMessageBox(SDL_Window* window, const std::string& message) override
2017-04-01 13:38:52 +01:00
{
log_verbose(message.c_str());
std::string executablePath;
DIALOG_TYPE dtype = GetDialogApp(&executablePath);
2018-06-22 23:22:29 +02:00
switch (dtype)
2017-04-01 13:38:52 +01:00
{
2018-06-22 23:22:29 +02:00
case DIALOG_TYPE::KDIALOG:
{
2022-06-22 23:46:34 -04:00
std::string cmd = String::StdFormat(
"%s --title \"OpenRCT2\" --msgbox \"%s\"", executablePath.c_str(), message.c_str());
2020-10-22 12:17:28 -07:00
Platform::Execute(cmd);
2018-06-22 23:22:29 +02:00
break;
}
case DIALOG_TYPE::ZENITY:
{
2022-06-22 23:46:34 -04:00
std::string cmd = String::StdFormat(
2018-06-22 23:22:29 +02:00
"%s --title=\"OpenRCT2\" --info --text=\"%s\"", executablePath.c_str(), message.c_str());
2020-10-22 12:17:28 -07:00
Platform::Execute(cmd);
2018-06-22 23:22:29 +02:00
break;
}
default:
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_WARNING, "OpenRCT2", message.c_str(), window);
break;
2017-04-01 13:38:52 +01:00
}
}
2018-09-04 15:38:19 +02:00
void OpenFolder(const std::string& path) override
{
2022-06-22 23:46:34 -04:00
std::string cmd = String::StdFormat("xdg-open %s", EscapePathForShell(path).c_str());
2020-10-22 12:17:28 -07:00
Platform::Execute(cmd);
2018-09-04 15:38:19 +02:00
}
void OpenURL(const std::string& url) override
{
2022-06-22 23:46:34 -04:00
std::string cmd = String::StdFormat("xdg-open %s", url.c_str());
2020-10-22 12:17:28 -07:00
Platform::Execute(cmd);
}
2018-06-22 23:22:29 +02:00
std::string ShowFileDialog(SDL_Window* window, const FileDialogDesc& desc) override
{
std::string result;
std::string executablePath;
u8string directory = EscapePathForShell(desc.InitialDirectory + '/');
DIALOG_TYPE dtype = GetDialogApp(&executablePath);
2018-06-22 23:22:29 +02:00
switch (dtype)
{
2018-06-22 23:22:29 +02:00
case DIALOG_TYPE::KDIALOG:
{
2022-01-28 23:50:24 +01:00
std::string action = (desc.Type == FileDialogType::Open) ? "--getopenfilename" : "--getsavefilename";
2018-06-22 23:22:29 +02:00
std::string filter = GetKDialogFilterString(desc.Filters);
std::string cmd = String::StdFormat(
"%s --title '%s' %s %s '%s'", executablePath.c_str(), desc.Title.c_str(), action.c_str(),
directory.c_str(), filter.c_str());
2018-06-22 23:22:29 +02:00
std::string output;
2020-10-22 12:17:28 -07:00
if (Platform::Execute(cmd, &output) == 0)
2018-06-22 23:22:29 +02:00
{
result = output;
}
break;
}
2018-06-22 23:22:29 +02:00
case DIALOG_TYPE::ZENITY:
{
2018-06-22 23:22:29 +02:00
std::string action = "--file-selection";
std::string flags;
2022-01-28 23:50:24 +01:00
if (desc.Type == FileDialogType::Save)
{
2018-06-22 23:22:29 +02:00
flags = "--confirm-overwrite --save";
}
std::string filters = GetZenityFilterString(desc.Filters);
std::string cmd = String::StdFormat(
"%s %s --filename=%s %s --title='%s' / %s", executablePath.c_str(), action.c_str(), directory.c_str(),
flags.c_str(), desc.Title.c_str(), filters.c_str());
2018-06-22 23:22:29 +02:00
std::string output;
2020-10-22 12:17:28 -07:00
if (Platform::Execute(cmd, &output) == 0)
2018-06-22 23:22:29 +02:00
{
2022-01-28 23:50:24 +01:00
if (desc.Type == FileDialogType::Save)
{
2018-06-22 23:22:29 +02:00
// The default file extension is taken from the **first** available filter, since
// we cannot obtain it from zenity's output. This means that the FileDialogDesc::Filters
// array must be carefully populated, at least the first element.
std::string pattern = desc.Filters[0].Pattern;
std::string defaultExtension = pattern.substr(pattern.find_last_of('.'));
2022-01-08 13:57:29 +01:00
const auto filename = Path::GetFileName(output.c_str());
2018-06-22 23:22:29 +02:00
// If there is no extension, append the pattern
2022-01-08 13:57:29 +01:00
const auto extension = Path::GetExtension(filename);
2018-06-22 23:22:29 +02:00
result = output;
if (extension[0] == '\0' && !defaultExtension.empty())
{
result = output.append(defaultExtension);
}
}
else
{
result = output;
}
}
2018-06-22 23:22:29 +02:00
break;
}
2018-06-22 23:22:29 +02:00
default:
ThrowMissingDialogApp();
break;
}
2017-05-27 20:00:44 +01:00
if (!result.empty())
{
2022-01-28 23:50:24 +01:00
if (desc.Type == FileDialogType::Open && access(result.c_str(), F_OK) == -1)
{
2018-06-22 23:22:29 +02:00
std::string msg = String::StdFormat(
"\"%s\" not found: %s, please choose another file\n", result.c_str(), strerror(errno));
2017-05-27 20:00:44 +01:00
ShowMessageBox(window, msg);
return ShowFileDialog(window, desc);
}
2022-01-28 23:50:24 +01:00
if (desc.Type == FileDialogType::Save && access(result.c_str(), F_OK) != -1 && dtype == DIALOG_TYPE::KDIALOG)
2017-05-27 20:00:44 +01:00
{
std::string cmd = String::StdFormat("%s --yesno \"Overwrite %s?\"", executablePath.c_str(), result.c_str());
2020-10-22 12:17:28 -07:00
if (Platform::Execute(cmd) != 0)
2017-05-27 20:00:44 +01:00
{
result = std::string();
}
}
}
return result;
}
2018-06-22 23:22:29 +02:00
std::string ShowDirectoryDialog(SDL_Window* window, const std::string& title) override
{
std::string result;
std::string executablePath;
DIALOG_TYPE dtype = GetDialogApp(&executablePath);
2018-06-22 23:22:29 +02:00
switch (dtype)
{
2018-06-22 23:22:29 +02:00
case DIALOG_TYPE::KDIALOG:
{
2018-06-22 23:22:29 +02:00
std::string output;
2022-06-22 23:46:34 -04:00
std::string cmd = String::StdFormat(
"%s --title '%s' --getexistingdirectory /", executablePath.c_str(), title.c_str());
2020-10-22 12:17:28 -07:00
if (Platform::Execute(cmd, &output) == 0)
2018-06-22 23:22:29 +02:00
{
result = output;
}
break;
}
2018-06-22 23:22:29 +02:00
case DIALOG_TYPE::ZENITY:
{
2018-06-22 23:22:29 +02:00
std::string output;
2022-06-22 23:46:34 -04:00
std::string cmd = String::StdFormat(
2018-06-22 23:22:29 +02:00
"%s --title='%s' --file-selection --directory /", executablePath.c_str(), title.c_str());
2020-10-22 12:17:28 -07:00
if (Platform::Execute(cmd, &output) == 0)
2018-06-22 23:22:29 +02:00
{
result = output;
}
break;
}
2018-06-22 23:22:29 +02:00
default:
ThrowMissingDialogApp();
break;
}
return result;
}
2020-11-08 13:29:44 +01:00
bool HasFilePicker() const override
{
2021-03-20 14:51:15 +01:00
if (!_hasFilePicker.has_value())
{
std::string dummy;
_hasFilePicker = (GetDialogApp(&dummy) != DIALOG_TYPE::NONE);
}
return _hasFilePicker.value();
2020-11-08 13:29:44 +01:00
}
2020-10-21 10:53:22 -07:00
bool HasMenuSupport() override
{
std::string executablePath;
DIALOG_TYPE dtype = GetDialogApp(&executablePath);
return dtype != DIALOG_TYPE::NONE;
}
int32_t ShowMenuDialog(
const std::vector<std::string>& options, const std::string& title, const std::string& text) override
{
std::string executablePath;
DIALOG_TYPE dtype = GetDialogApp(&executablePath);
size_t longest_string = 0;
for (const auto& option : options)
{
if (option.size() > longest_string)
{
longest_string = option.size();
}
}
// zenity and kdialog don't support automatic scaling, this is an approximation
int width = (longest_string + 1) * 8;
int height = (options.size() + 1) * 8;
switch (dtype)
{
case DIALOG_TYPE::ZENITY:
{
auto sb = StringBuilder();
2022-06-22 23:46:34 -04:00
sb.Append(String::StdFormat("zenity --list --column '' --width=%d --height=%d", width, height));
2020-10-21 10:53:22 -07:00
for (const auto& option : options)
{
2022-06-22 23:46:34 -04:00
sb.Append(String::StdFormat(" '%s'", option.c_str()));
2020-10-21 10:53:22 -07:00
}
2022-06-22 23:46:34 -04:00
sb.Append(String::StdFormat(" --title '%s' --text '%s'", title.c_str(), text.c_str()));
2020-10-21 10:53:22 -07:00
std::string buff;
Platform::Execute(sb.GetBuffer(), &buff);
return std::find(options.begin(), options.end(), buff) - options.begin();
}
case DIALOG_TYPE::KDIALOG:
{
auto sb = StringBuilder();
2022-06-22 23:46:34 -04:00
sb.Append(String::StdFormat("kdialog --geometry %dx%d --title '%s' --menu ", width, height, title.c_str()));
sb.Append(String::StdFormat(" '%s'", text.c_str()));
2020-10-21 10:53:22 -07:00
for (const auto& option : options)
{
2022-06-22 23:46:34 -04:00
sb.Append(String::StdFormat(" '%s' '%s'", option.c_str(), option.c_str()));
2020-10-21 10:53:22 -07:00
}
std::string buff;
Platform::Execute(sb.GetBuffer(), &buff);
return std::find(options.begin(), options.end(), buff) - options.begin();
}
default:
{
break;
}
}
return options.size();
}
2017-04-01 13:38:52 +01:00
private:
2018-06-22 23:22:29 +02:00
static DIALOG_TYPE GetDialogApp(std::string* executablePath)
2017-04-01 13:38:52 +01:00
{
// Prefer zenity as it offers more required features, e.g., overwrite
2018-07-07 14:53:50 -04:00
// confirmation and selecting only existing files.
// Silence error output with 2> /dev/null to avoid confusion in the
// case where a user does not have zenity and/or kdialog.
// OpenRCT2 will fall back to an SDL pop-up if the user has neither.
2020-10-22 12:17:28 -07:00
if (Platform::FindApp("zenity", executablePath))
2017-04-01 13:38:52 +01:00
{
return DIALOG_TYPE::ZENITY;
}
2020-10-22 12:17:28 -07:00
if (Platform::FindApp("kdialog", executablePath))
2017-04-01 13:38:52 +01:00
{
return DIALOG_TYPE::KDIALOG;
}
return DIALOG_TYPE::NONE;
}
static std::string GetKDialogFilterString(const std::vector<FileDialogDesc::Filter> filters)
{
std::stringstream filtersb;
bool first = true;
2018-06-22 23:22:29 +02:00
for (const auto& filter : filters)
{
2022-04-16 18:39:36 +02:00
if (!first)
{
2022-04-16 18:39:36 +02:00
filtersb << "\\n";
}
2022-04-16 18:39:36 +02:00
first = false;
2022-04-16 18:39:36 +02:00
filtersb << filter.Name.c_str() << " (";
AddFilterCaseInsensitive(filtersb, filter.Pattern);
filtersb << ")";
}
return filtersb.str();
}
static std::string GetZenityFilterString(const std::vector<FileDialogDesc::Filter> filters)
{
std::stringstream filtersb;
2018-06-22 23:22:29 +02:00
for (const auto& filter : filters)
{
filtersb << " --file-filter='" << filter.Name << " | ";
2022-04-16 18:39:36 +02:00
AddFilterCaseInsensitive(filtersb, filter.Pattern);
filtersb << "'";
}
return filtersb.str();
}
2022-04-16 18:39:36 +02:00
static void AddFilterCaseInsensitive(std::stringstream& stream, u8string pattern)
{
for (char c : pattern)
{
if (c == ';')
{
stream << ' ';
}
else if (isalpha(static_cast<unsigned char>(c)))
{
auto uc = static_cast<unsigned char>(c);
stream << '[' << static_cast<char>(tolower(uc)) << static_cast<char>(toupper(uc)) << ']';
}
else
{
stream << c;
}
}
}
static void ThrowMissingDialogApp()
{
auto uiContext = GetContext()->GetUiContext();
std::string dialogMissingWarning = language_get_string(STR_MISSING_DIALOG_APPLICATION_ERROR);
uiContext->ShowMessageBox(dialogMissingWarning);
throw std::runtime_error(dialogMissingWarning);
}
2018-09-05 19:41:14 +02:00
static std::string EscapePathForShell(std::string path)
2018-09-05 19:41:14 +02:00
{
for (size_t index = 0; (index = path.find('"', index)) != std::string::npos; index += 2)
2018-09-05 19:41:14 +02:00
{
path.replace(index, 1, "\\\"");
2018-09-05 19:41:14 +02:00
}
return '"' + path + '"';
2018-09-05 19:41:14 +02:00
}
2017-04-01 13:38:52 +01:00
};
2022-07-28 10:19:38 -07:00
std::unique_ptr<IPlatformUiContext> CreatePlatformUiContext()
2017-04-01 13:38:52 +01:00
{
2022-07-28 10:19:38 -07:00
return std::make_unique<LinuxContext>();
2017-04-01 13:38:52 +01:00
}
2018-05-04 22:40:09 +02:00
} // namespace OpenRCT2::Ui
2017-04-01 13:38:52 +01:00
#endif // (defined(__unix__) || defined(__EMSCRIPTEN__)) && !defined(__ANDROID__) && !defined(__APPLE__)