Skip to content

Commit

Permalink
Fix screen flicker on Wayland
Browse files Browse the repository at this point in the history
Fixes Slackadays#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.
  • Loading branch information
iFreilicht committed Sep 25, 2024
1 parent c9012ca commit 15bb982
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 74 deletions.
1 change: 0 additions & 1 deletion src/cb/src/clipboard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
67 changes: 0 additions & 67 deletions src/cb/src/externalclipboards.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
6 changes: 0 additions & 6 deletions src/cb/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 15bb982

Please sign in to comment.