Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[𝗛𝗲𝗹𝗽 𝗪𝗮𝗻𝘁𝗲𝗱] Clipboard steals focus every two seconds on Wayland #171

Open
2 of 6 tasks
DavidKoot opened this issue Oct 12, 2023 · 23 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@DavidKoot
Copy link

Describe the bug
Once I have used Clipboard it will keep stealing for a split-second every two seconds or so. Very annoying when using context menus or other, that will just disappear if anything else gets focus.

To Reproduce
I might have a bit specific setup. I'm on Ubuntu (Gnome) 22.04, and I use ddterm as my terminal app, which is a Gnome specific extension that displays a dropdown, screen-wide terminal. I noticed that using this with Clipboard, results in the Clipboard application opening in the background whenever I issue a cb command. I then would have to alt+tab to the Clipboard application which then disappears and with that completes the command. I therefore installed the Gnome extension 'NoAnnoyance' which focuses directly any new spawn application, so I don't have to alt+tab to the Clipboard application. Not sure if that all contributes to the bug, I'll try to turn that off the next time before I use cb. Once this Clipboard behaviour started, it doesn't help to turn off 'NoAnnoyance'.

Expected behavior
Clearly, I don't want something to interfere with my input through mouse and/or keyboard typing, every two seconds.

Screenshots
Can't make any screenshots

Version
Clipboard 0.82, from Flathub, on Ubuntu 22.04, possibly influenced by the extension 'NoAnnoyance' thatThe version of CB you're using, how you got it, and what system you're using. Didn't have this problem with 0.81, but I have it quite consistently on other computers as well where I use Ubuntu/gnome extenstions as well.

Questionnaire
Check these boxes to see if your issue is valid or not.

  • If I compiled CB, then any errors that may be present don't mention "LTO error" (this is sadly a bug with the compiler, not Clipboard)
  • If I compiled CB, then any errors that may be present don't mention "XYZ has not been defined in namespace std" (this sadly means you need a compiler with C++20 support, so upgrade first)
  • I check all these boxes without reading them
  • I think bachata is the best music
  • I'm using the latest commit/release (we only support the latest)
  • If I downloaded CB from GitHub Actions or with the install script, any errors that may be present don't mention "GLIBC XYZ not found" (your system is sadly too outdated for CB, so upgrade it)

Additional context
Add any other context about the problem here.

Before you post
Please make sure you check previous bug reports before filing a new one.
This will help keep the Issues section less cluttered. :)
Feel free to delete this section once you've completed this form.

@DavidKoot DavidKoot added the bug Something isn't working label Oct 12, 2023
@Slackadays
Copy link
Owner

This is because 0.8.2 added asynchronous clipboard syncing, where CB will refresh every 2 seconds in the background to grab any changed desktop clipboard contents. Unfortunately, because you're using Ubuntu 22.04 on GNOME, this means you have Wayland. And because Wayland has this horrible new security feature where a window needs focus to get the clipboard, CB and other clipboard managers have to add a horrible hack by opening a temporary 1x1 window just to get focus. There is a special feature that some desktop systems support that gets rid of this issue, but it would require special handling code in CB to use.

Therefore, you can try the following to work around this issue:

  • switch to X11
  • disable desktop clipboard syncing with export CLIPBOARD_NOGUI=1
  • wait for someone to add the special handling code to CB

@DavidKoot
Copy link
Author

That sounds pretty horrible indeed. Thanks for explaining, it also explains some of the other things I've seen. Need to think about what to do now. I have played around with CLIPBOARD_NOGUI, but I just like to use cb together with ctrl-v pasting. Don't like it much to go back to X11 either. I guess this closes the issue then.

@Slackadays
Copy link
Owner

Slackadays commented Oct 12, 2023

I can see if anyone would like to make a PR to add the special Wayland code before closing this, because this is a recurring issue on Wayland

@Slackadays Slackadays added the help wanted Extra attention is needed label Oct 12, 2023
@Slackadays Slackadays pinned this issue Oct 12, 2023
@Slackadays Slackadays changed the title Clipboard v0.82 (flathub on Ubuntu) steals focus every two seconds Clipboard v0.82 (flathub on Ubuntu) steals focus every two seconds on Wayland Oct 12, 2023
@Slackadays Slackadays unpinned this issue Oct 12, 2023
@Slackadays Slackadays pinned this issue Oct 12, 2023
@PlexSheep
Copy link

can cb maybe use wl-copy and wl-paste to sync the clipboards? I don't know how it's done in NeoVim, but I can copy from NeoVim to my system without opening a window. This is reduces the functionality of the app a lot for me.

@Slackadays
Copy link
Owner

can cb maybe use wl-copy and wl-paste to sync the clipboards?

Actually, this may be easily possible in a future version by adding a custom script hook to each clipboard. However, I haven't done anything to add this feature yet, so stay tuned.

@MaddyGuthridge
Copy link
Contributor

Unfortunately, despite much experimentation, I haven't found a workaround to this on Gnome, and so have had to keep the GUI integration disabled, lest my typing be significantly disrupted by the constant focus stealing. Is there a way to monitor the clipboard in the background without stealing focus from other applications, eg putting it in the system tray instead?

