How To Auto-Disable The Touchpad When The Mouse Is Plugged In (Fedora 13)

Want to support HowtoForge? Become a subscriber!
 
Submitted by deadlycheese (Contact Author) (Forums) on Fri, 2010-10-29 18:33. :: Fedora | Desktop

How To Auto-Disable The Touchpad When The Mouse Is Plugged In (Fedora 13)

Hello all, hopefully this brief how-to will help others, this issue has been bugging me for years. I want the same capability in Fedora that exists in most recent versions of Windows -- disable the touchpad on my laptop if an external mouse is plugged in.  Note that my how-to is a little hardware-specific regarding the actual disabling of the touchpad; I'll discuss that more at the end of the guide.

So, here goes:

For my OS (Fedora 13 x86_64) and hardware (Dell Precision M4500) - I needed a specific utility and three scripts.  This Dell's trackpad and nipple-stick are seen as an 'internal' PS/2 mouse by Fedora, so I had to install 'xinput' to disable it (and use a very arcane little command-line). 

# yum -y install xorg-x11-apps

The enable/disable commands may be different for different hardware ... I had to use some xinput commands to figure out the values I needed:

$ xinput list

    ⎡ Virtual core pointer                        id=2    [master pointer  (3)]
    ⎜   ↳ Virtual core XTEST pointer                  id=4    [slave  pointer  (2)]
    ⎜   ↳ USB Optical Mouse                           id=10    [slave  pointer  (2)]
    ⎜   ↳ PS/2 Generic Mouse                          id=13    [slave  pointer  (2)]

$ xinput list-props "PS/2 Generic Mouse"

    Device 'PS/2 Generic Mouse':
    Device Enabled (119):    1
    ...

This told me that I could use the following:

    to disable touchpad:

xinput --set-prop "PS/2 Generic Mouse" "Device Enabled" 0

    to enable touchpad: 

xinput --set-prop "PS/2 Generic Mouse" "Device Enabled" 1

Google to learn more about using xinput.

Then, 3 scripts:

  - Add the following as /etc/udev/rules.d/61-touchpad.rules:

# 61-touchpad.rules
#
# this rules file must be named 61* or later because it won't work
# unless it runs after '/lib/udev/rules.d/60-persistent-input.rules'
#
# NOTE: will only affect DISPLAY :0
#
# run:
#   udevadm test --action=add /sys/devices/platform/i8042/serio1/input/input6/mouse1
# or similar to test the following rules
#

# disable PS/2 touchpad on DISPLAY :0 if a mouse is added to the system
ACTION=="add", SUBSYSTEM=="input", ENV{ID_INPUT_MOUSE}=="1", RUN+="/bin/sh -c 'DISPLAY=:0 /usr/bin/xinput --set-prop PS/2\ Generic\ Mouse Device\ Enabled 0'"
# enable PS/2 touchpad on DISPLAY :0 if a mouse is removed from the system ACTION=="remove", SUBSYSTEM=="input", ENV{ID_INPUT_MOUSE}=="1", RUN+="/bin/sh -c 'DISPLAY=:0 /usr/bin/xinput --set-prop PS/2\ Generic\ Mouse Device\ Enabled 1'"

  - Update /etc/gdm/Init/Default.
    - Add the following just before the 'exit 0' at the end of the file:

# disable touchpad if more than one mouse is available
if [ `ls -d /sys/class/input/mouse* | wc -l` -gt 1 ]; then
    /usr/bin/xinput --set-prop PS/2\ Generic\ Mouse Device\ Enabled 0
fi

  - Add the following as /etc/pm/sleep.d/99touchpad:

#!/bin/sh
#
# disable touchpad if more than one mouse is available
#
# NOTE: will only affect DISPLAY :0
#

disable_touchpad()
{
    /bin/sh -c 'DISPLAY=:0 /usr/bin/xinput --set-prop PS/2\ Generic\ Mouse Device\ Enabled 0'
} case "$1" in thaw|resume) if [ `ls -d /sys/class/input/mouse* | wc -l` -gt 1 ]; then disable_touchpad fi ;; *) exit $NA ;; esac

