]> gitweb.michael.orlitzky.com - xfce4-hdaps.git/blobdiff - panel-plugin/hdaps.c
Added tooltip support for the three states.
[xfce4-hdaps.git] / panel-plugin / hdaps.c
index de1476f3199076e0e1791b26ad5c77ffc5f0dc94..a2962e40ffaaff47ccea819dd15afe9c46799ae5 100644 (file)
@@ -20,6 +20,7 @@
 #define SYSFS_BLOCK_DEVICE_DIR "/sys/block/"
 
 
+
 static int hdaps_device_exists(const char* device) {
   /* Determine whether or not a device (file) exists and has an
      unload_heads entry in sysfs. */
@@ -29,7 +30,13 @@ static int hdaps_device_exists(const char* device) {
 }
 
 
+
 int get_hdaps_device_list(char list[MAX_HDAPS_DEVICES][FILENAME_MAX]) {
+  /* Gets a list of devices which support HDAPS.
+   * We start by getting every device in the sysfs
+   * block device directory, and then eliminate those
+   * which don't have an unload_heads entry.
+   */  
   int list_idx = 0;
   DIR *dp;
   struct dirent *ep;
@@ -52,38 +59,46 @@ int get_hdaps_device_list(char list[MAX_HDAPS_DEVICES][FILENAME_MAX]) {
 }
 
 
+
 int slurp_file(const char* filename, char* buf, int max_bytes) {
   /* This function just reads the contents of filename
-   * into buf. It is slightly stolen from the hdapsd project.
-   */
-  
-  /* Return an error value by default. */
-  int ret = HDAPS_ERROR;
-  int fd = open(filename, O_RDONLY);
-
+   * into buf, and returns the number of bytes read.
+   */    
   if (filename == NULL || buf == NULL) {
-    return ret;
+    /* HDAPS_SUPER_BAD_ERROR */
+    return HDAPS_ERROR;
   }
-  
+
+  /* Return an error value by default. */
+  int bytes_read = HDAPS_ERROR;
+
+  int fd = open(filename, O_RDONLY);
+    
   if (fd < 0) {
+    /* open() didn't work. Report the error, and bail. */
     fprintf(stderr, "open(%s): %s\n", filename, strerror(errno));
-    return fd;
+    return HDAPS_ERROR;
   }    
 
-  ret = read(fd, buf, max_bytes-1);
+  bytes_read = read(fd, buf, max_bytes-1);
   
-  if (ret < 0) {
+  if (bytes_read < 0) {
+    /* Why did we read fewer than 0 bytes? */
     fprintf(stderr, "read(%s): %s\n", filename, strerror(errno));
   }
   else {
-    buf[ret] = 0; /* Null-terminate buf. */
+    /* Null-terminate buf if read() worked. */
+    buf[bytes_read] = 0;
   }
+  
 
   if (close(fd)) {
-    fprintf(stderr, "close(%s): %s\n", filename, strerror(errno));
+    /* Oh hey we should be able to close the file, too. */
+    fprintf(stderr, "close(%s): %s\n", filename, strerror(errno));    
   }
   
-  return ret;
+  return bytes_read;
 }
 
 
@@ -94,15 +109,18 @@ int parse_int_from_file(const char* filename) {
      to contain an integer (although in string form). */
   
   char buf[MAX_FILE_CONTENTS_SIZE];
-  int ret = slurp_file(filename, buf, sizeof(buf));
-
-  if (ret < 0) {
-    /* Why did we read fewer than 0 bytes? */
-    return ret;
+  int bytes_read = slurp_file(filename, buf, sizeof(buf));
+
+  if (bytes_read <= 0) {
+    /* We can't parse what doesn't exist. Note that
+     * reading "0" from a file should result in reading
+     * at least 1 byte.
+     */
+    return HDAPS_ERROR;
   }
   else {
     /* If we read more than 0 bytes, hopefully we can
-       count on atoi to succeed. */
+     * count on atoi to succeed. */
     return atoi(buf);
   }
 }