From 7e6858ca6877639f4ecf098a8ae3d264f34a9157 Mon Sep 17 00:00:00 2001 From: derped Date: Wed, 20 Nov 2024 18:00:45 +0100 Subject: [PATCH] sway: add screenshot wrapper script --- config/etc/sway/config.nix | 40 ++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/config/etc/sway/config.nix b/config/etc/sway/config.nix index be6274f..e65099b 100644 --- a/config/etc/sway/config.nix +++ b/config/etc/sway/config.nix @@ -1,8 +1,35 @@ {pkgs}: -pkgs.writeText "config" -/* -sway -*/ + +let + # 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/* @@ -114,6 +141,11 @@ sway 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. # Resize them with right mouse button + $mod. # Despite the name, also works for non-floating windows.