4
%% Copyright Ericsson AB 2011. All Rights Reserved.
6
%% The contents of this file are subject to the Erlang Public License,
7
%% Version 1.1, (the "License"); you may not use this file except in
8
%% compliance with the License. You should have received a copy of the
9
%% Erlang Public License along with this software. If not, it can be
10
%% retrieved online at http://www.erlang.org/.
12
%% Software distributed under the License is distributed on an "AS IS"
13
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14
%% the License for the specific language governing rights and limitations
18
-module(observer_app_wx).
20
-export([start_link/2]).
22
%% wx_object callbacks
23
-export([init/1, handle_info/2, terminate/2, code_change/3, handle_call/3,
24
handle_event/2, handle_sync_event/3, handle_cast/2]).
26
-behaviour(wx_object).
27
-include_lib("wx/include/wx.hrl").
28
-include("observer_defs.hrl").
43
-record(paint, {font, pen, brush, sel, links}).
45
-record(app, {ptree, n2p, links, dim}).
46
-record(box, {x,y, w,h, s1}).
47
-record(str, {x,y,text,pid}).
49
-define(BX_E, 10). %% Empty width between text and box
50
-define(BX_HE, (?BX_E div 2)).
51
-define(BY_E, 10). %% Empty height between text and box
52
-define(BY_HE, (?BY_E div 2)).
54
-define(BB_X, 16). %% Empty width between boxes
55
-define(BB_Y, 12). %% Empty height between boxes
58
-define(ID_PROC_INFO, 101).
59
-define(ID_PROC_MSG, 102).
60
-define(ID_PROC_KILL, 103).
61
-define(ID_TRACE_PID, 104).
62
-define(ID_TRACE_NAME, 105).
63
-define(ID_TRACE_TREE_PIDS, 106).
64
-define(ID_TRACE_TREE_NAMES, 107).
66
start_link(Notebook, Parent) ->
67
wx_object:start_link(?MODULE, [Notebook, Parent], []).
69
init([Notebook, Parent]) ->
70
Panel = wxPanel:new(Notebook, [{size, wxWindow:getClientSize(Notebook)},
73
Main = wxBoxSizer:new(?wxHORIZONTAL),
74
Splitter = wxSplitterWindow:new(Panel, [{size, wxWindow:getClientSize(Panel)},
75
{style, ?wxSP_LIVE_UPDATE},
78
Apps = wxListBox:new(Splitter, 3, []),
79
%% Need extra panel and sizer to get correct size updates
80
%% in draw area for some reason
81
P2 = wxPanel:new(Splitter, [{winid, 4}]),
82
Extra = wxBoxSizer:new(?wxVERTICAL),
83
DrawingArea = wxScrolledWindow:new(P2, [{winid, ?DRAWAREA},
84
{style,?wxFULL_REPAINT_ON_RESIZE}]),
85
wxWindow:setBackgroundColour(DrawingArea, ?wxWHITE),
86
wxWindow:setVirtualSize(DrawingArea, 800, 800),
87
wxSplitterWindow:setMinimumPaneSize(Splitter,50),
88
wxSizer:add(Extra, DrawingArea, [{flag, ?wxEXPAND},{proportion, 1}]),
89
wxWindow:setSizer(P2, Extra),
90
wxSplitterWindow:splitVertically(Splitter, Apps, P2, [{sashPosition, 150}]),
91
wxWindow:setSizer(Panel, Main),
93
wxSizer:add(Main, Splitter, [{flag, ?wxEXPAND bor ?wxALL},
94
{proportion, 1}, {border, 5}]),
95
wxWindow:setSizer(Panel, Main),
96
wxListBox:connect(Apps, command_listbox_selected),
97
wxPanel:connect(DrawingArea, paint, [callback]),
98
wxPanel:connect(DrawingArea, size, [{skip, true}]),
99
wxPanel:connect(DrawingArea, left_up),
100
wxPanel:connect(DrawingArea, left_dclick),
101
wxPanel:connect(DrawingArea, right_down),
103
DefFont = wxSystemSettings:getFont(?wxSYS_DEFAULT_GUI_FONT),
104
SelCol = wxSystemSettings:getColour(?wxSYS_COLOUR_HIGHLIGHT),
105
SelBrush = wxBrush:new(SelCol),
106
LinkPen = wxPen:new(SelCol, [{width, 2}]),
107
%% GC = wxGraphicsContext:create(DrawingArea),
108
%% _Font = wxGraphicsContext:createFont(GC, DefFont),
109
{Panel, #state{parent=Parent,
113
paint=#paint{font= DefFont,
115
brush=?wxLIGHT_GREY_BRUSH,
121
setup_scrollbar(AppWin, App) ->
122
setup_scrollbar(wxWindow:getClientSize(AppWin), AppWin, App).
124
setup_scrollbar({CW, CH}, AppWin, #app{dim={W0,H0}}) ->
128
if W0 =< CW, H0 =< CH ->
129
wxScrolledWindow:setScrollbars(AppWin, W, H, 1, 1);
131
wxScrolledWindow:setScrollbars(AppWin, PPC, H, W div PPC+1, 1);
133
wxScrolledWindow:setScrollbars(AppWin, W, PPC, 1, H div PPC+1);
135
wxScrolledWindow:setScrollbars(AppWin, PPC, PPC, W div PPC+1, H div PPC+1)
137
setup_scrollbar(_, _, undefined) -> ok.
139
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141
handle_event(#wx{event=#wxCommand{type=command_listbox_selected, cmdString=AppStr}},
142
State = #state{appmon=AppMon, current=Prev}) ->
147
App = list_to_atom(AppStr),
148
(Prev =/= undefined) andalso appmon_info:app(AppMon, Prev, false, []),
149
appmon_info:app(AppMon, App, true, []),
150
{noreply, State#state{current=App}}
153
handle_event(#wx{id=Id, event=_Sz=#wxSize{size=Size}},
154
State=#state{app=App, app_w=AppWin}) ->
155
Id =:= ?DRAWAREA andalso setup_scrollbar(Size,AppWin,App),
158
handle_event(#wx{event=#wxMouse{type=Type, x=X0, y=Y0}},
159
S0=#state{app=#app{ptree=Tree}, app_w=AppWin}) ->
160
{X,Y} = wxScrolledWindow:calcUnscrolledPosition(AppWin, X0, Y0),
161
Hit = locate_node(X,Y, [Tree]),
162
State = handle_mouse_click(Hit, Type, S0),
165
handle_event(#wx{event=#wxCommand{type=command_menu_selected}},
166
State = #state{sel=undefined}) ->
167
observer_lib:display_info_dialog("Select process first"),
170
handle_event(#wx{id=?ID_PROC_INFO, event=#wxCommand{type=command_menu_selected}},
171
State = #state{panel=Panel, sel={#box{s1=#str{pid=Pid}},_}}) ->
172
observer_procinfo:start(Pid, Panel, self()),
175
handle_event(#wx{id=?ID_PROC_MSG, event=#wxCommand{type=command_menu_selected}},
176
State = #state{panel=Panel, sel={#box{s1=#str{pid=Pid}},_}}) ->
177
case observer_lib:user_term(Panel, "Enter message", "") of
179
{ok, Term} -> Pid ! Term;
180
{error, Error} -> observer_lib:display_info_dialog(Error)
184
handle_event(#wx{id=?ID_PROC_KILL, event=#wxCommand{type=command_menu_selected}},
185
State = #state{panel=Panel, sel={#box{s1=#str{pid=Pid}},_}}) ->
186
case observer_lib:user_term(Panel, "Enter Exit Reason", "") of
188
{ok, Term} -> exit(Pid, Term);
189
{error, Error} -> observer_lib:display_info_dialog(Error)
194
handle_event(#wx{id=?ID_TRACE_PID, event=#wxCommand{type=command_menu_selected}},
195
State = #state{sel={Box,_}}) ->
196
observer_trace_wx:add_processes(observer_wx:get_tracer(), [box_to_pid(Box)]),
198
handle_event(#wx{id=?ID_TRACE_NAME, event=#wxCommand{type=command_menu_selected}},
199
State = #state{sel={Box,_}}) ->
200
observer_trace_wx:add_processes(observer_wx:get_tracer(), [box_to_reg(Box)]),
202
handle_event(#wx{id=?ID_TRACE_TREE_PIDS, event=#wxCommand{type=command_menu_selected}},
203
State = #state{sel=Sel}) ->
204
Get = fun(Box) -> box_to_pid(Box) end,
205
observer_trace_wx:add_processes(observer_wx:get_tracer(), tree_map(Sel, Get)),
207
handle_event(#wx{id=?ID_TRACE_TREE_NAMES, event=#wxCommand{type=command_menu_selected}},
208
State = #state{sel=Sel}) ->
209
Get = fun(Box) -> box_to_reg(Box) end,
210
observer_trace_wx:add_processes(observer_wx:get_tracer(), tree_map(Sel, Get)),
213
handle_event(Event, _State) ->
214
error({unhandled_event, Event}).
217
handle_sync_event(#wx{event = #wxPaint{}},_,
218
#state{app_w=DA, app=App, sel=Sel, paint=Paint}) ->
219
%% PaintDC must be created in a callback to work on windows.
220
DC = wxPaintDC:new(DA),
221
wxScrolledWindow:doPrepareDC(DA,DC),
222
%% Nothing is drawn until wxPaintDC is destroyed.
223
draw(DC, App, Sel, Paint),
224
wxPaintDC:destroy(DC),
227
handle_call(Event, From, _State) ->
228
error({unhandled_call, Event, From}).
230
handle_cast(Event, _State) ->
231
error({unhandled_cast, Event}).
233
handle_info({active, Node}, State = #state{parent=Parent, current=Curr, appmon=Appmon}) ->
234
create_menus(Parent, []),
235
{ok, Pid} = appmon_info:start_link(Node, self(), []),
239
_ -> %% Deregister me as client (and stop appmon if last)
242
appmon_info:app_ctrl(Pid, Node, true, []),
243
(Curr =/= undefined) andalso appmon_info:app(Pid, Curr, true, []),
244
{noreply, State#state{appmon=Pid}};
246
handle_info(not_active, State = #state{appmon=AppMon, current=Prev}) ->
247
appmon_info:app_ctrl(AppMon, node(AppMon), false, []),
248
(Prev =/= undefined) andalso appmon_info:app(AppMon, Prev, false, []),
251
handle_info({delivery, Pid, app_ctrl, _, Apps0},
252
State = #state{appmon=Pid, apps_w=LBox}) ->
253
Apps = [atom_to_list(App) || {_, App, {_, _, _}} <- Apps0],
254
wxListBox:clear(LBox),
255
wxListBox:appendStrings(LBox, [App || App <- lists:sort(Apps)]),
258
handle_info({delivery, _Pid, app, _Curr, {[], [], [], []}},
259
State = #state{panel=Panel}) ->
260
wxWindow:refresh(Panel),
261
{noreply, State#state{app=undefined, sel=undefined}};
263
handle_info({delivery, Pid, app, Curr, AppData},
264
State = #state{panel=Panel, appmon=Pid, current=Curr,
265
app_w=AppWin, paint=#paint{font=Font}}) ->
266
App = build_tree(AppData, {AppWin,Font}),
267
setup_scrollbar(AppWin, App),
268
wxWindow:refresh(Panel),
269
wxWindow:layout(Panel),
270
{noreply, State#state{app=App, sel=undefined}};
272
handle_info(_Event, State) ->
273
%% io:format("~p:~p: ~p~n",[?MODULE,?LINE,_Event]),
277
terminate(_Event, _State) ->
279
code_change(_, _, State) ->
282
handle_mouse_click(Node = {#box{s1=#str{pid=Pid}},_}, Type,
283
State=#state{app_w=AppWin,panel=Panel}) ->
285
left_dclick -> observer_procinfo:start(Pid, Panel, self());
286
right_down -> popup_menu(Panel);
289
wxWindow:refresh(AppWin),
290
State#state{sel=Node};
291
handle_mouse_click(_, _, State = #state{sel=undefined}) ->
293
handle_mouse_click(_, right_down, State=#state{panel=Panel}) ->
296
handle_mouse_click(_, _, State=#state{app_w=AppWin}) ->
297
wxWindow:refresh(AppWin),
298
State#state{sel=undefined}.
300
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302
create_menus(Parent, _) ->
305
[#create_menu{id=?ID_PROC_INFO, text="Process info"},
306
#create_menu{id=?ID_PROC_MSG, text="Send Msg"},
307
#create_menu{id=?ID_PROC_KILL, text="Kill process"}
310
[#create_menu{id=?ID_TRACE_PID, text="Trace process"},
311
#create_menu{id=?ID_TRACE_NAME, text="Trace named process"},
312
#create_menu{id=?ID_TRACE_TREE_PIDS, text="Trace process tree"},
313
#create_menu{id=?ID_TRACE_TREE_NAMES, text="Trace named process tree"}
315
observer_wx:create_menus(Parent, MenuEntries).
319
wxMenu:append(Menu, ?ID_PROC_INFO, "Process info"),
320
wxMenu:append(Menu, ?ID_TRACE_PID, "Trace process"),
321
wxMenu:append(Menu, ?ID_TRACE_NAME, "Trace named process"),
322
wxMenu:append(Menu, ?ID_TRACE_TREE_PIDS, "Trace process tree"),
323
wxMenu:append(Menu, ?ID_TRACE_TREE_NAMES, "Trace named process tree"),
324
wxWindow:popupMenu(Panel, Menu),
325
wxMenu:destroy(Menu).
328
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
329
locate_node(X, _Y, [{Box=#box{x=BX}, _Chs}|_Rest])
332
locate_node(X,Y, [Node={Box=#box{x=BX,y=BY,w=BW,h=BH}, _Chs}|Rest])
335
Y < BY -> {above, Box}; %% Above
336
Y =< (BY+BH) -> Node;
337
true -> locate_node(X,Y,Rest)
339
locate_node(X,Y, [{_, Chs}|Rest]) ->
340
case locate_node(X,Y,Chs) of
341
Node = {#box{},_} -> Node;
343
locate_node(X,Y,Rest)
345
locate_node(_, _, []) -> false.
347
locate_box(From, [{Box=#box{s1=#str{pid=From}},_}|_]) -> Box;
348
locate_box(From, [{_,Chs}|Rest]) ->
349
case locate_box(From, Chs) of
351
_ -> locate_box(From, Rest)
353
locate_box(From, []) -> {false, From}.
355
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
357
build_tree({Root, P2Name, Links, XLinks0}, Font) ->
358
Fam = sofs:relation_to_family(sofs:relation(Links)),
359
Name2P = gb_trees:from_orddict(lists:sort([{Name,Pid} || {Pid,Name} <- P2Name])),
360
Lookup = gb_trees:from_orddict(sofs:to_external(Fam)),
361
{_, Tree0} = build_tree2(Root, Lookup, Name2P, Font),
362
{Tree, Dim} = calc_tree_size(Tree0),
363
Fetch = fun({From, To}, Acc) ->
364
try {value, ToPid} = gb_trees:lookup(To, Name2P),
365
FromPid = gb_trees:get(From, Name2P),
366
[{locate_box(FromPid, [Tree]),locate_box(ToPid, [Tree])}|Acc]
371
XLinks = lists:foldl(Fetch, [], XLinks0),
372
#app{ptree=Tree, dim=Dim, links=XLinks}.
374
build_tree2(Root, Tree0, N2P, Font) ->
375
case gb_trees:lookup(Root, Tree0) of
376
none -> {Tree0, {box(Root, N2P, Font), []}};
378
Tree1 = gb_trees:delete(Root, Tree0),
379
{Tree, CHs} = lists:foldr(fun("port " ++_, Acc) ->
382
{T, C} = build_tree2(Child, T0, N2P, Font),
384
end, {Tree1, []}, Children),
385
{Tree, {box(Root, N2P, Font), CHs}}
388
calc_tree_size(Tree) ->
389
Cols = calc_col_start(Tree, [0]),
390
{Boxes,{W,Hs}} = calc_tree_size(Tree, Cols, ?BB_X, [?BB_Y]),
391
{Boxes, {W,lists:max(Hs)}}.
393
calc_col_start({#box{w=W}, Chs}, [Max|Acc0]) ->
394
Acc = if Acc0 == [] -> [0]; true -> Acc0 end,
395
Depth = lists:foldl(fun(Child, MDepth) -> calc_col_start(Child, MDepth) end,
399
calc_tree_size({Box=#box{w=W,h=H}, []}, _, X, [Y|Ys]) ->
400
{{Box#box{x=X,y=Y}, []}, {X+W+?BB_X,[Y+H+?BB_Y|Ys]}};
401
calc_tree_size({Box, Children}, [Col|Cols], X, [H0|Hs0]) ->
402
Hs1 = calc_row_start(Children, H0, Hs0),
403
StartX = X+Col+?BB_X,
404
{Boxes, {W,Hs}} = calc_tree_sizes(Children, Cols, StartX, StartX, Hs1, []),
405
Y = middle(Boxes, H0),
406
H = Y+Box#box.h+?BB_Y,
407
{{Box#box{x=X,y=Y}, Boxes}, {W,[H|Hs]}}.
409
calc_tree_sizes([Child|Chs], Cols, X0, W0, Hs0, Acc) ->
410
{Tree, {W,Hs}} = calc_tree_size(Child, Cols, X0, Hs0),
411
calc_tree_sizes(Chs, Cols, X0, max(W,W0), Hs, [Tree|Acc]);
412
calc_tree_sizes([], _, _, W,Hs, Acc) ->
413
{lists:reverse(Acc), {W,Hs}}.
415
calc_row_start(Chs = [{#box{h=H},_}|_], Start, Hs0) ->
417
Wanted = (H*NChs + ?BB_Y*(NChs-1)) div 2 - H div 2,
419
[] -> [max(?BB_Y, Start - Wanted)];
421
[max(Next, Start - Wanted)|Hs]
425
middle([{#box{y=Y}, _}], _) -> Y;
426
middle([{#box{y=Y0},_}|List], _) ->
427
{#box{y=Y1},_} = lists:last(List),
430
box(Str0, N2P, {Win,Font}) ->
431
Pid = gb_trees:get(Str0, N2P),
432
Str = if hd(Str0) =:= $< -> lists:append(io_lib:format("~w", [Pid]));
435
{TW,TH, _, _} = wxWindow:getTextExtent(Win, Str, [{theFont, Font}]),
436
Data = #str{text=Str, x=?BX_HE, y=?BY_HE, pid=Pid},
438
#box{w=TW+?BX_E, h=TH+?BY_E, s1=Data}.
440
box_to_pid(#box{s1=#str{pid=Pid}}) -> Pid.
441
box_to_reg(#box{s1=#str{text=[$<|_], pid=Pid}}) -> Pid;
442
box_to_reg(#box{s1=#str{text=Name}}) -> list_to_atom(Name).
444
tree_map({Box, Chs}, Fun) ->
445
tree_map(Chs, Fun, [Fun(Box)]).
446
tree_map([{Box, Chs}|Rest], Fun, Acc0) ->
447
Acc = tree_map(Chs, Fun, [Fun(Box)|Acc0]),
448
tree_map(Rest, Fun, Acc);
449
tree_map([], _ , Acc) -> Acc.
451
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
452
draw(_DC, undefined, _, _) ->
454
draw(DC, #app{dim={_W,_H}, ptree=Tree, links=Links}, Sel,
455
#paint{font=Font, pen=Pen, brush=Brush, links=LPen, sel=SelBrush}) ->
456
%% Canvas = wxGraphicsContext:create(DC),
457
%% Pen = wxGraphicsContext:createPen(Canvas, ?wxBLACK_PEN),
458
%% wxGraphicsContext:setPen(Canvas, Pen),
459
%% Brush = wxGraphicsContext:createBrush(Canvas, ?wxLIGHT_GREY_BRUSH),
460
%% wxGraphicsContext:setBrush(Canvas, Brush),
461
%% Font = wxGraphicsContext:createFont(Canvas, wxSystemSettings:getFont(?wxSYS_DEFAULT_GUI_FONT)),
462
%% wxGraphicsContext:setFont(Canvas, Font),
463
%% draw_tree(Tree, Canvas).
464
wxDC:setPen(DC, LPen),
465
[draw_xlink(Link, DC) || Link <- Links],
466
wxDC:setPen(DC, Pen),
467
%% wxDC:drawRectangle(DC, {2,2}, {W-2,H-2}), %% DEBUG
468
wxDC:setBrush(DC, Brush),
469
wxDC:setFont(DC, Font),
470
draw_tree(Tree, root, DC),
473
{#box{x=X,y=Y,w=W,h=H,s1=Str1}, _} ->
474
wxDC:setBrush(DC, SelBrush),
475
wxDC:drawRoundedRectangle(DC, {X-1,Y-1}, {W+2,H+2}, 8.0),
476
draw_str(DC, Str1, X, Y)
479
draw_tree({Box=#box{x=X,y=Y,w=W,h=H,s1=Str1}, Chs}, Parent, DC) ->
480
%%wxGraphicsContext:drawRoundedRectangle(DC, float(X), float(Y), float(W), float(H), 8.0),
481
wxDC:drawRoundedRectangle(DC, {X,Y}, {W,H}, 8.0),
482
draw_str(DC, Str1, X, Y),
485
[{#box{x=CX0},_}|_] ->
487
CX = CX0-(?BB_X div 2),
488
wxDC:drawLine(DC, {X+W, CY}, {CX, CY}),
491
draw_link(Parent, Box, DC),
492
[draw_tree(Child, Dot, DC) || Child <- Chs].
494
draw_link({CX,CY}, #box{x=X,y=Y0,h=H}, DC) ->
498
wxDC:drawLine(DC, {CX, CY}, {X, CY});
500
wxDC:drawLines(DC, [{CX, CY}, {CX, Y}, {X,Y}])
502
draw_link(_, _, _) -> ok.
504
draw_xlink({#box{x=X0, y=Y0, h=BH}, #box{x=X1, y=Y1}}, DC)
506
draw_xlink(X0,Y0,X1,Y1,BH,DC);
507
draw_xlink({#box{x=X0, y=Y0, h=BH, w=BW}, #box{x=X1, y=Y1}}, DC)
509
draw_xlink(X0+BW,Y0,X1,Y1,BH,DC);
510
draw_xlink({#box{x=X0, y=Y0, h=BH}, #box{x=X1, w=BW, y=Y1}}, DC)
512
draw_xlink(X1+BW,Y1,X0,Y0,BH,DC);
513
draw_xlink({_From, _To}, _DC) ->
515
draw_xlink(X0, Y00, X1, Y11, BH, DC) ->
516
{Y0,Y1} = if Y00 < Y11 -> {Y00+BH-6, Y11+6};
517
true -> {Y00+6, Y11+BH-6}
519
wxDC:drawLines(DC, [{X0,Y0}, {X0+5,Y0}, {X1-5,Y1}, {X1,Y1}]).
521
draw_str(DC, #str{x=Sx,y=Sy, text=Text}, X, Y) ->
522
%%wxGraphicsContext:drawText(DC, Text, float(Sx+X), float(Sy+Y));
523
wxDC:drawText(DC, Text, {X+Sx,Y+Sy});
524
draw_str(_, _, _, _) -> ok.