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