Commit 3ddd85a9 authored by Craig Everett's avatar Craig Everett

Initial commit

parents
{"src/*", [debug_info, {i, "include/"}, {outdir, "ebin/"}]}.
Copyright 2019 Craig Everett <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
{application,termifierg,
[{description,"Create, edit and convert JSON to Erlang terms."},
{registered,[]},
{included_applications,[]},
{applications,[stdlib,kernel]},
{vsn,"0.1.0"},
{modules,[termifierg,tg_con,tg_gui,tg_sup]},
{mod,{termifierg,[]}}]}.
%%% @doc
%%% Termifier GUI
%%% @end
-module(termifierg).
-behavior(application).
-author("Craig Everett <[email protected]>").
-copyright("Craig Everett <[email protected]>").
-license("MIT").
-export([start/2, stop/1]).
-spec start(normal, Args :: term()) -> {ok, pid()}.
%% @private
%% Called by OTP to kick things off. This is for the use of the "application" part of
%% OTP, not to be called by user code.
%%
%% NOTE:
%% The commented out second argument would come from ebin/termifierg.app's 'mod'
%% section, which is difficult to define dynamically so is not used by default
%% here (if you need this, you already know how to change it).
%%
%% Optional runtime arguments passed in at start time can be obtained by calling
%% zx_daemon:argv/0 anywhere in the body of the program.
%%
%% See: http://erlang.org/doc/apps/kernel/application.html
start(normal, _Args) ->
tg_sup:start_link().
-spec stop(term()) -> ok.
%% @private
%% Similar to start/2 above, this is to be called by the "application" part of OTP,
%% not client code. Causes a (hopefully graceful) shutdown of the application.
stop(_State) ->
ok.
%%% @doc
%%% Termifier GUI Controller
%%%
%%% This process is a in charge of maintaining the program's state.
%%% @end
-module(tg_con).
-author("Craig Everett <[email protected]>").
-copyright("Craig Everett <[email protected]>").
-license("MIT").
-behavior(gen_server).
-export([start_link/0, stop/0]).
-export([init/1, terminate/2, code_change/3,
handle_call/3, handle_cast/2, handle_info/2]).
-include("$zx_include/zx_logger.hrl").
%%% Type and Record Definitions
-record(s,
{window = none :: none | wx:wx_object()}).
-type state() :: #s{}.
%% Interface
-spec stop() -> ok.
stop() ->
gen_server:cast(?MODULE, stop).
%%% Startup Functions
-spec start_link() -> Result
when Result :: {ok, pid()}
| {error, Reason},
Reason :: {already_started, pid()}
| {shutdown, term()}
| term().
%% @private
%% Called by tg_sup.
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, none, []).
-spec init(none) -> no_return().
init(none) ->
ok = log(info, "Starting"),
Window = tg_gui:start_link("Hello, WX!"),
ok = log(info, "Window: ~p", [Window]),
State = #s{window = Window},
ArgV = zx_daemon:argv(),
ok = tg_gui:show(ArgV),
{ok, State}.
%%% gen_server Message Handling Callbacks
-spec handle_call(Message, From, State) -> Result
when Message :: term(),
From :: {pid(), reference()},
State :: state(),
Result :: {reply, Response, NewState}
| {noreply, State},
Response :: ok
| {error, {listening, inet:port_number()}},
NewState :: state().
%% @private
%% The gen_server:handle_call/3 callback.
%% See: http://erlang.org/doc/man/gen_server.html#Module:handle_call-3
handle_call(Unexpected, From, State) ->
ok = log(warning, "Unexpected call from ~tp: ~tp~n", [From, Unexpected]),
{noreply, State}.
-spec handle_cast(Message, State) -> {noreply, NewState}
when Message :: term(),
State :: state(),
NewState :: state().
%% @private
%% The gen_server:handle_cast/2 callback.
%% See: http://erlang.org/doc/man/gen_server.html#Module:handle_cast-2
handle_cast(stop, State) ->
ok = log(info, "Received a 'stop' message."),
% {noreply, State};
{stop, normal, State};
handle_cast(Unexpected, State) ->
ok = log(warning, "Unexpected cast: ~tp~n", [Unexpected]),
{noreply, State}.
-spec handle_info(Message, State) -> {noreply, NewState}
when Message :: term(),
State :: state(),
NewState :: state().
%% @private
%% The gen_server:handle_info/2 callback.
%% See: http://erlang.org/doc/man/gen_server.html#Module:handle_info-2
handle_info(Unexpected, State) ->
ok = log(warning, "Unexpected info: ~tp~n", [Unexpected]),
{noreply, State}.
%% @private
%% gen_server callback to handle state transformations necessary for hot
%% code updates. This template performs no transformation.
code_change(_, State, _) ->
{ok, State}.
terminate(Reason, State) ->
ok = log(info, "Reason: ~tp, State: ~tp", [Reason, State]),
zx:stop().
%%% @doc
%%% Termifier GUI GUI
%%%
%%% This process is responsible for creating the main GUI frame displayed to the user.
%%%
%%% Reference: http://erlang.org/doc/man/wx_object.html
%%% @end
-module(tg_gui).
-author("Craig Everett <[email protected]>").
-copyright("Craig Everett <[email protected]>").
-license("MIT").
-behavior(wx_object).
-include_lib("wx/include/wx.hrl").
-export([show/1]).
-export([start_link/1]).
-export([init/1, terminate/2, code_change/3,
handle_call/3, handle_cast/2, handle_info/2, handle_event/2]).
-include("$zx_include/zx_logger.hrl").
-record(s,
{frame = none :: none | wx:wx_object(),
text = none :: none | wx:wx_object()}).
-type state() :: term().
%%% Interface functions
show(Terms) ->
wx_object:cast(?MODULE, {show, Terms}).
%%% Startup Functions
start_link(Title) ->
wx_object:start_link({local, ?MODULE}, ?MODULE, Title, []).
init(Title) ->
ok = log(info, "GUI starting..."),
Wx = wx:new(),
Frame = wxFrame:new(Wx, ?wxID_ANY, Title),
MainSz = wxBoxSizer:new(?wxVERTICAL),
TextC = wxTextCtrl:new(Frame, ?wxID_ANY, [{style, ?wxDEFAULT bor ?wxTE_MULTILINE}]),
wxSizer:add(MainSz, TextC, [{flag, ?wxEXPAND}, {proportion, 1}]),
wxFrame:setSizer(Frame, MainSz),
wxSizer:layout(MainSz),
ok = wxFrame:connect(Frame, close_window),
ok = wxFrame:center(Frame),
true = wxFrame:show(Frame),
State = #s{frame = Frame, text = TextC},
{Frame, State}.
-spec handle_call(Message, From, State) -> Result
when Message :: term(),
From :: {pid(), reference()},
State :: state(),
Result :: {reply, Response, NewState}
| {noreply, State},
Response :: ok
| {error, {listening, inet:port_number()}},
NewState :: state().
handle_call(Unexpected, From, State) ->
ok = log(warning, "Unexpected call from ~tp: ~tp~n", [From, Unexpected]),
{noreply, State}.
-spec handle_cast(Message, State) -> {noreply, NewState}
when Message :: term(),
State :: state(),
NewState :: state().
%% @private
%% The gen_server:handle_cast/2 callback.
%% See: http://erlang.org/doc/man/gen_server.html#Module:handle_cast-2
handle_cast({show, Terms}, State) ->
ok = do_show(Terms, State),
{noreply, State};
handle_cast(Unexpected, State) ->
ok = log(warning, "Unexpected cast: ~tp~n", [Unexpected]),
{noreply, State}.
-spec handle_info(Message, State) -> {noreply, NewState}
when Message :: term(),
State :: state(),
NewState :: state().
%% @private
%% The gen_server:handle_info/2 callback.
%% See: http://erlang.org/doc/man/gen_server.html#Module:handle_info-2
handle_info(Unexpected, State) ->
ok = log(warning, "Unexpected info: ~tp~n", [Unexpected]),
{noreply, State}.
-spec handle_event(Event, State) -> {noreply, NewState}
when Event :: term(),
State :: state(),
NewState :: state().
%% @private
%% The wx_object:handle_event/2 callback.
%% See: http://erlang.org/doc/man/gen_server.html#Module:handle_info-2
handle_event(#wx{event = #wxClose{}}, State = #s{frame = Frame}) ->
ok = tg_con:stop(),
ok = wxWindow:destroy(Frame),
{noreply, State};
handle_event(Event, State) ->
ok = log(info, "Unexpected event ~tp State: ~tp~n", [Event, State]),
{noreply, State}.
code_change(_, State, _) ->
{ok, State}.
terminate(Reason, State) ->
ok = log(info, "Reason: ~tp, State: ~tp", [Reason, State]),
wx:destroy().
do_show(Terms, #s{text = TextC}) ->
String = io_lib:format("Received args: ~tp", [Terms]),
wxTextCtrl:changeValue(TextC, String).
%%% @doc
%%% Termifier GUI Top-level Supervisor
%%%
%%% The very top level supervisor in the system. It only has one service branch: the
%%% "con" (program controller). The con is the
%%% only be one part of a larger system. Were this a game system, for example, the
%%% item data management service would be a peer, as would a login credential provision
%%% service, game world event handling, and so on.
%%%
%%% See: http://erlang.org/doc/design_principles/applications.html
%%% See: http://zxq9.com/archives/1311
%%% @end
-module(tg_sup).
-behaviour(supervisor).
-author("Craig Everett <[email protected]>").
-copyright("Craig Everett <[email protected]>").
-license("MIT").
-export([start_link/0]).
-export([init/1]).
-spec start_link() -> {ok, pid()}.
%% @private
%% This supervisor's own start function.
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
-spec init([]) -> {ok, {supervisor:sup_flags(), [supervisor:child_spec()]}}.
%% @private
%% The OTP init/1 function.
init([]) ->
RestartStrategy = {one_for_one, 0, 60},
Clients = {tg_con,
{tg_con, start_link, []},
permanent,
5000,
worker,
[tg_con]},
Children = [Clients],
{ok, {RestartStrategy, Children}}.
{a_email,"[email protected]"}.
{author,"Craig Everett"}.
{c_email,"[email protected]"}.
{copyright,"Craig Everett"}.
{deps,[]}.
{desc,"Create, edit and convert JSON to Erlang terms."}.
{key_name,none}.
{license,"MIT"}.
{modules,[]}.
{name,"Termifier GUI"}.
{package_id,{"otpr","termifierg",{0,1,0}}}.
{prefix,"tg_"}.
{repo_url,[]}.
{tags,[]}.
{type,gui}.
{ws_url,[]}.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment