]> gitweb.michael.orlitzky.com - xfce4-hdaps.git/blob - panel-plugin/hdaps.c
Added tooltip support for the three states.
[xfce4-hdaps.git] / panel-plugin / hdaps.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4
5 #include <dirent.h>
6 #include <sys/types.h>
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include "hdaps.h"
12
13 /* The most space we expect to need when reading in
14 a file using these hdaps functions. To exceed 32
15 bytes worth of content in unload_heads you'd have
16 to throw your laptop hard as a motherfucker. */
17 #define MAX_FILE_CONTENTS_SIZE 32
18
19 /* Where the block device names are located. */
20 #define SYSFS_BLOCK_DEVICE_DIR "/sys/block/"
21
22
23
24 static int hdaps_device_exists(const char* device) {
25 /* Determine whether or not a device (file) exists and has an
26 unload_heads entry in sysfs. */
27 char path[FILENAME_MAX];
28 snprintf(path, FILENAME_MAX, UNLOAD_HEADS_FMT, device);
29 return (access(path, F_OK) == 0);
30 }
31
32
33
34 int get_hdaps_device_list(char list[MAX_HDAPS_DEVICES][FILENAME_MAX]) {
35 /* Gets a list of devices which support HDAPS.
36 * We start by getting every device in the sysfs
37 * block device directory, and then eliminate those
38 * which don't have an unload_heads entry.
39 */
40 int list_idx = 0;
41 DIR *dp;
42 struct dirent *ep;
43
44 dp = opendir(SYSFS_BLOCK_DEVICE_DIR);
45
46 if (dp != NULL) {
47 while ((ep = readdir(dp)) && list_idx < MAX_HDAPS_DEVICES) {
48 /* This next test covers "." and ".." too. */
49 if (hdaps_device_exists(ep->d_name)) {
50 strncpy(list[list_idx], ep->d_name, FILENAME_MAX);
51 list_idx++;
52 }
53 }
54
55 (void)closedir(dp); /* Explicitly ignore this. */
56 }
57
58 return list_idx;
59 }
60
61
62
63 int slurp_file(const char* filename, char* buf, int max_bytes) {
64 /* This function just reads the contents of filename
65 * into buf, and returns the number of bytes read.
66 */
67 if (filename == NULL || buf == NULL) {
68 /* HDAPS_SUPER_BAD_ERROR */
69 return HDAPS_ERROR;
70 }
71
72
73 /* Return an error value by default. */
74 int bytes_read = HDAPS_ERROR;
75
76 int fd = open(filename, O_RDONLY);
77
78 if (fd < 0) {
79 /* open() didn't work. Report the error, and bail. */
80 fprintf(stderr, "open(%s): %s\n", filename, strerror(errno));
81 return HDAPS_ERROR;
82 }
83
84 bytes_read = read(fd, buf, max_bytes-1);
85
86 if (bytes_read < 0) {
87 /* Why did we read fewer than 0 bytes? */
88 fprintf(stderr, "read(%s): %s\n", filename, strerror(errno));
89 }
90 else {
91 /* Null-terminate buf if read() worked. */
92 buf[bytes_read] = 0;
93 }
94
95
96 if (close(fd)) {
97 /* Oh hey we should be able to close the file, too. */
98 fprintf(stderr, "close(%s): %s\n", filename, strerror(errno));
99 }
100
101 return bytes_read;
102 }
103
104
105
106
107 int parse_int_from_file(const char* filename) {
108 /* Read an integer from a file. We expect the file
109 to contain an integer (although in string form). */
110
111 char buf[MAX_FILE_CONTENTS_SIZE];
112 int bytes_read = slurp_file(filename, buf, sizeof(buf));
113
114 if (bytes_read <= 0) {
115 /* We can't parse what doesn't exist. Note that
116 * reading "0" from a file should result in reading
117 * at least 1 byte.
118 */
119 return HDAPS_ERROR;
120 }
121 else {
122 /* If we read more than 0 bytes, hopefully we can
123 * count on atoi to succeed. */
124 return atoi(buf);
125 }
126 }