Skip to content

Commit 30cd555

Browse files
committed
Add the remaining keyboard functions
Also adds defines for all the keycode and scancode in SDL 2.0.7.
1 parent 435845a commit 30cd555

12 files changed

+778
-71
lines changed

README.asciidoc

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ corresponding to the public headers.
1111
* 'SDL.h'
1212
* 'SDL_blendmode.h'
1313
* 'SDL_cpuinfo.h'
14-
* 'SDL_mouse.h'
1514
* 'SDL_filesystem.h'
15+
* 'SDL_keyboard.h'
16+
* 'SDL_keycode.h'
17+
* 'SDL_mouse.h'
1618
* 'SDL_power.h'
19+
* 'SDL_scancode.h'
1720

1821
== Partially implemented
1922

@@ -57,7 +60,7 @@ corresponding to the public headers.
5760
** `SDL_EventState` and `SDL_GetEventState`
5861
** `SDL_RegisterEvents`
5962
* 'SDL_hints.h': We only have a proof of concept callback system.
60-
* 'SDL_keyboard.h': A few more functions are missing.
63+
* 'SDL_rect.h': Everything is missing except the map to point/rect conversion functions.
6164
* 'SDL_render.h': The following elements are missing:
6265
** `SDL_TextureAccess` enum
6366
** `SDL_TextureModulate` enum
@@ -149,13 +152,10 @@ corresponding to the public headers.
149152
* 'SDL_gesture.h'
150153
* 'SDL_haptic.h'
151154
* 'SDL_joystick.h'
152-
* 'SDL_keycode.h' (have a header containing the values)
153155
* 'SDL_messagebox.h'
154156
* 'SDL_pixels.h'
155157
* 'SDL_platform.h'
156-
* 'SDL_rect.h' (though we have a rect data type in sdl_renderer)
157158
* 'SDL_rwops.h' (unclear if we need it)
158-
* 'SDL_scancode.h' (have a header containing the values)
159159
* 'SDL_shape.h'
160160
* 'SDL_system.h'
161161
* 'SDL_syswm.h'

c_src/esdl2.h

+8
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,17 @@
272272
F(get_key_from_name, 1) \
273273
F(get_key_from_scancode, 1) \
274274
F(get_key_name, 1) \
275+
F(get_keyboard_focus, 0) \
276+
F(get_keyboard_state, 0) \
275277
F(get_mod_state, 0) \
276278
F(get_scancode_from_key, 1) \
277279
F(get_scancode_from_name, 1) \
278280
F(get_scancode_name, 1) \
281+
F(has_screen_keyboard_support, 0) \
282+
F(is_screen_keyboard_shown, 1) \
279283
F(is_text_input_active, 0) \
280284
F(set_mod_state, 1) \
285+
F(set_text_input_rect, 1) \
281286
F(start_text_input, 0) \
282287
F(stop_text_input, 0) \
283288
/* sdl_mouse */ \
@@ -384,6 +389,9 @@ NIF_ENUM_TO_ATOM_FUNCTION_DECL(window_event_to_atom, Uint8)
384389
NIF_LIST_TO_FLAGS_FUNCTION_DECL(keymod_list_to_flags, Uint16)
385390
NIF_FLAGS_TO_LIST_FUNCTION_DECL(keymod_flags_to_list, Uint16)
386391

392+
int map_to_point(ErlNifEnv*, ERL_NIF_TERM, SDL_Point*);
393+
int map_to_rect(ErlNifEnv*, ERL_NIF_TERM, SDL_Rect*);
394+
387395
ERL_NIF_TERM mouse_state_to_list(ErlNifEnv*, Uint32);
388396

389397
// --

c_src/sdl_keyboard.c

+98-5
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,51 @@ NIF_FUNCTION(get_scancode_name)
147147
return enif_make_binary(env, &bin);
148148
}
149149

