#!/bin/bash
#####################################################################################
#                                                                                   #
#   Shows a small osd like popup and/or sound if CAPSLOCK is pressed.               #
#   Needs to be started via 'autostart applications'.                               #
#   Depends on: yad, evtest (evtest needs sudo on some systems, add it to sudoers)  #
#                                                                                   #
#   Mostly scripted by Koentje (remon@cobrasoft.nl)                                 #
#                                                                                   #
#                                                                      version 1.0  #
#####################################################################################

# Set input device path of your keyboard
KEYBOARD="/dev/input/by-path/pci-0000:00:14.0-usb-0:12.1:1.1-event-kbd"

# Set path to oga soundfile to play (/usr/share/mint-artwork/sound)
SOUNDFILE="/usr/share/mint-artwork/sounds/notification.oga"

# Sound with popup (on/off)
SOUND="on"

# Timeout of popup in seconds
TIMEOUT="1"

# Height of popup window
HEIGHT="80"

# Width of popup window
WIDTH="300"

# Fontname
FONTNAME="Ubuntu Bold"

# Fontsize (x-small, small, large, x-large, xx-large)
FONTSIZE="xx-large"

# Color of text when capslock is ON
TEXTCOLORON="orange"

# Color of text when capslock is OFF
TEXTCOLOROFF="grey"


##################################################################################################
#########################  CHANGES AFTER THIS LINE ARE AT OWN RISK  ##############################
##################################################################################################

EVENTON="type 17 (EV_LED), code 1 (LED_CAPSL), value 1"
EVENTOFF="type 17 (EV_LED), code 1 (LED_CAPSL), value 0"

while read line
do

    if [[ "$line" == *"$EVENTON" ]]; then
       killall -q yad
       yad \
         --text='\n<span foreground='"\"$TEXTCOLORON\""' font='"\"$FONTNAME\""' font-size='"\"$FONTSIZE\""'>     CAPSLOCK  ON     </span>\n' \
         --width="$WIDTH" --height="$HEIGHT" \
         --no-buttons --splash --centered --fixed --on-top \
         --timeout="$TIMEOUT" &
       if [ "$SOUND" = "on" ]; then gst-launch-1.0 -q playbin uri=file://$SOUNDFILE & fi

    elif [[ "$line" == *"$EVENTOFF" ]]; then
       killall -q yad
       yad \
         --text='\n<span foreground='"\"$TEXTCOLOROFF\""' font='"\"$FONTNAME\""' font-size='"\"$FONTSIZE\""'>     CAPSLOCK  OFF    </span>\n' \
         --width="$WIDTH" --height="$HEIGHT" \
         --no-buttons --splash --centered --fixed --on-top \
         --timeout="$TIMEOUT" &
       if [ "$SOUND" = "on" ]; then gst-launch-1.0 -q playbin uri=file://$SOUNDFILE & fi

    fi
done< <(sudo evtest "$KEYBOARD")
