You are not logged in.
Hi,
I have this udev rule under /etc/udev/rules.d/99-hhkb.rules:
ACTION=="add", SUBSYSTEMS=="usb", ATTRS{idVendor}=="0853", ATTRS{idProduct}=="0100", ATTRS{manufacturer}=="Topre Corporation", ATTRS{product}=="HHKB Professional", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/pierre/.Xauthority", RUN+="/home/pierre/.local/bin/hhkb.sh"When I plug my usb device it works well. But when I boot with the device already plugged in, the rule is triggered too early (before X starts).
So I need a way to trigger this rule manually.
For that I tested this:
$ sudo udevadm trigger -c add -s usb --attr-match=idProduct=0100 --attr-match=idVendor=0853 --attr-match=manufacturer="Topre Corporation" --attr-match=product="HHKB Professiona
l" -v
/sys/devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1.1Thank to the verbose output we can see that the device is triggered correctly, but my rule seems not because my script is not executed.
Any idea why ?
Last edited by doums (2020-09-11 11:19:44)
Offline
1, Systemd-udevd will spawn several threads for different udev rules while booting. How about waiting for X to start inside your hhkb.sh?
> cat hhkb.sh
#!/bin/bash
hhkb()
{
local pid
local timer=1
while true;do
# Check if Xorg PID exists
pid=$(ps --no-headers -C Xorg -o pid=)
[[ -n $pid ]] || {
sleep $timer
continue
}
# Do what ever needed to be done.
touch /tmp/$timer
return
done
}
hhkb2, Another way is to create a user@.service that depends on Window Manager to start X, Display manager to start Login session. After that, your hhkb.sh can be triggered. It's much more involved compared with the first solution.
Last edited by solskog (2020-09-10 05:11:44)
Offline
After more test, I discovered that my rule is not trigger at all, at boot, usb already plugged in. I written a simple echo in my script and I don't see it.
echo "inside my script" >> /tmp/hhkb.logThere is a way to "force" the rule to be triggered at boot ?
Offline
I have tested your udev rule with a Kingston USB drive and my hhkb.sh solution. It triggered as expected.
Pls understand that, your udev rule will always been triggered at boot, the delay I have suggested will only delay a portion of the script until Xorg started. Did you set exec bit on your script? can you run your script manually without error?
Last edited by solskog (2020-09-11 01:00:53)
Offline
Why don't you just leave the udev rule for when you plug your device then copy or symlink your script to your autostart directory for when X starts at login? This is easily done with a DM if you're using one.
Offline
Why don't you just leave the udev rule for when you plug your device then copy or symlink your script to your autostart directory for when X starts at login? This is easily done with a DM if you're using one.OP wishes to run the hhkb.sh after X started, Not Before. Therefor I suggested a delay inside this script. If OP wishes to run the script before and after X starts, it's easy to change the script. But, when you run the script two times, without knowning what it's doing could cause problem e.g remapping/reverse keybindings.
Last edited by solskog (2020-09-11 02:09:41)
Offline
OP wishes to run the hhkb.sh after X started, Not Before.
Yes but that's why I asked if the OP if he's using a DM because you can tell the DM to run the script after X is initialised.
Offline
I'd also say just run the thing once during x start and just keep the udev rule to react to appearance/disappearance of the device. Delays inside scripts triggered by udev aren't a good idea, if you don't start xorg "quick" enough udev will kill the blocking/not ending script.
Also what exactly are you doing in your script, can you post it? Maybe udev is the wrong starting point anyway. If you e.g. manipulate xorg input device state it could be easier to set up a relevant configuration snippet as part of a xorg config and let xorg handle it directly.
Last edited by V1del (2020-09-11 07:40:42)
Offline
As I have already tested, systemd-udevd will not kill udev rule that waiting for Xorg to start. It just waiting as expected.
systemd-networkd(4673)--hhkb.sh(4678)--sleep(4684)And once Xorg started, the script also finished as expected.
systemd-networkd(4674)But, If the script is a run away process with exec or '&', systemd-networkd will indeed kill this process after a short time.
I also tried to add script inside xinitrc and rely on DM to trigger the script once login. But, the limitation is when you plug out your USB device and plug in again the script will Not be triggered again.
Last edited by solskog (2020-09-12 05:08:37)
Offline
Okay, it's the time for me to clarify some points and give you more details on what I'm trying to achieve:
I'm on a laptop and I trying to find a way to set automatically a custom keyboard layout (hhkb_fr.xkb) to my second, external keyboard USB wired (not the builtin one).
When I plug in that keyboard, the layout should be applied.
When I boot and the keyboard is already plugged in, the layout should be applied when my WM starts.
Also what exactly are you doing in your script, can you post it? Maybe udev is the wrong starting point anyway. If you e.g. manipulate xorg input device state it could be easier to set up a relevant configuration snippet as part of a xorg config and let xorg handle it directly.
Sure, here is my script:
#!/usr/bin/bash
# This script is called by udev rule /etc/udev/rules.d/99-hhkb.rules
# when the HHKB keyboard is plugged in (USB).
(
id=$(xinput list --id-only "Topre Corporation HHKB Professional");
while [ -z "$id" ]; do
sleep 1
id=$(xinput list --id-only "Topre Corporation HHKB Professional");
done
xkbcomp -i "$id" /home/pierre/.local/share/hhkb/hhkb_fr.xkb "$DISPLAY"
) & # as you can see I detach the process so it does not block the udev executionThe initial problem was the udev rules was not triggered at boot, with the presence of the env variables in the rule:
ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/pierre/.Xauthority"
As suggested by solskog, when I remove this env variable from the rule, it's triggered as expected at boot.
But too early, before Xorg starts.
But as you can see, inside my script I do stuff related to Xorg, so I needed a way to delay the logic after Xorg started.
So the most clean way I found is to make two separates scripts:
One called by my WM when it starts (so after Xorg started).
One called by the udev rule when I plug in my USB keyboard.
So for that I can leave my udev rules unchanged (env variable included) and keep the script I wrote above.
But for the boot part, I simply add an "auto start" script to my WM that is executed when my WM starts:
#!/usr/bin/bash
# This script is called by the WM when it starts.
# It sets the layout for the HHKB if it is plugged in.
id=$(xinput list --id-only "Topre Corporation HHKB Professional");
if [ -n "$id" ]; then
xkbcomp -i "$id" /home/pierre/.local/share/hhkb/hhkb_fr.xkb "$DISPLAY"
fiI would have preferred that everything done in one udev rule and one called script. But it's work well now.
Last edited by doums (2020-09-11 10:27:40)
Offline
very well, combined solutions cover all the cases. happy hacking! ![]()
Offline
Yep, thanks for the help all ! ![]()
Offline