summaryrefslogtreecommitdiffstats
path: root/src/core/ephraim_gui.erl
blob: 7bf4b741b6d1600a5bdc89c6f5138a41d0636a6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
-module(ephraim_gui).
-behaviour(gen_event).
-export([start/0, stop/0]).
-export([init/1, handle_event/2, handle_call/2, handle_info/2, terminate/2, code_change/3]).

start() ->
    gen_event:add_handler(ephraim_event_man, ?MODULE, []).
stop() ->
    gen_event:delete_handler(ephraim_event_man, ?MODULE, []).

-spec init(term()) -> ok.
init(_Args) ->
    Port = open_port({spawn_executable, "ephraim-gtk"}, [binary, nouse_stdio, {packet, 4}]),
    {ok, Port}.

handle_event({view_update, Msg}, Port) ->
    Port ! {self(), {command, term_to_binary(Msg)}},
    {ok, Port};
	
handle_event(_Event, Port) ->
    {ok, Port}.

handle_call(_Msg, Port) ->
    {noreply, Port}.

handle_info({Port, {data, Data}}, Port) ->
    Msg = binary_to_term(Data),
    case Msg of
	stop ->
	    init:stop(),
	    remove_handler;
	_ ->
	    ephraim:notify({view_request, Msg}),
	    {ok, Port}
    end;
    
handle_info(_Msg, Port) ->
    {ok, Port}.

terminate(_Reason, _Port) ->
    ok.
code_change(_OldVersion, Port, _Extra) ->
    {ok, Port}.