chore: use lizardbyte-common in c++ (#5356)
Co-authored-by: Kishi <41839133+Kishi85@users.noreply.github.com>
This commit is contained in:
@@ -953,21 +953,6 @@ namespace platf {
|
||||
|
||||
void restart();
|
||||
|
||||
/**
|
||||
* @brief Set an environment variable.
|
||||
* @param name The name of the environment variable.
|
||||
* @param value The value to set the environment variable to.
|
||||
* @return 0 on success, non-zero on failure.
|
||||
*/
|
||||
int set_env(const std::string &name, const std::string &value);
|
||||
|
||||
/**
|
||||
* @brief Unset an environment variable.
|
||||
* @param name The name of the environment variable.
|
||||
* @return 0 on success, non-zero on failure.
|
||||
*/
|
||||
int unset_env(const std::string &name);
|
||||
|
||||
/**
|
||||
* @brief Platform buffer pointer and size for batched socket sends.
|
||||
*/
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
* @file src/platform/linux/input/inputtino_seat.cpp
|
||||
* @brief Implementation for multi-seat naming (udev-only).
|
||||
*/
|
||||
// standard includes
|
||||
#include <cstdlib>
|
||||
// lib includes
|
||||
#include <lizardbyte/common/env.h>
|
||||
|
||||
// local includes
|
||||
#include "inputtino_seat.h"
|
||||
@@ -11,10 +11,8 @@
|
||||
namespace platf::inputtino_seat {
|
||||
|
||||
std::string get_target_seat() {
|
||||
if (const char *seat = std::getenv("XDG_SEAT")) {
|
||||
if (seat[0] != '\0') {
|
||||
return seat;
|
||||
}
|
||||
if (std::string seat; lizardbyte::common::get_env("XDG_SEAT", seat) && !seat.empty()) {
|
||||
return seat;
|
||||
}
|
||||
|
||||
return {};
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include <thread>
|
||||
|
||||
// lib includes
|
||||
#include <lizardbyte/common/env.h>
|
||||
#include <pipewire/pipewire.h>
|
||||
#include <poll.h>
|
||||
#include <unistd.h>
|
||||
@@ -55,7 +56,7 @@ namespace kwin {
|
||||
* @return True when KWin reports that the permission system is disabled.
|
||||
*/
|
||||
static bool is_permission_system_deactivated() {
|
||||
return getenvstr("KWIN_WAYLAND_NO_PERMISSION_CHECKS") == "1";
|
||||
return lizardbyte::common::get_env("KWIN_WAYLAND_NO_PERMISSION_CHECKS") == "1";
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -160,17 +161,9 @@ namespace kwin {
|
||||
static inline bool initialized = false;
|
||||
static inline bool create_file = true;
|
||||
|
||||
static std::string getenvstr(std::string const &key) {
|
||||
char const *val = std::getenv(key.c_str());
|
||||
if (!val) {
|
||||
return "";
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
static std::filesystem::path get_home_dir() {
|
||||
// Check HOME environment variable
|
||||
if (std::string homedir = getenvstr("HOME"); !homedir.empty()) {
|
||||
if (std::string homedir = lizardbyte::common::get_env("HOME"); !homedir.empty()) {
|
||||
return homedir;
|
||||
}
|
||||
// Fall back to home directory from NSS passwd
|
||||
@@ -182,7 +175,7 @@ namespace kwin {
|
||||
// Follow the XDG base directory specification for user data home:
|
||||
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
||||
std::filesystem::path xdg_data_home;
|
||||
if (std::string dir = getenvstr("XDG_DATA_HOME"); !dir.empty()) {
|
||||
if (std::string dir = lizardbyte::common::get_env("XDG_DATA_HOME"); !dir.empty()) {
|
||||
xdg_data_home = std::filesystem::path(dir);
|
||||
} else {
|
||||
const auto homedir = get_home_dir();
|
||||
@@ -222,7 +215,7 @@ namespace kwin {
|
||||
static bool check_kwin_system_permissions(const std::string_view &filenameprefix, const std::string_view &executablepath) {
|
||||
// Find data dirs to check from XDG_DATA_DIRS
|
||||
std::vector<std::string> xdg_data_dirs;
|
||||
if (const std::string e = getenvstr("XDG_DATA_DIRS"); !e.empty()) {
|
||||
if (const std::string e = lizardbyte::common::get_env("XDG_DATA_DIRS"); !e.empty()) {
|
||||
std::stringstream ss(e);
|
||||
std::string item;
|
||||
|
||||
@@ -322,13 +315,13 @@ namespace kwin {
|
||||
screencast_permission_helper_t::setup();
|
||||
}
|
||||
|
||||
const char *wl_name = std::getenv("WAYLAND_DISPLAY");
|
||||
if (!wl_name) {
|
||||
std::string wl_name;
|
||||
if (!lizardbyte::common::get_env("WAYLAND_DISPLAY", wl_name)) {
|
||||
BOOST_LOG(error) << "[kwingrab] WAYLAND_DISPLAY not set"sv;
|
||||
return -1;
|
||||
}
|
||||
|
||||
wl_display = wl_display_connect(wl_name);
|
||||
wl_display = wl_display_connect(wl_name.c_str());
|
||||
if (!wl_display) {
|
||||
BOOST_LOG(error) << "[kwingrab] cannot connect to Wayland display: "sv << wl_name;
|
||||
return -1;
|
||||
|
||||
@@ -44,6 +44,7 @@
|
||||
#include <boost/asio/ip/host_name.hpp>
|
||||
#include <boost/process/v1.hpp>
|
||||
#include <fcntl.h>
|
||||
#include <lizardbyte/common/env.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#ifdef SUNSHINE_BUILD_DRM
|
||||
@@ -179,38 +180,35 @@ namespace platf {
|
||||
std::call_once(migration_flag, []() {
|
||||
bool found = false;
|
||||
bool migrate_config = true;
|
||||
const char *dir;
|
||||
const char *homedir;
|
||||
const char *migrate_envvar;
|
||||
fs::path homedir {lizardbyte::common::get_env("HOME")};
|
||||
|
||||
// Get the home directory
|
||||
if ((homedir = getenv("HOME")) == nullptr || strlen(homedir) == 0) {
|
||||
if (homedir.empty()) {
|
||||
// If HOME is empty or not set, use the current user's home directory
|
||||
homedir = getpwuid(geteuid())->pw_dir;
|
||||
}
|
||||
|
||||
// May be set if running under a systemd service with the ConfigurationDirectory= option set.
|
||||
if ((dir = getenv("CONFIGURATION_DIRECTORY")) != nullptr && strlen(dir) > 0) {
|
||||
if (std::string dir; lizardbyte::common::get_env("CONFIGURATION_DIRECTORY", dir) && !dir.empty()) {
|
||||
found = true;
|
||||
config_path = fs::path(dir) / "sunshine"sv;
|
||||
}
|
||||
// Otherwise, follow the XDG base directory specification:
|
||||
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
||||
if (!found && (dir = getenv("XDG_CONFIG_HOME")) != nullptr && strlen(dir) > 0) {
|
||||
if (std::string dir; !found && lizardbyte::common::get_env("XDG_CONFIG_HOME", dir) && !dir.empty()) {
|
||||
found = true;
|
||||
config_path = fs::path(dir) / "sunshine"sv;
|
||||
}
|
||||
// As a last resort, use the home directory
|
||||
if (!found) {
|
||||
migrate_config = false;
|
||||
config_path = fs::path(homedir) / ".config/sunshine"sv;
|
||||
config_path = homedir / ".config" / "sunshine";
|
||||
}
|
||||
|
||||
// migrate from the old config location if necessary
|
||||
migrate_envvar = getenv("SUNSHINE_MIGRATE_CONFIG");
|
||||
if (migrate_config && found && migrate_envvar && strcmp(migrate_envvar, "1") == 0) {
|
||||
if (std::string migrate_envvar; migrate_config && found && lizardbyte::common::get_env("SUNSHINE_MIGRATE_CONFIG", migrate_envvar) && migrate_envvar == "1") {
|
||||
std::error_code ec;
|
||||
fs::path old_config_path = fs::path(homedir) / ".config/sunshine"sv;
|
||||
fs::path old_config_path = homedir / ".config" / "sunshine";
|
||||
if (old_config_path != config_path && fs::exists(old_config_path, ec)) {
|
||||
if (!fs::exists(config_path, ec)) {
|
||||
std::cout << "Migrating config from "sv << old_config_path << " to "sv << config_path << std::endl;
|
||||
@@ -365,7 +363,7 @@ namespace platf {
|
||||
*/
|
||||
void open_url(const std::string &url) {
|
||||
// set working dir to user home directory
|
||||
auto working_dir = boost::filesystem::path(std::getenv("HOME"));
|
||||
auto working_dir = boost::filesystem::path(lizardbyte::common::get_env("HOME"));
|
||||
std::string cmd = R"(xdg-open ")" + url + R"(")";
|
||||
|
||||
boost::process::v1::environment _env = boost::this_process::environment();
|
||||
@@ -506,42 +504,6 @@ namespace platf {
|
||||
lifetime::exit_sunshine(0, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Read an environment variable as an optional string.
|
||||
*
|
||||
* @param name Human-readable name to assign.
|
||||
* @return Environment variable value, or an empty string when unset.
|
||||
*/
|
||||
std::string get_env(const std::string &name) {
|
||||
if (const auto value = getenv(name.c_str()); value != nullptr) {
|
||||
return value;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
int set_env(const std::string &name, const std::string &value) {
|
||||
return setenv(name.c_str(), value.c_str(), 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Append a value to a separator-delimited environment variable.
|
||||
*
|
||||
* @param name Human-readable name to assign.
|
||||
* @param value Entry to add when it is not already present.
|
||||
* @param separator Character used to join or split the value.
|
||||
* @return Result from updating the environment variable.
|
||||
*/
|
||||
int append_env(const std::string &name, const std::string &value, const std::string &separator) {
|
||||
if (const std::string old_value = get_env(name); !old_value.contains(value)) {
|
||||
return set_env(name, old_value.empty() ? value : old_value + separator + value);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int unset_env(const std::string &name) {
|
||||
return unsetenv(name.c_str());
|
||||
}
|
||||
|
||||
bool request_process_group_exit(std::uintptr_t native_handle) {
|
||||
if (kill(-((pid_t) native_handle), SIGTERM) == 0 || errno == ESRCH) {
|
||||
BOOST_LOG(debug) << "Successfully sent SIGTERM to process group: "sv << native_handle;
|
||||
@@ -1267,25 +1229,25 @@ namespace platf {
|
||||
std::unique_ptr<deinit_t> init() {
|
||||
// enable low latency mode for AMD
|
||||
// https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/30039
|
||||
set_env("AMD_DEBUG", "lowlatencyenc");
|
||||
lizardbyte::common::set_env("AMD_DEBUG", "lowlatencyenc");
|
||||
|
||||
// enable Vulkan video extensions for AMD RADV
|
||||
set_env("RADV_PERFTEST", "video_encode");
|
||||
lizardbyte::common::set_env("RADV_PERFTEST", "video_encode");
|
||||
// Above is deprecated on Mesa 26.1+ and replaced by (keep both to ensure best compatibility):
|
||||
append_env("RADV_EXPERIMENTAL", "video_encode", ",");
|
||||
lizardbyte::common::append_env("RADV_EXPERIMENTAL", "video_encode", ",");
|
||||
|
||||
// These are allowed to fail.
|
||||
gbm::init();
|
||||
|
||||
window_system = window_system_e::NONE;
|
||||
#ifdef SUNSHINE_BUILD_WAYLAND
|
||||
if (std::getenv("WAYLAND_DISPLAY")) {
|
||||
if (std::string v; lizardbyte::common::get_env("WAYLAND_DISPLAY", v)) {
|
||||
window_system = window_system_e::WAYLAND;
|
||||
}
|
||||
#endif
|
||||
#if defined(SUNSHINE_BUILD_X11) || defined(SUNSHINE_BUILD_CUDA)
|
||||
if (std::getenv("DISPLAY") && window_system != window_system_e::WAYLAND) {
|
||||
if (std::getenv("WAYLAND_DISPLAY")) {
|
||||
if (std::string v; lizardbyte::common::get_env("DISPLAY", v) && window_system != window_system_e::WAYLAND) {
|
||||
if (lizardbyte::common::get_env("WAYLAND_DISPLAY", v)) {
|
||||
BOOST_LOG(warning) << "Wayland detected, yet sunshine will use X11 for screencasting, screencasting will only work on XWayland applications"sv;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,9 +2,6 @@
|
||||
* @file src/platform/linux/wayland.cpp
|
||||
* @brief Definitions for Wayland capture.
|
||||
*/
|
||||
// standard includes
|
||||
#include <cstdlib>
|
||||
|
||||
// platform includes
|
||||
#include <drm_fourcc.h>
|
||||
#include <fcntl.h>
|
||||
@@ -15,6 +12,9 @@
|
||||
#include <wayland-util.h>
|
||||
#include <xf86drm.h>
|
||||
|
||||
// lib includes
|
||||
#include <lizardbyte/common/env.h>
|
||||
|
||||
// local includes
|
||||
#include "graphics.h"
|
||||
#include "src/logging.h"
|
||||
@@ -53,8 +53,11 @@ namespace wl {
|
||||
};
|
||||
|
||||
int display_t::init(const char *display_name) {
|
||||
std::string env_display_name;
|
||||
if (!display_name) {
|
||||
display_name = std::getenv("WAYLAND_DISPLAY");
|
||||
if (lizardbyte::common::get_env("WAYLAND_DISPLAY", env_display_name)) {
|
||||
display_name = env_display_name.c_str();
|
||||
}
|
||||
}
|
||||
|
||||
if (!display_name) {
|
||||
|
||||
@@ -296,14 +296,6 @@ namespace platf {
|
||||
lifetime::exit_sunshine(0, true);
|
||||
}
|
||||
|
||||
int set_env(const std::string &name, const std::string &value) {
|
||||
return setenv(name.c_str(), value.c_str(), 1);
|
||||
}
|
||||
|
||||
int unset_env(const std::string &name) {
|
||||
return unsetenv(name.c_str());
|
||||
}
|
||||
|
||||
bool request_process_group_exit(std::uintptr_t native_handle) {
|
||||
if (killpg((pid_t) native_handle, SIGTERM) == 0 || errno == ESRCH) {
|
||||
BOOST_LOG(debug) << "Successfully sent SIGTERM to process group: "sv << native_handle;
|
||||
|
||||
@@ -520,8 +520,7 @@ namespace platf::dxgi {
|
||||
height = desc.DesktopCoordinates.bottom - offset_y;
|
||||
|
||||
display_rotation = desc.Rotation;
|
||||
if (display_rotation == DXGI_MODE_ROTATION_ROTATE90 ||
|
||||
display_rotation == DXGI_MODE_ROTATION_ROTATE270) {
|
||||
if (display_rotation == DXGI_MODE_ROTATION_ROTATE90 || display_rotation == DXGI_MODE_ROTATION_ROTATE270) {
|
||||
width_before_rotation = height;
|
||||
height_before_rotation = width;
|
||||
} else {
|
||||
@@ -621,8 +620,7 @@ namespace platf::dxgi {
|
||||
HANDLE token;
|
||||
LUID val;
|
||||
|
||||
if (OpenProcessToken(GetCurrentProcess(), flags, &token) &&
|
||||
!!LookupPrivilegeValue(nullptr, SE_INC_BASE_PRIORITY_NAME, &val)) {
|
||||
if (OpenProcessToken(GetCurrentProcess(), flags, &token) && !!LookupPrivilegeValue(nullptr, SE_INC_BASE_PRIORITY_NAME, &val)) {
|
||||
tp.PrivilegeCount = 1;
|
||||
tp.Privileges[0].Luid = val;
|
||||
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
|
||||
|
||||
@@ -1324,14 +1324,6 @@ namespace platf {
|
||||
lifetime::exit_sunshine(0, true);
|
||||
}
|
||||
|
||||
int set_env(const std::string &name, const std::string &value) {
|
||||
return _putenv_s(name.c_str(), value.c_str());
|
||||
}
|
||||
|
||||
int unset_env(const std::string &name) {
|
||||
return _putenv_s(name.c_str(), "");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Stores state while enumerating top-level Windows windows.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user