Bug 463199

Summary: "After taking screenshot: Copy to clipboard" does not work anymore on Wayland
Product: [Frameworks and Libraries] frameworks-kguiaddons Reporter: Nate Graham <nate>
Component: generalAssignee: Noah Davis <noahadvs>
Status: RESOLVED FIXED    
Severity: normal CC: amanita+KDEBUGS, andrej.halv, dmatteo002, gabrielgarocs, garageman, kde, kde, kde, o.rootedavid.o, tusooa
Priority: HI    
Version: 5.102.0   
Target Milestone: ---   
Platform: Other   
OS: Linux   
Latest Commit: Version Fixed In: 5.103
Attachments: logs for running spectacle -rcb
Wayland debug logs for running spectacle -ircb

Description Nate Graham 2022-12-18 17:30:50 UTC
Everything KDE from git master on top of Fedora 37.

STEPS TO REPRODUCE
0. Use the Plasma Wayland session
1. Configure Spectacle to use "After taking screenshot: Copy to clipboard" setting
2. Take a screenshot


OBSERVED RESULT
The screenshot is not copied to the clipboard automatically.


EXPECTED RESULT
The screenshot is copied to the clipboard automatically.


ADDITIONAL INFORMATION
1. This works as expected on X11
2. On Wayland, clicking the "Copy" button in the toolbar does work to copy the screenshot to the clipboard manually
3. This used to work on Wayland a week or two ago, but I bisected both Spectacle and KWin back to December 1 and it was still broken there, so I guess something somewhere else regressed to make this happen?
Comment 1 hexchain 2023-01-15 08:51:30 UTC
Same issue on Arch Linux. The most suspicious recent changes are a bunch of KF5 updates (5.101 -> 5.102) on 01-14, but I'm not sure if that is the reason.
Comment 2 hexchain 2023-01-15 13:56:02 UTC
The culprit seems to be https://invent.kde.org/frameworks/kguiaddons/-/commit/3984732e007e71632a525d89227fdfe94fa037ad.

