17
submitted 10 months ago* (last edited 10 months ago) by Andy@programming.dev to c/kde@lemmy.kde.social

Hello!

I'm still using X11, and one of the things that's keeping me there is that I make heavy use of a launch-or-focus script, so that I hit a certain hotkey and no matter what a browser/chat/editor/terminal/file-manager/etc. shows up focused on my current desktop.

In the world of Wayland, this isn't so easy. If this can be recreated at all, I think it'll have to be made to rely on some sort of interface to Kwin.

I don't think it's possible now, but might it be in the future?

Here's my script, let's see if the lemmy interface mangles it (EDIT: yes, the last character should be an ampersand, not &):

#!/bin/zsh -ex

# -- Usage --
# ./toggle_window.zsh LAUNCH_CMD [ WM_CLASS [ CHECK_CMD ] ]

# -- Defaults --
# WM_CLASS and CHECK_CMD each default to the value of LAUNCH_CMD

# -- Examples --
# ./toggle_window.zsh dolphin
# ./toggle_window.zsh wezterm-gui org.wezfurlong.wezterm
# ./toggle_window.zsh firefox firefox firefox-bin
# ./toggle_window.zsh \
#   'flatpak run --branch=stable --arch=x86_64 --command=telegram-desktop --file-forwarding org.telegram.desktop' \
#   telegram-desktop telegram-deskto
# Yes, "telegram-deskto" without a final p. Hmm.

# -- Dependencies --
# - procps (pgrep)
# - wmctrl
# - x11-utils (xprop)
# - xdotool

# -- TODO --
# - wayland

launch_cmd=(${(z)1})
wm_class=${2:-$1}
check_cmd=${3:-$1}

if [[ $(xprop -id $(xdotool getactivewindow) WM_CLASS) =~ \"$wm_class\" ]] {
  xdotool getactivewindow windowminimize
} else {
  wmctrl -xR $wm_class || true
}

pgrep -u $USER -x $check_cmd || exec $launch_cmd &
top 6 comments
sorted by: hot top controversial new old
[-] tubbadu@lemmy.kde.social 7 points 10 months ago* (last edited 10 months ago)

There is a way! I wrote this tool exactly for this purpose, and works on both x11/wayland: https://codeberg.org/tubbadu/ktoggle

it writes a kwin script on a temporary file, installs it, runs it and uninstalls it. All using DBUS communication.
it works quite well (there is just rarely an unexpected behavior I didn't find the time to fix yet), I use it daily for lots of apps like telegram, whatsapp, qalculate and a note taking app

once compiled and installed you can just assign to a hotkey the command

ktoggle --class org.telegram.desktop --name telegram-desktop --program telegram-desktop

and you're done! read the readme to see an explanation of all available flags

(note: this is a work in progress, I have little time to maintain it, so don't expect it to be perfect!)

hope this helps!

[-] Andy@programming.dev 2 points 10 months ago

Oooh wee, great, thanks!

[-] Andy@programming.dev 2 points 10 months ago

What's the idea behind installing, uninstalling, reinstalling, uninstalling... all the time?

[-] tubbadu@lemmy.kde.social 4 points 10 months ago

It was the best way I found to do it, because I couldn't find a proper way to run a kwin script with an argument (the program name and class), so I decided to just modify the script and then install and run it, and uninstall at the end. "Install" only takes a blink, although it may be made faster, for example implementing a sort of "caching" of the last used script in order to avoid removing and reinstalling the same script, perhaps one day I'll try to implement it. My first attempt at it was done using KWindowSystem api, but something didn't work so I had to give up and just install the scripts. perhaps I can try again with PlasmaWindowManagement, I may be luckier

[-] doomsdayrs@mastodon.ml 2 points 10 months ago

@Andy I recall that global hotkeys was added to KDE?

[-] Andy@programming.dev 3 points 10 months ago

Usually "global hotkeys" just means using a hotkey from any app. Is that what you mean?

To be clear, the trouble I'm hoping to overcome is the window manipulation, without using wmctrl, x11-utils, or xdotool.

this post was submitted on 30 Oct 2023
17 points (100.0% liked)

KDE

4997 readers
40 users here now

KDE is an international technology team creating user-friendly free and open source software for desktop and portable computing. KDE’s software runs on GNU/Linux, BSD and other operating systems, including Windows.

Plasma 6 Bugs

If you encounter a bug, proceed to https://bugs.kde.org, check whether it has been reported.

If it hasn't, report it yourself.

PLEASE THINK CAREFULLY BEFORE POSTING HERE.

Developers do not look for reports on social media, so they will not see it and all it does is clutter up the feed.

founded 1 year ago
MODERATORS