Skip to content

Commit

Permalink
directvt#86 WIP: Switch to scripting model (naive script parser)
Browse files Browse the repository at this point in the history
  • Loading branch information
o-sdn-o committed Dec 24, 2024
1 parent 8e4c134 commit 7e47e20
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/netxs/apps.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ namespace netxs::app::shared

auto window = ui::cake::ctor();
window->plugin<pro::focus>(pro::focus::mode::focused)
->plugin<pro::keybd>()
->plugin<pro::keybd>("defapp")
//->plugin<pro::acryl>()
->plugin<pro::cache>()
->invoke([](auto& boss)
Expand Down Expand Up @@ -516,7 +516,7 @@ namespace netxs::app::shared
.add(app::shared::repository);
auto window = ui::cake::ctor()
->plugin<pro::focus>(pro::focus::mode::focused)
->plugin<pro::keybd>()
->plugin<pro::keybd>("defapp")
->plugin<pro::acryl>()
->plugin<pro::cache>()
->colors(whitedk, 0x30000000)
Expand Down
2 changes: 1 addition & 1 deletion src/netxs/apps/calc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ namespace netxs::app::calc
window->plugin<pro::focus>(pro::focus::mode::focused)
->colors(whitelt, 0x60'00'5f'1A)
->limits({ 10,7 }, { -1,-1 })
->plugin<pro::keybd>()
->plugin<pro::keybd>("defapp")
->shader(c3, e2::form::state::focus::count)
//->plugin<pro::acryl>()
->plugin<pro::cache>()
Expand Down
2 changes: 1 addition & 1 deletion src/netxs/apps/shop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ namespace netxs::app::shop
auto window = ui::cake::ctor();
window->plugin<pro::focus>(pro::focus::mode::focused)
->colors(whitelt, 0x60000000)
->plugin<pro::keybd>()
->plugin<pro::keybd>("defapp")
->plugin<pro::acryl>()
->plugin<pro::cache>()
->invoke([](auto& boss)
Expand Down
2 changes: 1 addition & 1 deletion src/netxs/apps/test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ namespace netxs::app::test
auto topic = get_text();
auto window = ui::cake::ctor()
->plugin<pro::focus>(pro::focus::mode::focused)
->plugin<pro::keybd>()
->plugin<pro::keybd>("defapp")
//->plugin<pro::acryl>()
->plugin<pro::cache>()
->invoke([](auto& boss)
Expand Down
2 changes: 1 addition & 1 deletion src/netxs/apps/text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ displaying the requested definition in a popup window or temporary buffer. Some

auto window = ui::cake::ctor();
window->plugin<pro::focus>(pro::focus::mode::hub)
->plugin<pro::keybd>()
->plugin<pro::keybd>("defapp")
->shader(c3, e2::form::state::focus::count)
//->plugin<pro::acryl>()
->plugin<pro::cache>()
Expand Down
4 changes: 2 additions & 2 deletions src/netxs/apps/tile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ namespace netxs::app::tile
->active()
->template plugin<pro::mover>() //todo GCC 11 requires template keyword
->template plugin<pro::focus>(pro::focus::mode::focusable)
->template plugin<pro::keybd>()
->template plugin<pro::keybd>("grip")
->shader(c3, e2::form::state::focus::count)
->template plugin<pro::shade<cell::shaders::xlight>>()
->invoke([&](auto& boss)
Expand Down Expand Up @@ -876,7 +876,7 @@ namespace netxs::app::tile
auto object = ui::fork::ctor(axis::Y)
->plugin<items>()
->plugin<pro::focus>()
->plugin<pro::keybd>()
->plugin<pro::keybd>("tile")
->invoke([&](auto& boss)
{
auto oneoff = ptr::shared(hook{});
Expand Down
2 changes: 1 addition & 1 deletion src/netxs/desktopio/baseui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace netxs::events::userland
EVENT_XS( nextframe , bool ), // general: Signal for rendering the world, the parameter indicates whether the world has been modified since the last rendering.
EVENT_XS( shutdown , const text ), // general: Server shutdown.
EVENT_XS( area , rect ), // release: Object rectangle.
EVENT_XS( runscript , input::hids ), // release: Run script activated by gear.
EVENT_XS( runscript , input::hids ), // preview: Pass script activated by gear to the ui::host. release: Run script on objects in context. request: Restore scripting context.
GROUP_XS( extra , si32 ), // Event extension slot.
GROUP_XS( timer , time ), // Timer tick, arg: current moment (now).
GROUP_XS( render , ui::face ), // release: UI-tree rendering.
Expand Down
39 changes: 37 additions & 2 deletions src/netxs/desktopio/console.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,7 @@ namespace netxs::ui
input{ props, *this },
debug{*this },
//focus{*this },
keybd{*this },
keybd{*this, "gate" },
paint{ canal, props.vtmode },
conio{ canal, *this },
direct{ !!(vtmode & (ui::console::direct | ui::console::gui)) },
Expand All @@ -1127,7 +1127,7 @@ namespace netxs::ui
{
//todo scripting
//keybd.proc("ToggleDebugOverlay", [&](hids& gear){ gear.set_handled(); debug ? debug.stop() : debug.start(); });
auto bindings = pro::keybd::load(config, "tui");
auto bindings = pro::keybd::load(config, "tui"); //todo -> pro::keybd("gate")
keybd.bind(bindings);

base::root(true);
Expand Down Expand Up @@ -1436,6 +1436,8 @@ namespace netxs::ui
using tick = datetime::quartz<bell, tier::general, e2::timer::tick.id>;

pro::focus focus; // host: Focus controller. Must be the first of all focus subscriptions.
//todo scripting: process gui keybd events
pro::keybd keybd; // host: Keybd controller.

tick quartz; // host: Frame rate synchronizator.
si32 maxfps; // host: Frame rate.
Expand All @@ -1448,6 +1450,7 @@ namespace netxs::ui

host(xipc server, xmls config, si32 focus_type = pro::focus::mode::hub)
: focus{ *this, focus_type, faux },
keybd{ *this, "vtm" },
quartz{ *this },
config{ config },
active{ true }
Expand Down Expand Up @@ -1496,6 +1499,38 @@ namespace netxs::ui
maxfps = config.take("/config/timings/fps", 60);
if (maxfps <= 0) maxfps = 60;

LISTEN(tier::preview, e2::runscript, gear)
{
if (!gear.action_ptr) return;
if (!gear.scripting_context_ptr) return;
//todo scripting
auto& script_body = *gear.action_ptr;
auto& scripting_context = *gear.scripting_context_ptr;
log("script context:");
log(" script body: ", ansi::hi(script_body));
for (auto [object_name, object_wptr] : scripting_context)
{
if (auto object_ptr = object_wptr.lock())
{
log(" %name%: id=%%", utf::adjust(object_name, 11, ' ', true), object_ptr->id);
}
}
//todo unify
auto cmd_list = utf::split(script_body, '$');
for (auto cmd : cmd_list)
{
auto object_name = utf::take_front(cmd, ".");
utf::trim_front(cmd, "."); // Pop ".".
auto iter = scripting_context.find(object_name);
if (iter != scripting_context.end())
if (auto object_ptr = iter->second.lock())
{
gear.call_proc = cmd;
object_ptr->bell::signal(tier::release, e2::runscript, gear);
gear.call_proc = {};
}
}
};
LISTEN(tier::request, e2::config::creator, world_ptr, tokens)
{
world_ptr = base::This();
Expand Down
51 changes: 39 additions & 12 deletions src/netxs/desktopio/controls.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2033,10 +2033,11 @@ namespace netxs::ui
using skill::boss,
skill::memo;

netxs::sptr<std::unordered_map<text, wptr>> scripting_context_ptr; // hids: Script execution context: sptr<map<$object_name_str, $object_wptr>>.
std::unordered_map<text, std::pair<std::list<netxs::sptr<text>>, bool>, qiew::hash, qiew::equal> handlers; // Map<chord, pair<list<shared_ptr<action>>, preview>>.
bool interrupt_key_proc;
std::unordered_map<id_t, time> last_key;
si64 instance_id;
text boss_name_str;

auto _get_chord_list(qiew chord_str = {}) -> std::optional<std::invoke_result_t<decltype(input::key::kmap::chord_list), qiew>>
{
Expand Down Expand Up @@ -2083,14 +2084,25 @@ namespace netxs::ui
auto& [actions, run_preview] = iter->second;
if (!preview_mode || run_preview)
{
if (!scripting_context_ptr) // Restore scripting context.
{
scripting_context_ptr = ptr::shared<decltype(scripting_context_ptr)::element_type>();
std::swap(gear.scripting_context_ptr, scripting_context_ptr);
boss.base::riseup(tier::request, e2::runscript, gear, true);
std::swap(gear.scripting_context_ptr, scripting_context_ptr);
}
for (auto& action_ptr : actions)
{
if (!interrupt_key_proc)
if (!gear.interrupt_key_proc)
{
auto temp = std::exchange(gear.action_ptr, action_ptr);
log("run script: ", *gear.action_ptr);
boss.base::riseup(tier::release, e2::runscript, gear);
gear.action_ptr = temp;
auto temp_action_ptr = std::exchange(gear.action_ptr, action_ptr);
auto temp_scripting_context_ptr = std::exchange(gear.scripting_context_ptr, scripting_context_ptr);
if (auto world_ptr = gear.owner.parent())
{
world_ptr->bell::signal(tier::preview, e2::runscript, gear);
}
gear.action_ptr = temp_action_ptr;
gear.scripting_context_ptr = temp_scripting_context_ptr;
}
}
}
Expand All @@ -2103,11 +2115,26 @@ namespace netxs::ui

public:
keybd(base&&) = delete;
keybd(base& boss)
keybd(base& boss, text boss_name = {})
: skill{ boss },
interrupt_key_proc{ faux },
instance_id{ datetime::now().time_since_epoch().count() }
instance_id{ datetime::now().time_since_epoch().count() },
boss_name_str{ boss_name.empty() ? utf::concat("object"s, boss.id) : boss_name }
{
boss.LISTEN(tier::request, e2::runscript, gear)
{
if (gear.scripting_context_ptr)
{
auto& scripting_context = *gear.scripting_context_ptr;
scripting_context[boss_name_str] = boss.This();
}
};
boss.LISTEN(tier::release, e2::form::state::focus::count, count)
{
if (count == 0)
{
scripting_context_ptr.reset();
}
};
boss.LISTEN(tier::general, hids::events::die, gear, memo)
{
last_key.erase(gear.id);
Expand All @@ -2121,7 +2148,7 @@ namespace netxs::ui
timecod = gear.timecod;
if (gear.payload == input::keybd::type::keypress)
{
interrupt_key_proc = faux;
gear.interrupt_key_proc = faux;
if (!gear.handled) _dispatch(gear, faux, input::key::kmap::any_key);
if (!gear.handled) _dispatch(gear, faux, gear.vkchord);
if (!gear.handled) _dispatch(gear, faux, gear.chchord);
Expand All @@ -2145,8 +2172,8 @@ namespace netxs::ui
}
};
//todo scripting
//proc("Noop", [&](hids& gear){ gear.set_handled(); interrupt_key_proc = true; });
//proc("DropAutoRepeat", [&](hids& gear){ if (gear.keystat == input::key::repeated) { gear.set_handled(); interrupt_key_proc = true; }});
//proc("Noop", [&](hids& gear){ gear.set_handled(); gear.interrupt_key_proc = true; });
//proc("DropAutoRepeat", [&](hids& gear){ if (gear.keystat == input::key::repeated) { gear.set_handled(); gear.interrupt_key_proc = true; }});
}

auto filter(hids& gear)
Expand Down
2 changes: 1 addition & 1 deletion src/netxs/desktopio/gui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1968,7 +1968,7 @@ namespace netxs::gui
: base{ indexer },
titles{ *this, "", "", faux },
wfocus{ *this, ui::pro::focus::mode::relay },
wkeybd{ *this },
wkeybd{ *this, "gui" }, //todo scripting: move processing to ui::host
fcache{ font_names, cell_height, [&]{ netxs::set_flag<task::all>(reload); window_post_command(ipc::no_command); } },
gcache{ fcache, antialiasing },
blinks{ .init = blink_rate },
Expand Down
5 changes: 4 additions & 1 deletion src/netxs/desktopio/input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1499,7 +1499,10 @@ namespace netxs::input
bool slot_forced = faux; // .

//todo unify
netxs::sptr<text> action_ptr; // An action script body passed by pro::keybd.
bool interrupt_key_proc{}; // hids: .
netxs::sptr<text> action_ptr; // hids: A script body passed by pro::keybd/ui::menu.
netxs::sptr<std::unordered_map<text, wptr>> scripting_context_ptr; // hids: Script execution context: sptr<map<$object_name_str, $object_wptr>>.
qiew call_proc; // hids: .

//todo unify
bool mouse_disabled = faux; // Hide mouse cursor.
Expand Down
2 changes: 1 addition & 1 deletion src/netxs/desktopio/terminal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7798,7 +7798,7 @@ namespace netxs::ui
cursor{ *this, config.def_cur_on, config.def_cursor, dot_00, config.def_period, config.def_curclr },
worker{ *this },
dynamo{ *this },
chords{ *this },
chords{ *this, "terminal" },
mtrack{ *this },
ftrack{ *this },
wtrack{ *this },
Expand Down
30 changes: 29 additions & 1 deletion src/vtm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1270,8 +1270,9 @@ namespace netxs::app::vtm
id_t focus; // hall: Last active gear id.
text selected_item; // hall: Override default menu item (if not empty).
std::unordered_map<id_t, si32> switch_counter; // hall: Focus switch counter.
input::key::keybind_list_t window_bindings;

static auto window(link& what)
auto window(link& what)
{
return ui::cake::ctor()
->plugin<pro::d_n_d>()
Expand All @@ -1282,9 +1283,34 @@ namespace netxs::app::vtm
->plugin<pro::frame>()
->plugin<pro::light>()
->plugin<pro::focus>()
->plugin<pro::keybd>("window")
->limits(dot_11)
->invoke([&](auto& boss)
{
auto& keybd = boss.template plugins<pro::keybd>();
keybd.bind(window_bindings);

boss.LISTEN(tier::release, e2::runscript, gear)
{
//todo unify
gear.set_handled();
auto cmd = gear.call_proc;
log("cmd=", cmd);
auto proc_name = utf::take_front(cmd, "(");
log("proc_name=", proc_name);
utf::trim_front(cmd, "(");
auto args = utf::take_front(cmd, ")");
log("args=", args);
if (proc_name.starts_with("Warp"))
{
auto warp = xml::take_or(args, dent{});
boss.bell::enqueue(boss.This(), [warp](auto& boss) // Keep the focus tree intact while processing key events.
{
boss.bell::signal(tier::preview, e2::form::layout::swarp, warp);
});
}
};

boss.base::kind(base::reflow_root);
boss.LISTEN(tier::preview, vtm::events::d_n_d::drop, what, -, (menuid = what.menuid))
{
Expand Down Expand Up @@ -1951,6 +1977,8 @@ namespace netxs::app::vtm
: host{ server, config, pro::focus::mode::focusable },
focus{ id_t{} }
{
window_bindings = pro::keybd::load(host::config, "window");

auto current_module_file = os::process::binary();
auto& apps_list = dbase.apps;
auto& menu_list = dbase.menu;
Expand Down
Loading

0 comments on commit 7e47e20

Please sign in to comment.