Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Start Music On Console Player and Force Active Input via HDMI-CEC
#!/usr/bin/perl
use strict;
use warnings;
use JSON::XS;
# Checking for devices every time takes a few seconds, so store these
# settings in a file. Do so in /tmp so that it will be re-checked on reboot
my $tmp = '/tmp/cec-devices';
my $json = JSON::XS->new();
my $raw = '';
my $settings;
my $address;
# If the devices list already exist, use it.
if ( -e $tmp ) {
open my $config, '<', $tmp;
while (<$config>) {
chomp;
$raw .= $_;
}
close $config;
$settings = $json->decode($raw);
# Else generate device list
} else {
open my $config, '>', $tmp;
my $cec = `echo scan | cec-client -s -d 1`;
my @lines = split(/\n/,$cec);
my $device;
foreach (@lines) {
if ($_ =~ m/^device /) {
$device = $_;
my $name = $_;
$device =~ s/^.*#//;
$device =~ s/:.*$//;
$name =~ s/.*:\s*//;
$settings->{$device}->{name} = $name;
} elsif ($_ =~ m/^\s*$/) {
$device = undef;
next;
} elsif (!defined $device) {
next;
} else {
my $key = $_;
my $value = $_;
$key =~ s/:.*//;
$value =~ s/.*:\s*//;
$settings->{$device}->{$key} = $value;
}
}
print $config $json->encode($settings);
close $config;
}
foreach my $device (keys %$settings) {
if ($settings->{$device}->{vendor} =~ m/Pulse/) {
$address = $settings->{$device}->{address};
$address =~ s/(.)\.(.)\.(.)\.(.)/$1$2:$3$4/;
last;
}
}
system("echo \"tx 5f:82:$address\" | cec-client -s -d 1; mocp -p");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.