Blame samples/panasonic/ac_adapt.pl

Packit a94d48
#!/usr/bin/perl -w
Packit a94d48
# AC Power Handler v1.0
Packit a94d48
# Handles AC power events for Panasonic notebooks
Packit a94d48
#
Packit a94d48
# Copyright (C) 2004 David Bronaugh
Packit a94d48
#
Packit a94d48
# Requires pcc_acpi driver
Packit a94d48
#
Packit a94d48
# This program is free software; you can redistribute it and/or modify 
Packit a94d48
# it under the terms of the GNU General Public License version 2 as 
Packit a94d48
# published by the Free Software Foundation
Packit a94d48
#
Packit a94d48
# This program is distributed in the hope that it will be useful,
Packit a94d48
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit a94d48
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit a94d48
# GNU General Public License for more details.
Packit a94d48
#
Packit a94d48
# You should have received a copy of the GNU General Public License
Packit a94d48
# along with this program; if not, write to the Free Software
Packit a94d48
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Packit a94d48
Packit a94d48
use strict;
Packit a94d48
use POSIX qw(ceil floor);
Packit a94d48
Packit a94d48
our($config);
Packit a94d48
our($power_state);
Packit a94d48
Packit a94d48
sub read_file {
Packit a94d48
  my($file) = @_;
Packit a94d48
  my($fh);
Packit a94d48
  my($contents) = "";
Packit a94d48
  if(open($fh, $file)) {
Packit a94d48
    $/ = undef;
Packit a94d48
    $contents = <$fh>;
Packit a94d48
    close($fh);
Packit a94d48
  } else {
Packit a94d48
    print "Couldn't open file " . $file . "!\n";
Packit a94d48
  }
Packit a94d48
  return $contents;
Packit a94d48
}
Packit a94d48
Packit a94d48
sub write_file {
Packit a94d48
  my($file, $contents) = @_;
Packit a94d48
  my($fh);
Packit a94d48
Packit a94d48
  if(open($fh, ">", $file)) {
Packit a94d48
    print $fh $contents;
Packit a94d48
    close($fh);
Packit a94d48
    return 1;
Packit a94d48
  } else {
Packit a94d48
    print "Couldn't open file " . $file . "!\n";
Packit a94d48
    return 0;
Packit a94d48
  }
Packit a94d48
}
Packit a94d48
Packit a94d48
sub get_pcc_field {
Packit a94d48
  my($field) = @_;
Packit a94d48
  my($file) = $config->{'pcc_path'} . "/" . $power_state . "_" . $field;
Packit a94d48
Packit a94d48
  return read_file($file);
Packit a94d48
}
Packit a94d48
Packit a94d48
sub set_pcc_field {
Packit a94d48
  my($field, $contents) = @_;
Packit a94d48
  my($file) = $config->{'pcc_path'} . "/" . $power_state . "_" . $field;
Packit a94d48
Packit a94d48
  if(!write_file($file, $contents)) {
Packit a94d48
    print "Couldn't set pcc " . $field . " field (are you root?)\n";
Packit a94d48
    return 0;
Packit a94d48
  }
Packit a94d48
  return 1;
Packit a94d48
}
Packit a94d48
Packit a94d48
sub ac_disconnect {
Packit a94d48
  $power_state = "dc";
Packit a94d48
  set_pcc_field("brightness", get_pcc_field("brightness"));
Packit a94d48
}
Packit a94d48
Packit a94d48
sub ac_connect {
Packit a94d48
  $power_state = "ac";
Packit a94d48
  set_pcc_field("brightness", get_pcc_field("brightness"));
Packit a94d48
}
Packit a94d48
Packit a94d48
my($key) = $ARGV[3];
Packit a94d48
Packit a94d48
my(%dispatch) = (
Packit a94d48
	     "00000000" => \&ac_disconnect,
Packit a94d48
	     "00000001" => \&ac_connect,
Packit a94d48
	    );
Packit a94d48
Packit a94d48
$config = {
Packit a94d48
	       "pcc_path" => "/proc/acpi/pcc",
Packit a94d48
	      };
Packit a94d48
Packit a94d48
$dispatch{$key}();