Moving X scripts to dedicated dir
authorStefan Huber <shuber@sthu.org>
Wed, 5 Feb 2014 08:45:07 +0000 (09:45 +0100)
committerStefan Huber <shuber@sthu.org>
Wed, 5 Feb 2014 08:45:07 +0000 (09:45 +0100)
X/getXenv.inc [new file with mode: 0644]
X/tp_alert.sh [new file with mode: 0755]
lenovo-x220-linuxacpi/usr/local/bin/getXenv.inc [deleted symlink]
lenovo-x220-linuxacpi/usr/local/bin/tp_alert.sh [deleted symlink]

diff --git a/X/getXenv.inc b/X/getXenv.inc
new file mode 100644 (file)
index 0000000..4b1c7e3
--- /dev/null
@@ -0,0 +1,44 @@
+# Extracted from /etc/acpi/powerbtn.sh
+
+
+# Taken from Debian's 2.0.4-1 diff file.  This version handles KDE4.
+# Power Button event handler.
+# Checks to see if gnome or KDE are already handling the power button.
+# If not, initiates a plain shutdown.
+
+# getXuser gets the X user belonging to the display in $displaynum.
+# If you want the foreground X user, use getXconsole!
+# Input:
+#   displaynum - X display number
+# Output: 
+#   XUSER - the name of the user
+#   XAUTHORITY - full pathname of the user's .Xauthority file
+getXuser() {
+        user=`pinky -fw | awk '{ if ($2 == ":'$displaynum'" || $(NF) == ":'$displaynum'" ) { print $1; exit; } }'`
+        if [ x"$user" = x"" ]; then
+                startx=`pgrep -n startx`
+                if [ x"$startx" != x"" ]; then
+                        user=`ps -o user --no-headers $startx`
+                fi
+        fi
+        if [ x"$user" != x"" ]; then
+                userhome=`getent passwd $user | cut -d: -f6`
+                export XAUTHORITY=$userhome/.Xauthority
+        else
+                export XAUTHORITY=""
+        fi
+        export XUSER=$user
+}
+
+# Gets the X display number for the active virtual terminal.
+# Output:
+#   DISPLAY - the X display number
+#   See getXuser()'s output.
+getXconsole() {
+        console=`fgconsole`;
+        displaynum=`ps t tty$console | sed -n -re 's,.*/X .*:([0-9]+).*,\1,p'`
+        if [ x"$displaynum" != x"" ]; then
+                export DISPLAY=":$displaynum"
+                getXuser
+        fi
+}
diff --git a/X/tp_alert.sh b/X/tp_alert.sh
new file mode 100755 (executable)
index 0000000..43109e0
--- /dev/null
@@ -0,0 +1,126 @@
+#!/bin/bash
+#
+# Stefan Huber <shuber2@gmail.com>
+#
+# ChangeLog:
+#   * 2009-07-24: Initial version
+#   * 2010-10-09: Use more shell commands
+
+
+# Wait this amount of time until we activate lock
+INITWAIT="0"
+# Wait this amount of time after an alert call
+ALERTPOSTWAIT="2"
+# Busy wait for polling position sensor
+BUSYWAIT="0.2"
+# Tolerance for sensor differences
+TOLERANCE="5"
+# Position file
+POSFILE="/sys/devices/platform/hdaps/position"
+
+
+#Set the paths
+export PATH="/bin:/usr/bin"
+export DISPLAY=":0"
+
+state=0
+alert() {
+       amixer -q sset Master 100%,100% unmute
+       echo "Alert $state at time `date`"
+
+       case $state in
+               0)
+                       echo "Leave me alone!" | festival --tts
+                       state=1
+                       ;;
+               1)
+                       echo "E-mail sent!" | festival --tts
+                       sendemail
+                       state=2
+                       ;;
+               *)
+                       sendemail
+                       while isActive; do
+                               echo "HELP!" | festival --tts
+                       done
+                       return
+                       ;;
+       esac
+       sleep $ALERTPOSTWAIT
+}
+
+sendemail() {
+       /usr/sbin/sendmail $USER <<EOF
+Subject: tp_alert.sh alarm on `hostname`
+From: shuber@`hostname`
+To: shuber@`hostname`
+
+This is an automatic mail from the tp_alert.sh script:
+  state=$state
+  time=`date`
+EOF
+}
+
+isActive() {
+       if "`qdbus org.freedesktop.ScreenSaver /ScreenSaver GetActive`" \
+               = "true"; then
+               return 0
+       fi
+       return 1
+}
+
+getPosition() {
+       pos=`cat $POSFILE`
+       xtilt=${pos%,*}
+       xtilt=${xtilt#(}
+       ytilt=${pos#*,}
+       ytilt=${ytilt%)}
+}
+
+
+
+
+if [ ! -e $POSFILE ]; then
+       echo "HDAPS position file not existing!" 
+       return -1
+fi
+
+
+echo "Launched at time `date`" 
+#Initial wait
+sleep $INITWAIT
+
+
+
+#Wait for trigger to activate alertion system
+while ! isActive; do
+       sleep $BUSYWAIT
+done
+
+
+echo "Activated." 
+echo "Activated." | festival --tts
+
+
+#Get initial positions
+getPosition
+initxtilt=$xtilt
+initytilt=$ytilt
+
+#Now wait until alertion system is deactivated
+while isActive; do
+       getPosition
+       diffx=`echo "$xtilt-$initxtilt" | bc`
+       diffy=`echo "$ytilt-$initytilt" | bc`
+       diffx=${diffx#-}
+       diffy=${diffy#-}
+
+       if [[ $diffx -ge $TOLERANCE || $diffy -ge $TOLERANCE ]]; then
+               alert
+       fi
+       sleep $BUSYWAIT
+done
+
+echo "Unlocked at time `date`." 
+echo "Unlocked with state $state." | festival --tts
+
diff --git a/lenovo-x220-linuxacpi/usr/local/bin/getXenv.inc b/lenovo-x220-linuxacpi/usr/local/bin/getXenv.inc
deleted file mode 120000 (symlink)
index 266a687..0000000
+++ /dev/null
@@ -1 +0,0 @@
-/root/bin/getXenv.inc
\ No newline at end of file
diff --git a/lenovo-x220-linuxacpi/usr/local/bin/tp_alert.sh b/lenovo-x220-linuxacpi/usr/local/bin/tp_alert.sh
deleted file mode 120000 (symlink)
index c2990da..0000000
+++ /dev/null
@@ -1 +0,0 @@
-/root/bin/tp_alert.sh
\ No newline at end of file