]> gitweb.michael.orlitzky.com - xfce4-hdaps.git/blob - panel-plugin/hdaps.c
configure.ac.in: declare a "foreign" package and drop redundant NEWS file.
[xfce4-hdaps.git] / panel-plugin / hdaps.c
1 /*
2 * xfce4-hdaps, an XFCE4 panel plugin for the HDAPS system.
3 *
4 * Copyright (C) 2019 Michael Orlitzky
5 *
6 * http://michael.orlitzky.com/
7 *
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.
12 *
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:
17 *
18 * https://www.gnu.org/licenses/agpl-3.0.html
19 *
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <dirent.h>
27 #include <sys/types.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include "hdaps.h"
34
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
40
41 /* Where the block device names are located. */
42 #define SYSFS_BLOCK_DEVICE_DIR "/sys/block/"
43
44
45
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);
52 }
53
54
55
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.
61 */
62 int list_idx = 0;
63 DIR *dp;
64 struct dirent *ep;
65
66 dp = opendir(SYSFS_BLOCK_DEVICE_DIR);
67
68 if (dp != NULL) {
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);
73 list_idx++;
74 }
75 }
76
77 (void)closedir(dp); /* Explicitly ignore this. */
78 }
79
80 return list_idx;
81 }
82
83
84
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.
88 */
89 if (filename == NULL || buf == NULL) {
90 /* HDAPS_SUPER_BAD_ERROR */
91 return HDAPS_ERROR;
92 }
93
94
95 /* Return an error value by default. */
96 int bytes_read = HDAPS_ERROR;
97
98 int fd = open(filename, O_RDONLY);
99
100 if (fd < 0) {
101 /* open() didn't work. Report the error, and bail. */
102 fprintf(stderr, "open(%s): %s\n", filename, strerror(errno));
103 return HDAPS_ERROR;
104 }
105
106 bytes_read = read(fd, buf, max_bytes-1);
107
108 if (bytes_read < 0) {
109 /* Why did we read fewer than 0 bytes? */
110 fprintf(stderr, "read(%s): %s\n", filename, strerror(errno));
111 }
112 else {
113 /* Null-terminate buf if read() worked. */
114 buf[bytes_read] = 0;
115 }
116
117
118 if (close(fd)) {
119 /* Oh hey we should be able to close the file, too. */
120 fprintf(stderr, "close(%s): %s\n", filename, strerror(errno));
121 }
122
123 return bytes_read;
124 }
125
126
127
128
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). */
132
133 char buf[MAX_FILE_CONTENTS_SIZE];
134 int bytes_read = slurp_file(filename, buf, sizeof(buf));
135
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
139 * at least 1 byte.
140 */
141 return HDAPS_ERROR;
142 }
143 else {
144 /* If we read more than 0 bytes, hopefully we can
145 * count on atoi to succeed. */
146 return atoi(buf);
147 }
148 }