You are not logged in.

#1 2015-07-06 05:57:11

khouli
Member
Registered: 2014-12-14
Posts: 70

Run setxkbmap and xset upon plugging in keyboard

How can I run setxkbmap and xset commands automatically upon plugging in my keyboard?

This question has been asked a lot before.

https://superuser.com/questions/249064/ … plugged-in
https://bbs.archlinux.org/viewtopic.php?id=180079
https://bbs.archlinux.org/viewtopic.php?id=104338
https://bbs.archlinux.org/viewtopic.php?id=195026

At the moment all previous solutions are broken as far as I can tell. The second link indicates that this has happened before. The solution that previously seems to have had the most traction is to run a script started by udev that starts another script in the background and then returns. The second script sleeps for a second and then runs xset and setxkbmap commands. Apparently something else overwrites settings that are applied too early.

Here's that strategy and the output at the moment.

/etc/udev/rules.d/10-local.rules

SUBSYSTEM=="input", ATTRS{interface}=="Keyboard", ACTION=="add", RUN+="/home/khouli/scripts/keyboard_setup_udev.sh", OWNER="khouli"

The outer script, /home/khouli/scripts/keyboard_setup_udev.sh

#!/bin/bash

touch /home/khouli/udev_debug.txt
chown khouli /home/khouli/udev_debug.txt

echo "outer: started" >> /home/khouli/udev_debug.txt
echo "outer: udev triggered" >> /home/khouli/udev_debug.txt

# Try the background sleep hack.
(
    /home/khouli/scripts/keyboard_setup.sh
) &>/dev/null </dev/null &

echo "outer: jobs" >> /home/khouli/udev_debug.txt
jobs >> /home/khouli/udev_debug.txt
echo "outer: disowning" >> /home/khouli/udev_debug.txt

disown %1

# Make sure we've really forgotten about the job.
echo "outer: jobs" >> /home/khouli/udev_debug.txt
jobs >> /home/khouli/udev_debug.txt

# We've forgotten the job, wait for some output to confirm it's still running.
sleep 2
# When this script finishes, the inner script is killed.
echo "outer: finished" >> /home/khouli/udev_debug.txt

The inner script, /home/khouli/scripts/keyboard_setup.sh

#!/bin/bash

# Whatever kills this script can't be trapped, try anyway...
for t in $(seq 0 30); do
    trap "echo inner: it\'s a trap! $t >> /home/khouli/udev_debug.txt; exit" $t
done

echo "inner: started" >> /home/khouli/udev_debug.txt

export DISPLAY=":0.0"
export HOME=/home/khouli/
export XAUTHORITY=$HOME/.Xauthority
echo "inner: environment set" >> /home/khouli/udev_debug.txt

# show that setting this early will be overwritten
echo "inner: setxkbmap -option caps:swapescape" >> /home/khouli/udev_debug.txt
echo "inner: xset r rate 200 30" >> /home/khouli/udev_debug.txt
echo "inner: query" >> /home/khouli/udev_debug.txt
setxkbmap -option caps:swapescape >> /home/khouli/udev_debug.txt
xset r rate 200 30 >> /home/khouli/udev_debug.txt
setxkbmap -verbose -query >> /home/khouli/udev_debug.txt

echo "inner: naptime" >> /home/khouli/udev_debug.txt

# countdown, will be killed somewhere in here
n=3
while [ $n -gt 0 ]; do
    echo "inner: ${n}..." >> /home/khouli/udev_debug.txt
    sleep 1
    n=$(( n - 1 ))
done

echo "inner: running setkbmap and xset" >> /home/khouli/udev_debug.txt
echo "inner: query before" >> /home/khouli/udev_debug.txt
echo "inner: setxkbmap -query" >> /home/khouli/udev_debug.txt
setxkbmap -verbose -query >> /home/khouli/udev_debug.txt

# payload
setxkbmap -option caps:swapescape
xset r rate 200 30

echo "inner: query after" >> /home/khouli/udev_debug.txt
echo "inner: setxkbmap -query" >> /home/khouli/udev_debug.txt
setxkbmap -verbose -query >> /home/khouli/udev_debug.txt

echo "inner: finished" >> /home/khouli/udev_debug.txt

The output to udev_debug.txt:

outer: started
outer: udev triggered
outer: jobs
[1]+  Running                 ( /home/khouli/scripts/keyboard_setup.sh ) &> /dev/null < /dev/null &
outer: disowning
outer: jobs
inner: started
inner: environment set
inner: setxkbmap -option caps:swapescape
inner: xset r rate 200 30
inner: query
Trying to build keymap using the following components:
keycodes:   evdev+aliases(qwerty)
types:      complete
compat:     complete
symbols:    pc+us+inet(evdev)+capslock(swapescape)
geometry:   pc(pc105)
rules:      evdev
model:      pc105
layout:     us
options:    caps:swapescape
inner: naptime
inner: 3...
inner: 2...
outer: finished

The inner script is killed when the outer script returns.

The keyboard settings have been reset:

