From 30cd5551c4cdc7a2d9bbc92dfbc0313764d2bdad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Fri, 5 Jan 2018 16:32:06 +0100 Subject: Add the remaining keyboard functions Also adds defines for all the keycode and scancode in SDL 2.0.7. --- c_src/esdl2.h | 8 ++++ c_src/sdl_keyboard.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++++--- c_src/sdl_rect.c | 57 ++++++++++++++++++++++++++++ c_src/sdl_renderer.c | 42 --------------------- 4 files changed, 163 insertions(+), 47 deletions(-) create mode 100644 c_src/sdl_rect.c (limited to 'c_src') diff --git a/c_src/esdl2.h b/c_src/esdl2.h index 7fa237f..31a1fcc 100644 --- a/c_src/esdl2.h +++ b/c_src/esdl2.h @@ -272,12 +272,17 @@ F(get_key_from_name, 1) \ F(get_key_from_scancode, 1) \ F(get_key_name, 1) \ + F(get_keyboard_focus, 0) \ + F(get_keyboard_state, 0) \ F(get_mod_state, 0) \ F(get_scancode_from_key, 1) \ F(get_scancode_from_name, 1) \ F(get_scancode_name, 1) \ + F(has_screen_keyboard_support, 0) \ + F(is_screen_keyboard_shown, 1) \ F(is_text_input_active, 0) \ F(set_mod_state, 1) \ + F(set_text_input_rect, 1) \ F(start_text_input, 0) \ F(stop_text_input, 0) \ /* sdl_mouse */ \ @@ -384,6 +389,9 @@ NIF_ENUM_TO_ATOM_FUNCTION_DECL(window_event_to_atom, Uint8) NIF_LIST_TO_FLAGS_FUNCTION_DECL(keymod_list_to_flags, Uint16) NIF_FLAGS_TO_LIST_FUNCTION_DECL(keymod_flags_to_list, Uint16) +int map_to_point(ErlNifEnv*, ERL_NIF_TERM, SDL_Point*); +int map_to_rect(ErlNifEnv*, ERL_NIF_TERM, SDL_Rect*); + ERL_NIF_TERM mouse_state_to_list(ErlNifEnv*, Uint32); // -- diff --git a/c_src/sdl_keyboard.c b/c_src/sdl_keyboard.c index 0e961a8..f33a69d 100644 --- a/c_src/sdl_keyboard.c +++ b/c_src/sdl_keyboard.c @@ -147,8 +147,51 @@ NIF_FUNCTION(get_scancode_name) return enif_make_binary(env, &bin); } -// @todo get_keyboard_focus -// @todo get_keyboard_state +// get_keyboard_focus + +NIF_CALL_HANDLER(thread_get_keyboard_focus) +{ + SDL_Window* window; + + window = SDL_GetKeyboardFocus(); + + if (!window) + return atom_undefined; + + return esdl2_windows_find(env, window); +} + +NIF_FUNCTION(get_keyboard_focus) +{ + return nif_thread_call(env, thread_get_keyboard_focus, 0); +} + +// get_keyboard_state + +NIF_CALL_HANDLER(thread_get_keyboard_state) +{ + const Uint8* state; + int i, numkeys; + ERL_NIF_TERM map; + + state = SDL_GetKeyboardState(&numkeys); + + map = enif_make_new_map(env); + + for (i = 0; i < numkeys; i++) { + enif_make_map_put(env, map, + enif_make_int(env, i), + state[i] ? atom_true : atom_false, + &map); + } + + return map; +} + +NIF_FUNCTION(get_keyboard_state) +{ + return nif_thread_call(env, thread_get_keyboard_state, 0); +} // get_mod_state @@ -166,8 +209,40 @@ NIF_FUNCTION(get_mod_state) return nif_thread_call(env, thread_get_mod_state, 0); } -// @todo has_screen_keyboard_support -// @todo is_screen_keyboard_shown +// has_screen_keyboard_support + +NIF_CALL_HANDLER(thread_has_screen_keyboard_support) +{ + if (SDL_HasScreenKeyboardSupport()) + return atom_true; + + return atom_false; +} + +NIF_FUNCTION(has_screen_keyboard_support) +{ + return nif_thread_call(env, thread_has_screen_keyboard_support, 0); +} + +// is_screen_keyboard_shown + +NIF_CALL_HANDLER(thread_is_screen_keyboard_shown) +{ + if (SDL_IsScreenKeyboardShown(args[0])) + return atom_true; + + return atom_false; +} + +NIF_FUNCTION(is_screen_keyboard_shown) +{ + void* window_res; + + BADARG_IF(!enif_get_resource(env, argv[0], res_Window, &window_res)); + + return nif_thread_call(env, thread_is_screen_keyboard_shown, 1, + NIF_RES_GET(Window, window_res)); +} // is_text_input_active @@ -200,7 +275,25 @@ NIF_FUNCTION(set_mod_state) return nif_thread_cast(env, thread_set_mod_state, 1, mod); } -// @todo set_text_input_rect +// set_text_input_rect + +NIF_CAST_HANDLER(thread_set_text_input_rect) +{ + SDL_SetTextInputRect(args[0]); +} + +NIF_FUNCTION(set_text_input_rect) +{ + SDL_Rect* rect = NULL; + + rect = (SDL_Rect*)enif_alloc(sizeof(SDL_Rect)); + if (!map_to_rect(env, argv[0], rect)) { + enif_free(rect); + return enif_make_badarg(env); + } + + return nif_thread_cast(env, thread_set_text_input_rect, 1, rect); +} // start_text_input diff --git a/c_src/sdl_rect.c b/c_src/sdl_rect.c new file mode 100644 index 0000000..eb04d5b --- /dev/null +++ b/c_src/sdl_rect.c @@ -0,0 +1,57 @@ +// Copyright (c) 2014-2018, Loïc Hoguin +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +#include "esdl2.h" + +int map_to_point(ErlNifEnv* env, ERL_NIF_TERM map, SDL_Point* point) +{ + ERL_NIF_TERM x, y; + + if (!enif_get_map_value(env, map, atom_x, &x)) + return 0; + if (!enif_get_map_value(env, map, atom_y, &y)) + return 0; + + if (!enif_get_int(env, x, &point->x)) + return 0; + if (!enif_get_int(env, y, &point->y)) + return 0; + + return 1; +} + +int map_to_rect(ErlNifEnv* env, ERL_NIF_TERM map, SDL_Rect* rect) +{ + ERL_NIF_TERM x, y, w, h; + + if (!enif_get_map_value(env, map, atom_x, &x)) + return 0; + if (!enif_get_map_value(env, map, atom_y, &y)) + return 0; + if (!enif_get_map_value(env, map, atom_w, &w)) + return 0; + if (!enif_get_map_value(env, map, atom_h, &h)) + return 0; + + if (!enif_get_int(env, x, &rect->x)) + return 0; + if (!enif_get_int(env, y, &rect->y)) + return 0; + if (!enif_get_int(env, w, &rect->w)) + return 0; + if (!enif_get_int(env, h, &rect->h)) + return 0; + + return 1; +} diff --git a/c_src/sdl_renderer.c b/c_src/sdl_renderer.c index 17eaa07..60bfe85 100644 --- a/c_src/sdl_renderer.c +++ b/c_src/sdl_renderer.c @@ -37,48 +37,6 @@ static NIF_LIST_TO_FLAGS_FUNCTION(list_to_renderer_flags, Uint32, RENDERER_FLAGS static NIF_LIST_TO_FLAGS_FUNCTION(list_to_flip_flags, SDL_RendererFlip, FLIP_FLAGS) -static int map_to_point(ErlNifEnv* env, ERL_NIF_TERM map, SDL_Point* point) -{ - ERL_NIF_TERM x, y; - - if (!enif_get_map_value(env, map, atom_x, &x)) - return 0; - if (!enif_get_map_value(env, map, atom_y, &y)) - return 0; - - if (!enif_get_int(env, x, &point->x)) - return 0; - if (!enif_get_int(env, y, &point->y)) - return 0; - - return 1; -} - -static int map_to_rect(ErlNifEnv* env, ERL_NIF_TERM map, SDL_Rect* rect) -{ - ERL_NIF_TERM x, y, w, h; - - if (!enif_get_map_value(env, map, atom_x, &x)) - return 0; - if (!enif_get_map_value(env, map, atom_y, &y)) - return 0; - if (!enif_get_map_value(env, map, atom_w, &w)) - return 0; - if (!enif_get_map_value(env, map, atom_h, &h)) - return 0; - - if (!enif_get_int(env, x, &rect->x)) - return 0; - if (!enif_get_int(env, y, &rect->y)) - return 0; - if (!enif_get_int(env, w, &rect->w)) - return 0; - if (!enif_get_int(env, h, &rect->h)) - return 0; - - return 1; -} - // create_renderer NIF_CALL_HANDLER(thread_create_renderer) -- cgit v1.2.3