]>
gitweb.michael.orlitzky.com - xfce4-hdaps.git/blob - panel-plugin/hdaps.c
2 * xfce4-hdaps, an XFCE4 panel plugin for the HDAPS system.
4 * Copyright Michael Orlitzky
6 * http://michael.orlitzky.com/
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * http://www.fsf.org/licensing/licenses/gpl.html
26 #include <sys/types.h>
34 /* The most space we expect to need when reading in
35 a file using these hdaps functions. To exceed 32
36 bytes worth of content in unload_heads you'd have
37 to throw your laptop hard as a motherfucker. */
38 #define MAX_FILE_CONTENTS_SIZE 32
40 /* Where the block device names are located. */
41 #define SYSFS_BLOCK_DEVICE_DIR "/sys/block/"
45 static int hdaps_device_exists(const char* device
) {
46 /* Determine whether or not a device (file) exists and has an
47 unload_heads entry in sysfs. */
48 char path
[FILENAME_MAX
];
49 snprintf(path
, FILENAME_MAX
, UNLOAD_HEADS_FMT
, device
);
50 return (access(path
, F_OK
) == 0);
55 int get_hdaps_device_list(char list
[MAX_HDAPS_DEVICES
][FILENAME_MAX
]) {
56 /* Gets a list of devices which support HDAPS.
57 * We start by getting every device in the sysfs
58 * block device directory, and then eliminate those
59 * which don't have an unload_heads entry.
65 dp
= opendir(SYSFS_BLOCK_DEVICE_DIR
);
68 while ((ep
= readdir(dp
)) && list_idx
< MAX_HDAPS_DEVICES
) {
69 /* This next test covers "." and ".." too. */
70 if (hdaps_device_exists(ep
->d_name
)) {
71 strncpy(list
[list_idx
], ep
->d_name
, FILENAME_MAX
);
76 (void)closedir(dp
); /* Explicitly ignore this. */
84 int slurp_file(const char* filename
, char* buf
, int max_bytes
) {
85 /* This function just reads the contents of filename
86 * into buf, and returns the number of bytes read.
88 if (filename
== NULL
|| buf
== NULL
) {
89 /* HDAPS_SUPER_BAD_ERROR */
94 /* Return an error value by default. */
95 int bytes_read
= HDAPS_ERROR
;
97 int fd
= open(filename
, O_RDONLY
);
100 /* open() didn't work. Report the error, and bail. */
101 fprintf(stderr
, "open(%s): %s\n", filename
, strerror(errno
));
105 bytes_read
= read(fd
, buf
, max_bytes
-1);
107 if (bytes_read
< 0) {
108 /* Why did we read fewer than 0 bytes? */
109 fprintf(stderr
, "read(%s): %s\n", filename
, strerror(errno
));
112 /* Null-terminate buf if read() worked. */
118 /* Oh hey we should be able to close the file, too. */
119 fprintf(stderr
, "close(%s): %s\n", filename
, strerror(errno
));
128 int parse_int_from_file(const char* filename
) {
129 /* Read an integer from a file. We expect the file
130 to contain an integer (although in string form). */
132 char buf
[MAX_FILE_CONTENTS_SIZE
];
133 int bytes_read
= slurp_file(filename
, buf
, sizeof(buf
));
135 if (bytes_read
<= 0) {
136 /* We can't parse what doesn't exist. Note that
137 * reading "0" from a file should result in reading
143 /* If we read more than 0 bytes, hopefully we can
144 * count on atoi to succeed. */