~ubuntu-branches/ubuntu/maverick/acpi/maverick

« back to all changes in this revision

Viewing changes to main.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2008-06-23 10:11:54 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20080623101154-s08e43i3cjuoal01
Tags: 1.1-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Allow acpi to build on lpia as well.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
1
/* provides a simple client program that reads ACPI status from the /proc 
3
2
 * filesystem
4
3
 *
5
4
 * Copyright (C) 2001  Grahame Bowland <grahame@angrygoats.net>
 
5
 * Copyright (C) 2008  Michael Meskes <meskes@debian.org>
6
6
 *
7
7
 *  This program is free software; you can redistribute it and/or modify
8
8
 *  it under the terms of the GNU General Public License as published by
25
25
#include <getopt.h>
26
26
#include "acpi.h"
27
27
 
28
 
static void do_show_batteries(char *acpi_path, int show_empty_slots)/*{{{*/
 
28
struct device device[4] = {
 
29
                        { BATTERY, "battery", "power_supply", "BAT" },
 
30
                        { AC_ADAPTER, "ac_adapter", "power_supply", "AC" },
 
31
                        { THERMAL_ZONE, "thermal_zone", "thermal", "thermal_zone" },
 
32
                        { COOLING_DEV, "fan", "thermal", "cooling_device" }
 
33
                          };
 
34
 
 
35
static void do_show_batteries(char *acpi_path, int show_empty_slots, int proc_interface)
29
36
{
30
37
        struct list *batteries;
31
38
 
32
 
        batteries = find_devices(acpi_path, "battery", TRUE);
 
39
        batteries = find_devices(acpi_path, BATTERY, proc_interface);
33
40
        print_battery_information(batteries, show_empty_slots);
34
41
        free_devices(batteries);
35
42
}
36
43
 
37
 
