Skip to content

Instantly share code, notes, and snippets.

@jpouellet
Last active October 31, 2020 08:01
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jpouellet/0f74459699433cabc26c389caf36b455 to your computer and use it in GitHub Desktop.
Save jpouellet/0f74459699433cabc26c389caf36b455 to your computer and use it in GitHub Desktop.
Opens a terminal in the front-most Qubes VM.
#!/usr/bin/env perl
use strict;
use warnings;
sub dom0_term {
(exec 'xfce4-terminal') or (exec 'xterm') or die "dom0 terminal exec failed\n";
}
sub domU_term {
my $vm = shift;
exec {'qvm-run'} ('qvm-run', '-q', @_, '--', $vm, 'if command -v qubes-run-terminal >/dev/null; then exec qubes-run-terminal; elif command -v gnome-terminal >/dev/null; then exec gnome-terminal; else exec xterm; fi') or die "qvm-run exec failed\n";
}
sub term {
my $vm = shift;
if ($vm eq 'dom0') {
dom0_term;
} else {
domU_term $vm, @_;
}
}
sub front_vm {
$_ = `xprop -notype -root _NET_ACTIVE_WINDOW`;
/^_NET_ACTIVE_WINDOW: window id # (?<id>0x[0-9a-f]+), 0x0$/m or die "unable to get active window\n";
$_ = `xprop -id "$+{id}" -notype _QUBES_VMNAME`;
if (/^_QUBES_VMNAME = \"(?<vm>[^\"]+)\"$/m) {
length $+{vm} or die "empty string VM is invalid\n";
return $+{vm};
}
return undef;
}
my @auxargs = ();
if (defined($ARGV[0]) and $ARGV[0] eq '-r') {
shift;
@auxargs = qw(-u root);
}
my $vm;
defined($vm = $ARGV[0]) or defined($vm = front_vm) or $vm = 'dom0';
term $vm, @auxargs;
#!/bin/sh
set -e
front=$(xprop -notype -root _NET_ACTIVE_WINDOW | awk '{if (match($0,"^_NET_ACTIVE_WINDOW: window id # (0x[0-9a-f]*), 0x0$",m)){print m[1];} exit 0;}' )
test -n "$front"
vm=$(xprop -id "$front" -notype _QUBES_VMNAME | awk '{if (match($0,"^_QUBES_VMNAME = \"([^\"]*)\"$",m)){print m[1];} exit 0;}')
if [ -n "$vm" ]; then
exec qvm-run -- "$vm" 'sh -c "exec gnome-terminal || exec xterm"'
else
exec xfce4-terminal
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment