added support of cpu usage monitoring
[psensor.git] / src / ui_sensorlist.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
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "ui.h"
25 #include "ui_pref.h"
26 #include "ui_sensorlist.h"
27 #include "ui_sensorpref.h"
28 #include "cfg.h"
29 #include "ui_color.h"
30 #include "compat.h"
31
32 enum {
33         COL_NAME = 0,
34         COL_TEMP,
35         COL_TEMP_MIN,
36         COL_TEMP_MAX,
37         COL_COLOR,
38         COL_COLOR_STR,
39         COL_ENABLED,
40         COL_EMPTY,
41         COLS_COUNT
42 };
43
44 struct cb_data {
45         struct ui_psensor *ui;
46         struct psensor *sensor;
47 };
48
49 static int col_index_to_col(int idx)
50 {
51         if (idx == 5)
52                 return COL_ENABLED;
53         else if (idx > 5)
54                 return -1;
55
56         return idx;
57 }
58
59 void ui_sensorlist_update(struct ui_psensor *ui)
60 {
61         GtkTreeIter iter;
62         struct ui_sensorlist *ui_sl = ui->ui_sensorlist;
63         GtkTreeModel *model
64             = gtk_tree_view_get_model(ui_sl->treeview);
65         gboolean valid = gtk_tree_model_get_iter_first(model, &iter);
66         struct psensor **sensor = ui->sensors;
67
68         while (valid && *sensor) {
69                 struct psensor *s = *sensor;
70
71                 char *str;
72
73                 str = psensor_value_to_string(s->type,
74                                               s->measures[s->values_max_length -
75                                                           1].value.d_num);
76
77                 gtk_list_store_set(GTK_LIST_STORE(model), &iter, COL_TEMP, str,
78                                    -1);
79                 free(str);
80
81                 str = psensor_value_to_string(s->type, s->min);
82                 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
83                                    COL_TEMP_MIN, str, -1);
84                 free(str);
85
86                 str = psensor_value_to_string(s->type, s->max);
87                 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
88                                    COL_TEMP_MAX, str, -1);
89                 free(str);
90
91                 valid = gtk_tree_model_iter_next(model, &iter);
92                 sensor++;
93         }
94 }
95
96 /*
97  * Returns the sensor corresponding to the x/y position
98  * in the table.
99  *
100  * <null> if none.
101  */
102 static struct psensor *
103 get_sensor_at_pos(GtkTreeView *view, int x, int y, struct psensor **sensors)
104 {
105         GtkTreePath *path;
106
107         gtk_tree_view_get_path_at_pos(view, x, y, &path, NULL, NULL, NULL);
108
109         if (path) {
110                 gint *i = gtk_tree_path_get_indices(path);
111                 if (i)
112                         return *(sensors + *i);
113         }
114         return NULL;
115 }
116
117 /*
118  * Returns the index of the column corresponding
119  * to the x position in the table.
120  *
121  * -1 if none
122  */
123 static int get_col_index_at_pos(GtkTreeView *view, int x)
124 {
125         GList *columns = gtk_tree_view_get_columns(view);
126         GList *node;
127         int colx = 0;
128         int coli = 0;
129
130         for (node = columns; node; node = node->next) {
131                 GtkTreeViewColumn *checkcol = (GtkTreeViewColumn *) node->data;
132
133                 if (x >= colx &&
134                     x < (colx + gtk_tree_view_column_get_width(checkcol)))
135                         return coli;
136                 else
137                         colx += gtk_tree_view_column_get_width(checkcol);
138
139                 coli++;
140         }
141
142         return -1;
143 }
144
145 void ui_sensorlist_update_sensors_preferences(struct ui_psensor *ui)
146 {
147         GtkTreeIter iter;
148         GtkTreeModel *model
149             = gtk_tree_view_get_model(ui->ui_sensorlist->treeview);
150         gboolean valid = gtk_tree_model_get_iter_first(model, &iter);
151         struct psensor **sensor = ui->ui_sensorlist->sensors;
152
153         while (valid && *sensor) {
154                 GdkColor color;
155                 gchar *scolor;
156
157                 color.red = (*sensor)->color->red;
158                 color.green = (*sensor)->color->green;
159                 color.blue = (*sensor)->color->blue;
160
161                 scolor = gdk_color_to_string(&color);
162
163                 gtk_list_store_set(GTK_LIST_STORE(model),
164                                    &iter, COL_NAME, (*sensor)->name, -1);
165
166                 gtk_list_store_set(GTK_LIST_STORE(model),
167                                    &iter, COL_COLOR_STR, scolor, -1);
168
169                 gtk_list_store_set(GTK_LIST_STORE(model),
170                                    &iter, COL_ENABLED, (*sensor)->enabled, -1);
171
172                 free(scolor);
173
174                 valid = gtk_tree_model_iter_next(model, &iter);
175                 sensor++;
176         }
177 }
178
179 static void on_preferences_activated(GtkWidget *menu_item, gpointer data)
180 {
181         struct cb_data *cb_data = data;
182
183         ui_sensorpref_dialog_run(cb_data->sensor, cb_data->ui);
184 }
185
186 static GtkWidget *create_sensor_popup(struct ui_psensor *ui,
187                                       struct psensor *sensor)
188 {
189         GtkWidget *menu;
190         GtkWidget *item;
191         GtkWidget *separator;
192         struct cb_data *data;
193
194         menu = gtk_menu_new();
195
196         item = gtk_menu_item_new_with_label(sensor->name);
197         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
198
199         separator = gtk_separator_menu_item_new();
200         gtk_menu_shell_append(GTK_MENU_SHELL(menu), separator);
201
202         item = gtk_menu_item_new_with_label(_("Preferences"));
203         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
204
205         data = malloc(sizeof(struct cb_data));
206         data->ui = ui;
207         data->sensor = sensor;
208
209         g_signal_connect(item,
210                          "activate",
211                          G_CALLBACK(on_preferences_activated), data);
212
213         gtk_widget_show_all(menu);
214
215         return menu;
216 }
217
218 static int on_clicked(GtkWidget *widget, GdkEventButton *event, gpointer data)
219 {
220         struct ui_psensor *ui = (struct ui_psensor *)data;
221         GtkTreeView *view = ui->ui_sensorlist->treeview;
222
223         struct psensor *sensor = get_sensor_at_pos(view,
224                                                    event->x,
225                                                    event->y,
226                                                    ui->sensors);
227
228         if (sensor) {
229                 int coli = col_index_to_col(get_col_index_at_pos(view,
230                                                                  event->x));
231
232                 if (coli == COL_COLOR) {
233                         if (ui_change_color(_("Select foreground color"),
234                                             sensor->color)) {
235                                 ui_sensorlist_update_sensors_preferences(ui);
236                                 config_set_sensor_color(sensor->id,
237                                                         sensor->color);
238                         }
239                 } else if (coli >= 0 && coli != COL_ENABLED) {
240                         GtkWidget *menu = create_sensor_popup(ui,
241                                                               sensor);
242
243                         gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
244                                        event->button, event->time);
245
246                 }
247
248         }
249         return FALSE;
250 }
251
252 static void
253 on_toggled(GtkCellRendererToggle *cell, gchar *path_str, gpointer data)
254 {
255         struct ui_sensorlist *list = (struct ui_sensorlist *)data;
256         GtkTreeModel *model
257             = gtk_tree_view_get_model(list->treeview);
258         GtkTreeIter iter;
259         GtkTreePath *path = gtk_tree_path_new_from_string(path_str);
260         gboolean fixed;
261         gint *i;
262
263         gtk_tree_model_get_iter(model, &iter, path);
264         gtk_tree_model_get(model, &iter, COL_ENABLED, &fixed, -1);
265
266         fixed ^= 1;
267
268         i = gtk_tree_path_get_indices(path);
269         if (i) {
270                 int n = *i;
271                 struct psensor **sensor = list->sensors;
272                 while (n--)
273                         sensor++;
274                 (*sensor)->enabled = fixed;
275                 config_set_sensor_enabled((*sensor)->id, (*sensor)->enabled);
276         }
277
278         gtk_list_store_set(GTK_LIST_STORE(model),
279                            &iter, COL_ENABLED, fixed, -1);
280
281         gtk_tree_path_free(path);
282 }
283
284 static void create_widget(struct ui_psensor *ui)
285 {
286         GtkListStore *store;
287         GtkCellRenderer *renderer;
288         struct psensor **s_cur = ui->sensors;
289         struct ui_sensorlist *ui_sl = ui->ui_sensorlist;
290
291         store = gtk_list_store_new(COLS_COUNT,
292                                    G_TYPE_STRING,
293                                    G_TYPE_STRING,
294                                    G_TYPE_STRING,
295                                    G_TYPE_STRING,
296                                    G_TYPE_STRING,
297                                    G_TYPE_STRING,
298                                    G_TYPE_BOOLEAN, G_TYPE_STRING);
299
300         ui_sl->treeview = GTK_TREE_VIEW
301                 (gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)));
302
303         gtk_tree_selection_set_mode
304                 (gtk_tree_view_get_selection(ui_sl->treeview),
305                  GTK_SELECTION_NONE);
306
307         renderer = gtk_cell_renderer_text_new();
308         gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
309                                                     -1,
310                                                     _("Sensor"),
311                                                     renderer,
312                                                     "text", COL_NAME, NULL);
313
314         gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
315                                                     -1,
316                                                     _("Current"),
317                                                     renderer,
318                                                     "text", COL_TEMP, NULL);
319
320         gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
321                                                     -1,
322                                                     _("Min"),
323                                                     renderer,
324                                                     "text", COL_TEMP_MIN, NULL);
325
326         gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
327                                                     -1,
328                                                     _("Max"),
329                                                     renderer,
330                                                     "text", COL_TEMP_MAX, NULL);
331
332         renderer = gtk_cell_renderer_text_new();
333         gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
334                                                     -1,
335                                                     _("Color"),
336                                                     renderer,
337                                                     "text", COL_COLOR,
338                                                     "background", COL_COLOR_STR,
339                                                     NULL);
340
341         g_signal_connect(ui_sl->treeview,
342                          "button-press-event", (GCallback) on_clicked, ui);
343
344         renderer = gtk_cell_renderer_toggle_new();
345         gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
346                                                     -1,
347                                                     _("Enabled"),
348                                                     renderer,
349                                                     "active", COL_ENABLED,
350                                                     NULL);
351         g_signal_connect(G_OBJECT(renderer),
352                          "toggled", (GCallback) on_toggled, ui_sl);
353
354         renderer = gtk_cell_renderer_text_new();
355         gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
356                                                     -1,
357                                                     "",
358                                                     renderer,
359                                                     "text", COL_EMPTY, NULL);
360
361         while (*s_cur) {
362                 GtkTreeIter iter;
363                 GdkColor color;
364                 gchar *scolor;
365                 struct psensor *s = *s_cur;
366
367                 color.red = s->color->red;
368                 color.green = s->color->green;
369                 color.blue = s->color->blue;
370
371                 scolor = gdk_color_to_string(&color);
372
373                 gtk_list_store_append(store, &iter);
374                 gtk_list_store_set(store, &iter,
375                                    COL_NAME, s->name,
376                                    COL_TEMP, _("N/A"),
377                                    COL_TEMP_MIN, _("N/A"),
378                                    COL_TEMP_MAX, _("N/A"),
379                                    COL_COLOR_STR, scolor,
380                                    COL_ENABLED, s->enabled, -1);
381
382                 free(scolor);
383
384                 s_cur++;
385         }
386
387         ui_sl->widget = gtk_scrolled_window_new(NULL, NULL);
388         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ui_sl->widget),
389                                        GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
390         gtk_container_add(GTK_CONTAINER(ui_sl->widget),
391                           GTK_WIDGET(ui_sl->treeview));
392 }
393
394 void ui_sensorlist_create(struct ui_psensor *ui)
395 {
396         ui->ui_sensorlist = malloc(sizeof(struct ui_sensorlist));
397         ui->ui_sensorlist->sensors = ui->sensors;
398
399         create_widget(ui);
400 }