| 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 | #include <glib.h> |
| 98 | #include <gtk/gtk.h> |
| 99 | #include <gdk/gdk.h> |
| 100 | #include <lttv/hook.h> |
| 101 | #include <lttv/attribute.h> |
| 102 | #include <lttv/iattribute.h> |
| 103 | #include <string.h> |
| 104 | |
| 105 | #include <lttv/processTrace.h> |
| 106 | #include <lttv/state.h> |
| 107 | |
| 108 | #include "drawitem.h" |
| 109 | |
| 110 | |
| 111 | #define MAX_PATH_LEN 256 |
| 112 | |
| 113 | /* drawing hook functions */ |
| 114 | gboolean draw_text( void *hook_data, void *call_data) |
| 115 | { |
| 116 | PropertiesText *Properties = (PropertiesText*)hook_data; |
| 117 | DrawContext *Draw_Context = (DrawContext*)call_data; |
| 118 | |
| 119 | PangoContext *context; |
| 120 | PangoLayout *layout; |
| 121 | PangoAttribute *attribute; |
| 122 | PangoFontDescription *FontDesc;// = pango_font_description_new(); |
| 123 | gint Font_Size; |
| 124 | PangoRectangle ink_rect; |
| 125 | |
| 126 | layout = Draw_Context->pango_layout; |
| 127 | |
| 128 | context = pango_layout_get_context(layout); |
| 129 | FontDesc = pango_context_get_font_description(context); |
| 130 | |
| 131 | pango_font_description_set_size(FontDesc, Properties->size*PANGO_SCALE); |
| 132 | pango_layout_context_changed(layout); |
| 133 | |
| 134 | pango_layout_set_text(layout, Properties->text, -1); |
| 135 | pango_layout_get_pixel_extents(layout, &ink_rect, NULL); |
| 136 | switch(Properties->position) { |
| 137 | case OVER: |
| 138 | gdk_draw_layout_with_colors(Draw_Context->drawable, |
| 139 | Draw_Context->gc, |
| 140 | Draw_Context->current->modify_over->x, |
| 141 | Draw_Context->current->modify_over->y, |
| 142 | layout, Properties->foreground, Properties->background); |
| 143 | Draw_Context->current->modify_over->x += ink_rect.width; |
| 144 | |
| 145 | break; |
| 146 | case MIDDLE: |
| 147 | gdk_draw_layout_with_colors(Draw_Context->drawable, |
| 148 | Draw_Context->gc, |
| 149 | Draw_Context->current->modify_middle->x, |
| 150 | Draw_Context->current->modify_middle->y, |
| 151 | layout, Properties->foreground, Properties->background); |
| 152 | Draw_Context->current->modify_middle->x += ink_rect.width; |
| 153 | break; |
| 154 | case UNDER: |
| 155 | gdk_draw_layout_with_colors(Draw_Context->drawable, |
| 156 | Draw_Context->gc, |
| 157 | Draw_Context->current->modify_under->x, |
| 158 | Draw_Context->current->modify_under->y, |
| 159 | layout, Properties->foreground, Properties->background); |
| 160 | Draw_Context->current->modify_under->x += ink_rect.width; |
| 161 | break; |
| 162 | } |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | /* To speed up the process, search in already loaded icons list first. Only |
| 169 | * load it if not present. |
| 170 | */ |
| 171 | gboolean draw_icon( void *hook_data, void *call_data) |
| 172 | { |
| 173 | PropertiesIcon *Properties = (PropertiesIcon*)hook_data; |
| 174 | DrawContext *Draw_Context = (DrawContext*)call_data; |
| 175 | |
| 176 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
| 177 | LttvAttributeValue value; |
| 178 | gchar icon_name[MAX_PATH_LEN] = "icons/"; |
| 179 | IconStruct *icon_info; |
| 180 | |
| 181 | strcat(icon_name, Properties->icon_name); |
| 182 | |
| 183 | g_assert(lttv_iattribute_find_by_path(attributes, icon_name, |
| 184 | LTTV_POINTER, &value)); |
| 185 | if(*(value.v_pointer) == NULL) |
| 186 | { |
| 187 | *(value.v_pointer) = icon_info = g_new(IconStruct,1); |
| 188 | |
| 189 | icon_info->pixmap = gdk_pixmap_create_from_xpm(Draw_Context->drawable, |
| 190 | &icon_info->mask, NULL, Properties->icon_name); |
| 191 | } |
| 192 | else |
| 193 | { |
| 194 | icon_info = *(value.v_pointer); |
| 195 | } |
| 196 | |
| 197 | gdk_gc_set_clip_mask(Draw_Context->gc, icon_info->mask); |
| 198 | |
| 199 | switch(Properties->position) { |
| 200 | case OVER: |
| 201 | gdk_gc_set_clip_origin( |
| 202 | Draw_Context->gc, |
| 203 | Draw_Context->current->modify_over->x, |
| 204 | Draw_Context->current->modify_over->y); |
| 205 | gdk_draw_drawable(Draw_Context->drawable, |
| 206 | Draw_Context->gc, |
| 207 | icon_info->pixmap, |
| 208 | 0, 0, |
| 209 | Draw_Context->current->modify_over->x, |
| 210 | Draw_Context->current->modify_over->y, |
| 211 | Properties->width, Properties->height); |
| 212 | |
| 213 | Draw_Context->current->modify_over->x += Properties->width; |
| 214 | |
| 215 | break; |
| 216 | case MIDDLE: |
| 217 | gdk_gc_set_clip_origin( |
| 218 | Draw_Context->gc, |
| 219 | Draw_Context->current->modify_middle->x, |
| 220 | Draw_Context->current->modify_middle->y); |
| 221 | gdk_draw_drawable(Draw_Context->drawable, |
| 222 | Draw_Context->gc, |
| 223 | icon_info->pixmap, |
| 224 | 0, 0, |
| 225 | Draw_Context->current->modify_middle->x, |
| 226 | Draw_Context->current->modify_middle->y, |
| 227 | Properties->width, Properties->height); |
| 228 | |
| 229 | Draw_Context->current->modify_middle->x += Properties->width; |
| 230 | break; |
| 231 | case UNDER: |
| 232 | gdk_gc_set_clip_origin( |
| 233 | Draw_Context->gc, |
| 234 | Draw_Context->current->modify_under->x, |
| 235 | Draw_Context->current->modify_under->y); |
| 236 | gdk_draw_drawable(Draw_Context->drawable, |
| 237 | Draw_Context->gc, |
| 238 | icon_info->pixmap, |
| 239 | 0, 0, |
| 240 | Draw_Context->current->modify_under->x, |
| 241 | Draw_Context->current->modify_under->y, |
| 242 | Properties->width, Properties->height); |
| 243 | |
| 244 | Draw_Context->current->modify_under->x += Properties->width; |
| 245 | break; |
| 246 | } |
| 247 | |
| 248 | gdk_gc_set_clip_origin(Draw_Context->gc, 0, 0); |
| 249 | gdk_gc_set_clip_mask(Draw_Context->gc, NULL); |
| 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | gboolean draw_line( void *hook_data, void *call_data) |
| 255 | { |
| 256 | PropertiesLine *Properties = (PropertiesLine*)hook_data; |
| 257 | DrawContext *Draw_Context = (DrawContext*)call_data; |
| 258 | //GdkGC *gc = gdk_gc_new(Draw_Context->drawable); |
| 259 | |
| 260 | //gdk_gc_set_foreground(Draw_Context->gc, Properties->color); |
| 261 | gdk_gc_set_rgb_fg_color(Draw_Context->gc, Properties->color); |
| 262 | //gdk_gc_set_foreground(gc, Properties->color); |
| 263 | gdk_gc_set_line_attributes( Draw_Context->gc, |
| 264 | Properties->line_width, |
| 265 | Properties->style, |
| 266 | GDK_CAP_BUTT, |
| 267 | GDK_JOIN_MITER); |
| 268 | |
| 269 | switch(Properties->position) { |
| 270 | case OVER: |
| 271 | drawing_draw_line( |
| 272 | NULL, Draw_Context->drawable, |
| 273 | Draw_Context->previous->over->x, |
| 274 | Draw_Context->previous->over->y, |
| 275 | Draw_Context->current->over->x, |
| 276 | Draw_Context->current->over->y, |
| 277 | Draw_Context->gc); |
| 278 | break; |
| 279 | case MIDDLE: |
| 280 | drawing_draw_line( |
| 281 | NULL, Draw_Context->drawable, |
| 282 | Draw_Context->previous->middle->x, |
| 283 | Draw_Context->previous->middle->y, |
| 284 | Draw_Context->current->middle->x, |
| 285 | Draw_Context->current->middle->y, |
| 286 | Draw_Context->gc); |
| 287 | break; |
| 288 | case UNDER: |
| 289 | drawing_draw_line( |
| 290 | NULL, Draw_Context->drawable, |
| 291 | Draw_Context->previous->under->x, |
| 292 | Draw_Context->previous->under->y, |
| 293 | Draw_Context->current->under->x, |
| 294 | Draw_Context->current->under->y, |
| 295 | Draw_Context->gc); |
| 296 | |
| 297 | break; |
| 298 | } |
| 299 | |
| 300 | //gdk_gc_unref(gc); |
| 301 | |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | gboolean draw_arc( void *hook_data, void *call_data) |
| 306 | { |
| 307 | PropertiesArc *Properties = (PropertiesArc*)hook_data; |
| 308 | DrawContext *Draw_Context = (DrawContext*)call_data; |
| 309 | |
| 310 | //gdk_gc_set_foreground(Draw_Context->gc, Properties->color); |
| 311 | gdk_gc_set_rgb_fg_color(Draw_Context->gc, Properties->color); |
| 312 | |
| 313 | switch(Properties->position) { |
| 314 | case OVER: |
| 315 | gdk_draw_arc(Draw_Context->drawable, Draw_Context->gc, |
| 316 | Properties->filled, |
| 317 | Draw_Context->current->modify_over->x, |
| 318 | Draw_Context->current->modify_over->y, |
| 319 | Properties->size, Properties->size, 0, 360*64); |
| 320 | Draw_Context->current->modify_over->x += Properties->size; |
| 321 | break; |
| 322 | case MIDDLE: |
| 323 | gdk_draw_arc(Draw_Context->drawable, Draw_Context->gc, |
| 324 | Properties->filled, |
| 325 | Draw_Context->current->modify_middle->x, |
| 326 | Draw_Context->current->modify_middle->y, |
| 327 | Properties->size, Properties->size, 0, 360*64); |
| 328 | Draw_Context->current->modify_middle->x += Properties->size; |
| 329 | |
| 330 | break; |
| 331 | case UNDER: |
| 332 | gdk_draw_arc(Draw_Context->drawable, Draw_Context->gc, |
| 333 | Properties->filled, |
| 334 | Draw_Context->current->modify_under->x, |
| 335 | Draw_Context->current->modify_under->y, |
| 336 | Properties->size, Properties->size, 0, 360*64); |
| 337 | Draw_Context->current->modify_under->x += Properties->size; |
| 338 | |
| 339 | break; |
| 340 | } |
| 341 | |
| 342 | |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | gboolean draw_bg( void *hook_data, void *call_data) |
| 347 | { |
| 348 | PropertiesBG *Properties = (PropertiesBG*)hook_data; |
| 349 | DrawContext *Draw_Context = (DrawContext*)call_data; |
| 350 | |
| 351 | //gdk_gc_set_foreground(Draw_Context->gc, Properties->color); |
| 352 | gdk_gc_set_rgb_fg_color(Draw_Context->gc, Properties->color); |
| 353 | |
| 354 | |
| 355 | gdk_draw_rectangle(Draw_Context->drawable, Draw_Context->gc, |
| 356 | TRUE, |
| 357 | Draw_Context->previous->over->x, |
| 358 | Draw_Context->previous->over->y, |
| 359 | Draw_Context->current->over->x - Draw_Context->previous->over->x, |
| 360 | Draw_Context->previous->under->y); |
| 361 | |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | |