041e8d8b10b8973122501547c40b96899211b9e6
[psensor-pkg-debian.git] / src / ui.c
1 /*
2  * Copyright (C) 2010-2016 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_appindicator.h>
25 #include <ui_graph.h>
26 #include <ui_pref.h>
27 #include <ui_sensorlist.h>
28 #include <ui_sensorpref.h>
29 #include <ui_status.h>
30
31 static GtkWidget *w_sensors_scrolled_tree;
32 static GtkWidget *w_graph;
33 static GtkContainer *w_sensor_box;
34 static GtkContainer *w_main_box;
35
36 static void update_layout(void)
37 {
38         enum sensorlist_position sensorlist_pos;
39
40         g_object_ref(w_sensors_scrolled_tree);
41         g_object_ref(w_graph);
42
43         gtk_container_remove(w_sensor_box,
44                              w_sensors_scrolled_tree);
45
46         gtk_container_remove(w_sensor_box, w_graph);
47
48         gtk_container_remove(w_main_box, GTK_WIDGET(w_sensor_box));
49
50         sensorlist_pos = config_get_sensorlist_position();
51         if (sensorlist_pos == SENSORLIST_POSITION_RIGHT
52             || sensorlist_pos == SENSORLIST_POSITION_LEFT)
53                 w_sensor_box
54                         = GTK_CONTAINER(gtk_paned_new
55                                         (GTK_ORIENTATION_HORIZONTAL));
56         else
57                 w_sensor_box
58                         = GTK_CONTAINER(gtk_paned_new
59                                         (GTK_ORIENTATION_VERTICAL));
60
61         gtk_box_pack_end(GTK_BOX(w_main_box),
62                          GTK_WIDGET(w_sensor_box), TRUE, TRUE, 2);
63
64         if (sensorlist_pos == SENSORLIST_POSITION_RIGHT
65             || sensorlist_pos == SENSORLIST_POSITION_BOTTOM) {
66                 gtk_paned_pack1(GTK_PANED(w_sensor_box), w_graph, TRUE, TRUE);
67                 gtk_paned_pack2(GTK_PANED(w_sensor_box),
68                                 w_sensors_scrolled_tree,
69                                 FALSE,
70                                 TRUE);
71         } else {
72                 gtk_paned_pack1(GTK_PANED(w_sensor_box),
73                                 w_sensors_scrolled_tree,
74                                 FALSE,
75                                 TRUE);
76                 gtk_paned_pack2(GTK_PANED(w_sensor_box), w_graph, TRUE, TRUE);
77         }
78
79         g_object_unref(w_sensors_scrolled_tree);
80         g_object_unref(w_graph);
81
82         gtk_widget_show_all(GTK_WIDGET(w_sensor_box));
83 }
84
85 static void set_decoration(GtkWindow *win)
86 {
87         gtk_window_set_decorated(win, config_is_window_decoration_enabled());
88 }
89
90 static void set_keep_below(GtkWindow *win)
91 {
92         gtk_window_set_keep_below(win, config_is_window_keep_below_enabled());
93 }
94
95 static void set_menu_bar_enabled(GtkWidget *bar)
96 {
97         if (config_is_menu_bar_enabled())
98                 gtk_widget_show(bar);
99         else
100                 gtk_widget_hide(bar);
101 }
102
103 static void
104 decoration_changed_cbk(GSettings *settings, gchar *key, gpointer data)
105 {
106         set_decoration(GTK_WINDOW(data));
107 }
108
109 static void
110 keep_below_changed_cbk(GSettings *settings, gchar *key, gpointer data)
111 {
112         set_keep_below(GTK_WINDOW(data));
113 }
114
115 static void
116 menu_bar_changed_cbk(GSettings *settings, gchar *key, gpointer data)
117 {
118         set_menu_bar_enabled(GTK_WIDGET(data));
119 }
120
121 static void
122 sensorlist_position_changed_cbk(GSettings *settings, gchar *key, gpointer data)
123 {
124         update_layout();
125 }
126
127 static void connect_cbks(GtkWindow *win, GtkWidget *menu_bar)
128 {
129         log_fct_enter();
130
131         g_signal_connect_after(config_get_GSettings(),
132                                "changed::interface-window-decoration-disabled",
133                                G_CALLBACK(decoration_changed_cbk),
134                                win);
135
136         g_signal_connect_after(config_get_GSettings(),
137                                "changed::interface-window-keep-below-enabled",
138                                G_CALLBACK(keep_below_changed_cbk),
139                                win);
140
141         g_signal_connect_after(config_get_GSettings(),
142                                "changed::interface-menu-bar-disabled",
143                                G_CALLBACK(menu_bar_changed_cbk),
144                                menu_bar);
145
146         g_signal_connect_after(config_get_GSettings(),
147                                "changed::interface-sensorlist-position",
148                                G_CALLBACK(sensorlist_position_changed_cbk),
149                                menu_bar);
150
151
152         log_fct_exit();
153 }
154
155 static void save_window_pos(struct ui_psensor *ui)
156 {
157         gboolean visible;
158         GtkWindow *win;
159         struct config *cfg;
160
161         visible = gtk_widget_get_visible(ui->main_window);
162         log_debug("Window visible: %d", visible);
163
164         if (visible == TRUE) {
165                 cfg = ui->config;
166
167                 win = GTK_WINDOW(ui->main_window);
168
169                 gtk_window_get_position(win, &cfg->window_x, &cfg->window_y);
170                 log_debug("Window position: %d %d",
171                           cfg->window_x,
172                           cfg->window_y);
173
174                 gtk_window_get_size(win,
175                                     &cfg->window_w,
176                                     &cfg->window_h);
177                 log_debug("Window size: %d %d", cfg->window_w, cfg->window_h);
178
179                 cfg->window_divider_pos
180                         = gtk_paned_get_position(GTK_PANED(w_sensor_box));
181
182                 config_save(cfg);
183         }
184 }
185
186 static gboolean
187 on_delete_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
188 {
189         struct ui_psensor *ui = data;
190
191         save_window_pos(ui);
192
193         log_debug("is_status_supported: %d\n", is_status_supported());
194
195         if (is_appindicator_supported() || is_status_supported())
196                 gtk_widget_hide(ui->main_window);
197         else
198                 ui_psensor_quit(ui);
199
200         return TRUE;
201 }
202
203 void ui_show_about_dialog(GtkWindow *parent)
204 {
205         static const char *const authors[] = { "jeanfi@gmail.com", NULL };
206
207         log_fct("parent=%p", parent);
208
209         gtk_show_about_dialog
210                 (parent,
211                  "authors", authors,
212                  "comments",
213                  _("Psensor is a GTK+ application for monitoring hardware "
214                    "sensors"),
215                  "copyright",
216                  _("Copyright(c) 2010-2016 jeanfi@gmail.com"),
217 #if GTK_CHECK_VERSION(3, 12, 0)
218                  "license-type", GTK_LICENSE_GPL_2_0,
219 #endif
220                  "logo-icon-name", "psensor",
221                  "program-name", "Psensor",
222                  "title", _("About Psensor"),
223                  "translator-credits", _("translator-credits"),
224                  "version", VERSION,
225                  "website", PACKAGE_URL,
226                  "website-label", _("Psensor Homepage"),
227                  NULL);
228 }
229
230 void ui_cb_about(GtkAction *a, gpointer data)
231 {
232         struct ui_psensor *ui;
233         GtkWidget *parent;
234
235         ui = (struct ui_psensor *)data;
236
237         log_fct("ui=%p", ui);
238
239         if (ui)
240                 parent = ui->main_window;
241         else
242                 parent = NULL;
243
244         ui_show_about_dialog(GTK_WINDOW(parent));
245 }
246
247 void ui_cb_menu_quit(GtkMenuItem *mi, gpointer data)
248 {
249         ui_psensor_quit((struct ui_psensor *)data);
250 }
251
252 void ui_cb_preferences(GtkMenuItem *mi, gpointer data)
253 {
254         ui_pref_dialog_run((struct ui_psensor *)data);
255 }
256
257 void ui_cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
258 {
259         struct ui_psensor *ui = data;
260
261         if (ui->sensors && *ui->sensors)
262                 ui_sensorpref_dialog_run(*ui->sensors, ui);
263 }
264
265 void ui_psensor_quit(struct ui_psensor *ui)
266 {
267         save_window_pos(ui);
268
269         log_debug("Destroy main window");
270         gtk_widget_destroy(ui->main_window);
271         gtk_main_quit();
272 }
273
274 void ui_enable_alpha_channel(struct ui_psensor *ui)
275 {
276         GdkScreen *screen;
277         GdkVisual *visual;
278         struct config *cfg;
279
280         cfg = ui->config;
281
282         screen = gtk_widget_get_screen(ui->main_window);
283
284         log_debug("Config alpha channel enabled: %d",
285                   cfg->alpha_channel_enabled);
286         if (cfg->alpha_channel_enabled && gdk_screen_is_composited(screen)) {
287                 log_debug("Screen is composited");
288                 visual = gdk_screen_get_rgba_visual(screen);
289                 if (visual) {
290                         gtk_widget_set_visual(ui->main_window, visual);
291                 } else {
292                         cfg->alpha_channel_enabled = 0;
293                         log_err("Enable alpha channel has failed");
294                 }
295         } else {
296                 cfg->alpha_channel_enabled = 0;
297         }
298 }
299
300 static void slog_enabled_cbk(void *data)
301 {
302         struct ui_psensor *ui;
303         struct psensor **sensors;
304         pthread_mutex_t *mutex;
305
306         ui = (struct ui_psensor *)data;
307         sensors = ui->sensors;
308         mutex = &ui->sensors_mutex;
309
310         log_debug("slog_enabled_cbk");
311
312         if (is_slog_enabled())
313                 slog_activate(NULL, sensors, mutex, config_get_slog_interval());
314         else
315                 slog_close();
316 }
317
318 void ui_window_create(struct ui_psensor *ui)
319 {
320         GtkWidget *window, *menu_bar;
321         GdkPixbuf *icon;
322         GtkIconTheme *icon_theme;
323         struct config *cfg;
324         guint ok;
325         GtkBuilder *builder;
326         GError *error;
327
328         log_fct("ui=%p", ui);
329
330         builder = gtk_builder_new();
331
332         error = NULL;
333         ok = gtk_builder_add_from_file
334                 (builder,
335                  PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "psensor.glade",
336                  &error);
337
338         if (!ok) {
339                 log_printf(LOG_ERR, error->message);
340                 g_error_free(error);
341                 return;
342         }
343
344         window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
345         gtk_builder_connect_signals(builder, ui);
346         cfg = ui->config;
347         if (cfg->window_restore_enabled)
348                 gtk_window_move(GTK_WINDOW(window),
349                                 cfg->window_x,
350                                 cfg->window_y);
351
352         config_set_slog_enabled_changed_cbk(slog_enabled_cbk, ui);
353
354         gtk_window_set_default_size(GTK_WINDOW(window),
355                                     cfg->window_w,
356                                     cfg->window_h);
357
358         icon_theme = gtk_icon_theme_get_default();
359         icon = gtk_icon_theme_load_icon(icon_theme, "psensor", 48, 0, NULL);
360         if (icon)
361                 gtk_window_set_icon(GTK_WINDOW(window), icon);
362         else
363                 log_err(_("Failed to load Psensor icon."));
364
365         g_signal_connect(window,
366                          "delete_event", G_CALLBACK(on_delete_event_cb), ui);
367
368         set_decoration(GTK_WINDOW(window));
369         set_keep_below(GTK_WINDOW(window));
370
371         menu_bar = GTK_WIDGET(gtk_builder_get_object(builder, "menu_bar"));
372         w_main_box = GTK_CONTAINER(gtk_builder_get_object(builder, "main_box"));
373         ui->popup_menu = GTK_WIDGET(gtk_builder_get_object(builder,
374                                                            "popup_menu"));
375         g_object_ref(G_OBJECT(ui->popup_menu));
376         ui->main_window = window;
377         w_graph = GTK_WIDGET(gtk_builder_get_object(builder, "graph"));
378         ui_graph_create(ui);
379
380         w_sensor_box = GTK_CONTAINER(gtk_builder_get_object(builder,
381                                                             "sensor_box"));
382         ui->sensors_store = GTK_LIST_STORE(gtk_builder_get_object
383                                            (builder, "sensors_store"));
384         ui->sensors_tree = GTK_TREE_VIEW(gtk_builder_get_object
385                                          (builder, "sensors_tree"));
386         w_sensors_scrolled_tree
387                 = GTK_WIDGET(gtk_builder_get_object
388                              (builder, "sensors_scrolled_tree"));
389
390         ui_sensorlist_create(ui);
391
392         connect_cbks(GTK_WINDOW(window), menu_bar);
393
394         log_debug("ui_window_create(): show_all");
395         update_layout();
396         gtk_widget_show_all(GTK_WIDGET(w_main_box));
397         set_menu_bar_enabled(menu_bar);
398
399         g_object_unref(G_OBJECT(builder));
400
401         log_debug("ui_window_create() ends");
402 }
403
404 void ui_window_update(struct ui_psensor *ui)
405 {
406         struct config *cfg;
407
408         log_debug("ui_window_update()");
409
410         cfg = ui->config;
411
412         if (cfg->window_restore_enabled)
413                 gtk_paned_set_position(GTK_PANED(w_sensor_box),
414                                        cfg->window_divider_pos);
415
416 }
417
418 void ui_window_show(struct ui_psensor *ui)
419 {
420         log_debug("ui_window_show()");
421         ui_window_update(ui);
422         gtk_window_present(GTK_WINDOW(ui->main_window));
423 }
424
425 static int cmp_sensors(const void *p1, const void *p2)
426 {
427         const struct psensor *s1, *s2;
428         int pos1, pos2;
429
430         s1 = *(void **)p1;
431         s2 = *(void **)p2;
432
433         pos1 = config_get_sensor_position(s1->id);
434         pos2 = config_get_sensor_position(s2->id);
435
436         return pos1 - pos2;
437 }
438
439 struct psensor **ui_get_sensors_ordered_by_position(struct psensor **sensors)
440 {
441         struct psensor **result;
442
443         result = psensor_list_copy(sensors);
444         qsort(result,
445               psensor_list_size(result),
446               sizeof(struct psensor *),
447               cmp_sensors);
448
449         return result;
450 }
451
452 GtkWidget *ui_get_graph(void)
453 {
454         return w_graph;
455 }