Arch linux UDEV不能正常工作

时间:2017-09-10 08:46:58

标签: linux udev arch

我想在插入USB鼠标时关闭触控板。

以下是我的规则:

SUBSYSTEMS=="usb", DRIVERS=="usb", ATTRS{manufacturer}=="Logitech", ATTRS{product}=="USB Receiver", ACTION=="add", RUN+="/usr/bin/synclient touchpadoff=1"
SUBSYSTEMS=="usb", DRIVERS=="usb", ATTRS{manufacturer}=="Logitech", ATTRS{product}=="USB Receiver", ACTION=="remove", RUN+="/usr/bin/synclient touchpadoff=0"

但它给了我这样的信息:

Process '/usr/bin/synclient touchpadoff=1' failed with exit code 1.

我不知道上述消息的含义。

帮助我。

1 个答案:

答案 0 :(得分:1)

它不起作用,因为命令" / usr / bin / synclient touchpadoff = 1" udev规则由另一个用户运行,而不是在Xorg会话中使用auth。

例如,如果root run" / usr / bin / synclient touchpadoff = 1" 我们收到了一个错误:

X11 connection rejected because of wrong authentication.
Failed to connect to X Server.

你应该添加" udev monitor"桌面环境(link)中的启动/自动启动应用程序中的脚本,或由当前X授权用户运行。

我为我的鼠标编写了udev脚本。我的鼠标有VID 046d和PID c06d - 您应该用自己的鼠标替换它们。您还需要安装Udev::FFI模块(运行" cpanm Udev :: FFI"或" cpan Udev :: FFI")

#!/usr/bin/perl
# ATTENTION place it script to startup/autostart applications in your desktop
# environment or run by current x-session user.

use strict;
use warnings;

use Udev::FFI;


use constant {
    MOUSES => [{
        VID => '046d',
        PID => 'c06d'
    }],

    ON_TOUCHPAD_COMMAND => '/usr/bin/synclient touchpadoff=0',
    OFF_TOUCHPAD_COMMAND => '/usr/bin/synclient touchpadoff=1'
};


my %inserted_mouses;



my $udev = Udev::FFI->new() or
    die "Can't create udev context: $@.\n";


# monitor for new devices
my $monitor = $udev->new_monitor() or
    die "Can't create udev monitor: $@.\n";

$monitor->filter_by_subsystem_devtype('usb', 'usb_device');

# start monitor before enumerate to catch devices inserted between enumerate and
# $monitor->poll()
$monitor->start() or
    die "Can't start udev monitor :(\n";


# check already inserted devices
my $enumerate = $udev->new_enumerate() or
    die "Can't create enumerate context: $@.\n";

$enumerate->add_match_subsystem('usb');
# some versions of libudev work incorrectly with $enumerate->add_match_sysattr('idVendor', $vid);
$enumerate->add_match_sysattr('idVendor');
$enumerate->add_match_sysattr('idProduct');
$enumerate->scan_devices();

my @inserted_devices = $enumerate->get_list_entries();
for(@inserted_devices) {
    my $device = $udev->new_device_from_syspath($_);
    my $device_vid = $device->get_sysattr_value("idVendor");
    my $device_pid = $device->get_sysattr_value("idProduct");

    for(@{+MOUSES}) {
        if($device_vid eq $_->{VID} && $device_pid eq $_->{PID}) {
            $inserted_mouses{ $device->get_devpath() } = 1;
            last;
        }
    }
}

# known mouses > 0
if(%inserted_mouses) {
    system(OFF_TOUCHPAD_COMMAND);
}


for(;;) {
    my $device = $monitor->poll(); # blocking read
    my $action = $device->get_action();
    my $device_vid = $device->get_sysattr_value("idVendor");
    my $device_pid = $device->get_sysattr_value("idProduct");

    if($action eq 'add' && defined($device_vid) && defined($device_pid)) {
        for(@{+MOUSES}) {
            if($device_vid eq $_->{VID} && $device_pid eq $_->{PID}) {
                system(OFF_TOUCHPAD_COMMAND)
                    unless %inserted_mouses;

                $inserted_mouses{ $device->get_devpath() } = 1;
                last;
            }
        }
    }
    elsif($action eq 'remove') {
        delete $inserted_mouses{ $device->get_devpath() };

        # known mouses == 0
        unless(%inserted_mouses) {
            system(ON_TOUCHPAD_COMMAND);
        }
    }
}