2
* Copyright (C) 2002-2005 Mattia Dongili <malattia@linux.it>
3
* George Staikos <staikos@0wned.org>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
#include "cpufreqd_plugin.h"
27
#define ACPI_AC_DIR "/proc/acpi/ac_adapter/"
28
#define ACPI_AC_FILE "/state"
29
#define ACPI_AC_FORMAT "state: %s\n"
34
static char *ac_filelist[64];
35
static unsigned short ac_state;
36
static int ac_dir_num;
38
static int no_dots(const struct dirent *d) {
39
return d->d_name[0]!= '.';
42
/* static int acpi_ac_init(void)
44
* test if AC dirs are present
46
static int acpi_ac_init(void) {
47
struct dirent **namelist = NULL;
51
n = scandir(ACPI_AC_DIR, &namelist, no_dots, NULL);
54
*ac_filelist = malloc(n * 64 * sizeof(char));
56
snprintf(ac_filelist[n], 64, "%s%s%s", ACPI_AC_DIR, namelist[n]->d_name, ACPI_AC_FILE);
57
clog(LOG_INFO, "AC path %s\n", ac_filelist[n]);
63
clog(LOG_DEBUG, "no acpi_ac module compiled or inserted? (%s: %s)\n",
64
ACPI_AC_DIR, strerror(errno));
68
clog(LOG_NOTICE, "no ac adapters found, not a laptop?\n");
74
static int acpi_ac_exit(void) {
75
if (ac_filelist != NULL)
77
clog(LOG_INFO, "exited.\n");
81
/* static int acpi_ac_update(void)
83
* reads temperature valuse ant compute a medium value
85
static int acpi_ac_update(void) {
91
clog(LOG_DEBUG, "called\n");
92
for (i=0; i<ac_dir_num; i++) {
93
fp = fopen(ac_filelist[i], "r");
95
clog(LOG_ERR, "%s: %s\n", ac_filelist[i], strerror(errno));
98
fscanf(fp, ACPI_AC_FORMAT, temp);
101
clog(LOG_DEBUG, "read %s\n", temp);
102
ac_state |= (strncmp(temp, "on-line", 7)==0 ? PLUGGED : UNPLUGGED);
105
clog(LOG_INFO, "ac_adapter is %s\n",
106
ac_state==PLUGGED ? "on-line" : "off-line");
111
* parse the 'ac' keywork
113
static int acpi_ac_parse(const char *ev, void **obj) {
114
int *ret = malloc(sizeof(int));
116
clog(LOG_ERR, "couldn't make enough room for ac_status (%s)\n",
123
clog(LOG_DEBUG, "called with: %s\n", ev);
125
if (strncmp(ev, "on", 2) == 0) {
127
} else if (strncmp(ev, "off", 3) == 0) {
130
clog(LOG_ERR, "couldn't parse %s\n", ev);
135
clog(LOG_INFO, "parsed: %s\n", *ret==PLUGGED ? "on" : "off");
142
* evaluate the 'ac' keywork
144
static int acpi_ac_evaluate(const void *s) {
145
const int *ac = (const int *)s;
147
clog(LOG_DEBUG, "called: %s [%s]\n",
148
*ac==PLUGGED ? "on" : "off", ac_state==PLUGGED ? "on" : "off");
150
return (*ac == ac_state) ? MATCH : DONT_MATCH;
153
static struct cpufreqd_keyword kw[] = {
154
{ .word = "ac", .parse = &acpi_ac_parse, .evaluate = &acpi_ac_evaluate },
155
{ .word = NULL, .parse = NULL, .evaluate = NULL, .free = NULL }
158
static struct cpufreqd_plugin acpi_ac = {
159
.plugin_name = "acpi_ac_plugin", /* plugin_name */
160
.keywords = kw, /* config_keywords */
161
.plugin_init = &acpi_ac_init, /* plugin_init */
162
.plugin_exit = &acpi_ac_exit, /* plugin_exit */
163
.plugin_update = &acpi_ac_update /* plugin_update */
166
struct cpufreqd_plugin *create_plugin (void) {