ce0214a6 |
1 | /* This file is part of the Linux Trace Toolkit viewer |
2 | * Copyright (C) 2003-2004 Mathieu Desnoyers |
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 Version 2 as |
6 | * published by the Free Software Foundation; |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License |
14 | * along with this program; if not, write to the Free Software |
15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
16 | * MA 02111-1307, USA. |
17 | */ |
18 | |
19 | |
20 | |
cf6cb7e0 |
21 | /****************************************************************************** |
d66666fe |
22 | * drawitem.c |
cf6cb7e0 |
23 | * |
24 | * This file contains methods responsible for drawing a generic type of data |
25 | * in a drawable. Doing this generically will permit user defined drawing |
26 | * behavior in a later time. |
27 | * |
b782dd11 |
28 | * This file provides an API which is meant to be reusable for all viewers that |
29 | * need to show information in line, icon, text, background or point form in |
30 | * a drawable area having time for x axis. The y axis, in the control flow |
31 | * viewer case, is corresponding to the different processes, but it can be |
32 | * reused integrally for cpu, and eventually locks, buffers, network |
33 | * interfaces... What will differ between the viewers is the precise |
34 | * information which interests us. We may think that the most useful |
35 | * information for control flow are some specific events, like schedule |
36 | * change, and processes'states. It may differ for a cpu viewer : the |
37 | * interesting information could be more the execution mode of each cpu. |
38 | * This API in meant to make viewer's writers life easier : it will become |
39 | * a simple choice of icons and line types for the precise information |
40 | * the viewer has to provide (agremented with keeping supplementary records |
41 | * and modifying slightly the DrawContext to suit the needs.) |
42 | * |
f0728492 |
43 | * We keep each data type in attributes, keys to specific information |
44 | * being formed from the GQuark corresponding to the information received. |
45 | * (facilities / facility_name / events / eventname.) |
46 | * (cpus/cpu_name, process_states/ps_name, |
47 | * execution_modes/em_name, execution_submodes/es_name). |
cf6cb7e0 |
48 | * The goal is then to provide a generic way to print information on the |
49 | * screen for all this different information. |
50 | * |
51 | * Information can be printed as |
52 | * |
53 | * - text (text + color + size + position (over or under line) |
54 | * - icon (icon filename, corresponding to a loaded icon, accessible through |
55 | * a GQuark. Icons are loaded statically at the guiControlFlow level during |
56 | * module initialization and can be added on the fly if not present in the |
57 | * GQuark.) The habitual place for xpm icons is in |
58 | * ${prefix}/share/LinuxTraceToolkit.) + position (over or under line) |
59 | * - line (color, width, style) |
189a5d08 |
60 | * - Arc (big points) (color, size) |
cf6cb7e0 |
61 | * - background color (color) |
62 | * |
189a5d08 |
63 | * An item is a leaf of the attributes tree. It is, in that case, including |
64 | * all kind of events categories we can have. It then associates each category |
65 | * with one or more actions (drawing something) or nothing. |
66 | * |
7d5ffafa |
67 | * Each item has an array of hooks (hook list). Each hook represents an |
68 | * operation to perform. We seek the array each time we want to |
a2e850ff |
69 | * draw an item. We execute each operation in order. An operation type |
70 | * is associated with each hook to permit user listing and modification |
71 | * of these operations. The operation type is also used to find the |
72 | * corresponding priority for the sorting. Operation type and priorities |
73 | * are enum and a static int table. |
cf6cb7e0 |
74 | * |
75 | * The array has to be sorted by priority each time we add a task in it. |
a2e850ff |
76 | * A priority is associated with each operation type. It permits |
cf6cb7e0 |
77 | * to perform background color selection before line or text drawing. We also |
78 | * draw lines before text, so the text appears over the lines. |
79 | * |
80 | * Executing all the arrays of operations for a specific event (which |
81 | * implies information for state, event, cpu, execution mode and submode) |
82 | * has to be done in a same DrawContext. The goal there is to keep the offset |
83 | * of the text and icons over and under the middle line, so a specific |
84 | * event could be printed as ( R Si 0 for running, scheduled in, cpu 0 ), |
7d5ffafa |
85 | * text being easy to replace with icons. The DrawContext is passed as |
86 | * call_data for the operation hooks. |
cf6cb7e0 |
87 | * |
b782dd11 |
88 | * We use the lttv global attributes to keep track of the loaded icons. |
89 | * If we need an icon, we look for it in the icons / icon name pathname. |
90 | * If found, we use the pointer to it. If not, we load the pixmap in |
1a31868c |
91 | * memory and set the pointer to the GdkPixmap in the attributes. The |
92 | * structure pointed to contains the pixmap and the mask bitmap. |
b782dd11 |
93 | * |
cf6cb7e0 |
94 | * Author : Mathieu Desnoyers, October 2003 |
95 | */ |
7d5ffafa |
96 | |
97 | #include <glib.h> |
09e2606f |
98 | #include <gtk/gtk.h> |
99 | #include <gdk/gdk.h> |
7d5ffafa |
100 | #include <lttv/hook.h> |
f0728492 |
101 | #include <lttv/attribute.h> |
102 | #include <lttv/iattribute.h> |
1a31868c |
103 | #include <string.h> |
7d5ffafa |
104 | |
d8f124de |
105 | #include <lttv/tracecontext.h> |
b782dd11 |
106 | #include <lttv/state.h> |
2eef04b5 |
107 | #include <lttv/lttv.h> |
b782dd11 |
108 | |
d66666fe |
109 | #include "drawitem.h" |
1a31868c |
110 | |
111 | |
112 | #define MAX_PATH_LEN 256 |
113 | |
501d5405 |
114 | /* drawing hook functions */ |
6550d711 |
115 | gboolean draw_text( void *hook_data, void *call_data) |
b782dd11 |
116 | { |
e800cf84 |
117 | PropertiesText *properties = (PropertiesText*)hook_data; |
c8bba5fa |
118 | DrawContext *draw_context = (DrawContext*)call_data; |
a56a1ba4 |
119 | |
120 | PangoContext *context; |
121 | PangoLayout *layout; |
c8bba5fa |
122 | PangoFontDescription *font_desc;// = pango_font_description_new(); |
a56a1ba4 |
123 | PangoRectangle ink_rect; |
124 | |
c8bba5fa |
125 | layout = draw_context->pango_layout; |
a56a1ba4 |
126 | |
127 | context = pango_layout_get_context(layout); |
c8bba5fa |
128 | font_desc = pango_context_get_font_description(context); |
a56a1ba4 |
129 | |
e800cf84 |
130 | pango_font_description_set_size(font_desc, properties->size*PANGO_SCALE); |
a56a1ba4 |
131 | pango_layout_context_changed(layout); |
132 | |
e800cf84 |
133 | pango_layout_set_text(layout, properties->text, -1); |
a56a1ba4 |
134 | pango_layout_get_pixel_extents(layout, &ink_rect, NULL); |
a56a1ba4 |
135 | |
e800cf84 |
136 | gint x=0, y=0; |
137 | gint *offset=NULL; |
138 | gboolean enough_space = FALSE; |
139 | gint width = ink_rect.width; |
140 | |
141 | switch(properties->position.x) { |
142 | case POS_START: |
143 | x = draw_context->drawinfo.start.x; |
144 | switch(properties->position.y) { |
145 | case OVER: |
146 | offset = &draw_context->drawinfo.start.offset.over; |
147 | x += draw_context->drawinfo.start.offset.over; |
148 | y = draw_context->drawinfo.y.over; |
149 | break; |
150 | case MIDDLE: |
151 | offset = &draw_context->drawinfo.start.offset.middle; |
152 | x += draw_context->drawinfo.start.offset.middle; |
153 | y = draw_context->drawinfo.y.middle; |
154 | break; |
155 | case UNDER: |
156 | offset = &draw_context->drawinfo.start.offset.under; |
157 | x += draw_context->drawinfo.start.offset.under; |
158 | y = draw_context->drawinfo.y.under; |
159 | break; |
160 | } |
161 | /* verify if there is enough space to draw */ |
1d1df11d |
162 | if(unlikely(x + width <= draw_context->drawinfo.end.x)) { |
e800cf84 |
163 | enough_space = TRUE; |
164 | *offset += width; |
165 | } |
a56a1ba4 |
166 | break; |
e800cf84 |
167 | case POS_END: |
168 | x = draw_context->drawinfo.end.x; |
169 | switch(properties->position.y) { |
170 | case OVER: |
171 | offset = &draw_context->drawinfo.end.offset.over; |
172 | x += draw_context->drawinfo.end.offset.over; |
173 | y = draw_context->drawinfo.y.over; |
174 | break; |
175 | case MIDDLE: |
176 | offset = &draw_context->drawinfo.end.offset.middle; |
177 | x += draw_context->drawinfo.end.offset.middle; |
178 | y = draw_context->drawinfo.y.middle; |
179 | break; |
180 | case UNDER: |
181 | offset = &draw_context->drawinfo.end.offset.under; |
182 | x += draw_context->drawinfo.end.offset.under; |
183 | y = draw_context->drawinfo.y.under; |
184 | break; |
185 | } |
186 | /* verify if there is enough space to draw */ |
1d1df11d |
187 | if(unlikely(x - width >= draw_context->drawinfo.start.x)) { |
e800cf84 |
188 | enough_space = TRUE; |
189 | *offset -= width; |
190 | } |
a56a1ba4 |
191 | break; |
192 | } |
193 | |
1d1df11d |
194 | if(unlikely(enough_space)) |
e800cf84 |
195 | gdk_draw_layout_with_colors(draw_context->drawable, |
196 | draw_context->gc, |
197 | x, |
198 | y, |
199 | layout, properties->foreground, properties->background); |
200 | |
a56a1ba4 |
201 | return 0; |
b782dd11 |
202 | } |
203 | |
1a31868c |
204 | |
205 | /* To speed up the process, search in already loaded icons list first. Only |
206 | * load it if not present. |
207 | */ |
6550d711 |
208 | gboolean draw_icon( void *hook_data, void *call_data) |
b782dd11 |
209 | { |
c8bba5fa |
210 | PropertiesIcon *properties = (PropertiesIcon*)hook_data; |
211 | DrawContext *draw_context = (DrawContext*)call_data; |
b782dd11 |
212 | |
1a31868c |
213 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
a56a1ba4 |
214 | LttvAttributeValue value; |
215 | gchar icon_name[MAX_PATH_LEN] = "icons/"; |
216 | IconStruct *icon_info; |
8d088fb2 |
217 | |
c8bba5fa |
218 | strcat(icon_name, properties->icon_name); |
a56a1ba4 |
219 | |
1a31868c |
220 | g_assert(lttv_iattribute_find_by_path(attributes, icon_name, |
221 | LTTV_POINTER, &value)); |
1d1df11d |
222 | if(unlikely(*(value.v_pointer) == NULL)) |
a56a1ba4 |
223 | { |
224 | *(value.v_pointer) = icon_info = g_new(IconStruct,1); |
225 | |
c8bba5fa |
226 | icon_info->pixmap = gdk_pixmap_create_from_xpm(draw_context->drawable, |
227 | &icon_info->mask, NULL, properties->icon_name); |
a56a1ba4 |
228 | } |
229 | else |
230 | { |
231 | icon_info = *(value.v_pointer); |
232 | } |
233 | |
e800cf84 |
234 | gint x=0, y=0; |
235 | gint *offset=NULL; |
236 | gboolean enough_space = FALSE; |
237 | gint width = properties->width; |
a56a1ba4 |
238 | |
e800cf84 |
239 | switch(properties->position.x) { |
240 | case POS_START: |
241 | x = draw_context->drawinfo.start.x; |
242 | switch(properties->position.y) { |
243 | case OVER: |
244 | offset = &draw_context->drawinfo.start.offset.over; |
245 | x += draw_context->drawinfo.start.offset.over; |
246 | y = draw_context->drawinfo.y.over; |
247 | break; |
248 | case MIDDLE: |
249 | offset = &draw_context->drawinfo.start.offset.middle; |
250 | x += draw_context->drawinfo.start.offset.middle; |
251 | y = draw_context->drawinfo.y.middle; |
252 | break; |
253 | case UNDER: |
254 | offset = &draw_context->drawinfo.start.offset.under; |
255 | x += draw_context->drawinfo.start.offset.under; |
256 | y = draw_context->drawinfo.y.under; |
257 | break; |
258 | } |
259 | /* verify if there is enough space to draw */ |
1d1df11d |
260 | if(unlikely(x + width <= draw_context->drawinfo.end.x)) { |
e800cf84 |
261 | enough_space = TRUE; |
262 | *offset += width; |
263 | } |
a56a1ba4 |
264 | break; |
e800cf84 |
265 | case POS_END: |
266 | x = draw_context->drawinfo.end.x; |
267 | switch(properties->position.y) { |
268 | case OVER: |
269 | offset = &draw_context->drawinfo.end.offset.over; |
270 | x += draw_context->drawinfo.end.offset.over; |
271 | y = draw_context->drawinfo.y.over; |
272 | break; |
273 | case MIDDLE: |
274 | offset = &draw_context->drawinfo.end.offset.middle; |
275 | x += draw_context->drawinfo.end.offset.middle; |
276 | y = draw_context->drawinfo.y.middle; |
277 | break; |
278 | case UNDER: |
279 | offset = &draw_context->drawinfo.end.offset.under; |
280 | x += draw_context->drawinfo.end.offset.under; |
281 | y = draw_context->drawinfo.y.under; |
282 | break; |
283 | } |
284 | /* verify if there is enough space to draw */ |
1d1df11d |
285 | if(unlikely(x - width >= draw_context->drawinfo.start.x)) { |
e800cf84 |
286 | enough_space = TRUE; |
287 | *offset -= width; |
288 | } |
a56a1ba4 |
289 | break; |
290 | } |
291 | |
1d1df11d |
292 | if(unlikely(enough_space)) { |
e800cf84 |
293 | gdk_gc_set_clip_mask(draw_context->gc, icon_info->mask); |
294 | |
295 | gdk_gc_set_clip_origin( |
296 | draw_context->gc, |
297 | x, |
298 | y); |
299 | gdk_draw_drawable(draw_context->drawable, |
300 | draw_context->gc, |
301 | icon_info->pixmap, |
302 | 0, 0, |
303 | x, |
304 | y, |
305 | properties->width, properties->height); |
306 | |
307 | gdk_gc_set_clip_origin(draw_context->gc, 0, 0); |
308 | gdk_gc_set_clip_mask(draw_context->gc, NULL); |
309 | } |
a56a1ba4 |
310 | return 0; |
b782dd11 |
311 | } |
312 | |
6550d711 |
313 | gboolean draw_line( void *hook_data, void *call_data) |
b782dd11 |
314 | { |
c8bba5fa |
315 | PropertiesLine *properties = (PropertiesLine*)hook_data; |
316 | DrawContext *draw_context = (DrawContext*)call_data; |
a56a1ba4 |
317 | |
f16a5569 |
318 | gdk_gc_set_foreground(draw_context->gc, &properties->color); |
319 | //gdk_gc_set_rgb_fg_color(draw_context->gc, &properties->color); |
c8bba5fa |
320 | gdk_gc_set_line_attributes( draw_context->gc, |
321 | properties->line_width, |
322 | properties->style, |
a56a1ba4 |
323 | GDK_CAP_BUTT, |
324 | GDK_JOIN_MITER); |
d0cd7f09 |
325 | //g_critical("DRAWING LINE : x1: %i, y1: %i, x2:%i, y2:%i", |
c8bba5fa |
326 | // draw_context->previous->middle->x, |
327 | // draw_context->previous->middle->y, |
328 | // draw_context->drawinfo.middle.x, |
329 | // draw_context->drawinfo.middle.y); |
a56a1ba4 |
330 | |
e800cf84 |
331 | gint x_begin=0, x_end=0, y=0; |
332 | |
333 | x_begin = draw_context->drawinfo.start.x; |
334 | x_end = draw_context->drawinfo.end.x; |
335 | |
336 | switch(properties->y) { |
a56a1ba4 |
337 | case OVER: |
e800cf84 |
338 | y = draw_context->drawinfo.y.over; |
a56a1ba4 |
339 | break; |
340 | case MIDDLE: |
e800cf84 |
341 | y = draw_context->drawinfo.y.middle; |
a56a1ba4 |
342 | break; |
343 | case UNDER: |
e800cf84 |
344 | y = draw_context->drawinfo.y.under; |
a56a1ba4 |
345 | break; |
346 | } |
e800cf84 |
347 | |
348 | drawing_draw_line( |
349 | NULL, draw_context->drawable, |
350 | x_begin, |
351 | y, |
352 | x_end, |
353 | y, |
354 | draw_context->gc); |
a56a1ba4 |
355 | |
356 | return 0; |
b782dd11 |
357 | } |
358 | |
6550d711 |
359 | gboolean draw_arc( void *hook_data, void *call_data) |
b782dd11 |
360 | { |
c8bba5fa |
361 | PropertiesArc *properties = (PropertiesArc*)hook_data; |
362 | DrawContext *draw_context = (DrawContext*)call_data; |
a56a1ba4 |
363 | |
f16a5569 |
364 | gdk_gc_set_foreground(draw_context->gc, properties->color); |
365 | //gdk_gc_set_rgb_fg_color(draw_context->gc, properties->color); |
a56a1ba4 |
366 | |
e800cf84 |
367 | gint x=0, y=0; |
368 | gint *offset=NULL; |
369 | gboolean enough_space = FALSE; |
370 | gint width = properties->size; |
a56a1ba4 |
371 | |
e800cf84 |
372 | switch(properties->position.x) { |
373 | case POS_START: |
374 | x = draw_context->drawinfo.start.x; |
375 | switch(properties->position.y) { |
376 | case OVER: |
377 | offset = &draw_context->drawinfo.start.offset.over; |
378 | x += draw_context->drawinfo.start.offset.over; |
379 | y = draw_context->drawinfo.y.over; |
380 | break; |
381 | case MIDDLE: |
382 | offset = &draw_context->drawinfo.start.offset.middle; |
383 | x += draw_context->drawinfo.start.offset.middle; |
384 | y = draw_context->drawinfo.y.middle; |
385 | break; |
386 | case UNDER: |
387 | offset = &draw_context->drawinfo.start.offset.under; |
388 | x += draw_context->drawinfo.start.offset.under; |
389 | y = draw_context->drawinfo.y.under; |
390 | break; |
391 | } |
392 | /* verify if there is enough space to draw */ |
1d1df11d |
393 | if(unlikely(x + width <= draw_context->drawinfo.end.x)) { |
e800cf84 |
394 | enough_space = TRUE; |
395 | *offset += width; |
396 | } |
397 | break; |
398 | case POS_END: |
399 | x = draw_context->drawinfo.end.x; |
400 | switch(properties->position.y) { |
401 | case OVER: |
402 | offset = &draw_context->drawinfo.end.offset.over; |
403 | x += draw_context->drawinfo.end.offset.over; |
404 | y = draw_context->drawinfo.y.over; |
405 | break; |
406 | case MIDDLE: |
407 | offset = &draw_context->drawinfo.end.offset.middle; |
408 | x += draw_context->drawinfo.end.offset.middle; |
409 | y = draw_context->drawinfo.y.middle; |
410 | break; |
411 | case UNDER: |
412 | offset = &draw_context->drawinfo.end.offset.under; |
413 | x += draw_context->drawinfo.end.offset.under; |
414 | y = draw_context->drawinfo.y.under; |
415 | break; |
416 | } |
417 | /* verify if there is enough space to draw */ |
1d1df11d |
418 | if(unlikely(x - width >= draw_context->drawinfo.start.x)) { |
e800cf84 |
419 | enough_space = TRUE; |
420 | *offset -= width; |
421 | } |
a56a1ba4 |
422 | break; |
423 | } |
424 | |
1d1df11d |
425 | if(unlikely(enough_space)) |
e800cf84 |
426 | gdk_draw_arc(draw_context->drawable, draw_context->gc, |
427 | properties->filled, |
428 | x, |
429 | y, |
430 | properties->size, properties->size, 0, 360*64); |
a56a1ba4 |
431 | |
432 | return 0; |
b782dd11 |
433 | } |
434 | |
6550d711 |
435 | gboolean draw_bg( void *hook_data, void *call_data) |
b782dd11 |
436 | { |
c8bba5fa |
437 | PropertiesBG *properties = (PropertiesBG*)hook_data; |
438 | DrawContext *draw_context = (DrawContext*)call_data; |
b782dd11 |
439 | |
f16a5569 |
440 | gdk_gc_set_foreground(draw_context->gc, properties->color); |
441 | //gdk_gc_set_rgb_fg_color(draw_context->gc, properties->color); |
09e2606f |
442 | |
d0cd7f09 |
443 | //g_critical("DRAWING RECT : x: %i, y: %i, w:%i, h:%i, val1 :%i, val2:%i ", |
c8bba5fa |
444 | // draw_context->previous->over->x, |
445 | // draw_context->previous->over->y, |
446 | // draw_context->drawinfo.over.x - draw_context->previous->over->x, |
447 | // draw_context->previous->under->y-draw_context->previous->over->y, |
448 | // draw_context->drawinfo.over.x, |
449 | // draw_context->previous->over->x); |
450 | gdk_draw_rectangle(draw_context->drawable, draw_context->gc, |
a56a1ba4 |
451 | TRUE, |
e800cf84 |
452 | draw_context->drawinfo.start.x, |
453 | draw_context->drawinfo.y.over, |
454 | draw_context->drawinfo.end.x - draw_context->drawinfo.start.x, |
455 | draw_context->drawinfo.y.under - draw_context->drawinfo.y.over); |
09e2606f |
456 | |
a56a1ba4 |
457 | return 0; |
b782dd11 |
458 | } |
459 | |
460 | |