File: main.c

package info (click to toggle)
acpi 1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 456 kB
  • sloc: sh: 1,292; ansic: 857; makefile: 8
file content (258 lines) | stat: -rw-r--r-- 8,021 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* provides a simple client program that reads ACPI status from the /proc 
 * filesystem
 *
 * Copyright (C) 2001       Grahame Bowland <grahame@angrygoats.net>
 *           (C) 2008-2013  Michael Meskes <meskes@debian.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 of the License, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
 *  USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include "acpi.h"

struct device device[4] = {
			{ BATTERY, "battery", "power_supply", "BAT" },
			{ AC_ADAPTER, "ac_adapter", "power_supply", "AC" },
			{ THERMAL_ZONE, "thermal_zone", "thermal", "thermal_zone" },
			{ COOLING_DEV, "fan", "thermal", "cooling_device" }
			  };

static void do_show_batteries(char *acpi_path, int show_empty_slots, int show_details, int show_remaining_time, int show_end_time, char *end_time_format, int proc_interface)
{
	struct list *batteries;

	batteries = find_devices(acpi_path, BATTERY, proc_interface);
	print_battery_information(batteries, show_empty_slots, show_details, show_remaining_time, show_end_time, end_time_format);
	free_devices(batteries);
}

static void do_show_ac_adapter(char *acpi_path, int show_empty_slots, int proc_interface)
{
	struct list *ac_adapter;

	ac_adapter = find_devices(acpi_path, AC_ADAPTER, proc_interface);
	print_ac_adapter_information(ac_adapter, show_empty_slots);
	free_devices(ac_adapter);
}

static void do_show_thermal(char *acpi_path, int show_empty_slots, int temperature_units, int show_details, int proc_interface) {
	struct list *thermal;

	thermal = find_devices(acpi_path, THERMAL_ZONE, proc_interface);
	print_thermal_information(thermal, show_empty_slots, temperature_units, show_details);
	free_devices(thermal);
}

static void do_show_cooling(char *acpi_path, int show_empty_slots, int proc_interface) {
	struct list *cooling;

	cooling = find_devices(acpi_path, COOLING_DEV, proc_interface);
	print_cooling_information(cooling, show_empty_slots);
	free_devices(cooling);
}

static int version(void)
{
	printf(ACPI_VERSION_STRING "\n"
"\n"
"Copyright (C) 2001 Grahame Bowland.\n"
"              2008-2012 Michael Meskes.\n"
"This is free software; see the source for copying conditions.  There is NO\n"
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
);
	return 1;
}

static int usage(char *argv[])
{
	printf(
"Usage: acpi [OPTION]...\n"
"Shows information from the /proc filesystem, such as battery status or\n"
"thermal information.\n"
"\n"
"  -b, --battery            battery information\n"
"  -i, --details            show additional details if available:\n"
"                             - battery capacity information\n"
"                             - temperature trip points\n"
"  -a, --ac-adapter         ac adapter information\n"
"  -r, --remaining-time     show remaining time for (dis)charge\n"
"  -e, --end-time           show end time for (dis)charge\n"
"  -F, --format-time <str>  specify end-time format string\n"
"  -t, --thermal            thermal information\n"
"  -c, --cooling            cooling information\n"
"  -V, --everything         show every device, overrides above options\n"
"  -s, --show-empty         show non-operational devices\n"
"  -f, --fahrenheit         use fahrenheit as the temperature unit\n"
"  -k, --kelvin             use kelvin as the temperature unit\n"
"  -d, --directory <dir>    path to ACPI info (/sys/class resp. /proc/acpi)\n"
"  -p, --proc               use old proc interface instead of new sys interface\n"
"  -h, --help               display this help and exit\n"
"  -v, --version            output version information and exit\n"
"\n"
"By default, acpi displays information on installed system batteries.\n"
"Non-operational devices, for example empty battery slots are hidden.\n"
"The default unit of temperature is degrees celsius.\n"
"\n"
"Report bugs to Michael Meskes <meskes@debian.org>.\n"
);
	return 1;
}

