summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2024-05-08 21:47:43 +0200
committertv <tv@krebsco.de>2024-05-09 00:32:06 +0200
commitfe6f05c9cd87e5888dcf4c9403e400e4ad453f11 (patch)
treec1364f406222e35560bfc24f9cbb4a401f8cea31
parent7a2e587171482f39353efd80a159dce162a67eb1 (diff)
xoutinfo: init
-rw-r--r--pkgs/simple/xoutinfo/default.nix19
-rwxr-xr-xpkgs/simple/xoutinfo/xoutinfo.sh90
2 files changed, 109 insertions, 0 deletions
diff --git a/pkgs/simple/xoutinfo/default.nix b/pkgs/simple/xoutinfo/default.nix
new file mode 100644
index 0000000..92dbb42
--- /dev/null
+++ b/pkgs/simple/xoutinfo/default.nix
@@ -0,0 +1,19 @@
+{ lib, pkgs }:
+
+pkgs.runCommand "xoutinfo-1.0.0" {} /* sh */ ''
+ mkdir -p $out/bin
+
+ cp ${./xoutinfo.sh} $out/bin/xoutinfo
+ sed -i '
+ s|#! /bin/sh|#! ${pkgs.dash}/bin/dash|
+ s|^export AWKPATH=.*|export AWKPATH=${lib.makeSearchPath "/lib/awk" [
+ pkgs.awklib
+ ]}|
+ s|#PATH=|PATH=${lib.makeBinPath [
+ pkgs.coreutils
+ pkgs.gawk
+ pkgs.xdotool
+ pkgs.xorg.xrandr
+ ]}|
+ ' $out/bin/xoutinfo
+''
diff --git a/pkgs/simple/xoutinfo/xoutinfo.sh b/pkgs/simple/xoutinfo/xoutinfo.sh
new file mode 100755
index 0000000..3353cbe
--- /dev/null
+++ b/pkgs/simple/xoutinfo/xoutinfo.sh
@@ -0,0 +1,90 @@
+#! /bin/sh
+# usage: xoutinfo
+
+set -efu
+
+#PATH=
+export AWKPATH=$HOME/sync/src/hrm/pkgs/simple/awklib/src/lib/awk
+
+{
+ echo derp
+ xrandr --query --nograb
+ echo Screen 7: derp
+ echo bah
+} |
+awk \
+ $(
+ # assign X, Y, SCREEN, and WINDOW variables
+ xdotool getmouselocation --shell --prefix '-v ' | tr '\n' ' '
+ ) \
+'
+ @include "json"
+
+ function dprint(x) {
+ #print(x) > "/dev/stderr"
+ }
+
+ BEGIN {
+ default_dpi = 96
+ skip = mkbool(1)
+ }
+ $1 == "Screen" { skip=mkbool($2!=SCREEN":") }
+
+ { dprint( (skip?"skip:\033[31m":"read:\033[32m") $0 "\033[m") }
+
+ skip { next }
+
+ $2 == "connected" {
+ # Example line:
+ # HDMI-A-0 connected 3840x2160+0+0 (normal left inverted right x axis y axis) 597mm x 336mm
+
+ sub(/ [(][^)]+[)]/, "")
+
+ # Get physical width and height [in mm]
+ split(gensub(/^.* ([0-9]+)mm x ([0-9]+)mm$/, "\\1 \\2", 1), physical_geometry)
+ if (/\<left\>/ || /\<right\>/) {
+ pw=strtonum(physical_geometry[2])
+ ph=strtonum(physical_geometry[1])
+ } else {
+ pw=strtonum(physical_geometry[1])
+ ph=strtonum(physical_geometry[2])
+ }
+
+ name=$1
+ split(gensub(/^.* ([0-9]+x[0-9]+\+[0-9]+\+[0-9]+) .*$/, "\\1", 1), geometry, /[x+]/)
+ w=strtonum(geometry[1])
+ h=strtonum(geometry[2])
+ x=strtonum(geometry[3])
+ y=strtonum(geometry[4])
+
+ dpi_x = w / pw * 25.4
+ dpi_y = h / ph * 25.4
+
+ # This assumes DPI has not been configured
+ # (e.g. by passing -dpi to X or calling xrandr --dpi)
+ device_scale_factor = dpi_x / default_dpi
+
+ is_current_display=(x <= X && X < x + w && y <= Y && Y < y + h)
+
+ if (is_current_display) {
+ output["name"] = name
+ output["width"] = w
+ output["height"] = h
+ output["x"] = x
+ output["y"] = y
+ output["width_mm"] = pw
+ output["height_mm"] = ph
+ output["dpi_x"] = dpi_x
+ output["dpi_y"] = dpi_y
+ output["device_scale_factor"] = device_scale_factor
+ print toJSON(output)
+ delete output
+ }
+
+ dprint(name " " w "x" h "+" x "+" y " is_current_display:" is_current_display)
+ dprint("physical_geometry:" pw "mm" "x" ph "mm" ", dpi:" dpi_x "," dpi_y ", 0:" $0 \
+ ", device_scale_factor:" device_scale_factor)
+ dprint("X:" X)
+ dprint("Y:" Y)
+ }
+'