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