diff options
author | Chris Wilson <chris@chris-wilson.co.uk> | 2013-09-06 22:50:06 +0100 |
---|---|---|
committer | Chris Wilson <chris@chris-wilson.co.uk> | 2013-09-06 23:55:56 +0100 |
commit | 9fdaeec8f2ada82f790dcbd0018215103447a478 (patch) | |
tree | a29919988cd235356855dd728a5fb004df879185 /src | |
parent | 0485cdfb6dc8e51d57406f633d55f8a8ac1a0208 (diff) |
sna: Search /sys/class/power_state for the AC adapter
The adapter names are not uniform, so we need to scan the directory and
find the entry that corresponds to the Mains power supply. However, the
acpid does continue to report generic ac_adapter events.
Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Diffstat (limited to 'src')
-rw-r--r-- | src/sna/sna_acpi.c | 58 |
1 files changed, 47 insertions, 11 deletions
diff --git a/src/sna/sna_acpi.c b/src/sna/sna_acpi.c index ff7364c2..dcc0287b 100644 --- a/src/sna/sna_acpi.c +++ b/src/sna/sna_acpi.c @@ -33,6 +33,7 @@ #include <sys/socket.h> #include <sys/un.h> #include <unistd.h> +#include <dirent.h> #include <fcntl.h> #include <errno.h> @@ -135,22 +136,57 @@ void _sna_acpi_wakeup(struct sna *sna) } while (n); } -static int read_state(const char *path) +static int read_power_state(const char *path) { - char buf[80]; - int i, fd; + DIR *dir; + struct dirent *de; + int i = -1; - fd = open(path, 0); - if (fd < 0) + DBG(("%s: searching '%s'\n", __FUNCTION__, path)); + + dir = opendir(path); + if (dir == NULL) return -1; - i = read(fd, buf, sizeof(buf)-1); - if (i > 0) { - buf[i] = '\0'; - i = atoi(buf); + while ((de = readdir(dir))) { + char buf[1024]; + int fd; + + if (*de->d_name == '.') + continue; + + DBG(("%s: checking '%s'\n", __FUNCTION__, de->d_name)); + + snprintf(buf, sizeof(buf), "%s/%s/type", path, de->d_name); + fd = open(buf, 0); + if (fd < 0) + continue; + + i = read(fd, buf, sizeof(buf)); + buf[i > 0 ? i - 1: 0] = '\0'; + close(fd); + + DBG(("%s: %s is of type '%s'\n", __FUNCTION__, de->d_name, buf)); + + if (strcmp(buf, "Mains")) + continue; + + snprintf(buf, sizeof(buf), "%s/%s/online", path, de->d_name); + fd = open(buf, 0); + if (fd < 0) + continue; + + i = read(fd, buf, sizeof(buf)); + buf[i > 0 ? i - 1: 0] = '\0'; + if (i > 0) + i = atoi(buf); + DBG(("%s: %s is online? '%s'\n", __FUNCTION__, de->d_name, buf)); + close(fd); + + break; } + closedir(dir); - close(fd); return i; } @@ -169,7 +205,7 @@ void sna_acpi_init(struct sna *sna) sna->acpi.offset = 0; /* Read initial states */ - if (read_state("/sys/class/power_supply/ACAD/online") == 0) { + if (read_power_state("/sys/class/power_supply") == 0) { DBG(("%s: AC adapter is currently offline\n", __FUNCTION__)); sna->flags |= SNA_POWERSAVE; } |