key: Share state between multiple keys

This commit is contained in:
Dorota Czaplejewicz
2019-08-08 15:24:03 +00:00
parent cc3f2315a7
commit 1cfec4de50
11 changed files with 220 additions and 255 deletions

View File

@ -194,6 +194,10 @@ eek_key_init (EekKey *self)
priv->state = squeek_key_new (0);
}
void eek_key_share_state(EekKey *self, struct squeek_key *state) {
EekKeyPrivate *priv = eek_key_get_instance_private (self);
priv->state = state;
}
/**
* eek_key_set_keycode:
* @key: an #EekKey

View File

@ -71,6 +71,6 @@ gboolean eek_key_is_pressed (EekKey *key);
gboolean eek_key_is_locked (EekKey *key);
void eek_key_set_pressed (EekKey *key,
gboolean value);
void eek_key_share_state(EekKey *self, struct squeek_key *state);
G_END_DECLS
#endif /* EEK_KEY_H */

View File

@ -144,15 +144,18 @@ eek_keyboard_get_property (GObject *object,
}
/// Updates the state of locked keys based on the key that was activated
static void
/// FIXME: make independent of what the key are named,
/// and instead refer to the contained symbols
static guint
set_key_states (LevelKeyboard *keyboard,
EekKey *key)
EekKey *key,
guint new_level)
{
// Keys locking rules hardcoded for the time being...
const gchar *name = eek_element_get_name(EEK_ELEMENT(key));
// Lock the shift whenever it's pressed
// Lock the shift whenever it's pressed on the baselevel
// TODO: need to lock shift on the destination level
if (g_strcmp0(name, "LFSH-0") == 0) {
if (g_strcmp0(name, "Shift_L") == 0 && keyboard->level == 0) {
EekModifierKey *modifier_key = g_slice_new (EekModifierKey);
modifier_key->modifiers = 0;
modifier_key->key = g_object_ref (key);
@ -172,10 +175,12 @@ set_key_states (LevelKeyboard *keyboard,
g_list_free1 (head);
head = next;
}
keyboard->level = 0;
return 0;
}
return new_level;
}
// FIXME: unhardcode, parse some user information as to which key triggers which view (level)
static void
set_level_from_press (LevelKeyboard *keyboard, EekKey *key)
{
@ -184,31 +189,21 @@ set_level_from_press (LevelKeyboard *keyboard, EekKey *key)
/* Handle non-emitting keys */
if (key) {
const gchar *name = eek_element_get_name(EEK_ELEMENT(key));
if (g_strcmp0(name, "ABC123-0") == 0 || g_strcmp0(name, "ABC123-1") == 0) {
if (g_strcmp0(name, "show_numbers") == 0) {
level = 2;
} else if (g_strcmp0(name, "ABC123-2") == 0 || g_strcmp0(name, "ABC123-3") == 0) {
} else if (g_strcmp0(name, "show_letters") == 0) {
level = 0;
} else if (g_strcmp0(name, "LFSH-0") == 0) {
level = 1;
} else if (g_strcmp0(name, "LFSH-1") == 0) {
level = 0;
} else if (g_strcmp0(name, "LFSH-2") == 0) {
level = 3;
} else if (g_strcmp0(name, "LFSH-3") == 0) {
level = 2;
} else if (g_strcmp0(name, "Shift_L") == 0) {
level ^= 1;
}
}
set_key_states(keyboard, key);
keyboard->level = level;
keyboard->level = set_key_states(keyboard, key, level);
eek_layout_update_layout(keyboard);
}
void eek_keyboard_press_key(LevelKeyboard *keyboard, EekKey *key, guint32 timestamp) {
EekKeyboardPrivate *priv = EEK_KEYBOARD_GET_PRIVATE(level_keyboard_current(keyboard));
eek_key_set_pressed(key, TRUE);
keyboard->pressed_keys = g_list_prepend (keyboard->pressed_keys, key);
@ -218,8 +213,8 @@ void eek_keyboard_press_key(LevelKeyboard *keyboard, EekKey *key, guint32 timest
if (!symbol)
return;
set_key_states (keyboard, key);
set_level_from_press (keyboard, key);
// Only take action about setting level *after* the key has taken effect, i.e. on release
//set_level_from_press (keyboard, key);
// "Borrowed" from eek-context-service; doesn't influence the state but forwards the event
@ -231,8 +226,6 @@ void eek_keyboard_press_key(LevelKeyboard *keyboard, EekKey *key, guint32 timest
void eek_keyboard_release_key(LevelKeyboard *keyboard,
EekKey *key,
guint32 timestamp) {
EekKeyboardPrivate *priv = EEK_KEYBOARD_GET_PRIVATE(level_keyboard_current(keyboard));
for (GList *head = keyboard->pressed_keys; head; head = g_list_next (head)) {
if (head->data == key) {
keyboard->pressed_keys = g_list_remove_link (keyboard->pressed_keys, head);
@ -246,7 +239,6 @@ void eek_keyboard_release_key(LevelKeyboard *keyboard,
if (!symbol)
return;
set_key_states (keyboard, key);
set_level_from_press (keyboard, key);
// "Borrowed" from eek-context-service; doesn't influence the state but forwards the event
@ -311,7 +303,6 @@ eek_keyboard_class_init (EekKeyboardClass *klass)
{
EekContainerClass *container_class = EEK_CONTAINER_CLASS (klass);
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
GParamSpec *pspec;
/* signals */
container_class->child_added = eek_keyboard_real_child_added;

View File

@ -121,7 +121,8 @@ on_unlocked (EekKey *key,
static EekKey *
eek_section_real_create_key (EekSection *self,
const gchar *name,
gint keycode)
gint keycode,
guint oref)
{
EekSectionPrivate *priv = (EekSectionPrivate*)eek_section_get_instance_private (self);
@ -131,8 +132,9 @@ eek_section_real_create_key (EekSection *self,
EekKey *key = (EekKey*)g_object_new (EEK_TYPE_KEY,
"name", name,
NULL);
eek_key_set_keycode(key, keycode);
g_return_val_if_fail (key, NULL);
eek_key_set_keycode(key, keycode);
eek_key_set_oref(key, oref);
EEK_CONTAINER_GET_CLASS(self)->add_child (EEK_CONTAINER(self),
EEK_ELEMENT(key));
@ -140,6 +142,25 @@ eek_section_real_create_key (EekSection *self,
return key;
}
EekKey *eek_section_create_button(EekSection *self,
const gchar *name,
struct squeek_key *state) {
EekSectionPrivate *priv = (EekSectionPrivate*)eek_section_get_instance_private (self);
EekRow *row = &priv->row;
row->num_columns++;
EekKey *key = (EekKey*)g_object_new (EEK_TYPE_KEY,
"name", name,
NULL);
g_return_val_if_fail (key, NULL);
eek_key_share_state(key, state);
EEK_CONTAINER_GET_CLASS(self)->add_child (EEK_CONTAINER(self),
EEK_ELEMENT(key));
return key;
}
static void
eek_section_finalize (GObject *object)
{
@ -383,12 +404,13 @@ eek_section_get_row (EekSection *section,
EekKey *
eek_section_create_key (EekSection *section,
const gchar *name,
guint keycode)
guint keycode,
guint oref)
{
g_return_val_if_fail (EEK_IS_SECTION(section), NULL);
return EEK_SECTION_GET_CLASS(section)->create_key (section,
return eek_section_real_create_key (section,
name,
keycode);
keycode, oref);
}
const double keyspacing = 4.0;

View File

@ -29,6 +29,7 @@
#include "eek-container.h"
#include "eek-types.h"
#include "eek-keyboard.h"
#include "src/keyboard.h"
G_BEGIN_DECLS
@ -96,8 +97,10 @@ void eek_section_get_row (EekSection *section,
EekKey *eek_section_create_key (EekSection *section,
const gchar *name,
guint keycode);
guint keycode, guint oref);
EekKey *eek_section_create_button(EekSection *self,
const gchar *name,
struct squeek_key *state);
void eek_section_place_keys (EekSection *section, LevelKeyboard *keyboard);
G_END_DECLS

View File

@ -237,40 +237,42 @@ struct _GeometryParseData {
guint view_idx;
EekSection *section;
EekKey *key;
gint num_columns;
gint num_rows;
EekOrientation orientation;
gdouble corner_radius;
GSList *points;
gchar *name;
EekOutline outline;
gchar *oref;
gchar *outline_id;
guint keycode;
GString *text;
GArray *outline_array;
GHashTable *name_key_hash; // char* -> EekKey*
GHashTable *key_oref_hash;
GHashTable *oref_outline_hash;
GHashTable *keyname_oref_hash; // char* -> guint
GHashTable *outlineid_oref_hash; // char* -> guint
};
typedef struct _GeometryParseData GeometryParseData;
static GeometryParseData *
geometry_parse_data_new (EekKeyboard **views, GHashTable *name_key_hash)
geometry_parse_data_new (EekKeyboard **views, GHashTable *name_key_hash, GArray *outline_array)
{
GeometryParseData *data = g_slice_new0 (GeometryParseData);
data->views = views;
data->key_oref_hash =
g_hash_table_new_full (g_direct_hash,
g_direct_equal,
NULL,
g_free);
data->oref_outline_hash =
data->outline_array = outline_array;
data->keyname_oref_hash =
g_hash_table_new_full (g_str_hash,
g_str_equal,
g_free,
(GDestroyNotify)eek_outline_free);
NULL);
data->outlineid_oref_hash =
g_hash_table_new_full (g_str_hash,
g_str_equal,
g_free,
NULL);
data->name_key_hash = name_key_hash;
data->text = g_string_sized_new (BUFSIZE);
@ -281,8 +283,8 @@ geometry_parse_data_new (EekKeyboard **views, GHashTable *name_key_hash)
static void
geometry_parse_data_free (GeometryParseData *data)
{
g_hash_table_destroy (data->key_oref_hash);
g_hash_table_destroy (data->oref_outline_hash);
g_hash_table_destroy (data->keyname_oref_hash);
g_hash_table_destroy (data->outlineid_oref_hash);
g_string_free (data->text, TRUE);
g_slice_free (GeometryParseData, data);
}
@ -409,17 +411,21 @@ geometry_start_element_callback (GMarkupParseContext *pcontext,
const gchar *keycode_name = get_attribute (attribute_names, attribute_values,
"keycode");
for (uint i = 0; i < 4; i++) {
gchar *name = g_strdup_printf("%s-%d", base_name, i);
EekKey *key = g_hash_table_lookup(data->name_key_hash, name);
if (!key) {
continue; // not all keys have to be defined in every view
}
g_hash_table_insert (data->key_oref_hash,
key,
g_strdup (oref_name));
guint oref = GPOINTER_TO_UINT(g_hash_table_lookup(data->outlineid_oref_hash,
oref_name));
const gchar *name = base_name;
g_hash_table_insert (data->keyname_oref_hash,
g_strdup(name),
GUINT_TO_POINTER(oref));
EekKey *key = g_hash_table_lookup(data->name_key_hash, name);
// never gets used! this section gets executed before any buttons get defined
if (key) {
if (keycode_name != NULL) {
// This sets the keycode for all buttons,
// since they share state
// TODO: get rid of this in the parser;
// this belongs after keymap is defined
eek_key_set_keycode(key, strtol (keycode_name, NULL, 10));
}
}
@ -436,7 +442,7 @@ geometry_start_element_callback (GMarkupParseContext *pcontext,
"no \"id\" attribute for \"outline\"");
return;
}
data->oref = g_strdup (attribute);
data->outline_id = g_strdup (attribute);
attribute = get_attribute (attribute_names, attribute_values,
"corner-radius");
@ -484,6 +490,29 @@ geometry_start_element_callback (GMarkupParseContext *pcontext,
data->text->len = 0;
}
/**
* eek_keyboard_add_outline:
* @keyboard: an #EekKeyboard
* @outline: an #EekOutline
*
* Register an outline of @keyboard.
* Returns: an unsigned integer ID of the registered outline, for
* later reference
*/
static guint
add_outline (GArray *outline_array,
EekOutline *outline)
{
EekOutline *_outline;
_outline = eek_outline_copy (outline);
g_array_append_val (outline_array, *_outline);
/* don't use eek_outline_free here, so as to keep _outline->points */
g_slice_free (EekOutline, _outline);
return outline_array->len - 1;
}
static void
geometry_end_element_callback (GMarkupParseContext *pcontext,
const gchar *element_name,
@ -529,24 +558,33 @@ geometry_end_element_callback (GMarkupParseContext *pcontext,
break;
}
// Save button name together with its level,
// to account for buttons with the same name in multiple levels
gchar *base_name = g_strndup (&text[start], end - start);
gchar *name = g_strdup_printf("%s-%d", base_name, data->view_idx);
g_free(base_name);
guint keycode = data->keycode++;
gchar *name = g_strndup (&text[start], end - start);
EekKey *key = g_hash_table_lookup(data->name_key_hash, name);
if (!key) {
// Save button name together with its level,
// to account for buttons with the same name in multiple levels
guint keycode = data->keycode++;
data->key = eek_section_create_key (data->section,
name,
keycode);
g_hash_table_insert (data->name_key_hash,
g_strdup(name),
data->key);
data->num_columns++;
g_hash_table_insert (data->key_oref_hash,
data->key,
g_strdup ("default"));
guint oref = GPOINTER_TO_UINT(g_hash_table_lookup(data->keyname_oref_hash, name));
// default value gives idx 0, which is guaranteed to be occupied
key = eek_section_create_key (data->section,
name,
keycode,
oref);
g_hash_table_insert (data->name_key_hash,
g_strdup(name),
key);
} else {
EekKey *new_key = eek_section_create_button(data->section, name, eek_key_get_state(key));
if (!new_key) {
g_set_error (error,
G_MARKUP_ERROR,
G_MARKUP_ERROR_MISSING_ATTRIBUTE,
"Couldn't create a shared button");
return;
}
eek_key_set_oref(new_key, eek_key_get_oref(key));
}
}
data->section = NULL;
@ -573,11 +611,11 @@ geometry_end_element_callback (GMarkupParseContext *pcontext,
g_slist_free (data->points);
data->points = NULL;
g_hash_table_insert (data->oref_outline_hash,
g_strdup (data->oref),
outline);
guint oref = add_outline (data->outline_array, outline);
g_free (data->oref);
g_hash_table_insert (data->outlineid_oref_hash,
data->outline_id,
GUINT_TO_POINTER(oref));
return;
}
}
@ -607,8 +645,6 @@ struct _SymbolsParseData {
LevelKeyboard *keyboard;
EekKeyboard *view;
guint key_level;
const gchar *key_name;
gchar *label;
gchar *icon;
@ -636,6 +672,7 @@ symbols_parse_data_free (SymbolsParseData *data)
static const gchar *symbols_valid_path_list[] = {
"symbols",
"symbol/symbols",
"include/symbols",
"key/symbols",
"text/key/symbols",
@ -662,22 +699,6 @@ symbols_start_element_callback (GMarkupParseContext *pcontext,
error))
return;
if (g_strcmp0 (element_name, "key") == 0) {
data->key_level = 0;
attribute = get_attribute (attribute_names, attribute_values,
"name");
if (attribute == NULL) {
g_set_error (error,
G_MARKUP_ERROR,
G_MARKUP_ERROR_MISSING_ATTRIBUTE,
"no \"name\" attribute for \"key\"");
return;
}
data->key_name = strdup(attribute);
goto out;
}
if (g_strcmp0 (element_name, "keysym") == 0) {
attribute = get_attribute (attribute_names, attribute_values,
"keyval");
@ -710,7 +731,6 @@ symbols_start_element_callback (GMarkupParseContext *pcontext,
data->tooltip = g_strdup (attribute);
}
out:
data->element_stack = g_slist_prepend (data->element_stack,
g_strdup (element_name));
data->text->len = 0;
@ -730,33 +750,28 @@ symbols_end_element_callback (GMarkupParseContext *pcontext,
data->element_stack = g_slist_next (data->element_stack);
g_slist_free1 (head);
// TODO: this could all be moved to text handler
text = g_strndup (data->text->str, data->text->len);
if (g_strcmp0 (element_name, "symbol") == 0 ||
g_strcmp0 (element_name, "keysym") == 0 ||
g_strcmp0 (element_name, "text") == 0) {
gchar *name = g_strdup_printf("%s-%d", data->key_name, data->key_level);
gchar *name = text;
EekKey *key = eek_keyboard_find_key_by_name (data->keyboard,
name);
if (key == NULL) {
g_set_error (error,
G_MARKUP_ERROR,
G_MARKUP_ERROR_INVALID_CONTENT,
"no such key %s", name);
return;
if (key) {
squeek_key_add_symbol(
eek_key_get_state(key),
element_name,
text,
data->keyval,
data->label,
data->icon,
data->tooltip
);
}
squeek_key_add_symbol(
eek_key_get_state(key),
element_name,
text,
data->keyval,
data->label,
data->icon,
data->tooltip
);
data->keyval = 0;
g_free(data->label);
data->label = NULL;
@ -764,8 +779,6 @@ symbols_end_element_callback (GMarkupParseContext *pcontext,
data->icon = NULL;
g_free(data->tooltip);
data->tooltip = NULL;
data->key_level++;
goto out;
}
@ -1135,36 +1148,11 @@ eek_xml_keyboard_desc_free (EekXmlKeyboardDesc *desc)
g_slice_free (EekXmlKeyboardDesc, desc);
}
/**
* eek_keyboard_add_outline:
* @keyboard: an #EekKeyboard
* @outline: an #EekOutline
*
* Register an outline of @keyboard.
* Returns: an unsigned integer ID of the registered outline, for
* later reference
*/
static guint
add_outline (GArray *outline_array,
EekOutline *outline)
{
EekOutline *_outline;
_outline = eek_outline_copy (outline);
g_array_append_val (outline_array, *_outline);
/* don't use eek_outline_free here, so as to keep _outline->points */
g_slice_free (EekOutline, _outline);
return outline_array->len - 1;
}
static gboolean
parse_geometry (const gchar *path, EekKeyboard **views, GArray *outline_array, GHashTable *name_key_hash, GError **error)
{
GeometryParseData *data;
GMarkupParseContext *pcontext;
GHashTable *oref_hash;
GHashTableIter iter;
gpointer k, v;
GFile *file;
GFileInputStream *input;
gboolean retval;
@ -1178,7 +1166,7 @@ parse_geometry (const gchar *path, EekKeyboard **views, GArray *outline_array, G
if (input == NULL)
return FALSE;
data = geometry_parse_data_new (views, name_key_hash);
data = geometry_parse_data_new (views, name_key_hash, outline_array);
pcontext = g_markup_parse_context_new (&geometry_parser,
0,
data,
@ -1193,6 +1181,11 @@ parse_geometry (const gchar *path, EekKeyboard **views, GArray *outline_array, G
}
/* Resolve outline references. */
/*
* GHashTable *oref_hash;
GHashTableIter iter;
gpointer k, v;
oref_hash = g_hash_table_new (g_str_hash, g_str_equal);
g_hash_table_iter_init (&iter, data->oref_outline_hash);
while (g_hash_table_iter_next (&iter, &k, &v)) {
@ -1201,15 +1194,16 @@ parse_geometry (const gchar *path, EekKeyboard **views, GArray *outline_array, G
oref = add_outline (outline_array, outline);
g_hash_table_insert (oref_hash, k, GUINT_TO_POINTER(oref));
}
}*/
/*
g_hash_table_iter_init (&iter, data->key_oref_hash);
while (g_hash_table_iter_next (&iter, &k, &v)) {
gpointer oref;
if (g_hash_table_lookup_extended (oref_hash, v, NULL, &oref))
eek_key_set_oref (EEK_KEY(k), GPOINTER_TO_UINT(oref));
}
g_hash_table_destroy (oref_hash);
}*/
// g_hash_table_destroy (oref_hash);
geometry_parse_data_free (data);
return TRUE;