log_printf can be called concurently (removed static buffer)
[psensor.git] / src / ui.c
1 /*
2     Copyright (C) 2010-2011 jeanfi@gmail.com
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU 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
20 #include "cfg.h"
21 #include "ui.h"
22 #include "ui_graph.h"
23 #include "ui_pref.h"
24 #include "ui_sensorpref.h"
25 #include "ui_sensorlist.h"
26 #include "ui_appindicator.h"
27
28 static void save_window_pos(struct ui_psensor *ui)
29 {
30         gboolean visible;
31         GtkWindow *win;
32         struct config *cfg;
33
34         visible = gtk_widget_get_visible(ui->main_window);
35         log_printf(LOG_DEBUG, "Window visible: %d", visible);
36
37         if (visible == TRUE) {
38                 cfg = ui->config;
39
40                 win = GTK_WINDOW(ui->main_window);
41
42                 gtk_window_get_position(win, &cfg->window_x, &cfg->window_y);
43                 log_printf(LOG_DEBUG,
44                            "Window position: %d %d",
45                            cfg->window_x,
46                            cfg->window_y);
47
48                 gtk_window_get_size(win,
49                                     &cfg->window_w,
50                                     &cfg->window_h);
51                 log_printf(LOG_DEBUG,
52                            "Window size: %d %d",
53                            cfg->window_w,
54                            cfg->window_h);
55
56                 cfg->window_divider_pos
57                         = gtk_paned_get_position(GTK_PANED(ui->sensor_box));
58
59                 config_save(cfg);
60         }
61 }
62
63 static gboolean
64 on_delete_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
65 {
66         struct ui_psensor *ui = data;
67
68         save_window_pos(ui);
69
70 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
71         if (is_appindicator_supported()) {
72                 log_printf(LOG_DEBUG, "hiding, WM is supporting appindicator");
73                 gtk_widget_hide(ui->main_window);
74         } else {
75                 log_printf(LOG_DEBUG,
76                            "quitting, WM not supporting appindicator");
77                 ui_psensor_quit(ui);
78         }
79 #else
80         ui_psensor_quit(ui);
81 #endif
82
83         return TRUE;
84 }
85
86 void ui_show_about_dialog()
87 {
88         gtk_show_about_dialog(NULL,
89                               "comments",
90                               _("Psensor is a GTK+ application for monitoring "
91                                 "hardware sensors"),
92                               "copyright",
93                               _("Copyright(c) 2010-2011\njeanfi@gmail.com"),
94                               "logo-icon-name", "psensor",
95                               "program-name", "Psensor",
96                               "title", _("About Psensor"),
97                               "version", VERSION,
98                               "website", PACKAGE_URL,
99                               "website-label", _("Psensor Homepage"),
100                               NULL);
101 }
102
103 static void cb_about(GtkMenuItem *mi, gpointer data)
104 {
105         ui_show_about_dialog();
106 }
107
108 static void cb_menu_quit(GtkMenuItem *mi, gpointer data)
109 {
110         ui_psensor_quit((struct ui_psensor *)data);
111 }
112
113 static void cb_preferences(GtkMenuItem *mi, gpointer data)
114 {
115         ui_pref_dialog_run((struct ui_psensor *)data);
116 }
117
118 static void cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
119 {
120         struct ui_psensor *ui = data;
121
122         if (ui->sensors && *ui->sensors)
123                 ui_sensorpref_dialog_run(*ui->sensors, ui);
124 }
125
126 void ui_psensor_quit(struct ui_psensor *ui)
127 {
128         save_window_pos(ui);
129
130         log_printf(LOG_DEBUG, "Destroy main window");
131         gtk_widget_destroy(ui->main_window);
132         gtk_main_quit();
133 }
134
135 static const char *menu_desc =
136 "<ui>"
137 "  <menubar name='MainMenu'>"
138 "    <menu name='Psensor' action='PsensorMenuAction'>"
139 "      <menuitem name='Preferences' action='PreferencesAction' />"
140 "      <menuitem name='SensorPreferences' action='SensorPreferencesAction' />"
141 "      <separator />"
142 "      <menuitem name='Quit' action='QuitAction' />"
143 "    </menu>"
144 "    <menu name='Help' action='HelpMenuAction'>"
145 "      <menuitem name='About' action='AboutAction' />"
146 "    </menu>"
147 "  </menubar>"
148 "</ui>";
149
150 static GtkActionEntry entries[] = {
151   { "PsensorMenuAction", NULL, "_Psensor" }, /* name, stock id, label */
152
153   { "PreferencesAction", GTK_STOCK_PREFERENCES,   /* name, stock id */
154     N_("_Preferences"), NULL,                     /* label, accelerator */
155     N_("Preferences"),                            /* tooltip */
156     G_CALLBACK(cb_preferences) },
157
158   { "SensorPreferencesAction", GTK_STOCK_PREFERENCES,
159     N_("_Sensor Preferences"), NULL,
160     N_("Sensor Preferences"),
161     G_CALLBACK(cb_sensor_preferences) },
162
163   { "QuitAction",
164     GTK_STOCK_QUIT, N_("_Quit"), NULL, N_("Quit"), G_CALLBACK(cb_menu_quit) },
165
166   { "HelpMenuAction", NULL, "_Help" },
167
168   { "AboutAction", GTK_STOCK_PREFERENCES,
169     N_("_About"), NULL,
170     N_("About"),
171     G_CALLBACK(cb_about) }
172 };
173 static guint n_entries = G_N_ELEMENTS(entries);
174
175 static GtkWidget *get_menu(struct ui_psensor *ui)
176 {
177         GtkActionGroup      *action_group;
178         GtkUIManager        *menu_manager;
179         GError              *error;
180
181         action_group = gtk_action_group_new("PsensorActions");
182         gtk_action_group_set_translation_domain(action_group, PACKAGE);
183         menu_manager = gtk_ui_manager_new();
184
185         gtk_action_group_add_actions(action_group, entries, n_entries, ui);
186         gtk_ui_manager_insert_action_group(menu_manager, action_group, 0);
187
188         error = NULL;
189         gtk_ui_manager_add_ui_from_string(menu_manager, menu_desc, -1, &error);
190
191         if (error)
192                 g_error(_("building menus failed: %s"), error->message);
193
194         return gtk_ui_manager_get_widget(menu_manager, "/MainMenu");
195 }
196
197 static unsigned int enable_alpha_channel(GtkWidget *w)
198 {
199         GdkScreen *screen = gtk_widget_get_screen(w);
200
201 #if (GTK_CHECK_VERSION(3, 0, 0))
202         GdkVisual *visual = gdk_screen_get_rgba_visual(screen);
203
204         if (visual) {
205                 gtk_widget_set_visual(w, visual);
206                 return 1;
207         }
208 #else
209         GdkColormap *colormap = gdk_screen_get_rgba_colormap(screen);
210
211         if (colormap) {
212                 gtk_widget_set_colormap(w, colormap);
213                 return 1;
214         }
215 #endif
216         return 0;
217 }
218
219 void ui_window_create(struct ui_psensor *ui)
220 {
221         GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
222         GdkScreen *screen;
223         GdkPixbuf *icon;
224         GtkIconTheme *icon_theme;
225         GtkWidget *menubar;
226         struct config *cfg;
227
228         cfg = ui->config;
229         if (cfg->window_restore_enabled)
230                 gtk_window_move(GTK_WINDOW(window),
231                                 cfg->window_x,
232                                 cfg->window_y);
233
234         gtk_window_set_default_size(GTK_WINDOW(window),
235                                     cfg->window_w,
236                                     cfg->window_h);
237
238
239         gtk_window_set_title(GTK_WINDOW(window),
240                              _("Psensor - Temperature Monitor"));
241         gtk_window_set_role(GTK_WINDOW(window), "psensor");
242
243         screen = gtk_widget_get_screen(window);
244
245         if (ui->config->alpha_channel_enabled
246             && gdk_screen_is_composited(screen)) {
247                 if (!enable_alpha_channel(window))
248                         ui->config->alpha_channel_enabled = 0;
249         } else {
250                 ui->config->alpha_channel_enabled = 0;
251         }
252
253         icon_theme = gtk_icon_theme_get_default();
254         icon = gtk_icon_theme_load_icon(icon_theme, "psensor", 48, 0, NULL);
255         if (icon)
256                 gtk_window_set_icon(GTK_WINDOW(window), icon);
257         else
258                 fprintf(stderr, _("ERROR: Failed to load psensor icon.\n"));
259
260         g_signal_connect(window,
261                          "delete_event", G_CALLBACK(on_delete_event_cb), ui);
262
263         gtk_window_set_decorated(GTK_WINDOW(window),
264                                  ui->config->window_decoration_enabled);
265
266         gtk_window_set_keep_below(GTK_WINDOW(window),
267                                   ui->config->window_keep_below_enabled);
268
269         /* main box */
270         menubar = get_menu(ui);
271
272         ui->main_box = gtk_vbox_new(FALSE, 1);
273
274         gtk_box_pack_start(GTK_BOX(ui->main_box), menubar,
275                            FALSE, TRUE, 0);
276
277         gtk_container_add(GTK_CONTAINER(window), ui->main_box);
278
279         ui->main_window = window;
280         ui->menu_bar = menubar;
281
282 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
283         if (ui->config->hide_on_startup)
284                 gtk_widget_show_all(ui->main_box);
285         else
286                 ui_window_show(ui);
287 #else
288         ui_window_show(ui);
289 #endif
290 }
291
292 static void menu_bar_show(unsigned int show, struct ui_psensor *ui)
293 {
294         if (show)
295                 gtk_widget_show(ui->menu_bar);
296         else
297                 gtk_widget_hide(ui->menu_bar);
298 }
299
300 void ui_window_update(struct ui_psensor *ui)
301 {
302         struct config *cfg;
303         int init = 1;
304
305         cfg = ui->config;
306
307         if (ui->sensor_box) {
308                 g_object_ref(GTK_WIDGET(ui->ui_sensorlist->widget));
309
310                 gtk_container_remove(GTK_CONTAINER(ui->sensor_box),
311                                      ui->ui_sensorlist->widget);
312
313                 gtk_container_remove(GTK_CONTAINER(ui->main_box),
314                                      ui->sensor_box);
315
316                 ui->w_graph = ui_graph_create(ui);
317
318                 init = 0;
319         }
320
321         if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
322             || cfg->sensorlist_position == SENSORLIST_POSITION_LEFT)
323                 ui->sensor_box = gtk_hpaned_new();
324         else
325                 ui->sensor_box = gtk_vpaned_new();
326
327         gtk_box_pack_end(GTK_BOX(ui->main_box), ui->sensor_box, TRUE, TRUE, 2);
328
329         if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
330             || cfg->sensorlist_position == SENSORLIST_POSITION_BOTTOM) {
331                 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
332                                 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
333                 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
334                                 ui->ui_sensorlist->widget, FALSE, TRUE);
335         } else {
336                 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
337                                 ui->ui_sensorlist->widget, FALSE, TRUE);
338                 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
339                                 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
340         }
341
342         if (cfg->window_restore_enabled)
343                 gtk_paned_set_position(GTK_PANED(ui->sensor_box),
344                                        ui->config->window_divider_pos);
345
346
347         if (!init)
348                 g_object_unref(GTK_WIDGET(ui->ui_sensorlist->widget));
349
350         gtk_widget_show_all(ui->sensor_box);
351
352         if (cfg->menu_bar_disabled)
353                 menu_bar_show(0, ui);
354         else
355                 menu_bar_show(1, ui);
356 }
357
358 void ui_window_show(struct ui_psensor *ui)
359 {
360         gtk_widget_show_all(ui->main_window);
361
362 }