Imported Upstream version 0.8.0.6
[psensor-pkg-ubuntu.git] / src / ui.c
1 /*
2  * Copyright (C) 2010-2014 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19 #include <stdlib.h>
20
21 #include "cfg.h"
22 #include "slog.h"
23 #include "ui.h"
24 #include "ui_graph.h"
25 #include "ui_pref.h"
26 #include "ui_sensorpref.h"
27 #include "ui_sensorlist.h"
28 #include "ui_status.h"
29 #include "ui_appindicator.h"
30
31 static void save_window_pos(struct ui_psensor *ui)
32 {
33         gboolean visible;
34         GtkWindow *win;
35         struct config *cfg;
36
37         visible = gtk_widget_get_visible(ui->main_window);
38         log_debug("Window visible: %d", visible);
39
40         if (visible == TRUE) {
41                 cfg = ui->config;
42
43                 win = GTK_WINDOW(ui->main_window);
44
45                 gtk_window_get_position(win, &cfg->window_x, &cfg->window_y);
46                 log_debug("Window position: %d %d",
47                           cfg->window_x,
48                           cfg->window_y);
49
50                 gtk_window_get_size(win,
51                                     &cfg->window_w,
52                                     &cfg->window_h);
53                 log_debug("Window size: %d %d", cfg->window_w, cfg->window_h);
54
55                 cfg->window_divider_pos
56                         = gtk_paned_get_position(GTK_PANED(ui->sensor_box));
57
58                 config_save(cfg);
59         }
60 }
61
62 static gboolean
63 on_delete_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
64 {
65         struct ui_psensor *ui = data;
66
67         save_window_pos(ui);
68
69         log_debug("is_status_supported: %d\n", is_status_supported());
70
71         if (is_appindicator_supported() || is_status_supported())
72                 gtk_widget_hide(ui->main_window);
73         else
74                 ui_psensor_quit(ui);
75
76         return TRUE;
77 }
78
79 void ui_show_about_dialog()
80 {
81         gtk_show_about_dialog
82                 (NULL,
83                  "comments",
84                  _("Psensor is a GTK+ application for monitoring hardware "
85                    "sensors"),
86                  "copyright",
87                  _("Copyright(c) 2010-2014\njeanfi@gmail.com"),
88                  "logo-icon-name", "psensor",
89                  "program-name", "Psensor",
90                  "title", _("About Psensor"),
91                  "version", VERSION,
92                  "website", PACKAGE_URL,
93                  "website-label", _("Psensor Homepage"),
94                  NULL);
95 }
96
97 void ui_cb_about(GtkMenuItem *mi, gpointer data)
98 {
99         ui_show_about_dialog();
100 }
101
102 void ui_cb_menu_quit(GtkMenuItem *mi, gpointer data)
103 {
104         ui_psensor_quit((struct ui_psensor *)data);
105 }
106
107 void ui_cb_preferences(GtkMenuItem *mi, gpointer data)
108 {
109         ui_pref_dialog_run((struct ui_psensor *)data);
110 }
111
112 void ui_cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
113 {
114         struct ui_psensor *ui = data;
115
116         if (ui->sensors && *ui->sensors)
117                 ui_sensorpref_dialog_run(*ui->sensors, ui);
118 }
119
120 void ui_psensor_quit(struct ui_psensor *ui)
121 {
122         save_window_pos(ui);
123
124         log_debug("Destroy main window");
125         gtk_widget_destroy(ui->main_window);
126         gtk_main_quit();
127 }
128
129 void ui_enable_alpha_channel(struct ui_psensor *ui)
130 {
131         GdkScreen *screen;
132         GdkVisual *visual;
133         struct config *cfg;
134
135         cfg = ui->config;
136
137         screen = gtk_widget_get_screen(ui->main_window);
138
139         log_debug("Config alpha channel enabled: %d",
140                   cfg->alpha_channel_enabled);
141         if (cfg->alpha_channel_enabled && gdk_screen_is_composited(screen)) {
142                 log_debug("Screen is composited");
143                 visual = gdk_screen_get_rgba_visual(screen);
144                 if (visual) {
145                         gtk_widget_set_visual(ui->main_window, visual);
146                 } else {
147                         cfg->alpha_channel_enabled = 0;
148                         log_err("Enable alpha channel has failed");
149                 }
150         } else {
151                 cfg->alpha_channel_enabled = 0;
152         }
153 }
154
155 static void
156 slog_enabled_cbk(GConfClient *client, guint id, GConfEntry *e, gpointer data)
157 {
158         struct ui_psensor *ui;
159         struct psensor **sensors;
160         pthread_mutex_t *mutex;
161
162         ui = (struct ui_psensor *)data;
163         sensors = ui->sensors;
164         mutex = &ui->sensors_mutex;
165
166         log_debug("slog_enabled_cbk");
167
168         if (is_slog_enabled())
169                 slog_activate(NULL, sensors, mutex, config_get_slog_interval());
170         else
171                 slog_close();
172 }
173
174 void ui_window_create(struct ui_psensor *ui)
175 {
176         GtkWidget *window;
177         GdkPixbuf *icon;
178         GtkIconTheme *icon_theme;
179         struct config *cfg;
180         guint ok;
181         GtkBuilder *builder;
182         GError *error;
183
184         builder = gtk_builder_new();
185
186         error = NULL;
187         ok = gtk_builder_add_from_file
188                 (builder,
189                  PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "psensor.glade",
190                  &error);
191
192         if (!ok) {
193                 log_printf(LOG_ERR, error->message);
194                 g_error_free(error);
195                 return ;
196         }
197
198         window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
199         gtk_builder_connect_signals(builder, ui);
200         cfg = ui->config;
201         if (cfg->window_restore_enabled)
202                 gtk_window_move(GTK_WINDOW(window),
203                                 cfg->window_x,
204                                 cfg->window_y);
205
206         config_slog_enabled_notify_add(slog_enabled_cbk, ui);
207
208         gtk_window_set_default_size(GTK_WINDOW(window),
209                                     cfg->window_w,
210                                     cfg->window_h);
211
212         icon_theme = gtk_icon_theme_get_default();
213         icon = gtk_icon_theme_load_icon(icon_theme, "psensor", 48, 0, NULL);
214         if (icon)
215                 gtk_window_set_icon(GTK_WINDOW(window), icon);
216         else
217                 log_err(_("Failed to load Psensor icon."));
218
219         g_signal_connect(window,
220                          "delete_event", G_CALLBACK(on_delete_event_cb), ui);
221
222         gtk_window_set_decorated(GTK_WINDOW(window),
223                                  cfg->window_decoration_enabled);
224
225         gtk_window_set_keep_below(GTK_WINDOW(window),
226                                   cfg->window_keep_below_enabled);
227
228         ui->menu_bar = GTK_WIDGET(gtk_builder_get_object(builder, "menu_bar"));
229         ui->main_box = GTK_WIDGET(gtk_builder_get_object(builder, "main_box"));
230         ui->popup_menu = GTK_WIDGET(gtk_builder_get_object(builder,
231                                                            "popup_menu"));
232         g_object_ref(G_OBJECT(ui->popup_menu));
233         ui->main_window = window;
234         ui->w_graph = GTK_WIDGET(gtk_builder_get_object(builder,
235                                                         "graph"));
236         ui_graph_create(ui);
237
238         ui->sensor_box = GTK_PANED(gtk_builder_get_object(builder,
239                                                           "sensor_box"));
240         ui->sensors_store = GTK_LIST_STORE(gtk_builder_get_object
241                                            (builder, "sensors_store"));
242         ui->sensors_tree = GTK_TREE_VIEW(gtk_builder_get_object
243                                          (builder, "sensors_tree"));
244         ui->sensors_scrolled_tree
245                 = GTK_SCROLLED_WINDOW(gtk_builder_get_object
246                                       (builder, "sensors_scrolled_tree"));
247
248         ui_sensorlist_create(ui);
249
250         log_debug("ui_window_create(): show_all");
251         gtk_widget_show_all(ui->main_box);
252
253         g_object_unref(G_OBJECT(builder));
254
255         log_debug("ui_window_create() ends");
256 }
257
258 static void menu_bar_show(unsigned int show, struct ui_psensor *ui)
259 {
260         if (show)
261                 gtk_widget_show(ui->menu_bar);
262         else
263                 gtk_widget_hide(ui->menu_bar);
264 }
265
266 void ui_window_update(struct ui_psensor *ui)
267 {
268         struct config *cfg;
269
270         log_debug("ui_window_update()");
271
272         cfg = ui->config;
273
274         g_object_ref(GTK_WIDGET(ui->sensors_scrolled_tree));
275         g_object_ref(GTK_WIDGET(ui->w_graph));
276
277         gtk_container_remove(GTK_CONTAINER(ui->sensor_box),
278                              GTK_WIDGET(ui->sensors_scrolled_tree));
279
280         gtk_container_remove(GTK_CONTAINER(ui->sensor_box), ui->w_graph);
281
282         gtk_container_remove(GTK_CONTAINER(ui->main_box),
283                              GTK_WIDGET(ui->sensor_box));
284
285         if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
286             || cfg->sensorlist_position == SENSORLIST_POSITION_LEFT)
287                 ui->sensor_box
288                         = GTK_PANED(gtk_paned_new(GTK_ORIENTATION_HORIZONTAL));
289         else
290                 ui->sensor_box
291                         = GTK_PANED(gtk_paned_new(GTK_ORIENTATION_VERTICAL));
292
293         gtk_box_pack_end(GTK_BOX(ui->main_box),
294                          GTK_WIDGET(ui->sensor_box), TRUE, TRUE, 2);
295
296         if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
297             || cfg->sensorlist_position == SENSORLIST_POSITION_BOTTOM) {
298                 gtk_paned_pack1(ui->sensor_box,
299                                 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
300                 gtk_paned_pack2(ui->sensor_box,
301                                 GTK_WIDGET(ui->sensors_scrolled_tree),
302                                 FALSE, TRUE);
303         } else {
304                 gtk_paned_pack1(ui->sensor_box,
305                                 GTK_WIDGET(ui->sensors_scrolled_tree),
306                                 FALSE, TRUE);
307                 gtk_paned_pack2(ui->sensor_box,
308                                 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
309         }
310
311         if (cfg->window_restore_enabled)
312                 gtk_paned_set_position(ui->sensor_box, cfg->window_divider_pos);
313
314         g_object_unref(GTK_WIDGET(ui->sensors_scrolled_tree));
315         g_object_unref(GTK_WIDGET(ui->w_graph));
316
317         gtk_widget_show_all(GTK_WIDGET(ui->sensor_box));
318
319         if (cfg->menu_bar_disabled)
320                 menu_bar_show(0, ui);
321         else
322                 menu_bar_show(1, ui);
323 }
324
325 void ui_window_show(struct ui_psensor *ui)
326 {
327         log_debug("ui_window_show()");
328         ui_window_update(ui);
329         gtk_window_present(GTK_WINDOW(ui->main_window));
330 }
331
332 static int cmp_sensors(const void *p1, const void *p2)
333 {
334         const struct psensor *s1, *s2;
335         int pos1, pos2;
336
337         s1 = *(void **)p1;
338         s2 = *(void **)p2;
339
340         pos1 = config_get_sensor_position(s1->id);
341         pos2 = config_get_sensor_position(s2->id);
342
343         return pos1 - pos2;
344 }
345
346 struct psensor **ui_get_sensors_ordered_by_position(const struct ui_psensor *ui)
347 {
348         struct psensor **result;
349
350         result = psensor_list_copy(ui->sensors);
351         qsort(result,
352               psensor_list_size(result),
353               sizeof(struct psensor *),
354               cmp_sensors);
355
356         return result;
357 }