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>
29 #include "eventhooks.h"
31 #include "cfv-private.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
= drawing
->control_flow_data
;
86 // (ControlFlowData*)g_object_get_data(
87 // G_OBJECT(drawing->drawing_area), "control_flow_data");
89 LttTime start
, time_end
;
90 LttTime window_end
= ltt_time_add(time_window
->time_width
,
91 time_window
->start_time
);
93 g_debug("req : window start_time : %u, %u", time_window
->start_time
.tv_sec
,
94 time_window
->start_time
.tv_nsec
);
96 g_debug("req : window time width : %u, %u", time_window
->time_width
.tv_sec
,
97 time_window
->time_width
.tv_nsec
);
99 g_debug("req : window_end : %u, %u", window_end
.tv_sec
,
102 g_debug("x is : %i, x+width is : %i", x
, x
+width
);
104 convert_pixels_to_time(drawing
->drawing_area
->allocation
.width
, x
,
105 time_window
->start_time
,
109 convert_pixels_to_time(drawing
->drawing_area
->allocation
.width
, x
+width
,
110 time_window
->start_time
,
114 LttvTracesetContext
* tsc
=
115 lttvwindow_get_traceset_context(control_flow_data
->mw
);
116 LttvTracesetState
* tss
=
117 (LttvTracesetState
*)tsc
;
119 // Let's call processTrace() !!
121 EventRequest
*event_request
= control_flow_data
->event_request
;
122 event_request
->control_flow_data
= control_flow_data
;
123 event_request
->time_begin
= start
;
124 event_request
->time_end
= time_end
;
126 event_request
->x_begin
= x
;
127 event_request
->x_end
= x
+width
;
129 g_debug("req : start : %u, %u", event_request
->time_begin
.tv_sec
,
130 event_request
->time_begin
.tv_nsec
);
132 g_debug("req : end : %u, %u", event_request
->time_end
.tv_sec
,
133 event_request
->time_end
.tv_nsec
);
135 lttv_hooks_add(control_flow_data
->after_traceset
, after_data_request
, event_request
);
136 lttv_hooks_add(control_flow_data
->event
, draw_event_hook
, event_request
);
137 lttv_hooks_add(control_flow_data
->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
, control_flow_data
->after_traceset
, NULL
, NULL
, NULL
, NULL
,
145 //NULL, NULL, NULL, NULL, NULL, NULL,
146 NULL
, NULL
, NULL
, control_flow_data
->event
, control_flow_data
->after_event
);
147 TimeWindow time_request
;
148 time_request
.start_time
= start
;
149 time_request
.time_width
= ltt_time_sub(time_end
, start
);
151 lttvwindow_time_interval_request(drawing
->control_flow_data
->mw
,
152 time_request
, G_MAXUINT
,
153 after_process_traceset_hook
,
156 //lttv_process_traceset(tsc, end, G_MAXULONG);
162 void drawing_data_request_end(Drawing_t
*drawing
,
163 TimeWindow req_time_window
)
165 gint x
, x_end
, width
;
167 LttvTracesetContext
* tsc
=
168 lttvwindow_get_traceset_context(drawing
->control_flow_data
->mw
);
170 const TimeWindow
*time_window
= lttvwindow_get_time_window(drawing
->control_flow_data
->mw
);
172 //FIXME ? removing hooks during processtrace can BREAK things!
173 lttv_traceset_context_remove_hooks(tsc
,
174 NULL
, drawing
->control_flow_data
->after_traceset
, NULL
, NULL
, NULL
, NULL
,
176 drawing
->control_flow_data
->event
,
177 drawing
->control_flow_data
->after_event
);
179 g_debug("End of data request");
181 LttTime window_end
= ltt_time_add(time_window
->time_width
,
182 time_window
->start_time
);
184 LttTime req_window_end
= ltt_time_add(req_time_window
.time_width
,
185 req_time_window
.start_time
);
187 convert_time_to_pixels(
188 time_window
->start_time
,
190 req_time_window
.start_time
,
194 convert_time_to_pixels(
195 time_window
->start_time
,
203 /* ask for the buffer to be redrawn */
204 gtk_widget_queue_draw_area ( drawing
->drawing_area
,
206 width
, drawing
->height
);
214 /* Create a new backing pixmap of the appropriate size */
215 /* As the scaling will always change, it's of no use to copy old
219 configure_event( GtkWidget
*widget
, GdkEventConfigure
*event
,
222 Drawing_t
*drawing
= (Drawing_t
*)user_data
;
225 /* First, get the new time interval of the main window */
226 /* we assume (see documentation) that the main window
227 * has updated the time interval before this configure gets
230 //lttvwindow_get_time_window(drawing->control_flow_data->mw,
231 // &drawing->control_flow_data->time_window);
233 /* New pixmap, size of the configure event */
234 //GdkPixmap *pixmap = gdk_pixmap_new(widget->window,
235 // widget->allocation.width + SAFETY,
236 // widget->allocation.height + SAFETY,
239 g_debug("drawing configure event");
240 g_debug("New draw size : %i by %i",widget
->allocation
.width
, widget
->allocation
.height
);
244 gdk_pixmap_unref(drawing
->pixmap
);
246 /* If no old pixmap present */
247 //if(drawing->pixmap == NULL)
249 drawing
->pixmap
= gdk_pixmap_new(
251 widget
->allocation
.width
+ SAFETY
,
252 widget
->allocation
.height
+ SAFETY
,
253 //ProcessList_get_height
254 // (GuiControlFlow_get_process_list(drawing->control_flow_data)),
256 drawing
->width
= widget
->allocation
.width
;
257 drawing
->height
= widget
->allocation
.height
;
261 gdk_draw_rectangle (drawing
->pixmap
,
262 widget
->style
->black_gc
,
265 widget
->allocation
.width
+SAFETY
,
266 widget
->allocation
.height
+SAFETY
);
268 //g_info("init data request");
271 /* Initial data request */
272 /* no, do initial data request in the expose event */
273 // Do not need to ask for data of 1 pixel : not synchronized with
274 // main window time at this moment.
275 //drawing_data_request(drawing, &drawing->pixmap, 0, 0,
276 // widget->allocation.width,
277 // widget->allocation.height);
279 drawing
->width
= widget
->allocation
.width
;
280 drawing
->height
= widget
->allocation
.height
;
282 drawing
->data_injected
= FALSE
;
292 /* Redraw the screen from the backing pixmap */
294 expose_event( GtkWidget
*widget
, GdkEventExpose
*event
, gpointer user_data
)
296 Drawing_t
*drawing
= (Drawing_t
*)user_data
;
298 const TimeWindow
*time_window
= lttvwindow_get_time_window(drawing
->control_flow_data
->mw
);
299 const LttTime
* current_time
=
300 lttvwindow_get_current_time(drawing
->control_flow_data
->mw
);
302 ControlFlowData
*control_flow_data
=
303 (ControlFlowData
*)g_object_get_data(
305 "control_flow_data");
309 LttTime window_end
= ltt_time_add(time_window
->time_width
,
310 time_window
->start_time
);
312 convert_time_to_pixels(
313 time_window
->start_time
,
316 widget
->allocation
.width
,
322 if(!drawing
->data_injected
)
324 drawing_data_request(drawing
, &drawing
->pixmap
, 0, 0,
325 widget
->allocation
.width
,
326 widget
->allocation
.height
);
327 drawing
->data_injected
= TRUE
;
330 /* update the screen from the pixmap buffer */
331 gdk_draw_pixmap(widget
->window
,
332 widget
->style
->fg_gc
[GTK_WIDGET_STATE (widget
)],
334 event
->area
.x
, event
->area
.y
,
335 event
->area
.x
, event
->area
.y
,
336 event
->area
.width
, event
->area
.height
);
339 /* Draw the dotted lines */
341 gint8 dash_list
[] = { 1, 2 };
342 GdkGC
*gc
= gdk_gc_new(control_flow_data
->drawing
->pixmap
);
343 gdk_gc_copy(gc
, widget
->style
->white_gc
);
344 gdk_gc_set_line_attributes(gc
,
346 GDK_LINE_ON_OFF_DASH
,
349 gdk_gc_set_dashes(gc
,
353 drawing_draw_line(NULL
, widget
->window
,
355 cursor_x
, drawing
->height
,
366 after_expose_event( GtkWidget
*widget
, GdkEventExpose
*event
, gpointer user_data
)
369 g_critical("AFTER EXPOSE");
379 button_press_event( GtkWidget
*widget
, GdkEventButton
*event
, gpointer user_data
)
381 ControlFlowData
*control_flow_data
=
382 (ControlFlowData
*)g_object_get_data(
384 "control_flow_data");
385 Drawing_t
*drawing
= control_flow_data
->drawing
;
386 const TimeWindow
*time_window
= lttvwindow_get_time_window(drawing
->control_flow_data
->mw
);
389 if(event
->button
== 1)
393 LttTime window_end
= ltt_time_add(time_window
->time_width
,
394 time_window
->start_time
);
397 /* left mouse button click */
398 g_debug("x click is : %f", event
->x
);
400 convert_pixels_to_time(widget
->allocation
.width
, (guint
)event
->x
,
401 time_window
->start_time
,
405 lttvwindow_report_current_time(control_flow_data
->mw
, &time
);
409 lttvwindow_report_focus(control_flow_data
->mw
, gtk_widget_get_parent(control_flow_data
->scrolled_window
));
417 Drawing_t
*drawing_construct(ControlFlowData
*control_flow_data
)
419 Drawing_t
*drawing
= g_new(Drawing_t
, 1);
421 drawing
->control_flow_data
= control_flow_data
;
423 drawing
->vbox
= gtk_vbox_new(FALSE
, 1);
424 drawing
->ruler
= gtk_drawing_area_new ();
425 gtk_widget_set_size_request(drawing
->ruler
, -1, 27);
427 drawing
->drawing_area
= gtk_drawing_area_new ();
429 gtk_box_pack_start(GTK_BOX(drawing
->vbox
), drawing
->ruler
,
431 gtk_box_pack_end(GTK_BOX(drawing
->vbox
), drawing
->drawing_area
,
434 drawing
->pango_layout
=
435 gtk_widget_create_pango_layout(drawing
->drawing_area
, NULL
);
441 drawing
->data_injected
= FALSE
;
443 //gtk_widget_set_size_request(drawing->drawing_area->window, 50, 50);
444 g_object_set_data_full(
445 G_OBJECT(drawing
->drawing_area
),
448 (GDestroyNotify
)drawing_destroy
);
451 G_OBJECT(drawing
->ruler
),
456 //gtk_widget_modify_bg( drawing->drawing_area,
458 // &CF_Colors[BLACK]);
460 //gdk_window_get_geometry(drawing->drawing_area->window,
462 // &(drawing->width),
463 // &(drawing->height),
466 //drawing->pixmap = gdk_pixmap_new(
467 // drawing->drawing_area->window,
472 drawing
->pixmap
= NULL
;
474 // drawing->pixmap = gdk_pixmap_new(drawing->drawing_area->window,
475 // drawing->drawing_area->allocation.width,
476 // drawing->drawing_area->allocation.height,
479 gtk_widget_add_events(drawing
->drawing_area
, GDK_BUTTON_PRESS_MASK
);
481 g_signal_connect (G_OBJECT(drawing
->drawing_area
),
483 G_CALLBACK (configure_event
),
486 g_signal_connect (G_OBJECT(drawing
->ruler
),
488 G_CALLBACK(expose_ruler
),
491 gtk_widget_add_events(drawing
->ruler
, GDK_POINTER_MOTION_MASK
);
493 g_signal_connect (G_OBJECT(drawing
->ruler
),
494 "motion-notify-event",
495 G_CALLBACK(motion_notify_ruler
),
499 g_signal_connect (G_OBJECT(drawing
->drawing_area
),
501 G_CALLBACK (expose_event
),
504 g_signal_connect_after (G_OBJECT(drawing
->drawing_area
),
506 G_CALLBACK (after_expose_event
),
509 g_signal_connect (G_OBJECT(drawing
->drawing_area
),
510 "button-press-event",
511 G_CALLBACK (button_press_event
),
514 gtk_widget_show(drawing
->ruler
);
515 gtk_widget_show(drawing
->drawing_area
);
521 void drawing_destroy(Drawing_t
*drawing
)
524 // Do not unref here, Drawing_t destroyed by it's widget.
525 //g_object_unref( G_OBJECT(drawing->drawing_area));
527 g_free(drawing
->pango_layout
);
531 GtkWidget
*drawing_get_drawing_area(Drawing_t
*drawing
)
533 return drawing
->drawing_area
;
536 GtkWidget
*drawing_get_widget(Drawing_t
*drawing
)
538 return drawing
->vbox
;
541 /* convert_pixels_to_time
543 * Convert from window pixel and time interval to an absolute time.
545 void convert_pixels_to_time(
548 LttTime window_time_begin
,
549 LttTime window_time_end
,
552 LttTime window_time_interval
;
554 window_time_interval
= ltt_time_sub(window_time_end
,
556 *time
= ltt_time_mul(window_time_interval
, (x
/(float)width
));
557 *time
= ltt_time_add(window_time_begin
, *time
);
562 void convert_time_to_pixels(
563 LttTime window_time_begin
,
564 LttTime window_time_end
,
569 LttTime window_time_interval
;
570 float interval_float
, time_float
;
572 window_time_interval
= ltt_time_sub(window_time_end
,window_time_begin
);
574 time
= ltt_time_sub(time
, window_time_begin
);
576 interval_float
= ltt_time_to_double(window_time_interval
);
577 time_float
= ltt_time_to_double(time
);
579 *x
= (guint
)(time_float
/interval_float
* width
);
583 void drawing_draw_line( Drawing_t
*drawing
,
589 gdk_draw_line (pixmap
,
597 void drawing_resize(Drawing_t
*drawing
, guint h
, guint w
)
599 drawing
->height
= h
;
602 gtk_widget_set_size_request ( drawing
->drawing_area
,
610 /* Insert a square corresponding to a new process in the list */
611 /* Applies to whole drawing->width */
612 void drawing_insert_square(Drawing_t
*drawing
,
616 //GdkRectangle update_rect;
618 /* Allocate a new pixmap with new height */
619 GdkPixmap
*pixmap
= gdk_pixmap_new(drawing
->drawing_area
->window
,
620 drawing
->width
+ SAFETY
,
621 drawing
->height
+ height
+ SAFETY
,
624 /* Copy the high region */
625 gdk_draw_drawable (pixmap
,
626 drawing
->drawing_area
->style
->black_gc
,
630 drawing
->width
+ SAFETY
, y
);
635 /* add an empty square */
636 gdk_draw_rectangle (pixmap
,
637 drawing
->drawing_area
->style
->black_gc
,
640 drawing
->width
+ SAFETY
, // do not overlap
645 /* copy the bottom of the region */
646 gdk_draw_drawable (pixmap
,
647 drawing
->drawing_area
->style
->black_gc
,
651 drawing
->width
+SAFETY
, drawing
->height
- y
+ SAFETY
);
657 gdk_pixmap_unref(drawing
->pixmap
);
659 drawing
->pixmap
= pixmap
;
661 drawing
->height
+=height
;
663 /* Rectangle to update, from new drawing dimensions */
664 //update_rect.x = 0 ;
665 //update_rect.y = y ;
666 //update_rect.width = drawing->width;
667 //update_rect.height = drawing->height - y ;
668 //gtk_widget_draw( drawing->drawing_area, &update_rect);
672 /* Remove a square corresponding to a removed process in the list */
673 void drawing_remove_square(Drawing_t
*drawing
,
677 //GdkRectangle update_rect;
679 /* Allocate a new pixmap with new height */
680 GdkPixmap
*pixmap
= gdk_pixmap_new(
681 drawing
->drawing_area
->window
,
682 drawing
->width
+ SAFETY
,
683 drawing
->height
- height
+ SAFETY
,
686 /* Copy the high region */
687 gdk_draw_drawable (pixmap
,
688 drawing
->drawing_area
->style
->black_gc
,
692 drawing
->width
+ SAFETY
, y
);
696 /* Copy up the bottom of the region */
697 gdk_draw_drawable (pixmap
,
698 drawing
->drawing_area
->style
->black_gc
,
702 drawing
->width
, drawing
->height
- y
- height
+ SAFETY
);
706 gdk_pixmap_unref(drawing
->pixmap
);
708 drawing
->pixmap
= pixmap
;
710 drawing
->height
-=height
;
712 /* Rectangle to update, from new drawing dimensions */
713 //update_rect.x = 0 ;
714 //update_rect.y = y ;
715 //update_rect.width = drawing->width;
716 //update_rect.height = drawing->height - y ;
717 //gtk_widget_draw( drawing->drawing_area, &update_rect);
720 void drawing_update_ruler(Drawing_t
*drawing
, TimeWindow
*time_window
)
725 req
.width
= drawing
->ruler
->allocation
.width
;
726 req
.height
= drawing
->ruler
->allocation
.height
;
731 rect
.width
= req
.width
;
732 rect
.height
= req
.height
;
734 gtk_widget_queue_draw(drawing
->ruler
);
735 //gtk_widget_draw( drawing->ruler, &rect);
738 /* Redraw the ruler */
740 expose_ruler( GtkWidget
*widget
, GdkEventExpose
*event
, gpointer user_data
)
742 Drawing_t
*drawing
= (Drawing_t
*)user_data
;
743 const TimeWindow
*time_window
= lttvwindow_get_time_window(drawing
->control_flow_data
->mw
);
746 PangoContext
*context
;
748 PangoAttribute
*attribute
;
749 PangoFontDescription
*FontDesc
;
751 PangoRectangle ink_rect
;
752 guint global_width
=0;
753 GdkColor foreground
= { 0, 0, 0, 0 };
754 GdkColor background
= { 0, 0xffff, 0xffff, 0xffff };
757 ltt_time_add(time_window
->time_width
,
758 time_window
->start_time
);
760 ltt_time_div(time_window
->time_width
,2.0);
761 LttTime window_middle
=
762 ltt_time_add(half_width
,
763 time_window
->start_time
);
764 g_debug("ruler expose event");
766 gdk_draw_rectangle (drawing
->ruler
->window
,
767 drawing
->ruler
->style
->white_gc
,
769 event
->area
.x
, event
->area
.y
,
773 GdkGC
*gc
= gdk_gc_new(drawing
->ruler
->window
);
774 gdk_gc_copy(gc
, drawing
->ruler
->style
->black_gc
);
775 gdk_gc_set_line_attributes(gc
,
780 gdk_draw_line (drawing
->ruler
->window
,
783 event
->area
.x
+ event
->area
.width
, 1);
786 snprintf(text
, 255, "%lus\n%luns",
787 time_window
->start_time
.tv_sec
,
788 time_window
->start_time
.tv_nsec
);
790 layout
= gtk_widget_create_pango_layout(drawing
->drawing_area
, NULL
);
792 context
= pango_layout_get_context(layout
);
793 FontDesc
= pango_context_get_font_description(context
);
795 pango_font_description_set_size(FontDesc
, 6*PANGO_SCALE
);
796 pango_layout_context_changed(layout
);
798 pango_layout_set_text(layout
, text
, -1);
799 pango_layout_get_pixel_extents(layout
, &ink_rect
, NULL
);
800 global_width
+= ink_rect
.width
;
802 gdk_draw_layout_with_colors(drawing
->ruler
->window
,
806 layout
, &foreground
, &background
);
808 gdk_gc_set_line_attributes(gc
,
814 gdk_draw_line (drawing
->ruler
->window
,
820 snprintf(text
, 255, "%lus\n%luns", window_end
.tv_sec
,
823 pango_layout_set_text(layout
, text
, -1);
824 pango_layout_get_pixel_extents(layout
, &ink_rect
, NULL
);
825 global_width
+= ink_rect
.width
;
827 if(global_width
<= drawing
->ruler
->allocation
.width
)
829 gdk_draw_layout_with_colors(drawing
->ruler
->window
,
831 drawing
->ruler
->allocation
.width
- ink_rect
.width
,
833 layout
, &foreground
, &background
);
835 gdk_gc_set_line_attributes(gc
,
841 gdk_draw_line (drawing
->ruler
->window
,
843 drawing
->ruler
->allocation
.width
-1, 1,
844 drawing
->ruler
->allocation
.width
-1, 7);
848 snprintf(text
, 255, "%lus\n%luns", window_middle
.tv_sec
,
849 window_middle
.tv_nsec
);
851 pango_layout_set_text(layout
, text
, -1);
852 pango_layout_get_pixel_extents(layout
, &ink_rect
, NULL
);
853 global_width
+= ink_rect
.width
;
855 if(global_width
<= drawing
->ruler
->allocation
.width
)
857 gdk_draw_layout_with_colors(drawing
->ruler
->window
,
859 (drawing
->ruler
->allocation
.width
- ink_rect
.width
)/2,
861 layout
, &foreground
, &background
);
863 gdk_gc_set_line_attributes(gc
,
869 gdk_draw_line (drawing
->ruler
->window
,
871 drawing
->ruler
->allocation
.width
/2, 1,
872 drawing
->ruler
->allocation
.width
/2, 7);
880 g_object_unref(layout
);
886 /* notify mouse on ruler */
888 motion_notify_ruler(GtkWidget
*widget
, GdkEventMotion
*event
, gpointer user_data
)
891 //eventually follow mouse and show time here