fix: screenshot tools differ in window and region

Fixes: https://github.com/ben-grande/qubes-qvm-screenshot-tool/issues/1
This commit is contained in:
Ben Grande 2025-02-20 12:26:32 +01:00
parent 0eb6e1d316
commit 6424a7b4a6
No known key found for this signature in database
GPG Key ID: 00C64E14F51F9E56
2 changed files with 69 additions and 31 deletions

View File

@ -12,40 +12,62 @@
set -eu set -eu
msg(){
printf '%s\n' "$*" >&2
}
unsupported_type_per_tool(){
msg "${screenshot_cmd}: Unsupported screenshot type: ${screenshot_type}"
exit 1
}
take_screenshot() { take_screenshot() {
screenshot_type="${1}" screenshot_type="${1}"
shift "${#}"
set -- "${screenshot_file}"
arg=""
case "${screenshot_cmd}" in case "${screenshot_cmd}" in
spectacle) spectacle)
set -- -o "${@}"
case "${screenshot_type}" in case "${screenshot_type}" in
window) spectacle -a -o "${screenshot_file}";; region) arg="-r";;
fullscreen) spectacle -f -o "${screenshot_file}";; window) arg="-a";;
*) printf '%s\n' "Unsupported screenshot type" >&2; exit 1;; fullscreen) arg="-f";;
*) unsupported_type_per_tool;;
esac esac
;; ;;
xfce4-screenshooter) xfce4-screenshooter)
set -- -s "${@}"
case "${screenshot_type}" in case "${screenshot_type}" in
window) xfce4-screenshooter -w -s "${screenshot_file}";; region) arg="-r";;
fullscreen) xfce4-screenshooter -f -s "${screenshot_file}";; window) arg="-w";;
*) printf '%s\n' "Unsupported screenshot type" >&2; exit 1;; fullscreen) arg="-f";;
*) unsupported_type_per_tool;;
esac esac
;; ;;
scrot) scrot)
set -- -b "${@}"
case "${screenshot_type}" in case "${screenshot_type}" in
window) scrot -s -b -- "${screenshot_file}";; region|window) arg="-s";;
fullscreen) scrot -b -- "${screenshot_file}";; fullscreen) arg="";;
*) printf '%s\n' "Unsupported screenshot type" >&2; exit 1;; *) unsupported_type_per_tool;;
esac esac
;; ;;
maim) maim)
set -- -o -u -F "${@}"
case "${screenshot_type}" in case "${screenshot_type}" in
window) maim -s -o -u -- "${screenshot_file}";; region|window) arg="-s";;
fullscreen) maim -o -u -- "${screenshot_file}";; fullscreen) arg="";;
*) printf '%s\n' "Unsupported screenshot type" >&2; exit 1;; *) unsupported_type_per_tool;;
esac esac
;; ;;
*) printf '%s\n' "Unsupported screenshot tool" >&2; exit 1;; *) msg "Unsupported screenshot tool"; exit 1;;
esac esac
if test -n "${arg}"; then
set -- "${arg}" "${@}"
fi
"${screenshot_cmd}" "${@}"
} }
print_help(){ print_help(){
@ -53,8 +75,9 @@ print_help(){
printf '%s\n' "Usage: ${0##*/} [OPTIONS] printf '%s\n' "Usage: ${0##*/} [OPTIONS]
-h, --help print this help message and exit -h, --help print this help message and exit
Capture mode: Capture mode:
-r, --region select only a region of the screen -r, --region capture a region
-f, --fullscreen select all the available screen -w, --window capture a window
-f, --fullscreen capture everything
File outcome: File outcome:
-d, --qube NAME qube to save screenshot -d, --qube NAME qube to save screenshot
--qube-file-manager open file manager in qube --qube-file-manager open file manager in qube
@ -62,7 +85,14 @@ File outcome:
Development mode: Development mode:
-D, --dialog-cmd dialog tool: kdialog, zenity -D, --dialog-cmd dialog tool: kdialog, zenity
-S, --screenshot-cmd screenshot tool: maim, scrot, spectacle, -S, --screenshot-cmd screenshot tool: maim, scrot, spectacle,
xfce4-screenshooter" xfce4-screenshooter
Note:
maim and scrot:
They do not have a separate option for region or window, therefore,
selecting either of them will have the same effect, which is, capture a
window by clicking on it, capture a region by dragging the mouse.
xfce4-screenshooter:
Window option can only capture the active window."
# editorconfig-checker-enable # editorconfig-checker-enable
exit 1 exit 1
} }
@ -95,7 +125,10 @@ while test "$#" -gt 0; do
print_help print_help
;; ;;
-r|--region) -r|--region)
screenshot_type_text="Region or Window" screenshot_type_text="Region"
;;
-w|--window)
screenshot_type_text="Window"
;; ;;
-f|--fullscreen) -f|--fullscreen)
screenshot_type_text="Fullscreen" screenshot_type_text="Fullscreen"
@ -162,7 +195,7 @@ if test -n "${screenshot_cmd_wanted}"; then
case "${dialog_cmd}" in case "${dialog_cmd}" in
zenity) zenity --info --text -- "${msg}";; zenity) zenity --info --text -- "${msg}";;
kdialog) kdialog --msgbox -- "${msg}";; kdialog) kdialog --msgbox -- "${msg}";;
*) printf '%s\n' "Unsupported dialog command" >&2; exit 1;; *) msg "Unsupported dialog command"; exit 1;;
esac esac
exit 1 exit 1
fi fi
@ -192,7 +225,7 @@ else
case "${dialog_cmd}" in case "${dialog_cmd}" in
zenity) zenity --info --text -- "${msg}";; zenity) zenity --info --text -- "${msg}";;
kdialog) kdialog --msgbox -- "${msg}";; kdialog) kdialog --msgbox -- "${msg}";;
*) printf '%s\n' "Unsupported dialog command" >&2; exit 1;; *) msg "Unsupported dialog command"; exit 1;;
esac esac
exit 1 exit 1
fi fi
@ -207,22 +240,25 @@ if test -z "${screenshot_type_text}"; then
--text "${dialog_title}" \ --text "${dialog_title}" \
--radiolist \ --radiolist \
--column "Pick" --column "Mode" -- \ --column "Pick" --column "Mode" -- \
TRUE "Region or Window" \ TRUE "Region" \
TRUE "Window" \
FALSE "Fullscreen" \ FALSE "Fullscreen" \
)" )"
;; ;;
kdialog) kdialog)
screenshot_type_text="$(kdialog --radiolist "${dialog_title}" -- \ screenshot_type_text="$(kdialog --radiolist "${dialog_title}" -- \
"Region or Window" "Region or Window" off \ "Region" "Region" off \
"Window" "Window" off \
"Fullscreen" "Fullscreen" off \ "Fullscreen" "Fullscreen" off \
)" )"
;; ;;
*) printf '%s\n' "Unsupported dialog command" >&2; exit 1;; *) msg "Unsupported dialog command"; exit 1;;
esac esac
fi fi
case "${screenshot_type_text}" in case "${screenshot_type_text}" in
"Region or Window") take_screenshot window;; "Region") take_screenshot region;;
"Window") take_screenshot window;;
"Fullscreen") take_screenshot fullscreen;; "Fullscreen") take_screenshot fullscreen;;
*) printf '%s\n' "[ERROR] mode not selected"; exit 1;; *) printf '%s\n' "[ERROR] mode not selected"; exit 1;;
esac esac
@ -233,7 +269,7 @@ if ! test -f "${screenshot_file}"; then
case "${dialog_cmd}" in case "${dialog_cmd}" in
zenity) zenity --warning --text -- "${msg}";; zenity) zenity --warning --text -- "${msg}";;
kdialog) kdialog --sorry -- "${msg}";; kdialog) kdialog --sorry -- "${msg}";;
*) printf '%s\n' "Unsupported dialog command" >&2; exit 1;; *) msg "Unsupported dialog command"; exit 1;;
esac esac
exit 1 exit 1
fi fi
@ -259,7 +295,7 @@ if test "${screenshot_action_supplied}" != "1"; then
"Move file" "Move file" off "Move file" "Move file" off
)" )"
;; ;;
*) printf '%s\n' "Unsupported dialog command" >&2; exit 1;; *) msg "Unsupported dialog command"; exit 1;;
esac esac
if test -z "${screenshot_action_text}"; then if test -z "${screenshot_action_text}"; then
@ -305,7 +341,7 @@ if test -z "${qube}"; then
# shellcheck disable=SC2086 # shellcheck disable=SC2086
qube="$(kdialog --radiolist "${dialog_title}" -- ${qube_list})" qube="$(kdialog --radiolist "${dialog_title}" -- ${qube_list})"
;; ;;
*) printf '%s\n' "Unsupported dialog command" >&2; exit 1;; *) msg "Unsupported dialog command"; exit 1;;
esac esac
if test -z "${qube}"; then if test -z "${qube}"; then
msg="qube was not selected" msg="qube was not selected"
@ -313,7 +349,7 @@ if test -z "${qube}"; then
case "${dialog_cmd}" in case "${dialog_cmd}" in
zenity) zenity --error --text -- "${msg}";; zenity) zenity --error --text -- "${msg}";;
kdialog) kdialog --error -- "${msg}";; kdialog) kdialog --error -- "${msg}";;
*) printf '%s\n' "Unsupported dialog command" >&2; exit 1;; *) msg "Unsupported dialog command"; exit 1;;
esac esac
exit 1 exit 1
fi fi
@ -325,7 +361,7 @@ if ! qvm-check -- "${qube}" >/dev/null 2>&1; then
case "${dialog_cmd}" in case "${dialog_cmd}" in
zenity) zenity --error --text -- "${msg}";; zenity) zenity --error --text -- "${msg}";;
kdialog) kdialog --error -- "${msg}";; kdialog) kdialog --error -- "${msg}";;
*) printf '%s\n' "Unsupported dialog command" >&2; exit 1;; *) msg "Unsupported dialog command"; exit 1;;
esac esac
exit 1 exit 1
fi fi

