cf6cb7e0 |
1 | /****************************************************************************** |
2 | * Draw_Item.c |
3 | * |
4 | * This file contains methods responsible for drawing a generic type of data |
5 | * in a drawable. Doing this generically will permit user defined drawing |
6 | * behavior in a later time. |
7 | * |
b782dd11 |
8 | * This file provides an API which is meant to be reusable for all viewers that |
9 | * need to show information in line, icon, text, background or point form in |
10 | * a drawable area having time for x axis. The y axis, in the control flow |
11 | * viewer case, is corresponding to the different processes, but it can be |
12 | * reused integrally for cpu, and eventually locks, buffers, network |
13 | * interfaces... What will differ between the viewers is the precise |
14 | * information which interests us. We may think that the most useful |
15 | * information for control flow are some specific events, like schedule |
16 | * change, and processes'states. It may differ for a cpu viewer : the |
17 | * interesting information could be more the execution mode of each cpu. |
18 | * This API in meant to make viewer's writers life easier : it will become |
19 | * a simple choice of icons and line types for the precise information |
20 | * the viewer has to provide (agremented with keeping supplementary records |
21 | * and modifying slightly the DrawContext to suit the needs.) |
22 | * |
f0728492 |
23 | * We keep each data type in attributes, keys to specific information |
24 | * being formed from the GQuark corresponding to the information received. |
25 | * (facilities / facility_name / events / eventname.) |
26 | * (cpus/cpu_name, process_states/ps_name, |
27 | * execution_modes/em_name, execution_submodes/es_name). |
cf6cb7e0 |
28 | * The goal is then to provide a generic way to print information on the |
29 | * screen for all this different information. |
30 | * |
31 | * Information can be printed as |
32 | * |
33 | * - text (text + color + size + position (over or under line) |
34 | * - icon (icon filename, corresponding to a loaded icon, accessible through |
35 | * a GQuark. Icons are loaded statically at the guiControlFlow level during |
36 | * module initialization and can be added on the fly if not present in the |
37 | * GQuark.) The habitual place for xpm icons is in |
38 | * ${prefix}/share/LinuxTraceToolkit.) + position (over or under line) |
39 | * - line (color, width, style) |
189a5d08 |
40 | * - Arc (big points) (color, size) |
cf6cb7e0 |
41 | * - background color (color) |
42 | * |
189a5d08 |
43 | * An item is a leaf of the attributes tree. It is, in that case, including |
44 | * all kind of events categories we can have. It then associates each category |
45 | * with one or more actions (drawing something) or nothing. |
46 | * |
7d5ffafa |
47 | * Each item has an array of hooks (hook list). Each hook represents an |
48 | * operation to perform. We seek the array each time we want to |
a2e850ff |
49 | * draw an item. We execute each operation in order. An operation type |
50 | * is associated with each hook to permit user listing and modification |
51 | * of these operations. The operation type is also used to find the |
52 | * corresponding priority for the sorting. Operation type and priorities |
53 | * are enum and a static int table. |
cf6cb7e0 |
54 | * |
55 | * The array has to be sorted by priority each time we add a task in it. |
a2e850ff |
56 | * A priority is associated with each operation type. It permits |
cf6cb7e0 |
57 | * to perform background color selection before line or text drawing. We also |
58 | * draw lines before text, so the text appears over the lines. |
59 | * |
60 | * Executing all the arrays of operations for a specific event (which |
61 | * implies information for state, event, cpu, execution mode and submode) |
62 | * has to be done in a same DrawContext. The goal there is to keep the offset |
63 | * of the text and icons over and under the middle line, so a specific |
64 | * event could be printed as ( R Si 0 for running, scheduled in, cpu 0 ), |
7d5ffafa |
65 | * text being easy to replace with icons. The DrawContext is passed as |
66 | * call_data for the operation hooks. |
cf6cb7e0 |
67 | * |
b782dd11 |
68 | * We use the lttv global attributes to keep track of the loaded icons. |
69 | * If we need an icon, we look for it in the icons / icon name pathname. |
70 | * If found, we use the pointer to it. If not, we load the pixmap in |
1a31868c |
71 | * memory and set the pointer to the GdkPixmap in the attributes. The |
72 | * structure pointed to contains the pixmap and the mask bitmap. |
b782dd11 |
73 | * |
cf6cb7e0 |
74 | * Author : Mathieu Desnoyers, October 2003 |
75 | */ |
7d5ffafa |
76 | |
77 | #include <glib.h> |
09e2606f |
78 | #include <gtk/gtk.h> |
79 | #include <gdk/gdk.h> |
7d5ffafa |
80 | #include <lttv/hook.h> |
f0728492 |
81 | #include <lttv/attribute.h> |
82 | #include <lttv/iattribute.h> |
1a31868c |
83 | #include <string.h> |
7d5ffafa |
84 | |
b782dd11 |
85 | #include <lttv/processTrace.h> |
86 | #include <lttv/state.h> |
87 | |
09e2606f |
88 | #include "Draw_Item.h" |
1a31868c |
89 | |
90 | |
91 | #define MAX_PATH_LEN 256 |
92 | |
b782dd11 |
93 | /* The DrawContext keeps information about the current drawing position and |
94 | * the previous one, so we can use both to draw lines. |
95 | * |
96 | * over : position for drawing over the middle line. |
97 | * middle : middle line position. |
98 | * under : position for drawing under the middle line. |
09e2606f |
99 | * |
100 | * the modify_* are used to take into account that we should go forward |
101 | * when we draw a text, an arc or an icon, while it's unneeded when we |
102 | * draw a line or background. |
1a31868c |
103 | * |
b782dd11 |
104 | */ |
105 | struct _DrawContext { |
106 | GdkDrawable *drawable; |
107 | GdkGC *gc; |
1a31868c |
108 | |
b782dd11 |
109 | |
110 | DrawInfo *Current; |
111 | DrawInfo *Previous; |
112 | }; |
113 | |
114 | struct _DrawInfo { |
115 | ItemInfo *over; |
116 | ItemInfo *middle; |
117 | ItemInfo *under; |
09e2606f |
118 | |
119 | ItemInfo *modify_over; |
120 | ItemInfo *modify_middle; |
121 | ItemInfo *modify_under; |
b782dd11 |
122 | }; |
123 | |
124 | /* LttvExecutionState is accessible through the LttvTracefileState. Is has |
125 | * a pointer to the LttvProcessState which points to the top of stack |
126 | * execution state : LttvExecutionState *state. |
127 | * |
128 | * LttvExecutionState contains (useful here): |
129 | * LttvExecutionMode t, |
130 | * LttvExecutionSubmode n, |
131 | * LttvProcessStatus s |
132 | * |
133 | * |
134 | * LttvTraceState will be used in the case we need the string of the |
135 | * different processes, eventtype_names, syscall_names, trap_names, irq_names. |
136 | * |
137 | * LttvTracefileState also gives the cpu_name and, as it herits from |
138 | * LttvTracefileContext, it gives the LttEvent structure, which is needed |
139 | * to get facility name and event name. |
140 | */ |
141 | struct _ItemInfo { |
142 | gint x, y; |
143 | LttvTraceState *ts; |
144 | LttvTracefileState *tfs; |
145 | }; |
146 | |
1a31868c |
147 | /* |
148 | * Structure used to keep information about icons. |
149 | */ |
150 | struct _IconStruct { |
151 | GdkPixmap *pixmap; |
152 | GdkBitmap *mask; |
153 | }; |
154 | |
b782dd11 |
155 | |
156 | /* |
157 | * The Item element is only used so the DrawOperation is modifiable by users. |
158 | * During drawing, only the Hook is needed. |
159 | */ |
160 | struct _DrawOperation { |
161 | DrawableItems Item; |
162 | LttvHooks *Hook; |
163 | }; |
164 | |
165 | /* |
166 | * We define here each items that can be drawn, together with their |
167 | * associated priority. Many item types can have the same priority, |
168 | * it's only used for quicksorting the operations when we add a new one |
169 | * to the array of operations to perform. Lower priorities are executed |
170 | * first. So, for example, we may want to give background color a value |
171 | * of 10 while a line would have 20, so the background color, which |
172 | * is in fact a rectangle, does not hide the line. |
173 | */ |
174 | |
09e2606f |
175 | static int Items_Priorities[] = { |
b782dd11 |
176 | 50, /* ITEM_TEXT */ |
177 | 40, /* ITEM_ICON */ |
178 | 20, /* ITEM_LINE */ |
179 | 30, /* ITEM_POINT */ |
180 | 10 /* ITEM_BACKGROUND */ |
181 | }; |
182 | |
b782dd11 |
183 | /* |
184 | * Here are the different structures describing each item type that can be |
185 | * drawn. They contain the information necessary to draw the item : not the |
186 | * position (this is provided by the DrawContext), but the text, icon name, |
187 | * line width, color; all the properties of the specific items. |
188 | */ |
189 | |
190 | struct _PropertiesText { |
191 | GdkColor *foreground; |
192 | GdkColor *background; |
193 | gint size; |
194 | gchar *Text; |
195 | RelPos position; |
196 | }; |
197 | |
198 | |
199 | struct _PropertiesIcon { |
200 | gchar *icon_name; |
201 | gint width; |
202 | gint height; |
203 | RelPos position; |
204 | }; |
205 | |
206 | struct _PropertiesLine { |
207 | GdkColor *color; |
208 | gint line_width; |
209 | GdkLineStyle style; |
210 | RelPos position; |
211 | }; |
212 | |
213 | struct _PropertiesArc { |
214 | GdkColor *color; |
215 | gint size; /* We force circle by width = height */ |
216 | gboolean filled; |
217 | RelPos position; |
218 | }; |
219 | |
220 | struct _PropertiesBG { |
221 | GdkColor *color; |
222 | }; |
223 | |
224 | |
b782dd11 |
225 | /* Drawing hook functions */ |
4c69e0cc |
226 | gboolean draw_text( void *hook_data, void *call_data) |
b782dd11 |
227 | { |
228 | PropertiesText *Properties = (PropertiesText*)hook_data; |
229 | DrawContext *Draw_Context = (DrawContext*)call_data; |
09e2606f |
230 | |
231 | PangoContext *context; |
232 | PangoLayout *layout; |
233 | PangoFontDescription *FontDesc;// = pango_font_description_new(); |
234 | gint Font_Size; |
235 | PangoRectangle ink_rect; |
236 | |
237 | gdk_gc_set_foreground(Draw_Context->gc, Properties->foreground); |
238 | gdk_gc_set_background(Draw_Context->gc, Properties->background); |
239 | |
240 | layout = gtk_widget_create_pango_layout(GTK_WIDGET(Draw_Context->drawable), NULL); |
241 | context = pango_layout_get_context(layout); |
242 | FontDesc = pango_context_get_font_description(context); |
243 | Font_Size = pango_font_description_get_size(FontDesc); |
244 | pango_font_description_set_size(FontDesc, Properties->size*PANGO_SCALE); |
245 | |
246 | |
247 | pango_layout_set_text(layout, Properties->Text, -1); |
248 | pango_layout_get_pixel_extents(layout, &ink_rect, NULL); |
249 | switch(Properties->position) { |
250 | case OVER: |
251 | gdk_draw_layout(Draw_Context->drawable, Draw_Context->gc, |
252 | Draw_Context->Current->modify_over->x, |
253 | Draw_Context->Current->modify_over->y, |
254 | layout); |
255 | Draw_Context->Current->modify_over->x += ink_rect.width; |
256 | |
257 | break; |
258 | case MIDDLE: |
259 | gdk_draw_layout(Draw_Context->drawable, Draw_Context->gc, |
260 | Draw_Context->Current->modify_middle->x, |
261 | Draw_Context->Current->modify_middle->y, |
262 | layout); |
263 | Draw_Context->Current->modify_middle->x += ink_rect.width; |
264 | break; |
265 | case UNDER: |
266 | gdk_draw_layout(Draw_Context->drawable, Draw_Context->gc, |
267 | Draw_Context->Current->modify_under->x, |
268 | Draw_Context->Current->modify_under->y, |
269 | layout); |
270 | Draw_Context->Current->modify_under->x += ink_rect.width; |
271 | break; |
272 | } |
273 | |
274 | |
275 | pango_font_description_set_size(FontDesc, Font_Size); |
276 | g_free(layout); |
277 | |
278 | return 0; |
b782dd11 |
279 | } |
280 | |
1a31868c |
281 | |
282 | /* To speed up the process, search in already loaded icons list first. Only |
283 | * load it if not present. |
284 | */ |
4c69e0cc |
285 | gboolean draw_icon( void *hook_data, void *call_data) |
b782dd11 |
286 | { |
287 | PropertiesIcon *Properties = (PropertiesIcon*)hook_data; |
288 | DrawContext *Draw_Context = (DrawContext*)call_data; |
289 | |
1a31868c |
290 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
291 | LttvAttributeValue value; |
292 | gchar icon_name[MAX_PATH_LEN] = "icons/"; |
293 | IconStruct *icon_info; |
294 | |
295 | strcat(icon_name, Properties->icon_name); |
296 | |
297 | g_assert(lttv_iattribute_find_by_path(attributes, icon_name, |
298 | LTTV_POINTER, &value)); |
299 | if(*(value.v_pointer) == NULL) |
300 | { |
301 | *(value.v_pointer) = icon_info = g_new(IconStruct,1); |
302 | |
303 | icon_info->pixmap = gdk_pixmap_create_from_xpm(Draw_Context->drawable, |
304 | &icon_info->mask, NULL, Properties->icon_name); |
305 | } |
306 | else |
307 | { |
308 | icon_info = *(value.v_pointer); |
309 | } |
310 | |
311 | gdk_gc_set_clip_mask(Draw_Context->gc, icon_info->mask); |
312 | |
09e2606f |
313 | switch(Properties->position) { |
314 | case OVER: |
1a31868c |
315 | gdk_gc_set_clip_origin( |
316 | Draw_Context->gc, |
317 | Draw_Context->Current->modify_over->x, |
318 | Draw_Context->Current->modify_over->y); |
09e2606f |
319 | gdk_draw_drawable(Draw_Context->drawable, |
1a31868c |
320 | Draw_Context->gc, |
321 | icon_info->pixmap, |
09e2606f |
322 | 0, 0, |
323 | Draw_Context->Current->modify_over->x, |
324 | Draw_Context->Current->modify_over->y, |
325 | Properties->width, Properties->height); |
326 | |
327 | Draw_Context->Current->modify_over->x += Properties->width; |
328 | |
329 | break; |
330 | case MIDDLE: |
1a31868c |
331 | gdk_gc_set_clip_origin( |
332 | Draw_Context->gc, |
333 | Draw_Context->Current->modify_middle->x, |
334 | Draw_Context->Current->modify_middle->y); |
09e2606f |
335 | gdk_draw_drawable(Draw_Context->drawable, |
1a31868c |
336 | Draw_Context->gc, |
337 | icon_info->pixmap, |
09e2606f |
338 | 0, 0, |
339 | Draw_Context->Current->modify_middle->x, |
340 | Draw_Context->Current->modify_middle->y, |
341 | Properties->width, Properties->height); |
342 | |
09e2606f |
343 | Draw_Context->Current->modify_middle->x += Properties->width; |
344 | break; |
345 | case UNDER: |
1a31868c |
346 | gdk_gc_set_clip_origin( |
347 | Draw_Context->gc, |
348 | Draw_Context->Current->modify_under->x, |
349 | Draw_Context->Current->modify_under->y); |
09e2606f |
350 | gdk_draw_drawable(Draw_Context->drawable, |
1a31868c |
351 | Draw_Context->gc, |
352 | icon_info->pixmap, |
09e2606f |
353 | 0, 0, |
354 | Draw_Context->Current->modify_under->x, |
355 | Draw_Context->Current->modify_under->y, |
356 | Properties->width, Properties->height); |
357 | |
358 | Draw_Context->Current->modify_under->x += Properties->width; |
359 | break; |
360 | } |
361 | |
1a31868c |
362 | gdk_gc_set_clip_origin(Draw_Context->gc, 0, 0); |
363 | gdk_gc_set_clip_mask(Draw_Context->gc, NULL); |
09e2606f |
364 | |
365 | return 0; |
b782dd11 |
366 | } |
367 | |
4c69e0cc |
368 | gboolean draw_line( void *hook_data, void *call_data) |
b782dd11 |
369 | { |
370 | PropertiesLine *Properties = (PropertiesLine*)hook_data; |
371 | DrawContext *Draw_Context = (DrawContext*)call_data; |
372 | |
09e2606f |
373 | gdk_gc_set_foreground(Draw_Context->gc, Properties->color); |
374 | gdk_gc_set_line_attributes( Draw_Context->gc, |
375 | Properties->line_width, |
376 | Properties->style, |
377 | GDK_CAP_BUTT, |
378 | GDK_JOIN_MITER); |
379 | |
380 | switch(Properties->position) { |
381 | case OVER: |
382 | drawing_draw_line( |
383 | NULL, Draw_Context->drawable, |
384 | Draw_Context->Previous->over->x, |
385 | Draw_Context->Previous->over->y, |
386 | Draw_Context->Current->over->x, |
387 | Draw_Context->Current->over->y, |
388 | Draw_Context->gc); |
389 | break; |
390 | case MIDDLE: |
391 | drawing_draw_line( |
392 | NULL, Draw_Context->drawable, |
393 | Draw_Context->Previous->middle->x, |
394 | Draw_Context->Previous->middle->y, |
395 | Draw_Context->Current->middle->x, |
396 | Draw_Context->Current->middle->y, |
397 | Draw_Context->gc); |
398 | break; |
399 | case UNDER: |
400 | drawing_draw_line( |
401 | NULL, Draw_Context->drawable, |
402 | Draw_Context->Previous->under->x, |
403 | Draw_Context->Previous->under->y, |
404 | Draw_Context->Current->under->x, |
405 | Draw_Context->Current->under->y, |
406 | Draw_Context->gc); |
407 | |
408 | break; |
409 | } |
410 | |
411 | return 0; |
b782dd11 |
412 | } |
413 | |
4c69e0cc |
414 | gboolean draw_arc( void *hook_data, void *call_data) |
b782dd11 |
415 | { |
416 | PropertiesArc *Properties = (PropertiesArc*)hook_data; |
417 | DrawContext *Draw_Context = (DrawContext*)call_data; |
418 | |
09e2606f |
419 | gdk_gc_set_foreground(Draw_Context->gc, Properties->color); |
420 | |
421 | switch(Properties->position) { |
422 | case OVER: |
423 | gdk_draw_arc(Draw_Context->drawable, Draw_Context->gc, |
424 | Properties->filled, |
425 | Draw_Context->Current->modify_over->x, |
426 | Draw_Context->Current->modify_over->y, |
427 | Properties->size, Properties->size, 0, 360*64); |
428 | Draw_Context->Current->modify_over->x += Properties->size; |
429 | |
430 | break; |
431 | case MIDDLE: |
432 | gdk_draw_arc(Draw_Context->drawable, Draw_Context->gc, |
433 | Properties->filled, |
434 | Draw_Context->Current->modify_middle->x, |
435 | Draw_Context->Current->modify_middle->y, |
436 | Properties->size, Properties->size, 0, 360*64); |
437 | Draw_Context->Current->modify_middle->x += Properties->size; |
438 | |
439 | break; |
440 | case UNDER: |
441 | gdk_draw_arc(Draw_Context->drawable, Draw_Context->gc, |
442 | Properties->filled, |
443 | Draw_Context->Current->modify_under->x, |
444 | Draw_Context->Current->modify_under->y, |
445 | Properties->size, Properties->size, 0, 360*64); |
446 | Draw_Context->Current->modify_under->x += Properties->size; |
447 | |
448 | break; |
449 | } |
450 | |
451 | |
452 | return 0; |
b782dd11 |
453 | } |
454 | |
4c69e0cc |
455 | gboolean draw_bg( void *hook_data, void *call_data) |
b782dd11 |
456 | { |
457 | PropertiesBG *Properties = (PropertiesBG*)hook_data; |
458 | DrawContext *Draw_Context = (DrawContext*)call_data; |
459 | |
09e2606f |
460 | gdk_gc_set_foreground(Draw_Context->gc, Properties->color); |
461 | |
462 | |
463 | gdk_draw_rectangle(Draw_Context->drawable, Draw_Context->gc, |
464 | TRUE, |
465 | Draw_Context->Previous->over->x, |
466 | Draw_Context->Previous->over->y, |
467 | Draw_Context->Current->over->x - Draw_Context->Previous->over->x, |
468 | Draw_Context->Previous->under->y); |
469 | |
470 | return 0; |
b782dd11 |
471 | } |
472 | |
473 | |