d3c7d96130480d40011b85636a47f75caddb2d79
[lttv.git] / lttv / modules / gui / histogram / histodrawitem.c
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
21 /******************************************************************************
22 * drawitem.c
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 *
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 *
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).
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)
60 * - Arc (big points) (color, size)
61 * - background color (color)
62 *
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 *
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
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.
74 *
75 * The array has to be sorted by priority each time we add a task in it.
76 * A priority is associated with each operation type. It permits
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 ),
85 * text being easy to replace with icons. The DrawContext is passed as
86 * call_data for the operation hooks.
87 *
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
91 * memory and set the pointer to the GdkPixmap in the attributes. The
92 * structure pointed to contains the pixmap and the mask bitmap.
93 *
94 * Author : Mathieu Desnoyers, October 2003
95 */
96
97 #ifdef HAVE_CONFIG_H
98 #include <config.h>
99 #endif
100
101 #include <glib.h>
102 #include <gtk/gtk.h>
103 #include <gdk/gdk.h>
104 #include <lttv/hook.h>
105 #include <lttv/attribute.h>
106 #include <lttv/iattribute.h>
107 #include <string.h>
108
109 #include <lttv/tracecontext.h>
110 #include <lttv/state.h>
111 #include <lttv/lttv.h>
112
113 #include "histodrawitem.h"
114 #include "histodrawing.h"
115
116
117 #define MAX_PATH_LEN 256
118
119 /* drawing hook functions */
120 gboolean histo_draw_text( void *hook_data, void *call_data)
121 {
122 histo_PropertiesText *properties = (histo_PropertiesText*)hook_data;
123 histo_DrawContext *draw_context = (histo_DrawContext*)call_data;
124
125 PangoContext *context;
126 PangoLayout *layout;
127 PangoFontDescription *font_desc;// = pango_font_description_new();
128 PangoRectangle ink_rect;
129
130 layout = draw_context->pango_layout;
131
132 context = pango_layout_get_context(layout);
133 font_desc = pango_context_get_font_description(context);
134
135 pango_font_description_set_size(font_desc, properties->size*PANGO_SCALE);
136 pango_layout_context_changed(layout);
137
138 pango_layout_set_text(layout, properties->text, -1);
139 pango_layout_get_pixel_extents(layout, &ink_rect, NULL);
140
141 gint x=0, y=0;
142 gint *offset=NULL;
143 gboolean enough_space = FALSE;
144 gint width = ink_rect.width;
145
146 switch(properties->histo_position.x) {
147 case POS_START:
148 x = draw_context->histo_drawinfo.start.x;
149 switch(properties->histo_position.y) {
150 case OVER:
151 offset = &draw_context->histo_drawinfo.start.offset.over;
152 x += draw_context->histo_drawinfo.start.offset.over;
153 y = draw_context->histo_drawinfo.y.over;
154 break;
155 case MIDDLE:
156 offset = &draw_context->histo_drawinfo.start.offset.middle;
157 x += draw_context->histo_drawinfo.start.offset.middle;
158 y = draw_context->histo_drawinfo.y.middle;
159 break;
160 case UNDER:
161 offset = &draw_context->histo_drawinfo.start.offset.under;
162 x += draw_context->histo_drawinfo.start.offset.under;
163 y = draw_context->histo_drawinfo.y.under;
164 break;
165 }
166 /* verify if there is enough space to draw */
167 if(unlikely(x + width <= draw_context->histo_drawinfo.end.x)) {
168 enough_space = TRUE;
169 *offset += width;
170 }
171 break;
172 case POS_END:
173 x = draw_context->histo_drawinfo.end.x;
174 switch(properties->histo_position.y) {
175 case OVER:
176 offset = &draw_context->histo_drawinfo.end.offset.over;
177 x += draw_context->histo_drawinfo.end.offset.over;
178 y = draw_context->histo_drawinfo.y.over;
179 break;
180 case MIDDLE:
181 offset = &draw_context->histo_drawinfo.end.offset.middle;
182 x += draw_context->histo_drawinfo.end.offset.middle;
183 y = draw_context->histo_drawinfo.y.middle;
184 break;
185 case UNDER:
186 offset = &draw_context->histo_drawinfo.end.offset.under;
187 x += draw_context->histo_drawinfo.end.offset.under;
188 y = draw_context->histo_drawinfo.y.under;
189 break;
190 }
191 /* verify if there is enough space to draw */
192 if(unlikely(x - width >= draw_context->histo_drawinfo.start.x)) {
193 enough_space = TRUE;
194 *offset -= width;
195 }
196 break;
197 }
198
199 if(unlikely(enough_space))
200 gdk_draw_layout_with_colors(draw_context->drawable,
201 draw_context->gc,
202 x,
203 y,
204 layout, properties->foreground, properties->background);
205
206 return 0;
207 }
208
209
210 /* To speed up the process, search in already loaded icons list first. Only
211 * load it if not present.
212 */
213 gboolean histo_draw_icon( void *hook_data, void *call_data)
214 {
215 histo_PropertiesIcon *properties = (histo_PropertiesIcon*)hook_data;
216 histo_DrawContext *draw_context = (histo_DrawContext*)call_data;
217 gboolean retval;
218
219 LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes());
220 LttvAttributeValue value;
221 gchar icon_name[MAX_PATH_LEN] = "icons/";
222 histo_IconStruct *icon_info;
223
224 strcat(icon_name, properties->icon_name);
225
226 retval= lttv_iattribute_find_by_path(attributes, icon_name,
227 LTTV_POINTER, &value);
228 g_assert(retval);
229 if(unlikely(*(value.v_pointer) == NULL))
230 {
231 *(value.v_pointer) = icon_info = g_new(histo_IconStruct,1);
232
233 icon_info->pixmap = gdk_pixmap_create_from_xpm(draw_context->drawable,
234 &icon_info->mask, NULL, properties->icon_name);
235 }
236 else
237 {
238 icon_info = *(value.v_pointer);
239 }
240
241 gint x=0, y=0;
242 gint *offset=NULL;
243 gboolean enough_space = FALSE;
244 gint width = properties->width;
245
246 switch(properties->histo_position.x) {
247 case POS_START:
248 x = draw_context->histo_drawinfo.start.x;
249 switch(properties->histo_position.y) {
250 case OVER:
251 offset = &draw_context->histo_drawinfo.start.offset.over;
252 x += draw_context->histo_drawinfo.start.offset.over;
253 y = draw_context->histo_drawinfo.y.over;
254 break;
255 case MIDDLE:
256 offset = &draw_context->histo_drawinfo.start.offset.middle;
257 x += draw_context->histo_drawinfo.start.offset.middle;
258 y = draw_context->histo_drawinfo.y.middle;
259 break;
260 case UNDER:
261 offset = &draw_context->histo_drawinfo.start.offset.under;
262 x += draw_context->histo_drawinfo.start.offset.under;
263 y = draw_context->histo_drawinfo.y.under;
264 break;
265 }
266 /* verify if there is enough space to draw */
267 if(unlikely(x + width <= draw_context->histo_drawinfo.end.x)) {
268 enough_space = TRUE;
269 *offset += width;
270 }
271 break;
272 case POS_END:
273 x = draw_context->histo_drawinfo.end.x;
274 switch(properties->histo_position.y) {
275 case OVER:
276 offset = &draw_context->histo_drawinfo.end.offset.over;
277 x += draw_context->histo_drawinfo.end.offset.over;
278 y = draw_context->histo_drawinfo.y.over;
279 break;
280 case MIDDLE:
281 offset = &draw_context->histo_drawinfo.end.offset.middle;
282 x += draw_context->histo_drawinfo.end.offset.middle;
283 y = draw_context->histo_drawinfo.y.middle;
284 break;
285 case UNDER:
286 offset = &draw_context->histo_drawinfo.end.offset.under;
287 x += draw_context->histo_drawinfo.end.offset.under;
288 y = draw_context->histo_drawinfo.y.under;
289 break;
290 }
291 /* verify if there is enough space to draw */
292 if(unlikely(x - width >= draw_context->histo_drawinfo.start.x)) {
293 enough_space = TRUE;
294 *offset -= width;
295 }
296 break;
297 }
298
299 if(unlikely(enough_space)) {
300 gdk_gc_set_clip_mask(draw_context->gc, icon_info->mask);
301
302 gdk_gc_set_clip_origin(
303 draw_context->gc,
304 x,
305 y);
306 gdk_draw_drawable(draw_context->drawable,
307 draw_context->gc,
308 icon_info->pixmap,
309 0, 0,
310 x,
311 y,
312 properties->width, properties->height);
313
314 gdk_gc_set_clip_origin(draw_context->gc, 0, 0);
315 gdk_gc_set_clip_mask(draw_context->gc, NULL);
316 }
317 return 0;
318 }
319
320 gboolean histo_draw_line( void *hook_data, void *call_data)
321 {
322 histo_PropertiesLine *properties = (histo_PropertiesLine*)hook_data;
323 histo_DrawContext *draw_context = (histo_DrawContext*)call_data;
324
325 gdk_gc_set_foreground(draw_context->gc, &properties->color);
326 //gdk_gc_set_rgb_fg_color(draw_context->gc, &properties->color);
327 gdk_gc_set_line_attributes( draw_context->gc,
328 properties->line_width,
329 properties->style,
330 GDK_CAP_BUTT,
331 GDK_JOIN_MITER);
332 //g_critical("DRAWING LINE : x1: %i, y1: %i, x2:%i, y2:%i",
333 // draw_context->previous->middle->x,
334 // draw_context->previous->middle->y,
335 // draw_context->drawinfo.middle.x,
336 // draw_context->drawinfo.middle.y);
337
338 gint x_begin=0, x_end=0, y=0;
339
340 x_begin = draw_context->histo_drawinfo.start.x;
341 x_end = draw_context->histo_drawinfo.end.x;
342
343 switch(properties->y) {
344 case OVER:
345 y = draw_context->histo_drawinfo.y.over;
346 break;
347 case MIDDLE:
348 y = draw_context->histo_drawinfo.y.middle;
349 break;
350 case UNDER:
351 y = draw_context->histo_drawinfo.y.under;
352 break;
353 }
354
355 histo_drawing_draw_line(
356 NULL, draw_context->drawable,
357 x_begin,
358 y,
359 x_end,
360 y,
361 draw_context->gc);
362
363 return 0;
364 }
365
366 gboolean histo_draw_arc( void *hook_data, void *call_data)
367 {
368 histo_PropertiesArc *properties = (histo_PropertiesArc*)hook_data;
369 histo_DrawContext *draw_context = (histo_DrawContext*)call_data;
370
371 gdk_gc_set_foreground(draw_context->gc, properties->color);
372 //gdk_gc_set_rgb_fg_color(draw_context->gc, properties->color);
373
374 gint x=0, y=0;
375 gint *offset=NULL;
376 gboolean enough_space = FALSE;
377 gint width = properties->size;
378
379 switch(properties->histo_position.x) {
380 case POS_START:
381 x = draw_context->histo_drawinfo.start.x;
382 switch(properties->histo_position.y) {
383 case OVER:
384 offset = &draw_context->histo_drawinfo.start.offset.over;
385 x += draw_context->histo_drawinfo.start.offset.over;
386 y = draw_context->histo_drawinfo.y.over;
387 break;
388 case MIDDLE:
389 offset = &draw_context->histo_drawinfo.start.offset.middle;
390 x += draw_context->histo_drawinfo.start.offset.middle;
391 y = draw_context->histo_drawinfo.y.middle;
392 break;
393 case UNDER:
394 offset = &draw_context->histo_drawinfo.start.offset.under;
395 x += draw_context->histo_drawinfo.start.offset.under;
396 y = draw_context->histo_drawinfo.y.under;
397 break;
398 }
399 /* verify if there is enough space to draw */
400 if(unlikely(x + width <= draw_context->histo_drawinfo.end.x)) {
401 enough_space = TRUE;
402 *offset += width;
403 }
404 break;
405 case POS_END:
406 x = draw_context->histo_drawinfo.end.x;
407 switch(properties->histo_position.y) {
408 case OVER:
409 offset = &draw_context->histo_drawinfo.end.offset.over;
410 x += draw_context->histo_drawinfo.end.offset.over;
411 y = draw_context->histo_drawinfo.y.over;
412 break;
413 case MIDDLE:
414 offset = &draw_context->histo_drawinfo.end.offset.middle;
415 x += draw_context->histo_drawinfo.end.offset.middle;
416 y = draw_context->histo_drawinfo.y.middle;
417 break;
418 case UNDER:
419 offset = &draw_context->histo_drawinfo.end.offset.under;
420 x += draw_context->histo_drawinfo.end.offset.under;
421 y = draw_context->histo_drawinfo.y.under;
422 break;
423 }
424 /* verify if there is enough space to draw */
425 if(unlikely(x - width >= draw_context->histo_drawinfo.start.x)) {
426 enough_space = TRUE;
427 *offset -= width;
428 }
429 break;
430 }
431
432 if(unlikely(enough_space))
433 gdk_draw_arc(draw_context->drawable, draw_context->gc,
434 properties->filled,
435 x,
436 y,
437 properties->size, properties->size, 0, 360*64);
438
439 return 0;
440 }
441
442 gboolean histo_draw_bg( void *hook_data, void *call_data)
443 {
444 histo_PropertiesBG *properties = (histo_PropertiesBG*)hook_data;
445 histo_DrawContext *draw_context = (histo_DrawContext*)call_data;
446
447 gdk_gc_set_foreground(draw_context->gc, properties->color);
448 //gdk_gc_set_rgb_fg_color(draw_context->gc, properties->color);
449
450 //g_critical("DRAWING RECT : x: %i, y: %i, w:%i, h:%i, val1 :%i, val2:%i ",
451 // draw_context->previous->over->x,
452 // draw_context->previous->over->y,
453 // draw_context->drawinfo.over.x - draw_context->previous->over->x,
454 // draw_context->previous->under->y-draw_context->previous->over->y,
455 // draw_context->drawinfo.over.x,
456 // draw_context->previous->over->x);
457 gdk_draw_rectangle(draw_context->drawable, draw_context->gc,
458 TRUE,
459 draw_context->histo_drawinfo.start.x,
460 draw_context->histo_drawinfo.y.over,
461 draw_context->histo_drawinfo.end.x - draw_context->histo_drawinfo.start.x,
462 draw_context->histo_drawinfo.y.under - draw_context->histo_drawinfo.y.over);
463
464 return 0;
465 }
466
467
This page took 0.037776 seconds and 3 git commands to generate.