View File

@ -1,5 +1,5 @@
{# {#
SPDX-FileCopyrightText: 2024 Benjamin Grande M. S. <ben.grande.b@gmail.com> SPDX-FileCopyrightText: 2024 - 2025 Benjamin Grande M. S. <ben.grande.b@gmail.com>
SPDX-License-Identifier: AGPL-3.0-or-later SPDX-License-Identifier: AGPL-3.0-or-later
#} #}
@ -39,8 +39,10 @@ include:
- name: | - name: |
DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/{{ gui_user.gui_user_id }}/bus" DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/{{ gui_user.gui_user_id }}/bus"
export DBUS_SESSION_BUS_ADDRESS export DBUS_SESSION_BUS_ADDRESS
xfconf-query -c xfce4-keyboard-shortcuts -p "/commands/custom/Print" -n -s "qvm-screenshot --fullscreen" xfconf-query -c xfce4-keyboard-shortcuts -p "/commands/custom/Print" -n -s "qvm-screenshot"
xfconf-query -c xfce4-keyboard-shortcuts -p "/commands/custom/<Alt>Print" -n -s "qvm-screenshot --region" xfconf-query -c xfce4-keyboard-shortcuts -p "/commands/custom/<Primary>Print" -n -s "qvm-screenshot --fullscreen"
xfconf-query -c xfce4-keyboard-shortcuts -p "/commands/custom/<Alt>Print" -n -s "qvm-screenshot --window"
xfconf-query -c xfce4-keyboard-shortcuts -p "/commands/custom/<Shift>Print" -n -s "qvm-screenshot --region"
- runas: {{ gui_user.gui_user }} - runas: {{ gui_user.gui_user }}
- require: - require:
- file: "{{ slsdotpath }}-screenshot-script" - file: "{{ slsdotpath }}-screenshot-script"