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

2014-11-18

Compter les mots dans un document LaTeX - Count words in a LaTeX document


An excellent tool to count words in a LaTeX document : TeXcount.

Un outils excellent pour compter le nombre de mots dans un document LaTeX : TeXcount.


2014-03-12

La gestion des dispositions clavier

Étant un inconditionnel de Awesome comme gestionnaire de fenêtre, je n'ai pas les jolis outils gnome ou kde pour gérer le basculement entre dispositions clavier.
Pour palier ce manque j'utilise une combinaison de xxkb et setxkbmap.

Pour avoir une liste de dispositions disponibles j'ai ajouté ça dans mon .xsession :
setxkbmap -option "" -option "grp:rctrl_toggle,grp_led:scroll" -layout "ca,fr(bepo),fr"
(Au passage, cette commande permet d'alterner entre les deux dispositions les plus récentes avec la touche Ctrl de droite)

Pour démarrer xxkb, j'ajoute ça dans mon fichier de configuration de Awesome (rc.lua) :
awful.util.spawn_with_shell("sleep 3 && killall xxkb ; xxkb &")

ma config pour xxkb (~/.xxkbrc) :
XXkb.mainwindow.enable: yes
XXkb.mainwindow.appicon: no
XXkb.mainwindow.type: tray
XXkb.mainwindow.in_tray: yes
XXkb.mainwindow.geometry: 16x16+0+0
XXkb.mainwindow.label.enable: yes
XXkb.mainwindow.label.text.1: Qc
XXkb.mainwindow.label.text.2: Fr
XXkb.mainwindow.label.text.3: Be
XXkb.mainwindow.label.background: black
XXkb.mainwindow.label.foreground: #569892
XXkb.mainwindow.label.font: -misc-*-r-*-15-*
XXkb.button.enable: no
XXkb.controls.add_when_start: no
XXkb.controls.add_when_create: no
XXkb.controls.add_when_change: no

Et voilou.
Il faut bien faire correspondre l'ordre des layouts déclarés dans la commande setxkbmap et dans le fichier de config de xxkb, après c'est tout bon. Juste à cliquer sur l'icone de xxkb dans le système tray et hop la disposition change.

2013-06-27

Tiny convenient bash functions to find files


Look for file by name, case insensitive, containing the given string:

function mfind
{
   find . -iname "*$1*"
}

Grep second parameter in files with names including first parameter. Could be replaced by a grep with --include option.

function mgrep
{
   find . -iname "*$1*" -exec grep -in $2 {} \+
}

2008-11-25

latex beamer

latex beamer class's command \inserttotalframenumber includes appendix frames and
the \insertpresentationendpage command includes all slides (not convenient when using overlays).

It could be pretty cool to have a \insertpresentationendframe but it doesn't exists.

A dirty solution I found is to define a new latex counter
\newcounter{presentationendframenumber}

to use it in the footer template (by the way I defined a footer with navigation symbols within it instead of within the default right hidden sidebar):

\defbeamertemplate{footline}{my page number}
{%
\usebeamercolor[fg]{page number in head/foot}%
\usebeamerfont{page number in head/foot}%
% \hspace{1em}\insertframenumber\,/\,\inserttotalframenumber\hfill\usebeamertemplate***{navigation symbols}\vskip5pt%
\hspace{1em}\insertframenumber\,/\,\thepresentationendframenumber\hfill\usebeamertemplate***{navigation
symbols}\vskip5pt%
% \insertshortauthor\kern1em\vskip2pt%
}
\setbeamertemplate{footline}[my page number]{}
% remove default sidebar with navigation symbols
\setbeamertemplate{sidebar right}{}


And then to set this new counter to the correct value at the begining of the document:

\setcounter{presentationendframenumber}{\inserttotalframenumber}
% my presentation has one page in appendix
\addtocounter{presentationendframenumber}{-1}

It requires to set the number of appendix frame by hand, which is not perfect.
At least it works.