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