Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can my perl script react to arbitrary devices using UDev and not HAL?

I'd like to use a simple Perl script to make some configurations every time I connect e.g. my bluetooth headset. I tried using Net::DBus, but my OS/DE (Fedora 17, GNOME3) doesn't use HAL anymore.

I really don't want to install HAL just for this, so what do I do? My ideas so far:

  • (Preferred): Use DBus; simply listen to UDev events instead of org.freedesktop.Hal. Problem: I cannot find the corresponding service, org.freedesktop.UDisks only seems to monitor disks (duh). Does UDev even send DBus messages for other devices and if not, can I configure it to do so?
  • Use an UDev rule to trigger another script. I like to have my scripts in one place for easy transition to new OS installs, so I'd rather avoid that.
  • Am I better off just using Python to listen directly to UDev?
  • Or can I use Perl to do just that? A CPAN search for "udev" didn't yield anything helpful.

Or I may be completely off and UDev isn't even what I need. Neither the docs nor Google were really helpful regarding that matter. A workaround would be if anyone knows how to get GNOME3 to switch audio output to a newly connected bluetooth headset per default, but I'd like to learn scripting stuff like that anyway.

Thanks in advance for any pointers!
A.

PS: By the way, Google&Co. claim that UDev gets the devices and sends a message to HAL, which in turn notifies DBus. That is most definitely not the case since HAL isn't even in the Fedora Repos aymore.

like image 767
von Bregelsaum Avatar asked Nov 25 '22 04:11

von Bregelsaum


1 Answers

You can use Udev::FFI (cpanm Udev::FFI)

For example:

use Udev::FFI;

my $udev = Udev::FFI->new() or
    die "Can't create udev context.";
my $monitor = $udev->new_monitor() or
    die "Can't create udev monitor.";
$monitor->filter_by_subsystem_devtype('usb', 'usb_device');
$monitor->start() or die "Can't start monitor.";

for(;;) {
    if(defined(my $device = $monitor->poll())) {
        my $action = $device->get_action();
        if($action eq 'add') {
            #work with $device
...
like image 132
ilux Avatar answered Jan 15 '23 00:01

ilux