2015-10-06

Hotplug monitor setup using udev and xrandr

A small post about my configuration to configure my screens on monitor plug event

The process is quite simple :

  1. Add a udev rule to call a script when a monitor is plugged in.
  2. Design an appropriate script that setup connected monitor using xrandr.

Here is the udev rule, to be put in a file named (for example)
"95-monitor-hotplug.rules" and placed in the /etc/udev/rultes.d/ directory
 
KERNEL=="card0", SUBSYSTEM=="drm", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/\/.Xauthority", RUN+="/home/\/install/bin/updateMonitors.zsh"
(This should be on one line in the file)


The zsh script to configure monitors :


#!/bin/zsh
monitors=("DP2" "DP1-1" "DP1-2" "DP1-3" "HDMI1" "HDMI2" "VIRTUAL1")
# Associate various ports with their desired positions
typeset -A positions
# DP1-? are dvi display ports
positions[DP1-1]=--left-of
positions[DP1-2]=--left-of
positions[DP1-3]=--left-of

# DP2 is the VGA port
positions[DP2]=--right-of

positions[HDMI1]=--right-of
positions[HDMI2]=--right-of
positions[VIRTUAL1]=--right-of
integrated=eDP1
# Get monitors informations from xrandr output
xrandrOutput=`xrandr -q`

for m in $monitors
do
   # echo "Considering monitor : [$m]"
   # echo "associated position : [$positions[$m]]"

   # Manage deconnexions
   if [[ "$xrandrOutput" =~ "$m disconnected" ]]; then
      xrandr --output $m --off 
      # echo "xrandr --output $m --off"
   fi

   # Manage connexions
   if [[ "$xrandrOutput" =~ "$m connected" ]]; then
      xrandr --output $m --auto --noprimary $positions[$m] $integrated
      # echo "xrandr --output $m --auto --noprimary $positions[$m] $integrated"
   fi
done

2015-06-05

Auto shutdown on low battery

My window manager, Awesome, is not a desktop manager so it does not come with all the bells and whistles (and therefore start in a split of a second) but it also lacks some interesting features.
In order to prevent my laptop to die abruptly when battery is critically low and corrupting my main filesystem, I put up a tiny little simple solution: a script, run by cron, that checks if battery is low and cleanly shutdown the system when it is critically low.

So, first, the script, largely inspired by this answer.

#!/bin/sh
# touch $HOME/.dbus/Xdbus
# chmod 600 $HOME/.dbus/Xdbus
# env | grep DBUS_SESSION_BUS_ADDRESS > $HOME/.dbus/Xdbus
# echo 'export DBUS_SESSION_BUS_ADDRESS' >> $HOME/.dbus/Xdbus
# exit 0
#
if [ -r ~/.dbus/Xdbus ]; then
  . ~/.dbus/Xdbus
fi


low_threshold=6
critical_threshold=4
timeout=50
shutdown_cmd='sudo /sbin/shutdown -h now'
 

level=$(cat /sys/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:08/PNP0C09:00/PNP0C0A:00/power_supply/BAT0/capacity)
state=$(cat /sys/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:08/PNP0C09:00/PNP0C0A:00/power_supply/BAT0/status)


if [ x"$state" != x'Discharging' ]; then
  exit 0
fi


do_shutdown() {
  sleep $timeout && kill $zenity_pid 2>/dev/null
  if [ x"$state" != x'Discharging' ]; then
    exit 0
  else
    $shutdown_cmd
  fi
}

if [ "$level" -gt $critical_threshold ] && [ "$level" -lt $low_threshold ]; then
  notify-send -u critical -t 6000 "Battery level is low: $level%"
fi

if [ "$level" -lt $critical_threshold ]; then
  notify-send -u critical -t 20000 "Battery level is low: $level%" \
    "The system is going to shut down in $timeout seconds."
  DISPLAY=:0 zenity --question --ok-label 'OK' --cancel-label 'Cancel' \
    --text "Battery level is low: $level%.\n\n The system is going to shut down in $timeout seconds." &
  zenity_pid=$!

  do_shutdown &
  shutdown_pid=$!

  trap 'kill $shutdown_pid' 1

  if ! wait $zenity_pid; then
    kill $shutdown_pid 2>/dev/null
  fi
fi


You have to adapt to the place where the battery state is on your system.
You may have to check that the battery state is really "Discharging".

You also have to add a cron job to run this script on a sufficiently regular basis. To do so :
$ crontab -e
 and add this line :
*/1 * * * * /home/me/usr/bin/auto-shutdown

This will cause the script to be run every second.

For the script to work, you'll need:
  1. To be able to shutdown your system from the command line using sudo.
  2. Have zenity and notify-send installed.
For the first point, you can add a file in /etc/sudoers.d/shutdown containing the line:
username ALL=(ALL) NOPASSWD: /sbin/shutdown

In debian, notify-send is in the libnotify-bin package.

Hope it helps.

2015-05-09

Garmin Forerunner 610 under Linux


Nothing to add to this site.

Just for quick retrieval : antfs-cli.

:wq