The udev rules notice when a mouse is hot-plugged, and enable or disable the touchpad as necessary.  The gdm script is necessary to handle the situation where the system is booted with a mouse plugged in (and therefore a hot-plug event doesn't occur).  I thought that was it, but after a suspend and resume, I discovered that the touchpad was active again.  So, more googling, and I added the /etc/pm/sleep.d script to handle things on resume.

The actual disabling utility will be specific to different types of hardware; synaptic touchpads would use the 'syndaemon' utility, I believe.  There are lots of little subtleties to this process - the naming of the udev rules file and the sleep.d script is significant (the rules have to run after other mouse setup rules have run), the DISPLAY variable has to be set in the disable commands (because the daemons executing the scripts are not running in the X server), and the determining of whether or not you have multiple mice is maybe not as robust as I'd like.  But, it is working for me.  Also, different versions of Fedora may require different udev rules, or different directories for script placement, or different numbers in the filenames ... this isn't pretty, it should really be handled by a utility bundled with the distribution.

Finally, note that I am assuming that DISPLAY :0 is the DISPLAY of interest, and that the udev rules won't work right if you plug in more than two mice (???) ...  If someone is interested they might be able to make this more robust ... go for it!

So, this isn't the most complete how-to ever, but I wanted to get it out there.  My apologies if a better how-to exists, or if a better procedure is available.  If you know of something better ... please follow up.

 

Addendum:

FYI - the udev stuff was not easy to figure out.  Here are some commands I ran while I was trying to get this sorted:

- Get udev environment info for the touchpad:

udevadm info --query=all --name=/dev/input/mouse1

- Testing specific udev rules:

# udevadm test --action=add /sys/devices/platform/i8042/serio1/input/input6/mouse1 2>&1 | grep "run:"

udevadm_test: run: '/bin/sh -c 'DISPLAY=:0 /usr/bin/xinput --set-prop PS/2\ Generic\ Mouse Device\ Enabled 0''
udevadm_test: run: 'socket:@/org/freedesktop/hal/udev_event'

# udevadm test --action=remove /sys/devices/platform/i8042/serio1/input/input6/mouse1 2>&1 | grep "run:"

udevadm_test: run: '/bin/sh -c 'DISPLAY=:0 /usr/bin/xinput --set-prop PS/2\ Generic\ Mouse Device\ Enabled 1''
udevadm_test: run: 'socket:@/org/freedesktop/hal/udev_event'


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Cipher (not registered) on Wed, 2012-10-17 18:35.
Submitted by MP (not registered) on Mon, 2012-10-01 06:08.

 (see the weblink for full scripts for Ubuntu 12)

The lines/shell calls that start with DISPLAY=:0 do not incorporate this value of DISPLAY. Those statements must be set off from the following commands by ; or && or the variable will not be set. To test: type "DISPLAY=:99 echo hi $DISPLAY" at the command line)

The other major change I had to make was in conjunction with the .rules files - for some reason an external USB keyboard registers in my system as being mouse0, despite not having ID_INPUT_MOUSE set.

case "$1" in
    thaw
|resume)
      tc
=0
     
for i in `ls -d /dev/input/mouse*`; do
       
if [ `udevadm info --query=property --name=$i | egrep -c "ID_INPUT_(TOUCHPAD|MOUSE)"` -ge 1 ]; then
               
#some are entered as both a touchpad AND a mouse, each device can only add one to the total number of mice.  Could alternately check if any mouse is plugged in that is NOT a touchpad. This counting is needed in general because I have an external USB keyboard that registers as "mouse0", but does not match either MOUSE or TOUCHPAD above.
         
(( tc += 1 ))
       
fi
     
done
     
if [ $tc -gt 1 ]; then
                disable_touchpad
           
else
                enable_touchpad
     
fi
     
;;
   
*) exit $NA
       
;;
esac
Submitted by MP (not registered) on Mon, 2012-10-01 15:53.

also need to launch the xhost application (requires /root/.Xauthority to exist...can be a sym link to your primary user directory file)

to turn off is now:

 sh -c 'export DISPLAY=:0; export PROTOCOL=auto-dev; export XAUTHORITY=/root/.Xauthority; /usr/bin/xhost +localhost; /usr/bin/xinput --set-prop "SynPS/2 Synaptics TouchPad" "Device Enabled" 0' >>/var/log/synoff 2>>/var/log/synoff

 

It also looks like the weblink was stripped from my previous posting.  Full scripts can be found here: 

 http://www.evga.com/forums/tm.aspx?high=&m=1755212

Submitted by MP (not registered) on Mon, 2012-10-01 15:51.

also need to launch the xhost application (requires /root/.Xauthority to exist...can be a sym link to your primary user directory file)

to turn off is now:

 sh -c 'export DISPLAY=:0; export PROTOCOL=auto-dev; export XAUTHORITY=/root/.Xauthority; /usr/bin/xhost +localhost; /usr/bin/xinput --set-prop "SynPS/2 Synaptics TouchPad" "Device Enabled" 0' >>/var/log/synoff 2>>/var/log/synoff

Submitted by Filippo (not registered) on Fri, 2011-01-28 12:17.

Your HowTo works well also on my Sony Vaio VPCF1 with Ubuntu 10.10 but with one addition: on my laptop /etc/pm/sleep.d/99touchpad has to be copied to /etc/pm/power.d/99touchpad, otherwise when AC cable is not connected touchpad is no available.

 Filippo.

Submitted by Chris (not registered) on Mon, 2010-11-08 00:18.

Debian Squeeze running on an older Macbook.  Strangely, when I type "xinput --set-prop appletouch Device\ Enabled 1" (or 0), the appletouch enables and disables.  However, when I put the exact same command in 61_touchpad.rules, it doesn't work.

I've verified that the rules are being run, and that what is called from the script is the same as what I type (with the exception of the back slash).  But it doesn't work.

I suspect the problem is the "Macintosh-ness" of the computer, rather than your scripts and that this would work on a "normal" laptop.  I'll come back if I figure it out.

 

