Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 108 lines (89 sloc) 3.692 kb
515baab Changed the license to Apache License. Please see the file LICENSE. The ...
Mazen Harake authored
1 %%==============================================================================
2 %% Copyright 2010 Erlang Solutions Ltd.
4482ad2 @mazenharake Update outdated email
authored
3 %% Author: Mazen Harake (mazen.harake@gmail.com)
4df93e4 Initial commit
Mazen Harake authored
4 %%
515baab Changed the license to Apache License. Please see the file LICENSE. The ...
Mazen Harake authored
5 %% Licensed under the Apache License, Version 2.0 (the "License");
6 %% you may not use this file except in compliance with the License.
7 %% You may obtain a copy of the License at
4df93e4 Initial commit
Mazen Harake authored
8 %%
515baab Changed the license to Apache License. Please see the file LICENSE. The ...
Mazen Harake authored
9 %% http://www.apache.org/licenses/LICENSE-2.0
4df93e4 Initial commit
Mazen Harake authored
10 %%
515baab Changed the license to Apache License. Please see the file LICENSE. The ...
Mazen Harake authored
11 %% Unless required by applicable law or agreed to in writing, software
12 %% distributed under the License is distributed on an "AS IS" BASIS,
13 %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 %% See the License for the specific language governing permissions and
15 %% limitations under the License.
16 %%==============================================================================
4df93e4 Initial commit
Mazen Harake authored
17
18 -module(cecho_srv).
515baab Changed the license to Apache License. Please see the file LICENSE. The ...
Mazen Harake authored
19
4482ad2 @mazenharake Update outdated email
authored
20 -author('mazen.harake@gmail.com').
515baab Changed the license to Apache License. Please see the file LICENSE. The ...
Mazen Harake authored
21
4df93e4 Initial commit
Mazen Harake authored
22 -behaviour(gen_server).
6b4fb8e @kevsmith A bit of clean up
kevsmith authored
23 -include("cecho.hrl").
24 -include("cecho_commands.hrl").
4df93e4 Initial commit
Mazen Harake authored
25
26 %% Behaviour Callbacks
27 -export([init/1, terminate/2, handle_call/3, handle_cast/2, handle_info/2,
28 code_change/3]).
29
30 %% Module API
7f6fd67 @mazenharake Added async-input.
authored
31 -export([start_link/0, call/2, getch/0]).
4df93e4 Initial commit
Mazen Harake authored
32
33 %% Records
176aa42 Changed how the getch() function is called. An async thread is now
Mazen Harake authored
34 -record(state, { port, getch, observer }).
4df93e4 Initial commit
Mazen Harake authored
35
36 %% =============================================================================
37 %% Module API
38 %% =============================================================================
39 start_link() ->
40 gen_server:start_link({local, ?MODULE}, ?MODULE, no_args, []).
41
42 call(Cmd, Args) ->
3d0d89d Added new commands (mvaddch and mvaddstr) and new examples (cursmove)
Mazen Harake authored
43 gen_server:call(?MODULE, {call, Cmd, Args}, infinity).
4df93e4 Initial commit
Mazen Harake authored
44
7f6fd67 @mazenharake Added async-input.
authored
45 getch() ->
42cd2ed @mazenharake Rewrote some of the getch logic to make it behave more in accordance wit...
authored
46 gen_server:call(?MODULE, getch, infinity).
7f6fd67 @mazenharake Added async-input.
authored
47
4df93e4 Initial commit
Mazen Harake authored
48 %% =============================================================================
49 %% Behaviour Callbacks
50 %% =============================================================================
51 init(no_args) ->
52 process_flag(trap_exit, true),
6b4fb8e @kevsmith A bit of clean up
kevsmith authored
53 case load_driver() of
4df93e4 Initial commit
Mazen Harake authored
54 ok ->
7f6fd67 @mazenharake Added async-input.
authored
55 Port = erlang:open_port({spawn, "cecho"}, [binary]),
3d0d89d Added new commands (mvaddch and mvaddstr) and new examples (cursmove)
Mazen Harake authored
56 ok = do_call(Port, ?INITSCR),
8fcfd6a Fixed compilation bug.
Mazen Harake authored
57 ok = do_call(Port, ?WERASE, 0),
618a3ab Added the possibility to provide the scrollok function with a window. If...
Mazen Harake authored
58 ok = do_call(Port, ?REFRESH),
4df93e4 Initial commit
Mazen Harake authored
59 {ok, #state{ port = Port }};
60 {error, ErrorCode} ->
61 exit({driver_error, erl_ddll:format_error(ErrorCode)})
62 end.
63
3d0d89d Added new commands (mvaddch and mvaddstr) and new examples (cursmove)
Mazen Harake authored
64 handle_call({call, Cmd, Args}, _From, State) ->
42cd2ed @mazenharake Rewrote some of the getch logic to make it behave more in accordance wit...
authored
65 {reply, do_call(State#state.port, Cmd, Args), State};
66 handle_call(getch, From, #state{ getch = undefined } = State) ->
67 {noreply, State#state{ getch = From }};
68 handle_call(getch, _From, State) ->
69 {reply, -1, State}.
4df93e4 Initial commit
Mazen Harake authored
70
71 terminate(_Reason, State) ->
3d0d89d Added new commands (mvaddch and mvaddstr) and new examples (cursmove)
Mazen Harake authored
72 do_call(State#state.port, ?ENDWIN),
73 do_call(State#state.port, ?CURS_SET, ?ceCURS_NORMAL),
4df93e4 Initial commit
Mazen Harake authored
74 erlang:port_close(State#state.port),
75 erl_ddll:unload("cecho").
76
42cd2ed @mazenharake Rewrote some of the getch logic to make it behave more in accordance wit...
authored
77 handle_info({_Port, {data, _Binary}}, #state{ getch = undefined } = State) ->
7f6fd67 @mazenharake Added async-input.
authored
78 {noreply, State};
452908b Key input now use driver_select instead of "asynch polling"
Mazen Harake authored
79 handle_info({_Port, {data, Binary}}, State) ->
42cd2ed @mazenharake Rewrote some of the getch logic to make it behave more in accordance wit...
authored
80 gen_server:reply(State#state.getch, binary_to_term(Binary)),
81 {noreply, State#state{ getch = undefined }}.
4df93e4 Initial commit
Mazen Harake authored
82
83 %% @hidden
7f6fd67 @mazenharake Added async-input.
authored
84 handle_cast(_, State) ->
4df93e4 Initial commit
Mazen Harake authored
85 {noreply, State}.
86
87 %% @hidden
88 code_change(_, State, _) ->
89 {noreply, State}.
90
91 %% =============================================================================
92 %% Internal Functions
93 %% =============================================================================
3d0d89d Added new commands (mvaddch and mvaddstr) and new examples (cursmove)
Mazen Harake authored
94 do_call(Port, Cmd) ->
b401218 Changed so that a call to the c port with no arguments now sends
Mazen Harake authored
95 do_call(Port, Cmd, undefined).
4df93e4 Initial commit
Mazen Harake authored
96
3d0d89d Added new commands (mvaddch and mvaddstr) and new examples (cursmove)
Mazen Harake authored
97 do_call(Port, Cmd, Args) ->
4df93e4 Initial commit
Mazen Harake authored
98 binary_to_term(erlang:port_control(Port, Cmd, term_to_binary(Args))).
6b4fb8e @kevsmith A bit of clean up
kevsmith authored
99
100 load_driver() ->
101 Dir = case code:priv_dir(cecho) of
102 {error, bad_name} ->
103 filename:dirname(code:which(?MODULE)) ++ "/../priv";
104 D ->
105 D
106 end,
107 erl_ddll:load(Dir, "cecho").
Something went wrong with that request. Please try again.