-
Notifications
You must be signed in to change notification settings - Fork 14
/
bindings.ex
292 lines (237 loc) · 10.2 KB
/
bindings.ex
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
defmodule ExTermbox.Bindings do
@moduledoc """
Provides the low-level bindings to the termbox library. This module loads the
NIFs defined in `c_src/` and thinly wraps the C interface.
For event-handling, it's recommended to use the `ExTermbox.EventManager` API
instead of the raw interface exposed here.
For more complex applications, it's recommended to use the high-level
rendering API provided by Ratatouille (a terminal UI kit based on the bindings
here). Ratatouille manages things like initialization, updates and shutdown
automatically, and provides a declarative, HTML-like interface for rendering
content to the screen. See the repo for details:
<https://github.com/ndreynolds/ratatouille>
See also the termbox header file for additional documentation of the functions
here:
<https://github.com/nsf/termbox/blob/master/src/termbox.h>
Note that the "NIF <function>/<arity> not loaded" messages below are fallbacks
normally replaced by the natively-implemented functions at load. If you're
seeing this message, it means the native bindings could not be loaded. Please
open an issue with the error and relevant system information.
### Event Polling
The event polling API differs slightly from the termbox API in order to make
it in the Erlang ecosystem. Instead of blocking poll calls, it uses
asynchronous message passing to deliver events to the caller.
It's recommended to use the `ExTermbox.EventManager` gen_server to subscribe
to terminal events instead of using these bindings directly. It supports
multiple subscriptions and more gracefully handles errors.
#### Implementation Notes
In the `start_polling/1` NIF, an OS-level thread is created which performs the
blocking event polling (i.e., a `select` call). This allows the NIF to return
quickly and avoid causing the scheduler too much trouble. It would be very bad
to block the scheduler thread until an event is received.
While using threads solves this problem, it unfortunately also introduces new
ones. The bindings implement some locking mechanisms to try to coordinate
threading logic and prevent polling from occurring simultaneously, but this
sort of logic is hard to get right (one of the reasons we use Elixir/Erlang).
No issues are currently known, but please report any you happen to encounter.
#### Timeouts
You might have noticed that there's no binding for `tb_peek_event` (which
accepts a timeout). That's because it's easy enough to implement a timeout
ourselves with `start_polling/1` and `receive` with `after`, e.g.:
{:ok, _resource} = Bindings.start_polling(self())
receive do
{:event, event} ->
# handle the event...
after
1_000 ->
:ok = Bindings.stop_polling(self())
# do something else...
end
"""
alias ExTermbox.{Cell, Constants, Position}
@on_load :load_nifs
def load_nifs do
case :code.priv_dir(:ex_termbox) do
{:error, _} = err ->
err
path ->
path
|> Path.join("termbox_bindings")
|> to_charlist()
|> :erlang.load_nif(0)
end
end
@doc """
Initializes the termbox library. Must be called before any other bindings are
called.
Returns `:ok` on success and otherwise one of the following errors:
* `{:error, :already_running} - the library was already initialized.
* `{:error, code}` - where code is an integer error code from termbox.
"""
@spec init :: :ok | {:error, integer() | :already_running}
def init do
error("NIF init/0 not loaded")
end
@doc """
Finalizes the termbox library. Should be called when the terminal application
is exited, and before your program or OTP application stops.
Returns `:ok` on success and otherwise one of the following errors:
* `{:error, :not_running} - the library can not be shut down because it is not
initialized.
* `{:error, code}` - where `code` is an integer error code from termbox.
"""
@spec shutdown :: :ok | {:error, integer() | :not_running}
def shutdown do
error("NIF shutdown/0 not loaded")
end
@doc """
Returns `{:ok, width}` where `width` is the width of the terminal window in
characters.
If termbox was not initialized, returns `{:error, :not_running}` (call
`init/0` first).
"""
@spec width :: {:ok, integer()} | {:error, :not_running}
def width do
error("NIF width/0 not loaded")
end
@doc """
Returns `{:ok, height}` where `height` is the height of the terminal window in
characters.
If termbox was not initialized, returns `{:error, :not_running}` (call
`init/0` first).
"""
@spec height :: {:ok, integer()} | {:error, :not_running}
def height do
error("NIF height/0 not loaded")
end
@doc """
Clears the internal back buffer, setting the foreground and background to the
defaults, or those specified by `set_clear_attributes/2`.
Returns `:ok` if successful. If termbox was not initialized, returns
`{:error, :not_running}` (call `init/0` first).
"""
@spec clear :: :ok | {:error, :not_running}
def clear do
error("NIF clear/0 not loaded")
end
@doc """
Sets the default foreground and background colors used when `clear/0` is
called.
Returns `:ok` if successful. If termbox was not initialized, returns
`{:error, :not_running}` (call `init/0` first).
"""
@spec set_clear_attributes(Constants.color(), Constants.color()) ::
:ok | {:error, :not_running}
def set_clear_attributes(_fg, _bg) do
error("NIF set_clear_attributes/2 not loaded")
end
@doc """
Synchronizes the internal back buffer and the terminal.
Returns `:ok` if successful. If termbox was not initialized, returns
`{:error, :not_running}` (call `init/0` first).
"""
@spec present :: :ok | {:error, :not_running}
def present do
error("NIF present/0 not loaded")
end
@doc """
Sets the position of the cursor to the coordinates `(x, y)`, or hide the
cursor by passing `ExTermbox.Constants.hide_cursor/0` for both x and y.
Returns `:ok` if successful. If termbox was not initialized, returns
`{:error, :not_running}` (call `init/0` first).
"""
@spec set_cursor(non_neg_integer(), non_neg_integer()) ::
:ok | {:error, :not_running}
def set_cursor(_x, _y) do
error("NIF set_cursor/2 not loaded")
end
@doc """
Puts a cell in the internal back buffer at the cell's position. Note that this is
implemented in terms of `change_cell/5`.
Returns `:ok` if successful. If termbox was not initialized, returns
`{:error, :not_running}` (call `init/0` first).
"""
@spec put_cell(Cell.t()) :: :ok | {:error, :not_running}
def put_cell(%Cell{position: %Position{x: x, y: y}, ch: ch, fg: fg, bg: bg}) do
change_cell(x, y, ch, fg, bg)
end
@doc """
Changes the attributes of the cell at the specified position in the internal
back buffer. Prefer using `put_cell/1`, which supports passing an
`ExTermbox.Cell` struct.
Returns `:ok` if successful. If termbox was not initialized, returns
`{:error, :not_running}` (call `init/0` first).
"""
@spec change_cell(
non_neg_integer(),
non_neg_integer(),
non_neg_integer(),
Constants.color(),
Constants.color()
) :: :ok | {:error, :not_running}
def change_cell(_x, _y, _ch, _fg, _bg) do
error("NIF change_cell/5 not loaded")
end
@doc """
Sets or retrieves the input mode (see `ExTermbox.Constants.input_modes/0`).
See the [termbox source](https://github.com/nsf/termbox/blob/master/src/termbox.h)
for additional documentation.
Returns `{:ok, input_mode}` when successful, where `input_mode` is an integer
representing the current mode. If termbox was not initialized, returns
`{:error, :not_running}` (call `init/0` first).
"""
@spec select_input_mode(Constants.input_mode()) ::
{:ok, integer()} | {:error, :not_running}
def select_input_mode(_mode) do
error("NIF select_input_mode/1 not loaded")
end
@doc """
Sets or retrieves the output mode (see `ExTermbox.Constants.output_modes/0`).
See the [termbox source](https://github.com/nsf/termbox/blob/master/src/termbox.h)
for additional documentation.
Returns `{:ok, output_mode}` when successful, where `output_mode` is an
integer representing the current mode. If termbox was not initialized, returns
`{:error, :not_running}` (call `init/0` first).
"""
@spec select_output_mode(Constants.output_mode()) ::
{:ok, integer()} | {:error, :not_running}
def select_output_mode(_mode) do
error("NIF select_output_mode/1 not loaded")
end
@doc """
Starts polling for terminal events asynchronously. The function accepts a PID
as argument and returns immediately. When an event is received, it's sent to
the specified process. It continues polling until either `stop_polling/0` or
`shutdown/0` is called. An error is returned when this function is called
again before polling has been stopped.
If successful, returns `{:ok, resource}`, where `resource` is an Erlang
resource object representing a handle for the poll thread. Otherwise, one of
the following errors is returned:
* `{:error, :not_running} - termbox should be initialized before events are
polled.
* `{:error, :already_polling}` - `start_polling/1` was previously called and
has not been since stopped.
"""
@spec start_polling(pid()) ::
{:ok, reference()} | {:error, :not_running | :already_polling}
def start_polling(recipient_pid) when is_pid(recipient_pid) do
error("NIF start_polling/1 not loaded")
end
@doc """
Cancels a previous call to `start_polling/1` and blocks until polling has
stopped. The polling loop checks every 10 ms for a stop condition, so calls
can take up to 10 ms to return.
This can be useful, for example, if the `start_polling/1` recipient process
dies and the polling needs to be restarted by another process.
Returns `:ok` on success and otherwise one of the following errors:
* `{:error, :not_running} - termbox should be initialized before any polling
functions are called.
* `{:error, :not_polling} - polling cannot be stopped because it was already
stopped or never started.
"""
@spec stop_polling() :: :ok | {:error, :not_running | :not_polling}
def stop_polling do
error("NIF stop_polling/1 not loaded")
end
defp error(reason), do: :erlang.nif_error(reason)
end