2 * Copyright (C) 2010-2016 jeanfi@gmail.com
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.
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.
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
24 #include <ui_appindicator.h>
27 #include <ui_sensorlist.h>
28 #include <ui_sensorpref.h>
29 #include <ui_status.h>
31 static GtkWidget *w_sensors_scrolled_tree;
32 static GtkWidget *w_graph;
33 static GtkContainer *w_sensor_box;
34 static GtkContainer *w_main_box;
36 static void update_layout(void)
38 enum sensorlist_position sensorlist_pos;
40 g_object_ref(w_sensors_scrolled_tree);
41 g_object_ref(w_graph);
43 gtk_container_remove(w_sensor_box,
44 w_sensors_scrolled_tree);
46 gtk_container_remove(w_sensor_box, w_graph);
48 gtk_container_remove(w_main_box, GTK_WIDGET(w_sensor_box));
50 sensorlist_pos = config_get_sensorlist_position();
51 if (sensorlist_pos == SENSORLIST_POSITION_RIGHT
52 || sensorlist_pos == SENSORLIST_POSITION_LEFT)
54 = GTK_CONTAINER(gtk_paned_new
55 (GTK_ORIENTATION_HORIZONTAL));
58 = GTK_CONTAINER(gtk_paned_new
59 (GTK_ORIENTATION_VERTICAL));
61 gtk_box_pack_end(GTK_BOX(w_main_box),
62 GTK_WIDGET(w_sensor_box), TRUE, TRUE, 2);
64 if (sensorlist_pos == SENSORLIST_POSITION_RIGHT
65 || sensorlist_pos == SENSORLIST_POSITION_BOTTOM) {
66 gtk_paned_pack1(GTK_PANED(w_sensor_box), w_graph, TRUE, TRUE);
67 gtk_paned_pack2(GTK_PANED(w_sensor_box),
68 w_sensors_scrolled_tree,
72 gtk_paned_pack1(GTK_PANED(w_sensor_box),
73 w_sensors_scrolled_tree,
76 gtk_paned_pack2(GTK_PANED(w_sensor_box), w_graph, TRUE, TRUE);
79 g_object_unref(w_sensors_scrolled_tree);
80 g_object_unref(w_graph);
82 gtk_widget_show_all(GTK_WIDGET(w_sensor_box));
85 static void set_decoration(GtkWindow *win)
87 gtk_window_set_decorated(win, config_is_window_decoration_enabled());
90 static void set_keep_below(GtkWindow *win)
92 gtk_window_set_keep_below(win, config_is_window_keep_below_enabled());
95 static void set_menu_bar_enabled(GtkWidget *bar)
97 if (config_is_menu_bar_enabled())
100 gtk_widget_hide(bar);
104 decoration_changed_cbk(GSettings *settings, gchar *key, gpointer data)
106 set_decoration(GTK_WINDOW(data));
110 keep_below_changed_cbk(GSettings *settings, gchar *key, gpointer data)
112 set_keep_below(GTK_WINDOW(data));
116 menu_bar_changed_cbk(GSettings *settings, gchar *key, gpointer data)
118 set_menu_bar_enabled(GTK_WIDGET(data));
122 sensorlist_position_changed_cbk(GSettings *settings, gchar *key, gpointer data)
127 static void connect_cbks(GtkWindow *win, GtkWidget *menu_bar)
131 g_signal_connect_after(config_get_GSettings(),
132 "changed::interface-window-decoration-disabled",
133 G_CALLBACK(decoration_changed_cbk),
136 g_signal_connect_after(config_get_GSettings(),
137 "changed::interface-window-keep-below-enabled",
138 G_CALLBACK(keep_below_changed_cbk),
141 g_signal_connect_after(config_get_GSettings(),
142 "changed::interface-menu-bar-disabled",
143 G_CALLBACK(menu_bar_changed_cbk),
146 g_signal_connect_after(config_get_GSettings(),
147 "changed::interface-sensorlist-position",
148 G_CALLBACK(sensorlist_position_changed_cbk),
155 static void save_window_pos(struct ui_psensor *ui)
161 visible = gtk_widget_get_visible(ui->main_window);
162 log_debug("Window visible: %d", visible);
164 if (visible == TRUE) {
167 win = GTK_WINDOW(ui->main_window);
169 gtk_window_get_position(win, &cfg->window_x, &cfg->window_y);
170 log_debug("Window position: %d %d",
174 gtk_window_get_size(win,
177 log_debug("Window size: %d %d", cfg->window_w, cfg->window_h);
179 cfg->window_divider_pos
180 = gtk_paned_get_position(GTK_PANED(w_sensor_box));
187 on_delete_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
189 struct ui_psensor *ui = data;
193 log_debug("is_status_supported: %d\n", is_status_supported());
195 if (is_appindicator_supported() || is_status_supported())
196 gtk_widget_hide(ui->main_window);
203 void ui_show_about_dialog(GtkWindow *parent)
205 static const char *const authors[] = { "jeanfi@gmail.com", NULL };
207 log_fct("parent=%p", parent);
209 gtk_show_about_dialog
213 _("Psensor is a GTK+ application for monitoring hardware "
216 _("Copyright(c) 2010-2016 jeanfi@gmail.com"),
217 #if GTK_CHECK_VERSION(3, 12, 0)
218 "license-type", GTK_LICENSE_GPL_2_0,
220 "logo-icon-name", "psensor",
221 "program-name", "Psensor",
222 "title", _("About Psensor"),
223 "translator-credits", _("translator-credits"),
225 "website", PACKAGE_URL,
226 "website-label", _("Psensor Homepage"),
230 void ui_cb_about(GtkAction *a, gpointer data)
232 struct ui_psensor *ui;
235 ui = (struct ui_psensor *)data;
237 log_fct("ui=%p", ui);
240 parent = ui->main_window;
244 ui_show_about_dialog(GTK_WINDOW(parent));
247 void ui_cb_menu_quit(GtkMenuItem *mi, gpointer data)
249 ui_psensor_quit((struct ui_psensor *)data);
252 void ui_cb_preferences(GtkMenuItem *mi, gpointer data)
254 ui_pref_dialog_run((struct ui_psensor *)data);
257 void ui_cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
259 struct ui_psensor *ui = data;
261 if (ui->sensors && *ui->sensors)
262 ui_sensorpref_dialog_run(*ui->sensors, ui);
265 void ui_psensor_quit(struct ui_psensor *ui)
269 log_debug("Destroy main window");
270 gtk_widget_destroy(ui->main_window);
274 void ui_enable_alpha_channel(struct ui_psensor *ui)
282 screen = gtk_widget_get_screen(ui->main_window);
284 log_debug("Config alpha channel enabled: %d",
285 cfg->alpha_channel_enabled);
286 if (cfg->alpha_channel_enabled && gdk_screen_is_composited(screen)) {
287 log_debug("Screen is composited");
288 visual = gdk_screen_get_rgba_visual(screen);
290 gtk_widget_set_visual(ui->main_window, visual);
292 cfg->alpha_channel_enabled = 0;
293 log_err("Enable alpha channel has failed");
296 cfg->alpha_channel_enabled = 0;
300 static void slog_enabled_cbk(void *data)
302 struct ui_psensor *ui;
303 struct psensor **sensors;
304 pthread_mutex_t *mutex;
306 ui = (struct ui_psensor *)data;
307 sensors = ui->sensors;
308 mutex = &ui->sensors_mutex;
310 log_debug("slog_enabled_cbk");
312 if (is_slog_enabled())
313 slog_activate(NULL, sensors, mutex, config_get_slog_interval());
318 void ui_window_create(struct ui_psensor *ui)
320 GtkWidget *window, *menu_bar;
322 GtkIconTheme *icon_theme;
328 log_fct("ui=%p", ui);
330 builder = gtk_builder_new();
333 ok = gtk_builder_add_from_file
335 PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "psensor.glade",
339 log_printf(LOG_ERR, error->message);
344 window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
345 gtk_builder_connect_signals(builder, ui);
347 if (cfg->window_restore_enabled)
348 gtk_window_move(GTK_WINDOW(window),
352 config_set_slog_enabled_changed_cbk(slog_enabled_cbk, ui);
354 gtk_window_set_default_size(GTK_WINDOW(window),
358 icon_theme = gtk_icon_theme_get_default();
359 icon = gtk_icon_theme_load_icon(icon_theme, "psensor", 48, 0, NULL);
361 gtk_window_set_icon(GTK_WINDOW(window), icon);
363 log_err(_("Failed to load Psensor icon."));
365 g_signal_connect(window,
366 "delete_event", G_CALLBACK(on_delete_event_cb), ui);
368 set_decoration(GTK_WINDOW(window));
369 set_keep_below(GTK_WINDOW(window));
371 menu_bar = GTK_WIDGET(gtk_builder_get_object(builder, "menu_bar"));
372 w_main_box = GTK_CONTAINER(gtk_builder_get_object(builder, "main_box"));
373 ui->popup_menu = GTK_WIDGET(gtk_builder_get_object(builder,
375 g_object_ref(G_OBJECT(ui->popup_menu));
376 ui->main_window = window;
377 w_graph = GTK_WIDGET(gtk_builder_get_object(builder, "graph"));
380 w_sensor_box = GTK_CONTAINER(gtk_builder_get_object(builder,
382 ui->sensors_store = GTK_LIST_STORE(gtk_builder_get_object
383 (builder, "sensors_store"));
384 ui->sensors_tree = GTK_TREE_VIEW(gtk_builder_get_object
385 (builder, "sensors_tree"));
386 w_sensors_scrolled_tree
387 = GTK_WIDGET(gtk_builder_get_object
388 (builder, "sensors_scrolled_tree"));
390 ui_sensorlist_create(ui);
392 connect_cbks(GTK_WINDOW(window), menu_bar);
394 log_debug("ui_window_create(): show_all");
396 gtk_widget_show_all(GTK_WIDGET(w_main_box));
397 set_menu_bar_enabled(menu_bar);
399 g_object_unref(G_OBJECT(builder));
401 log_debug("ui_window_create() ends");
404 void ui_window_update(struct ui_psensor *ui)
408 log_debug("ui_window_update()");
412 if (cfg->window_restore_enabled)
413 gtk_paned_set_position(GTK_PANED(w_sensor_box),
414 cfg->window_divider_pos);
418 void ui_window_show(struct ui_psensor *ui)
420 log_debug("ui_window_show()");
421 ui_window_update(ui);
422 gtk_window_present(GTK_WINDOW(ui->main_window));
425 static int cmp_sensors(const void *p1, const void *p2)
427 const struct psensor *s1, *s2;
433 pos1 = config_get_sensor_position(s1->id);
434 pos2 = config_get_sensor_position(s2->id);
439 struct psensor **ui_get_sensors_ordered_by_position(struct psensor **sensors)
441 struct psensor **result;
443 result = psensor_list_copy(sensors);
445 psensor_list_size(result),
446 sizeof(struct psensor *),
452 GtkWidget *ui_get_graph(void)