-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.sh
More file actions
executable file
·458 lines (374 loc) · 12.7 KB
/
handler.sh
File metadata and controls
executable file
·458 lines (374 loc) · 12.7 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#!/bin/sh
# Default acpi script that takes an entry for all actions
# ----------------------------------------------------------------------
# Globals
# ----------------------------------------------------------------------
#btstatus=block
#btstatus=
SAFESLEEP=0 # slightly slower if on; restarts network
# ----------------------------------------------------------------------
# Set options
# ----------------------------------------------------------------------
set $*
# ----------------------------------------------------------------------
# Get current X user and environment
# ----------------------------------------------------------------------
PID=`pgrep startx || pgrep xinit`
if [[ -n $PID ]];
then
# found startx
USER=`ps -o user --no-headers $PID `
USERHOME=`getent passwd $USER | cut -d: -f6`
export XAUTHORITY="$USERHOME/.Xauthority"
for x in /tmp/.X11-unix/*; do
displaynum=`echo $x | sed s#/tmp/.X11-unix/X##`
if [ x"$XAUTHORITY" != x"" ]; then
export DISPLAY=":$displaynum"
fi
done
else
# TODO: detect correct console user
USER=root
fi
# ----------------------------------------------------------------------
# Main ACPI event handler
# ----------------------------------------------------------------------
ACPI_Event ()
{
local _options="$*"
logger "ACPI EVENT RECEIVED: $_options";
case $1 in
power) shift; case $1 in
button) #_Poweroff;
;;
lid) shift; case $1 in
open) ;;
close) ;;
esac ;;
hotkey) shift; case $1 in
battery) _Powersave; ;;
sleep) #_Sleep
;;
hibernate) #_Sleep;
;;
esac ;;
ac) shift; case $1 in
connected) _Powersave max ;;
disconnected) _Powersave min; ;;
esac ;;
battery) shift; case $1 in
connected) ;;
disconnected) ;;
esac ;;
esac ;;
security) shift; case $1 in
lock) _Lock_System ;;
esac ;;
display) shift; case $1 in
video) shift; case $1 in
on) ;;
off) ;;
esac ;;
hotkey) shift; case $1 in
switchmode) ;;
brightness) shift; case $1 in
up) ;;
down) ;;
esac ;;
esac ;;
lightoff) _Light_Off ;;
esac ;;
tablet) shift; case $1 in
display) shift; case $1 in
tabletmode) ;;
laptopmode) ;;
esac ;;
stylus) shift; case $1 in
eject) _Annotate start ;;
dock) _Annotate finish ;;
esac ;;
esac ;;
wireless) shift; case $1 in
switch) _Switch_Radios ;;
hotkey) shift; case $1 in
wifi) _Toggle_Wifi ;;
bluetooth) _Toggle_Bluetooth ;;
esac ;;
esac ;;
volume) shift; case $1 in
input) shift; case $1 in
mute) ;;
esac ;;
output) shift; case $1 in
up) amixer set Master 5%+ unmute -q ;;
down) amixer set Master 5%- unmute -q ;;
mute) amixer set Master toggle -q ;;
esac ;;
esac ;;
*) logger "ACPI KNOWN BUT UNHANDLED EVENT: $_options"
esac
}
# ----------------------------------------------------------------------
# Functions
# ----------------------------------------------------------------------
Log () { logger "ACPI EXECUTION: $* (${FUNCNAME[1]})"; }
Is_True ()
{
shopt -s nocasematch;
case $1 in
1|yes|true) return 0 ;;
*) return 1 ;;
esac;
shopt -u nocasematch;
}
Map_Event_Codes ()
{
# I don't parse out the various ACPI event strings since they seem to change not infrequently between kernel releases.
# They instead are line items and I use them to in turn call an easier to manage function (ACPI_Event)
case "$*" in
# POWER
xyz) _Light_Off ;;
button/power*) ACPI_Event power button ;;
button/battery*) ACPI_Event power hotkey battery ;;
button/sleep*) ACPI_Event power hotkey sleep ;;
button/suspend*) ACPI_Event power hotkey hibernate ;;
ac_adapter*0) ACPI_Event power ac disconnected ;;
ac_adapter*1) ACPI_Event power ac connected ;;
ibm/hotkey\ LEN0068:00*4010) ACPI_Event power battery connected aux ;;
ibm/hotkey\ LEN0068:00*4011) ACPI_Event power battery disconnected aux ;;
battery*:00*0) ACPI_Event power battery disconnected main ;;
battery*:00*1) ACPI_Event power battery connected main ;;
button/lid\ LID\ close) ACPI_Event power lid close ;;
button/lid\ LID\ open) ACPI_Event power lid open ;;
# SECURITY
button/screenlock*) ACPI_Event security lock ;;
# WIRELESS
MISSING) ACPI_Event wireless switch ;;
button/wlan*) ACPI_Event wireless hotkey wifi ;;
button/f24*) ACPI_Event wireless hotkey bluetooth ;; # f9
# AUDIO
button/volumedown*) ACPI_Event volume output down ;;
button/volumeup*) ACPI_Event volume output up ;;
button/mute*) ACPI_Event volume output mute ;;
MISSING) ACPI_Event volume input mute ;; # button: microphone mute (led is)
# VIDEO/DISPLAY
video/switchmode*) ACPI_Event display hotkey switchmode ;;
video/brightnessup*) ACPI_Event display hotkey brightness up ;; # two events per key stroke?
video/brightnessdown*) ACPI_Event display hotkey brightness down ;; # two events per key stroke?
MISSING) ACPI_Event display on ;; # event: display on
MISSING) ACPI_Event display off ;; # event: display off (to be confirmed)
# TABLET MODE
MISSING) ACPI_Event tablet display tabletmode ;; # screen moved to tablet mode
MISSING) ACPI_Event tablet display laptopmode ;; # screen moved back to laptop mode
ibm/hotkey\ LEN0068:00*500c) ACPI_Event tablet stylus eject ;;
ibm/hotkey\ LEN0068:00*500b) ACPI_Event tablet stylus dock ;;
# MISC BUTTONS
button/prog1*) ACPI_Event misc button thinkvantage ;;
# MISC HOTKEYS
button/fnf1*) ACPI_Event display lightoff ;; # fn-f1
MISSING) ACPI_Event misc hotkey f6 ;; # media switchmode
MISSING) ACPI_Event misc hotkey f7 ;; # input switchmode
button/fnf11*) ACPI_Event misc hotkey f11 ;;
button/zoom*) ACPI_Event misc hotkey zoom ;;
# MEDIA HOTKEYS
cd/stop\ CDSTOP*) ACPI_Event media stop ;;
cd/play\ CDPLAY*) ACPI_Event media play ;;
cd/prev\ CDPREV*) ACPI_Event media prev ;;
cd/next\ CDNEXT*) ACPI_Event media next ;;
*) Log "UNKNOWN EVENT: $*" ;;
esac
}
_Lock_System () {
sleep 1; xset dpms force off;
physlock -ds -u $USER;
logger lock system;
}
_Poweroff ()
{
_Powersave max
poweroff
}
_Sleep ()
{
Log "SUSPENDING"; Is_True $SAFESLEEP && local sm=true || local sm=;
ip link show wlan0 | grep -q UP && wlanstate=up || wlanstate=down # check current state of network
netcfg -a
ip link set wlan0 down
ip link set eth0 down
_Lock_System
# echo -n mem >/sys/power/state
systemctl suspend
ip link set wlan0 $wlanstate
rc.d restart net-auto-wireless
rc.d restart net-auto-wired
[[ $(cat /sys/class/power_supply/AC/online) > 0 ]] && _Powersave max || _Powersave min # power state?
Log "RESUMED";
}
_Sleep_for_wpa_auto ()
{
Log "SUSPENDING"; Is_True $SAFESLEEP && local sm=true || local sm=;
ip link show wlan0 | grep -q UP && wlanstate=up || wlanstate=down # check current state of network
[[ -n $sm || $wlanstate == down ]] && (modprobe -r iwlagn && Log "wlan down or safe sleep mode on: unloading network module")
echo -n mem >/sys/power/state # kernel sleep
modprobe iwlagn && ip link set wlan0 $wlanstate # always reload module; network stutters briefly otherwise
[[ -n $sm && $wlanstate == up ]] && rc.d restart wpa_auto && Log "safe sleep mode; restarting wpa_auto" # slower, only in safe mode
[[ $(cat /sys/class/power_supply/AC/online) > 0 ]] && _Powersave max || _Powersave min # power state?
Log "RESUMED";
}
_Toggle_Bluetooth ()
{
if rfkill list bluetooth | grep -iq "soft blocked: yes"
then
Log "BLUETOOTH TOGGLE ON";
local btstatus=unblock
else
Log "BLUETOOTH TOGGLE OFF";
local btstatus=block
fi
echo -n $btstatus > /var/tmp/bt
eval "rfkill $btstatus bluetooth"
}
_Toggle_Wifi ()
{
if rfkill list wifi | grep -iq "soft blocked: yes"
then
Log "WIFI OFF"
#netcfg all-suspend
netcfg -a
ip link set wlan0 down
#modprobe -r iwlagn
else
Log "WIFI ON"
#ip link set wlan0 up && rc.d restart wpa_auto && Log "WIFI ON"
#netcfg all-resume
#modprobe iwlagn
ip link set wlan0 up
rc.d restart net-auto-wireless
:
fi
}
_Switch_Radios ()
{
if rfkill list wifi | grep -iq "hard blocked: yes";
then
Log "RADIOS HARD SWITCHED OFF";
else
Log "RADIOS HARD SWITCHED ON";
sleep .5;
eval "rfkill $(cat /var/tmp/bt) bluetooth";
ip link set wlan0 up; #rc.d restart wpa_auto;
fi;
}
#action_cycle_powersave () { :; }
_Powersave ()
{
# min max mov
MIN_BACKLIGHT_PERCENT=75
MAX_BACKLIGHT_PERCENT=100
local powersave_state_file=/var/tmp/powersave
[[ -e $powersave_state_file ]] && local current_powersave_state=$(cat $powersave_state_file) || current_powersave_state=max
# change to new state
case $current_powersave_state in
min) new_powersave_state=max ;;
#max) new_powersave_state=mov ;;
*) new_powersave_state=min ;;
esac
# use new state value or override with argument
powersave_state=${1:-$new_powersave_state}
# change powersave states
case $powersave_state in
min)
target_cpu_governor=powersave
target_brute_force=auto
target_wlan_power_save=on
dpms_seconds=60
backlight_percent=75
echo 255 > /sys/class/leds/tpacpi\:\:thinklight/brightness; sleep .05; echo 0 > /sys/class/leds/tpacpi\:\:thinklight/brightness;
Log "POWER MODE: MIN (full powersave)"
;;
max)
#target_cpu_governor=ondemand
target_cpu_governor=performance
target_brute_force=on
target_wlan_power_save=off
dpms_seconds=600
backlight_percent=100
echo 255 > /sys/class/leds/tpacpi\:\:thinklight/brightness; sleep .05; echo 0 > /sys/class/leds/tpacpi\:\:thinklight/brightness;
sleep .1;
echo 255 > /sys/class/leds/tpacpi\:\:thinklight/brightness; sleep .05; echo 0 > /sys/class/leds/tpacpi\:\:thinklight/brightness;
Log "POWER MODE: MAX (no powersave)"
;;
esac
# CPU
#for cpupath in /sys/devices/systems/cpu/cpu?; do echo -n $target_cpu_governor >"$cpupath/cpufreq/scaling_governor"; done
cpupower -c all frequency-set -g $target_cpu_governor; \
Log "POWERSAVE-$powersave_state cpu $cpu set to $target_cpu_governor"
# BRUTE FORCE SYS TREE
for _control in /sys/bus/{pci,spi,i2c}/devices/*/power/control; do echo $target_brute_force > $_control && Log "POWERSAVE SET: $_control"|| true; done
for _control in /sys/bus/usb/devices/*/power/control; do echo $target_brute_force > $_control && Log "POWERSAVE SET: $_control"|| true; done
# NETWORK
iw wlan0 set power_save $target_wlan_power_save && Log "POWERSAVE-$powersave_state wifi adapter powersave $target_wlan_power_save"
# DISPLAY
# graphics powersave taken care of in brute force above
# DPMS
xset dpms 0 0 $dpms_seconds 2>/dev/null && Log "POWERSAVE-$powersave_state DPMS timeout set to $dpms_seconds"
# read current/max brightness values
sys_current_brightness=$(cat /sys/class/backlight/acpi_video0/actual_brightness)
sys_full_brightness=$(cat /sys/class/backlight/acpi_video0/max_brightness)
# set target
target_brightness=$((($backlight_percent*$sys_full_brightness)/100))
if [[ $sys_current_brightness > $target_brightness ]]
then
# reduce
Log "REDUCE BACKLIGHT from $sys_current_brightness to $target_brightness"
for (( b=$sys_current_brightness; b>=$target_brightness; b--)); do echo -n $b > /sys/class/backlight/acpi_video0/brightness; sleep 0.015; done
else
# increase
Log "INCREASE BACKLIGHT from $sys_current_brightness to $target_brightness"
for (( b=$sys_current_brightness; b<=$target_brightness; b++)); do echo -n $b > /sys/class/backlight/acpi_video0/brightness; sleep 0.015; done
fi
# save state
echo -n $powersave_state > $powersave_state_file
}
_Cycle_Display () { :; }
_Cycle_Input () { :; }
_Light_Off ()
{
# return;
Log;
# killall calise
# calise --no-gui --verbosity=0 --gap 0.1 --cam=/dev/video0 --steps=15 --delta=0.50 --offset=0.25 --bl-offset=6 --invert --profile=/etc/calise.conf &
# (sleep 3 && killall calise) &
# xset dpms force off;
sleep 1; xset dpms force off
}
_Annotate ()
{
_capture_dir="/home/$USER/tmp/screenshots"
_scrot_filename="$(hostname)_%Y-%m-%d-%T_\$wx\$h.png"
_scrot_cmd="xdotool key "Ctrl+F9" && \
chmod 644 \$f && \
chown $USER:users \$f && \
mv \$f /home/$USER/tmp/screenshots/ && \
su -c \"google picasa post 'Screenshots' /home/$USER/tmp/screenshots/\$f\" es"
case $1 in
start)
xdotool key "Shift+F9" "F9";
;;
finish)
[[ ! -d "$_capture_dir" ]] && mkdir -p "$_capture_dir" && chmod 755 $_capture_dir && chown $USER:users $_capture_dir
scrot "$_scrot_filename" -e "$_scrot_cmd"
;;
esac
}
# TODO: this isn't working...
_Mic () { /usr/bin/amixer sset Capture toggle; Log "AUDIO: MIC MUTE"; }
_Boot_Events ()
{
[[ $(cat /sys/class/power_supply/AC/online) > 0 ]] && _Powersave max || _Powersave min # power state?
[[ -e "/var/tmp/bt" ]] && eval "rfkill $(cat /var/tmp/bt) bluetooth"
exit
}
# START EXECUTION
[[ "$*" == "boot" ]] && _Boot_Events
Map_Event_Codes "$*"