From 1174f2a70b39dc3b77df93057ce500c9acad1e58 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 17 Jul 2026 18:06:42 -0700 Subject: [PATCH] Add live webcam overlay sizing --- bin/omarchy-capture-screenrecording | 37 +++++----- bin/omarchy-capture-webcam-resize | 101 +++++++++++++++++++++++++++ default/hypr/bindings/utilities.lua | 2 + test/shell.d/screenrecording-test.sh | 48 +++++++++++++ 4 files changed, 172 insertions(+), 16 deletions(-) create mode 100755 bin/omarchy-capture-webcam-resize diff --git a/bin/omarchy-capture-screenrecording b/bin/omarchy-capture-screenrecording index 24ac4add..b41b93b2 100755 --- a/bin/omarchy-capture-screenrecording +++ b/bin/omarchy-capture-screenrecording @@ -2,7 +2,7 @@ # omarchy:summary=Start or stop screen recording # omarchy:group=capture -# omarchy:args=[--fullscreen] [--with-desktop-audio] [--with-microphone-audio] [--with-webcam] [--webcam-device=] [--resolution=] [--stop-recording] +# omarchy:args=[--fullscreen] [--with-desktop-audio] [--with-microphone-audio] [--with-webcam] [--webcam-device=] [--webcam-size=] [--resolution=] [--stop-recording] # omarchy:examples=omarchy screenrecord | omarchy capture screenrecord --with-desktop-audio # omarchy:aliases=omarchy screenrecord # @@ -30,6 +30,7 @@ DESKTOP_AUDIO="false" MICROPHONE_AUDIO="false" WEBCAM="false" WEBCAM_DEVICE="" +WEBCAM_SIZE="medium" RESOLUTION="" FULLSCREEN="false" STOP_RECORDING="false" @@ -42,12 +43,21 @@ for arg in "$@"; do --with-microphone-audio) MICROPHONE_AUDIO="true" ;; --with-webcam) WEBCAM="true" ;; --webcam-device=*) WEBCAM_DEVICE="${arg#*=}" ;; + --webcam-size=*) WEBCAM_SIZE="${arg#*=}" ;; --resolution=*) RESOLUTION="${arg#*=}" ;; --fullscreen) FULLSCREEN="true" ;; --stop-recording) STOP_RECORDING="true" ;; esac done +case $WEBCAM_SIZE in +small | medium | large) ;; +*) + echo "Invalid webcam size: $WEBCAM_SIZE (expected small, medium, or large)" >&2 + exit 1 + ;; +esac + start_webcam_overlay() { cleanup_webcam @@ -60,32 +70,27 @@ start_webcam_overlay() { fi fi - # Get monitor scale - local scale=$(hyprctl monitors -j | jq -r '.[] | select(.focused == true) | .scale') - - # Target width (base 360px, scaled to monitor) - local target_width=$(awk "BEGIN {printf \"%.0f\", 360 * $scale}") - # Try preferred 16:9 resolutions in order, use first available local preferred_resolutions=("640x360" "1280x720" "1920x1080") - local video_size_arg="" + local capture_options="framerate=30" local available_formats=$(v4l2-ctl --list-formats-ext -d "$WEBCAM_DEVICE" 2>/dev/null) for resolution in "${preferred_resolutions[@]}"; do if echo "$available_formats" | grep -q "$resolution"; then - video_size_arg="-video_size $resolution" + capture_options="video_size=$resolution,$capture_options" break fi done - ffplay -f v4l2 $video_size_arg -framerate 30 "$WEBCAM_DEVICE" \ - -vf "crop=iw/2:ih,scale=${target_width}:-1" \ - -window_title "WebcamOverlay" \ - -noborder \ - -fflags nobuffer -flags low_delay \ - -probesize 32 -analyzeduration 0 \ - -loglevel quiet & + mpv "av://v4l2:$WEBCAM_DEVICE" \ + --profile=low-latency --untimed --no-cache \ + --demuxer-lavf-o="$capture_options" \ + '--vf=lavfi=[crop=ih*8/9:ih]' \ + --title="WebcamOverlay" \ + --no-border --no-audio --no-osc --osd-level=0 \ + --really-quiet &>/dev/null & sleep 1 + omarchy-capture-webcam-resize "$WEBCAM_SIZE" } cleanup_webcam() { diff --git a/bin/omarchy-capture-webcam-resize b/bin/omarchy-capture-webcam-resize new file mode 100755 index 00000000..623103a9 --- /dev/null +++ b/bin/omarchy-capture-webcam-resize @@ -0,0 +1,101 @@ +#!/bin/bash + +# omarchy:summary=Resize the active webcam recording overlay +# omarchy:group=capture +# omarchy:args= +# omarchy:examples=omarchy capture webcam resize smaller | omarchy-capture-webcam-resize reset + +set -euo pipefail + +readonly MARGIN=40 +readonly SMALL_WIDTH=240 +readonly MEDIUM_WIDTH=360 +readonly LARGE_WIDTH=480 + +usage() { + echo "Usage: omarchy-capture-webcam-resize " >&2 + exit 1 +} + +hypr_dispatch() { + local lua="$1" + shift + + hyprctl dispatch "$lua" >/dev/null 2>&1 || hyprctl dispatch "$@" >/dev/null +} + +action=${1:-} +case $action in +smaller | larger | reset | small | medium | large) ;; +*) usage ;; +esac + +if ! client=$(hyprctl clients -j 2>/dev/null | jq -cer 'first(.[] | select(.title == "WebcamOverlay")) // empty' 2>/dev/null); then + exit 0 +fi + +read -r address current_width current_height monitor_id < <( + jq -r '[.address, .size[0], .size[1], .monitor] | @tsv' <<<"$client" +) + +[[ -n $address && $current_width =~ ^[0-9]+$ && $current_height =~ ^[0-9]+$ && $monitor_id =~ ^[0-9]+$ ]] || exit 0 +((current_width > 0 && current_height > 0)) || exit 0 + +target_width=$current_width +case $action in +small) target_width=$SMALL_WIDTH ;; +medium | reset) target_width=$MEDIUM_WIDTH ;; +large) target_width=$LARGE_WIDTH ;; +smaller) + for width in $LARGE_WIDTH $MEDIUM_WIDTH $SMALL_WIDTH; do + if ((width < current_width)); then + target_width=$width + break + fi + done + ;; +larger) + for width in $SMALL_WIDTH $MEDIUM_WIDTH $LARGE_WIDTH; do + if ((width > current_width)); then + target_width=$width + break + fi + done + ;; +esac + +# The camera is center-cropped to a consistent 8:9 portrait frame. +target_height=$(((target_width * 9 + 4) / 8)) + +if ! monitor=$(hyprctl monitors -j 2>/dev/null | jq -cer --argjson id "$monitor_id" 'first(.[] | select(.id == $id)) // empty' 2>/dev/null); then + exit 0 +fi + +read -r monitor_x monitor_y monitor_width monitor_height < <( + jq -r ' + . as $monitor | + (($monitor.transform // 0) % 2 == 1) as $rotated | + [ + .x, + .y, + (((if $rotated then .height else .width end) / .scale) | floor), + (((if $rotated then .width else .height end) / .scale) | floor) + ] | @tsv + ' <<<"$monitor" +) + +[[ $monitor_x =~ ^-?[0-9]+$ && $monitor_y =~ ^-?[0-9]+$ && $monitor_width =~ ^[0-9]+$ && $monitor_height =~ ^[0-9]+$ ]] || exit 0 + +target_x=$((monitor_x + monitor_width - target_width - MARGIN)) +target_y=$((monitor_y + monitor_height - target_height - MARGIN)) + +((target_x < monitor_x + MARGIN)) && target_x=$((monitor_x + MARGIN)) +((target_y < monitor_y + MARGIN)) && target_y=$((monitor_y + MARGIN)) + +window="address:$address" +hypr_dispatch \ + "hl.dsp.window.resize({ window = \"$window\", x = $target_width, y = $target_height })" \ + resizewindowpixel "exact $target_width $target_height,$window" +hypr_dispatch \ + "hl.dsp.window.move({ window = \"$window\", x = $target_x, y = $target_y })" \ + movewindowpixel "exact $target_x $target_y,$window" diff --git a/default/hypr/bindings/utilities.lua b/default/hypr/bindings/utilities.lua index ca6e95ff..482ceb4f 100644 --- a/default/hypr/bindings/utilities.lua +++ b/default/hypr/bindings/utilities.lua @@ -38,6 +38,8 @@ o.bind("switch:off:Lid Switch", nil, "omarchy-hyprland-monitor-clamshell", { loc o.bind("PRINT", "Screenshot", "omarchy-capture-screenshot") o.bind("ALT + PRINT", "Screenrecording", "omarchy-capture-screenrecording --stop-recording || omarchy-menu toggle trigger.capture.screenrecord") +o.bind("SUPER + ALT + code:26", "Make webcam overlay smaller", "omarchy-capture-webcam-resize smaller") +o.bind("SUPER + ALT + code:27", "Make webcam overlay larger", "omarchy-capture-webcam-resize larger") o.bind("SUPER + PRINT", "Color picker", "pkill hyprpicker || hyprpicker -a") o.bind("SUPER + CTRL + PRINT", "Extract text (OCR) from screenshot", "omarchy-capture-text") diff --git a/test/shell.d/screenrecording-test.sh b/test/shell.d/screenrecording-test.sh index 93d5bc94..c32a502f 100644 --- a/test/shell.d/screenrecording-test.sh +++ b/test/shell.d/screenrecording-test.sh @@ -76,3 +76,51 @@ if ! cmp -s "$OMARCHY_TEST_RECORDER_ARGS" "$expected_recorder_args"; then fail "screenrecording webcam picker starts recording with selected device" "$(diff -u "$expected_recorder_args" "$OMARCHY_TEST_RECORDER_ARGS")" fi pass "screenrecording webcam picker starts recording with selected device" + +cat >"$stub_bin/hyprctl" <<'SH' +#!/bin/bash + +case $1 in +clients) + printf '[{"address":"0xabc","title":"%s","size":[%s,%s],"monitor":2}]\n' \ + "${OMARCHY_TEST_CLIENT_TITLE:-WebcamOverlay}" \ + "${OMARCHY_TEST_CLIENT_WIDTH:-360}" \ + "${OMARCHY_TEST_CLIENT_HEIGHT:-405}" + ;; +monitors) + printf '[{"id":2,"x":1280,"y":-100,"width":2560,"height":1600,"scale":2}]\n' + ;; +dispatch) + printf '%s\n' "$*" >>"$OMARCHY_TEST_HYPRCTL_ARGS" + ;; +esac +SH +chmod +x "$stub_bin/hyprctl" + +export OMARCHY_TEST_HYPRCTL_ARGS="$tmp_dir/hyprctl-args" + +"$ROOT/bin/omarchy-capture-webcam-resize" smaller + +expected_hyprctl_args="$tmp_dir/expected-hyprctl-args" +printf '%s\n' \ + 'dispatch hl.dsp.window.resize({ window = "address:0xabc", x = 240, y = 270 })' \ + 'dispatch hl.dsp.window.move({ window = "address:0xabc", x = 2280, y = 390 })' >"$expected_hyprctl_args" + +if ! cmp -s "$OMARCHY_TEST_HYPRCTL_ARGS" "$expected_hyprctl_args"; then + fail "webcam resize preserves its aspect ratio and corner anchor" "$(diff -u "$expected_hyprctl_args" "$OMARCHY_TEST_HYPRCTL_ARGS")" +fi +pass "webcam resize preserves its aspect ratio and corner anchor" + +: >"$OMARCHY_TEST_HYPRCTL_ARGS" +OMARCHY_TEST_CLIENT_TITLE="Other Window" "$ROOT/bin/omarchy-capture-webcam-resize" larger + +if [[ -s $OMARCHY_TEST_HYPRCTL_ARGS ]]; then + fail "webcam resize ignores other windows" "$(cat "$OMARCHY_TEST_HYPRCTL_ARGS")" +fi +pass "webcam resize ignores other windows" + +grep -F 'o.bind("SUPER + ALT + code:26", "Make webcam overlay smaller", "omarchy-capture-webcam-resize smaller")' \ + "$ROOT/default/hypr/bindings/utilities.lua" >/dev/null || fail "webcam smaller hotkey is configured" +grep -F 'o.bind("SUPER + ALT + code:27", "Make webcam overlay larger", "omarchy-capture-webcam-resize larger")' \ + "$ROOT/default/hypr/bindings/utilities.lua" >/dev/null || fail "webcam larger hotkey is configured" +pass "webcam resize hotkeys are configured"