aboutsummaryrefslogtreecommitdiffstats
path: root/bin/setup_screens
blob: 1a84bba67d3916605efbd2ca31491863113275a6 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env bash
# Script to setup screens on login and lid-switch-action
# Uses autorandr to determine which screens are connected and which to setup.
#
# Per default a configuration named "internal" will be used, if the computer is
# a laptop and not docked.
# A configuration named "docked-closed" will be used during login, when the lid
# is closed and docked.
# A configuration named "docked-open" will be used during login, when the lid
# is open and docked.

set -e -u

lid_state=""
docking_state=""
fingerprint=""
action=""
x_user=""
current_user=""
path=""

# get current user running X
get_x_user()
{
  x_user="$(loginctl list-sessions --no-legend| grep "seat"| awk '{print $3}')"
}

# get current user running this script
get_current_user()
{
  current_user="$(whoami)"
}

# get full path to this script
get_path()
{
  path="$0"
}

# Get current lid state ("open" or "closed")
function get_lid_state()
{
  if [ -r "/proc/acpi/button/lid/LID/state" ]; then
    lid_state="$(cat /proc/acpi/button/lid/LID/state|awk '{print $2}')"
  fi
}

# Get current docking state ("true" or "false")
function get_docking_state()
{
  if [ -x "/usr/bin/busctl" ]; then
    docking_state="$(busctl introspect org.freedesktop.login1 /org/freedesktop/login1|grep "\.Docked"|awk '{print $4}')"
  fi
}

# Get the md5sum of the current autorandr fingerprint
function get_setup_fingerprint()
{
  fingerprint="$(autorandr --fingerprint| md5sum| cut -d ' ' -f 1)"
}

# Get the md5sum of a autorandr configuration
function get_configuration_fingerprint()
{
  local fingerprint=""
  if [ -r $HOME/.config/autorandr/$1/setup ]; then
    fingerprint="$(md5sum $HOME/.config/autorandr/$1/setup| cut -d ' ' -f 1)"
  fi
  echo "$fingerprint"
}

function set_configuration()
{
  local state=0
  # if the computer is docked
  if [ "$docking_state" = "true" ]; then
    # if there's a lid-switch action
    if [ -n "$action" ]; then
      case "$action" in 
        "open")
          if [ $(get_configuration_fingerprint "docked-open")  == "$fingerprint" ]; then
            echo "Loading docked-open."
            state=1
            autorandr -l docked-open
          fi
          ;;
        "close")
          if [ $(get_configuration_fingerprint "docked-closed") == "$fingerprint" ]; then
            echo "Loading docked-closed."
            state=1
            autorandr -l docked-closed
          fi
          ;;
      esac
    else
      # check the lid state
      case "$lid_state" in
        "open")
          if [[ $(get_configuration_fingerprint "docked-open") == "$fingerprint" ]]; then
            echo "Loading docked-open."
            state=1
            autorandr -l docked-open
          fi
          ;;
        "closed")
          if [[ $(get_configuration_fingerprint "docked-closed") == "$fingerprint" ]]; then
            echo "Loading docked-closed."
            state=1
            autorandr -l docked-closed
          fi
          ;;
          *)
            echo "Your computer is docked but has no lid?!"
            exit 1
          ;;
      esac
    fi
  # else if not docked
  else
    if [ "$action" = "open" ]; then
      if [[ $(get_configuration_fingerprint "external") == "$fingerprint" ]]; then
        echo "Loading external."
        state=1
        autorandr -l external
      fi
    fi
  fi
  # if the screen still has not been setup, try using internal
  if [ $state -ne 1 ]; then
    if [[ $(get_configuration_fingerprint "internal") == "$fingerprint" ]]; then
      echo "Loading internal."
      autorandr -l internal
    fi
  fi
}

# check if autorandr is installed
if [ ! -x "/usr/bin/autorandr" ]; then
  echo "Autorandr is not installed."
  exit 1
fi

# get (lid-switch) action from parameter ("open" or "close")
if [ "$#" -eq 1 ]; then
  action="$1"
fi

logger "Calling 'setup_screens'"
get_x_user
get_current_user
get_path

# Export Xorg DISPLAY and XAUTHORITY
export DISPLAY=$(ls /tmp/.X*|grep "lock"|cut -d '.' -f2| cut -d '-' -f1|sed -e 's/X/:/')
export XAUTHORITY="$(eval echo ~$x_user/.Xauthority)"

# if the script caller is the current X user or root (and lightdm is the current X user)
if [ "$current_user" = "$x_user" ] || [ $current_user = "root" -a $x_user = "lightdm" ]; then
  get_lid_state
  get_docking_state
  get_setup_fingerprint
  set_configuration
else
  if [ $current_user = "root" ]; then
    logger "Running $path as user $x_user now."
    runuser -l $x_user -c $path
  else
    echo "$current_user is not currently running X and is not allowed to let the current X user run this script."
    exit 1
  fi
fi

exit 0