Skip to content

Commit

Permalink
#393 Application scripting (add script engine parallel launch)
Browse files Browse the repository at this point in the history
  • Loading branch information
o-sdn-o committed Jun 1, 2023
1 parent b08bcea commit 4dc5e77
Show file tree
Hide file tree
Showing 7 changed files with 528 additions and 16 deletions.
21 changes: 20 additions & 1 deletion src/netxs/desktopio/console.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "input.hpp"
#include "system.hpp"
#include "scripting.hpp"

namespace netxs::ui
{
Expand Down Expand Up @@ -3716,6 +3717,7 @@ namespace netxs::ui
using tick = datetime::quartz<events::reactor<>, hint>;
using list = std::vector<rect>;
using gptr = sptr<gate>;
using repl = scripting::repl<host>;

//pro::keybd keybd{*this }; // host: Keyboard controller.
pro::mouse mouse{*this }; // host: Mouse controller.
Expand All @@ -3727,6 +3729,7 @@ namespace netxs::ui
xmls config; // host: Running configuration.
gptr client; // host: Standalone app.
subs tokens; // host: Subscription tokens.
repl engine; // host:: Scripting engine.

std::vector<bool> user_numbering; // host: .

Expand All @@ -3739,10 +3742,26 @@ namespace netxs::ui
host(sptr<pipe> server, xmls config, pro::focus::mode m = pro::focus::mode::hub)
: focus{*this, m, faux },
quartz{ bell::router<tier::general>(), e2::timer::tick.id },
config{ config }
config{ config },
engine{ *this }
{
using namespace std::chrono;
auto& canal = *server;

config.pushd("/config/scripting/");
if (config.take("enabled", faux))
{
auto lang = config.take("engine", ""s);
config.cd(lang);
auto path = config.take("cwd", ""s);
auto exec = config.take("cmd", ""s);
auto main = config.take("main", ""s);
engine.start(path, exec);
engine.write(main + "\n");
//todo run integration script
}
config.popd();

auto& g = skin::globals();
g.brighter = config.take("brighter" , cell{});//120);
g.kb_focus = config.take("kb_focus" , cell{});//60
Expand Down
2 changes: 2 additions & 0 deletions src/netxs/desktopio/directvt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ namespace netxs::prompt
X(pipe) /* */ \
X(pool) /* */ \
X(rail) /* */ \
X(repl) /* */ \
X(s11n) /* */ \
X(sock) /* */ \
X(term) /* */ \
X(task) /* */ \
X(text) /* */ \
X(tile) /* */ \
X(user) /* */ \
Expand Down
133 changes: 133 additions & 0 deletions src/netxs/desktopio/scripting.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// Copyright (c) NetXS Group.
// Licensed under the MIT license.

#pragma once

#include "application.hpp"

namespace netxs::scripting
{
using namespace ui;

template<class Host>
class repl
{
using s11n = directvt::binary::s11n;
using pidt = os::pidt;
using task = os::task;

Host& owner;

// repl: Event handler.
class xlat
: public s11n
{
Host& owner; // xlat: Boss object reference.
subs token; // xlat: Subscription tokens.

public:
void disable()
{
token.clear();
}

void handle(s11n::xs::fullscreen lock)
{
//...
}
void handle(s11n::xs::focus_cut lock)
{
//...
}
void handle(s11n::xs::focus_set lock)
{
//...
}
void handle(s11n::xs::keybd_event lock)
{
//...
};

xlat(Host& owner)
: s11n{ *this },
owner{ owner }
{
owner.LISTEN(tier::anycast, e2::form::prop::ui::header, utf8, token)
{
//s11n::form_header.send(owner, 0, utf8);
};
owner.LISTEN(tier::anycast, e2::form::prop::ui::footer, utf8, token)
{
//s11n::form_footer.send(owner, 0, utf8);
};
owner.LISTEN(tier::release, hids::events::device::mouse::any, gear, token)
{
//...
};
owner.LISTEN(tier::general, hids::events::die, gear, token)
{
//...
};
}
};

xlat stream; // repl: Event tracker.
text curdir; // repl: Current working directory.
text cmdarg; // repl: Startup command line arguments.
flag active; // repl: Scripting engine lifetime.
pidt procid; // repl: PTY child process id.
task engine; // repl: Scripting engine instance.

// repl: Proceed DirectVT input.
void ondata(view data)
{
if (active)
{
log(prompt::repl, ansi::hi(utf::debase<faux, faux>(data)));
//stream.s11n::sync(data);
}
}
// repl: Shutdown callback handler.
void onexit(si32 code)
{
//netxs::events::enqueue(owner.This(), [&, code](auto& boss) mutable
//{
// if (code) log(ansi::bgc(reddk).fgc(whitelt).add('\n', prompt::repl, "Exit code ", utf::to_hex_0x(code), ' ').nil());
// else log(prompt::repl, "Exit code 0");
// //backup.reset(); // Call repl::dtor.
//});
}

public:
// repl: Write client data.
void write(view data)
{
log(prompt::repl, "exec: ", ansi::hi(utf::debase<faux, faux>(data)));
engine.write(data);
}
// repl: Start a new process.
void start(text cwd, text cmd)
{
curdir = cwd;
cmdarg = cmd;
if (!engine)
{
procid = engine.start(curdir, cmdarg, [&](auto utf8_shadow) { ondata(utf8_shadow); },
[&](auto exit_reason) { onexit(exit_reason); });
}
}
void shut()
{
active = faux;
if (engine) engine.shut();
}

repl(Host& owner)
: owner{ owner },
stream{owner },
active{ true }
{

}
};
}

0 comments on commit 4dc5e77

Please sign in to comment.