5

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.

Community
  • 1
  • 1

3 Answers3

1

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
...
ilux
  • 347
  • 4
  • 9
0

No, udev does not send D-Bus events on its own. Programs such as Xorg, PuslseAudio, and udisks directly monitor uevents (some of which come from the kernel and some of which are generated by udev). For many uevents there is nothing which reflects them onto D-Bus.

udevadm monitor will print out a stream of uevents. This is easy to read as a pipe in Perl. For example,

open my $udev, '-|', qw(udevadm monitor);
while (<$udev>) {
    my ($source, $ts, $action, $dev, $sys) = split;
    if ($action eq 'add') {
        # etc.

However, Bluetooth is handled via BlueZ on most distros, and BlueZ provides a D-Bus interface. For example, you could monitor the org.bluez.Device1.Connected property of the /org/bluez/hciX/dev_XX_XX_XX_XX_XX_XX object on the well-known system bus name org.bluez, if you're interested in a specific device.

ephemient
  • 198,619
  • 38
  • 280
  • 391
-1

Did you checked Device::USB?

http://metacpan.org/pod/Device::USB

You could use it check for your device in a loop.

It contains examples to start with: https://metacpan.org/pod/distribution/Device-USB/dump_usb.pl

Regards,

szabgab
  • 6,202
  • 11
  • 50
  • 64
user1126070
  • 5,059
  • 1
  • 16
  • 15
  • Thanks for the tip. Finally got Device::USB installed, though it just throws segmentation faults at me. If I got it to work, would that actually catch all devices? Or just USB devices? – von Bregelsaum Mar 01 '13 at 08:32
  • I think, it will catch USB only. Sorry, I thought your device is USB. – user1126070 Mar 01 '13 at 13:17