Merge tag 'upstream/1.1.5'
[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         static const char *const authors[] = { "jeanfi@gmail.com", NULL };
82
83         gtk_show_about_dialog
84                 (NULL,
85                  "authors", authors,
86                  "comments",
87                  _("Psensor is a GTK+ application for monitoring hardware "
88                    "sensors"),
89                  "copyright",
90                  _("Copyright(c) 2010-2014 jeanfi@gmail.com"),
91 #if GTK_CHECK_VERSION(3, 12, 0)
92                  "license-type", GTK_LICENSE_GPL_2_0,
93 #endif
94                  "logo-icon-name", "psensor",
95                  "program-name", "Psensor",
96                  "title", _("About Psensor"),
97                  "translator-credits", _("translator-credits"),
98                  "version", VERSION,
99                  "website", PACKAGE_URL,
100                  "website-label", _("Psensor Homepage"),
101                  NULL);
102 }
103
104 void ui_cb_about(GtkMenuItem *mi, gpointer data)
105 {
106         ui_show_about_dialog();
107 }
108
109 void ui_cb_menu_quit(GtkMenuItem *mi, gpointer data)
110 {
111         ui_psensor_quit((struct ui_psensor *)data);
112 }
113
114 void ui_cb_preferences(GtkMenuItem *mi, gpointer data)
115 {
116         ui_pref_dialog_run((struct ui_psensor *)data);
117 }
118
119 void ui_cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
120 {
121         struct ui_psensor *ui = data;
122
123         if (ui->sensors && *ui->sensors)
124                 ui_sensorpref_dialog_run(*ui->sensors, ui);
125 }
126
127 void ui_psensor_quit(struct ui_psensor *ui)
128 {
129         save_window_pos(ui);
130
131         log_debug("Destroy main window");
132         gtk_widget_destroy(ui->main_window);
133         gtk_main_quit();
134 }
135
136 void ui_enable_alpha_channel(struct ui_psensor *ui)
137 {
138         GdkScreen *screen;
139         GdkVisual *visual;
140         struct config *cfg;
141
142         cfg = ui->config;
143
144         screen = gtk_widget_get_screen(ui->main_window);
145
146         log_debug("Config alpha channel enabled: %d",
147                   cfg->alpha_channel_enabled);
148         if (cfg->alpha_channel_enabled && gdk_screen_is_composited(screen)) {
149                 log_debug("Screen is composited");
150                 visual = gdk_screen_get_rgba_visual(screen);
151                 if (visual) {
152                         gtk_widget_set_visual(ui->main_window, visual);
153                 } else {
154                         cfg->alpha_channel_enabled = 0;
155                         log_err("Enable alpha channel has failed");
156                 }
157         } else {
158                 cfg->alpha_channel_enabled = 0;
159         }
160 }
161
162 static void slog_enabled_cbk(void *data)
163 {
164         struct ui_psensor *ui;
165         struct psensor **sensors;
166         pthread_mutex_t *mutex;
167
168         ui = (struct ui_psensor *)data;
169         sensors = ui->sensors;
170         mutex = &ui->sensors_mutex;
171
172         log_debug("slog_enabled_cbk");
173
174         if (is_slog_enabled())
175                 slog_activate(NULL, sensors, mutex, config_get_slog_interval());
176         else
177                 slog_close();
178 }
179
180 void ui_window_create(struct ui_psensor *ui)
181 {
182         GtkWidget *window;
183         GdkPixbuf *icon;
184         GtkIconTheme *icon_theme;
185         struct config *cfg;
186         guint ok;
187         GtkBuilder *builder;
188         GError *error;
189
190         builder = gtk_builder_new();
191
192         error = NULL;
193         ok = gtk_builder_add_from_file
194                 (builder,
195                  PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "psensor.glade",
196                  &error);
197
198         if (!ok) {
199                 log_printf(LOG_ERR, error->message);
200                 g_error_free(error);
201                 return;
202         }
203
204         window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
205         gtk_builder_connect_signals(builder, ui);
206         cfg = ui->config;
207         if (cfg->window_restore_enabled)
208                 gtk_window_move(GTK_WINDOW(window),
209                                 cfg->window_x,
210                                 cfg->window_y);
211
212         config_set_slog_enabled_changed_cbk(slog_enabled_cbk, ui);
213
214         gtk_window_set_default_size(GTK_WINDOW(window),
215                                     cfg->window_w,
216                                     cfg->window_h);
217
218         icon_theme = gtk_icon_theme_get_default();
219         icon = gtk_icon_theme_load_icon(icon_theme, "psensor", 48, 0, NULL);
220         if (icon)
221                 gtk_window_set_icon(GTK_WINDOW(window), icon);
222         else
223                 log_err(_("Failed to load Psensor icon."));
224
225         g_signal_connect(window,
226                          "delete_event", G_CALLBACK(on_delete_event_cb), ui);
227
228         gtk_window_set_decorated(GTK_WINDOW(window),
229                                  cfg->window_decoration_enabled);
230
231         gtk_window_set_keep_below(GTK_WINDOW(window),
232                                   cfg->window_keep_below_enabled);
233
234         ui->menu_bar = GTK_WIDGET(gtk_builder_get_object(builder, "menu_bar"));
235         ui->main_box = GTK_WIDGET(gtk_builder_get_object(builder, "main_box"));
236         ui->popup_menu = GTK_WIDGET(gtk_builder_get_object(builder,
237                                                            "popup_menu"));
238         g_object_ref(G_OBJECT(ui->popup_menu));
239         ui->main_window = window;
240         ui->w_graph = GTK_WIDGET(gtk_builder_get_object(builder,
241                                                         "graph"));
242         ui_graph_create(ui);
243
244         ui->sensor_box = GTK_PANED(gtk_builder_get_object(builder,
245                                                           "sensor_box"));
246         ui->sensors_store = GTK_LIST_STORE(gtk_builder_get_object
247                                            (builder, "sensors_store"));
248         ui->sensors_tree = GTK_TREE_VIEW(gtk_builder_get_object
249                                          (builder, "sensors_tree"));
250         ui->sensors_scrolled_tree
251                 = GTK_SCROLLED_WINDOW(gtk_builder_get_object
252                                       (builder, "sensors_scrolled_tree"));
253
254         ui_sensorlist_create(ui);
255
256         log_debug("ui_window_create(): show_all");
257         gtk_widget_show_all(ui->main_box);
258
259         g_object_unref(G_OBJECT(builder));
260
261         log_debug("ui_window_create() ends");
262 }
263
264 static void menu_bar_show(unsigned int show, struct ui_psensor *ui)
265 {
266         if (show)
267                 gtk_widget_show(ui->menu_bar);
268         else
269                 gtk_widget_hide(ui->menu_bar);
270 }
271
272 void ui_window_update(struct ui_psensor *ui)
273 {
274         struct config *cfg;
275
276         log_debug("ui_window_update()");
277
278         cfg = ui->config;
279
280         g_object_ref(GTK_WIDGET(ui->sensors_scrolled_tree));
281         g_object_ref(GTK_WIDGET(ui->w_graph));
282
283         gtk_container_remove(GTK_CONTAINER(ui->sensor_box),
284                              GTK_WIDGET(ui->sensors_scrolled_tree));
285
286         gtk_container_remove(GTK_CONTAINER(ui->sensor_box), ui->w_graph);
287
288         gtk_container_remove(GTK_CONTAINER(ui->main_box),
289                              GTK_WIDGET(ui->sensor_box));
290
291         if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
292             || cfg->sensorlist_position == SENSORLIST_POSITION_LEFT)
293                 ui->sensor_box
294                         = GTK_PANED(gtk_paned_new(GTK_ORIENTATION_HORIZONTAL));
295         else
296                 ui->sensor_box
297                         = GTK_PANED(gtk_paned_new(GTK_ORIENTATION_VERTICAL));
298
299         gtk_box_pack_end(GTK_BOX(ui->main_box),
300                          GTK_WIDGET(ui->sensor_box), TRUE, TRUE, 2);
301
302         if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
303             || cfg->sensorlist_position == SENSORLIST_POSITION_BOTTOM) {
304                 gtk_paned_pack1(ui->sensor_box,
305                                 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
306                 gtk_paned_pack2(ui->sensor_box,
307                                 GTK_WIDGET(ui->sensors_scrolled_tree),
308                                 FALSE, TRUE);
309         } else {
310                 gtk_paned_pack1(ui->sensor_box,
311                                 GTK_WIDGET(ui->sensors_scrolled_tree),
312                                 FALSE, TRUE);
313                 gtk_paned_pack2(ui->sensor_box,
314                                 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
315         }
316
317         if (cfg->window_restore_enabled)
318                 gtk_paned_set_position(ui->sensor_box, cfg->window_divider_pos);
319
320         g_object_unref(GTK_WIDGET(ui->sensors_scrolled_tree));
321         g_object_unref(GTK_WIDGET(ui->w_graph));
322
323         gtk_widget_show_all(GTK_WIDGET(ui->sensor_box));
324
325         if (cfg->menu_bar_disabled)
326                 menu_bar_show(0, ui);
327         else
328                 menu_bar_show(1, ui);
329 }
330
331 void ui_window_show(struct ui_psensor *ui)
332 {
333         log_debug("ui_window_show()");
334         ui_window_update(ui);
335         gtk_window_present(GTK_WINDOW(ui->main_window));
336 }
337
338 static int cmp_sensors(const void *p1, const void *p2)
339 {
340         const struct psensor *s1, *s2;
341         int pos1, pos2;
342
343         s1 = *(void **)p1;
344         s2 = *(void **)p2;
345
346         pos1 = config_get_sensor_position(s1->id);
347         pos2 = config_get_sensor_position(s2->id);
348
349         return pos1 - pos2;
350 }
351
352 struct psensor **ui_get_sensors_ordered_by_position(const struct ui_psensor *ui)
353 {
354         struct psensor **result;
355
356         result = psensor_list_copy(ui->sensors);
357         qsort(result,
358               psensor_list_size(result),
359               sizeof(struct psensor *),
360               cmp_sensors);
361
362         return result;
363 }