Merge branch 'add-built-in-keymaps' into 'master'
Add built in keymaps See merge request Librem5/squeekboard!78
This commit is contained in:
		@ -28,6 +28,7 @@
 | 
			
		||||
#include "config.h"
 | 
			
		||||
 | 
			
		||||
#include "eekboard/eekboard-context-service.h"
 | 
			
		||||
#include "keymap.h"
 | 
			
		||||
 | 
			
		||||
#include <fcntl.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
@ -83,6 +84,53 @@ struct _EekboardContextServicePrivate {
 | 
			
		||||
G_DEFINE_TYPE_WITH_PRIVATE (EekboardContextService, eekboard_context_service, G_TYPE_OBJECT);
 | 
			
		||||
 | 
			
		||||
/*static Display *display = NULL; */
 | 
			
		||||
gchar *
 | 
			
		||||
get_keymap_from_resource(const gchar *keyboard_type, gboolean fallback)
 | 
			
		||||
{
 | 
			
		||||
    g_autoptr (GFile) file = NULL;
 | 
			
		||||
    g_autoptr (GFileInfo) info = NULL;
 | 
			
		||||
    g_autoptr (GFileInputStream) stream = NULL;
 | 
			
		||||
    goffset size = 0;
 | 
			
		||||
    gsize bytes_read = 0;
 | 
			
		||||
    g_autofree gchar *contents = NULL;
 | 
			
		||||
    g_autofree gchar *path = NULL;
 | 
			
		||||
    GError *error = NULL;
 | 
			
		||||
 | 
			
		||||
    if (fallback)
 | 
			
		||||
        g_debug ("falling back to loading a %s keymap", keyboard_type);
 | 
			
		||||
 | 
			
		||||
    path = g_strconcat ("resource:///sm/puri/squeekboard/keyboards/keymaps/",
 | 
			
		||||
                        keyboard_type, ".xkb", NULL);
 | 
			
		||||
    file = g_file_new_for_uri (path);
 | 
			
		||||
    stream = g_file_read (file, NULL, &error);
 | 
			
		||||
 | 
			
		||||
    if (!stream)
 | 
			
		||||
        goto keymap_error;
 | 
			
		||||
 | 
			
		||||
    info = g_file_input_stream_query_info (stream,
 | 
			
		||||
                                           G_FILE_ATTRIBUTE_STANDARD_SIZE,
 | 
			
		||||
                                           NULL,
 | 
			
		||||
                                           &error);
 | 
			
		||||
 | 
			
		||||
    if (!info)
 | 
			
		||||
        goto keymap_error;
 | 
			
		||||
 | 
			
		||||
    size = g_file_info_get_size (info);
 | 
			
		||||
    contents = g_malloc0 (size);
 | 
			
		||||
 | 
			
		||||
    if (!g_input_stream_read_all (G_INPUT_STREAM(stream), contents, size,
 | 
			
		||||
                                  &bytes_read, NULL, &error))
 | 
			
		||||
        goto keymap_error;
 | 
			
		||||
 | 
			
		||||
    return g_utf8_make_valid (contents, -1);
 | 
			
		||||
 | 
			
		||||
keymap_error:
 | 
			
		||||
    if (fallback)
 | 
			
		||||
        g_error ("failed to load keymap from resource: %s", error->message);
 | 
			
		||||
 | 
			
		||||
    g_error_free (error);
 | 
			
		||||
    return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static EekKeyboard *
 | 
			
		||||
eekboard_context_service_real_create_keyboard (EekboardContextService *self,
 | 
			
		||||
@ -143,18 +191,28 @@ eekboard_context_service_real_create_keyboard (EekboardContextService *self,
 | 
			
		||||
    if (!context) {
 | 
			
		||||
        g_error("No context created");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
    struct xkb_rule_names rules = { 0 };
 | 
			
		||||
    rules.layout = strdup(keyboard_type);
 | 
			
		||||
    struct xkb_keymap *keymap = xkb_keymap_new_from_names(context, &rules,
 | 
			
		||||
        XKB_KEYMAP_COMPILE_NO_FLAGS);
 | 
			
		||||
*/
 | 
			
		||||
    char *keymap_str = get_keymap_from_resource(keyboard_type, FALSE);
 | 
			
		||||
    if (!keymap_str)
 | 
			
		||||
        keymap_str = get_keymap_from_resource("us", TRUE);
 | 
			
		||||
 | 
			
		||||
    struct xkb_keymap *keymap = xkb_keymap_new_from_string(context, keymap_str,
 | 
			
		||||
        XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS);
 | 
			
		||||
 | 
			
		||||
    free(keymap_str);
 | 
			
		||||
 | 
			
		||||
    xkb_context_unref(context);
 | 
			
		||||
    if (!keymap) {
 | 
			
		||||
        g_error("Bad keymap");
 | 
			
		||||
    }
 | 
			
		||||
    keyboard->keymap = keymap;
 | 
			
		||||
    char *keymap_str = xkb_keymap_get_as_string(keymap, XKB_KEYMAP_FORMAT_TEXT_V1);
 | 
			
		||||
 | 
			
		||||
    keymap_str = xkb_keymap_get_as_string(keymap, XKB_KEYMAP_FORMAT_TEXT_V1);
 | 
			
		||||
    keyboard->keymap_len = strlen(keymap_str) + 1;
 | 
			
		||||
 | 
			
		||||
    g_autofree char *path = strdup("/eek_keymap-XXXXXX");
 | 
			
		||||
    char *r = &path[strlen(path) - 6];
 | 
			
		||||
    getrandom(r, 6, GRND_NONBLOCK);
 | 
			
		||||
@ -287,6 +345,7 @@ settings_update_layout(EekboardContextService *context)
 | 
			
		||||
    g_autofree gchar *keyboard_type = NULL;
 | 
			
		||||
    g_autofree gchar *keyboard_layout = NULL;
 | 
			
		||||
    settings_get_layout(context->priv->settings, &keyboard_type, &keyboard_layout);
 | 
			
		||||
    g_debug("type=%s, layout=%s", keyboard_type, keyboard_layout);
 | 
			
		||||
 | 
			
		||||
    if (!keyboard_type) {
 | 
			
		||||
        keyboard_type = g_strdup("us");
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user