summaryrefslogtreecommitdiffstats
path: root/pkgs/simple/xoutinfo/xoutinfo.sh
blob: 3353cbec8cb515b5273f4d0bb8f238a0195a426e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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)
  }
'