static struct option long_options[] = {
	{ "help", 0, 0, 'h' }, 
	{ "version", 0, 0, 'v' }, 
	{ "verbose", 0, 0, 'V' }, 
	{ "battery", 0, 0, 'b' }, 
	{ "ac-adapter", 0, 0, 'a' }, 
	{ "thermal", 0, 0, 't' }, 
	{ "cooling", 0, 0, 'c' }, 
	{ "show-empty", 0, 0, 's' }, 
	{ "fahrenheit", 0, 0, 'f' }, 
	{ "kelvin", 0, 0, 'k' }, 
	{ "directory", 1, 0, 'd' },
	{ "everything", 0, 0, 'V' }, 
	{ "proc", 0, 0, 'p' }, 
	{ "details", 0, 0, 'i' }, 
	{ "remaining-time", 0, 0, 'r' },
	{ "end-time", 0, 0, 'e' },
	{ "format-time", 1, 0, 'F' },
	{ 0, 0, 0, 0 }, 
};

int main(int argc, char *argv[])
{
	int show_batteries = FALSE;
	int show_ac_adapter = FALSE;
	int show_thermal = FALSE;
	int show_cooling = FALSE;
	int show_empty_slots = FALSE;
	int show_details = FALSE;
	int show_remaining_time = 2;
	int show_end_time = FALSE;
	int proc_interface = FALSE;
	int temperature_units = TEMP_CELSIUS;
	int ch, option_index;
	char *acpi_path = strdup(ACPI_PATH_SYS);
	char *end_time_format = NULL;

	if (!acpi_path) {
		fprintf(stderr, "Out of memory in main()\n");
		return -1;
	}

	while ((ch = getopt_long(argc, argv, "F:eripVbtashvfkcd:", long_options, &option_index)) != -1) {
		switch (ch) {
			case 'V':
				show_batteries = show_ac_adapter = show_thermal = show_cooling = show_details = TRUE;
				break;
			case 'b':
				show_batteries = TRUE;
				break;
			case 'a':
				show_ac_adapter = TRUE;
				break;
			case 't':
				show_thermal = TRUE;
				break;
			case 'c':
				show_cooling = TRUE;
				break;
			case 's':
				show_empty_slots = TRUE;
				break;
			case 'i':
				show_details = TRUE;
				break;
			case 'v':
				return version();
				break;
			case 'f':
				temperature_units = TEMP_FAHRENHEIT;
				break;
			case 'k':
				temperature_units = TEMP_KELVIN;
				break;
			case 'r':
				show_remaining_time = TRUE;
				break;
			case 'e':
				show_end_time = TRUE;
				if (show_remaining_time==2) {
					show_remaining_time = FALSE;
				}
				break;
			case 'F':
				end_time_format = strdup(optarg);
				if (!end_time_format) {
					fprintf(stderr, "Out of memory in main()\n");
					return -1;
					}
				break;
			case 'p':
				proc_interface = TRUE;
				free(acpi_path);
				acpi_path = strdup(ACPI_PATH_PROC);
				if (!acpi_path) {
					fprintf(stderr, "Out of memory in main()\n");
					return -1;
				}
				break;
			case 'd':
				free(acpi_path);
				acpi_path = strdup(optarg);
				if (!acpi_path) {
					fprintf(stderr, "Out of memory in main()\n");
					return -1;
				}
				break;
			case 'h':
			default:
				return usage(argv);
		}
	}


	/* if neither -e nor -r was chosen, show remaining time, for backward compatibility */
	if (show_remaining_time==2) {
		show_remaining_time = TRUE;
	}

	if (end_time_format && !show_end_time) {
		fprintf(stderr, "Warning: ignoring the time format since option -e is not specified");
	}

	/* if nothing was chosen, we show the battery information */
	if (!show_batteries && !show_ac_adapter && !show_thermal && !show_cooling)
		show_batteries = TRUE;

	if (show_batteries) {
		do_show_batteries(acpi_path, show_empty_slots, show_details, show_remaining_time, show_end_time, end_time_format, proc_interface);
	}
	if (show_ac_adapter) {
		do_show_ac_adapter(acpi_path, show_empty_slots, proc_interface);
	}
	if (show_thermal) {
		do_show_thermal(acpi_path, show_empty_slots, temperature_units, show_details, proc_interface);
	}
	if (show_cooling) {
		do_show_cooling(acpi_path, show_empty_slots, proc_interface);
	}
	return 0;
}