diff options
Diffstat (limited to '')
| -rw-r--r-- | src/LuaRuntime.cpp | 45 | ||||
| -rw-r--r-- | src/LuaRuntime.hpp | 50 | ||||
| -rw-r--r-- | src/LuaRuntimeApi.hpp | 1 | ||||
| -rw-r--r-- | src/schemes/NullRpcSchemeHandler.hpp | 14 | ||||
| -rw-r--r-- | src/widgets/BrowserApp.cpp | 5 | ||||
| -rw-r--r-- | src/widgets/BrowserWindow.cpp | 5 | ||||
| -rw-r--r-- | src/widgets/WebView.cpp | 13 | ||||
| -rw-r--r-- | src/widgets/WebView.hpp | 4 |
8 files changed, 73 insertions, 64 deletions
diff --git a/src/LuaRuntime.cpp b/src/LuaRuntime.cpp index e9dc86f..343266c 100644 --- a/src/LuaRuntime.cpp +++ b/src/LuaRuntime.cpp @@ -112,3 +112,48 @@ LuaRuntime::~LuaRuntime() { lua_close(state); state = nullptr; } + +std::vector<QString> LuaRuntime::lua_tostringlist(lua_State *state) { + std::vector<QString> values; + if (!lua_istable(state, -1)) + return values; + + lua_pushnil(state); // First key for lua_next() + while (lua_next(state, -2) != 0) { + if (lua_isstring(state, -1)) + values.emplace_back(lua_tostring(state, -1)); + lua_pop(state, 1); + } + lua_pop(state, 1); + + return values; +} + +void LuaRuntime::inspect_lua_stack(lua_State *state) { + int top = lua_gettop(state); + qDebug() << "--- Lua Stack (top: " << top << ") ---\n"; + + for (int i = 1; i <= top; i++) { + int type = lua_type(state, i); + qDebug() << " " << i << ": " << lua_typename(state, type); + } + + qDebug() << "---------------------------\n"; + lua_settop(state, top); +} + +QVariant LuaRuntime::get_lua_value(lua_State *state, int idx, QVariant default_value) { + if (lua_isnoneornil(state, idx)) + return default_value; + + if (lua_isstring(state, idx)) + return lua_tostring(state, idx); + + if (lua_isboolean(state, idx)) + return lua_toboolean(state, idx); + + if (lua_isnumber(state, idx)) + return lua_tonumber(state, idx); + + return lua_tostring(state, idx); +} diff --git a/src/LuaRuntime.hpp b/src/LuaRuntime.hpp index 0e4584d..04c17bc 100644 --- a/src/LuaRuntime.hpp +++ b/src/LuaRuntime.hpp @@ -6,12 +6,10 @@ #include <optional> #include "AsyncEventLoop.hpp" -#include "lua.h" #include "utils.hpp" #include "widgets/BrowserWindow.hpp" #include "widgets/Decorations.hpp" #include "widgets/WebView.hpp" -#include "widgets/WebViewStack.hpp" #ifndef PROJECT_LUA_PATH #define PROJECT_LUA_PATH "" @@ -79,50 +77,8 @@ private: lua_State *state; AsyncEventLoop *event_loop = nullptr; - // TEMP public: - static void inspect_lua_stack(lua_State *state) { - int top = lua_gettop(state); - qDebug() << "--- Lua Stack (top: " << top << ") ---\n"; - - for (int i = 1; i <= top; i++) { - int type = lua_type(state, i); - qDebug() << " " << i << ": " << lua_typename(state, type); - } - - qDebug() << "---------------------------\n"; - lua_settop(state, top); - } - - static std::vector<QString> lua_tostringlist(lua_State *state) { - std::vector<QString> values; - if (!lua_istable(state, -1)) - return values; - - lua_pushnil(state); // First key for lua_next() - while (lua_next(state, -2) != 0) { - if (lua_isstring(state, -1)) - values.emplace_back(lua_tostring(state, -1)); - lua_pop(state, 1); - } - lua_pop(state, 1); - - return values; - } - - static QVariant get_lua_value(lua_State *state, int idx, QVariant default_value = 0) { - if (lua_isnoneornil(state, idx)) - return default_value; - - if (lua_isstring(state, idx)) - return lua_tostring(state, idx); - - if (lua_isboolean(state, idx)) - return lua_toboolean(state, idx); - - if (lua_isnumber(state, idx)) - return lua_tonumber(state, idx); - - return lua_tostring(state, idx); - } + static void inspect_lua_stack(lua_State *state); + static std::vector<QString> lua_tostringlist(lua_State *state); + static QVariant get_lua_value(lua_State *state, int idx, QVariant default_value = 0); }; diff --git a/src/LuaRuntimeApi.hpp b/src/LuaRuntimeApi.hpp index 41d3364..2dcba15 100644 --- a/src/LuaRuntimeApi.hpp +++ b/src/LuaRuntimeApi.hpp @@ -6,7 +6,6 @@ #include "LuaRuntime.hpp" #include "WindowActionRouter.hpp" #include "events/Event.hpp" -#include "lua.h" #include "widgets/BrowserWindow.hpp" #include "widgets/Decorations.hpp" #include "widgets/WebView.hpp" diff --git a/src/schemes/NullRpcSchemeHandler.hpp b/src/schemes/NullRpcSchemeHandler.hpp index b4bb216..b2b22cb 100644 --- a/src/schemes/NullRpcSchemeHandler.hpp +++ b/src/schemes/NullRpcSchemeHandler.hpp @@ -5,6 +5,11 @@ #include <QtCore> #include <qurlquery.h> +struct NullRPCMessage { + QString name; + QUrlQuery params; +}; + class NullRPCSchemeHandler : public QWebEngineUrlSchemeHandler { Q_OBJECT @@ -30,8 +35,11 @@ public: return; } - QUrlQuery query(url.query()); - emit message_received(url.host(), query); + NullRPCMessage message{ + .name = url.host(), + .params = QUrlQuery(url.query()), + }; + emit message_received(message); // TODO: responses managed with request ids QByteArray data = "{}"; @@ -42,7 +50,7 @@ public: } signals: - void message_received(const QString &action, QUrlQuery params); + void message_received(NullRPCMessage message); private: NullRPCSchemeHandler() = default; diff --git a/src/widgets/BrowserApp.cpp b/src/widgets/BrowserApp.cpp index 922c5cc..d6f274b 100644 --- a/src/widgets/BrowserApp.cpp +++ b/src/widgets/BrowserApp.cpp @@ -41,6 +41,11 @@ BrowserApp::BrowserApp(Configuration &configuration) : configuration(configurati setup_profile(profile); } + // Default keymaps + auto &keymap = KeymapEvaluator::instance(); + keymap.define_mode("n", {.passthrough = false}); + keymap.define_mode("i", {.passthrough = true}); + connect(&window_action_router, &WindowActionRouter::new_window_requested, this, [this](const QUrl &url) { create_window({url.toString()}); }); }; diff --git a/src/widgets/BrowserWindow.cpp b/src/widgets/BrowserWindow.cpp index 1fcf0c0..5d78360 100644 --- a/src/widgets/BrowserWindow.cpp +++ b/src/widgets/BrowserWindow.cpp @@ -37,11 +37,6 @@ BrowserWindow::BrowserWindow(const Configuration &configuration, QWebEngineProfi } } - // Default keymaps - auto &keymap = KeymapEvaluator::instance(); - keymap.define_mode("n", {.passthrough = false}); - keymap.define_mode("i", {.passthrough = true}); - // Update window title when webview changes connect(webview_stack, &WebViewStack::current_webview_title_changed, this, [this](int index) { auto webviews = webview_stack->get_webview_list(); diff --git a/src/widgets/WebView.cpp b/src/widgets/WebView.cpp index 604a502..2941e8d 100644 --- a/src/widgets/WebView.cpp +++ b/src/widgets/WebView.cpp @@ -5,8 +5,8 @@ #include <qwebenginepage.h> #include <qwebengineprofile.h> -#include "LuaRuntime.hpp" #include "schemes/NullRpcSchemeHandler.hpp" + #include "widgets/WebView.hpp" WebView::WebView(uint32_t webview_id, QWebEngineProfile *profile, QWidget *parent_node) @@ -50,6 +50,7 @@ void WebView::scroll_to_bottom() { } void WebView::enable_rpc_api() { + rpc_enabled = true; auto &nullrpc = NullRPCSchemeHandler::instance(); connect(&nullrpc, &NullRPCSchemeHandler::message_received, this, &WebView::on_rpc_message); } @@ -58,16 +59,14 @@ void WebView::expose_rpc_function(const QString &name, const RpcFunc &action) { exposed_functions.insert({name, action}); } -void WebView::on_rpc_message(const QString &action, const QUrlQuery ¶ms) { - if (!exposed_functions.contains(action)) { - qDebug() << "function not defined:" << action; +void WebView::on_rpc_message(const NullRPCMessage &message) { + if (!rpc_enabled || !exposed_functions.contains(message.name)) return; - } RpcArgs args; - for (auto pair : params.queryItems()) + for (auto pair : message.params.queryItems()) args.insert(pair); - auto func = exposed_functions.at(action); + auto func = exposed_functions.at(message.name); func(args); } diff --git a/src/widgets/WebView.hpp b/src/widgets/WebView.hpp index 5be9831..7a767d3 100644 --- a/src/widgets/WebView.hpp +++ b/src/widgets/WebView.hpp @@ -12,6 +12,7 @@ #include <qurlquery.h> #include <unordered_map> +#include "schemes/NullRpcSchemeHandler.hpp" #include "utils.hpp" #include "widgets/DevtoolsWindow.hpp" @@ -36,6 +37,7 @@ private: uint32_t id; DevtoolsWindow *devtools_window = nullptr; std::unordered_map<QString, RpcFunc> exposed_functions; + bool rpc_enabled = false; - void on_rpc_message(const QString &action, const QUrlQuery ¶ms); + void on_rpc_message(const NullRPCMessage &message); }; |
