From 15bb982412e3134a09eab28d8c27d9a60f5f9aef Mon Sep 17 00:00:00 2001 From: Felix Uhl Date: Wed, 25 Sep 2024 22:58:03 +0200 Subject: [PATCH] Fix screen flicker on Wayland Fixes #190 The GUIClipboardDaemon created a new Wayland window every ~2 seconds to interact with the clipboard. This steals focus from the currently active window for a very short time, causing context menus and overlays to close. It basically made Clipboard unusable on Wayland. The simplest strategy of fixing this is to align Linux with all other platforms by only reading data from the clipboard on-demand instead of doing it continuously. --- src/cb/src/clipboard.hpp | 1 - src/cb/src/externalclipboards.cpp | 67 ------------------------------- src/cb/src/main.cpp | 6 --- 3 files changed, 74 deletions(-) diff --git a/src/cb/src/clipboard.hpp b/src/cb/src/clipboard.hpp index 0a2fc0e90..e67fbc6c6 100644 --- a/src/cb/src/clipboard.hpp +++ b/src/cb/src/clipboard.hpp @@ -353,7 +353,6 @@ std::string formatBytes(const auto& bytes) { } void verifyClipboardName(); -void setupGUIClipboardDaemon(); void syncWithRemoteClipboard(bool force = false); void syncWithGUIClipboard(bool force = false); void fixMissingItems(); diff --git a/src/cb/src/externalclipboards.cpp b/src/cb/src/externalclipboards.cpp index 379e63a81..56d972d05 100644 --- a/src/cb/src/externalclipboards.cpp +++ b/src/cb/src/externalclipboards.cpp @@ -304,70 +304,3 @@ void updateExternalClipboards(bool force) { if (!envVarIsTrue("CLIPBOARD_NOREMOTE")) writeToRemoteClipboard(thisContent); } } - -void setupGUIClipboardDaemon() { - if (envVarIsTrue("CLIPBOARD_NOGUI")) return; - -#if defined(__linux__) || defined(__APPLE__) || defined(__unix__) - auto pid = fork(); - if (pid > 0) return; - if (pid < 0) { - perror("fork"); - exit(EXIT_FAILURE); - } - if (setsid() < 0) { - perror("setsid"); - exit(EXIT_FAILURE); - } - if (chdir("/") < 0) { - perror("chdir"); - exit(EXIT_FAILURE); - } - -#if defined(__linux__) - // check if there is already a cb daemon by checking /proc for a process which has an exe symlink entry that points to a binary called "cb" and which does not have stdin or stdout file descriptors - // that point to a tty - - for (const auto& entry : fs::directory_iterator("/proc")) { - try { - if (!fs::is_directory(entry)) continue; - auto exe = entry.path() / "exe"; - if (!fs::exists(exe)) continue; - auto exeTarget = fs::read_symlink(exe); - if (exeTarget.filename() != "cb") continue; - auto fd = entry.path() / "fd"; - - auto pointsToFilesystemObject = [](const fs::path& path) { - auto target = fs::read_symlink(path); - if (fs::is_directory(target) || fs::is_regular_file(target)) return true; - return false; - }; - - if (fs::exists(fd / "0") && !pointsToFilesystemObject(fd / "0")) continue; - if (fs::exists(fd / "1") && !pointsToFilesystemObject(fd / "1")) continue; - if (fs::exists(fd / "2") && !pointsToFilesystemObject(fd / "2")) continue; - - // found a cb daemon - _exit(EXIT_SUCCESS); - } catch (...) {} - } -#endif - - close(STDIN_FILENO); - close(STDOUT_FILENO); - close(STDERR_FILENO); - -#elif defined(_WIN32) | defined(_WIN64) - -#endif - - while (fs::exists(path)) { - path.getLock(); - syncWithGUIClipboard(true); - path.releaseLock(); - std::this_thread::sleep_for(std::chrono::milliseconds(2000)); - path = Clipboard(std::string(constants.default_clipboard_name)); - } - - exit(EXIT_SUCCESS); -} \ No newline at end of file diff --git a/src/cb/src/main.cpp b/src/cb/src/main.cpp index a8f95a337..424dd0b1b 100644 --- a/src/cb/src/main.cpp +++ b/src/cb/src/main.cpp @@ -45,14 +45,8 @@ int main(int argc, char* argv[]) { verifyAction(); -#if defined(__linux__) - setupGUIClipboardDaemon(); - syncWithRemoteClipboard(); - if (action != Action::Info) path.getLock(); -#else if (action != Action::Info) path.getLock(); syncWithExternalClipboards(); -#endif fixMissingItems();