From 7337ebb888a5046cf257dae1706c6d60754f30cd Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Mon, 17 Apr 2023 20:15:11 +0200 Subject: [PATCH] [libdom] keyboard.cpp --- src/ecmascript/libdom/spidermonkey/Makefile | 2 +- .../libdom/spidermonkey/keyboard.cpp | 217 ++++++++++++++++++ .../libdom/spidermonkey/meson.build | 2 +- src/ecmascript/spidermonkey/keyboard.cpp | 2 + 4 files changed, 221 insertions(+), 2 deletions(-) create mode 100644 src/ecmascript/libdom/spidermonkey/keyboard.cpp diff --git a/src/ecmascript/libdom/spidermonkey/Makefile b/src/ecmascript/libdom/spidermonkey/Makefile index 9481a033..fa388ee4 100644 --- a/src/ecmascript/libdom/spidermonkey/Makefile +++ b/src/ecmascript/libdom/spidermonkey/Makefile @@ -2,6 +2,6 @@ top_builddir=../../.. include $(top_builddir)/Makefile.config INCLUDES += $(SPIDERMONKEY_CFLAGS) -OBJS = localstorage.obj location.obj message.obj navigator.obj screen.obj unibar.obj window.obj xhr.obj +OBJS = keyboard.obj localstorage.obj location.obj message.obj navigator.obj screen.obj unibar.obj window.obj xhr.obj include $(top_srcdir)/Makefile.lib diff --git a/src/ecmascript/libdom/spidermonkey/keyboard.cpp b/src/ecmascript/libdom/spidermonkey/keyboard.cpp new file mode 100644 index 00000000..9182078c --- /dev/null +++ b/src/ecmascript/libdom/spidermonkey/keyboard.cpp @@ -0,0 +1,217 @@ +/* The SpiderMonkey KeyboardEvent object implementation. */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include + +#include "elinks.h" + +#include "ecmascript/spidermonkey/util.h" +#include +#include + +#include "bfu/dialog.h" +#include "cache/cache.h" +#include "cookies/cookies.h" +#include "dialogs/menu.h" +#include "dialogs/status.h" +#include "document/html/frames.h" +#include "document/document.h" +#include "document/forms.h" +#include "document/view.h" +#include "ecmascript/ecmascript.h" +#include "ecmascript/spidermonkey.h" +#include "ecmascript/spidermonkey/heartbeat.h" +#include "ecmascript/spidermonkey/keyboard.h" +#include "ecmascript/timer.h" +#include "intl/libintl.h" +#include "main/select.h" +#include "main/timer.h" +#include "network/connection.h" +#include "osdep/newwin.h" +#include "osdep/sysname.h" +#include "protocol/http/http.h" +#include "protocol/uri.h" +#include "session/download.h" +#include "session/history.h" +#include "session/location.h" +#include "session/session.h" +#include "session/task.h" +#include "terminal/tab.h" +#include "terminal/terminal.h" +#include "util/conv.h" +#include "util/memory.h" +#include "util/string.h" +#include "viewer/text/draw.h" +#include "viewer/text/form.h" +#include "viewer/text/link.h" +#include "viewer/text/vs.h" + +#include +#include +#include +#include +#include +#include + + +static bool keyboardEvent_get_property_key(JSContext *cx, unsigned int argc, JS::Value *vp); +static bool keyboardEvent_get_property_keyCode(JSContext *cx, unsigned int argc, JS::Value *vp); + +static unicode_val_T keyCode; + +struct keyboard { + unicode_val_T keyCode; +}; + +static void +keyboardEvent_finalize(JS::GCContext *op, JSObject *keyb_obj) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + struct keyboard *keyb = JS::GetMaybePtrFromReservedSlot(keyb_obj, 0); + + if (keyb) { + mem_free(keyb); + } +} + +JSClassOps keyboardEvent_ops = { + nullptr, // addProperty + nullptr, // deleteProperty + nullptr, // enumerate + nullptr, // newEnumerate + nullptr, // resolve + nullptr, // mayResolve + keyboardEvent_finalize, // finalize + nullptr, // call + nullptr, // construct + JS_GlobalObjectTraceHook // trace +}; + +JSClass keyboardEvent_class = { + "KeyboardEvent", + JSCLASS_HAS_RESERVED_SLOTS(1), + &keyboardEvent_ops +}; + +bool +keyboardEvent_constructor(JSContext* ctx, unsigned argc, JS::Value* vp) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + JS::CallArgs args = JS::CallArgsFromVp(argc, vp); + JS::RootedObject newObj(ctx, JS_NewObjectForConstructor(ctx, &keyboardEvent_class, args)); + JS::Realm *comp = js::GetContextRealm(ctx); + + if (!comp) { +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s %d\n", __FILE__, __FUNCTION__, __LINE__); +#endif + return false; + } + if (!newObj) { + return false; + } + struct keyboard *keyb = (struct keyboard *)mem_calloc(1, sizeof(*keyb)); + + if (!keyb) { + return false; + } + JS::SetReservedSlot(newObj, 0, JS::PrivateValue(keyb)); + keyb->keyCode = keyCode; + args.rval().setObject(*newObj); + + return true; +} + +JSPropertySpec keyboardEvent_props[] = { + JS_PSG("key", keyboardEvent_get_property_key, JSPROP_ENUMERATE), + JS_PSG("keyCode", keyboardEvent_get_property_keyCode, JSPROP_ENUMERATE), + JS_PS_END +}; + +static bool +keyboardEvent_get_property_key(JSContext *ctx, unsigned int argc, JS::Value *vp) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + JS::CallArgs args = JS::CallArgsFromVp(argc, vp); + JS::RootedObject hobj(ctx, &args.thisv().toObject()); + JS::Realm *comp = js::GetContextRealm(ctx); + + if (!comp) { +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s %d\n", __FILE__, __FUNCTION__, __LINE__); +#endif + return false; + } + struct keyboard *keyb = JS::GetMaybePtrFromReservedSlot(hobj, 0); + + if (!keyb) { + return false; + } + char text[8] = {0}; + + *text = keyb->keyCode; + args.rval().setString(JS_NewStringCopyZ(ctx, text)); + + return true; +} + +static bool +keyboardEvent_get_property_keyCode(JSContext *ctx, unsigned int argc, JS::Value *vp) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + JS::CallArgs args = JS::CallArgsFromVp(argc, vp); + JS::RootedObject hobj(ctx, &args.thisv().toObject()); + JS::Realm *comp = js::GetContextRealm(ctx); + + if (!comp) { +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s %d\n", __FILE__, __FUNCTION__, __LINE__); +#endif + return false; + } + struct keyboard *keyb = JS::GetMaybePtrFromReservedSlot(hobj, 0); + + if (!keyb) { + return false; + } + args.rval().setInt32(keyb->keyCode); + + return true; +} + + +JSObject * +get_keyboardEvent(JSContext *ctx, struct term_event *ev) +{ + JSObject *k = JS_NewObject(ctx, &keyboardEvent_class); + + if (!k) { + return NULL; + } + + JS::RootedObject r_keyb(ctx, k); + JS_DefineProperties(ctx, r_keyb, (JSPropertySpec *) keyboardEvent_props); + + struct keyboard *keyb = (struct keyboard *)mem_calloc(1, sizeof(*keyb)); + + if (!keyb) { + return NULL; + } + keyCode = keyb->keyCode = get_kbd_key(ev); + JS::SetReservedSlot(k, 0, JS::PrivateValue(keyb)); + + return k; +} diff --git a/src/ecmascript/libdom/spidermonkey/meson.build b/src/ecmascript/libdom/spidermonkey/meson.build index cd37622e..611e1a69 100644 --- a/src/ecmascript/libdom/spidermonkey/meson.build +++ b/src/ecmascript/libdom/spidermonkey/meson.build @@ -1 +1 @@ -srcs += files('localstorage.cpp', 'location.cpp', 'message.cpp', 'navigator.cpp', 'screen.cpp', 'unibar.cpp', 'window.cpp', 'xhr.cpp') +srcs += files('keyboard.cpp', 'localstorage.cpp', 'location.cpp', 'message.cpp', 'navigator.cpp', 'screen.cpp', 'unibar.cpp', 'window.cpp', 'xhr.cpp') diff --git a/src/ecmascript/spidermonkey/keyboard.cpp b/src/ecmascript/spidermonkey/keyboard.cpp index 9182078c..86517b3d 100644 --- a/src/ecmascript/spidermonkey/keyboard.cpp +++ b/src/ecmascript/spidermonkey/keyboard.cpp @@ -58,6 +58,7 @@ #include #include +#ifndef CONFIG_LIBDOM static bool keyboardEvent_get_property_key(JSContext *cx, unsigned int argc, JS::Value *vp); static bool keyboardEvent_get_property_keyCode(JSContext *cx, unsigned int argc, JS::Value *vp); @@ -215,3 +216,4 @@ get_keyboardEvent(JSContext *ctx, struct term_event *ev) return k; } +#endif