1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Mathieu Desnoyers
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;
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.
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,
22 #include <lttv/lttv.h>
23 #include <lttv/tracecontext.h>
24 #include <lttvwindow/viewer.h>
25 #include <lttv/state.h>
26 #include <lttv/hook.h>
30 #include "cfv-private.h"
31 #include "eventhooks.h"
33 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
34 #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
36 /*****************************************************************************
38 *****************************************************************************/
41 expose_ruler( GtkWidget
*widget
, GdkEventExpose
*event
, gpointer user_data
);
44 motion_notify_ruler(GtkWidget
*widget
, GdkEventMotion
*event
, gpointer user_data
);
47 //FIXME Colors will need to be dynamic. Graphic context part not done so far.
58 /* Vector of unallocated colors */
59 static GdkColor CF_Colors
[] =
61 { 0, 0xffff, 0x0000, 0x0000 }, // RED
62 { 0, 0x0000, 0xffff, 0x0000 }, // GREEN
63 { 0, 0x0000, 0x0000, 0xffff }, // BLUE
64 { 0, 0xffff, 0xffff, 0xffff }, // WHITE
65 { 0, 0x0000, 0x0000, 0x0000 } // BLACK
69 /* Function responsible for updating the exposed area.
70 * It must call processTrace() to ask for this update.
71 * Note : this function cannot clear the background, because it may
72 * erase drawing already present (SAFETY).
74 void drawing_data_request(Drawing_t
*drawing
,
80 if(width
< 0) return ;
81 if(height
< 0) return ;
83 const TimeWindow
*time_window
= lttvwindow_get_time_window(drawing
->control_flow_data
->mw
);
85 ControlFlowData
*control_flow_data
=
86 (ControlFlowData
*)g_object_get_data(
87 G_OBJECT(drawing
->drawing_area
), "control_flow_data");
90 LttTime window_end
= ltt_time_add(time_window
->time_width
,
91 time_window
->start_time
);
93 g_debug("req : window_end : %u, %u", window_end
.tv_sec
,
96 g_debug("req : time width : %u, %u", time_window
->time_width
.tv_sec
,
97 time_window
->time_width
.tv_nsec
);
99 g_debug("x is : %i, x+width is : %i", x
, x
+width
);
101 convert_pixels_to_time(drawing
->drawing_area
->allocation
.width
, x
,
102 time_window
->start_time
,
106 convert_pixels_to_time(drawing
->drawing_area
->allocation
.width
, x
+ width
,
107 time_window
->start_time
,
111 LttvTracesetContext
* tsc
=
112 lttvwindow_get_traceset_context(control_flow_data
->mw
);
113 LttvTracesetState
* tss
=
114 (LttvTracesetState
*)tsc
;
116 // Let's call processTrace() !!
117 EventRequest event_request
; // Variable freed at the end of the function.
118 event_request
.control_flow_data
= control_flow_data
;
119 event_request
.time_begin
= start
;
120 event_request
.time_end
= end
;
121 event_request
.x_begin
= x
;
122 event_request
.x_end
= x
+width
;
124 g_debug("req : start : %u, %u", event_request
.time_begin
.tv_sec
,
125 event_request
.time_begin
.tv_nsec
);
127 g_debug("req : end : %u, %u", event_request
.time_end
.tv_sec
,
128 event_request
.time_end
.tv_nsec
);
130 LttvHooks
*event
= lttv_hooks_new();
131 LttvHooks
*after_event
= lttv_hooks_new();
132 LttvHooks
*after_traceset
= lttv_hooks_new();
133 lttv_hooks_add(after_traceset
, after_data_request
, &event_request
);
134 lttv_hooks_add(event
, draw_event_hook
, &event_request
);
135 //Modified by xiangxiu: state update hooks are added by the main window
136 //lttv_state_add_event_hooks(tsc);
137 lttv_hooks_add(after_event
, draw_after_hook
, &event_request
);
139 //lttv_process_traceset_seek_time(tsc, start);
140 lttv_state_traceset_seek_time_closest(tss
, start
);
141 // FIXME : would like to place the after_traceset hook after the traceset,
142 // but the traceset context state is not valid anymore.
143 lttv_traceset_context_add_hooks(tsc
,
144 NULL
, after_traceset
, NULL
, NULL
, NULL
, NULL
,
145 //NULL, NULL, NULL, NULL, NULL, NULL,
146 NULL
, NULL
, NULL
, event
, after_event
);
147 lttv_process_traceset(tsc
, end
, G_MAXULONG
);
148 //after_data_request((void*)&event_request,(void*)tsc);
149 lttv_traceset_context_remove_hooks(tsc
,
150 NULL
, after_traceset
, NULL
, NULL
, NULL
, NULL
,
151 // NULL, NULL, NULL, NULL, NULL, NULL,
152 NULL
, NULL
, NULL
, event
, after_event
);
153 //Modified by xiangxiu: state update hooks are removed by the main window
154 //lttv_state_remove_event_hooks(tsc);
156 lttv_hooks_destroy(after_traceset
);
157 lttv_hooks_destroy(event
);
158 lttv_hooks_destroy(after_event
);
166 /* Create a new backing pixmap of the appropriate size */
167 /* As the scaling will always change, it's of no use to copy old
171 configure_event( GtkWidget
*widget
, GdkEventConfigure
*event
,
174 Drawing_t
*drawing
= (Drawing_t
*)user_data
;
177 /* First, get the new time interval of the main window */
178 /* we assume (see documentation) that the main window
179 * has updated the time interval before this configure gets
182 //lttvwindow_get_time_window(drawing->control_flow_data->mw,
183 // &drawing->control_flow_data->time_window);
185 /* New pixmap, size of the configure event */
186 //GdkPixmap *pixmap = gdk_pixmap_new(widget->window,
187 // widget->allocation.width + SAFETY,
188 // widget->allocation.height + SAFETY,
191 g_debug("drawing configure event");
192 g_debug("New draw size : %i by %i",widget
->allocation
.width
, widget
->allocation
.height
);
196 gdk_pixmap_unref(drawing
->pixmap
);
198 /* If no old pixmap present */
199 //if(drawing->pixmap == NULL)
201 drawing
->pixmap
= gdk_pixmap_new(
203 widget
->allocation
.width
+ SAFETY
,
204 widget
->allocation
.height
+ SAFETY
,
205 //ProcessList_get_height
206 // (GuiControlFlow_get_process_list(drawing->control_flow_data)),
208 drawing
->width
= widget
->allocation
.width
;
209 drawing
->height
= widget
->allocation
.height
;
213 gdk_draw_rectangle (drawing
->pixmap
,
214 widget
->style
->black_gc
,
217 widget
->allocation
.width
+SAFETY
,
218 widget
->allocation
.height
+SAFETY
);
220 //g_info("init data request");
223 /* Initial data request */
224 // Do not need to ask for data of 1 pixel : not synchronized with
225 // main window time at this moment.
226 drawing_data_request(drawing
, &drawing
->pixmap
, 0, 0,
227 widget
->allocation
.width
,
228 widget
->allocation
.height
);
230 drawing
->width
= widget
->allocation
.width
;
231 drawing
->height
= widget
->allocation
.height
;
239 // /* Draw empty background */
240 // gdk_draw_rectangle (pixmap,
241 // widget->style->black_gc,
244 // widget->allocation.width,
245 // widget->allocation.height);
247 /* Copy old data to new pixmap */
248 gdk_draw_drawable (pixmap
,
249 widget
->style
->black_gc
,
256 gdk_pixmap_unref(drawing
->pixmap
);
258 drawing
->pixmap
= pixmap
;
260 // Clear the bottom part of the image (SAFETY)
261 gdk_draw_rectangle (pixmap
,
262 widget
->style
->black_gc
,
264 0, drawing
->height
+SAFETY
,
265 drawing
->width
+SAFETY
, // do not overlap
266 (widget
->allocation
.height
) - drawing
->height
);
268 // Clear the right part of the image (SAFETY)
269 gdk_draw_rectangle (pixmap
,
270 widget
->style
->black_gc
,
272 drawing
->width
+SAFETY
, 0,
273 (widget
->allocation
.width
) - drawing
->width
, // do not overlap
274 drawing
->height
+SAFETY
);
276 /* Clear the backgound for data request, but not SAFETY */
277 gdk_draw_rectangle (pixmap
,
278 drawing
->drawing_area
->style
->black_gc
,
280 drawing
->width
+ SAFETY
, 0,
281 widget
->allocation
.width
- drawing
->width
, // do not overlap
282 widget
->allocation
.height
+SAFETY
);
284 /* Request data for missing space */
285 g_info("missing data request");
286 drawing_data_request(drawing
, &pixmap
, drawing
->width
, 0,
287 widget
->allocation
.width
- drawing
->width
,
288 widget
->allocation
.height
);
290 drawing
->width
= widget
->allocation
.width
;
291 drawing
->height
= widget
->allocation
.height
;
298 /* Redraw the screen from the backing pixmap */
300 expose_event( GtkWidget
*widget
, GdkEventExpose
*event
, gpointer user_data
)
302 Drawing_t
*drawing
= (Drawing_t
*)user_data
;
303 const TimeWindow
*time_window
= lttvwindow_get_time_window(drawing
->control_flow_data
->mw
);
304 const LttTime
* current_time
=
305 lttvwindow_get_current_time(drawing
->control_flow_data
->mw
);
307 ControlFlowData
*control_flow_data
=
308 (ControlFlowData
*)g_object_get_data(
310 "control_flow_data");
312 g_debug("drawing expose event");
316 LttTime window_end
= ltt_time_add(time_window
->time_width
,
317 time_window
->start_time
);
319 convert_time_to_pixels(
320 time_window
->start_time
,
323 widget
->allocation
.width
,
326 gdk_draw_pixmap(widget
->window
,
327 widget
->style
->fg_gc
[GTK_WIDGET_STATE (widget
)],
329 event
->area
.x
, event
->area
.y
,
330 event
->area
.x
, event
->area
.y
,
331 event
->area
.width
, event
->area
.height
);
333 if(x
>= event
->area
.x
&& x
<= event
->area
.x
+event
->area
.width
)
335 gint8 dash_list
[] = { 1, 2 };
336 GdkGC
*gc
= gdk_gc_new(control_flow_data
->drawing
->pixmap
);
337 gdk_gc_copy(gc
, widget
->style
->white_gc
);
338 gdk_gc_set_line_attributes(gc
,
340 GDK_LINE_ON_OFF_DASH
,
343 gdk_gc_set_dashes(gc
,
347 drawing_draw_line(NULL
, widget
->window
,
349 x
, event
->area
.y
+event
->area
.height
,
358 button_press_event( GtkWidget
*widget
, GdkEventButton
*event
, gpointer user_data
)
360 ControlFlowData
*control_flow_data
=
361 (ControlFlowData
*)g_object_get_data(
363 "control_flow_data");
364 Drawing_t
*drawing
= control_flow_data
->drawing
;
365 const TimeWindow
*time_window
= lttvwindow_get_time_window(drawing
->control_flow_data
->mw
);
368 if(event
->button
== 1)
372 LttTime window_end
= ltt_time_add(time_window
->time_width
,
373 time_window
->start_time
);
376 /* left mouse button click */
377 g_debug("x click is : %f", event
->x
);
379 convert_pixels_to_time(widget
->allocation
.width
, (guint
)event
->x
,
380 time_window
->start_time
,
384 lttvwindow_report_current_time(control_flow_data
->mw
, &time
);
388 lttvwindow_report_focus(control_flow_data
->mw
, gtk_widget_get_parent(control_flow_data
->scrolled_window
));
396 Drawing_t
*drawing_construct(ControlFlowData
*control_flow_data
)
398 Drawing_t
*drawing
= g_new(Drawing_t
, 1);
400 drawing
->control_flow_data
= control_flow_data
;
402 drawing
->vbox
= gtk_vbox_new(FALSE
, 1);
403 drawing
->ruler
= gtk_drawing_area_new ();
404 gtk_widget_set_size_request(drawing
->ruler
, -1, 27);
406 drawing
->drawing_area
= gtk_drawing_area_new ();
408 gtk_box_pack_start(GTK_BOX(drawing
->vbox
), drawing
->ruler
,
410 gtk_box_pack_end(GTK_BOX(drawing
->vbox
), drawing
->drawing_area
,
413 drawing
->pango_layout
=
414 gtk_widget_create_pango_layout(drawing
->drawing_area
, NULL
);
416 //gtk_widget_set_size_request(drawing->drawing_area->window, 50, 50);
417 g_object_set_data_full(
418 G_OBJECT(drawing
->drawing_area
),
421 (GDestroyNotify
)drawing_destroy
);
424 G_OBJECT(drawing
->ruler
),
429 //gtk_widget_modify_bg( drawing->drawing_area,
431 // &CF_Colors[BLACK]);
433 //gdk_window_get_geometry(drawing->drawing_area->window,
435 // &(drawing->width),
436 // &(drawing->height),
439 //drawing->pixmap = gdk_pixmap_new(
440 // drawing->drawing_area->window,
445 drawing
->pixmap
= NULL
;
447 // drawing->pixmap = gdk_pixmap_new(drawing->drawing_area->window,
448 // drawing->drawing_area->allocation.width,
449 // drawing->drawing_area->allocation.height,
452 gtk_widget_add_events(drawing
->drawing_area
, GDK_BUTTON_PRESS_MASK
);
454 g_signal_connect (G_OBJECT(drawing
->drawing_area
),
456 G_CALLBACK (configure_event
),
459 g_signal_connect (G_OBJECT(drawing
->ruler
),
461 G_CALLBACK(expose_ruler
),
464 gtk_widget_add_events(drawing
->ruler
, GDK_POINTER_MOTION_MASK
);
466 g_signal_connect (G_OBJECT(drawing
->ruler
),
467 "motion-notify-event",
468 G_CALLBACK(motion_notify_ruler
),
472 g_signal_connect (G_OBJECT(drawing
->drawing_area
),
474 G_CALLBACK (expose_event
),
477 g_signal_connect (G_OBJECT(drawing
->drawing_area
),
478 "button-press-event",
479 G_CALLBACK (button_press_event
),
482 gtk_widget_show(drawing
->ruler
);
483 gtk_widget_show(drawing
->drawing_area
);
489 void drawing_destroy(Drawing_t
*drawing
)
492 // Do not unref here, Drawing_t destroyed by it's widget.
493 //g_object_unref( G_OBJECT(drawing->drawing_area));
495 g_free(drawing
->pango_layout
);
499 GtkWidget
*drawing_get_drawing_area(Drawing_t
*drawing
)
501 return drawing
->drawing_area
;
504 GtkWidget
*drawing_get_widget(Drawing_t
*drawing
)
506 return drawing
->vbox
;
509 /* convert_pixels_to_time
511 * Convert from window pixel and time interval to an absolute time.
513 void convert_pixels_to_time(
516 LttTime window_time_begin
,
517 LttTime window_time_end
,
520 LttTime window_time_interval
;
522 window_time_interval
= ltt_time_sub(window_time_end
,
524 *time
= ltt_time_mul(window_time_interval
, (x
/(float)width
));
525 *time
= ltt_time_add(window_time_begin
, *time
);
530 void convert_time_to_pixels(
531 LttTime window_time_begin
,
532 LttTime window_time_end
,
537 LttTime window_time_interval
;
538 float interval_float
, time_float
;
540 window_time_interval
= ltt_time_sub(window_time_end
,window_time_begin
);
542 time
= ltt_time_sub(time
, window_time_begin
);
544 interval_float
= ltt_time_to_double(window_time_interval
);
545 time_float
= ltt_time_to_double(time
);
547 *x
= (guint
)(time_float
/interval_float
* width
);
551 void drawing_refresh ( Drawing_t
*drawing
,
553 guint width
, guint height
)
555 g_info("Drawing.c : drawing_refresh %u, %u, %u, %u", x
, y
, width
, height
);
556 GdkRectangle update_rect
;
559 drawing
->drawing_area
->window
,
560 drawing
->drawing_area
->
561 style
->fg_gc
[GTK_WIDGET_STATE (drawing
->drawing_area
)],
562 GDK_DRAWABLE(drawing
->pixmap
),
569 update_rect
.width
= drawing
->width
;
570 update_rect
.height
= drawing
->height
;
571 gtk_widget_draw( drawing
->drawing_area
, &update_rect
);
576 void drawing_draw_line( Drawing_t
*drawing
,
582 gdk_draw_line (pixmap
,
590 void drawing_resize(Drawing_t
*drawing
, guint h
, guint w
)
592 drawing
->height
= h
;
595 gtk_widget_set_size_request ( drawing
->drawing_area
,
603 /* Insert a square corresponding to a new process in the list */
604 /* Applies to whole drawing->width */
605 void drawing_insert_square(Drawing_t
*drawing
,
609 //GdkRectangle update_rect;
611 /* Allocate a new pixmap with new height */
612 GdkPixmap
*pixmap
= gdk_pixmap_new(drawing
->drawing_area
->window
,
613 drawing
->width
+ SAFETY
,
614 drawing
->height
+ height
+ SAFETY
,
617 /* Copy the high region */
618 gdk_draw_drawable (pixmap
,
619 drawing
->drawing_area
->style
->black_gc
,
623 drawing
->width
+ SAFETY
, y
);
628 /* add an empty square */
629 gdk_draw_rectangle (pixmap
,
630 drawing
->drawing_area
->style
->black_gc
,
633 drawing
->width
+ SAFETY
, // do not overlap
638 /* copy the bottom of the region */
639 gdk_draw_drawable (pixmap
,
640 drawing
->drawing_area
->style
->black_gc
,
644 drawing
->width
+SAFETY
, drawing
->height
- y
+ SAFETY
);
650 gdk_pixmap_unref(drawing
->pixmap
);
652 drawing
->pixmap
= pixmap
;
654 drawing
->height
+=height
;
656 /* Rectangle to update, from new drawing dimensions */
657 //update_rect.x = 0 ;
658 //update_rect.y = y ;
659 //update_rect.width = drawing->width;
660 //update_rect.height = drawing->height - y ;
661 //gtk_widget_draw( drawing->drawing_area, &update_rect);
665 /* Remove a square corresponding to a removed process in the list */
666 void drawing_remove_square(Drawing_t
*drawing
,
670 //GdkRectangle update_rect;
672 /* Allocate a new pixmap with new height */
673 GdkPixmap
*pixmap
= gdk_pixmap_new(
674 drawing
->drawing_area
->window
,
675 drawing
->width
+ SAFETY
,
676 drawing
->height
- height
+ SAFETY
,
679 /* Copy the high region */
680 gdk_draw_drawable (pixmap
,
681 drawing
->drawing_area
->style
->black_gc
,
685 drawing
->width
+ SAFETY
, y
);
689 /* Copy up the bottom of the region */
690 gdk_draw_drawable (pixmap
,
691 drawing
->drawing_area
->style
->black_gc
,
695 drawing
->width
, drawing
->height
- y
- height
+ SAFETY
);
699 gdk_pixmap_unref(drawing
->pixmap
);
701 drawing
->pixmap
= pixmap
;
703 drawing
->height
-=height
;
705 /* Rectangle to update, from new drawing dimensions */
706 //update_rect.x = 0 ;
707 //update_rect.y = y ;
708 //update_rect.width = drawing->width;
709 //update_rect.height = drawing->height - y ;
710 //gtk_widget_draw( drawing->drawing_area, &update_rect);
713 void drawing_update_ruler(Drawing_t
*drawing
, TimeWindow
*time_window
)
718 req
.width
= drawing
->ruler
->allocation
.width
;
719 req
.height
= drawing
->ruler
->allocation
.height
;
724 rect
.width
= req
.width
;
725 rect
.height
= req
.height
;
727 gtk_widget_queue_draw(drawing
->ruler
);
728 //gtk_widget_draw( drawing->ruler, &rect);
731 /* Redraw the ruler */
733 expose_ruler( GtkWidget
*widget
, GdkEventExpose
*event
, gpointer user_data
)
735 Drawing_t
*drawing
= (Drawing_t
*)user_data
;
736 const TimeWindow
*time_window
= lttvwindow_get_time_window(drawing
->control_flow_data
->mw
);
739 PangoContext
*context
;
741 PangoAttribute
*attribute
;
742 PangoFontDescription
*FontDesc
;
744 PangoRectangle ink_rect
;
745 guint global_width
=0;
746 GdkColor foreground
= { 0, 0, 0, 0 };
747 GdkColor background
= { 0, 0xffff, 0xffff, 0xffff };
750 ltt_time_add(time_window
->time_width
,
751 time_window
->start_time
);
753 ltt_time_div(time_window
->time_width
,2.0);
754 LttTime window_middle
=
755 ltt_time_add(half_width
,
756 time_window
->start_time
);
757 g_debug("ruler expose event");
759 gdk_draw_rectangle (drawing
->ruler
->window
,
760 drawing
->ruler
->style
->white_gc
,
762 event
->area
.x
, event
->area
.y
,
766 GdkGC
*gc
= gdk_gc_new(drawing
->ruler
->window
);
767 gdk_gc_copy(gc
, drawing
->ruler
->style
->black_gc
);
768 gdk_gc_set_line_attributes(gc
,
773 gdk_draw_line (drawing
->ruler
->window
,
776 event
->area
.x
+ event
->area
.width
, 1);
779 snprintf(text
, 255, "%lus\n%luns",
780 time_window
->start_time
.tv_sec
,
781 time_window
->start_time
.tv_nsec
);
783 layout
= gtk_widget_create_pango_layout(drawing
->drawing_area
, NULL
);
785 context
= pango_layout_get_context(layout
);
786 FontDesc
= pango_context_get_font_description(context
);
788 pango_font_description_set_size(FontDesc
, 6*PANGO_SCALE
);
789 pango_layout_context_changed(layout
);
791 pango_layout_set_text(layout
, text
, -1);
792 pango_layout_get_pixel_extents(layout
, &ink_rect
, NULL
);
793 global_width
+= ink_rect
.width
;
795 gdk_draw_layout_with_colors(drawing
->ruler
->window
,
799 layout
, &foreground
, &background
);
801 gdk_gc_set_line_attributes(gc
,
807 gdk_draw_line (drawing
->ruler
->window
,
813 snprintf(text
, 255, "%lus\n%luns", window_end
.tv_sec
,
816 pango_layout_set_text(layout
, text
, -1);
817 pango_layout_get_pixel_extents(layout
, &ink_rect
, NULL
);
818 global_width
+= ink_rect
.width
;
820 if(global_width
<= drawing
->ruler
->allocation
.width
)
822 gdk_draw_layout_with_colors(drawing
->ruler
->window
,
824 drawing
->ruler
->allocation
.width
- ink_rect
.width
,
826 layout
, &foreground
, &background
);
828 gdk_gc_set_line_attributes(gc
,
834 gdk_draw_line (drawing
->ruler
->window
,
836 drawing
->ruler
->allocation
.width
-1, 1,
837 drawing
->ruler
->allocation
.width
-1, 7);
841 snprintf(text
, 255, "%lus\n%luns", window_middle
.tv_sec
,
842 window_middle
.tv_nsec
);
844 pango_layout_set_text(layout
, text
, -1);
845 pango_layout_get_pixel_extents(layout
, &ink_rect
, NULL
);
846 global_width
+= ink_rect
.width
;
848 if(global_width
<= drawing
->ruler
->allocation
.width
)
850 gdk_draw_layout_with_colors(drawing
->ruler
->window
,
852 (drawing
->ruler
->allocation
.width
- ink_rect
.width
)/2,
854 layout
, &foreground
, &background
);
856 gdk_gc_set_line_attributes(gc
,
862 gdk_draw_line (drawing
->ruler
->window
,
864 drawing
->ruler
->allocation
.width
/2, 1,
865 drawing
->ruler
->allocation
.width
/2, 7);
873 g_object_unref(layout
);
879 /* notify mouse on ruler */
881 motion_notify_ruler(GtkWidget
*widget
, GdkEventMotion
*event
, gpointer user_data
)
884 //eventually follow mouse and show time here