Linuxdoc Linux Questions
Click here to ask our community of linux experts!
Custom Search

C.1. hotplug: input.agent

/etc/hotplug/input.agent


#!/bin/sh
# Please place this file /etc/hotplug
#
# input-specific hotplug policy agent.
#
# Kernel Input params are:
#       
#       ACTION=add
#       PHYS=pysical location of device
#       NAME=Name of the device
#
# HISTORY:
#       15-JUN-2003     removed paste
#       07-MAY-2003     remake by Aivils Stoss
#                       /proc manipulation added
#                       parse kbd.conf event.conf mouse.conf.
#                       create necessary symbolic links
#       28-SEP-2002     Initial version from Andreas Schuldei
#                        andreas (at) schuldei.org 
#
cd /etc/hotplug
. hotplug.functions
DEBUG=yes export DEBUG

KBD_CONFIG="./kbd.conf"
EVENT_CONFIG="./event.conf"
MOUSE_CONFIG="./mouse.conf"

setup_kbd ()
{
    while read VT_NUM PHYS_PATTERN NAME_PATTERN
    do
        if [ `echo "$VT_NUM" | grep "^#"` ]; then
            continue;
        fi
        if [ `echo "$PHYS" | grep $PHYS_PATTERN 2>/dev/null` ]; then
            VT=`echo "$VT_NUM" | sed 's/VT//' | awk '{printf "%02d", $0}'`
            if [ -d /proc/bus/console/$VT ]; then
                echo "$PHYS" > "/proc/bus/console/$VT/keyboard"
                debug_mesg "Input device $NAME on $PHYS mapping as secondary to VT$VT"
                return;
            else
                debug_mesg "Trying to configure keyboard for VT$VT , but not enough VT's available"
            fi
        fi
    done
    debug_mesg "Found no fitting VT"
}

setup_event ()
{
    while read SYM_LINK PHYS_PATTERN NAME_PATTERN
    do
        if [ `echo "$SYM_LINK" | grep "^#"` ]; then
            continue;
        fi
        if [ `echo "$PHYS" | grep $PHYS_PATTERN 2>/dev/null` ]; then
            case $ACTION in
            add)
                cd /dev/input
                rm -f $SYM_LINK
                ln -s $DEV_EVENT $SYM_LINK
                debug_mesg "Input event device $NAME on $PHYS linked to $SYM_LINK"
            ;;
            remove)
                rm -f /dev/input/$SYM_LINK
                debug_mesg "Input event device link $SYM_LINK removed"
            ;;
            esac
            return;
        fi
    done
    debug_mesg "Found no fitting event device"
}

setup_mouse ()
{
    while read SYM_LINK PHYS_PATTERN NAME_PATTERN
    do
        if [ `echo "$SYM_LINK" | grep "^#"` ]; then
            continue;
        fi
        if [ `echo "$PHYS" | grep $PHYS_PATTERN 2>/dev/null` ]; then
            case $ACTION in
            add)
                cd /dev/input
                rm -f $SYM_LINK
                ln -s $DEV_MOUSE $SYM_LINK
                debug_mesg "Input mouse device $NAME on $PHYS linked to $SYM_LINK"
            ;;
            remove)
                rm -f /dev/input/$SYM_LINK
                debug_mesg "Input mouse device link $SYM_LINK removed"
            ;;
            esac
            return;
        fi
    done
    debug_mesg "Found no fitting mouse device"
}

if [ "$ACTION" = "" ]; then
    mesg Bad input agent invocation
    exit 1
fi

DEV_HANDLERS=`grep -E 'Phys|Handlers' /proc/bus/input/devices | \
    awk '{ if(count == 0) { printf("%s\t",$0); count++; } else { print $0; count=0; } }' | \
    grep $PHYS | awk -F\t '{print $2}' | sed 's/^.*=//'`

# older grep do not support -o :-(
#DEV_EVENT=`echo $MATCHED | grep -oE event[0-9]+`
#DEV_MOUSE=`echo $MATCHED | grep -oE mouse[0-9]+`
#DEV_KBD=`echo $MATCHED | grep -o kbd`

DEV_EVENT=`echo $DEV_HANDLERS | \
            awk -F" " '{for(n=1;$n;n=n+1) if($n ~ /event/) print $n}'`
DEV_MOUSE=`echo $DEV_HANDLERS | \
            awk -F" " '{for(n=1;$n;n=n+1) if($n ~ /mouse/) print $n}'`
DEV_KBD=`echo $DEV_HANDLERS | \
            awk -F" " '{for(n=1;$n;n=n+1) if($n ~ /kbd/) print $n}'`

#
# What to do with this input device event?
#
case "$ACTION" in

add)
    if [ -n "$DEV_KBD" ]; then
        setup_kbd < $KBD_CONFIG
    fi
    if [ -n "$DEV_EVENT" ]; then
        setup_event < $EVENT_CONFIG
    fi
    if [ -n "$DEV_MOUSE" ]; then
        setup_mouse < $MOUSE_CONFIG
    fi
    ;;
remove)
    #setup_event < $EVENT_CONFIG
    #setup_mouse < $MOUSE_CONFIG
    ;;
*)
    debug_mesg "Input '$ACTION' event not supported"
    return 1
    ;;

esac