| 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 | * |
| 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 | * |
| 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). |
| 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) |
| 40 | * - Arc (big points) (color, size) |
| 41 | * - background color (color) |
| 42 | * |
| 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 | * |
| 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 |
| 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. |
| 54 | * |
| 55 | * The array has to be sorted by priority each time we add a task in it. |
| 56 | * A priority is associated with each operation type. It permits |
| 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 ), |
| 65 | * text being easy to replace with icons. The DrawContext is passed as |
| 66 | * call_data for the operation hooks. |
| 67 | * |
| 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 |
| 71 | * memory and set the pointer to the GdkPixmap in the attributes. The |
| 72 | * structure pointed to contains the pixmap and the mask bitmap. |
| 73 | * |
| 74 | * Author : Mathieu Desnoyers, October 2003 |
| 75 | */ |
| 76 | |
| 77 | #include <glib.h> |
| 78 | #include <gtk/gtk.h> |
| 79 | #include <gdk/gdk.h> |
| 80 | #include <lttv/hook.h> |
| 81 | #include <lttv/attribute.h> |
| 82 | #include <lttv/iattribute.h> |
| 83 | #include <string.h> |
| 84 | |
| 85 | #include <lttv/processTrace.h> |
| 86 | #include <lttv/state.h> |
| 87 | |
| 88 | #include "Draw_Item.h" |
| 89 | |
| 90 | |
| 91 | #define MAX_PATH_LEN 256 |
| 92 | |
| 93 | /* Drawing hook functions */ |
| 94 | gboolean draw_text( void *hook_data, void *call_data) |
| 95 | { |
| 96 | PropertiesText *Properties = (PropertiesText*)hook_data; |
| 97 | DrawContext *Draw_Context = (DrawContext*)call_data; |
| 98 | |
| 99 | PangoContext *context; |
| 100 | PangoLayout *layout; |
| 101 | PangoFontDescription *FontDesc;// = pango_font_description_new(); |
| 102 | gint Font_Size; |
| 103 | PangoRectangle ink_rect; |
| 104 | |
| 105 | gdk_gc_set_foreground(Draw_Context->gc, Properties->foreground); |
| 106 | gdk_gc_set_background(Draw_Context->gc, Properties->background); |
| 107 | |
| 108 | layout = gtk_widget_create_pango_layout(GTK_WIDGET(Draw_Context->drawable), NULL); |
| 109 | context = pango_layout_get_context(layout); |
| 110 | FontDesc = pango_context_get_font_description(context); |
| 111 | Font_Size = pango_font_description_get_size(FontDesc); |
| 112 | pango_font_description_set_size(FontDesc, Properties->size*PANGO_SCALE); |
| 113 | |
| 114 | |
| 115 | pango_layout_set_text(layout, Properties->Text, -1); |
| 116 | pango_layout_get_pixel_extents(layout, &ink_rect, NULL); |
| 117 | switch(Properties->position) { |
| 118 | case OVER: |
| 119 | gdk_draw_layout(Draw_Context->drawable, Draw_Context->gc, |
| 120 | Draw_Context->Current->modify_over->x, |
| 121 | Draw_Context->Current->modify_over->y, |
| 122 | layout); |
| 123 | Draw_Context->Current->modify_over->x += ink_rect.width; |
| 124 | |
| 125 | break; |
| 126 | case MIDDLE: |
| 127 | gdk_draw_layout(Draw_Context->drawable, Draw_Context->gc, |
| 128 | Draw_Context->Current->modify_middle->x, |
| 129 | Draw_Context->Current->modify_middle->y, |
| 130 | layout); |
| 131 | Draw_Context->Current->modify_middle->x += ink_rect.width; |
| 132 | break; |
| 133 | case UNDER: |
| 134 | gdk_draw_layout(Draw_Context->drawable, Draw_Context->gc, |
| 135 | Draw_Context->Current->modify_under->x, |
| 136 | Draw_Context->Current->modify_under->y, |
| 137 | layout); |
| 138 | Draw_Context->Current->modify_under->x += ink_rect.width; |
| 139 | break; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | pango_font_description_set_size(FontDesc, Font_Size); |
| 144 | g_free(layout); |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | |
| 150 | /* To speed up the process, search in already loaded icons list first. Only |
| 151 | * load it if not present. |
| 152 | */ |
| 153 | gboolean draw_icon( void *hook_data, void *call_data) |
| 154 | { |
| 155 | PropertiesIcon *Properties = (PropertiesIcon*)hook_data; |
| 156 | DrawContext *Draw_Context = (DrawContext*)call_data; |
| 157 | |
| 158 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
| 159 | LttvAttributeValue value; |
| 160 | gchar icon_name[MAX_PATH_LEN] = "icons/"; |
| 161 | IconStruct *icon_info; |
| 162 | |
| 163 | strcat(icon_name, Properties->icon_name); |
| 164 | |
| 165 | g_assert(lttv_iattribute_find_by_path(attributes, icon_name, |
| 166 | LTTV_POINTER, &value)); |
| 167 | if(*(value.v_pointer) == NULL) |
| 168 | { |
| 169 | *(value.v_pointer) = icon_info = g_new(IconStruct,1); |
| 170 | |
| 171 | icon_info->pixmap = gdk_pixmap_create_from_xpm(Draw_Context->drawable, |
| 172 | &icon_info->mask, NULL, Properties->icon_name); |
| 173 | } |
| 174 | else |
| 175 | { |
| 176 | icon_info = *(value.v_pointer); |
| 177 | } |
| 178 | |
| 179 | gdk_gc_set_clip_mask(Draw_Context->gc, icon_info->mask); |
| 180 | |
| 181 | switch(Properties->position) { |
| 182 | case OVER: |
| 183 | gdk_gc_set_clip_origin( |
| 184 | Draw_Context->gc, |
| 185 | Draw_Context->Current->modify_over->x, |
| 186 | Draw_Context->Current->modify_over->y); |
| 187 | gdk_draw_drawable(Draw_Context->drawable, |
| 188 | Draw_Context->gc, |
| 189 | icon_info->pixmap, |
| 190 | 0, 0, |
| 191 | Draw_Context->Current->modify_over->x, |
| 192 | Draw_Context->Current->modify_over->y, |
| 193 | Properties->width, Properties->height); |
| 194 | |
| 195 | Draw_Context->Current->modify_over->x += Properties->width; |
| 196 | |
| 197 | break; |
| 198 | case MIDDLE: |
| 199 | gdk_gc_set_clip_origin( |
| 200 | Draw_Context->gc, |
| 201 | Draw_Context->Current->modify_middle->x, |
| 202 | Draw_Context->Current->modify_middle->y); |
| 203 | gdk_draw_drawable(Draw_Context->drawable, |
| 204 | Draw_Context->gc, |
| 205 | icon_info->pixmap, |
| 206 | 0, 0, |
| 207 | Draw_Context->Current->modify_middle->x, |
| 208 | Draw_Context->Current->modify_middle->y, |
| 209 | Properties->width, Properties->height); |
| 210 | |
| 211 | Draw_Context->Current->modify_middle->x += Properties->width; |
| 212 | break; |
| 213 | case UNDER: |
| 214 | gdk_gc_set_clip_origin( |
| 215 | Draw_Context->gc, |
| 216 | Draw_Context->Current->modify_under->x, |
| 217 | Draw_Context->Current->modify_under->y); |
| 218 | gdk_draw_drawable(Draw_Context->drawable, |
| 219 | Draw_Context->gc, |
| 220 | icon_info->pixmap, |
| 221 | 0, 0, |
| 222 | Draw_Context->Current->modify_under->x, |
| 223 | Draw_Context->Current->modify_under->y, |
| 224 | Properties->width, Properties->height); |
| 225 | |
| 226 | Draw_Context->Current->modify_under->x += Properties->width; |
| 227 | break; |
| 228 | } |
| 229 | |
| 230 | gdk_gc_set_clip_origin(Draw_Context->gc, 0, 0); |
| 231 | gdk_gc_set_clip_mask(Draw_Context->gc, NULL); |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | gboolean draw_line( void *hook_data, void *call_data) |
| 237 | { |
| 238 | PropertiesLine *Properties = (PropertiesLine*)hook_data; |
| 239 | DrawContext *Draw_Context = (DrawContext*)call_data; |
| 240 | |
| 241 | gdk_gc_set_foreground(Draw_Context->gc, Properties->color); |
| 242 | gdk_gc_set_line_attributes( Draw_Context->gc, |
| 243 | Properties->line_width, |
| 244 | Properties->style, |
| 245 | GDK_CAP_BUTT, |
| 246 | GDK_JOIN_MITER); |
| 247 | |
| 248 | switch(Properties->position) { |
| 249 | case OVER: |
| 250 | drawing_draw_line( |
| 251 | NULL, Draw_Context->drawable, |
| 252 | Draw_Context->Previous->over->x, |
| 253 | Draw_Context->Previous->over->y, |
| 254 | Draw_Context->Current->over->x, |
| 255 | Draw_Context->Current->over->y, |
| 256 | Draw_Context->gc); |
| 257 | break; |
| 258 | case MIDDLE: |
| 259 | drawing_draw_line( |
| 260 | NULL, Draw_Context->drawable, |
| 261 | Draw_Context->Previous->middle->x, |
| 262 | Draw_Context->Previous->middle->y, |
| 263 | Draw_Context->Current->middle->x, |
| 264 | Draw_Context->Current->middle->y, |
| 265 | Draw_Context->gc); |
| 266 | break; |
| 267 | case UNDER: |
| 268 | drawing_draw_line( |
| 269 | NULL, Draw_Context->drawable, |
| 270 | Draw_Context->Previous->under->x, |
| 271 | Draw_Context->Previous->under->y, |
| 272 | Draw_Context->Current->under->x, |
| 273 | Draw_Context->Current->under->y, |
| 274 | Draw_Context->gc); |
| 275 | |
| 276 | break; |
| 277 | } |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | gboolean draw_arc( void *hook_data, void *call_data) |
| 283 | { |
| 284 | PropertiesArc *Properties = (PropertiesArc*)hook_data; |
| 285 | DrawContext *Draw_Context = (DrawContext*)call_data; |
| 286 | |
| 287 | gdk_gc_set_foreground(Draw_Context->gc, Properties->color); |
| 288 | |
| 289 | switch(Properties->position) { |
| 290 | case OVER: |
| 291 | gdk_draw_arc(Draw_Context->drawable, Draw_Context->gc, |
| 292 | Properties->filled, |
| 293 | Draw_Context->Current->modify_over->x, |
| 294 | Draw_Context->Current->modify_over->y, |
| 295 | Properties->size, Properties->size, 0, 360*64); |
| 296 | Draw_Context->Current->modify_over->x += Properties->size; |
| 297 | break; |
| 298 | case MIDDLE: |
| 299 | gdk_draw_arc(Draw_Context->drawable, Draw_Context->gc, |
| 300 | Properties->filled, |
| 301 | Draw_Context->Current->modify_middle->x, |
| 302 | Draw_Context->Current->modify_middle->y, |
| 303 | Properties->size, Properties->size, 0, 360*64); |
| 304 | Draw_Context->Current->modify_middle->x += Properties->size; |
| 305 | |
| 306 | break; |
| 307 | case UNDER: |
| 308 | gdk_draw_arc(Draw_Context->drawable, Draw_Context->gc, |
| 309 | Properties->filled, |
| 310 | Draw_Context->Current->modify_under->x, |
| 311 | Draw_Context->Current->modify_under->y, |
| 312 | Properties->size, Properties->size, 0, 360*64); |
| 313 | Draw_Context->Current->modify_under->x += Properties->size; |
| 314 | |
| 315 | break; |
| 316 | } |
| 317 | |
| 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | gboolean draw_bg( void *hook_data, void *call_data) |
| 323 | { |
| 324 | PropertiesBG *Properties = (PropertiesBG*)hook_data; |
| 325 | DrawContext *Draw_Context = (DrawContext*)call_data; |
| 326 | |
| 327 | gdk_gc_set_foreground(Draw_Context->gc, Properties->color); |
| 328 | |
| 329 | |
| 330 | gdk_draw_rectangle(Draw_Context->drawable, Draw_Context->gc, |
| 331 | TRUE, |
| 332 | Draw_Context->Previous->over->x, |
| 333 | Draw_Context->Previous->over->y, |
| 334 | Draw_Context->Current->over->x - Draw_Context->Previous->over->x, |
| 335 | Draw_Context->Previous->under->y); |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | |