Skip to content

Instantly share code, notes, and snippets.

@JohnMertz
Last active June 18, 2019 22:07
Show Gist options
  • Save JohnMertz/93e47ebd92608339bd27245ae2e082c5 to your computer and use it in GitHub Desktop.
Save JohnMertz/93e47ebd92608339bd27245ae2e082c5 to your computer and use it in GitHub Desktop.
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