From 276a44441d8795cd29a215dff35ab6aefcdc6557 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Wed, 21 Oct 2015 03:41:03 +0200 Subject: Add sdl_hints:add_callback/3 function This also sets up esdl2 to start accepting callbacks. The module/process esdl2_callbacks must always be running for that purpose, so esdl2 was made an OTP application instead of a simple library. Implementation of the rest of SDL_hints will follow in subsequent commits. --- c_src/esdl2.h | 7 ++++ c_src/internal.c | 35 +++++++++++++++++ c_src/sdl_hints.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++ src/esdl2.erl | 16 ++++++++ src/esdl2_app.erl | 25 ++++++++++++ src/esdl2_callbacks.erl | 64 ++++++++++++++++++++++++++++++ src/esdl2_sup.erl | 27 +++++++++++++ src/sdl.erl | 21 ++++++++-- src/sdl_hints.erl | 30 ++++++++++++++ 9 files changed, 323 insertions(+), 3 deletions(-) create mode 100644 c_src/internal.c create mode 100644 c_src/sdl_hints.c create mode 100644 src/esdl2_app.erl create mode 100644 src/esdl2_callbacks.erl create mode 100644 src/esdl2_sup.erl create mode 100644 src/sdl_hints.erl diff --git a/c_src/esdl2.h b/c_src/esdl2.h index 530abc4..df16664 100644 --- a/c_src/esdl2.h +++ b/c_src/esdl2.h @@ -27,6 +27,7 @@ A(blend) \ A(borderless) \ A(button) \ + A(callback) \ A(caps) \ A(centered) \ A(charged) \ @@ -133,6 +134,8 @@ #define NIF_FUNCTION_NAME(f) esdl2_ ## f #define NIF_FUNCTIONS(F) \ + /* internal */ \ + F(register_callback_process, 0) \ /* sdl */ \ F(init, 1) \ F(init_subsystem, 1) \ @@ -166,6 +169,8 @@ /* sdl_gl */ \ F(gl_create_context, 1) \ F(gl_swap_window, 1) \ + /* sdl_hints */ \ + F(add_hint_callback, 3) \ /* sdl_keyboard */ \ F(is_text_input_active, 0) \ F(start_text_input, 0) \ @@ -254,6 +259,8 @@ NIF_FUNCTIONS(NIF_FUNCTION_H_DECL) // -- +ErlNifPid* get_callback_process(); + #define sdl_error_tuple(env) \ enif_make_tuple2(env, \ atom_error, \ diff --git a/c_src/internal.c b/c_src/internal.c new file mode 100644 index 0000000..0c58476 --- /dev/null +++ b/c_src/internal.c @@ -0,0 +1,35 @@ +// Copyright (c) 2015, 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" + +// @todo Make thread-safe. + +// register_callback_process + +ErlNifPid callback_process; + +NIF_FUNCTION(register_callback_process) +{ + BADARG_IF(!enif_self(env, &callback_process)); + + return atom_ok; +} + +// get_callback_process + +ErlNifPid* get_callback_process() +{ + return &callback_process; +} diff --git a/c_src/sdl_hints.c b/c_src/sdl_hints.c new file mode 100644 index 0000000..fe4eceb --- /dev/null +++ b/c_src/sdl_hints.c @@ -0,0 +1,101 @@ +// Copyright (c) 2015, 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" + +// SDL_HintCallback + +typedef struct esdl2_callback { + char* module; + char* function; +} esdl2_callback; + +void esdl2_hint_callback(void* userdata, const char* name, const char* oldValue, const char* newValue) +{ + ErlNifEnv* env = enif_alloc_env(); + esdl2_callback* callback = (esdl2_callback*)userdata; + ERL_NIF_TERM old; + ERL_NIF_TERM new; + + if (oldValue == NULL) + old = atom_undefined; + else + old = enif_make_string(env, oldValue, ERL_NIF_LATIN1); + + if (newValue == NULL) + new = atom_undefined; + else + new = enif_make_string(env, newValue, ERL_NIF_LATIN1); + + enif_send(NULL, get_callback_process(), env, + enif_make_tuple4(env, atom_callback, + enif_make_atom(env, callback->module), + enif_make_atom(env, callback->function), + enif_make_list3(env, + enif_make_string(env, name, ERL_NIF_LATIN1), + old, + new))); + + enif_free_env(env); +} + +// add_hint_callback +// @todo We must free the userdata in SDL_DelHintCallback. + +NIF_CAST_HANDLER(thread_add_hint_callback) +{ + esdl2_callback* callback = (esdl2_callback*)enif_alloc(sizeof(esdl2_callback)); + + callback->module = args[1]; + callback->function = args[2]; + + SDL_AddHintCallback(args[0], &esdl2_hint_callback, callback); + + enif_free(args[0]); +} + +NIF_FUNCTION(add_hint_callback) +{ + unsigned int hint_len, module_len, function_len; + char* hint; + char* module; + char* function; + + BADARG_IF(!enif_get_list_length(env, argv[0], &hint_len)); + BADARG_IF(!enif_get_atom_length(env, argv[1], &module_len, ERL_NIF_LATIN1)); + BADARG_IF(!enif_get_atom_length(env, argv[2], &function_len, ERL_NIF_LATIN1)); + + hint = (char*)enif_alloc(hint_len + 1); + if (!enif_get_string(env, argv[0], hint, hint_len + 1, ERL_NIF_LATIN1)) { + enif_free(hint); + return enif_make_badarg(env); + } + + module = (char*)enif_alloc(module_len + 1); + if (!enif_get_atom(env, argv[1], module, module_len + 1, ERL_NIF_LATIN1)) { + enif_free(hint); + enif_free(module); + return enif_make_badarg(env); + } + + function = (char*)enif_alloc(function_len + 1); + if (!enif_get_atom(env, argv[2], function, function_len + 1, ERL_NIF_LATIN1)) { + enif_free(hint); + enif_free(module); + enif_free(function); + return enif_make_badarg(env); + } + + return nif_thread_cast(env, thread_add_hint_callback, 3, hint, module, function); +} diff --git a/src/esdl2.erl b/src/esdl2.erl index 3e0db38..7bacb71 100644 --- a/src/esdl2.erl +++ b/src/esdl2.erl @@ -14,6 +14,9 @@ -module(esdl2). +%% internal +-export([register_callback_process/0]). + %% sdl -export([init/1]). -export([init_subsystem/1]). @@ -53,6 +56,9 @@ -export([gl_create_context/1]). -export([gl_swap_window/1]). +%% sdl_hints +-export([add_hint_callback/3]). + %% sdl_keyboard -export([is_text_input_active/0]). -export([start_text_input/0]). @@ -137,6 +143,8 @@ -export([set_window_title/2]). -export([show_window/1]). +%% internal + %% @todo We probably want to accept an env variable or somthing for the location. -on_load(on_load/0). on_load() -> @@ -149,6 +157,9 @@ on_load() -> end, erlang:load_nif(filename:join(PrivDir, atom_to_list(?MODULE)), 0). +register_callback_process() -> + erlang:nif_error({not_loaded, ?MODULE}). + %% sdl init(_) -> @@ -242,6 +253,11 @@ gl_create_context(_) -> gl_swap_window(_) -> erlang:nif_error({not_loaded, ?MODULE}). +%% sdl_hints + +add_hint_callback(_, _, _) -> + erlang:nif_error({not_loaded, ?MODULE}). + %% sdl_keyboard is_text_input_active() -> diff --git a/src/esdl2_app.erl b/src/esdl2_app.erl new file mode 100644 index 0000000..2e90bcb --- /dev/null +++ b/src/esdl2_app.erl @@ -0,0 +1,25 @@ +%% Copyright (c) 2015, 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. + +-module(esdl2_app). +-behaviour(application). + +-export([start/2]). +-export([stop/1]). + +start(_, _) -> + esdl2_sup:start_link(). + +stop(_) -> + ok. diff --git a/src/esdl2_callbacks.erl b/src/esdl2_callbacks.erl new file mode 100644 index 0000000..d683992 --- /dev/null +++ b/src/esdl2_callbacks.erl @@ -0,0 +1,64 @@ +%% Copyright (c) 2015, 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. + +-module(esdl2_callbacks). +-behaviour(gen_server). + +%% API. +-export([start_link/0]). + +%% gen_server. +-export([init/1]). +-export([handle_call/3]). +-export([handle_cast/2]). +-export([handle_info/2]). +-export([terminate/2]). +-export([code_change/3]). + +-record(state, { +}). + +%% API. + +-spec start_link() -> {ok, pid()}. +start_link() -> + gen_server:start_link(?MODULE, [], []). + +%% gen_server. + +init([]) -> + ok = esdl2:register_callback_process(), + {ok, #state{}}. + +handle_call(_Request, _From, State) -> + {reply, ignored, State}. + +handle_cast(_Msg, State) -> + {noreply, State}. + +handle_info({callback, M, F, A}, State) -> + try + apply(M, F, A) + catch Class:Reason -> + error_logger:error_msg("Exception ~p:~p with callback:~n{~p,~p,~p}~n", [Class, Reason, M, F, A]) + end, + {noreply, State}; +handle_info(_Info, State) -> + {noreply, State}. + +terminate(_Reason, _State) -> + ok. + +code_change(_OldVsn, State, _Extra) -> + {ok, State}. diff --git a/src/esdl2_sup.erl b/src/esdl2_sup.erl new file mode 100644 index 0000000..bacda01 --- /dev/null +++ b/src/esdl2_sup.erl @@ -0,0 +1,27 @@ +%% Copyright (c) 2015, 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. + +-module(esdl2_sup). +-behaviour(supervisor). + +-export([start_link/0]). +-export([init/1]). + +start_link() -> + supervisor:start_link({local, ?MODULE}, ?MODULE, []). + +init([]) -> + {ok, {#{}, [ + #{id => esdl2_callbacks, start => {esdl2_callbacks, start_link, []}} + ]}}. diff --git a/src/sdl.erl b/src/sdl.erl index 5391d32..8706eff 100644 --- a/src/sdl.erl +++ b/src/sdl.erl @@ -33,10 +33,25 @@ start() -> start([]). --spec start([subsystem()]) -> ok | error(). +-spec start([subsystem()]) -> ok | {application_start_error, term()} | error(). start(Subsystems) -> - esdl2:init(Subsystems), - receive {'_nif_thread_ret_', Ret} -> Ret end. + case ensure_started() of + ok -> + esdl2:init(Subsystems), + receive {'_nif_thread_ret_', Ret} -> Ret end; + Error -> + Error + end. + +ensure_started() -> + case application:start(esdl2) of + ok -> + ok; + {error, {already_started, esdl2}} -> + ok; + {error, Reason} -> + {application_start_error, Reason} + end. -spec stop() -> ok. stop() -> diff --git a/src/sdl_hints.erl b/src/sdl_hints.erl new file mode 100644 index 0000000..a6921b0 --- /dev/null +++ b/src/sdl_hints.erl @@ -0,0 +1,30 @@ +%% Copyright (c) 2015, 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. + +-module(sdl_hints). + +-export([add_callback/3]). +-export([test_callback/3]). + +%% The hint names are the same as the environment variable names +%% that SDL2 accepts. + +-spec add_callback(string(), module(), atom()) -> ok. +add_callback(Hint, Module, Function) -> + esdl2:add_hint_callback(Hint, Module, Function). + +%% This callback can be used to test hints. +-spec test_callback(string(), undefined | string(), undefined | string()) -> ok. +test_callback(Hint, OldValue, NewValue) -> + io:format("Hint ~p has value changed from ~p to ~p~n", [Hint, OldValue, NewValue]). -- cgit v1.2.3