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 ;
82 ControlFlowData
*control_flow_data
=
83 (ControlFlowData
*)g_object_get_data(
85 drawing
->drawing_area
),
89 LttTime window_end
= ltt_time_add(control_flow_data
->time_window
.time_width
,
90 control_flow_data
->time_window
.start_time
);
92 g_debug("req : window_end : %u, %u", window_end
.tv_sec
,
95 g_debug("req : time width : %u, %u", control_flow_data
->time_window
.time_width
.tv_sec
,
96 control_flow_data
->time_window
.time_width
.tv_nsec
);
98 g_debug("x is : %i, x+width is : %i", x
, x
+width
);
100 convert_pixels_to_time(drawing
->drawing_area
->allocation
.width
, x
,
101 &control_flow_data
->time_window
.start_time
,
105 convert_pixels_to_time(drawing
->drawing_area
->allocation
.width
, x
+ width
,
106 &control_flow_data
->time_window
.start_time
,
110 LttvTracesetContext
* tsc
=
111 get_traceset_context(control_flow_data
->mw
);
112 LttvTracesetState
* tss
=
113 (LttvTracesetState
*)tsc
;
116 //guicontrolflow_get_process_list(drawing->control_flow_data),
119 //guicontrolflow_get_process_list(drawing->control_flow_data),
120 //drawing, *pixmap, x, y, width, height);
122 // Let's call processTrace() !!
123 EventRequest event_request
; // Variable freed at the end of the function.
124 event_request
.control_flow_data
= control_flow_data
;
125 event_request
.time_begin
= start
;
126 event_request
.time_end
= end
;
127 event_request
.x_begin
= x
;
128 event_request
.x_end
= x
+width
;
130 g_debug("req : start : %u, %u", event_request
.time_begin
.tv_sec
,
131 event_request
.time_begin
.tv_nsec
);
133 g_debug("req : end : %u, %u", event_request
.time_end
.tv_sec
,
134 event_request
.time_end
.tv_nsec
);
136 LttvHooks
*event
= lttv_hooks_new();
137 LttvHooks
*after_event
= lttv_hooks_new();
138 LttvHooks
*after_traceset
= lttv_hooks_new();
139 lttv_hooks_add(after_traceset
, after_data_request
, &event_request
);
140 lttv_hooks_add(event
, draw_event_hook
, &event_request
);
141 //Modified by xiangxiu: state update hooks are added by the main window
142 //lttv_state_add_event_hooks(tsc);
143 lttv_hooks_add(after_event
, draw_after_hook
, &event_request
);
145 //lttv_process_traceset_seek_time(tsc, start);
146 lttv_state_traceset_seek_time_closest(tss
, start
);
147 // FIXME : would like to place the after_traceset hook after the traceset,
148 // but the traceset context state is not valid anymore.
149 lttv_traceset_context_add_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 lttv_process_traceset(tsc
, end
, G_MAXULONG
);
154 //after_data_request((void*)&event_request,(void*)tsc);
155 lttv_traceset_context_remove_hooks(tsc
,
156 NULL
, after_traceset
, NULL
, NULL
, NULL
, NULL
,
157 // NULL, NULL, NULL, NULL, NULL, NULL,
158 NULL
, NULL
, NULL
, event
, after_event
);
159 //Modified by xiangxiu: state update hooks are removed by the main window
160 //lttv_state_remove_event_hooks(tsc);
162 lttv_hooks_destroy(after_traceset
);
163 lttv_hooks_destroy(event
);
164 lttv_hooks_destroy(after_event
);
172 /* Create a new backing pixmap of the appropriate size */
173 /* As the scaling will always change, it's of no use to copy old
177 configure_event( GtkWidget
*widget
, GdkEventConfigure
*event
,
180 Drawing_t
*drawing
= (Drawing_t
*)user_data
;
183 /* First, get the new time interval of the main window */
184 /* we assume (see documentation) that the main window
185 * has updated the time interval before this configure gets
188 get_time_window(drawing
->control_flow_data
->mw
,
189 &drawing
->control_flow_data
->time_window
);
191 /* New pixmap, size of the configure event */
192 //GdkPixmap *pixmap = gdk_pixmap_new(widget->window,
193 // widget->allocation.width + SAFETY,
194 // widget->allocation.height + SAFETY,
197 g_debug("drawing configure event");
198 g_debug("New draw size : %i by %i",widget
->allocation
.width
, widget
->allocation
.height
);
202 gdk_pixmap_unref(drawing
->pixmap
);
204 /* If no old pixmap present */
205 //if(drawing->pixmap == NULL)
207 drawing
->pixmap
= gdk_pixmap_new(
209 widget
->allocation
.width
+ SAFETY
,
210 widget
->allocation
.height
+ SAFETY
,
211 //ProcessList_get_height
212 // (GuiControlFlow_get_process_list(drawing->control_flow_data)),
214 drawing
->width
= widget
->allocation
.width
;
215 drawing
->height
= widget
->allocation
.height
;
219 gdk_draw_rectangle (drawing
->pixmap
,
220 widget
->style
->black_gc
,
223 widget
->allocation
.width
+SAFETY
,
224 widget
->allocation
.height
+SAFETY
);
226 //g_info("init data request");
229 /* Initial data request */
230 // Do not need to ask for data of 1 pixel : not synchronized with
231 // main window time at this moment.
232 drawing_data_request(drawing
, &drawing
->pixmap
, 0, 0,
233 widget
->allocation
.width
,
234 widget
->allocation
.height
);
236 drawing
->width
= widget
->allocation
.width
;
237 drawing
->height
= widget
->allocation
.height
;
245 // /* Draw empty background */
246 // gdk_draw_rectangle (pixmap,
247 // widget->style->black_gc,
250 // widget->allocation.width,
251 // widget->allocation.height);
253 /* Copy old data to new pixmap */
254 gdk_draw_drawable (pixmap
,
255 widget
->style
->black_gc
,
262 gdk_pixmap_unref(drawing
->pixmap
);
264 drawing
->pixmap
= pixmap
;
266 // Clear the bottom part of the image (SAFETY)
267 gdk_draw_rectangle (pixmap
,
268 widget
->style
->black_gc
,
270 0, drawing
->height
+SAFETY
,
271 drawing
->width
+SAFETY
, // do not overlap
272 (widget
->allocation
.height
) - drawing
->height
);
274 // Clear the right part of the image (SAFETY)
275 gdk_draw_rectangle (pixmap
,
276 widget
->style
->black_gc
,
278 drawing
->width
+SAFETY
, 0,
279 (widget
->allocation
.width
) - drawing
->width
, // do not overlap
280 drawing
->height
+SAFETY
);
282 /* Clear the backgound for data request, but not SAFETY */
283 gdk_draw_rectangle (pixmap
,
284 drawing
->drawing_area
->style
->black_gc
,
286 drawing
->width
+ SAFETY
, 0,
287 widget
->allocation
.width
- drawing
->width
, // do not overlap
288 widget
->allocation
.height
+SAFETY
);
290 /* Request data for missing space */
291 g_info("missing data request");
292 drawing_data_request(drawing
, &pixmap
, drawing
->width
, 0,
293 widget
->allocation
.width
- drawing
->width
,
294 widget
->allocation
.height
);
296 drawing
->width
= widget
->allocation
.width
;
297 drawing
->height
= widget
->allocation
.height
;
304 /* Redraw the screen from the backing pixmap */
306 expose_event( GtkWidget
*widget
, GdkEventExpose
*event
, gpointer user_data
)
308 Drawing_t
*drawing
= (Drawing_t
*)user_data
;
309 ControlFlowData
*control_flow_data
=
310 (ControlFlowData
*)g_object_get_data(
312 "control_flow_data");
314 g_debug("drawing expose event");
317 LttTime
* current_time
=
318 guicontrolflow_get_current_time(control_flow_data
);
320 LttTime window_end
= ltt_time_add(control_flow_data
->time_window
.time_width
,
321 control_flow_data
->time_window
.start_time
);
323 convert_time_to_pixels(
324 control_flow_data
->time_window
.start_time
,
327 widget
->allocation
.width
,
330 gdk_draw_pixmap(widget
->window
,
331 widget
->style
->fg_gc
[GTK_WIDGET_STATE (widget
)],
333 event
->area
.x
, event
->area
.y
,
334 event
->area
.x
, event
->area
.y
,
335 event
->area
.width
, event
->area
.height
);
337 if(x
>= event
->area
.x
&& x
<= event
->area
.x
+event
->area
.width
)
339 gint8 dash_list
[] = { 1, 2 };
340 GdkGC
*gc
= gdk_gc_new(control_flow_data
->drawing
->pixmap
);
341 gdk_gc_copy(gc
, widget
->style
->white_gc
);
342 gdk_gc_set_line_attributes(gc
,
344 GDK_LINE_ON_OFF_DASH
,
347 gdk_gc_set_dashes(gc
,
351 drawing_draw_line(NULL
, widget
->window
,
353 x
, event
->area
.y
+event
->area
.height
,
362 button_press_event( GtkWidget
*widget
, GdkEventButton
*event
, gpointer user_data
)
364 ControlFlowData
*control_flow_data
=
365 (ControlFlowData
*)g_object_get_data(
367 "control_flow_data");
368 Drawing_t
*drawing
= control_flow_data
->drawing
;
372 if(event
->button
== 1)
376 LttTime window_end
= ltt_time_add(control_flow_data
->time_window
.time_width
,
377 control_flow_data
->time_window
.start_time
);
380 /* left mouse button click */
381 g_debug("x click is : %f", event
->x
);
383 convert_pixels_to_time(widget
->allocation
.width
, (guint
)event
->x
,
384 &control_flow_data
->time_window
.start_time
,
388 set_current_time(control_flow_data
->mw
, &time
);
392 set_focused_pane(control_flow_data
->mw
, gtk_widget_get_parent(control_flow_data
->scrolled_window
));
400 Drawing_t
*drawing_construct(ControlFlowData
*control_flow_data
)
402 Drawing_t
*drawing
= g_new(Drawing_t
, 1);
404 drawing
->control_flow_data
= control_flow_data
;
406 drawing
->vbox
= gtk_vbox_new(FALSE
, 1);
407 drawing
->ruler
= gtk_drawing_area_new ();
408 gtk_widget_set_size_request(drawing
->ruler
, -1, 27);
410 drawing
->drawing_area
= gtk_drawing_area_new ();
412 gtk_box_pack_start(GTK_BOX(drawing
->vbox
), drawing
->ruler
,
414 gtk_box_pack_end(GTK_BOX(drawing
->vbox
), drawing
->drawing_area
,
417 drawing
->pango_layout
=
418 gtk_widget_create_pango_layout(drawing
->drawing_area
, NULL
);
420 //gtk_widget_set_size_request(drawing->drawing_area->window, 50, 50);
421 g_object_set_data_full(
422 G_OBJECT(drawing
->drawing_area
),
425 (GDestroyNotify
)drawing_destroy
);
428 G_OBJECT(drawing
->ruler
),
433 //gtk_widget_modify_bg( drawing->drawing_area,
435 // &CF_Colors[BLACK]);
437 //gdk_window_get_geometry(drawing->drawing_area->window,
439 // &(drawing->width),
440 // &(drawing->height),
443 //drawing->pixmap = gdk_pixmap_new(
444 // drawing->drawing_area->window,
449 drawing
->pixmap
= NULL
;
451 // drawing->pixmap = gdk_pixmap_new(drawing->drawing_area->window,
452 // drawing->drawing_area->allocation.width,
453 // drawing->drawing_area->allocation.height,
456 gtk_widget_add_events(drawing
->drawing_area
, GDK_BUTTON_PRESS_MASK
);
458 g_signal_connect (G_OBJECT(drawing
->drawing_area
),
460 G_CALLBACK (configure_event
),
463 g_signal_connect (G_OBJECT(drawing
->ruler
),
465 G_CALLBACK(expose_ruler
),
468 gtk_widget_add_events(drawing
->ruler
, GDK_POINTER_MOTION_MASK
);
470 g_signal_connect (G_OBJECT(drawing
->ruler
),
471 "motion-notify-event",
472 G_CALLBACK(motion_notify_ruler
),
476 g_signal_connect (G_OBJECT(drawing
->drawing_area
),
478 G_CALLBACK (expose_event
),
481 g_signal_connect (G_OBJECT(drawing
->drawing_area
),
482 "button-press-event",
483 G_CALLBACK (button_press_event
),
486 gtk_widget_show(drawing
->ruler
);
487 gtk_widget_show(drawing
->drawing_area
);
493 void drawing_destroy(Drawing_t
*drawing
)
496 // Do not unref here, Drawing_t destroyed by it's widget.
497 //g_object_unref( G_OBJECT(drawing->drawing_area));
499 g_free(drawing
->pango_layout
);
503 GtkWidget
*drawing_get_drawing_area(Drawing_t
*drawing
)
505 return drawing
->drawing_area
;
508 GtkWidget
*drawing_get_widget(Drawing_t
*drawing
)
510 return drawing
->vbox
;
513 /* convert_pixels_to_time
515 * Convert from window pixel and time interval to an absolute time.
517 void convert_pixels_to_time(
520 LttTime
*window_time_begin
,
521 LttTime
*window_time_end
,
524 LttTime window_time_interval
;
526 window_time_interval
= ltt_time_sub(*window_time_end
,
528 *time
= ltt_time_mul(window_time_interval
, (x
/(float)width
));
529 *time
= ltt_time_add(*window_time_begin
, *time
);
534 void convert_time_to_pixels(
535 LttTime window_time_begin
,
536 LttTime window_time_end
,
541 LttTime window_time_interval
;
542 float interval_float
, time_float
;
544 window_time_interval
= ltt_time_sub(window_time_end
,window_time_begin
);
546 time
= ltt_time_sub(time
, window_time_begin
);
548 interval_float
= ltt_time_to_double(window_time_interval
);
549 time_float
= ltt_time_to_double(time
);
551 *x
= (guint
)(time_float
/interval_float
* width
);
555 void drawing_refresh ( Drawing_t
*drawing
,
557 guint width
, guint height
)
559 g_info("Drawing.c : drawing_refresh %u, %u, %u, %u", x
, y
, width
, height
);
560 GdkRectangle update_rect
;
563 drawing
->drawing_area
->window
,
564 drawing
->drawing_area
->
565 style
->fg_gc
[GTK_WIDGET_STATE (drawing
->drawing_area
)],
566 GDK_DRAWABLE(drawing
->pixmap
),
573 update_rect
.width
= drawing
->width
;
574 update_rect
.height
= drawing
->height
;
575 gtk_widget_draw( drawing
->drawing_area
, &update_rect
);
580 void drawing_draw_line( Drawing_t
*drawing
,
586 gdk_draw_line (pixmap
,
594 void drawing_resize(Drawing_t
*drawing
, guint h
, guint w
)
596 drawing
->height
= h
;
599 gtk_widget_set_size_request ( drawing
->drawing_area
,
607 /* Insert a square corresponding to a new process in the list */
608 /* Applies to whole drawing->width */
609 void drawing_insert_square(Drawing_t
*drawing
,
613 //GdkRectangle update_rect;
615 /* Allocate a new pixmap with new height */
616 GdkPixmap
*pixmap
= gdk_pixmap_new(drawing
->drawing_area
->window
,
617 drawing
->width
+ SAFETY
,
618 drawing
->height
+ height
+ SAFETY
,
621 /* Copy the high region */
622 gdk_draw_drawable (pixmap
,
623 drawing
->drawing_area
->style
->black_gc
,
627 drawing
->width
+ SAFETY
, y
);
632 /* add an empty square */
633 gdk_draw_rectangle (pixmap
,
634 drawing
->drawing_area
->style
->black_gc
,
637 drawing
->width
+ SAFETY
, // do not overlap
642 /* copy the bottom of the region */
643 gdk_draw_drawable (pixmap
,
644 drawing
->drawing_area
->style
->black_gc
,
648 drawing
->width
+SAFETY
, drawing
->height
- y
+ SAFETY
);
654 gdk_pixmap_unref(drawing
->pixmap
);
656 drawing
->pixmap
= pixmap
;
658 drawing
->height
+=height
;
660 /* Rectangle to update, from new drawing dimensions */
661 //update_rect.x = 0 ;
662 //update_rect.y = y ;
663 //update_rect.width = drawing->width;
664 //update_rect.height = drawing->height - y ;
665 //gtk_widget_draw( drawing->drawing_area, &update_rect);
669 /* Remove a square corresponding to a removed process in the list */
670 void drawing_remove_square(Drawing_t
*drawing
,
674 //GdkRectangle update_rect;
676 /* Allocate a new pixmap with new height */
677 GdkPixmap
*pixmap
= gdk_pixmap_new(
678 drawing
->drawing_area
->window
,
679 drawing
->width
+ SAFETY
,
680 drawing
->height
- height
+ SAFETY
,
683 /* Copy the high region */
684 gdk_draw_drawable (pixmap
,
685 drawing
->drawing_area
->style
->black_gc
,
689 drawing
->width
+ SAFETY
, y
);
693 /* Copy up the bottom of the region */
694 gdk_draw_drawable (pixmap
,
695 drawing
->drawing_area
->style
->black_gc
,
699 drawing
->width
, drawing
->height
- y
- height
+ SAFETY
);
703 gdk_pixmap_unref(drawing
->pixmap
);
705 drawing
->pixmap
= pixmap
;
707 drawing
->height
-=height
;
709 /* Rectangle to update, from new drawing dimensions */
710 //update_rect.x = 0 ;
711 //update_rect.y = y ;
712 //update_rect.width = drawing->width;
713 //update_rect.height = drawing->height - y ;
714 //gtk_widget_draw( drawing->drawing_area, &update_rect);
717 void drawing_update_ruler(Drawing_t
*drawing
, TimeWindow
*time_window
)
722 req
.width
= drawing
->ruler
->allocation
.width
;
723 req
.height
= drawing
->ruler
->allocation
.height
;
728 rect
.width
= req
.width
;
729 rect
.height
= req
.height
;
731 gtk_widget_queue_draw(drawing
->ruler
);
732 //gtk_widget_draw( drawing->ruler, &rect);
735 /* Redraw the ruler */
737 expose_ruler( GtkWidget
*widget
, GdkEventExpose
*event
, gpointer user_data
)
739 Drawing_t
*drawing
= (Drawing_t
*)user_data
;
743 PangoContext
*context
;
745 PangoAttribute
*attribute
;
746 PangoFontDescription
*FontDesc
;
748 PangoRectangle ink_rect
;
749 guint global_width
=0;
750 GdkColor foreground
= { 0, 0, 0, 0 };
751 GdkColor background
= { 0, 0xffff, 0xffff, 0xffff };
754 ltt_time_add(drawing
->control_flow_data
->time_window
.time_width
,
755 drawing
->control_flow_data
->time_window
.start_time
);
757 ltt_time_div(drawing
->control_flow_data
->time_window
.time_width
,2.0);
758 LttTime window_middle
=
759 ltt_time_add(half_width
,
760 drawing
->control_flow_data
->time_window
.start_time
);
761 g_debug("ruler expose event");
763 gdk_draw_rectangle (drawing
->ruler
->window
,
764 drawing
->ruler
->style
->white_gc
,
766 event
->area
.x
, event
->area
.y
,
770 GdkGC
*gc
= gdk_gc_new(drawing
->ruler
->window
);
771 gdk_gc_copy(gc
, drawing
->ruler
->style
->black_gc
);
772 gdk_gc_set_line_attributes(gc
,
777 gdk_draw_line (drawing
->ruler
->window
,
780 event
->area
.x
+ event
->area
.width
, 1);
783 snprintf(text
, 255, "%lus\n%luns",
784 drawing
->control_flow_data
->time_window
.start_time
.tv_sec
,
785 drawing
->control_flow_data
->time_window
.start_time
.tv_nsec
);
787 layout
= gtk_widget_create_pango_layout(drawing
->drawing_area
, NULL
);
789 context
= pango_layout_get_context(layout
);
790 FontDesc
= pango_context_get_font_description(context
);
792 pango_font_description_set_size(FontDesc
, 6*PANGO_SCALE
);
793 pango_layout_context_changed(layout
);
795 pango_layout_set_text(layout
, text
, -1);
796 pango_layout_get_pixel_extents(layout
, &ink_rect
, NULL
);
797 global_width
+= ink_rect
.width
;
799 gdk_draw_layout_with_colors(drawing
->ruler
->window
,
803 layout
, &foreground
, &background
);
805 gdk_gc_set_line_attributes(gc
,
811 gdk_draw_line (drawing
->ruler
->window
,
817 snprintf(text
, 255, "%lus\n%luns", window_end
.tv_sec
,
820 pango_layout_set_text(layout
, text
, -1);
821 pango_layout_get_pixel_extents(layout
, &ink_rect
, NULL
);
822 global_width
+= ink_rect
.width
;
824 if(global_width
<= drawing
->ruler
->allocation
.width
)
826 gdk_draw_layout_with_colors(drawing
->ruler
->window
,
828 drawing
->ruler
->allocation
.width
- ink_rect
.width
,
830 layout
, &foreground
, &background
);
832 gdk_gc_set_line_attributes(gc
,
838 gdk_draw_line (drawing
->ruler
->window
,
840 drawing
->ruler
->allocation
.width
-1, 1,
841 drawing
->ruler
->allocation
.width
-1, 7);
845 snprintf(text
, 255, "%lus\n%luns", window_middle
.tv_sec
,
846 window_middle
.tv_nsec
);
848 pango_layout_set_text(layout
, text
, -1);
849 pango_layout_get_pixel_extents(layout
, &ink_rect
, NULL
);
850 global_width
+= ink_rect
.width
;
852 if(global_width
<= drawing
->ruler
->allocation
.width
)
854 gdk_draw_layout_with_colors(drawing
->ruler
->window
,
856 (drawing
->ruler
->allocation
.width
- ink_rect
.width
)/2,
858 layout
, &foreground
, &background
);
860 gdk_gc_set_line_attributes(gc
,
866 gdk_draw_line (drawing
->ruler
->window
,
868 drawing
->ruler
->allocation
.width
/2, 1,
869 drawing
->ruler
->allocation
.width
/2, 7);
877 g_object_unref(layout
);
883 /* notify mouse on ruler */
885 motion_notify_ruler(GtkWidget
*widget
, GdkEventMotion
*event
, gpointer user_data
)
888 //eventually follow mouse and show time here