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