@Slackadays
Copy link
Owner

Slackadays commented Jul 31, 2024

@MaddyGuthridge There is unfortunately no concrete fix, but the following will at least get rid of the symptom:

  • turn off Wayland with NO_WAYLAND=1
  • switch to a different window manager

There is a way to keep Wayland support without interfering with the focus, but it involves supporting a new non-standard protocol and I refuse to do that because Wayland feels like a huge waste of effort to me. However, I'd be all ears for a PR if anybody wants to take on the challenge!

@MaddyGuthridge
Copy link
Contributor

The NO_WAYLAND=1 thing doesn't seem to have worked for me (installed via Flatpak, configured using Flatseal).

image

Could it be a better user experience to disable GUI integration on Gnome Wayland (and other affected desktops/WMs) by default, given the degree to which it breaks the user experience?

Out of curiosity, which desktops/window managers aren't affected by the issue?

Either way, maybe I'll look into improving the Wayland support if I have time, since this tool is so excellent on every other system I use. No promises though, since C++ and I have a pretty questionable relationship :p

@Slackadays
Copy link
Owner

I think NO_WAYLAND=1 is actually what you'd use when building Clipboard to disable the Wayland library requirement, so it doesn't apply during runtime. Now that I look at it, CLIPBOARD_NO_GUI=1 seems to be the only way to disable it at runtime, but I can see if maybe a "one shot" option would work so there isn't any daemon constantly checking for new data. When I was testing this feature I was in fact using Gnome Wayland but maybe they changed something recently?

@MaddyGuthridge
Copy link
Contributor

Perhaps having it only check for new data when performing a paste operation could improve things? Still less than ideal, but unless people have scripts running in the background, it at least wouldn't interrupt typing.

Additionally, having a command to kill the daemon once it is started could be helpful (currently I need to kill it via my system monitor).

@bharathanr
Copy link

I'm really interested in this project, and I do hope to see this issue get fixed eventually. I'm also pretty excited of the option to sync through wl-clipboard.

If I find some time to learn more about Wayland, I'll gladly try my hand at raising a PR and providing a fix.

@VectorKappa
Copy link

VectorKappa commented Oct 3, 2024

adding a custom script hook to each clipboard.

wl-paste already provides --watch

@Slackadays
Copy link
Owner

Slackadays commented Oct 6, 2024

Sorry for the super long wait, but next on my todo list is the script hook feature! Here's a preview of what it might look like:

  1. Set up the hook with cb script /path/to/script
  2. CB will run this script before and after any action
  3. In the script, customize its behavior with the CLIPBOARD_SCRIPT_TIMING and/or CLIPBOARD_ACTION environment variables according to what exactly you want to do

@Slackadays
Copy link
Owner

Slackadays commented Oct 16, 2024

@MaddyGuthridge do you mind testing out the latest commit? The new script action is almost ready and it should work with the instructions in my previous comment (try doing something like cb sc "pwd" first)

@MaddyGuthridge
Copy link
Contributor

@Slackadays I'd love to give it a go! I'll have some time after work (later today). Excited to be able to have Clipboard properly integrated with my desktop!

@nhosama
Copy link

nhosama commented Oct 21, 2024

Hi @Slackadays! I am currently trying Version 0.9.1 (commit 9974069) using clipboard-git in Hyprland on EndeavourOS.

I was trying the script function using cb sc "pwd" command. And the output is

⬤ Setting up... %?, 60s elapsed ████████████████████████████████▒▒▒▒▒▒▒     ✘  Internal error: Invalid argument
┃ This might be a bug, or you might be lacking permissions on this system.

The issue is, as I am using Fish as my shell, clipboard can not assign CLIPBOARD_ACTION or other environment variables. So the command is stuck. Also if I set the variable manually (like using set CLIPBOARD_ACTION pwd), then cb sc works as it should. So please add support for setting environment variables in Fish shell. BTW, the flickering is still here (not if CLIPBOARD_NOGUI is set to 1).

And many thanks for such an awesome utility!

