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