150-
// @todo get_keyboard_focus
151-
// @todo get_keyboard_state
150+
// get_keyboard_focus
151+
152+
NIF_CALL_HANDLER(thread_get_keyboard_focus)
153+
{
154+
SDL_Window* window;
155+
156+
window = SDL_GetKeyboardFocus();
157+
158+
if (!window)
159+
return atom_undefined;
160+
161+
return esdl2_windows_find(env, window);
162+
}
163+
164+
NIF_FUNCTION(get_keyboard_focus)
165+
{
166+
return nif_thread_call(env, thread_get_keyboard_focus, 0);
167+
}
168+
169+
// get_keyboard_state
170+
171+
NIF_CALL_HANDLER(thread_get_keyboard_state)
172+
{
173+
const Uint8* state;
174+
int i, numkeys;
175+
ERL_NIF_TERM map;
176+
177+
state = SDL_GetKeyboardState(&numkeys);
178+
179+
map = enif_make_new_map(env);
180+
181+
for (i = 0; i < numkeys; i++) {
182+
enif_make_map_put(env, map,
183+
enif_make_int(env, i),
184+
state[i] ? atom_true : atom_false,
185+
&map);
186+
}
187+
188+
return map;
189+
}
190+
191+
NIF_FUNCTION(get_keyboard_state)
192+
{
193+
return nif_thread_call(env, thread_get_keyboard_state, 0);
194+
}
152195

153196
// get_mod_state
154197

@@ -166,8 +209,40 @@ NIF_FUNCTION(get_mod_state)
166209
return nif_thread_call(env, thread_get_mod_state, 0);
167210
}
168211

169-
// @todo has_screen_keyboard_support
170-
// @todo is_screen_keyboard_shown
212+
// has_screen_keyboard_support
213+
214+
NIF_CALL_HANDLER(thread_has_screen_keyboard_support)
215+
{
216+
if (SDL_HasScreenKeyboardSupport())
217+
return atom_true;
218+
219+
return atom_false;
220+
}
221+
222+
NIF_FUNCTION(has_screen_keyboard_support)
223+
{
224+
return nif_thread_call(env, thread_has_screen_keyboard_support, 0);
225+
}
226+
227+
// is_screen_keyboard_shown
228+
229+
NIF_CALL_HANDLER(thread_is_screen_keyboard_shown)
230+
{
231+
if (SDL_IsScreenKeyboardShown(args[0]))
232+
return atom_true;
233+
234+
return atom_false;
235+
}
236+
237+
NIF_FUNCTION(is_screen_keyboard_shown)
238+
{
239+
void* window_res;
240+
241+
BADARG_IF(!enif_get_resource(env, argv[0], res_Window, &window_res));
242+
243+
return nif_thread_call(env, thread_is_screen_keyboard_shown, 1,
244+
NIF_RES_GET(Window, window_res));
245+
}
171246

172247
// is_text_input_active
173248

@@ -200,7 +275,25 @@ NIF_FUNCTION(set_mod_state)
200275
return nif_thread_cast(env, thread_set_mod_state, 1, mod);
201276
}
202277

203-
// @todo set_text_input_rect
278+
// set_text_input_rect
279+
280+
NIF_CAST_HANDLER(thread_set_text_input_rect)
281+
{
282+
SDL_SetTextInputRect(args[0]);
283+
}
284+
285+
NIF_FUNCTION(set_text_input_rect)
286+
{
287+
SDL_Rect* rect = NULL;
288+
289+
rect = (SDL_Rect*)enif_alloc(sizeof(SDL_Rect));
290+
if (!map_to_rect(env, argv[0], rect)) {
291+
enif_free(rect);
292+
return enif_make_badarg(env);
293+
}
294+
295+
return nif_thread_cast(env, thread_set_text_input_rect, 1, rect);
296+
}
204297

205298
// start_text_input
206299

