Skip to content

Instantly share code, notes, and snippets.

@kulp
Created November 22, 2011 14:39
Show Gist options
  • Save kulp/1385804 to your computer and use it in GitHub Desktop.
Save kulp/1385804 to your computer and use it in GitHub Desktop.
a daemon to respond to events from Sound Blaster Surround 5.1-like devices
#!/usr/bin/env perl
use strict;
use warnings;
use Device::USB;
my ($vendorid, $productid) = (0x041e, 0x30df);
my $usb = Device::USB->new;
my $dev = $usb->find_device($vendorid, $productid)
or die "Failed to find device $vendorid:$productid";
$dev->claim_interface
or $dev->reset && $dev->claim_interface
or die "Failed to claim interface: $!";
$dev->open
or die "Failed to open device: $!";
my %cmds = (
0x29 => "remote power",
0x2b => "remote vol up",
0x2c => "remote vol down",
0x2a => "remote mute",
# XXX dups ?
0x1b => "remote menu",
0x2d => "remote menu",
0x30 => "remote OK",
0x31 => "remote up",
0x27 => "remote left",
0x28 => "remote right",
0x32 => "remote down",
0x3c => "remote repeat",
0x3b => "remote shuffle",
0x1a => "remote return",
0x2f => "remote pause",
# XXX dups ?
0x33 => "remote back",
0x35 => "remote back",
0x34 => "remote forward",
0x36 => "remote forward",
0x0f => "knob left",
0x10 => "knob right",
0x0d => "knob press",
);
sub osify ($) { my $what = shift; sub { qx(osascript -e '$what') } }
my $toggle = osify 'set volume output muted (not output muted of (get volume settings))';
my $voldown = osify 'set volume output volume ((output volume of (get volume settings)) - (100 / 17))';
my $volup = osify 'set volume output volume ((output volume of (get volume settings)) + (100 / 17))';
my %ops = (
"remote mute" => $toggle,
"knob press" => $toggle,
"knob left" => $voldown,
"knob right" => $volup,
);
my $maxlen = 6;
my $buf = "\000" x $maxlen;
while (1) {
my $bytes = $dev->control_msg((0x1 << 5) | 0x01 | 0x80, 0x85, 0, 0, $buf, $maxlen, 100000);
if ($bytes > 0) {
my $cmd = $cmds{ord $buf};
my $op = $ops{$cmd};
print unpack("H*", $buf), "\t", $cmd, "\n";
$op->() if $op;
}
}
$dev->release_interface
or die "Failed to release interface: $!";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment