1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-09-29 03:17:53 -04:00

[quickjs] libdom keyboard.c

This commit is contained in:
Witold Filipczyk 2023-03-27 16:20:09 +02:00
parent 53c53a861a
commit a7e0eb03f7
5 changed files with 130 additions and 2 deletions

View File

@ -1,6 +1,6 @@
top_builddir=../../../..
include $(top_builddir)/Makefile.config
OBJS = attr.o attributes.o collection.o console.o heartbeat.o history.o mapa.obj
OBJS = attr.o attributes.o collection.o console.o heartbeat.o history.o keyboard.o mapa.obj
include $(top_srcdir)/Makefile.lib

View File

@ -0,0 +1,117 @@
/* The QuickJS KeyboardEvent object implementation. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "elinks.h"
#include "ecmascript/ecmascript.h"
#include "ecmascript/quickjs.h"
#include "ecmascript/quickjs/keyboard.h"
#include "intl/charsets.h"
#include "terminal/event.h"
#define countof(x) (sizeof(x) / sizeof((x)[0]))
static JSClassID js_keyboardEvent_class_id;
static JSValue js_keyboardEvent_get_property_key(JSContext *ctx, JSValueConst this_val);
static JSValue js_keyboardEvent_get_property_keyCode(JSContext *ctx, JSValueConst this_val);
static unicode_val_T keyCode;
struct keyboard {
unicode_val_T keyCode;
};
static
void js_keyboardEvent_finalizer(JSRuntime *rt, JSValue val)
{
REF_JS(val);
struct keyboard *keyb = (struct keyboard *)JS_GetOpaque(val, js_keyboardEvent_class_id);
if (keyb) {
mem_free(keyb);
}
}
static JSClassDef js_keyboardEvent_class = {
"KeyboardEvent",
js_keyboardEvent_finalizer
};
static const JSCFunctionListEntry js_keyboardEvent_proto_funcs[] = {
JS_CGETSET_DEF("key", js_keyboardEvent_get_property_key, NULL),
JS_CGETSET_DEF("keyCode", js_keyboardEvent_get_property_keyCode, NULL)
};
static JSValue
js_keyboardEvent_get_property_key(JSContext *ctx, JSValueConst this_val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
REF_JS(this_val);
struct keyboard *keyb = (struct keyboard *)(JS_GetOpaque(this_val, js_keyboardEvent_class_id));
if (!keyb) {
return JS_NULL;
}
char text[8] = {0};
*text = keyb->keyCode;
JSValue r = JS_NewString(ctx, text);
RETURN_JS(r);
}
static JSValue
js_keyboardEvent_get_property_keyCode(JSContext *ctx, JSValueConst this_val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
REF_JS(this_val);
struct keyboard *keyb = (struct keyboard *)(JS_GetOpaque(this_val, js_keyboardEvent_class_id));
if (!keyb) {
return JS_NULL;
}
return JS_NewUint32(ctx, keyb->keyCode);
}
JSValue
get_keyboardEvent(JSContext *ctx, struct term_event *ev)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
static int initialized;
/* create the element class */
if (!initialized) {
JS_NewClassID(&js_keyboardEvent_class_id);
JS_NewClass(JS_GetRuntime(ctx), js_keyboardEvent_class_id, &js_keyboardEvent_class);
initialized = 1;
}
struct keyboard *keyb = (struct keyboard *)mem_calloc(1, sizeof(*keyb));
if (!keyb) {
return JS_NULL;
}
keyCode = keyb->keyCode = get_kbd_key(ev);
JSValue keyb_obj = JS_NewObjectClass(ctx, js_keyboardEvent_class_id);
JS_SetPropertyFunctionList(ctx, keyb_obj, js_keyboardEvent_proto_funcs, countof(js_keyboardEvent_proto_funcs));
JS_SetClassProto(ctx, js_keyboardEvent_class_id, keyb_obj);
JS_SetOpaque(keyb_obj, keyb);
JSValue rr = JS_DupValue(ctx, keyb_obj);
RETURN_JS(rr);
}

View File

@ -1 +1 @@
srcs += files('attr.c', 'attributes.c', 'collection.c', 'console.c', 'heartbeat.c', 'history.c', 'mapa.cpp')
srcs += files('attr.c', 'attributes.c', 'collection.c', 'console.c', 'heartbeat.c', 'history.c', 'keyboard.c', 'mapa.cpp')

View File

@ -54,6 +54,8 @@
#include <sstream>
#include <vector>
#ifndef CONFIG_LIBDOM
#define countof(x) (sizeof(x) / sizeof((x)[0]))
static JSClassID js_keyboardEvent_class_id;
@ -194,3 +196,4 @@ get_keyboardEvent(JSContext *ctx, struct term_event *ev)
JSValue rr = JS_DupValue(ctx, keyb_obj);
RETURN_JS(rr);
}
#endif

View File

@ -3,8 +3,16 @@
#include <quickjs/quickjs.h>
#ifdef __cplusplus
extern "C" {
#endif
struct term_event;
JSValue get_keyboardEvent(JSContext *ctx, struct term_event *ev);
#ifdef __cplusplus
}
#endif
#endif