Skip to content

Instantly share code, notes, and snippets.

@mzpqnxow
Created August 11, 2024 13:00
Show Gist options
  • Save mzpqnxow/c49c7aa063246a429c01d2780bf40b2c to your computer and use it in GitHub Desktop.
Save mzpqnxow/c49c7aa063246a429c01d2780bf40b2c to your computer and use it in GitHub Desktop.
Simple script to start a terminal in the active qube
#!/bin/bash
# Start a terminal inside the qube associated with the active Xorg window
# To use this, in dom0:
# - copy to ~/bin
# - assign it to a key binding in dom0
#
# Use-case:
# Firefox (or any other app) is open in qube "personal" and you want to pop a terminal
# open in "personal" without having to use context menus
#
# You want this behavior to work for any qube, without having a different binding for
# each
#
# - mzpqnxow, 2024 CE
#
# (no copyright, released into the public domain)
#
DEFAULT_TERM=xfce4-terminal
FALLBACK_TERM=xterm
id="$(xdotool getactivewindow)" # Get window ID of active window
vm="$(xprop -id "$id" _QUBES_VMNAME | grep -oP '"\K[^"]*')" # Get qube name via xproperty
if [ "$vm" = "" ]; then
echo "dom0 / no active vm !!"
else
echo "Current window is for Qube named $vm"
echo "Running qvm-run $vm xfce4-terminal ..."
if ! qvm-run "$vm" $DEFAULT_TERM; then
qvm-run "$vm" $FALLBACK_TERM || echo "Giving up ..."
fi
fi
@mzpqnxow
Copy link
Author

This is easy to do without much hackyness because qubes vms have rich information (including qube name) set in their window properties

I couldn't figure out how to make xprop emit the qube name without extra data on the line, so it grabs it with grep -Po - maybe someone can show me how to avoid needing that ...

user@dom0 : ~ $ xprop -id $WINDOW_ID
_NET_WM_ICON_GEOMETRY(CARDINAL) = 238, 4294967247, 200, 30
_NET_FRAME_EXTENTS(CARDINAL) = 5, 5, 29, 5
_NET_WM_ALLOWED_ACTIONS(ATOM) = _NET_WM_ACTION_CLOSE, _NET_WM_ACTION_ABOVE, _NET_WM_ACTION_BELOW, _NET_WM_ACTION_FULLSCREEN, _NET_WM_ACTION_MOVE, _NET_WM_ACTION_RESIZE, _NET_WM_ACTION_MAXIMIZE_HORZ, _NET_WM_ACTION_MAXIMIZE_VERT, _NET_WM_ACTION_SHADE, _NET_WM_ACTION_MINIMIZE, _NET_WM_ACTION_CHANGE_DESKTOP, _NET_WM_ACTION_STICK
WM_STATE(WM_STATE):
		window state: Normal
		icon window: 0x1dd28500
_NET_WM_DESKTOP(CARDINAL) = 3
_NET_WM_STATE(ATOM) = 
_QUBES_VMWINDOWID(WINDOW): window id # 0x20003c
_QUBES_VMNAME(STRING) = "personal"
_QUBES_LABEL_COLOR(CARDINAL) = 15586304
_QUBES_LABEL(CARDINAL) = 3
WM_CLASS(STRING) = "personal:Navigator", "personal:Firefox-esr"
_NET_WM_ICON(CARDINAL) = 	Icon (128 x 128):
	(not shown)

WM_PROTOCOLS(ATOM): protocols  WM_DELETE_WINDOW
_NET_WM_USER_TIME_WINDOW(WINDOW): window id # 0x5e001d2
WM_NORMAL_HINTS(WM_SIZE_HINTS):
		program specified minimum size: 450 by 120
		program specified base size: 450 by 120
WM_COMMAND(STRING) = {  }
WM_ICON_NAME(UTF8_STRING) = "Simple script to start a terminal in the active qube ___ Mozilla Firefox"
WM_NAME(UTF8_STRING) = "Simple script to start a terminal in the active qube ___ Mozilla Firefox"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment