From 6aa7cd4dd55e09b88c6fbd85ed2e9fd7cbe833c2 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Fri, 23 Jan 2009 22:59:11 -0500 Subject: [PATCH] Added tooltip support for the three states. Changed the "error" icon to emblem-noread. Updated the hdaps slurp/parse functions to always return HDAPS_ERROR when there is an error. Altered hdaps_set_icon to check for (status == HDAPS_ERROR) rather than (status < HDAPS_OFF) as a result of the previous modification. --- configure.in.in | 2 +- panel-plugin/hdaps.c | 60 +++++++++++++++++++++++++------------- panel-plugin/xfce4-hdaps.c | 33 +++++++++++++++++++-- panel-plugin/xfce4-hdaps.h | 7 +++-- 4 files changed, 75 insertions(+), 27 deletions(-) diff --git a/configure.in.in b/configure.in.in index cd3f594..c6f5656 100644 --- a/configure.in.in +++ b/configure.in.in @@ -8,7 +8,7 @@ dnl dnl *************************** dnl *** Version information *** dnl *************************** -m4_define([xfce4_hdaps_version], [0.0.2]) +m4_define([xfce4_hdaps_version], [0.0.3]) dnl *************************** dnl *** Initialize autoconf *** diff --git a/panel-plugin/hdaps.c b/panel-plugin/hdaps.c index de1476f..a2962e4 100644 --- a/panel-plugin/hdaps.c +++ b/panel-plugin/hdaps.c @@ -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); } } diff --git a/panel-plugin/xfce4-hdaps.c b/panel-plugin/xfce4-hdaps.c index 6f7bcfc..c9d9cb5 100644 --- a/panel-plugin/xfce4-hdaps.c +++ b/panel-plugin/xfce4-hdaps.c @@ -69,10 +69,10 @@ void hdaps_set_icon(HdapsPlugin *hdaps, int status) { size = xfce_panel_plugin_get_size(hdaps->plugin) - 6; /* Try to load an icon from the current icon theme. */ - if (status < HDAPS_OFF) { + if (status == HDAPS_ERROR) { /* Error */ icon = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(), - "dialog-error", + "emblem-noread", size, 0, NULL); } else if (status == HDAPS_OFF) { @@ -107,6 +107,21 @@ void hdaps_set_icon(HdapsPlugin *hdaps, int status) { } + +void hdaps_set_tooltip(HdapsPlugin *hdaps, int status) { + + if (status == HDAPS_ERROR) { + gtk_tooltips_set_tip(hdaps->tooltip, hdaps->eventbox, "HDAPS Error", NULL); + } + else if (status == HDAPS_OFF) { + gtk_tooltips_set_tip(hdaps->tooltip, hdaps->eventbox, "HDAPS Off", NULL); + } + else { + gtk_tooltips_set_tip(hdaps->tooltip, hdaps->eventbox, "HDAPS On", NULL); + } +} + + static void hdaps_set_defaults(HdapsPlugin *hdaps) { DBG("Configuring all settings to defaults."); hdaps->device_name = g_strdup(DEFAULT_DEVICE_NAME); @@ -205,6 +220,15 @@ static HdapsPlugin *hdaps_new(XfcePanelPlugin *plugin) { so it's important that they start out n*sync. */ hdaps->previous_status = HDAPS_OFF; hdaps_set_icon(hdaps, HDAPS_OFF); + + /* Create the tooltip widget, and set its initial value. + * The first couple of lines are stolen from the battery + * status plugin. I make no claim as to their correctness. + */ + hdaps->tooltip = gtk_tooltips_new(); + g_object_ref(G_OBJECT(hdaps->tooltip)); + gtk_object_sink(GTK_OBJECT(hdaps->tooltip)); + hdaps_set_tooltip(hdaps, HDAPS_OFF); return hdaps; } @@ -238,6 +262,10 @@ static void hdaps_free(XfcePanelPlugin *plugin, if (G_LIKELY(hdaps->device_name != NULL)) { g_free(hdaps->device_name); } + + /* Goodbye, tooltips. */ + gtk_tooltips_set_tip(hdaps->tooltip, hdaps->eventbox, NULL, NULL); + g_object_unref(G_OBJECT(hdaps->tooltip)); /* ...and finally free the plugin structure. */ panel_slice_free(HdapsPlugin, hdaps); @@ -289,6 +317,7 @@ static gboolean hdaps_update_status(HdapsPlugin *hdaps) { if (status != hdaps->previous_status) { /* And we only update the icon if we need to. */ hdaps_set_icon(hdaps, status); + hdaps_set_tooltip(hdaps, status); hdaps->previous_status = status; } diff --git a/panel-plugin/xfce4-hdaps.h b/panel-plugin/xfce4-hdaps.h index 7de1d5e..312c534 100644 --- a/panel-plugin/xfce4-hdaps.h +++ b/panel-plugin/xfce4-hdaps.h @@ -12,9 +12,10 @@ typedef struct { XfcePanelPlugin *plugin; /* Panel widgets */ - GtkWidget *eventbox; - GtkWidget *hvbox; - GtkWidget *icon; + GtkWidget *eventbox; + GtkWidget *hvbox; + GtkWidget *icon; + GtkTooltips *tooltip; /* A pointer to the timeout function. That is, a pointer to the function that gets -- 2.44.2