Bug 498575 - GlobalShortcuts BindShortcuts issues
Summary: GlobalShortcuts BindShortcuts issues
Status: REPORTED
Alias: None
Product: xdg-desktop-portal-kde
Classification: Plasma
Component: general (show other bugs)
Version: 6.2.5
Platform: Arch Linux Linux
: NOR normal
Target Milestone: ---
Assignee: Plasma Bugs List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2025-01-12 20:55 UTC by Kye
Modified: 2025-04-11 08:01 UTC (History)
2 users (show)

See Also:
Latest Commit:
Version Fixed In:
Sentry Crash Report:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kye 2025-01-12 20:55:01 UTC
SUMMARY

A few related issues calling org.freedesktop.portal.GlobalShortcuts.BindShortcuts:
1. Registering a new shortcut with a conflicting `preferred_trigger` will return that trigger in `trigger_description` as if it was bound and works for that new shortcut.
    - Also happens if the conflicting `preferred_trigger` was made in the same `BindShortcuts` call, first shortcut in the array will *actually* have the bind applied

2. Re-registering an existing shortcut that was originally registered without a preferred_trigger will turn any Custom shortcuts into Default shortcuts
    - System settings needs to be closed for this effect. Needs to be the _first_ BindShortcuts call of the Session (which brings me to my next point)

3. Does not conform to the spec:
    - > "An application can only attempt bind shortcuts of a session once." https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GlobalShortcuts.html#org-freedesktop-portal-globalshortcuts-bindshortcuts

4. The `description` of the `shortcuts` array has no upper limit meaning apps can create HUGE entries that crash systemsettings on trying to load the config for the entry
    - Users can still delete the entry just fine.


STEPS TO REPRODUCE
Create a valid GlobalShortcuts session and call BindShortcuts with:
for 1. ``` shortcuts = [( "shortcut1", {"description": "Demo Shortcut 1", "preferred_trigger": "CTRL+1"}),
            ( "shortcut2", {"description": "Demo Shortcut 2",  "preferred_trigger": "CTRL+1" }) ```
for 2.  ``` shortcuts = [( "shortcut1", {"description": "Demo Shortcut 1"}) ```
    2a. Add some custom shortcuts binds to the shortcut with the shortcuts KCM then close SystemSettings
    2b. close then create a new GlobalShortcuts session and call BindShortcuts with the above shortcuts array.
for 3. Do any of the above shortcuts arrays but continue calling BindShortcuts with it.
for 4. Do any of the above shortcuts arrays but fill the description with an absurd amount of characters like ``` insane_string = 'a' * 20 * 1024 * 1024 ```


OBSERVED RESULT
for 1. You will see both shortcuts have Ctrl+1 returned in their respective `trigger_description`s but only shortcut1 will have Activated and Deactivated Signals get triggered on a key press.
    - Until A `ShortcutsChanged` signal that effects the misreporting shortcut or A new dbus session is used, `ListShortcuts` calls and `ShortcutsChanged` signals will continue to return this wrong trigger_description for the misbehaving shortcut.

for 2. Viewing the shortcut's binds in the keybinds Settings KCM will show the Custom shortcuts you previously bound as Default shortcuts. They are only disableable and not removable.

for 3. After the first call they are not rejected.

for 4. Calling BindShortcuts with this shortcuts array will open systemsettings and immediately hang/crash. Later attempts to edit the shortcuts of the entry will crash. Can only fully delete the entry

EXPECTED RESULT
for 1. The conflicted shortcut will not have wrong info returned in `trigger_description` 
for 2. On shortcuts with no preferred_trigger user set custom shortcuts are not turned into default shortcuts on rebind
for 3. After the first call BindShortcuts attempts are rejected.
for 4. Concatenate what is displayed in the shortcuts KCM or reject bind attempts with huge description elements

SOFTWARE/OS VERSIONS
KDE Plasma Version: 6.2.5
KDE Frameworks Version: 6.10.0
Qt Version: 6.8.1
Graphics Platform: Wayland

ADDITIONAL INFORMATION
I used a custom python & gtk demo app with a .desktop for testing purposes. LMK if I should share it for testing
Comment 1 Aleix Pol 2025-01-13 19:48:55 UTC
If you have the test application created and don't mind sharing it, then yes it will surely be useful.
Comment 2 Kye 2025-01-15 00:08:13 UTC
(In reply to Aleix Pol from comment #1)
> If you have the test application created and don't mind sharing it, then yes
> it will surely be useful.

Here you go :p https://gist.github.com/Covkie/b647a5f33ede7ee66bb446caa2f1d5fd
Play with the shortcuts array under bind_shortcuts()
Comment 3 David Redondo 2025-04-11 08:01:18 UTC
Git commit 2fd2d093388f8ec2fc4bcb3b9c484b2ea3075ea7 by David Redondo.
Committed on 11/04/2025 at 07:02.
Pushed by davidre into branch 'master'.

Improve the global shortcuts workflow

Instead of always opening system settings on bind a dialog
is shown with new shortcuts that lets the user customize
them. If the application just wants to rebind previously
bound global shortcuts no dialog is shown.

A  +133  -0    src/GlobalShortcutsDialog.qml     [License: LGPL(3+eV) LGPL(v3.0) LGPL(v2.1)]
M  +208  -18   src/globalshortcuts.cpp
M  +17   -8    src/globalshortcuts.h
M  +1    -0    src/resources.qrc
M  +5    -4    src/session.cpp
M  +6    -1    src/session.h

https://invent.kde.org/plasma/xdg-desktop-portal-kde/-/commit/2fd2d093388f8ec2fc4bcb3b9c484b2ea3075ea7