Update memory sizes to 64 bit and fix compilation warnings.
[lxde/lxtask.git] / src / functions.c
1 /* $Id: functions.c 3940 2008-02-10 22:48:45Z nebulon $
2 *
3 * Copyright (c) 2006 Johannes Zellner, <webmaster@nebulon.de>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 /* modified by Hong Jen Yee <pcman.tw@gmail.com> and the LXDE team
21 * on 2008-04-26 to be used in LXDE.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <glib.h>
29 #include <glib/gi18n.h>
30 #include <stdio.h>
31
32 #include "functions.h"
33
34 extern gint refresh_interval;
35
36 static system_status *sys_stat =NULL;
37
38 gboolean refresh_task_list(void)
39 {
40 guint i, j;
41 GArray *new_task_list;
42 gdouble cpu_usage;
43 guint num_cpus;
44 guint memory_used;
45 char tooltip[256];
46
47 if (sys_stat!=NULL)
48 num_cpus = sys_stat->cpu_count;
49 else
50 num_cpus = 1;
51
52 /* gets the new task list */
53 new_task_list = (GArray*) get_task_list();
54
55 /* check if task is new and marks the task that its checked*/
56 for(i = 0; i < task_array->len; i++)
57 {
58 struct task *tmp = &g_array_index(task_array, struct task, i);
59 tmp->checked = FALSE;
60
61 for(j = 0; j < new_task_list->len; j++)
62 {
63 struct task *new_tmp = &g_array_index(new_task_list, struct task, j);
64
65 if(new_tmp->pid == tmp->pid)
66 {
67 tmp->old_time = tmp->time;
68
69 tmp->time = new_tmp->time;
70
71 new_tmp->time_percentage = (gfloat)(tmp->time - tmp->old_time) * (gfloat)(1000.0f / (refresh_interval * 1000 * num_cpus));
72 if(
73 tmp->ppid != new_tmp->ppid ||
74 tmp->state[0]!=new_tmp->state[0] ||
75 tmp->size != new_tmp->size ||
76 tmp->rss != new_tmp->rss ||
77 tmp->time_percentage != new_tmp->time_percentage ||
78 tmp->prio != new_tmp->prio
79 )
80 {
81 tmp->ppid = new_tmp->ppid;
82 tmp->state[0]=new_tmp->state[0];
83 tmp->size = new_tmp->size;
84 tmp->rss = new_tmp->rss;
85 tmp->prio = new_tmp->prio;
86 tmp->time_percentage=new_tmp->time_percentage;
87
88 refresh_list_item(i);
89 }
90 else
91 {
92 tmp->time_percentage=new_tmp->time_percentage;
93 }
94 tmp->checked = TRUE;
95 new_tmp->checked = TRUE;
96 break;
97 }
98 else
99 tmp->checked = FALSE;
100 }
101 }
102
103 /* check for unchecked old-tasks for deleting */
104 i = 0;
105 while( i < task_array->len)
106 {
107
108 struct task *tmp = &g_array_index(task_array, struct task, i);
109
110 if(!tmp->checked)
111 {
112 remove_list_item(tmp->pid);
113 g_array_remove_index(task_array, i);
114 tasks--;
115 }
116 else
117 i++;
118
119 }
120
121 /* check for unchecked new tasks for inserting */
122 for(i = 0; i < new_task_list->len; i++)
123 {
124 struct task *new_tmp = &g_array_index(new_task_list, struct task, i);
125
126 if(!new_tmp->checked)
127 {
128 struct task *new_task = new_tmp;
129 g_array_append_val(task_array, *new_task);
130 if((show_user_tasks && new_task->uid == own_uid) || (show_root_tasks && new_task->uid == 0) || (show_other_tasks && new_task->uid != own_uid && new_task->uid != 0))
131 add_new_list_item(tasks);
132 tasks++;
133 }
134 }
135
136 g_array_free(new_task_list, TRUE);
137
138 /* update the CPU and memory progress bars */
139 if (sys_stat == NULL)
140 sys_stat = g_new0 (system_status, 1);
141 get_system_status (sys_stat);
142
143 memory_used = sys_stat->mem_total - sys_stat->mem_free;
144 if ( show_cached_as_free )
145 {
146 memory_used-=sys_stat->mem_cached+sys_stat->mem_buffered;
147 }
148 sprintf (tooltip, _("Memory: %d MB of %d MB used"), memory_used / 1024, sys_stat->mem_total / 1024);
149 if(strcmp(tooltip,gtk_progress_bar_get_text(GTK_PROGRESS_BAR(mem_usage_progress_bar))))
150 {
151 gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (mem_usage_progress_bar), (gdouble)memory_used / sys_stat->mem_total);
152 gtk_progress_bar_set_text (GTK_PROGRESS_BAR (mem_usage_progress_bar), tooltip);
153 }
154
155 cpu_usage = get_cpu_usage (sys_stat);
156 sprintf (tooltip,_("CPU usage: %0.0f %%"), cpu_usage * 100.0);
157 if(strcmp(tooltip,gtk_progress_bar_get_text(GTK_PROGRESS_BAR(cpu_usage_progress_bar))))
158 {
159 gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (cpu_usage_progress_bar), cpu_usage);
160 gtk_progress_bar_set_text (GTK_PROGRESS_BAR (cpu_usage_progress_bar), tooltip);
161 }
162 return TRUE;
163 }
164
165 gdouble get_cpu_usage(system_status *sys_stat)
166 {
167 gdouble cpu_usage = 0.0;
168 guint current_jiffies;
169 guint current_used;
170 guint delta_jiffies;
171
172 if ( get_cpu_usage_from_proc( sys_stat ) == FALSE )
173 {
174 guint i = 0;
175
176 for(i = 0; i < task_array->len; i++)
177 {
178 struct task *tmp = &g_array_index(task_array, struct task, i);
179 cpu_usage += tmp->time_percentage;
180 }
181
182 cpu_usage = cpu_usage / (sys_stat->cpu_count * 100.0);
183 } else {
184
185 if ( sys_stat->cpu_old_jiffies > 0 ) {
186 current_used =
187 sys_stat->cpu_user +
188 sys_stat->cpu_nice +
189 sys_stat->cpu_system;
190 current_jiffies =
191 current_used +
192 sys_stat->cpu_idle;
193 delta_jiffies =
194 current_jiffies - (gdouble)sys_stat->cpu_old_jiffies;
195
196 cpu_usage = (gdouble)( current_used - sys_stat->cpu_old_used ) /
197 (gdouble)delta_jiffies ;
198 }
199 }
200 return cpu_usage;
201 }
202
203 static gboolean key_file_get_int( GKeyFile* kf, const char* group, const char* name, gboolean def )
204 {
205 int ret;
206 GError* err = NULL;
207 ret = g_key_file_get_integer( kf, group, name, &err );
208 if( err )
209 {
210 ret = def;
211 g_error_free( err );
212 }
213 return ret;
214 }
215
216 static gboolean key_file_get_bool( GKeyFile* kf, const char* group, const char* name, gboolean def )
217 {
218 return !!key_file_get_int(kf, group, name, def);
219 }
220
221 /*
222 * configurationfile support
223 */
224 void load_config(void)
225 {
226 static const char group[]="General";
227 GKeyFile *rc_file = g_key_file_new();
228 g_key_file_load_from_file(rc_file, config_file, 0, NULL);
229
230 show_user_tasks = key_file_get_bool(rc_file, group, "show_user_tasks", TRUE);
231 show_root_tasks = key_file_get_bool(rc_file, group, "show_root_tasks", FALSE);
232 show_other_tasks = key_file_get_bool(rc_file, group, "show_other_tasks", FALSE);
233 show_full_path = key_file_get_bool(rc_file, group, "show_full_path", FALSE);
234 show_cached_as_free = key_file_get_bool(rc_file, group, "show_cached_as_free", TRUE);
235
236 full_view = key_file_get_bool(rc_file, group, "full_view", TRUE);
237
238 win_width = key_file_get_int(rc_file, group, "win_width", 500 );
239 win_height = key_file_get_int(rc_file, group, "win_height", 400 );
240 refresh_interval = key_file_get_int(rc_file, group, "refresh_interval", 2 );
241
242 g_key_file_free(rc_file);
243 }
244
245 static int check_config(void)
246 {
247 static const char group[]="General";
248 int res=0;
249 GKeyFile *rc_file = g_key_file_new();
250 g_key_file_load_from_file(rc_file, config_file, 0, NULL);
251
252 if(show_user_tasks!=key_file_get_bool(rc_file, group, "show_user_tasks", TRUE))
253 {
254 res=1;
255 goto out;
256 }
257 if(show_root_tasks!=key_file_get_bool(rc_file, group, "show_root_tasks", FALSE))
258 {
259 res=1;
260 goto out;
261 }
262 if(show_other_tasks!=key_file_get_bool(rc_file, group, "show_other_tasks", FALSE))
263 {
264 res=1;
265 goto out;
266 }
267 if(show_full_path!=key_file_get_bool(rc_file, group, "show_full_path", FALSE))
268 {
269 res=1;
270 goto out;
271 }
272 if(show_cached_as_free!=key_file_get_bool(rc_file, group, "show_cached_as_free", TRUE))
273 {
274 res=1;
275 goto out;
276 }
277 if(full_view!=key_file_get_bool(rc_file, group, "full_view", TRUE))
278 {
279 res=1;
280 goto out;
281 }
282 gtk_window_get_size(GTK_WINDOW (main_window), &win_width, &win_height);
283 if(win_width != key_file_get_int(rc_file, group, "win_width", 500 ))
284 {
285 res=1;
286 goto out;
287 }
288 if(win_height != key_file_get_int(rc_file, group, "win_height", 400 ))
289 {
290 res=1;
291 goto out;
292 }
293 if(refresh_interval != key_file_get_int(rc_file, group, "refresh_interval", 2 ))
294 {
295 res=1;
296 goto out;
297 }
298 out:
299 g_key_file_free(rc_file);
300 return res;
301 }
302
303 void save_config(void)
304 {
305 FILE* rc_file ;
306
307 if(!check_config())
308 {
309 return;
310 }
311
312 rc_file = fopen( config_file, "w" );
313 if(!rc_file) return;
314
315 fputs( "[General]\n", rc_file );
316 fprintf( rc_file, "show_user_tasks=%d\n", show_user_tasks);
317 fprintf( rc_file, "show_root_tasks=%d\n", show_root_tasks);
318 fprintf( rc_file, "show_other_tasks=%d\n", show_other_tasks);
319 fprintf( rc_file, "show_full_path=%d\n", show_full_path);
320
321 fprintf( rc_file, "show_cached_as_free=%d\n", show_cached_as_free);
322
323 fprintf( rc_file, "full_view=%d\n", full_view);
324
325 gtk_window_get_size(GTK_WINDOW (main_window), &win_width, &win_height);
326
327 fprintf( rc_file, "win_width=%d\n", win_width);
328 fprintf( rc_file, "win_height=%d\n", win_height);
329 fprintf( rc_file, "refresh_interval=%d\n", refresh_interval);
330
331 fclose(rc_file);
332 }