Is this behavior already possible?
Everything is possible in linux. You will need some basic knowledge about the Xserver, some time and a system to test on, since you are messing with your X and you certainly need the browser running if you need help :-)
In my opinion there are three possible solutions:
Multimonitor-setups use Xinerama. The simplest solution i can think of is to add a third virtual screen to your configuration and simply move the windows there using xdotool. There are some downsides to this solution but it would not require the effort of having to move windows between different Xservers which in general is not possible.
Since it is not possible to move windows between different Xservers, we need a kind of proxy like xmove or Xpra to do this for us. I think the windows then live in a separate Xfvb session to hide the windows. You can attach and detach to the session.
Regardless of what method you are using, you need an event when the monitor gets unplugged. The event should get triggered when the screen is disconnected and also when reconnected. You can daemonize the script to watch for the event.
I would prefer the solution using Xpra for various reasons. I can only give you some hints of how to do it. The solutions are not complete but should get you started.
The Xinerama setup has to get extended to have a static configuration:
see also fake third screen and Xdummy
get affected windows
We can use xdotool to search for windows. Afterwards we can check their position and move them. I am assuming two monitors with
1920x1080 configuration. The following will move all windows from the right to the left xinerama screen.
while read xwindowid; do
eval $(xdotool getwindowgeometry --shell "$xwindowid")
if (( X > 1920 && X <= (1920 * 2))); then
xdotool windowmove --relative $WINDOW -1920 y
done <<< "$(xdotool search --screen 0 '.*')"
You can now move all the windows from your laptop screen (screen0) to a third (virtual) screen if a monitor gets attached. At the same time you can move any window that is stored on the third screen to the first screen.
In order to use Xpra for moving the windows, all windows have to get managed by Xpra prior. You have to start the whole desktop using Xpra on system startup. Two times one is your main desktop where you work and the other is mainly a virtual desktop.
xpra start-desktop :99
xpra start-desktop :1 --exit-with-children −−attach
Now you can have Windows for the disconnected screen running in a separate xpra session and attach/detach from it when the monitor switches.
xpra attach :99
xpra detatch :99
There is also a dicontinued gnome shell extension that could be useful if you want to manually move a window from the hidden
:99 to the normal
:0 between started xpra displays.
You can watch for the disconnect/reconnect of a specific display
maybe watch for changed dimensions of your xinerama using xwininfo to have it independent of the un-plugged monitor if you want i.e. to extend your setup to 3 monitors and unplug an arbitrary device. Note that this will only work for the xpra solution.
xwininfo -display :0 -root | grep -e "-geometry"