From a39a24f6f85bc26ff3fe5a071440573cee4230f5 Mon Sep 17 00:00:00 2001 From: Michael Drake Date: Sun, 1 Dec 2019 17:18:35 +0000 Subject: keyboard events: Update to latest DOM spec. --- include/dom/events/keyboard_event.h | 45 +++++--- src/events/keyboard_event.c | 223 ++++++++++++++++++++++++++---------- src/events/keyboard_event.h | 15 +-- 3 files changed, 200 insertions(+), 83 deletions(-) diff --git a/include/dom/events/keyboard_event.h b/include/dom/events/keyboard_event.h index 236a3f4..03cab4b 100644 --- a/include/dom/events/keyboard_event.h +++ b/include/dom/events/keyboard_event.h @@ -27,14 +27,20 @@ dom_exception _dom_keyboard_event_create(dom_keyboard_event **evt); #define dom_keyboard_event_create(n) \ _dom_keyboard_event_create((dom_keyboard_event **) (n)) -dom_exception _dom_keyboard_event_get_key_identifier(dom_keyboard_event *evt, - dom_string **ident); -#define dom_keyboard_event_get_key_identifier(e, i) \ - _dom_keyboard_event_get_key_identifier( \ +dom_exception _dom_keyboard_event_get_key(dom_keyboard_event *evt, + dom_string **key); +#define dom_keyboard_event_get_key(e, i) \ + _dom_keyboard_event_get_key( \ (dom_keyboard_event *) (e), (dom_string **) (i)) -dom_exception _dom_keyboard_event_get_key_location(dom_keyboard_event *evt, - dom_key_location *loc); +dom_exception _dom_keyboard_event_get_code(dom_keyboard_event *evt, + dom_string **code); +#define dom_keyboard_event_get_code(e, i) \ + _dom_keyboard_event_get_code( \ + (dom_keyboard_event *) (e), (dom_string **) (i)) + +dom_exception _dom_keyboard_event_get_location(dom_keyboard_event *evt, + dom_key_location *location); #define dom_keyboard_event_get_key_location(e, l) \ _dom_keyboard_event_get_key_location( \ (dom_keyboard_event *) (e), (dom_key_location *) (l)) @@ -69,25 +75,32 @@ dom_exception _dom_keyboard_event_get_modifier_state(dom_keyboard_event *evt, dom_exception _dom_keyboard_event_init(dom_keyboard_event *evt, dom_string *type, bool bubble, bool cancelable, - struct dom_abstract_view *view, dom_string *key_ident, - dom_key_location key_loc, dom_string *modifier_list); -#define dom_keyboard_event_init(e, t, b, c, v, ki, kl, m) \ + struct dom_abstract_view *view, dom_string *key, + dom_string *code, dom_key_location location, + bool ctrl_key, bool shift_key, bool alt_key, bool meta_key, + bool repeat, bool is_composing); +#define dom_keyboard_event_init(e, t, b, c, v, kk, kc, kl, ck, sk, ak, mk, r, ic) \ _dom_keyboard_event_init((dom_keyboard_event *) (e), \ (dom_string *) (t), (bool) (b), (bool) (c), \ - (struct dom_abstract_view *) (v), (dom_string *) (ki), \ - (dom_key_location) (kl), (dom_string *) (m)) + (struct dom_abstract_view *) (v), (dom_string *) (kk), \ + (dom_string *) (kc), (dom_key_location) (kl), \ + (bool) (ck), (bool) (sk), (bool) (ak), (bool) (mk), \ + (bool) (r), (bool) (ic)) dom_exception _dom_keyboard_event_init_ns(dom_keyboard_event *evt, dom_string *namespace, dom_string *type, bool bubble, bool cancelable, struct dom_abstract_view *view, - dom_string *key_ident, dom_key_location key_loc, - dom_string *modifier_list); + dom_string *key, dom_string *code, dom_key_location location, + bool ctrl_key, bool shift_key, bool alt_key, bool meta_key, + bool repeat, bool is_composing); #define dom_keyboard_event_init_ns(e, n, t, b, c, v, ki, kl, m) \ _dom_keyboard_event_init_ns((dom_keyboard_event *) (e), \ (dom_string *) (n), (dom_string *) (t), \ - (bool) (b), (bool) (c), (struct dom_abstract_view *) (v), \ - (dom_string *) (ki), (dom_key_location) (kl), \ - (dom_string *) (m)) + (bool) (b), (bool) (c), \ + (struct dom_abstract_view *) (v), (dom_string *) (kk), \ + (dom_string *) (kc), (dom_key_location) (kl), \ + (bool) (ck), (bool) (sk), (bool) (ak), (bool) (mk), \ + (bool) (r), (bool) (ic)) #endif diff --git a/src/events/keyboard_event.c b/src/events/keyboard_event.c index 2d9a26e..d349355 100644 --- a/src/events/keyboard_event.c +++ b/src/events/keyboard_event.c @@ -42,12 +42,28 @@ void _dom_keyboard_event_destroy(struct dom_keyboard_event *evt) /* Initialise function */ dom_exception _dom_keyboard_event_initialise(struct dom_keyboard_event *evt) { + dom_exception err; + dom_string *empty_string; + + err = dom_string_create((const uint8_t *)"", 0, &empty_string); + if (err != DOM_NO_ERR) { + return err; + } + + evt->key = empty_string; + evt->code = dom_string_ref(empty_string); + return _dom_ui_event_initialise(&evt->base); } /* Finalise function */ void _dom_keyboard_event_finalise(struct dom_keyboard_event *evt) { + if (evt->key != NULL) + dom_string_unref(evt->key); + if (evt->code != NULL) + dom_string_unref(evt->code); + _dom_ui_event_finalise(&evt->base); } @@ -61,17 +77,31 @@ void _virtual_dom_keyboard_event_destroy(struct dom_event *evt) /* The public API */ /** - * Get the key identifier + * Get the key * * \param evt The Event object - * \param ident The returned key identifier + * \param key The returned key * \return DOM_NO_ERR. */ -dom_exception _dom_keyboard_event_get_key_identifier(dom_keyboard_event *evt, - dom_string **ident) +dom_exception _dom_keyboard_event_get_key(dom_keyboard_event *evt, + dom_string **key) { - *ident = evt->key_ident; - dom_string_ref(*ident); + *key = dom_string_ref(evt->key); + + return DOM_NO_ERR; +} + +/** + * Get the code + * + * \param evt The Event object + * \param code The returned code + * \return DOM_NO_ERR. + */ +dom_exception _dom_keyboard_event_get_code(dom_keyboard_event *evt, + dom_string **code) +{ + *code = dom_string_ref(evt->code); return DOM_NO_ERR; } @@ -80,13 +110,13 @@ dom_exception _dom_keyboard_event_get_key_identifier(dom_keyboard_event *evt, * Get the key location * * \param evt The Event object - * \param loc The returned key location + * \param location The returned key location * \return DOM_NO_ERR. */ -dom_exception _dom_keyboard_event_get_key_location(dom_keyboard_event *evt, - dom_key_location *loc) +dom_exception _dom_keyboard_event_get_location(dom_keyboard_event *evt, + dom_key_location *location) { - *loc = evt->key_loc; + *location = evt->location; return DOM_NO_ERR; } @@ -120,7 +150,7 @@ dom_exception _dom_keyboard_event_get_shift_key(dom_keyboard_event *evt, return DOM_NO_ERR; } - + /** * Get the alt key state * @@ -204,70 +234,147 @@ dom_exception _dom_keyboard_event_get_modifier_state(dom_keyboard_event *evt, } /** - * Initialise the keyboard event + * Helper to initialise the keyboard event * - * \param evt The Event object - * \param type The event's type - * \param bubble Whether this is a bubbling event - * \param cancelable Whether this is a cancelable event - * \param view The AbstractView associated with this event - * \param key_indent The key identifier of pressed key - * \param key_loc The key location of the preesed key - * \param modifier_list A string of modifier key identifiers, separated with - * space - * \return DOM_NO_ERR on success, appropriate dom_exception on failure. + * \param evt The Event object + * \param view The AbstractView associated with this event + * \param key The key identifier of pressed key + * \param code The code identifier of pressed key + * \param location The key location of the preesed key + * \param ctrl_key Whether the ctrl_key is pressed + * \param shift_key Whether the shift_key is pressed + * \param alt_key Whether the alt_key is pressed + * \param meta_key Whether the ctrl_key is pressed + * \param repeat Whether this is a repeat press from a held key + * \param is_composing Whether the input is being composed */ -dom_exception _dom_keyboard_event_init(dom_keyboard_event *evt, - dom_string *type, bool bubble, bool cancelable, - struct dom_abstract_view *view, dom_string *key_ident, - dom_key_location key_loc, dom_string *modifier_list) +static void _dom_keyboard_event_init_helper( + dom_keyboard_event *evt, + dom_string *key, + dom_string *code, + dom_key_location location, + bool ctrl_key, + bool shift_key, + bool alt_key, + bool meta_key, + bool repeat, + bool is_composing) { - dom_exception err; + if (key != NULL) { + dom_string_unref(evt->key); + evt->key = dom_string_ref(key); + } + if (code != NULL) { + dom_string_unref(evt->code); + evt->code = dom_string_ref(code); + } - evt->key_ident = key_ident; - dom_string_ref(evt->key_ident); - evt->key_loc = key_loc; + evt->location = location; - err = _dom_parse_modifier_list(modifier_list, &evt->modifier_state); - if (err != DOM_NO_ERR) - return err; + if (ctrl_key) { + evt->modifier_state |= DOM_MOD_CTRL; + } + if (shift_key) { + evt->modifier_state |= DOM_MOD_CTRL; + } + if (alt_key) { + evt->modifier_state |= DOM_MOD_SHIFT; + } + if (meta_key) { + evt->modifier_state |= DOM_MOD_META; + } - return _dom_ui_event_init(&evt->base, type, bubble, cancelable, - view, 0); + evt->repeat = repeat; + evt->is_composing = is_composing; } + + /** * Initialise the keyboard event with namespace * - * \param evt The Event object - * \param namespace The namespace of this event - * \param type The event's type - * \param bubble Whether this is a bubbling event - * \param cancelable Whether this is a cancelable event - * \param view The AbstractView associated with this event - * \param key_indent The key identifier of pressed key - * \param key_loc The key location of the preesed key - * \param modifier_list A string of modifier key identifiers, separated with - * space + * \param evt The Event object + * \param type The event's type + * \param bubble Whether this is a bubbling event + * \param cancelable Whether this is a cancelable event + * \param view The AbstractView associated with this event + * \param key The key identifier of pressed key + * \param code The code identifier of pressed key + * \param location The key location of the preesed key + * \param ctrl_key Whether the ctrl_key is pressed + * \param shift_key Whether the shift_key is pressed + * \param alt_key Whether the alt_key is pressed + * \param meta_key Whether the ctrl_key is pressed + * \param repeat Whether this is a repeat press from a held key + * \param is_composing Whether the input is being composed * \return DOM_NO_ERR on success, appropriate dom_exception on failure. */ -dom_exception _dom_keyboard_event_init_ns(dom_keyboard_event *evt, - dom_string *namespace, dom_string *type, - bool bubble, bool cancelable, struct dom_abstract_view *view, - dom_string *key_ident, dom_key_location key_loc, - dom_string *modifier_list) +dom_exception _dom_keyboard_event_init( + dom_keyboard_event *evt, + dom_string *type, + bool bubble, + bool cancelable, + struct dom_abstract_view *view, + dom_string *key, + dom_string *code, + dom_key_location location, + bool ctrl_key, + bool shift_key, + bool alt_key, + bool meta_key, + bool repeat, + bool is_composing) { - dom_exception err; + _dom_keyboard_event_init_helper(evt, key, code, location, + ctrl_key, shift_key, alt_key, meta_key, + repeat, is_composing); - evt->key_ident = key_ident; - dom_string_ref(evt->key_ident); - evt->key_loc = key_loc; + return _dom_ui_event_init(&evt->base, type, bubble, cancelable, + view, 0); +} - err = _dom_parse_modifier_list(modifier_list, &evt->modifier_state); - if (err != DOM_NO_ERR) - return err; +/** + * Initialise the keyboard event with namespace + * + * \param evt The Event object + * \param namespace The namespace of this event + * \param type The event's type + * \param bubble Whether this is a bubbling event + * \param cancelable Whether this is a cancelable event + * \param view The AbstractView associated with this event + * \param key The key identifier of pressed key + * \param code The code identifier of pressed key + * \param location The key location of the preesed key + * \param ctrl_key Whether the ctrl_key is pressed + * \param shift_key Whether the shift_key is pressed + * \param alt_key Whether the alt_key is pressed + * \param meta_key Whether the ctrl_key is pressed + * \param repeat Whether this is a repeat press from a held key + * \param is_composing Whether the input is being composed + * \return DOM_NO_ERR on success, appropriate dom_exception on failure. + */ +dom_exception _dom_keyboard_event_init_ns( + dom_keyboard_event *evt, + dom_string *namespace, + dom_string *type, + bool bubble, + bool cancelable, + struct dom_abstract_view *view, + dom_string *key, + dom_string *code, + dom_key_location location, + bool ctrl_key, + bool shift_key, + bool alt_key, + bool meta_key, + bool repeat, + bool is_composing) +{ + _dom_keyboard_event_init_helper(evt, key, code, location, + ctrl_key, shift_key, alt_key, meta_key, + repeat, is_composing); - return _dom_ui_event_init_ns(&evt->base, namespace, type, bubble, + return _dom_ui_event_init_ns(&evt->base, namespace, type, bubble, cancelable, view, 0); } diff --git a/src/events/keyboard_event.h b/src/events/keyboard_event.h index 60cbca3..81b702f 100644 --- a/src/events/keyboard_event.h +++ b/src/events/keyboard_event.h @@ -18,17 +18,14 @@ struct dom_keyboard_event { struct dom_ui_event base; /**< The base class */ - dom_string *key_ident; /**< The identifier of the key in this - * event, please refer: - * http://www.w3.org/TR/DOM-Level-3-Events/keyset.html#KeySet-Set - * for detail - */ - - dom_key_location key_loc; /**< Indicate the location of the key on - * the keyboard - */ + dom_string *key; + dom_string *code; + dom_key_location location; uint32_t modifier_state; /**< The modifier keys state */ + + bool repeat; + bool is_composing; }; /* Destructor */ -- cgit v1.2.3