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