summaryrefslogtreecommitdiff
path: root/at.c
blob: 1babd83a64e210189bf8aa3bd7229cf21b8d7dfb (plain)
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
#include <string.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <libacpi.h>

#include "error.h"
#include "sched.h"
#include "emit.h"

sched_t scheds[N_SCHED + 1] = {
	{ SCHED_BATT,    0, DI_BATT },
	{ SCHED_ACSTATE, 0, DI_ACSTATE },
	{ SCHED_ZONE,    0, DI_ZONE },
	{ SCHED_FAN,     0, DI_FAN},
	{ -1, -1, -1 }
};

int get_interval(int what, sched_t *scheds)
{
	int index = 0;

	while(scheds[index].what != -1)
	{
		if (scheds[index].what == what)
			return scheds[index].interval;

		index++;
	}

	return -1;
}

void set_interval(int what, sched_t *scheds, int value)
{
	int index = 0;

	while(scheds[index].what != -1)
	{
		if (scheds[index].what == what)
		{
			scheds[index].interval = value;
		}

		index++;
	}
}

void version(void)
{
	printf("acpitail v" VERSION ", (C) 2007 by folkert@vanheusden.com\n\n");
}

void help(void)
{
	version();

	printf("-A x   set check interval for AC-state (default: %ds)\n", get_interval(SCHED_ACSTATE, scheds));
	printf("-B x   interval for battery (%d)\n", get_interval(SCHED_BATT, scheds));
	printf("-F x   interval for fans (%d)\n", get_interval(SCHED_FAN, scheds));
	printf("-Z x   interval for zone information (%d)\n", get_interval(SCHED_ZONE, scheds));
	printf("-h     this help\n");
	printf("-V     show version information\n");
}

int main(int argc, char *argv[])
{
	global_t globals;
	int c;

	while((c = getopt(argc, argv, "B:A:Z:F:")) != -1)
	{
		switch(c)
		{
			case 'B':
				set_interval(SCHED_BATT, scheds, atoi(optarg));
				break;

			case 'A':
				set_interval(SCHED_ACSTATE, scheds, atoi(optarg));
				break;

			case 'Z':
				set_interval(SCHED_ZONE, scheds, atoi(optarg));
				break;

			case 'F':
				set_interval(SCHED_FAN, scheds, atoi(optarg));
				break;

			case 'V':
				version();
				return 0;

			case 'h':
				help();
				return 0;

			default:
				help();
				return 1;
		}
	}

	if (check_acpi_support() == NOT_SUPPORTED)
		error_exit("This system does not support ACPI.\n");

	memset(&globals, 0x00, sizeof(globals));

	if (init_acpi_batt(&globals) == ALLOC_ERR)
		error_exit("Failed to obtain battery information.\n");

	if (init_acpi_acadapt(&globals) == ALLOC_ERR)
		error_exit("Failed to obtain AC adapter information.\n");

	if (init_acpi_thermal(&globals) == ALLOC_ERR)
		error_exit("Failed to obtain thermal areas information.\n");

	if (init_acpi_fan(&globals) == ALLOC_ERR)
		error_exit("Failed to obtain fan information.\n");

	for(;;)
	{
		int loop;
		char header_shown = 0;

		for(loop=0; loop<N_SCHED; loop++)
		{
			time_t now = time(NULL);
			time_t cur_next_event = scheds[loop].last_run + scheds[loop].interval;

			if (cur_next_event <= now)
			{
				switch(scheds[loop].what)
				{
					case SCHED_BATT:
						emit_battery(&globals, &header_shown);
						break;

					case SCHED_ACSTATE:
						emit_acstate(&globals, &header_shown);
						break;

					case SCHED_ZONE:
						emit_zone(&globals, &header_shown);
						break;

					case SCHED_FAN:
						emit_fan(&globals, &header_shown);
						break;
				}

				scheds[loop].last_run = now;
			}
		}

		sleep_untill_next_event(scheds, N_SCHED);
	}

	return 0;
}