You are not logged in.

#1 2023-04-26 18:38:32

grandrew
Member
Registered: 2022-12-20
Posts: 6

[SOLVED] Save and restore the position of the windows in KDE - 2023

The issue is persistent through many years, so here is a script that works in 2023:

installation:

1. install wmctrl and xwininfo
2. save scripts below in your home directory (/home/USERNAME)
3. mark restore.sh as executable
4. drag-and-drop .desktop files to your panel
5. use

script command-line usage just in case:

./restore.sh save
./restore.sh restore

The script - save as restore.sh in your home directory and mark as "executable" in file preferences:

#!/bin/bash
MODE=$1
if [ -e $MODE ]; then
echo "$0 save - to save"
echo "$0 restore - to restore"
exit
fi
if [ $MODE = "save" ]; then
        echo "saving"
        xwininfo -tree -root > /tmp/savewin.log
fi
if [ $MODE = "restore" ]; then
        echo "restoring"
        while read p; do
          LN=`echo $p | xargs`
          WINID=$(echo $LN | cut -d' ' -f1)
          XYWH=$(echo $LN | rev | cut -d' ' -f2 | rev)
          W=$(echo $LN | rev | cut -d' ' -f2 | rev | cut -d'x' -f1)
          H=$(echo $LN | rev | cut -d' ' -f2 | rev | cut -d'x' -f2 | cut -d'+' -f1)
          X=$(echo $LN | rev | cut -d' ' -f1 | rev | cut -d'x' -f2 | cut -d'+' -f2)
          Y=$(echo $LN | rev | cut -d' ' -f1 | rev | cut -d'x' -f2 | cut -d'+' -f3)
          if [ $H -lt 300 ]; then
                  continue;
          fi
          wmctrl -i -r $((${WINID})) -e 1,$(($X-2)),$(($Y-25)),$W,$H
          echo wmctrl -i -r $((${WINID})) -e 1,$(($X-2)),$(($Y-25)),$W,$H
        done <<< $(cat /tmp/savewin.log | grep " +" | grep "0x" | grep -v " +0" | grep -v "+-")
fi

the .desktop files:

./SAV.desktop

[Desktop Entry]
Exec=~/restore2.sh save
GenericName=Save Windows Positions
Icon=preferences-desktop-default-applications
Name=Save Windows Positions
StartupNotify=true
Terminal=true
Type=Application
X-KDE-SubstituteUID=false

./RES.desktop

[Desktop Entry]
Comment[en_US]=
Comment=
Exec=~/restore2.sh restore
GenericName[en_US]=Restore Windows Positions
GenericName=Restore Windows Positions
Icon=plasma-media-center
MimeType=
Name[en_US]=Restore Windows Positions
Name=Restore Windows Positions
Path=
StartupNotify=true
Terminal=true
TerminalOptions=
Type=Application
X-DBUS-ServiceName=
X-DBUS-StartupType=
X-KDE-SubstituteUID=false
X-KDE-Username=

save all files to home, drag-drop SAV.desktop and RES.desktop to panel and use

License - WTFPL

Offline

#2 2023-04-26 21:16:02

seth
Member
Registered: 2012-09-03
Posts: 64,524

Re: [SOLVED] Save and restore the position of the windows in KDE - 2023

How is this not a PSA re-post of https://bbs.archlinux.org/viewtopic.php … 4#p2086994 ?
Also you're giving the Parrot Inspector a heart attack, https://bbs.archlinux.org/viewtopic.php … 3#p2096713

And the approach is bonkers: try

wmctrl -lG

and nb. that "read" can read into multiple variables.
That should shorten the thing significantly and end up being far more efficient, less fragile and elegant enough to not lead to further cardiac arrests.

Offline

#3 2023-04-26 22:26:01

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,370
Website

Re: [SOLVED] Save and restore the position of the windows in KDE - 2023

Something like this should then work:

#!/bin/sh

case $1 in

   -s|save)
      wmctrl -lG > /tmp/winlist
      ;;

   -r|restore)
      while read win desk x y w h; do
         [ desktop == -1 ] && deskarg="-t $desk" || deskarg=""
         wmctrl -r $win -e 0,$x,$y,$w,$h $deskarg
      done < /tmp/winlist
      ;;

esac

I'm not sure how the -t argument is handled if there is no desktop information - nor do I know if KDE sets a desktop value for windows (I think it does), so I've written this in a fairly generic way, but for specific use cases and / or depending on whether a -1 value for -t would be appropriately ignored, that second case could be simplified a bit more.

Last edited by Trilby (2023-04-26 22:29:13)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#4 2023-04-26 23:02:48

seth
Member
Registered: 2012-09-03
Posts: 64,524

Re: [SOLVED] Save and restore the position of the windows in KDE - 2023

One pitfall is that wmctrl lists the geometry w/o _NET_FRAME_EXTENTS, what's great for the size and to stabilize against window decoration changes, but the WM will likely mishandle the position.

xprop -id <WId> -notype _NET_FRAME_EXTENTS

gets you

_NET_FRAME_EXTENTS = <left>, <right>, <top>, <bottom>

w/ an unfortunate comma and you'll need shell math.

Offline

