From c8bbbb605572062e5c03985e46a990df99818187 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kier=C3=A1n=20Meinhardt?= Date: Wed, 9 Sep 2020 09:35:50 +0200 Subject: [PATCH] feat: add dmenurandr script --- configs/i3.nix | 2 + packages/scripts/default.nix | 6 +++ packages/scripts/dmenurandr.sh | 89 ++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100755 packages/scripts/dmenurandr.sh diff --git a/configs/i3.nix b/configs/i3.nix index abd58ee..37e8f7d 100644 --- a/configs/i3.nix +++ b/configs/i3.nix @@ -248,6 +248,8 @@ in with config.niveum; { "exec --no-startup-id ${pkgs.playerctl}/bin/playerctl previous"; "XF86ScreenSaver" = "exec ${pkgs.xautolock}/bin/xautolock -locknow"; + "XF86Display" = "exec ${pkgs.scripts.dmenurandr}/bin/dmenurandr"; + # key names detected with xorg.xev: # XF86WakeUp (fn twice) # XF86Battery (fn f3) diff --git a/packages/scripts/default.nix b/packages/scripts/default.nix index cdb0b17..97e06d2 100644 --- a/packages/scripts/default.nix +++ b/packages/scripts/default.nix @@ -91,6 +91,12 @@ in rec { script = ./vg.sh; }; + dmenurandr = wrapScript { + packages = [ pkgs.dmenu pkgs.gnugrep pkgs.gnused pkgs.xorg.xrandr pkgs.gawk pkgs.libnotify pkgs.arandr ]; + name = "dmenurandr"; + script = ./dmenurandr.sh; + }; + fkill = wrapScript { packages = [ pkgs.procps pkgs.gawk pkgs.gnused pkgs.fzf pkgs.bash ]; script = ./fkill.sh; diff --git a/packages/scripts/dmenurandr.sh b/packages/scripts/dmenurandr.sh new file mode 100755 index 0000000..b051ebf --- /dev/null +++ b/packages/scripts/dmenurandr.sh @@ -0,0 +1,89 @@ +#!/bin/sh +# ref https://github.com/LukeSmithxyz/voidrice/blob/927a7c39c24272eeb6c7ca9e75a359314ad20025/.local/bin/displayselect + +# A UI for detecting and selecting all displays. Probes xrandr for connected +# displays and lets user select one to use. User may also select "manual +# selection" which opens arandr. + +query_direction() { + printf "left-of\\nright-of\\nabove\\nbelow" +} + +twoscreen() { # If multi-monitor is selected and there are two screens. + + mirror=$(printf "no\\nyes" | dmenu -i -p "Mirror displays?") + # Mirror displays using native resolution of external display and a scaled + # version for the internal display + if [ "$mirror" = "yes" ]; then + external=$(echo "$screens" | dmenu -i -p "Optimize resolution for:") + internal=$(echo "$screens" | grep -v "$external") + + res_external=$(xrandr --query | sed -n "/^$external/,/\+/p" | \ + tail -n 1 | awk '{print $1}') + + res_internal=$(xrandr --query | sed -n "/^$internal/,/\+/p" | \ + tail -n 1 | awk '{print $1}') + + res_ext_x=$(echo "$res_external" | sed 's/x.*//') + res_ext_y=$(echo "$res_external" | sed 's/.*x//') + res_int_x=$(echo "$res_internal" | sed 's/x.*//') + res_int_y=$(echo "$res_internal" | sed 's/.*x//') + + scale_x=$(echo "$res_ext_x / $res_int_x" | bc -l) + scale_y=$(echo "$res_ext_y / $res_int_y" | bc -l) + + xrandr --output "$external" --auto --scale 1.0x1.0 \ + --output "$internal" --auto --same-as "$external" \ + --scale "$scale_x"x"$scale_y" + + else + + primary=$(echo "$screens" | dmenu -i -p "Select primary display:") + secondary=$(echo "$screens" | grep -v "$primary") + direction=$(query_direction | dmenu -i -p "What side of $primary should $secondary be on?") + xrandr --output "$primary" --auto --scale 1.0x1.0 --output "$secondary" --"$direction" "$primary" --auto --scale 1.0x1.0 + fi +} + +# If multi-monitor is selected and there are more than two screens. +morescreen() { + primary=$(echo "$screens" | dmenu -i -p "Select primary display:") + secondary=$(echo "$screens" | grep -v "$primary" | dmenu -i -p "Select secondary display:") + direction=$(query_direction | dmenu -i -p "What side of $primary should $secondary be on?") + tertiary=$(echo "$screens" | grep -v "$primary" | grep -v "$secondary" | dmenu -i -p "Select third display:") + xrandr --output "$primary" --auto --output "$secondary" --"$direction" "$primary" --auto --output "$tertiary" --"$(query_direction | grep -v "$direction")" "$primary" --auto +} + +# Multi-monitor handler. +multimon() { + case "$(echo "$screens" | wc -l)" in + 2) twoscreen ;; + *) morescreen ;; + esac +} + +# If only one output available or chosen. +onescreen() { + xrandr --output "$1" --auto --scale 1.0x1.0 "$(echo "$allposs" | grep -v "$1" | awk '{print "--output", $1, "--off"}' | paste -sd ' ')" +} + +# Get all possible displays +allposs=$(xrandr -q | grep "connected") + +# Get all connected screens. +screens=$(echo "$allposs" | awk '/ connected/ {print $1}') + +# If there's only one screen +[ "$(echo "$screens" | wc -l)" -lt 2 ] && { + onescreen "$screens" + notify-send "💻 Only one screen detected." "Using it in its optimal settings..." + exit +} + +# Get user choice including multi-monitor and manual selection: +chosen=$(printf "%s\\nmulti-monitor\\nmanual selection" "$screens" | dmenu -i -p "Select display arangement:") && + case "$chosen" in + "manual selection") arandr ; exit ;; + "multi-monitor") multimon ;; + *) onescreen "$chosen" ;; + esac