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
25 #include "xfce4-hdaps.h"
26 #include "xfce4-hdaps-dialogs.h"
28 /* We need a default device. Since we require kernels
29 newer than 2.6.28, it's probably sdX, and I'm guessing
30 that sda is most likely. */
31 #define DEFAULT_DEVICE_NAME "sda"
33 /* How often do we poll, in milliseconds?
34 This should be configured, but we set the default here. */
35 #define DEFAULT_POLL_FREQUENCY 500
38 /* Prototype, needed to register below. */
39 static void hdaps_construct(XfcePanelPlugin
*plugin
);
41 /* Register the plugin with the panel. */
42 XFCE_PANEL_PLUGIN_REGISTER_EXTERNAL(hdaps_construct
);
45 void hdaps_save(XfcePanelPlugin
*plugin
, HdapsPlugin
*hdaps
) {
50 /* Get the config file location. XFCE should know this. */
51 file
= xfce_panel_plugin_save_location(plugin
, TRUE
);
53 if (G_UNLIKELY(file
== NULL
)) {
54 DBG("Failed to find the configuration file. Bailing.");
58 /* Open the config file read/write. */
59 rc
= xfce_rc_simple_open(file
, FALSE
);
61 /* And we can free the file path now that we've
65 if (G_UNLIKELY(rc
== NULL
)) {
66 DBG("Failed to open the configuration file. Bailing.");
71 /* Write any user-configured values to the resource file. */
72 if (hdaps
->device_name
) {
73 xfce_rc_write_entry(rc
, "device_name", hdaps
->device_name
);
76 xfce_rc_write_int_entry(rc
, "poll_frequency", hdaps
->poll_frequency
);
78 /* close the rc file */
84 void hdaps_set_icon(HdapsPlugin
*hdaps
, int status
) {
88 /* Panel info magic. */
89 size
= xfce_panel_plugin_get_size(hdaps
->plugin
) - 6;
91 /* Try to load an icon from the current icon theme. */
92 if (status
== HDAPS_ERROR
) {
94 icon
= gtk_icon_theme_load_icon(gtk_icon_theme_get_default(),
98 else if (status
== HDAPS_OFF
) {
99 icon
= gtk_icon_theme_load_icon(gtk_icon_theme_get_default(),
104 /* status > HDAPS_OFF means it's on. */
105 icon
= gtk_icon_theme_load_icon(gtk_icon_theme_get_default(),
110 /* Get rid of the previous icon. */
112 gtk_widget_destroy(hdaps
->icon
);
116 hdaps
->icon
= gtk_image_new_from_pixbuf(icon
);
117 g_object_unref(G_OBJECT(icon
));
120 hdaps
->icon
= gtk_image_new_from_icon_name("dialog-warning", GTK_ICON_SIZE_BUTTON
);
123 gtk_box_pack_start(GTK_BOX(hdaps
->hvbox
), GTK_WIDGET(hdaps
->icon
), FALSE
, FALSE
, 0);
124 gtk_widget_show(hdaps
->icon
);
131 void hdaps_set_tooltip(HdapsPlugin
*hdaps
, int status
) {
133 if (status
== HDAPS_ERROR
) {
134 gtk_tooltips_set_tip(hdaps
->tooltip
, hdaps
->eventbox
, "HDAPS Error", NULL
);
136 else if (status
== HDAPS_OFF
) {
137 gtk_tooltips_set_tip(hdaps
->tooltip
, hdaps
->eventbox
, "HDAPS Off", NULL
);
140 gtk_tooltips_set_tip(hdaps
->tooltip
, hdaps
->eventbox
, "HDAPS On", NULL
);
145 static void hdaps_set_defaults(HdapsPlugin
*hdaps
) {
146 DBG("Configuring all settings to defaults.");
148 /* Here we determine the default device name. There are essentially
149 two "defaults," one soft, and the other hard. The soft default
150 is to choose the first supported HDAPS device in the system. This
151 would benefit users who, for example, only have one supported drive
152 named hda. If we can't find any supported HDAPS devices, we use the
153 hard default of DEFAULT_DEVICE_NAME. */
154 char hdaps_devices
[MAX_HDAPS_DEVICES
][FILENAME_MAX
];
155 int found_devices
= get_hdaps_device_list(hdaps_devices
);
157 if (found_devices
> 0) {
158 hdaps
->device_name
= g_strdup(hdaps_devices
[0]);
161 hdaps
->device_name
= g_strdup(DEFAULT_DEVICE_NAME
);
164 snprintf(hdaps
->sysfs_file
, FILENAME_MAX
, UNLOAD_HEADS_FMT
, hdaps
->device_name
);
166 /* The other default is easier. */
167 hdaps
->poll_frequency
= DEFAULT_POLL_FREQUENCY
;
171 static void hdaps_read(HdapsPlugin
*hdaps
) {
175 const gchar
*saved_device_name
;
177 /* Get the plugin config file location. XFCE should know this. */
178 file
= xfce_panel_plugin_save_location(hdaps
->plugin
, TRUE
);
180 if (G_UNLIKELY(file
== NULL
)) {
181 DBG("Something went wrong getting the configuration file location.");
182 DBG("Retaining default settings.");
186 /* Open the config file read-only. */
187 rc
= xfce_rc_simple_open(file
, TRUE
);
189 /* Don't need this anymore if we've got a file handle. */
192 if (G_UNLIKELY(rc
== NULL
)) {
193 DBG("There was an error opening the configuration file.");
194 DBG("Retaining default settings.");
198 /* Read the settings, one at a time. */
200 /* We use saved_device_name here because we need
201 to dupe the string after we read it in from the
203 saved_device_name
= xfce_rc_read_entry(rc
,
205 DEFAULT_DEVICE_NAME
);
206 hdaps
->device_name
= g_strdup(saved_device_name
);
207 snprintf(hdaps
->sysfs_file
, FILENAME_MAX
, UNLOAD_HEADS_FMT
, hdaps
->device_name
);
209 /* Integers are easier. */
210 hdaps
->poll_frequency
= xfce_rc_read_int_entry(rc
,
212 DEFAULT_POLL_FREQUENCY
);
214 /* And close the config file. */
221 static HdapsPlugin
*hdaps_new(XfcePanelPlugin
*plugin
) {
223 GtkOrientation orientation
;
225 /* Allocate memory for the plugin struct, and zero it. */
226 hdaps
= panel_slice_new0(HdapsPlugin
);
228 /* The HdapsPlugin gets a copy of the XfcePanelPlugin. */
229 hdaps
->plugin
= plugin
;
231 /* Set default values right before reading in the user's settings.
232 This way, hdaps_read() doesn't have to set defaults on error
234 hdaps_set_defaults(hdaps
);
236 /* Read any user settings into the HdapsPlugin. */
239 /* Get the current orientation. Magic. */
240 orientation
= xfce_panel_plugin_get_orientation(plugin
);
242 /* This creates the event box and shows it. This
246 b) Interact with it via right-click, etc.
248 hdaps
->eventbox
= gtk_event_box_new();
249 gtk_widget_show(hdaps
->eventbox
);
251 /* Set up the hvbox for the widget, which supports
252 both horizontal and vertical (hv) orientations. */
253 hdaps
->hvbox
= xfce_hvbox_new(orientation
, FALSE
, 2);
254 gtk_widget_show(hdaps
->hvbox
);
255 gtk_container_add(GTK_CONTAINER(hdaps
->eventbox
), hdaps
->hvbox
);
257 /* We only change the icon when the status has changed,
258 so it's important that they start out n*sync. */
259 hdaps
->previous_status
= HDAPS_OFF
;
260 hdaps_set_icon(hdaps
, HDAPS_OFF
);
262 /* Create the tooltip widget, and set its initial value.
263 * The first couple of lines are stolen from the battery
264 * status plugin. I make no claim as to their correctness.
266 hdaps
->tooltip
= gtk_tooltips_new();
267 g_object_ref(G_OBJECT(hdaps
->tooltip
));
268 gtk_object_sink(GTK_OBJECT(hdaps
->tooltip
));
269 hdaps_set_tooltip(hdaps
, HDAPS_OFF
);
276 static void hdaps_free(XfcePanelPlugin
*plugin
,
277 HdapsPlugin
*hdaps
) {
281 /* Destroy the dialog if it's still open. */
282 dialog
= g_object_get_data(G_OBJECT(plugin
), "dialog");
284 if (G_UNLIKELY(dialog
!= NULL
)) {
285 gtk_widget_destroy(dialog
);
288 /* Destroy the panel widgets. When dumb comments like these
289 are left over, they're from the sample panel applet,
291 gtk_widget_destroy(hdaps
->hvbox
);
293 /* Remove the timeout that was set during creation. */
294 if (hdaps
->timeout
) {
295 g_source_remove(hdaps
->timeout
);
298 /* And free the string (if any) containing
300 if (G_LIKELY(hdaps
->device_name
!= NULL
)) {
301 g_free(hdaps
->device_name
);
304 /* Goodbye, tooltips. */
305 gtk_tooltips_set_tip(hdaps
->tooltip
, hdaps
->eventbox
, NULL
, NULL
);
306 g_object_unref(G_OBJECT(hdaps
->tooltip
));
308 /* ...and finally free the plugin structure. */
309 panel_slice_free(HdapsPlugin
, hdaps
);
314 static void hdaps_orientation_changed(XfcePanelPlugin
*plugin
,
315 GtkOrientation orientation
,
316 HdapsPlugin
*hdaps
) {
318 /* Change the plugin's orientation. Basically magic to me. */
319 xfce_hvbox_set_orientation(XFCE_HVBOX(hdaps
->hvbox
), orientation
);
324 static gboolean
hdaps_size_changed(XfcePanelPlugin
*plugin
,
326 HdapsPlugin
*hdaps
) {
328 GtkOrientation orientation
;
330 /* Get the current orientation of the plugin. */
331 orientation
= xfce_panel_plugin_get_orientation(plugin
);
333 /* We want to make the widget "bigger" in the direction
334 of its orientation. Or is it the other way around? */
335 if (orientation
== GTK_ORIENTATION_HORIZONTAL
) {
336 gtk_widget_set_size_request(GTK_WIDGET(plugin
), -1, size
);
339 gtk_widget_set_size_request(GTK_WIDGET(plugin
), size
, -1);
342 /* We handled the change, so we're supposed to return TRUE. */
348 static gboolean
hdaps_update_status(HdapsPlugin
*hdaps
) {
349 /* This checks the status of HDAPS and updates the
350 widget accordingly. */
352 /* This just gets the status. */
353 int status
= parse_int_from_file(hdaps
->sysfs_file
);
355 /* The value in the sysfs_file represents the number of milliseconds
356 * that the drive heads will be parked. Of course, this will
357 * generally count down, and appear different each time we poll.
359 * So, to determine whether or not HDAPS has gone from "on"
360 * to "off," we treat all values greater than zero the same.
366 if (status
!= hdaps
->previous_status
) {
367 /* And we only update the icon if we need to. */
368 hdaps_set_icon(hdaps
, status
);
369 hdaps_set_tooltip(hdaps
, status
);
370 hdaps
->previous_status
= status
;
378 void hdaps_reset_timeout(HdapsPlugin
*hdaps
) {
379 /* Remove, and then re-set the timeout function. Useful
380 for changing the poll frequency. */
381 if (hdaps
->timeout
) {
382 g_source_remove(hdaps
->timeout
);
385 hdaps
->timeout
= g_timeout_add(hdaps
->poll_frequency
, (GSourceFunc
)hdaps_update_status
, hdaps
);
390 static void hdaps_construct(XfcePanelPlugin
*plugin
) {
393 /* Set the "translation domain". I don't know what that does. */
394 xfce_textdomain(GETTEXT_PACKAGE
, PACKAGE_LOCALE_DIR
, "UTF-8");
396 /* First, create the plugin. */
397 hdaps
= hdaps_new(plugin
);
399 /* Add the plugin's eventbox to the panel. */
400 gtk_container_add(GTK_CONTAINER(plugin
), hdaps
->eventbox
);
402 /* This configures the right-click menu to appear
403 on the plugin's eventbox. */
404 xfce_panel_plugin_add_action_widget(plugin
, hdaps
->eventbox
);
406 /* Connect the common event handlers. */
407 g_signal_connect(G_OBJECT(plugin
), "free-data",
408 G_CALLBACK(hdaps_free
), hdaps
);
410 g_signal_connect (G_OBJECT(plugin
), "save",
411 G_CALLBACK(hdaps_save
), hdaps
);
413 g_signal_connect (G_OBJECT(plugin
), "size-changed",
414 G_CALLBACK(hdaps_size_changed
), hdaps
);
416 g_signal_connect (G_OBJECT(plugin
), "orientation-changed",
417 G_CALLBACK(hdaps_orientation_changed
), hdaps
);
419 /* Show the "configure" right-click menu item, and
420 connect its event handler. */
421 xfce_panel_plugin_menu_show_configure(plugin
);
422 g_signal_connect(G_OBJECT(plugin
), "configure-plugin",
423 G_CALLBACK(hdaps_configure
), hdaps
);
425 /* Show the "about" right-click menu item, and
426 connect its event handler. */
427 xfce_panel_plugin_menu_show_about(plugin
);
428 g_signal_connect(G_OBJECT(plugin
), "about",
429 G_CALLBACK(hdaps_about
), hdaps
);
431 /* Set the timeout for the function which checks the
433 hdaps_reset_timeout(hdaps
);