File: apm.c

package info (click to toggle)
apmd 3.0beta5-1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 244 kB
  • ctags: 106
  • sloc: ansic: 1,047; sh: 119; makefile: 109
file content (185 lines) | stat: -rw-r--r-- 5,656 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* apm.c -- Simple APM interface
 * Created: Mon Jan  8 10:28:16 1996 by faith@acm.org
 * Revised: Fri Dec 26 20:34:25 1997 by faith@acm.org
 * Copyright 1996, 1997 Rickard E. Faith (faith@acm.org)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: apm.c,v 1.10 1998/09/06 03:30:20 apenwarr Exp $
 * 
 */

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <getopt.h>
#include "apm.h"

static int verbose = 0;

void change_state( int suspend )
{
   int    fd;
   time_t then, now;
   int    error;

   if ((fd = apm_open()) < 0) {
      fprintf( stderr, "Cannot open APM device: %s\n", strerror( errno ) );
      exit( 1 );
   }
   time( &then );
   if (suspend) error = apm_suspend( fd );
   else         error = apm_standby( fd );
   if (error != 0)
      perror( "apm" );
   else {
      time( &now );

#if 0
/*
 * This currently doesn't work as we should wait for a resume before
 * trying to time this. It is possible for the above calls to return
 * before the machine enters the desired state if some other apm-aware
 * application (e.g., apmd) is running. -- sfr
 */
      if (verbose) {
         printf( "%s for %s\n",
	         suspend ? "Suspended" : "On standby",
	         apm_delta_time( then, now ) );
      }
#endif
   }
   apm_close( fd );
}

void usage( void )
{
   fprintf( stderr,
	    "usage: apm [-vmsS] [--verbose] [--minutes] [--suspend] [--standby]\n" );
   exit( 1 );
}

int main( int argc, char **argv )
{
   apm_info i;
   int      suspend        = 0;
   int      standby        = 0;
   int      debug          = 0;
   int      displayMinutes = 0;
   int      c;
   struct option longopts[] = {
      { "version", 0, 0, 'V' },
      { "verbose", 0, 0, 'v' },
      { "suspend", 0, 0, 's' },
      { "standby", 0, 0, 'S' },
      { "debug",   0, 0, 'd' },
      { "minutes", 0, 0, 'm' },
      { NULL,      0, 0, 0   }
   };
   extern int opterr;

   opterr = 0;
   while ((c = getopt_long( argc, argv, "VvsSdm", longopts, NULL )) != -1)
      switch (c) {
      case 'V':
	 fprintf( stderr, "apm version %s\n", VERSION );
	 exit( 0 );
	 break;
      case 'v': ++verbose;          break;
      case 's': ++suspend;          break;
      case 'S': ++standby;          break;
      case 'd': ++debug; ++verbose; break;
      case 'm': ++displayMinutes;   break;
      default:  usage();            break;
      }

   switch (apm_exists()) {
   case 1: fprintf( stderr, "No APM support in kernel\n" );  exit( 1 );
   case 2: fprintf( stderr, "Old APM support in kernel\n" ); exit( 2 );
   }

   if (suspend && standby) usage();
   if (suspend || standby) {
      change_state( suspend );
      exit( 0 );
   }

   if (apm_read( &i )) {
      fprintf( stderr, "Cannot read APM information\n" );
      exit( 1 );
   }

   if (verbose)
      printf( "APM BIOS %d.%d (kernel driver %s)\n",
	      i.apm_version_major, i.apm_version_minor, i.driver_version );
   
   if (!(i.apm_flags & APM_32_BIT_SUPPORT)) {
      fprintf( stderr, "32-bit APM interface not supported\n" );
      exit( 1 );
   }

   if (verbose && (i.apm_flags & 0x10))
      printf( "APM BIOS Power Management is currently disabled\n" );
   if (verbose && (i.apm_flags & 0x20))
      printf( "APM BIOS Power Management is currently disengaged\n" );
   
   switch (i.ac_line_status) {
   case 0: printf( "AC off-line" );     break;
   case 1: printf( "AC on-line" );      break;
   case 2: printf( "On backup power" ); break;
   }
   if (i.battery_flags != 0xff  /* Have a 1.1 BIOS and a system battery. */
       && i.battery_flags & 0x80) {
      printf( ", no system battery" );
   } else {
				/* Even if battery_flags == 0xff, some 1.0
                                   BIOSs provide this information.  If we
                                   have it, use it. */
      switch (i.battery_status) {
      case 0: printf( ", battery status high" ); break;
      case 1: printf( ", battery status low" ); break;
      case 2: printf( ", battery status critical" ); break;
      case 3: printf( ", battery charging" ); break;
      }
      if (i.battery_percentage >= 0)
	 printf( ": %d%%", i.battery_percentage );
      if (i.battery_time >= 0) {
	 if (displayMinutes) {
	    printf( " (%d %s)",
		    i.battery_time, i.using_minutes ? "min" : "sec" );
	 } else {
	    int seconds = i.using_minutes
			  ? i.battery_time * 60 : i.battery_time;
	       
	    printf( " (%s)", apm_time_nosec( seconds ) );
	 }
      }
   }
   printf( "\n" );

   if (debug) {
      printf( "Using device 0x%04lx, %s: %d.%d\n", (unsigned long)apm_dev(),
 	      i.driver_version, i.apm_version_major, i.apm_version_minor );
      printf( "APM Flags =      0x%02x; AC Line Status = 0x%02x\n",
	      i.apm_flags, i.ac_line_status );
      printf( "Battery Status = 0x%02x; Battery Flags =  0x%02x\n",
	      i.battery_status, i.battery_flags );
      printf( "Battery %%age   = %4d; Battery Time   = %4d, use_mins=%d\n",
 	      i.battery_percentage, i.battery_time, i.using_minutes);
   }

   return 0;
}