static void do_show_thermal(char *acpi_path, int show_empty_slots, int temperature_units) {/*{{{*/
 
44
static void do_show_ac_adapter(char *acpi_path, int show_empty_slots, int proc_interface)
 
45
{
 
46
        struct list *ac_adapter;
 
47
 
 
48
        ac_adapter = find_devices(acpi_path, AC_ADAPTER, proc_interface);
 
49
        print_ac_adapter_information(ac_adapter, show_empty_slots);
 
50
        free_devices(ac_adapter);
 
51
}
 
52
 
 
53
static void do_show_thermal(char *acpi_path, int show_empty_slots, int temperature_units, int proc_interface) {
38
54
        struct list *thermal;
39
 
        thermal = find_devices(acpi_path, "thermal_zone", FALSE);
40
 
        if (!thermal) {
41
 
                /* old acpi directory structure */
42
 
                thermal = find_devices(acpi_path, "thermal", TRUE); 
43
 
        }
 
55
 
 
56
        thermal = find_devices(acpi_path, THERMAL_ZONE, proc_interface);
44
57
        print_thermal_information(thermal, show_empty_slots, temperature_units);
45
58
        free_devices(thermal);
46
59
}
47
60
 
48
 
static void do_show_ac_adapter(char *acpi_path, int show_empty_slots)/*{{{*/
49
 
{
50
 
        struct list *ac_adapter;
51
 
        ac_adapter = find_devices(acpi_path, "ac_adapter", TRUE);
52
 
        print_ac_adapter_information(ac_adapter, show_empty_slots);
53
 
        free_devices(ac_adapter);
 
61
static void do_show_cooling(char *acpi_path, int show_empty_slots, int proc_interface) {
 
62
        struct list *cooling;
 
63
 
 
64
        cooling = find_devices(acpi_path, COOLING_DEV, proc_interface);
 
65
        print_cooling_information(cooling, show_empty_slots);
 
66
        free_devices(cooling);
54
67
}
55
68
 
56
 
static int version(void)/*{{{*/
 
69
static int version(void)
57
70
{
58
71
        printf(ACPI_VERSION_STRING "\n"
59
72
"\n"
60
73
"Copyright (C) 2001 Grahame Bowland.\n"
 
74
"              2008 Michael Meskes.\n"
61
75
"This is free software; see the source for copying conditions.  There is NO\n"
62
76
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
63
77
);
64
78
        return 1;
65
79
}
66
80
 
67
 
static int usage(char *argv[])/*{{{*/
 
81
static int usage(char *argv[])
68
82
{
69
83
        printf(
70
84
"Usage: acpi [OPTION]...\n"
71
85
"Shows information from the /proc filesystem, such as battery status or\n"
72
86
"thermal information.\n"
73
87
"\n"
74
 
"  -b, --battery                                battery information\n"
 
88
"  -b, --battery                        battery information\n"
75
89
"  -B, --without-battery                suppress battery information\n"
76
 
"  -t, --thermal                                thermal information\n"
 
90
"  -a, --ac-adapter                     ac adapter information\n"
 
91
"  -A, --without-ac-adapter             suppress ac-adapter information\n"
 
92
"  -t, --thermal                        thermal information\n"
77
93
"  -T, --without-thermal                suppress thermal information\n"
78
 
"  -a, --ac-adapter                      ac adapter information\n"
79
 
"  -A, --without-ac-adapter      suppress ac-adapter information\n"
80
 
"  -V, --everything                      show every device, overrides above options\n"
81
 
"  -s, --show-empty                      show non-operational devices\n"
82
 
"  -S, --hide-empty                      hide non-operational devices\n"
83
 
"  -c, --celsius                                use celsius as the temperature unit\n"
84
 
"  -f, --fahrenheit                      use fahrenheit as the temperature unit\n"
85
 
"  -k, --kelvin                          use kelvin as the temperature unit\n"
86
 
"  -d, --directory <dir>                path to ACPI info (/proc/acpi)\n"
87
 
"  -h, --help                              display this help and exit\n"
88
 
"  -v, --version                                output version information and exit\n"
 
94
"  -c, --cooling                        cooling information\n"
 
95
"  -C, --without-cooling                suppress cooling information\n"
 
96
"  -V, --everything                     show every device, overrides above options\n"
 
97
"  -s, --show-empty                     show non-operational devices\n"
 
98
"  -S, --hide-empty                     hide non-operational devices\n"
 
99
"  -f, --fahrenheit                     use fahrenheit as the temperature unit\n"
 
100
"  -k, --kelvin                 use kelvin as the temperature unit\n"
 
101
"  -d, --directory <dir>                path to ACPI info (/sys/class resp. /proc/acpi)\n"
 
102
"  -p, --proc                           use old proc interface instead of new sys interface\n"
 
103
"  -h, --help                           display this help and exit\n"
 
104
"  -v, --version                        output version information and exit\n"
89
105
"\n"
90
106
"By default, acpi displays information on installed system batteries.\n"
91
107
"Non-operational devices, for example empty battery slots are hidden.\n"
92
108
"The default unit of temperature is degrees celsius.\n"
93
109
"\n"
94
 
"Report bugs to Grahame Bowland <grahame@angrygoats.net>.\n"
 
110
"Report bugs to Michael Meskes <meskes@debian.org>.\n"
95
111
);
96
112
        return 1;
97
113
}
98
114
 
99
 
static struct option long_options[] = {/*{{{*/
 
115
static struct option long_options[] = {
100
116
        { "help", 0, 0, 'h' }, 
101
117
        { "version", 0, 0, 'v' }, 
102
118
        { "verbose", 0, 0, 'V' }, 
103
119
        { "battery", 0, 0, 'b' }, 
104
120
        { "without-battery", 0, 0, 'B' }, 
 
121
        { "ac-adapter", 0, 0, 'a' }, 
 
122
        { "without-ac-adapter", 0, 0, 'A' }, 
105
123
        { "thermal", 0, 0, 't' }, 
106
124
        { "without-thermal", 0, 0, 'T' }, 
107
 
        { "ac-adapter", 0, 0, 'a' }, 
108
 
        { "without-ac-adapter", 0, 0, 'A' }, 
 
125
        { "cooling", 0, 0, 'c' }, 
 
126
        { "without-cooling", 0, 0, 'C' }, 
109
127
        { "show-empty", 0, 0, 's' }, 
110
128
        { "hide-empty", 0, 0, 'S' }, 
111
 
        { "celcius", 0, 0, 'c' }, 
112
 
        { "celsius", 0, 0, 'c' }, 
113
129
        { "fahrenheit", 0, 0, 'f' }, 
114
130
        { "kelvin", 0, 0, 'k' }, 
115
131
        { "directory", 1, 0, 'd' },
116
132
        { "everything", 0, 0, 'V' }, 
 
133
        { "proc", 0, 0, 'p' }, 
117
134
        { 0, 0, 0, 0 }, 
118
135
};
119
136
 
120
 
int main(int argc, char *argv[])/*{{{*/
 
137
int main(int argc, char *argv[])
121
138
{
122
 
        int show_everything = 0;
123
 
        int show_batteries = 1;
124
 
        int show_thermal = 0;
125
 
        int show_ac_adapter = 0;
126
 
        int show_empty_slots = 0;
 
139
        int show_batteries = TRUE;
 
140
        int show_ac_adapter = FALSE;
 
141
        int show_thermal = FALSE;
 
142
        int show_cooling = FALSE;
 
143
        int show_empty_slots = FALSE;
 
144
        int proc_interface = FALSE;
127
145
        int temperature_units = TEMP_CELSIUS;
128
146
        int ch, option_index;
129
 
        char *acpi_path = strdup(ACPI_PATH);
 
147
        char *acpi_path = strdup(ACPI_PATH_SYS);
130
148
 
131
149
        if (!acpi_path) {
132
150
                fprintf(stderr, "Out of memory in main()\n");
133
151
                return -1;
134
152
        }
135
153
 
136
 
        while ((ch = getopt_long(argc, argv, "VbBtTaAsShvfkcd:", long_options, &option_index)) != -1) {
 
154
        while ((ch = getopt_long(argc, argv, "pVbBtTaAsShvfkcCd:", long_options, &option_index)) != -1) {
137
155
                switch (ch) {
138
156
                        case 'V':
139
 
                                show_everything = 1;
 
157
                                show_batteries = show_ac_adapter = show_thermal = show_cooling = TRUE;
140
158
                                break;
141
159
                        case 'b':
142
 
                                show_batteries = 1;
 
160
                                show_batteries = TRUE;
143
161
                                break;
144
162
                        case 'B':
145
 
                                show_batteries = 0;
 
163
                                show_batteries = FALSE;
 
164
                                break;
 
165
                        case 'a':
 
166
                                show_ac_adapter = TRUE;
 
167
                                break;
 
168
                        case 'A':
 
169
                                show_ac_adapter = FALSE;
146
170
                                break;
147
171
                        case 't':
148
 
                                show_thermal = 1;
 
172
                                show_thermal = TRUE;
149
173
                                break;
150
174
                        case 'T':
151
 
                                show_thermal = 0;
152
 
                                break;
153
 
                        case 'a':
154
 
                                show_ac_adapter = 1;
155
 
                                break;
156
 
                        case 'A':
157
 
                                show_ac_adapter = 0;
 
175
                                show_thermal = FALSE;
 
176
                                break;
 
177
                        case 'c':
 
178
                                show_cooling = TRUE;
 
179
                                break;
 
180
                        case 'C':
 
181
                                show_cooling = FALSE;
158
182
                                break;
159
183
                        case 's':
160
 
                                show_empty_slots = 1;
 
184
                                show_empty_slots = TRUE;
161
185
                                break;
162
186
                        case 'S':
163
 
                                show_empty_slots = 0;
 
187
                                show_empty_slots = FALSE;
164
188
                                break;
165
189
                        case 'v':
166
190
                                return version();
171
195
                        case 'k':
172
196
                                temperature_units = TEMP_KELVIN;
173
197
                                break;
174
 
                        case 'c':
175
 
                                temperature_units = TEMP_CELSIUS;
 
198
                        case 'p':
 
199
                                proc_interface = TRUE;
 
200
                                free(acpi_path);
 
201
                                acpi_path = strdup(ACPI_PATH_PROC);
 
202
                                if (!acpi_path) {
 
203
                                        fprintf(stderr, "Out of memory in main()\n");
 
204
                                        return -1;
 
205
                                }
176
206
                                break;
177
207
                        case 'd':
178
208
                                free(acpi_path);
188
218
                }
189
219
        }
190
220
 
191
 
        if (show_everything || show_batteries) {
192
 
                do_show_batteries(acpi_path, show_empty_slots);
193
 
        }
194
 
        if (show_everything || show_thermal) {
195
 
                do_show_thermal(acpi_path, show_empty_slots, temperature_units);
196
 
        }
197
 
        if (show_everything || show_ac_adapter) {
198
 
                do_show_ac_adapter(acpi_path, show_empty_slots);
 
221
        if (show_batteries) {
 
222
                do_show_batteries(acpi_path, show_empty_slots, proc_interface);
 
223
        }
 
224
        if (show_ac_adapter) {
 
225
                do_show_ac_adapter(acpi_path, show_empty_slots, proc_interface);
 
226
        }
 
227
        if (show_thermal) {
 
228
                do_show_thermal(acpi_path, show_empty_slots, temperature_units, proc_interface);
 
229
        }
 
230
        if (show_cooling) {
 
231
                do_show_cooling(acpi_path, show_empty_slots, proc_interface);
199
232
        }
200
233
        return 0;
201
234
}