]>
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 (C) 2019 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 Affero General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (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 Affero General Public License for more details:
18 * https://www.gnu.org/licenses/agpl-3.0.html
27 #include <sys/types.h>
35 /* The most space we expect to need when reading in
36 a file using these hdaps functions. To exceed 32
37 bytes worth of content in unload_heads you'd have
38 to throw your laptop hard as a motherfucker. */
39 #define MAX_FILE_CONTENTS_SIZE 32
41 /* Where the block device names are located. */
42 #define SYSFS_BLOCK_DEVICE_DIR "/sys/block/"
46 static int hdaps_device_exists(const char* device
) {
47 /* Determine whether or not a device (file) exists and has an
48 unload_heads entry in sysfs. */
49 char path
[FILENAME_MAX
];
50 snprintf(path
, FILENAME_MAX
, UNLOAD_HEADS_FMT
, device
);
51 return (access(path
, F_OK
) == 0);
56 int get_hdaps_device_list(char list
[MAX_HDAPS_DEVICES
][FILENAME_MAX
]) {
57 /* Gets a list of devices which support HDAPS.
58 * We start by getting every device in the sysfs
59 * block device directory, and then eliminate those
60 * which don't have an unload_heads entry.
66 dp
= opendir(SYSFS_BLOCK_DEVICE_DIR
);
69 while ((ep
= readdir(dp
)) && list_idx
< MAX_HDAPS_DEVICES
) {
70 /* This next test covers "." and ".." too. */
71 if (hdaps_device_exists(ep
->d_name
)) {
72 strncpy(list
[list_idx
], ep
->d_name
, FILENAME_MAX
);
77 (void)closedir(dp
); /* Explicitly ignore this. */
85 int slurp_file(const char* filename
, char* buf
, int max_bytes
) {
86 /* This function just reads the contents of filename
87 * into buf, and returns the number of bytes read.
89 if (filename
== NULL
|| buf
== NULL
) {
90 /* HDAPS_SUPER_BAD_ERROR */
95 /* Return an error value by default. */
96 int bytes_read
= HDAPS_ERROR
;
98 int fd
= open(filename
, O_RDONLY
);
101 /* open() didn't work. Report the error, and bail. */
102 fprintf(stderr
, "open(%s): %s\n", filename
, strerror(errno
));
106 bytes_read
= read(fd
, buf
, max_bytes
-1);
108 if (bytes_read
< 0) {
109 /* Why did we read fewer than 0 bytes? */
110 fprintf(stderr
, "read(%s): %s\n", filename
, strerror(errno
));
113 /* Null-terminate buf if read() worked. */
119 /* Oh hey we should be able to close the file, too. */
120 fprintf(stderr
, "close(%s): %s\n", filename
, strerror(errno
));
129 int parse_int_from_file(const char* filename
) {
130 /* Read an integer from a file. We expect the file
131 to contain an integer (although in string form). */
133 char buf
[MAX_FILE_CONTENTS_SIZE
];
134 int bytes_read
= slurp_file(filename
, buf
, sizeof(buf
));
136 if (bytes_read
<= 0) {
137 /* We can't parse what doesn't exist. Note that
138 * reading "0" from a file should result in reading
144 /* If we read more than 0 bytes, hopefully we can
145 * count on atoi to succeed. */