summaryrefslogtreecommitdiff
path: root/src/events/keyboard_event.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/events/keyboard_event.c')
-rw-r--r--src/events/keyboard_event.c228
1 files changed, 166 insertions, 62 deletions
diff --git a/src/events/keyboard_event.c b/src/events/keyboard_event.c
index fb29c18..d1fa1ca 100644
--- a/src/events/keyboard_event.c
+++ b/src/events/keyboard_event.c
@@ -15,14 +15,14 @@
static void _virtual_dom_keyboard_event_destroy(struct dom_event *evt);
-static struct dom_event_private_vtable _event_vtable = {
+static const struct dom_event_private_vtable _event_vtable = {
_virtual_dom_keyboard_event_destroy
};
/* Constructor */
dom_exception _dom_keyboard_event_create(struct dom_keyboard_event **evt)
{
- *evt = malloc(sizeof(dom_keyboard_event));
+ *evt = calloc(1, sizeof(dom_keyboard_event));
if (*evt == NULL)
return DOM_NO_MEM_ERR;
@@ -42,8 +42,16 @@ void _dom_keyboard_event_destroy(struct dom_keyboard_event *evt)
/* Initialise function */
dom_exception _dom_keyboard_event_initialise(struct dom_keyboard_event *evt)
{
- evt->key_ident = NULL;
- evt->modifier_state = 0;
+ 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);
}
@@ -51,6 +59,11 @@ dom_exception _dom_keyboard_event_initialise(struct dom_keyboard_event *evt)
/* 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);
}
@@ -64,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(dom_keyboard_event *evt,
+ dom_string **key)
+{
+ *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_key_identifier(dom_keyboard_event *evt,
- dom_string **ident)
+dom_exception _dom_keyboard_event_get_code(dom_keyboard_event *evt,
+ dom_string **code)
{
- *ident = evt->key_ident;
- dom_string_ref(*ident);
+ *code = dom_string_ref(evt->code);
return DOM_NO_ERR;
}
@@ -83,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;
}
@@ -123,7 +150,7 @@ dom_exception _dom_keyboard_event_get_shift_key(dom_keyboard_event *evt,
return DOM_NO_ERR;
}
-
+
/**
* Get the alt key state
*
@@ -207,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);
}