diff --git a/src/browser/client.cxx b/src/browser/client.cxx index c2a5148..5328604 100644 --- a/src/browser/client.cxx +++ b/src/browser/client.cxx @@ -9,7 +9,9 @@ #include #include -Browser::Client::Client(CefRefPtr app, std::filesystem::path data_dir): show_devtools(true), data_dir(data_dir) { +Browser::Client::Client(CefRefPtr app,std::filesystem::path config_dir, std::filesystem::path data_dir): + show_devtools(true), config_dir(config_dir), data_dir(data_dir) +{ CefString mime_type_html = "text/html"; CefString mime_type_js = "application/javascript"; CefString mime_type_css = "text/css"; diff --git a/src/browser/client.hxx b/src/browser/client.hxx index 709c8b2..9676d50 100644 --- a/src/browser/client.hxx +++ b/src/browser/client.hxx @@ -20,7 +20,7 @@ namespace Browser { /// https://github.com/chromiumembedded/cef/blob/5735/include/cef_life_span_handler.h /// https://github.com/chromiumembedded/cef/blob/5735/include/cef_request_handler.h struct Client: public CefClient, CefBrowserProcessHandler, CefLifeSpanHandler, CefRequestHandler { - Client(CefRefPtr, std::filesystem::path); + Client(CefRefPtr, std::filesystem::path config_dir, std::filesystem::path data_dir); /* CefClient overrides */ CefRefPtr GetLifeSpanHandler() override; @@ -70,6 +70,7 @@ namespace Browser { CefRefCount ref_count; bool show_devtools; + std::filesystem::path config_dir; std::filesystem::path data_dir; std::map internal_pages; diff --git a/src/main.cxx b/src/main.cxx index bb97a45..11cc39b 100644 --- a/src/main.cxx +++ b/src/main.cxx @@ -36,7 +36,7 @@ int XIOErrorHandlerImpl(Display* display) { #endif int BoltRunBrowserProcess(CefMainArgs, CefRefPtr); -bool LockDataDirectory(std::filesystem::path&); +bool LockXdgDirectories(std::filesystem::path&, std::filesystem::path&); int BoltRunAnyProcess(CefMainArgs main_args) { // CefApp struct - this implements handlers used by multiple processes @@ -60,14 +60,15 @@ int BoltRunBrowserProcess(CefMainArgs main_args, CefRefPtr cef_app XSetIOErrorHandler(XIOErrorHandlerImpl); #endif - // find home directory + // find and lock the xdg base directories (or an approximation of them on Windows) + std::filesystem::path config_dir; std::filesystem::path data_dir; - if (!LockDataDirectory(data_dir)) { + if (!LockXdgDirectories(config_dir, data_dir)) { return 1; } // CefClient struct - central object for main thread, and implements lots of handlers for browser process - Browser::Client client_(cef_app, data_dir); + Browser::Client client_(cef_app, config_dir, data_dir); CefRefPtr client = &client_; // CEF settings - only set the ones we're interested in @@ -118,30 +119,49 @@ int main(int argc, char* argv[]) { return ret; } -bool LockDataDirectory(std::filesystem::path& path) { +bool LockXdgDirectories(std::filesystem::path& config_dir, std::filesystem::path& data_dir) { + const char* xdg_config_home = getenv("XDG_CONFIG_HOME"); const char* xdg_data_home = getenv("XDG_DATA_HOME"); const char* home = getenv("HOME"); + if (xdg_data_home && *xdg_data_home) { - path.assign(xdg_data_home); + data_dir.assign(xdg_data_home); } else if (home) { - path.assign(home); - path.append(".local"); - path.append("share"); + data_dir.assign(home); + data_dir.append(".local"); + data_dir.append("share"); } else { fmt::print("No $XDG_DATA_HOME or $HOME\n"); return false; } - path.append("bolt-launcher"); - std::error_code err; - std::filesystem::create_directories(path, err); - if (err.value() != 0) { - fmt::print("Could not create directories (error {}) {}\n", err.value(), path.c_str()); + + if (xdg_config_home && *xdg_config_home) { + config_dir.assign(xdg_config_home); + } else if (home) { + config_dir.assign(home); + config_dir.append(".config"); + } else { + fmt::print("No $XDG_CONFIG_HOME or $HOME\n"); return false; } - std::filesystem::path fpath = path; - fpath.append("lock"); - int lockfile = open(fpath.c_str(), O_WRONLY | O_CREAT | O_CLOEXEC, 0666); + config_dir.append("bolt-launcher"); + data_dir.append("bolt-launcher"); + std::error_code err; + std::filesystem::create_directories(config_dir, err); + if (err.value() != 0) { + fmt::print("Could not create config directories (error {}) {}\n", err.value(), data_dir.c_str()); + return false; + } + std::filesystem::create_directories(data_dir, err); + if (err.value() != 0) { + fmt::print("Could not create data directories (error {}) {}\n", err.value(), data_dir.c_str()); + return false; + } + + std::filesystem::path data_lock = data_dir; + data_lock.append("lock"); + int lockfile = open(data_lock.c_str(), O_WRONLY | O_CREAT | O_CLOEXEC, 0666); if (flock(lockfile, LOCK_EX | LOCK_NB)) { fmt::print("Failed to obtain lockfile; is the program already running?\n"); return false;