For now I simply disable it by hand from a command prompt.  I set couple of aliases that take the pain out.  Which is not as nice, but better than nothing...

Submitted by Anonymous (not registered) on Wed, 2011-09-28 19:56.
I have the same issue on a Dell laptop with Ubuntu 11.04
Submitted by The IceMAn (registered user) on Thu, 2010-11-04 10:34.
this tip is most useful on my small netbook while i type.
Submitted by leif81 (not registered) on Fri, 2011-03-18 17:40.

If you're running Gnome (on Fedora 14)

1. System->Preferences->Mouse

2. Go to Touchpad tab

3. Select ' Disable touchpad while typing'

 

Submitted by deadlycheese (registered user) on Wed, 2010-11-03 21:56.
Update: I edited the scripts above to reflect a better way of using the xinput utility. Previously, I was recommending a command like the following to disable the touchpad:

xinput --set-int-prop 13 119 8 0

This works ... as long as the X server doesn't reorder devices. I discovered the vulnerability when my keyboard stopped working whenever my external mouse was plugged in (!). This was because I had rebooted, and this time the X server gave my keyboard an ID of '13', and the touchpad was now ID '14'. So, my scripts were enabling and disabling the keyboard whenever the mouse was plugged in ... not what I wanted. The edits above now show a more reliable way to enable and disable the touchpad:

to disable touchpad: xinput --set-prop "PS/2 Generic Mouse" "Device Enabled" 0
to enable touchpad: xinput --set-prop "PS/2 Generic Mouse" "Device Enabled" 1

Not only is this more reliable, but it is much easier to understand what xinput is doing (and also avoids the deprecated '--set-prop-int' xinput parameter).

So, an important improvement, please revisit your scripts if you were using my first xinput approach.   
Submitted by Anonymous (not registered) on Tue, 2010-11-02 13:58.
This works nicely in Ubuntu, too. ximput is already installed by default, so Ubuntu users can just go ahead with the rest of the tutorial. Better yet, why not make this howto generic?
Submitted by Filippo (not registered) on Fri, 2011-01-28 12:25.

It works also for me on Ubuntu 10.10 but I added /etc/pm/sleep.d/touchpad99  to the /etc/pm/power.d directory too, otherwise when AC cable is not connected touchpad is disabled.

Submitted by deadlycheese (registered user) on Wed, 2010-11-03 22:04.

Ah, good, glad to hear it works on Ubuntu, too.  Unfortunately, I don't have the time to fully test this on Ubuntu ... I would expect that it is mostly the same, but there is always the chance that the directory for the resume script is different on different distributions, or that the numbering of the name of the udev rules file might need to change on another distribution.  As a matter of fact, both of those elements could change from release to release in the same distribution (and I haven't tested these scripts on Fedora 14 yet).

 So, caveat emptor.  I hope it does work great on Ubuntu ... but so far it has only been thoroughly tested on Fedora 13.  Maybe people using this method can leave comments like "Works on Ubuntu 10.10" if they discover that it works for them.

Submitted by Philippe (not registered) on Fri, 2010-11-26 17:36.

Well, I have to say...

 Works on Ubuntu 10.04! (not yet switched to 10.10 though)

 Excellent tutorial. Many, many thanks!

Submitted by Mad-Halfling (not registered) on Thu, 2010-12-02 10:34.

Just a tip for an enable or disable script, try running

xinput list

to show the devices, mine worked with

xinput --set-prop SynPS/2\ Synaptics\ TouchPad Device\ Enabled 0

in Ubuntu 10.10/Gnome
Submitted by Anonymous (not registered) on Wed, 2011-01-05 16:52.
Good tip ... of course, it is already mentioned as the second command in the author's how-to, above ...
Submitted by Anonymous (not registered) on Thu, 2011-07-14 23:49.

Using quotes around the name of the device was not working for me for some reason. In Ubuntu 10.10 I had to:

sudo apt-get install xinput
 xinput list
 xinput list-props ImPS/2\ ALPS\ GlidePoint\

DISABLE TOUCH PAD:
xinput --set-prop ImPS/2\ ALPS\ GlidePoint "Device Enabled" 0


ENABLE TOUCH PAD:
xinput --set-prop ImPS/2\ ALPS\ GlidePoint "Device Enabled" 1

 

Now if I can map those two scripts to a button on my keyboard I will be happy. Can linux commands have an IF statement? If so,(no pun intended) anyone have an idea of what I could do to check if the mouse is disabled or enabled? Maybe something like:

If

xinput --set-prop ImPS/2\ ALPS\ GlidePoint "Device Enabled" 1 =true

 xinput --set-prop ImPS/2\ ALPS\ GlidePoint "Device Enabled" 0

 else

xinput --set-prop ImPS/2\ ALPS\ GlidePoint "Device Enabled" 1

Submitted by JayBofMA (not registered) on Fri, 2011-09-16 02:25.
I would expect you could get your conditional logic using the CASE statement seen in the 99touchpad example.