(Also I'm not sure if I should create a new issue for this. Please let me know if I should.)

@Slackadays
Copy link
Owner

@nhosama Not getting this in Fish:
image

I also can't find anything about this issue anywhere else.

Did you customize yours in some way that may be causing this problem?

@nhosama
Copy link

nhosama commented Oct 22, 2024

@Slackadays You're right. I tried the command on Fish with no config and it works just fine. I'll check where in my config the problem is. Thanks for checking it!

EDIT: I couldn't find the problem after restarting once, not even on my config. Now it works fine every time. I don't know what was even happening.

@MaddyGuthridge
Copy link
Contributor

MaddyGuthridge commented Oct 23, 2024

@Slackadays sorry for the wait! I've cloned and compiled it now, and it seemed to work perfectly on my system (Fedora 40, Gnome, Wayland). One point of confusion is that scripts seem to run before and after every single cb command. I wonder if it could be nicer to register scripts for specific events. Perhaps with an input like:

$ cb script --before=paste --after=cp,cut /path/to/script
...
$ cb cp "Example text"
# Text is copied
# Script is run
$ cb p
# Script is run
# Text is written to stdout

That being said, I couldn't find much documentation on how scripts are planned to work, so perhaps you already have something similar in mind, and I just didn't realise. I imagine you'll document this far more before it is released.

Here's the output of my testing. Seems pretty solid for the simple use cases I tried, and I especially like that the status of the program is reported if it exits with a failure code.

# Me cloning your project to ~/Source/other-peoples-problems is a personal joke, not an insult, I swear!
| maddy@yzma :: ~/Source/other-peoples-problems/Clipboard/build (main)
⋙   git rev-parse --short HEAD
d89fcbb
| maddy@yzma :: ~/Source/other-peoples-problems/Clipboard/build (main)
⋙   ./cb script pwd
 ✔  Saved script "pwd"
/run/user/1000/Clipboard/0/data/3
| maddy@yzma :: ~/Source/other-peoples-problems/Clipboard/build (main)
⋙   ./cb cp "Hello world"
/run/user/1000/Clipboard/0/data/4
 ✔  Copied text "Hello world"
/run/user/1000/Clipboard/0/data/4
| maddy@yzma :: ~/Source/other-peoples-problems/Clipboard/build (main)
⋙   ./cb script ls
/run/user/1000/Clipboard/0/data/4
 ✔  Saved script "ls"
rawdata.clipboard
| maddy@yzma :: ~/Source/other-peoples-problems/Clipboard/build (main)
⋙   echo -e "#\!/usr/bin/env bash \ncat rawdata.clipboard" > example.sh
| maddy@yzma :: ~/Source/other-peoples-problems/Clipboard/build (main)
⋙   chmod +x example.sh
| maddy@yzma :: ~/Source/other-peoples-problems/Clipboard/build (main)
⋙   ./cb script /home/maddy/Source/other-peoples-problems/Clipboard/build/example.sh
rawdata.clipboard
 ✔  Saved script "#!/usr/bin/env bash
cat rawdata.clipboard
"
Hello world%                                                                          
# !!! No trailing newline, but that's to-be-expected given my input didn't have a new-line.
| maddy@yzma :: ~/Source/other-peoples-problems/Clipboard/build (main)
⋙   ./cb cp "This is some example tests to check whether my fancy new script works correctly"
cat: rawdata.clipboard: No such file or directory
 ✘  Failed to run the clipboard script (returned exit code 1)
 ✔  Copied text "This is some example tests to check whether my fancy new script works correctly"
This is some example tests to check whether my fancy new script works correctly%      
| maddy@yzma :: ~/Source/other-peoples-problems/Clipboard/build (main)
⋙

Note: as can be seen in the final command I ran, the behaviour of running the script before and after every operation seems less-than ideal, since it means that scripts must be made much more complex, as they must determine whether they should be run themselves, rather than relying on cb to only call them when needed.

Given I'm not a C++ expert, your code was refreshingly simple to compile -- I just had to dnf install cmake g++ alsa-lib-devel beforehand. It did take a little hunting for the specific dependencies I needed, so perhaps a documentation improvement for the future could be to list the specific dependency names required for various operating systems.

@Slackadays
Copy link
Owner

Slackadays commented Oct 23, 2024

You'll need to add some way to check the action and timing from within the script itself. For example:

if [ "$CLIPBOARD_ACTION" == "copy" ]; then exit 0; fi
if [ "$CLIPBOARD_SCRIPT_TIMING" == "before" ]; then exit 0; fi

CLIPBOARD_SCRIPT_TIMING can be before or after and CLIPBOARD_ACTION just matches the full length name of whatever action you're doing.

@MaddyGuthridge
Copy link
Contributor

Great to see there's a system for it, but I still think that scripts would be nicer to write if it was easier to bind them to specific events, just from a design perspective. I suppose that does add complexity to the process of registering scripts though, so the better solution depends on whether it is intended for average users to be writing their own scripts (simpler scripting interface is better) or using pre-written ones (simpler cb script arguments is better).

@Slackadays Slackadays changed the title Clipboard v0.82 (flathub on Ubuntu) steals focus every two seconds on Wayland [𝗛𝗲𝗹𝗽 𝗪𝗮𝗻𝘁𝗲𝗱] Clipboard v0.82 (flathub on Ubuntu) steals focus every two seconds on Wayland Oct 23, 2024
@Slackadays Slackadays changed the title [𝗛𝗲𝗹𝗽 𝗪𝗮𝗻𝘁𝗲𝗱] Clipboard v0.82 (flathub on Ubuntu) steals focus every two seconds on Wayland [𝗛𝗲𝗹𝗽 𝗪𝗮𝗻𝘁𝗲𝗱] Clipboard steals focus every two seconds on Wayland Oct 23, 2024
@Slackadays
Copy link
Owner

Just putting it out there again that if anybody knows C++ and can implement the wlr-data-control API, that would be incredible for everybody!

@Slackadays
Copy link
Owner

@MaddyGuthridge The latest commit has the ability to configure the script actions and timing based on the --actions and --timings flags, so --actions search,history,in would only run it for those actions, and --timings before would only run it before the action

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

7 participants