wmbattery: AC_INIT changes.
[dockapps.git] / wmhdplop / gkrellm_hdplop.c
blob5cead206afc49280e4f83f52ee410a3179431df2
1 /*
3 * gkrellm_hdplop.c
4 * dae (jolly.frederic@fnac.net)
6 * gkrellm plugin for wmhdplop:
7 * http://hules.free.fr/wmhdplop/
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 * 02111-1307, USA.
26 #include <libgen.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <time.h>
32 #include "config.h"
33 #include "global.h"
34 #include "wmhdplop.h"
35 #define PLUGIN_VERSION VERSION
37 #define PLUGIN_NAME "gkhdplop"
38 #define PLUGIN_DESC "wmhdplop gkrellm port"
39 #define PLUGIN_URL "http://hules.free.fr/wmhdplop/"
40 #define PLUGIN_STYLE PLUGIN_NAME
41 #define PLUGIN_KEYWORD PLUGIN_NAME
44 static GkrellmMonitor *mon = NULL;
45 static GkrellmChart *chart = NULL;
46 static GkrellmChartconfig *chart_config = NULL;
48 #define TIMER1 50 /* main updates every 40 ms */
49 #define TIMER2 50 * TIMER1 /* small updates every 1600 ms */
51 #define PLUGIN_HEIGHT 64
53 static gint timeout_id;
54 static int option_timer = 0;
56 /* Options stuffs */
57 GtkWidget *swap_check = NULL;
58 GtkWidget *io_check = NULL;
59 GtkWidget *leds_check = NULL;
60 GtkWidget *colormap = NULL;
61 GtkWidget *hdlist_check = NULL;
62 GtkWidget *throughput_threshold = NULL;
63 GtkWidget *hddtemp_check = NULL;
64 GtkWidget *entry_smallfont = NULL, *entry_bigfont = NULL;
66 static gboolean update_plugin(void)
68 GdkEventExpose event;
69 gint ret_val;
70 gtk_signal_emit_by_name(GTK_OBJECT(chart->drawing_area), "expose_event", &event, &ret_val);
71 return TRUE; /* restart timer */
74 static gint chart_expose_event(GtkWidget *widget UNUSED, GdkEventExpose *ev UNUSED)
76 int update_options = 0;
77 option_timer++;
78 if (option_timer == TIMER2 / TIMER1) {
79 option_timer = 0;
80 update_options = 1;
82 gkrellm_hdplop_update(update_options);
83 gkrellm_draw_chart_to_screen(chart);
84 return TRUE;
87 static gint wheel_event(GtkWidget *widget UNUSED, GdkEventScroll *ev)
89 if (ev->direction == GDK_SCROLL_UP)
90 next_displayed_hd(); //change_displayed_hd(+1);
91 else if (ev->direction == GDK_SCROLL_DOWN)
92 prev_displayed_hd(); //change_displayed_hd(-1);
93 return TRUE;
96 static gint button_release_event(GtkWidget *widget UNUSED, GdkEventButton *ev, gpointer data UNUSED)
98 if (ev->button == 3) {
99 gkrellm_open_config_window(mon);
100 } else if (ev->button == 1) {
101 change_displayed_hd(+1);
103 return TRUE;
106 static void disable_plugin(void)
108 if (timeout_id)
109 gtk_timeout_remove(timeout_id);
110 timeout_id = 0;
113 static void create_plugin(GtkWidget *vbox, gint first_create)
115 if(first_create) {
116 chart = gkrellm_chart_new0();
119 gkrellm_set_chart_height_default(chart, PLUGIN_HEIGHT);
120 gkrellm_chart_create(vbox, mon, chart, &chart_config);
122 if (first_create) {
123 hdplop_main(chart->w, chart->h, chart->drawing_area->window);
124 } else {
125 dockimlib2_gkrellm_xinit(app->dock, chart->drawing_area->window);
126 reshape(chart->w, chart->h);
128 if (first_create) {
129 //printf("chart : w=%d, h=%d\n", chart->w, chart->h);
130 gtk_signal_connect(GTK_OBJECT(chart->drawing_area),
131 "expose_event", (GtkSignalFunc) chart_expose_event, NULL);
132 gtk_signal_connect(GTK_OBJECT(chart->drawing_area),
133 "button_release_event", GTK_SIGNAL_FUNC(button_release_event), NULL);
134 g_signal_connect(G_OBJECT(chart->drawing_area),
135 "scroll_event", G_CALLBACK(wheel_event), NULL);
137 /* Update plugin every TIMER1 ms */
138 if (!timeout_id)
139 timeout_id = g_timeout_add(TIMER1, (GtkFunction) update_plugin, NULL);
140 gkrellm_disable_plugin_connect(mon, disable_plugin);
143 static void option_toggled_cb(GtkToggleButton *button, gpointer user_data UNUSED)
145 gboolean active = gtk_toggle_button_get_active(button);
146 GtkWidget *togglebutton = GTK_WIDGET(button);
148 if (togglebutton == swap_check) {
149 Prefs.disable_swap_matrix = !active;
151 else if (togglebutton == io_check) {
152 Prefs.disable_io_matrix = !active;
154 else if (togglebutton == hdlist_check) {
155 if (active) {
156 Prefs.hdlist_pos = AL_BOTTOM + AL_LEFT;
157 gtk_widget_set_sensitive(leds_check, TRUE);
158 gtk_widget_set_sensitive(hddtemp_check, TRUE);
160 else {
161 Prefs.hdlist_pos = AL_NONE;
162 gtk_widget_set_sensitive(leds_check, FALSE);
163 gtk_widget_set_sensitive(hddtemp_check, FALSE);
166 else if (togglebutton == leds_check) {
167 Prefs.disable_hd_leds = !active;
169 else if (togglebutton == hddtemp_check) {
170 Prefs.enable_hddtemp = active;
171 app->displayed_hd_changed = 1;
175 static void cb_colormap_modified(GtkWidget *widget UNUSED, GtkSpinButton *spin)
177 Prefs.iomatrix_colormap = gtk_spin_button_get_value_as_int(spin);
180 static void cb_spinbutton_modified(GtkWidget *widget UNUSED, GtkSpinButton *spin)
182 if (GTK_WIDGET(spin) == throughput_threshold) {
183 Prefs.popup_throughput_threshold = gtk_spin_button_get_value_as_float(spin);
187 static void cb_reload_fonts(GtkWidget *widget) {
188 (void) widget;
189 if (strcmp(gtk_entry_get_text(GTK_ENTRY(entry_smallfont)), app->current_smallfont_name) ||
190 strcmp(gtk_entry_get_text(GTK_ENTRY(entry_bigfont)), app->current_bigfont_name)) {
191 ASSIGN_STRING(Prefs.smallfontname, gtk_entry_get_text(GTK_ENTRY(entry_smallfont)));
192 ASSIGN_STRING(Prefs.bigfontname, gtk_entry_get_text(GTK_ENTRY(entry_bigfont)));
193 init_fonts(app);
194 if (app->smallfont == NULL) {
195 gkrellm_config_message_dialog("font problem..", "could not load the small font");
196 } else gtk_entry_set_text(GTK_ENTRY(entry_smallfont), app->current_smallfont_name);
197 if (app->bigfont == NULL) {
198 gkrellm_config_message_dialog("font problem..", "could not load the big font");
199 } else gtk_entry_set_text(GTK_ENTRY(entry_bigfont), app->current_bigfont_name);
200 /* force recomputations of some dimensions */
201 app->displayed_hd_changed = 1; app->reshape_cnt++;
205 static void create_plugin_tab(GtkWidget *tab_vbox)
207 GtkWidget *tabs = NULL;
208 GtkWidget *options_tab = NULL;
209 GtkWidget *info_tab = NULL;
210 GtkWidget *info = NULL;
211 GtkWidget *about_tab = NULL;
212 GtkWidget *about = NULL;
213 GtkWidget *main_box;
215 static gchar *info_text[] =
217 "<b>" PLUGIN_NAME "\n\n",
218 PLUGIN_DESC "\n\n",
219 "improve your productivity with " PLUGIN_NAME ":\n",
220 "It monitors your hard-drives by sending visual stimuli to your cortex\n",
221 "each time your /dev/hdx writes or reads anything.\n",
222 "Try to launch openoffice and enjoy the gkhdplop show!\n\n",
223 "<i>Usage:\n\n",
224 "- Change the hard drive with the mouse wheel\n",
225 "- Animation reflecting swap activity...\n",
226 "- Background animation reflecting disk activity...\n",
227 "- Small led indicating disk activity...\n",
228 "- Several glittering color schemes...\n",
229 "- Popup display with the io throughput...\n",
232 static gchar *about_text =
234 PLUGIN_NAME " " PLUGIN_VERSION "\n"
235 "a " PLUGIN_DESC "\n\n"
236 "Copyright (c) 2004 dae\n"
237 "jolly.frederic@fnac.net\n"
238 "http://quadaemon.free.fr\n\n"
239 "Based on wmhdplop\nCopyright (c) 2003,2004 pouaite\n"
240 PLUGIN_URL "\n\n"
241 "Released under the GNU Public Licence"
244 tabs = gtk_notebook_new();
245 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(tabs), GTK_POS_TOP);
246 gtk_box_pack_start(GTK_BOX(tab_vbox), tabs, TRUE, TRUE, 0);
248 /* Options tab */
249 options_tab = gkrellm_gtk_notebook_page(tabs, _("Options"));
251 main_box = gtk_vbox_new(FALSE, 0);
252 gtk_widget_set_name(main_box, "main_box");
253 gtk_widget_ref(main_box);
254 gtk_object_set_data_full(GTK_OBJECT (options_tab), "main_box", main_box,
255 (GtkDestroyNotify) gtk_widget_unref);
256 gtk_widget_show(main_box);
257 gtk_container_add(GTK_CONTAINER (options_tab), main_box);
259 /* io button */
260 gkrellm_gtk_check_button_connected(main_box, &io_check, !Prefs.disable_io_matrix, 1, 1, 0,
261 option_toggled_cb, NULL, _("Show disk activity"));
262 /* swap button */
263 gkrellm_gtk_check_button_connected(main_box, &swap_check, !Prefs.disable_swap_matrix, 1, 1, 0,
264 option_toggled_cb, NULL, _("Show swap activity"));
265 /* hdlist button */
266 gkrellm_gtk_check_button_connected(main_box, &hdlist_check,
267 (Prefs.hdlist_pos == AL_NONE)?FALSE:TRUE, 1, 1, 0,
268 option_toggled_cb, NULL, _("Show disk name"));
269 /* leds button */
270 gkrellm_gtk_check_button_connected(main_box, &leds_check, !Prefs.disable_hd_leds, 1, 1, 0,
271 option_toggled_cb, NULL, _("Led indicating disk activity"));
272 gtk_widget_set_sensitive(leds_check, (Prefs.hdlist_pos == AL_NONE)?FALSE:TRUE);
273 /* hddtemp button */
274 gkrellm_gtk_check_button_connected(main_box, &hddtemp_check, Prefs.enable_hddtemp, 1, 1, 0,
275 option_toggled_cb, NULL,
276 _("Display hd temperature (requires hddtemp daemon running on port 7634)"));
277 gtk_widget_set_sensitive(hddtemp_check, (Prefs.hdlist_pos == AL_NONE)?FALSE:TRUE);
278 /* colormap spin button */
279 gkrellm_gtk_spin_button(main_box, NULL, Prefs.iomatrix_colormap, 0, 4, 1, 1, 0, 0,
280 cb_colormap_modified, NULL, FALSE, _("Colormap"));
282 /* threshold spin button */
283 gkrellm_gtk_spin_button(main_box, &throughput_threshold, Prefs.popup_throughput_threshold, 0., 500, 0.1, 1, 1, 0,
284 cb_spinbutton_modified, NULL, FALSE, _("minimum io throughput (MB/s)"));
287 GtkWidget *frame = gtk_frame_new("Fonts");
288 gtk_box_pack_start(GTK_BOX(main_box), frame, TRUE, FALSE, 0);
290 GtkWidget *hbox0 = gtk_hbox_new(FALSE, 4);
291 gtk_container_add(GTK_CONTAINER(frame), hbox0);
293 GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
294 gtk_box_pack_start(GTK_BOX(hbox0), vbox, TRUE, FALSE, 0);
296 GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
297 gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, FALSE, 0);
298 entry_smallfont = gtk_entry_new();
299 gtk_box_pack_start(GTK_BOX(hbox), entry_smallfont, TRUE, FALSE, 0);
300 GtkWidget *pLabel = gtk_label_new("Small font (without .ttf extension):");
301 gtk_entry_set_text(GTK_ENTRY(entry_smallfont), app->current_smallfont_name);
302 gtk_box_pack_start(GTK_BOX(hbox), pLabel, TRUE, FALSE, 0);
304 hbox = gtk_hbox_new(FALSE, 0);
305 gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, FALSE, 0);
306 entry_bigfont = gtk_entry_new();
307 gtk_box_pack_start(GTK_BOX(hbox), entry_bigfont, TRUE, FALSE, 0);
308 gtk_entry_set_text(GTK_ENTRY(entry_bigfont), app->current_bigfont_name);
309 pLabel = gtk_label_new("Big font (without .ttf extension)");
310 gtk_box_pack_start(GTK_BOX(hbox), pLabel, TRUE, FALSE, 0);
312 gkrellm_gtk_button_connected(hbox0, NULL, TRUE/* expand*/, TRUE /* fill*/, 0 /*pad*/,
313 G_CALLBACK(cb_reload_fonts), NULL, "reload fonts");
316 /* Info tab */
317 info_tab = gkrellm_gtk_framed_notebook_page(tabs, _("Info"));
318 info = gkrellm_gtk_scrolled_text_view(info_tab, NULL, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
320 gkrellm_gtk_text_view_append_strings(info, info_text, sizeof(info_text) / sizeof(gchar *));
322 /* About tab */
323 about_tab = gkrellm_gtk_notebook_page(tabs, _("About"));
324 about = gtk_label_new(about_text);
325 gtk_box_pack_start(GTK_BOX(about_tab), about, TRUE, TRUE, 0);
328 static void save_plugin_config(FILE *f)
330 fprintf(f, "%s options %d %d %d %d %d %d %f smallfont=%s bigfont=%s\n", PLUGIN_KEYWORD,
331 Prefs.disable_swap_matrix,
332 Prefs.disable_io_matrix,
333 Prefs.disable_hd_leds,
334 Prefs.hdlist_pos,
335 Prefs.enable_hddtemp,
336 Prefs.iomatrix_colormap,
337 Prefs.popup_throughput_threshold,
338 app->current_smallfont_name, app->current_bigfont_name);
341 static void load_plugin_config(gchar *config_line)
343 char sf[1000], bf[1000]; sf[0] = bf[0] = 0;
344 int n =sscanf(config_line, "options %d %d %d %d %d %d %f smallfont=%1000s bigfont=%1000s",
345 &Prefs.disable_swap_matrix,
346 &Prefs.disable_io_matrix,
347 &Prefs.disable_hd_leds,
348 &Prefs.hdlist_pos,
349 &Prefs.enable_hddtemp,
350 &Prefs.iomatrix_colormap,
351 &Prefs.popup_throughput_threshold,
352 sf, bf);
353 if (n>= 8) Prefs.smallfontname = strdup(sf);
354 if (n>= 9) Prefs.bigfontname = strdup(bf);
357 static GkrellmMonitor hdplop_mon =
359 PLUGIN_NAME, /* Name, for config tab. */
360 0, /* Id, 0 if a plugin */
361 create_plugin, /* The create_plugin() function */
362 NULL, /* The update_plugin() function */
363 create_plugin_tab, /* The create_plugin_tab() config function */
364 NULL, /* The apply_plugin_config() function */
366 save_plugin_config, /* The save_plugin_config() function */
367 load_plugin_config, /* The load_plugin_config() function */
368 PLUGIN_KEYWORD, /* config keyword */
370 NULL, /* Undefined 2 */
371 NULL, /* Undefined 1 */
372 NULL, /* private */
374 MON_CPU, /* Insert plugin before this monitor. */
375 NULL, /* Handle if a plugin, filled in by GKrellM */
376 NULL /* path if a plugin, filled in by GKrellM */
380 GkrellmMonitor *gkrellm_init_plugin(void)
382 gkrellm_add_meter_style(&hdplop_mon, PLUGIN_STYLE);
383 return (mon = &hdplop_mon);