$ setxkbmap -query
rules:      evdev
model:      pc105
layout:     us

Thoughts on how to get this working again?

Offline

#2 2015-07-08 10:21:32

berbae
Member
From: France
Registered: 2007-02-12
Posts: 1,302

Re: Run setxkbmap and xset upon plugging in keyboard

Hello Khouli.
I seem to understand that Xorg has a hotplugging feature for input devices using evdev and udev.

To verify if xorg server sees your keyboard plugged in, would you agree to test that on your computer?

First remove the '/etc/udev/rules.d/10-local.rules' and have udev reload without the rule.

Then restart xorg server without the keyboard you want to configure on plugging in.
And post the final part of Xorg.0.log where input devices are detected and evdev is used for power button, mouse, keyboard, ...

Then plug in the other keyboard you want to configure when hotplugged.

And post the end of Xorg.0.log as above, to see if the xorg server has seen it and the configuration it used for it.
(you can 'tail -f Xorg.0.log' before plugging the keyboard in)
Waiting for your answer...

Offline

#3 2015-07-27 03:28:45

khouli
Member
Registered: 2014-12-14
Posts: 70

Re: Run setxkbmap and xset upon plugging in keyboard

Oh wow, sorry! I was travelling for a while and this one for a while and then didn't notice this for a few days after that -- I really need to figure out to get the forum software here to send me emails.

I'd be happy to test anything. I'd very much like to know what's going on here and how to figure it out.


This gist has a page for my my /var/log/Xorg.0.log with the keyboard and another without:

https://gist.github.com/khouli/75d15848eea7275c1936

Here's the portion where the keyboard is being added:

[200002.344] (II) config/udev: Adding input device Topre Corporation Realforce 87 (/dev/input/event3)
[200002.344] (**) Topre Corporation Realforce 87: Applying InputClass "evdev keyboard catchall"
[200002.344] (**) Topre Corporation Realforce 87: Applying InputClass "libinput keyboard catchall"
[200002.344] (II) systemd-logind: got fd for /dev/input/event3 13:67 fd 35 paused 0
[200002.344] (II) Using input driver 'libinput' for 'Topre Corporation Realforce 87'
[200002.344] (**) Topre Corporation Realforce 87: always reports core events
[200002.344] (**) Option "Device" "/dev/input/event3"
[200002.344] (II) input device 'Topre Corporation Realforce 87', /dev/input/event3 is tagged by udev as: Keyboard
[200002.344] (II) input device 'Topre Corporation Realforce 87', /dev/input/event3 is a keyboard
[200002.344] (**) Option "config_info" "udev:/sys/devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9:1.0/0003:0853:0111.0007/input/input24/event3"
[200002.344] (II) XINPUT: Adding extended input device "Topre Corporation Realforce 87" (type: KEYBOARD, id 9)
[200002.345] (II) input device 'Topre Corporation Realforce 87', /dev/input/event3 is tagged by udev as: Keyboard
[200002.345] (II) input device 'Topre Corporation Realforce 87', /dev/input/event3 is a keyboard

I also have a /home/khouli/.local/share/xorg/Xorg.0.log if that's relevant.

Thanks.

Offline

#4 2015-07-27 10:32:37

berbae
Member
From: France
Registered: 2007-02-12
Posts: 1,302

Re: Run setxkbmap and xset upon plugging in keyboard

Did you follow exactly the sequence of actions I described in my post #2?
It doesn't seem so, from the logs you posted?
There is no delay there:

[200002.344] (II) input device 'Logitech Optical USB Mouse', /dev/input/event4 is a pointer caps
[200002.344] (II) config/udev: Adding input device Logitech Optical USB Mouse (/dev/input/mouse0)
[200002.344] (II) No input driver specified, ignoring this device.
[200002.344] (II) This device may have been added with another device file.
[200002.344] (II) config/udev: Adding input device Topre Corporation Realforce 87 (/dev/input/event3)
[200002.344] (**) Topre Corporation Realforce 87: Applying InputClass "evdev keyboard catchall"

Last edited by berbae (2015-07-27 10:32:50)

Offline

#5 2015-07-27 10:59:55

khouli
Member
Registered: 2014-12-14
Posts: 70

Re: Run setxkbmap and xset upon plugging in keyboard

I didn't quite understand what you wanted me to do. I removed the udev rule I had added, stopped X and then started it without the keyboard plugged and then I started and stopped it a second time but with the keyboard plugged in.

Instead you wanted me to restart X without the keyboard pluggin in, wait a little, plug it in, and then save the last part of the log, is that it?

Offline

#6 2015-07-27 14:46:43

berbae
Member
From: France
Registered: 2007-02-12
Posts: 1,302

Re: Run setxkbmap and xset upon plugging in keyboard

Yes it is.
I want to see if xorg sees the keyboard when you plug it in, and what files it uses to configure it;
ie if hotplugging works with udev/evdev and actual xorg.

Use 'tail -f /var/log/Xorg.0.log' to see live what appears on the screen, and post that here.

Offline

Board footer

Powered by FluxBB