ac_adapt.pl 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/perl -w
  2. # AC Power Handler v1.0
  3. # Handles AC power events for Panasonic notebooks
  4. #
  5. # Copyright (C) 2004 David Bronaugh
  6. #
  7. # Requires pcc_acpi driver
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License version 2 as
  11. # published by the Free Software Foundation
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. use strict;
  22. use POSIX qw(ceil floor);
  23. our($config);
  24. our($power_state);
  25. sub read_file {
  26. my($file) = @_;
  27. my($fh);
  28. my($contents) = "";
  29. if(open($fh, $file)) {
  30. $/ = undef;
  31. $contents = <$fh>;
  32. close($fh);
  33. } else {
  34. print "Couldn't open file " . $file . "!\n";
  35. }
  36. return $contents;
  37. }
  38. sub write_file {
  39. my($file, $contents) = @_;
  40. my($fh);
  41. if(open($fh, ">", $file)) {
  42. print $fh $contents;
  43. close($fh);
  44. return 1;
  45. } else {
  46. print "Couldn't open file " . $file . "!\n";
  47. return 0;
  48. }
  49. }
  50. sub get_pcc_field {
  51. my($field) = @_;
  52. my($file) = $config->{'pcc_path'} . "/" . $power_state . "_" . $field;
  53. return read_file($file);
  54. }
  55. sub set_pcc_field {
  56. my($field, $contents) = @_;
  57. my($file) = $config->{'pcc_path'} . "/" . $power_state . "_" . $field;
  58. if(!write_file($file, $contents)) {
  59. print "Couldn't set pcc " . $field . " field (are you root?)\n";
  60. return 0;
  61. }
  62. return 1;
  63. }
  64. sub ac_disconnect {
  65. $power_state = "dc";
  66. set_pcc_field("brightness", get_pcc_field("brightness"));
  67. }
  68. sub ac_connect {
  69. $power_state = "ac";
  70. set_pcc_field("brightness", get_pcc_field("brightness"));
  71. }
  72. my($key) = $ARGV[3];
  73. my(%dispatch) = (
  74. "00000000" => \&ac_disconnect,
  75. "00000001" => \&ac_connect,
  76. );
  77. $config = {
  78. "pcc_path" => "/proc/acpi/pcc",
  79. };
  80. $dispatch{$key}();