c_src/sdl_rect.c

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) 2014-2018, Loïc Hoguin <[email protected]>
2+
//
3+
// Permission to use, copy, modify, and/or distribute this software for any
4+
// purpose with or without fee is hereby granted, provided that the above
5+
// copyright notice and this permission notice appear in all copies.
6+
//
7+
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8+
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10+
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12+
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13+
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14+
15+
#include "esdl2.h"
16+
17+
int map_to_point(ErlNifEnv* env, ERL_NIF_TERM map, SDL_Point* point)
18+
{
19+
ERL_NIF_TERM x, y;
20+
21+
if (!enif_get_map_value(env, map, atom_x, &x))
22+
return 0;
23+
if (!enif_get_map_value(env, map, atom_y, &y))
24+
return 0;
25+
26+
if (!enif_get_int(env, x, &point->x))
27+
return 0;
28+
if (!enif_get_int(env, y, &point->y))
29+
return 0;
30+
31+
return 1;
32+
}
33+
34+
int map_to_rect(ErlNifEnv* env, ERL_NIF_TERM map, SDL_Rect* rect)
35+
{
36+
ERL_NIF_TERM x, y, w, h;
37+
38+
if (!enif_get_map_value(env, map, atom_x, &x))
39+
return 0;
40+
if (!enif_get_map_value(env, map, atom_y, &y))
41+
return 0;
42+
if (!enif_get_map_value(env, map, atom_w, &w))
43+
return 0;
44+
if (!enif_get_map_value(env, map, atom_h, &h))
45+
return 0;
46+
47+
if (!enif_get_int(env, x, &rect->x))
48+
return 0;
49+
if (!enif_get_int(env, y, &rect->y))
50+
return 0;
51+
if (!enif_get_int(env, w, &rect->w))
52+
return 0;
53+
if (!enif_get_int(env, h, &rect->h))
54+
return 0;
55+
56+
return 1;
57+
}

c_src/sdl_renderer.c

-42
Original file line numberDiff line numberDiff line change
@@ -37,48 +37,6 @@ static NIF_LIST_TO_FLAGS_FUNCTION(list_to_renderer_flags, Uint32, RENDERER_FLAGS
3737

3838
static NIF_LIST_TO_FLAGS_FUNCTION(list_to_flip_flags, SDL_RendererFlip, FLIP_FLAGS)
3939

40-
static int map_to_point(ErlNifEnv* env, ERL_NIF_TERM map, SDL_Point* point)
41-
{
42-
ERL_NIF_TERM x, y;
43-
44-
if (!enif_get_map_value(env, map, atom_x, &x))
45-
return 0;
46-
if (!enif_get_map_value(env, map, atom_y, &y))
47-
return 0;
48-
49-
if (!enif_get_int(env, x, &point->x))
50-
return 0;
51-
if (!enif_get_int(env, y, &point->y))
52-
return 0;
53-
54-
return 1;
55-
}
56-
57-
static int map_to_rect(ErlNifEnv* env, ERL_NIF_TERM map, SDL_Rect* rect)
58-
{
59-
ERL_NIF_TERM x, y, w, h;
60-
61-
if (!enif_get_map_value(env, map, atom_x, &x))
62-
return 0;
63-
if (!enif_get_map_value(env, map, atom_y, &y))
64-
return 0;
65-
if (!enif_get_map_value(env, map, atom_w, &w))
66-
return 0;
67-
if (!enif_get_map_value(env, map, atom_h, &h))
68-
return 0;
69-
70-
if (!enif_get_int(env, x, &rect->x))
71-
return 0;
72-
if (!enif_get_int(env, y, &rect->y))
73-
return 0;
74-
if (!enif_get_int(env, w, &rect->w))
75-
return 0;
76-
if (!enif_get_int(env, h, &rect->h))
77-
return 0;
78-
79-
return 1;
80-
}
81-
8240
// create_renderer
8341

8442
NIF_CALL_HANDLER(thread_create_renderer)

ebin/esdl2.app

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{application, 'esdl2', [
22
{description, "SDL2 Erlang NIF."},
33
{vsn, "0.1.0"},
4-
{modules, ['esdl2','esdl2_app','esdl2_callbacks','esdl2_sup','sdl','sdl_blend_mode','sdl_clipboard','sdl_cpu_info','sdl_cursor','sdl_events','sdl_filesystem','sdl_gl','sdl_hints','sdl_keyboard','sdl_keycode','sdl_mouse','sdl_power','sdl_renderer','sdl_surface','sdl_texture','sdl_version','sdl_window']},
4+
{modules, ['esdl2','esdl2_app','esdl2_callbacks','esdl2_sup','sdl','sdl_blend_mode','sdl_clipboard','sdl_cpu_info','sdl_cursor','sdl_events','sdl_filesystem','sdl_gl','sdl_hints','sdl_keyboard','sdl_keycode','sdl_mouse','sdl_power','sdl_rect','sdl_renderer','sdl_surface','sdl_texture','sdl_version','sdl_window']},
55
{registered, [esdl2_sup]},
66
{applications, [kernel,stdlib]},
77
{mod, {esdl2_app, []}},

0 commit comments

Comments
 (0)