8a9febd39f6d876fe4356f1c5b65149e32f5af6a — Timothée Floure 1 year, 2 months ago acde4c6 master
Handler: match monitor EDID and move "configuration" to external file
3 files changed, 68 insertions(+), 29 deletions(-)

M README.md
M monitor-event-handler
A rules.example
M README.md => README.md +4 -2
@@ 10,5 10,7 @@ * `monitor-event-listener`: simple C program listening for X event. It calls
      `monitor-event-handler` with the output name and action (plugged or unplugged)
      when a "monitor event" is detected.
-   * `monitor-event-handler`: simple shell script taking the output name and
-     action as arguments and executing preconfigured actions.
+   * `monitor-event-handler`: simple shell script that takes the output name and
+     action as arguments, compute the md5 hash of the EDID and feed
+     `$XDG_CONFIG_HOME/monitor-handler-rules` which contains preconfigured
+     actions.

M monitor-event-handler => monitor-event-handler +47 -27
@@ 1,29 1,49 @@ #!/bin/bash
  
- if [ "$#" -lt 4 ]; then
-   echo "Missing arguments !"
-   exit 1
- fi
- 
- while [ "$#" -gt 0 ]; do
-   case "$1" in
-     --output) output="$2"; shift 2;;
-     --action) action="$2"; shift 2;;
-     *) echo "Unknown parameter passed: $1"; exit 1;;
-   esac
- done
- 
- echo "Marching: $action $output"
- case "$output $action" in
-   "DP-1 connect")
-     xrandr --output DP-1 --mode 2560x1080 --pos 1920x0 --rotate normal
-     nitrogen --restore
-     ;;
-   "DP-1 disconnect")
-     xrandr --output DP-1 --off
-     nitrogen --restore
-     ;;
-   *)
-     echo "-> Unknown combination."
-     ;;
- esac
+ RULES_SCRIPT="$XDG_CONFIG_HOME/monitor-handler-rules"
+ DRM_DIRECTORY="/sys/devices/pci0000:00/0000:00:02.0/drm"
+ CARD="card0"
+ 
+ print_usage() {
+   echo "Usage: $0 --output OUTPUT --action ACTION"
+ }
+ 
+ main() {
+   while [ "$#" -gt 0 ]; do
+     case "$1" in
+       --output) output="$2"; shift 2;;
+       --action) action="$2"; shift 2;;
+       *)
+         echo "Unknown parameter: $1"
+         print_usage
+         exit 1
+         ;;
+     esac
+   done
+ 
+   if [ -z "$output" ] || [ -z "$action" ]; then
+     echo "A required parameter is missing."
+     print_usage
+     exit 1
+   fi
+ 
+   echo "[Handler] Received $action $output."
+ 
+   edid_path="$DRM_DIRECTORY/$CARD/$CARD-$output/edid"
+   if [ -f $edid_path ]; then
+     raw=$(cat $DRM_DIRECTORY/$CARD/$CARD-$output/edid)
+     hash=$(echo $edid | md5sum | head -c 32)
+     echo "[Handler] EDID hash is $hash."
+   else
+     echo "[Handler] Error: $edid_path does not exist."
+     exit 1
+   fi
+ 
+   if [ -f $RULES_SCRIPT ]; then
+     . $RULES_SCRIPT
+   else
+     echo "[Handler] Error: $RULES_SCRIPT does not exist."
+   fi
+ }
+ 
+ main $@

A rules.example => rules.example +17 -0
@@ 0,0 1,17 @@
+ #!/bin/sh
+ 
+ case "$hash $action" in
+   "68b329da9893e34099c7d8ad5cb9c940 connect") # DP-1
+     set -x
+     xrandr --output $output --mode 2560x1080 --pos 1920x0 --rotate normal
+     nitrogen --restore
+     ;;
+   "68b329da9893e34099c7d8ad5cb9c940 disconnect") # DP-1
+     set -x
+     xrandr --output $output --off
+     nitrogen --restore
+     ;;
+   *)
+     echo "-> Unknown combination."
+     ;;
+ esac