nix/home/config/sway/module/Screenshot.nix

144 lines
4.2 KiB
Nix
Raw Normal View History

2024-09-07 05:36:55 +03:00
{ config, pkgs, lib, ... }: let
2024-08-22 19:54:20 +03:00
codec = "libsvtav1";
2024-08-22 19:13:40 +03:00
color = config.style.color;
container = "mp4";
2024-08-22 19:54:20 +03:00
format = "%Y-%m-%d_%H-%M-%S";
2024-08-28 03:09:03 +03:00
framerate = 10;
2024-08-22 19:13:40 +03:00
opacity = "26";
selection = "slurp -d -b ${color.bg.light}${opacity} -c ${color.fg.light} -w 0 -s 00000000";
2024-08-22 20:53:39 +03:00
pixfmt = "yuv420p10le";
2024-08-22 19:13:40 +03:00
in {
2024-09-07 05:36:55 +03:00
text = let
picEdit = ''swappy -f - -o -'';
picFull = ''-o $(swaymsg -t get_outputs | jq -r ".[] | select(.focused) | .name") -'';
picPrepFile = prepFile "\${XDG_PICTURES_DIR[0]}" "png";
picRefLatestFile = refLatestFile "png";
picSelected = ''-g "''${scrSelection}" -'';
picToBuffer = ''wl-copy -t image/png'';
picToFile = ''tee "''${scrFile}"'';
screenshot = ''grim'';
updateWaybar = ''pkill -RTMIN+4 waybar'';
vidFull = ''-o $(swaymsg -t get_outputs | jq -r ".[] | select(.focused) | .name") -'';
vidPrepFile = prepFile "\${XDG_VIDEOS_DIR[0]}" container;
vidRefLatestFile = refLatestFile container;
vidSelected = ''--geometry "''${scrSelection}"'';
vidStop = ''pkill -SIGINT wf-recorder'';
2024-09-07 05:36:55 +03:00
prepFile = path: ext: ''
# Focused app id by default.
curWindow=$(parse_snake $(swaymsg -t get_tree | jq '.. | select(.type?) | select(.focused==true) | .app_id'))
# If no id (i.e. xwayland), then use a name (title).
[[ "''${curWindow}" = "null" ]] && curWindow=$(parse_snake $(swaymsg -t get_tree | jq '.. | select(.type?) | select(.focused==true) | .name'))
# If no app in focus, use "unknown" dir.
[[ "''${curWindow}" =~ ^[0-9]+$ ]] && curWindow="unknown"
# Prepare dir and file path.
scrPath="${path}"
scrDir="${path}/''${curWindow}"
mkdir -p "''${scrDir}"
scrName="$(date +${format}).${ext}"
scrFile="''${scrDir}/''${scrName}"
scrLatestRef="./''${curWindow}/''${scrName}"
'';
refLatestFile = ext: ''
scrLatest="''${scrPath}/Latest.${ext}"
rm "''${scrLatest}"
ln -s "''${scrLatestRef}" "''${scrLatest}"
'';
2024-09-14 11:35:58 +03:00
getSelection = ''
2024-09-07 05:36:55 +03:00
scrSelection=$(${selection})
[[ -n "''${scrSelection}" ]] || exit
'';
2024-09-14 11:35:58 +03:00
getTransform = ''
scrTransform="$(swaymsg -t get_outputs | jq -r '.[] | select(.focused) | .transform')"
[[ "''${scrTransform}" = "normal" ]] && scrTransform=""
'';
2024-09-07 05:36:55 +03:00
2024-09-14 11:35:58 +03:00
vidStart = ''
wf-recorder \
--codec ${codec} \
--file "''${scrFile}" \
--framerate ${toString framerate} \
--pixel-format ${pixfmt} \
'';
2024-09-07 05:36:55 +03:00
2024-09-14 11:35:58 +03:00
vidMuxAudio = ''
ffmpeg \
-f lavfi \
-i anullsrc=channel_layout=stereo:sample_rate=44100 \
-i "''${scrFile}" \
-c:v copy \
-c:a libopus \
-shortest \
-f ${container} \
"''${scrFile}_" \
&& mv "''${scrFile}_" "''${scrFile}" \
|| rm "''${scrFile}_"
'';
2024-09-07 05:36:55 +03:00
2024-09-14 11:35:58 +03:00
vidTransform = ''
if [[ -n "''${scrTransform}" ]]; then
ffmpeg \
-display_rotation ''${scrTransform} \
-i "''${scrFile}" \
-c copy \
-f ${container} \
"''${scrFile}_" \
&& mv "''${scrFile}_" "''${scrFile}" \
|| rm "''${scrFile}_"
2024-09-14 11:35:58 +03:00
fi
'';
2024-09-14 05:09:53 +03:00
2024-09-14 11:35:58 +03:00
SelectRecording = pkgs.writeShellScriptBin "SelectRecording" ''
${vidStop} || {
${getSelection}
${getTransform}
${vidPrepFile}
2024-09-14 11:35:58 +03:00
${updateWaybar}
${vidStart} ${vidSelected}
2024-09-14 11:35:58 +03:00
${vidMuxAudio}
${vidTransform}
${vidRefLatestFile}
2024-09-14 11:35:58 +03:00
${updateWaybar}
2024-09-07 05:36:55 +03:00
};
'';
2024-09-14 11:35:58 +03:00
FullscreenRecording = pkgs.writeShellScriptBin "FullscreenRecording" ''
${vidStop} || {
${getTransform}
${vidPrepFile}
2024-09-14 11:35:58 +03:00
${updateWaybar}
${vidStart} ${vidFull}
${vidMuxAudio}
${vidTransform}
${vidRefLatestFile}
2024-09-14 11:35:58 +03:00
${updateWaybar}
};
'';
2024-09-07 05:36:55 +03:00
2024-09-14 11:35:58 +03:00
FullscreenScreenshot = pkgs.writeShellScriptBin "FullscreenScreenshot" ''
${picPrepFile}
2024-09-14 05:09:53 +03:00
${screenshot} ${picFull} | ${picToFile} | ${picToBuffer} && ${picRefLatestFile}
2024-09-14 11:35:58 +03:00
'';
SelectScreenshot = pkgs.writeShellScriptBin "SelectScreenshot" ''
${getSelection}
${picPrepFile}
2024-09-14 11:35:58 +03:00
${screenshot} ${picSelected} | ${picEdit} | ${picToFile} | ${picToBuffer} && ${picRefLatestFile}
2024-09-07 05:36:55 +03:00
'';
in ''
bindsym --to-code $mod+y exec ${lib.getExe FullscreenScreenshot}
2024-09-14 11:35:58 +03:00
bindsym --to-code $mod+shift+y exec ${lib.getExe FullscreenRecording}
2024-04-06 03:03:58 +03:00
bindsym --to-code $mod+v exec ${lib.getExe SelectScreenshot}
2024-09-14 11:35:58 +03:00
bindsym --to-code $mod+shift+v exec ${lib.getExe SelectRecording}
2024-04-06 03:03:58 +03:00
'';
}