sway: add screenshot wrapper script

This commit is contained in:
Kevin Baensch 2024-11-20 18:00:45 +01:00
parent 7b5936d322
commit 7e6858ca68
Signed by: derped
GPG key ID: C0F1D326C7626543

View file

@ -1,8 +1,35 @@
{pkgs}: {pkgs}:
pkgs.writeText "config"
/* let
sway # TODO/FIX: pkgs.sway-unwrapped is not the same sway derivation as the one running the desktop
*/ grim-wrapper = pkgs.writeShellScript "grim-wrapper" /* bash */ ''
OUTPUT_DIR="$(${pkgs.xdg-user-dirs}/bin/xdg-user-dir PICTURES)"
DATE="$(${pkgs.coreutils}/bin/date +'%Y-%m-%d;%H:%M:%S;')";
WINDOW_LIST="$(${pkgs.sway-unwrapped}/bin/swaymsg -t get_tree | ${pkgs.jq}/bin/jq -r '.. | select(.pid? and .visible?) | "\(.rect.x+.window_rect.x),\(.rect.y+.window_rect.y) \(.window_rect.width)x\(.window_rect.height) \(.app_id):\(.name)"' | ${pkgs.coreutils}/bin/tr -d '"/;\\')";
case "''${1}" in
selection)
# split slurp selection result into jseon selectable screen area (for grim) and output name
SELECTION_FORMAT='{ "area": "%x,%y %wx%h", "name": "%wx%h;%o;%l" }';
SELECTION="$(${pkgs.slurp}/bin/slurp -d -f "''${SELECTION_FORMAT}" <<< ''${WINDOW_LIST})";
SELECTION_AREA="$(${pkgs.jq}/bin/jq -r '.area' <<< "''${SELECTION}")";
SELECTION_NAME="$(${pkgs.jq}/bin/jq -r '.name' <<< "''${SELECTION}"| ${pkgs.coreutils}/bin/tr ' ' '_')";
OUTNAME="''${OUTPUT_DIR}/''${DATE}''${SELECTION_NAME}.png";
${pkgs.grim}/bin/grim -g "''${SELECTION_AREA}" "''${OUTNAME}";
;;
clip)
SELECTION="$(${pkgs.slurp}/bin/slurp -d <<< ''${WINDOW_LIST})";
${pkgs.grim}/bin/grim - | ${pkgs.wl-clipboard}/bin/wl-copy;
;;
*)
DISPLAY="$(${pkgs.sway-unwrapped}/bin/swaymsg -t get_outputs | ${pkgs.jq}/bin/jq -r '.[] | select(.focused) | .name')";
DISPLAY_RES="$(${pkgs.sway-unwrapped}/bin/swaymsg -t get_outputs | jq -r '.[] | select(.focused) | .current_mode | "\(.width)x\(.height)"')";
OUTNAME="''${OUTPUT_DIR}/''${DATE}''${DISPLAY_RES};''${DISPLAY};.png"
${pkgs.grim}/bin/grim -o "''${DISPLAY}" "''${OUTNAME}";
;;
esac;
'';
in pkgs.writeText "config"
'' ''
include /etc/sway/config.d/* include /etc/sway/config.d/*
@ -114,6 +141,11 @@ sway
bindsym $mod+Shift+w sticky toggle bindsym $mod+Shift+w sticky toggle
# Screenshots
bindsym $mod+Ctrl+s exec ${grim-wrapper} selection
bindsym $mod+Ctrl+a exec ${grim-wrapper}
bindsym $mod+Ctrl+y exec ${grim-wrapper} clip
# Drag floating windows by holding down $mod and left mouse button. # Drag floating windows by holding down $mod and left mouse button.
# Resize them with right mouse button + $mod. # Resize them with right mouse button + $mod.
# Despite the name, also works for non-floating windows. # Despite the name, also works for non-floating windows.