This appears to be a bug (the fact that the touchscreen device randomly disconnects and reconnects as a new device, which has the side-effect of resetting all settings to default).
As a workaround, you can create your own udev rule (file name based on these suggestions by Daniel Drake) that will run a script that re-applies the xinput changes whenever the touchscreen reconnects:
sudo nano /etc/udev/rules.d/10-custom-elan.rules
and add this line which contains "idVendor" and "idProduct" information (from your syslog). The absolute path to "elan.sh" must be used.
ATTRS{idVendor}=="04f3", ATTRS{idProduct}=="2073", RUN+="/home/username/elan.sh"
(which roughly translates to ""when a device that matches the specified attributes is found, run the designated script").
Then create the actual script to run xinput:
nano /home/username/elan.sh
with the following lines:
#!/usr/bin/env bash
#These lines allow the script to be called by udev rules
export DISPLAY=":0"
export XAUTHORITY="/home/username/.Xauthority"
#Command to remap buttons
xinput set-button-map "ELAN Touchscreen Pen" 1 3 2 4 5
And of course make it executable:
chmod +x /home/username/elan.sh
Without the export lines, the script works when called by you (the active user) directly, but it doesn't work when called by udev (the root user). Details can be found in this and this answer, but here's a brief summary:
To launch a graphical program on a user's desktop, you need two things: the address (what display the user's desktop is on) and authorisation. When a user logs in, the login manager authorises a connection to the X server by generating a cookie, adding it to the server and passing it to the user by writing it to $HOME/.Xauthority. The root user should, then, be able to connect by knowing the display used by the user and by having access to the Xauthority cookie. This is what the export lines achieve.
Note: the fact that the display number is hardcoded might cause a problem under some circumstances, but in this usage scenario (single user of a personal laptop) it will probably be OK.
You can use touchegg
.
Installation and configuration
See my other answer.
My experience with touchegg
Sadly, at least with my touchscreen (ThinkPad X201 Tablet), I have found that the more complex gestures like "two-finger pinch" are not recognized reliably enough by touchegg
. With a bit of caution, it can be well used for two-finger-tap right-clicking (details). The two-finger-drag gestures are also recognized well, but for me, none beyond these.
Alternatives to touchegg
There is fusuma
, a Ruby based application made for gesture recognition on touchpads. It may also work on touchscreens; I did not try yet. For installation instructions, see this answer.
There is libinput-gestures
, and you can find recommendations for it. In my understanding, this is however specific to the libinput
driver, which is used for most touchpads. Touchscreens will usually use the wacom
driver instead. But, I did not try this – there might be a way to get it to work.
Best Answer
I have filled a bug report about touchscreen. It is at https://bugs.launchpad.net/ubuntu/+source/xserver-xorg-input-evdev/+bug/1121379 . Please press the "affects me too" button in order to increase the "heat" and make more people/developers notice it. I can help with more tests or trying fixes. So if you have to propose something to test (e.g. install a newer kernel or X version, apply some patch, tryout some config), please tell me.