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. --- src/esdl2_callbacks.erl | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/esdl2_callbacks.erl (limited to 'src/esdl2_callbacks.erl') 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}. -- cgit v1.2.3