1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

[mujs] keyboard.c

This commit is contained in:
Witold Filipczyk 2023-04-12 17:49:43 +02:00
parent a033ef49a0
commit ac8b24881c

View File

@ -0,0 +1,88 @@
/* The MuJS 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/mujs.h"
#include "ecmascript/mujs/keyboard.h"
#include "intl/charsets.h"
#include "terminal/event.h"
static void mjs_keyboardEvent_get_property_key(js_State *J);
static void mjs_keyboardEvent_get_property_keyCode(js_State *J);
static unicode_val_T keyCode;
struct keyboard {
unicode_val_T keyCode;
};
static void
mjs_keyboardEvent_finalizer(js_State *J, void *val)
{
struct keyboard *keyb = (struct keyboard *)val;
if (keyb) {
mem_free(keyb);
}
}
void
mjs_push_keyboardEvent(js_State *J, struct term_event *ev)
{
struct keyboard *keyb = (struct keyboard *)mem_calloc(1, sizeof(*keyb));
if (!keyb) {
js_error(J, "out of memory");
return;
}
keyCode = keyb->keyCode = get_kbd_key(ev);
js_newobject(J);
{
js_newuserdata(J, "event", keyb, mjs_keyboardEvent_finalizer);
addproperty(J, "key", mjs_keyboardEvent_get_property_key, NULL);
addproperty(J, "keyCode", mjs_keyboardEvent_get_property_keyCode, NULL);
}
}
static void
mjs_keyboardEvent_get_property_key(js_State *J)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct keyboard *keyb = (struct keyboard *)js_touserdata(J, 0, "event");
if (!keyb) {
js_pushnull(J);
return;
}
char text[8] = {0};
*text = keyb->keyCode;
js_pushstring(J, text);
}
static void
mjs_keyboardEvent_get_property_keyCode(js_State *J)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct keyboard *keyb = (struct keyboard *)js_touserdata(J, 0, "event");
if (!keyb) {
js_pushnull(J);
return;
}
js_pushnumber(J, keyb->keyCode);
}