From 9036fc4ba5e7ffc041f6e812eb0b3dcc4efe2c5a Mon Sep 17 00:00:00 2001
From: Oliver Davies <oliver@oliverdavies.dev>
Date: Mon, 17 Mar 2025 23:05:33 +0000
Subject: [PATCH] Change scaling logic

---
 nix/hosts/t490/configuration.nix     |  1 -
 nix/pkgs/displayselect/default.nix   |  5 ++++-
 nix/pkgs/displayselect/scaling.patch | 26 ++++++++++++++++++++++++++
 3 files changed, 30 insertions(+), 2 deletions(-)
 create mode 100644 nix/pkgs/displayselect/scaling.patch

diff --git a/nix/hosts/t490/configuration.nix b/nix/hosts/t490/configuration.nix
index 30d97293..347ec4f9 100644
--- a/nix/hosts/t490/configuration.nix
+++ b/nix/hosts/t490/configuration.nix
@@ -52,7 +52,6 @@
   };
 
   environment.systemPackages = with pkgs; [
-    bc # Needed for ./displayselect script.
     gtypist
     newsboat
     pam_gnupg
diff --git a/nix/pkgs/displayselect/default.nix b/nix/pkgs/displayselect/default.nix
index efc5c8a7..a4992c24 100644
--- a/nix/pkgs/displayselect/default.nix
+++ b/nix/pkgs/displayselect/default.nix
@@ -15,13 +15,16 @@ stdenv.mkDerivation {
 
   buildInputs = [
     bash
-    bc
     xorg.xrandr
   ];
 
+  patches = [ ./scaling.patch ];
+
   installPhase = ''
     mkdir -p $out/bin
     cp .local/bin/displayselect $out/bin
     chmod +x $out/bin/displayselect
   '';
+
+  meta.mainProgram = "displayselect";
 }
diff --git a/nix/pkgs/displayselect/scaling.patch b/nix/pkgs/displayselect/scaling.patch
new file mode 100644
index 00000000..914a19a4
--- /dev/null
+++ b/nix/pkgs/displayselect/scaling.patch
@@ -0,0 +1,26 @@
+diff --git a/.local/bin/displayselect b/.local/bin/displayselect
+index 51dd468..c132b7a 100755
+--- a/.local/bin/displayselect
++++ b/.local/bin/displayselect
+@@ -15,20 +15,13 @@ twoscreen() { # If multi-monitor is selected and there are two screens.
+ 
+         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"
++            --scale-from "${res_ext_x}x${res_ext_y}"
+     else
+ 
+         primary=$(echo "$screens" | dmenu -i -p "Select primary display:")