12

I want to have my touchpad disabled automatically when an external mouse is connected and enabled when there is none. I have tried using touchpad-indicator but that fails in cases when the computer has been put to sleep with a mouse connected and awoken with the mouse disconnected.

I have tried to make the following script into a daemon to solve this issue but I can't get it to work:

#!/bin/bash

declare -i TID
declare -i MID
TID=`xinput list | grep -Eo 'Touchpad\s*id\=[0-9]{1,2}' | grep -Eo '[0-9]{1,2}'`
MID=`xinput list | grep -Eo 'Mouse\s*id\=[0-9]{1,2}' | grep -Eo '[0-9]{1,2}'`
if [ $MID -gt 0 ]
then
    xinput disable $TID
else
    xinput enable $TID
fi

I tried start-stop-daemon -S -x ./myscript.sh -b

and setsid ./myscript.sh >/dev/null 2>&1 < /dev/null &

and nohup ./myscript 0<&- &>/dev/null & and even ./myscript.sh &

All of these return some 4-digit number, which, I guess, should be PID of the started process but when I launch lxtask there are no processes with this PID, even if I tick "view all processes". And, of course, it doesn't work!

6
  • @terdon Im trying to make a daemon watching connected usb mouse and ask xinput to enable or disable touchpad. The only other solution for the problem, a program called touchpad-indicator, has a little flaw. When I sen my notebook asleep with mouse connected and wake it up without, I have to search for the nearest mouse to connect/disconnect it so my touchpad would be enabled. In short, I want the fact of it to be re-checked on wake-up.
    – mekkanizer
    Mar 23, 2014 at 18:11
  • Welcome to Ask Ubuntu! Please edit your question to add extra info, it is hard to read and easy to miss in the comments.
    – terdon
    Mar 23, 2014 at 18:20
  • First of all you should explain that you are "trying to daemonize it" and how exactly you're going about that. Then, xinput needs a running X server, and most methods of running stuff as daemons have no knowledge of or ability to connect to X. Third, and the main reason why you should always explain what you are trying to do is that very often the solution that someone is attempting to apply is not actually the way to get it done and you end up asking the wrong question, commonly known as the XY problem.
    – terdon
    Mar 23, 2014 at 18:34
  • @terdon Back to looking for solutions, what is the (possible?) analogue of xinput that: 1. Can run in background 2. Can enable/disable devices?
    – mekkanizer
    Mar 23, 2014 at 18:40
  • 1
    comments removed. Being nice on Ask Ubuntu is not optional. Please be nice. Rude comments will be removed without warning.
    – Seth
    Mar 23, 2014 at 19:29

2 Answers 2

12

Okay, I've made an udev rule for it, and it is, like @terdon said, a much cleaner way

So, thanks to this guide, I created a "touchpad_toggle.rules" file in /etc/udev/rules.d/ (requires root access) and filled it with two lines:

SUBSYSTEM=="input", KERNEL=="mouse[0-9]*", ACTION=="add", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/username/.Xauthority", RUN+="/home/username/on.sh"
SUBSYSTEM=="input", KERNEL=="mouse[0-9]*", ACTION=="remove", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/username/.Xauthority", RUN+="/home/username/off.sh"

Don't forget to replace "username" with your username!

The contents of these on and off shell scripts are just castrated version of the script in my question. Example - off.sh:

#!/bin/bash

declare -i TID
TID=`xinput list | grep -Eo 'Touchpad\s*id\=[0-9]{1,2}' | grep -Eo '[0-9]{1,2}'`
xinput disable $TID

You'll have to use xinput enable $TID in the on.sh

And don't forget to add the script in my question (or the one @terdon suggested, but without a loop) to session autostart like he told you in his answer.

That is it, but I have to add one thing:

If you have a Synaptics touchpad (I have Elantech, so it's not suitable for me), you can replace your scripts (paths to which you write after RUN+=) with a simple command /usr/bin/synclient TouchpadOff=0 and 1 respectively

4
  • just a nitpick, since you need root access to create the udev rule any way, it may be better to save the file somewhere other than the home directory. For example: /usr/local/bin or /opt/touchpad-toggle/bin/.
    – Dan
    Dec 15, 2017 at 10:34
  • @Dan declined. Everything related to user settings better be in home directory. Unfortunately, udev rules, login manager settings and a couple of other things are fated to be copied back to /* each time you scrap / partition, unless you restore a snapshot of course. So having more things stored in /home/ is just convenience. So, your advice has no value
    – mekkanizer
    Dec 16, 2017 at 7:20
  • But since it got added as a udev rule, wouldn't that mean that it would run for any user, no matter who has logged it?
    – Dan
    Dec 18, 2017 at 8:56
  • I didn't check, as I never used Linux on shared PCs. Anyways, any user who is irritated by this neat udev rule is none of my interest. If they are so sensitive, they can get their own installation
    – mekkanizer
    Dec 18, 2017 at 13:26
11

The basic script you need is simply:

#!/usr/bin/env bash

## Get the touchpad id. The -P means perl regular expressions (for \K)
## the -i makes it case insensitive (better portability) and the -o
## means print only the matched portion. The \K discards anything matched
## before it so this command will print the numeric id only.
TID=$(xinput list | grep -iPo 'touchpad.*id=\K\d+')

## Run every second
while :
do
   ## Disable the touchpad if there is a mouse connected
   ## and enable it if there is none.
    xinput list | grep -iq mouse &&  xinput disable "$TID" || xinput enable "$TID" 
    ## wait one second to avoind spamming your CPU
    sleep 1
done

The script above will toggle the trackpad depending on whether a mouse is connected. When launched, it will run for ever and will check for a mouse every second, disabling or enabling the touchpad accordingly.

Now, save the script as ~/touchpad.sh, make it executable (chmod +x ~/touchpad.sh) and add it to your GUI session startup programs. You have not specified which desktop environment you are using but since you mentioned lxtask, I will assume you are using LXDE. In any case, here are instructions for both LXDE and Unity:

  1. Add the script to LXDE's autostart files

    echo "@$HOME/touchpad.sh" >> ~/.config/lxsession/PROFILE/autostart file
    

    Make sure you replace "PROFILE" with the actual name of your LXDE profile, you can find out what it is by running ls ~/.config/lxsession/.

  2. Add the script to Unity's autostart files

    Open Startup Applications (search in the dashboard for "Startup")

    enter image description here

    Click on "Add" and then paste the path to your script in the command field:

    enter image description here

3
  • I didn't use a loop because when I googled about running scripts in background, everyone suggested not to use loops. If that's the only solution and there's no analogue of xinput which CAN be used in a script for a daemon and CAN enable/disable devices... ok, I'll use it.
    – mekkanizer
    Mar 23, 2014 at 19:32
  • 1
    @mekkanizer this is not really the best way as such. A much cleaner way would be to use udev rules and exporting the necessary X credentials to them. You can look into that if you wish.
    – terdon
    Mar 23, 2014 at 19:39
  • Thanks, will do. If I get it to work, I'll write back (or post an answer)
    – mekkanizer
    Mar 23, 2014 at 19:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .