4 #include "Process_List.h"
7 /*****************************************************************************
8 * Methods to synchronize process list *
9 *****************************************************************************/
11 typedef struct _ProcessInfo
{
21 GtkWidget
*Process_List_VC
;
22 GtkListStore
*Store_M
;
24 /* A hash table by PID to speed up process position find in the list */
25 GHashTable
*Process_Hash
;
27 guint Number_Of_Process
;
32 /* Enumeration of the columns */
43 gint
process_sort_func ( GtkTreeModel
*model
,
50 memset(&a
, 0, sizeof(GValue
));
51 memset(&b
, 0, sizeof(GValue
));
54 gtk_tree_model_get_value( model
,
59 gtk_tree_model_get_value( model
,
64 if(G_VALUE_TYPE(&a
) == G_TYPE_UINT
65 && G_VALUE_TYPE(&b
) == G_TYPE_UINT
)
67 if(g_value_get_uint(&a
) > g_value_get_uint(&b
))
73 if(g_value_get_uint(&a
) < g_value_get_uint(&b
))
85 /* Order by birth second */
86 gtk_tree_model_get_value( model
,
91 gtk_tree_model_get_value( model
,
97 if(G_VALUE_TYPE(&a
) == G_TYPE_ULONG
98 && G_VALUE_TYPE(&b
) == G_TYPE_ULONG
)
100 if(g_value_get_ulong(&a
) > g_value_get_ulong(&b
))
106 if(g_value_get_ulong(&a
) < g_value_get_ulong(&b
))
118 /* Order by birth nanosecond */
119 gtk_tree_model_get_value( model
,
124 gtk_tree_model_get_value( model
,
130 if(G_VALUE_TYPE(&a
) == G_TYPE_ULONG
131 && G_VALUE_TYPE(&b
) == G_TYPE_ULONG
)
133 if(g_value_get_ulong(&a
) > g_value_get_ulong(&b
))
140 //if(g_value_get_ulong(&a) < g_value_get_ulong(&b))
142 // g_value_unset(&a);
143 // g_value_unset(&b);
156 void send_test_data(ProcessList
*Process_List
)
160 ProcessInfo Process_Info
= {10000, 12000, 55600};
161 //ProcessInfo Process_Info = {156, 14000, 55500};
162 GtkTreeRowReference
*got_RowRef
;
165 birth
.tv_sec
= 12000;
166 birth
.tv_nsec
= 55500;
168 ProcessList_add(Process_List
,
173 g_critical("height : %u", height
);
175 birth
.tv_sec
= 14000;
176 birth
.tv_nsec
= 55500;
178 ProcessList_add(Process_List
,
183 g_critical("height : %u", height
);
185 birth
.tv_sec
= 12000;
186 birth
.tv_nsec
= 55700;
188 ProcessList_add(Process_List
,
195 birth
.tv_sec
= i
*12000;
196 birth
.tv_nsec
= i
*55700;
198 ProcessList_add(Process_List
,
204 g_critical("height : %u", height
);
206 birth
.tv_sec
= 12000;
207 birth
.tv_nsec
= 55600;
209 ProcessList_add(Process_List
,
213 g_critical("height : %u", height
);
215 ProcessList_add(Process_List
,
219 g_critical("height : %u", height
);
222 ProcessList_remove( Process_List
,
227 (GtkTreeRowReference
*)g_hash_table_lookup(
228 Process_List
->Process_Hash
,
231 g_critical("key found");
232 g_critical("position in the list : %s",
233 gtk_tree_path_to_string (
234 gtk_tree_row_reference_get_path(
235 (GtkTreeRowReference
*)got_RowRef
)
243 guint
hash_fct(gconstpointer key
)
245 return ((ProcessInfo
*)key
)->pid
;
248 gboolean
equ_fct(gconstpointer a
, gconstpointer b
)
250 if(((ProcessInfo
*)a
)->pid
!= ((ProcessInfo
*)b
)->pid
)
252 g_critical("compare %u and %u",((ProcessInfo
*)a
)->pid
,((ProcessInfo
*)b
)->pid
);
253 if(((ProcessInfo
*)a
)->birth
.tv_sec
!= ((ProcessInfo
*)b
)->birth
.tv_sec
)
255 g_critical("compare %u and %u",((ProcessInfo
*)a
)->birth
.tv_sec
,((ProcessInfo
*)b
)->birth
.tv_sec
);
257 if(((ProcessInfo
*)a
)->birth
.tv_nsec
!= ((ProcessInfo
*)b
)->birth
.tv_nsec
)
259 g_critical("compare %u and %u",((ProcessInfo
*)a
)->birth
.tv_nsec
,((ProcessInfo
*)b
)->birth
.tv_nsec
);
264 void Destroy_hash_key(gpointer key
);
266 void Destroy_hash_data(gpointer data
);
271 ProcessList
*ProcessList_construct(void)
273 GtkTreeViewColumn
*column
;
274 GtkCellRenderer
*renderer
;
276 ProcessList
* Process_List
= g_new(ProcessList
,1);
278 Process_List
->Number_Of_Process
= 0;
280 /* Create the Process list */
281 Process_List
->Store_M
= gtk_list_store_new ( N_COLUMNS
,
288 Process_List
->Process_List_VC
=
289 gtk_tree_view_new_with_model
290 (GTK_TREE_MODEL (Process_List
->Store_M
));
292 g_object_unref (G_OBJECT (Process_List
->Store_M
));
294 gtk_tree_sortable_set_sort_func(
295 GTK_TREE_SORTABLE(Process_List
->Store_M
),
301 gtk_tree_sortable_set_sort_column_id(
302 GTK_TREE_SORTABLE(Process_List
->Store_M
),
306 Process_List
->Process_Hash
= g_hash_table_new_full(
308 Destroy_hash_key
, Destroy_hash_data
312 gtk_tree_view_set_headers_visible(
313 GTK_TREE_VIEW(Process_List
->Process_List_VC
), FALSE
);
315 /* Create a column, associating the "text" attribute of the
316 * cell_renderer to the first column of the model */
317 /* Columns alignment : 0.0 : Left 0.5 : Center 1.0 : Right */
318 renderer
= gtk_cell_renderer_text_new ();
319 column
= gtk_tree_view_column_new_with_attributes ( "Process",
324 gtk_tree_view_column_set_alignment (column
, 0.0);
325 gtk_tree_view_column_set_fixed_width (column
, 45);
326 gtk_tree_view_append_column (
327 GTK_TREE_VIEW (Process_List
->Process_List_VC
), column
);
329 column
= gtk_tree_view_column_new_with_attributes ( "PID",
334 gtk_tree_view_append_column (
335 GTK_TREE_VIEW (Process_List
->Process_List_VC
), column
);
338 column
= gtk_tree_view_column_new_with_attributes ( "Birth sec",
343 gtk_tree_view_append_column (
344 GTK_TREE_VIEW (Process_List
->Process_List_VC
), column
);
346 //gtk_tree_view_column_set_visible(column, 0);
348 column
= gtk_tree_view_column_new_with_attributes ( "Birth nsec",
353 gtk_tree_view_append_column (
354 GTK_TREE_VIEW (Process_List
->Process_List_VC
), column
);
356 //gtk_tree_view_column_set_visible(column, 0);
358 g_object_set_data_full(
359 G_OBJECT(Process_List
->Process_List_VC
),
362 (GDestroyNotify
)ProcessList_destroy
);
364 send_test_data(Process_List
);
368 void ProcessList_destroy(ProcessList
*Process_List
)
370 g_hash_table_destroy(Process_List
->Process_Hash
);
371 Process_List
->Process_Hash
= NULL
;
373 g_free(Process_List
);
376 GtkWidget
*ProcessList_getWidget(ProcessList
*Process_List
)
378 return Process_List
->Process_List_VC
;
383 gint
get_cell_height(GtkTreeView
*TreeView
)
386 GtkTreeViewColumn
*Column
= gtk_tree_view_get_column(TreeView
, 0);
387 GList
*Render_List
= gtk_tree_view_column_get_cell_renderers(Column
);
388 GtkCellRenderer
*Renderer
= g_list_first(Render_List
)->data
;
390 gtk_tree_view_column_cell_get_size(Column
, NULL
, NULL
, NULL
, NULL
, &height
);
391 g_critical("cell 0 height : %u",height
);
396 void Destroy_hash_key(gpointer key
)
401 void Destroy_hash_data(gpointer data
)
406 int ProcessList_add( ProcessList
*Process_List
,
412 ProcessInfo
*Process_Info
= g_new(ProcessInfo
, 1);
413 GtkTreeRowReference
*RowRef
;
415 Process_Info
->pid
= pid
;
416 Process_Info
->birth
= *birth
;
418 /* Add a new row to the model */
419 gtk_list_store_append ( Process_List
->Store_M
, &iter
);
420 g_critical ( "iter before : %s", gtk_tree_path_to_string (
421 gtk_tree_model_get_path (
422 GTK_TREE_MODEL(Process_List
->Store_M
),
424 gtk_list_store_set ( Process_List
->Store_M
, &iter
,
425 PROCESS_COLUMN
, "name",
427 BIRTH_S_COLUMN
, birth
->tv_sec
,
428 BIRTH_NS_COLUMN
, birth
->tv_nsec
,
430 RowRef
= gtk_tree_row_reference_new (
431 GTK_TREE_MODEL(Process_List
->Store_M
),
432 gtk_tree_model_get_path(
433 GTK_TREE_MODEL(Process_List
->Store_M
),
436 g_hash_table_insert( Process_List
->Process_Hash
,
437 (gpointer
)Process_Info
,
440 g_critical ( "iter after : %s", gtk_tree_path_to_string (
441 gtk_tree_model_get_path (
442 GTK_TREE_MODEL(Process_List
->Store_M
),
444 Process_List
->Number_Of_Process
++;
446 *height
= get_cell_height(GTK_TREE_VIEW(Process_List
->Process_List_VC
))
447 * Process_List
->Number_Of_Process
;
454 int ProcessList_remove( ProcessList
*Process_List
,
458 ProcessInfo Process_Info
;
460 GtkTreeRowReference
*got_RowRef
;
463 Process_Info
.pid
= pid
;
464 Process_Info
.birth
= *birth
;
467 (GtkTreeRowReference
*)g_hash_table_lookup(
468 Process_List
->Process_Hash
,
471 gtk_tree_model_get_iter (
472 GTK_TREE_MODEL(Process_List
->Store_M
),
474 gtk_tree_row_reference_get_path(
475 (GtkTreeRowReference
*)got_RowRef
)
478 gtk_list_store_remove (Process_List
->Store_M
, &iter
);
480 g_hash_table_remove(Process_List
->Process_Hash
,
483 Process_List
->Number_Of_Process
--;
492 guint
ProcessList_get_height(ProcessList
*Process_List
)
494 return get_cell_height(GTK_TREE_VIEW(Process_List
->Process_List_VC
))
495 * Process_List
->Number_Of_Process
;
499 gint
ProcessList_get_process_pixels( ProcessList
*Process_List
,
500 guint pid
, LttTime
*birth
,
504 ProcessInfo Process_Info
;
506 GtkTreeRowReference
*got_RowRef
;
508 Process_Info
.pid
= pid
;
509 Process_Info
.birth
= *birth
;
512 (GtkTreeRowReference
*)g_hash_table_lookup(
513 Process_List
->Process_Hash
,
516 path_indices
= gtk_tree_path_get_indices (
517 gtk_tree_row_reference_get_path(
518 (GtkTreeRowReference
*)got_RowRef
)
521 *height
= get_cell_height(
522 GTK_TREE_VIEW(Process_List
->Process_List_VC
));
523 *x
= *height
* path_indices
[0];