|
Packit Service |
26469c |
#!/usr/bin/perl -w
|
|
Packit Service |
26469c |
# Hotkey handler v1.0
|
|
Packit Service |
26469c |
# Handles hotkey events for Panasonic notebooks
|
|
Packit Service |
26469c |
#
|
|
Packit Service |
26469c |
# Copyright (C) 2004 David Bronaugh
|
|
Packit Service |
26469c |
#
|
|
Packit Service |
26469c |
# This program is free software; you can redistribute it and/or modify
|
|
Packit Service |
26469c |
# it under the terms of the GNU General Public License version 2 as
|
|
Packit Service |
26469c |
# published by the Free Software Foundation
|
|
Packit Service |
26469c |
#
|
|
Packit Service |
26469c |
# This program is distributed in the hope that it will be useful,
|
|
Packit Service |
26469c |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
26469c |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
26469c |
# GNU General Public License for more details.
|
|
Packit Service |
26469c |
#
|
|
Packit Service |
26469c |
# You should have received a copy of the GNU General Public License
|
|
Packit Service |
26469c |
# along with this program; if not, write to the Free Software
|
|
Packit Service |
26469c |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
use strict;
|
|
Packit Service |
26469c |
use POSIX qw(ceil floor);
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
our($config);
|
|
Packit Service |
26469c |
our($power_state);
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
sub read_file {
|
|
Packit Service |
26469c |
my($file) = @_;
|
|
Packit Service |
26469c |
my($fh);
|
|
Packit Service |
26469c |
my($contents) = "";
|
|
Packit Service |
26469c |
if(open($fh, $file)) {
|
|
Packit Service |
26469c |
$/ = undef;
|
|
Packit Service |
26469c |
$contents = <$fh>;
|
|
Packit Service |
26469c |
close($fh);
|
|
Packit Service |
26469c |
} else {
|
|
Packit Service |
26469c |
print "Couldn't open file " . $file . "!\n";
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
return $contents;
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
sub write_file {
|
|
Packit Service |
26469c |
my($file, $contents) = @_;
|
|
Packit Service |
26469c |
my($fh);
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
if(open($fh, ">", $file)) {
|
|
Packit Service |
26469c |
print $fh $contents;
|
|
Packit Service |
26469c |
close($fh);
|
|
Packit Service |
26469c |
return 1;
|
|
Packit Service |
26469c |
} else {
|
|
Packit Service |
26469c |
print "Couldn't open file " . $file . "!\n";
|
|
Packit Service |
26469c |
return 0;
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
sub get_amixer_control_info {
|
|
Packit Service |
26469c |
my($control) = @_;
|
|
Packit Service |
26469c |
my($cmd) = $config->{'mixer_program'} . " cget name='" . $control . "'";
|
|
Packit Service |
26469c |
my(%info);
|
|
Packit Service |
26469c |
my($fh, $field);
|
|
Packit Service |
26469c |
my($contents) = "";
|
|
Packit Service |
26469c |
if(open($fh, $cmd . "|")) {
|
|
Packit Service |
26469c |
while(<$fh>) {
|
|
Packit Service |
26469c |
chomp;
|
|
Packit Service |
26469c |
$contents .= $_;
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
} else {
|
|
Packit Service |
26469c |
print "Couldn't run command " . $cmd . "!\n";
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
$contents =~ m/\; ([^\s]*)/;
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
foreach(split(/,/, $+)) {
|
|
Packit Service |
26469c |
my(@foo) = split(/=/, $_);
|
|
Packit Service |
26469c |
$info{$foo[0]} = $foo[1];
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
$contents =~ m/\: ([^\s]*)/;
|
|
Packit Service |
26469c |
my(@foo) = split(/=/, $+);
|
|
Packit Service |
26469c |
$info{$foo[0]} = [];
|
|
Packit Service |
26469c |
@{$info{$foo[0]}} = split(/,/, $foo[1]);
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
return \%info;
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
sub set_amixer_control_info {
|
|
Packit Service |
26469c |
my($control, $values) = @_;
|
|
Packit Service |
26469c |
my($cmd) = $config->{'mixer_program'} . " -q cset name='" . $control . "' " . $values;
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
if(system($cmd) == 0) {
|
|
Packit Service |
26469c |
return 1;
|
|
Packit Service |
26469c |
} else {
|
|
Packit Service |
26469c |
return 0;
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
sub get_pcc_field {
|
|
Packit Service |
26469c |
my($field) = @_;
|
|
Packit Service |
26469c |
my($file) = $config->{'pcc_path'} . "/" . $power_state . "_" . $field;
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
return read_file($file);
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
sub set_pcc_field {
|
|
Packit Service |
26469c |
my($field, $contents) = @_;
|
|
Packit Service |
26469c |
my($file) = $config->{'pcc_path'} . "/" . $power_state . "_" . $field;
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
if(!write_file($file, $contents)) {
|
|
Packit Service |
26469c |
print "Couldn't set pcc " . $field . " field (are you root?)\n";
|
|
Packit Service |
26469c |
return 0;
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
return 1;
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
sub get_brightness {
|
|
Packit Service |
26469c |
return (get_pcc_field("brightness_min"), get_pcc_field("brightness_max"), get_pcc_field("brightness"));
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
sub set_brightness {
|
|
Packit Service |
26469c |
my($value) = @_;
|
|
Packit Service |
26469c |
return set_pcc_field("brightness", $value);
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
sub get_mute {
|
|
Packit Service |
26469c |
my($info) = get_amixer_control_info($config->{'mute_switch'});
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
if($info->{'values'}[0] eq "on") {
|
|
Packit Service |
26469c |
return 0;
|
|
Packit Service |
26469c |
} elsif($info->{'values'}[0] eq "off") {
|
|
Packit Service |
26469c |
return 1;
|
|
Packit Service |
26469c |
} else {
|
|
Packit Service |
26469c |
print "Error getting mute status!\n";
|
|
Packit Service |
26469c |
return -1;
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
sub set_mute {
|
|
Packit Service |
26469c |
my($value) = @_;
|
|
Packit Service |
26469c |
if($value == 0) {
|
|
Packit Service |
26469c |
$value = "on";
|
|
Packit Service |
26469c |
} elsif($value == 1) {
|
|
Packit Service |
26469c |
$value = "off";
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
if(set_amixer_control_info($config->{'mute_switch'}, $value)) {
|
|
Packit Service |
26469c |
return 1;
|
|
Packit Service |
26469c |
} else {
|
|
Packit Service |
26469c |
print "Couldn't set mute status!\n";
|
|
Packit Service |
26469c |
return 0;
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
sub get_volume {
|
|
Packit Service |
26469c |
my($config) = @_;
|
|
Packit Service |
26469c |
my($info) = get_amixer_control_info($config->{'volume_ctl'});
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
return ($info->{'min'}, $info->{'max'}, $info->{'values'});
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
sub set_volume {
|
|
Packit Service |
26469c |
my($values) = @_;
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
return set_amixer_control_info($config->{'volume_ctl'}, join(",", @{$values}));
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
sub get_power_state {
|
|
Packit Service |
26469c |
my($data) = read_file($config->{"ac_state"});
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
if($data =~ /on-line/) {
|
|
Packit Service |
26469c |
return "ac";
|
|
Packit Service |
26469c |
} elsif($data =~ /off-line/) {
|
|
Packit Service |
26469c |
return "dc";
|
|
Packit Service |
26469c |
} else {
|
|
Packit Service |
26469c |
print "Couldn't get power state! (is ACPI enabled?)\n";
|
|
Packit Service |
26469c |
exit(1);
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
sub adjust_brightness {
|
|
Packit Service |
26469c |
my($adjust) = @_;
|
|
Packit Service |
26469c |
my($min, $max, $bright) = get_brightness($config);
|
|
Packit Service |
26469c |
my($threshold) = $config->{'max_bright_levels'};
|
|
Packit Service |
26469c |
my($divisor) = 1;
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
$bright -= $min;
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
if($max - $min > $threshold) {
|
|
Packit Service |
26469c |
$divisor = ($max - $min) / $threshold;
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
$bright = ceil($bright / $divisor);
|
|
Packit Service |
26469c |
$bright += $adjust;
|
|
Packit Service |
26469c |
$bright = floor($bright * $divisor);
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
$bright += $min;
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
if($bright < $min) {
|
|
Packit Service |
26469c |
$bright = $min;
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
if($bright > $max) {
|
|
Packit Service |
26469c |
$bright = $max;
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
if(!set_brightness($bright)) {
|
|
Packit Service |
26469c |
print "Couldn't adjust brightness!\n";
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
return;
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
sub adjust_volume {
|
|
Packit Service |
26469c |
my($increment) = @_;
|
|
Packit Service |
26469c |
my($min, $max, $volume) = get_volume($config);
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
$volume->[0] += $increment;
|
|
Packit Service |
26469c |
$volume->[1] += $increment;
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
$volume->[0] = ($volume->[0] < $min)?$min:$volume->[0];
|
|
Packit Service |
26469c |
$volume->[1] = ($volume->[1] < $min)?$min:$volume->[1];
|
|
Packit Service |
26469c |
$volume->[0] = ($volume->[0] > $max)?$max:$volume->[0];
|
|
Packit Service |
26469c |
$volume->[1] = ($volume->[1] > $max)?$max:$volume->[1];
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
if(!set_volume($volume)) {
|
|
Packit Service |
26469c |
print "Couldn't set volume!\n";
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
return;
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
# Functions which implement hotkey functions directly
|
|
Packit Service |
26469c |
sub down_brightness {
|
|
Packit Service |
26469c |
adjust_brightness(-1);
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
sub up_brightness {
|
|
Packit Service |
26469c |
adjust_brightness(1);
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
sub switch_monitor {
|
|
Packit Service |
26469c |
#STUB
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
sub toggle_mute {
|
|
Packit Service |
26469c |
my($mute) = get_mute();
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
if($mute >= 0) {
|
|
Packit Service |
26469c |
set_mute($mute ^ 1);
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
sub volume_up {
|
|
Packit Service |
26469c |
adjust_volume($config->{"volume_increment"})
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
sub volume_down {
|
|
Packit Service |
26469c |
adjust_volume(-1 * $config->{"volume_increment"})
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
sub suspend_to_ram {
|
|
Packit Service |
26469c |
# This space intentionally left blank (because it doesn't work here)
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
sub spin_down_hd {
|
|
Packit Service |
26469c |
if(system("hdparm -q -y /dev/hda") != 0) {
|
|
Packit Service |
26469c |
print "Error running hdparm -- is it installed?\n";
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
sub suspend_to_disk {
|
|
Packit Service |
26469c |
system("hwclock --systohc");
|
|
Packit Service |
26469c |
write_file($config->{'suspend_control'}, "disk");
|
|
Packit Service |
26469c |
system("hwclock --hctosys");
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
my($key) = $ARGV[3];
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
my(%dispatch) = (
|
|
Packit Service |
26469c |
"00000081" => \&down_brightness,
|
|
Packit Service |
26469c |
"00000082" => \&up_brightness,
|
|
Packit Service |
26469c |
"00000003" => \&switch_monitor,
|
|
Packit Service |
26469c |
"00000084" => \&toggle_mute,
|
|
Packit Service |
26469c |
"00000085" => \&volume_down,
|
|
Packit Service |
26469c |
"00000086" => \&volume_up,
|
|
Packit Service |
26469c |
"00000007" => \&suspend_to_ram,
|
|
Packit Service |
26469c |
"00000089" => \&spin_down_hd,
|
|
Packit Service |
26469c |
"0000000a" => \&suspend_to_disk
|
|
Packit Service |
26469c |
);
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
$config = {
|
|
Packit Service |
26469c |
"pcc_path" => "/proc/acpi/pcc",
|
|
Packit Service |
26469c |
"mixer_program" => "amixer",
|
|
Packit Service |
26469c |
"ac_state" => "/proc/acpi/ac_adapter/AC/state",
|
|
Packit Service |
26469c |
"mute_switch" => "Master Playback Switch",
|
|
Packit Service |
26469c |
"volume_ctl" => "Master Playback Volume",
|
|
Packit Service |
26469c |
"max_bright_levels" => 20,
|
|
Packit Service |
26469c |
"volume_increment" => 2,
|
|
Packit Service |
26469c |
"suspend_control" => "/sys/power/state"
|
|
Packit Service |
26469c |
};
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
$power_state = get_power_state();
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
$dispatch{$key}();
|