#5 2023-04-27 03:51:34

grandrew
Member
Registered: 2022-12-20
Posts: 6

Re: [SOLVED] Save and restore the position of the windows in KDE - 2023

seth wrote:

How is this not a PSA re-post of https://bbs.archlinux.org/viewtopic.php … 4#p2086994 ?
Also you're giving the Parrot Inspector a heart attack, https://bbs.archlinux.org/viewtopic.php … 3#p2096713

And the approach is bonkers: try

wmctrl -lG

and nb. that "read" can read into multiple variables.
That should shorten the thing significantly and end up being far more efficient, less fragile and elegant enough to not lead to further cardiac arrests.

1. Feel free to improve the scripts until moderators close this thread too, then repost

2. Not a coding style show-off, if you propose a better solution - please test it in your environment and excplicitly tell if that's a ready-to-use stuff and I'll happily update the post + add credits

it's just a very frustrating problem for me switching between different large external screens and on-device screen with no working solution in the wild. I'm using 2 copies of the script for 2 different restore setups

Offline

#6 2023-04-27 05:57:39

seth
Member
Registered: 2012-09-03
Posts: 64,524

Re: [SOLVED] Save and restore the position of the windows in KDE - 2023

please test it in your environment

I'll most certainly not install plasma.

I told you a faster way, Trilby has kindly provided an example, I explained what needs to be improved.
For your situation xdotool might be the better tool

xprop -notype -root _NET_CLIENT_LIST
xdotool getwindowgeometry --shell <WId here>

If you're interested in learning enough about shell scripting to be able to leverage that, I'll happily help you and I'm sure Trilby will as well, just for mental rest.

To deal w/ the spcific problem, you can possibly also just SIGSTOP kwin_x11 while changing the outputs and or approach the layout change smarter in the sense that you first grow the layout (to the left) before you remove any outputs.

Offline

#7 2023-04-27 15:06:51

grandrew
Member
Registered: 2022-12-20
Posts: 6

Re: [SOLVED] Save and restore the position of the windows in KDE - 2023

seth, Trillby, thank you for the hints and comments! I'll update the scripts once I get some free time to work on this problem. Or, hopefully someone else affected by this could chime in and release an improved solution!

Regarding plasma et al - that's what came with my Steam Deck and caught me by surprise smile This is another VERY helpful script that I use daily: Custom Shortcuts -> CTRL-ALT-Y -> "killall -9 kwin_x11 && sleep 1 &&  kwin_x11 --replace & killall plasmashell & sleep 1 && plasmashell"

Cheers

Offline

#8 2023-04-27 15:36:37

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,370
Website

Re: [SOLVED] Save and restore the position of the windows in KDE - 2023

grandrew wrote:

... that's what came with my Steam Deck ...

Should have seen that coming.  SteamOS is not arch linux:

https://bbs.archlinux.org/misc.php?action=rules


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#9 2023-04-27 16:31:15

grandrew
Member
Registered: 2022-12-20
Posts: 6

Re: [SOLVED] Save and restore the position of the windows in KDE - 2023

I would have posted my solution elsewhere - just found similar discussion here, by doing absolutely random googling. It is distro-agnostic and I guess none of the plasma problems are arch's but hopefully it may help other people using arch or whatever.

I did not install this. This rule is about installers. Please write another rule that says I should've explicitly installed Arch myself on a specific Intel CPUID to ask questions

Offline

#10 2023-04-27 18:40:11

seth
Member
Registered: 2012-09-03
Posts: 64,524

Re: [SOLVED] Save and restore the position of the windows in KDE - 2023

This rule is about installers.

The rules wrote:

These forums are for Arch Linux x86_64 ONLY.

Not Artix, or Apricity, or Manjaro, or any of the "easy Arch installers", nor Arch-ARM; nothing other than vanilla 64-bit Arch Linux.

SteamOS is not "vanilla 64-bit Arch Linux" by any stretch of your imagination.

Your post is also in violation of https://wiki.archlinux.org/title/Genera … ow_to_post

Do not post tutorials or "how to's": documentation belongs in the wiki, where it can be maintained.

If you want help w/ your script, that should happen in https://bbs.archlinux.org/viewforum.php?id=33 and you should specifically ask for that and work towards that end.
If you disclose the SteamOS condition, I'd personally be fine w/ you running a thread to learn somthing there, regardless of the specific OS.
Any moderator might disagree, though.

Offline

#11 2023-04-27 18:54:22

grandrew
Member
Registered: 2022-12-20
Posts: 6

Re: [SOLVED] Save and restore the position of the windows in KDE - 2023

Thank you for clarification, that's really helpful for me to understand the status quo

I use a different logic though: If the solution googles in a specific place, I assume google's algorithms detected that (many) people find the solution useful by following the link. This means that if I were to contribute to the community, I would want to post to the same thread that comes up first in google search - as best, and post to the same platform with same keywords as second-best solution - to help the community in a most efficient way.

Apologies for this going completely off-topic, but that's the reality of 2023 that frustrates - I believe - not only me, when useful solutions and topics get "closed" by moderator/community rules/whatever while actual value for the very community hasn't been taken into consideration.

Offline

Board footer

Powered by FluxBB