With KGuiAddons v5.101.0 the issue disappears, and git bisect found this commit.
Comment 3 Noah Davis 2023-01-15 19:32:12 UTC
(In reply to hexchain from comment #2)
> The culprit seems to be
> https://invent.kde.org/frameworks/kguiaddons/-/commit/
> 3984732e007e71632a525d89227fdfe94fa037ad.
> 
> With KGuiAddons v5.101.0 the issue disappears, and git bisect found this
> commit.

Interesting. Perhaps we should we say that this is actually a KSystemClipboard bug?
Comment 4 David Redondo 2023-01-17 08:00:16 UTC
Seems for me the delay settings play a role
- no delay no copy is happening
- 1s or more copy works
Comment 5 David Redondo 2023-01-17 09:09:36 UTC
I've updated my system and can't reproduce anymore.
Comment 6 David Redondo 2023-01-17 09:13:29 UTC
If you can
- could you give more detailed steps, maybe there is a difference in my testing?
- provide me the output of `WAYLAND_DEBUG=1 spectacle` of taking jsut one screenshot with that setting.
Comment 7 Gabriel Garcia 2023-01-17 12:43:58 UTC
(In reply to David Redondo from comment #6)
> - could you give more detailed steps, maybe there is a difference in my
> testing?
Launch spectacle -rbc either through the console or through a shortcut I've created through system settings. Select a rectangular region, I get a notification saying it's copied to the clipboard, it's not copied to the clipboard.

- provide me the output of `WAYLAND_DEBUG=1 spectacle` of taking jsut one screenshot with that setting.
Attached is the output of `WAYLAND_DEBUG=1 spectacle -rbc`

Launching Spectacle gui, taking a screenshot, and manually clicking the copy to clipboard button works. 
Launching Spectacle with the background option + copy to clipboard + any other option except the rectangular one works.
Launching Spectacle with the background option + copy to clipboard + rectangular selection, doesn't work.
Comment 8 Gabriel Garcia 2023-01-17 12:45:09 UTC
Created attachment 155375 [details]
logs for running spectacle -rcb
Comment 9 Nate Graham 2023-01-17 16:10:33 UTC
Can confirm that it is now working for me with current git master as well.
Comment 10 hexchain 2023-01-18 03:35:01 UTC
Created attachment 155396 [details]
Wayland debug logs for running spectacle -ircb

My steps are the same as in #c7 (except I added an additional -i flag). Here is my Wayland debug logs.
Comment 11 David Redondo 2023-01-19 10:31:13 UTC
Even with these commands copies happen for me. From your wayland logs it looks to me like you wouldn't have https://invent.kde.org/frameworks/kguiaddons/-/commit/0fbb0aa524adbd077b2ba7716218d4c6f042bed0 but it should be included in 5.102:
 
Window is hidden (surface destroyed) but data_device is used

[3695470.952] wl_pointer@26.frame()
[3695471.113]  -> xdg_toplevel@41.destroy()
[3695471.116]  -> xdg_surface@40.destroy()
[3695471.122]  -> wl_display@1.sync(new id wl_callback@53)
[3695471.124]  -> wl_surface@37.destroy()
[3695471.614]  -> org_kde_plasma_surface@34.destroy()
[3695472.136]  -> wl_shm_pool@51.destroy()
[3695472.141]  -> wl_buffer@52.destroy()
[3695472.696]  -> wl_shm_pool@42.destroy()
[3695472.700]  -> wl_buffer@43.destroy()
[3695472.732]  -> wl_data_device_manager@9.create_data_source(new id wl_data_source@54)
[3695474.937]  -> wl_data_source@54.offer("application/x-qt-image")
[3695474.941]  -> wl_data_source@54.offer("x-kde-force-image-copy")
[3695474.942]  -> wl_data_source@54.offer("image/png")
[...]
[3695474.956]  -> wl_data_device@10.set_selection(wl_data_source@54, 26798)

[1946768.478] wl_pointer@20.frame()
[1946770.191]  -> xdg_toplevel@35.destroy()
[1946770.208]  -> xdg_surface@34.destroy()
[1946770.227]  -> wl_display@1.sync(new id wl_callback@50)
[1946770.240]  -> wl_surface@31.destroy()
[1946770.673]  -> org_kde_plasma_surface@28.destroy()
[1946771.791]  -> wl_shm_pool@46.destroy()
[1946771.812]  -> wl_buffer@47.destroy()
[1946772.872]  -> wl_shm_pool@41.destroy()
[1946772.886]  -> wl_buffer@42.destroy()
[1946774.022]  -> wl_shm_pool@36.destroy()
[1946774.042]  -> wl_buffer@37.destroy()
[1946775.128]  -> wl_shm_pool@44.destroy()
[1946775.144]  -> wl_buffer@45.destroy()
[1946775.967]  -> wl_data_device_manager@9.create_data_source(new id wl_data_source@51)
[1946776.192]  -> wl_data_source@51.offer("application/x-qt-image")
[1946776.223]  -> wl_data_source@51.offer("x-kde-force-image-copy")
[1946776.232]  -> wl_data_source@51.offer("image/png")
[...]
[1946776.407]  -> wl_data_device@10.set_selection(wl_data_source@51, 27682)

While on my machine happens what I would expect, no window so data_control is used:
[1118803.392] wl_keyboard@3.key(2533, 3267142, 28, 1)
[1118804.192]  -> xdg_toplevel@40.destroy()
[1118804.213]  -> xdg_surface@39.destroy()
[1118804.256]  -> wl_display@1.sync(new id wl_callback@77)
[1118804.272]  -> wl_surface@35.destroy()
[1118804.324]  -> org_kde_kwin_appmenu@69.release()
[1118806.243]  -> zwlr_data_control_manager_v1@24.create_data_source(new id zwlr_data_control_source_v1@81)
[1118806.261]  -> zwlr_data_control_source_v1@81.offer("application/x-qt-image")
[1118806.269]  -> zwlr_data_control_source_v1@81.offer("x-kde-force-image-copy")
[1118806.413]  -> zwlr_data_control_source_v1@81.offer("image/png")
[...]
[1118806.586]  -> zwlr_data_control_device_v1@27.set_selection(zwlr_data_control_source_v1@81)

I am bit of a loss at the moment why it would happen
Comment 12 hexchain 2023-01-19 12:31:09 UTC
I honestly have no idea. It still happens even after removing ~/.config/spectaclerc.

Are you running everything from git master? Could it be something other than KGuiAddons?
Comment 13 Nate Graham 2023-01-23 23:02:34 UTC
*** Bug 464478 has been marked as a duplicate of this bug. ***
Comment 14 Nate Graham 2023-01-24 16:53:15 UTC
*** Bug 464506 has been marked as a duplicate of this bug. ***
Comment 15 hexchain 2023-01-25 14:25:43 UTC
Tried to do some debugging on this issue with some added output to KGuiAddons: https://gist.github.com/hexchain/06e71deb8b29f661fee646fe0096a499

Spectacle prints the following output after a screenshot is taken (and nothing is copied to the clipboard):
kb enter
using normal mechanism fw: QWindow(0x0) , fw->isVisible: false , keyboard has focus: true
kb leave

If I remove the whole "if" block of the "normal mechanism", Spectacle is then able to copy the image to the clipboard. With WAYLAND_DEBUG the output also matches what you expected in #c11.
Comment 16 dmatteo002 2023-01-26 10:35:41 UTC
Having same problem on Arch (Kernel 6.1.8, Plasma version 5.26.5, Kde Frameworks 5.102.0) and downgrading to kguiaddons 5.101 seem to be working partially (meaning that it now copy to clipboard but using spectacle -brc it just never stop the command until i use ^C)
Comment 17 David Redondo 2023-01-27 12:34:30 UTC
I reproduced now with the release version of spectacle compared to the rewritten version.
Comment 18 Bug Janitor Service 2023-01-27 12:55:25 UTC
A possibly relevant merge request was started @ https://invent.kde.org/frameworks/kguiaddons/-/merge_requests/90
Comment 19 David Edmundson 2023-01-27 22:40:09 UTC
Git commit dcb254dc6b4e5b76e04a5ee8b4db8177aff10d29 by David Edmundson, on behalf of David Redondo.
Committed on 27/01/2023 at 22:26.
Pushed by davidedmundson into branch 'master'.

waylandclipboard: roundtrip to get accurate focus state

The current approach is too complicated and not correct anyway.
To summarize QGuiApplication::focusWindow is delayed so the focus
watcher was introduced, however when the window is hidden and
setMimeData called before Wayland events have been processed,
then focusWatcher is naturally out of sync, so the check
was complicated to work around this state. However the same
issue arises when the window is deleted before calling
setMimeData.
Instead of introducing more complicated checks and workaround
roundtrip so the focusWatcher state is up to date before setting
the clipboard.
FIXED-IN: 5.103

M  +8    -3    src/systemclipboard/waylandclipboard.cpp

https://invent.kde.org/frameworks/kguiaddons/commit/dcb254dc6b4e5b76e04a5ee8b4db8177aff10d29
Comment 20 Fushan Wen 2023-01-29 05:14:26 UTC
Git commit e195913d37f8ff79a14351c029952830394e7057 by Fushan Wen, on behalf of David Redondo.
Committed on 29/01/2023 at 05:14.
Pushed by fusionfuture into branch 'cherry-pick-dcb254dc'.

waylandclipboard: roundtrip to get accurate focus state

The current approach is too complicated and not correct anyway.
To summarize QGuiApplication::focusWindow is delayed so the focus
watcher was introduced, however when the window is hidden and
setMimeData called before Wayland events have been processed,
then focusWatcher is naturally out of sync, so the check
was complicated to work around this state. However the same
issue arises when the window is deleted before calling
setMimeData.
Instead of introducing more complicated checks and workaround
roundtrip so the focusWatcher state is up to date before setting
the clipboard.
FIXED-IN: 5.103


(cherry picked from commit dcb254dc6b4e5b76e04a5ee8b4db8177aff10d29)

M  +8    -3    src/systemclipboard/waylandclipboard.cpp

https://invent.kde.org/frameworks/kguiaddons/commit/e195913d37f8ff79a14351c029952830394e7057
Comment 21 Patrick Silva 2023-01-29 20:10:36 UTC
*** Bug 464997 has been marked as a duplicate of this bug. ***
Comment 22 d_avery 2023-02-16 00:58:55 UTC
After updating to kguiaddons 5.103, issue is still present.

KDE Plasma 5.26.90 Wayland session, Arch Linux kernel 6.1.11-arch1-1.
Comment 23 d_avery 2023-02-17 15:37:47 UTC
My mistake. I forgot I had put kguidaddons in IgnorePkg and it did not actually update when I ran a full system upgrade. 5.103 fixes the issue.