4
%% Copyright Ericsson AB 2008-2009. 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
%%%-------------------------------------------------------------------
19
%%% File : wxe_util.erl
20
%%% Author : Dan Gudmundsson <dgud@erix.ericsson.se>
23
%%% Created : 9 Feb 2007 by Dan Gudmundsson <dgud@erix.ericsson.se>
24
%%%-------------------------------------------------------------------
28
-export([call/2,cast/2,construct/2,
29
destroy/2, register_pid/1,
30
connect_cb/2,disconnect_cb/2,
31
send_bin/1, get_cbId/1,
32
get_const/1,colour_bin/1,datetime_bin/1,
33
to_bool/1,from_bool/1]).
42
from_bool(false) -> 0.
44
colour_bin({R,G,B}) ->
45
<<R:32/?UI,G:32/?UI, B:32/?UI,255:32/?UI>>;
46
colour_bin({R,G,B,A}) ->
47
<<R:32/?UI,G:32/?UI, B:32/?UI,A:32/?UI>>.
49
datetime_bin({{Y,Mo,D},{H,Mi,S}}) ->
50
%% DMY fits wxDaytime constructor
51
%% Also wxDaytime:Month is enum from zero
52
<<D:32/?UI,(Mo-1):32/?UI,Y:32/?UI,H:32/?UI,Mi:32/?UI,S:32/?UI>>.
55
[{Id, Data}] = ets:lookup(wx_non_consts, Id),
59
#wx_env{port=Port,debug=Dbg} = wx:get_env(),
60
erlang:port_control(Port,Op,Args),
62
true -> debug_cast(Dbg band 15, Op,Args,Port);
68
#wx_env{port=Port,debug=Dbg} = wx:get_env(),
71
erlang:port_control(Port,Op,Args),
74
debug_call(Dbg band 15, Op, Args, Port)
79
{'_wxe_result_', Res} -> Res;
80
{'_wxe_error_', Op, Error} ->
81
[{_,MF}] = ets:lookup(wx_debug_info,Op),
82
erlang:error({Error, MF});
83
{'_wxe_error_', Old, Error} ->
84
[{_,MF}] = ets:lookup(wx_debug_info,Old),
85
erlang:exit({Error, MF})
88
construct(Op, Args) ->
91
destroy(Op, #wx_ref{ref=Ref}) ->
92
cast(Op,<<Ref:32/?UI>>).
94
register_pid(#wx_ref{ref=Ref}) ->
95
send_bin(term_to_binary(self())),
96
call(?WXE_REGISTER_OBJECT, <<Ref:32/?UI>>).
98
send_bin(Bin) when is_binary(Bin) ->
99
#wx_env{port=Port,debug=Dbg} = wx:get_env(),
102
erlang:port_command(Port, Bin);
104
io:format("WX binary ~p(~p) ~n",[self(), Port]),
105
erlang:port_command(Port, Bin)
109
gen_server:call((wx:get_env())#wx_env.sv,{register_cb, Fun}).
111
connect_cb(Object,EvData) ->
112
handle_listener(connect_cb, Object, EvData).
114
disconnect_cb(Object,EvData) ->
115
handle_listener(disconnect_cb, Object, EvData).
117
handle_listener(Op,Object,EvData) ->
118
Listener = gen_server:call((wx:get_env())#wx_env.sv, {Op,Object,EvData}),
120
{call_impl, connect_cb, EvtList} ->
121
wxEvtHandler:connect_impl(EvtList,Object,EvData);
126
debug_cast(1, Op, _Args, _Port) ->
128
case ets:lookup(wx_debug_info,Op) of
130
io:format("WX ~p: ~s:~s(~p) -> ok~n", [self(),M,F,Op]);
132
io:format("WX ~p: unknown(~p) -> ok~n", [self(),Op])
134
debug_cast(2, Op, Args, Port) ->
136
case ets:lookup(wx_debug_info,Op) of
138
io:format("WX ~p(~p): ~s:~s(~p) (~p) -> ok~n",
139
[self(),Port,M,F,Op,Args]);
141
io:format("WX ~p(~p): unknown(~p) (~p) -> ok~n",
142
[self(),Port,Op,Args])
144
debug_cast(_, _Op, _Args, _Port) ->
148
debug_call(1, Op, Args, Port) ->
150
case ets:lookup(wx_debug_info,Op) of
152
io:format("WX ~p: ~s:~s(~p) -> ",[self(),M,F,Op]);
154
io:format("WX ~p: unknown(~p) -> ",[self(),Op])
156
erlang:port_control(Port,Op,Args),
159
debug_call(2, Op, Args, Port) ->
161
case ets:lookup(wx_debug_info,Op) of
163
io:format("WX ~p(~p): ~s:~s(~p) (~p) -> ",
164
[self(), Port, M, F, Op, Args]);
166
io:format("WX ~p(~p): unknown(~p) (~p) -> ",
167
[self(), Port, Op, Args])
169
erlang:port_control(Port,Op,Args),
171
debug_call(_, Op, Args, Port) ->
173
erlang:port_control(Port,Op,Args),
180
{'_wxe_result_', Res} ->
181
io:format("complete ~n", []),
183
{'_wxe_error_', Op2, Error} ->
184
[{_,MF2}] = ets:lookup(wx_debug_info,Op2),
185
erlang:error({Error, MF2})
189
{'_wxe_result_', Res} ->
190
io:format("~p ~n", [Res]),
192
{'_wxe_error_', Op, Error} ->
193
io:format("Error ~p ~n", [Error]),
194
[{_,MF}] = ets:lookup(wx_debug_info,Op),
195
erlang:error({Error, MF})
200
{'_wxe_error_', Op, Error} ->
201
[{_,MF={M,F,_}}] = ets:lookup(wx_debug_info,Op),
202
io:format("WX ~p: ERROR in previous command ~s:~s~n",[self(), M,F]),
203
erlang:error({Error, MF})