diff --git a/eek/eek-keyboard.c b/eek/eek-keyboard.c index c4633a1d..24ef537b 100644 --- a/eek/eek-keyboard.c +++ b/eek/eek-keyboard.c @@ -20,25 +20,76 @@ #include "config.h" +#define _XOPEN_SOURCE 500 +#include +#include +#include +#include // TODO: this is Linux-specific +#include + + #include "eek-keyboard.h" -void level_keyboard_deinit(LevelKeyboard *self) { +void level_keyboard_free(LevelKeyboard *self) { xkb_keymap_unref(self->keymap); close(self->keymap_fd); squeek_layout_free(self->layout); -} - -void level_keyboard_free(LevelKeyboard *self) { - level_keyboard_deinit(self); g_free(self); } -void level_keyboard_init(LevelKeyboard *self, struct squeek_layout *layout) { - self->layout = layout; -} - -LevelKeyboard *level_keyboard_new(struct squeek_layout *layout) { +LevelKeyboard* +level_keyboard_new (const gchar *keyboard_type, + enum squeek_arrangement_kind t) +{ + struct squeek_layout *layout = squeek_load_layout(keyboard_type, t); LevelKeyboard *keyboard = g_new0(LevelKeyboard, 1); - level_keyboard_init(keyboard, layout); + + if (!keyboard) { + g_error("Failed to create a keyboard"); + } + keyboard->layout = layout; + + struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + if (!context) { + g_error("No context created"); + } + + const gchar *keymap_str = squeek_layout_get_keymap(keyboard->layout); + + struct xkb_keymap *keymap = xkb_keymap_new_from_string(context, keymap_str, + XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS); + + if (!keymap) + g_error("Bad keymap:\n%s", keymap_str); + + xkb_context_unref(context); + keyboard->keymap = keymap; + + 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); + for (unsigned i = 0; i < 6; i++) { + r[i] = (r[i] & 0b1111111) | 0b1000000; // A-z + r[i] = r[i] > 'z' ? '?' : r[i]; // The randomizer doesn't need to be good... + } + int keymap_fd = shm_open(path, O_RDWR | O_CREAT | O_EXCL, 0600); + if (keymap_fd < 0) { + g_error("Failed to set up keymap fd"); + } + keyboard->keymap_fd = keymap_fd; + shm_unlink(path); + if (ftruncate(keymap_fd, (off_t)keyboard->keymap_len)) { + g_error("Failed to increase keymap fd size"); + } + char *ptr = mmap(NULL, keyboard->keymap_len, PROT_WRITE, MAP_SHARED, + keymap_fd, 0); + if ((void*)ptr == (void*)-1) { + g_error("Failed to set up mmap"); + } + strncpy(ptr, keymap_str, keyboard->keymap_len); + munmap(ptr, keyboard->keymap_len); return keyboard; } diff --git a/eek/eek-keyboard.h b/eek/eek-keyboard.h index d36e3a46..fab549e1 100644 --- a/eek/eek-keyboard.h +++ b/eek/eek-keyboard.h @@ -28,7 +28,6 @@ #include #include #include "eek-types.h" -#include "eek-layout.h" #include "src/layout.h" G_BEGIN_DECLS @@ -47,8 +46,9 @@ typedef struct _LevelKeyboard LevelKeyboard; gchar * eek_keyboard_get_keymap (LevelKeyboard *keyboard); -LevelKeyboard *level_keyboard_new(struct squeek_layout *layout); -void level_keyboard_deinit(LevelKeyboard *self); +LevelKeyboard* +level_keyboard_new (const gchar *keyboard_type, + enum squeek_arrangement_kind t); void level_keyboard_free(LevelKeyboard *self); G_END_DECLS diff --git a/eek/eek-layout.c b/eek/eek-layout.c deleted file mode 100644 index ff4f96ca..00000000 --- a/eek/eek-layout.c +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (C) 2010-2011 Daiki Ueno - * Copyright (C) 2010-2011 Red Hat, Inc. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation; either version 2 of - * the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - * 02110-1301 USA - */ - -/** - * SECTION:eek-layout - * @short_description: Base class of a layout engine - * - * The #EekLayout class is a base class of layout engine which - * arranges keyboard elements. - */ - -#include "config.h" - -#include "eek-layout.h" -#include "eek-keyboard.h" -#include "eekboard/eekboard-context-service.h" -#include "eek-xml-layout.h" - -G_DEFINE_ABSTRACT_TYPE (EekLayout, eek_layout, G_TYPE_OBJECT) - -static void -eek_layout_class_init (EekLayoutClass *klass) -{ - klass->create_keyboard = NULL; -} - -void -eek_layout_init (EekLayout *self) -{ -} diff --git a/eek/eek-layout.h b/eek/eek-layout.h deleted file mode 100644 index 480f1692..00000000 --- a/eek/eek-layout.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (C) 2010-2011 Daiki Ueno - * Copyright (C) 2010-2011 Red Hat, Inc. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation; either version 2 of - * the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - * 02110-1301 USA - */ - -#if !defined(__EEK_H_INSIDE__) && !defined(EEK_COMPILATION) -#error "Only can be included directly." -#endif - -#ifndef EEK_LAYOUT_H -#define EEK_LAYOUT_H 1 - -#include -#include "eek-types.h" -#include "src/layout.h" - -G_BEGIN_DECLS - -#define EEK_TYPE_LAYOUT (eek_layout_get_type()) -G_DECLARE_DERIVABLE_TYPE (EekLayout, eek_layout, EEK, LAYOUT, GObject) - -/** - * EekLayoutClass: - * @create_keyboard: virtual function for creating a keyboard - */ -struct _EekLayoutClass -{ - /*< private >*/ - GObjectClass parent_class; - - /*< public >*/ - LevelKeyboard* (* create_keyboard) (EekboardContextService *manager, - EekLayout *self, - gdouble initial_width, - gdouble initial_height); - - /*< private >*/ - /* padding */ - gpointer pdummy[24]; -}; - -GType eek_layout_get_type (void) G_GNUC_CONST; - -G_END_DECLS -#endif /* EEK_LAYOUT_H */ diff --git a/eek/eek-xml-layout.c b/eek/eek-xml-layout.c deleted file mode 100644 index 64802b4c..00000000 --- a/eek/eek-xml-layout.c +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (C) 2011 Daiki Ueno - * Copyright (C) 2011 Red Hat, Inc. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -/** - * SECTION:eek-xml-layout - * @short_description: Layout engine which loads layout information from XML - */ - -#include "config.h" - -#include "eek-keyboard.h" -#include "src/layout.h" - -#include "eek-xml-layout.h" - -LevelKeyboard * -eek_xml_layout_real_create_keyboard (const char *keyboard_type, - enum squeek_arrangement_kind t) -{ - struct squeek_layout *layout = squeek_load_layout(keyboard_type, t); - return level_keyboard_new(layout); -} diff --git a/eek/eek-xml-layout.h b/eek/eek-xml-layout.h deleted file mode 100644 index 278537e6..00000000 --- a/eek/eek-xml-layout.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (C) 2011 Daiki Ueno - * Copyright (C) 2011 Red Hat, Inc. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#if !defined(__EEK_H_INSIDE__) && !defined(EEK_COMPILATION) -#error "Only can be included directly." -#endif - -#ifndef EEK_XML_LAYOUT_H -#define EEK_XML_LAYOUT_H 1 - -#include "eek-types.h" -#include "src/layout.h" - -G_BEGIN_DECLS - -LevelKeyboard * -eek_xml_layout_real_create_keyboard (const char *keyboard_type, - enum squeek_arrangement_kind t); -G_END_DECLS -#endif /* EEK_XML_LAYOUT_H */ diff --git a/eek/eek.h b/eek/eek.h index c1548121..5ba3d099 100644 --- a/eek/eek.h +++ b/eek/eek.h @@ -23,7 +23,6 @@ #define __EEK_H_INSIDE__ 1 #include "eek-keyboard.h" -#include "eek-layout.h" void eek_init (void); diff --git a/eekboard/eekboard-context-service.c b/eekboard/eekboard-context-service.c index e854ebc1..e9f7b9db 100644 --- a/eekboard/eekboard-context-service.c +++ b/eekboard/eekboard-context-service.c @@ -16,31 +16,15 @@ * along with this program. If not, see . */ -/** - * SECTION:eekboard-context-service - * @short_description: base server implementation of eekboard input - * context service - * - * The #EekboardService class provides a base server side - * implementation of eekboard input context service. - */ - #include "config.h" -#include #include -#define _XOPEN_SOURCE 500 -#include -#include -#include // TODO: this is Linux-specific -#include #include #include "wayland.h" #include "eek/eek-keyboard.h" -#include "eek/eek-xml-layout.h" #include "src/server-context-service.h" #include "eekboard/eekboard-context-service.h" @@ -80,60 +64,6 @@ struct _EekboardContextServicePrivate { G_DEFINE_TYPE_WITH_PRIVATE (EekboardContextService, eekboard_context_service, G_TYPE_OBJECT); -static LevelKeyboard * -eekboard_context_service_real_create_keyboard (const gchar *keyboard_type, - enum squeek_arrangement_kind t) -{ - LevelKeyboard *keyboard = eek_xml_layout_real_create_keyboard(keyboard_type, t); - if (!keyboard) { - g_error("Failed to create a keyboard"); - } - - struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); - if (!context) { - g_error("No context created"); - } - - const gchar *keymap_str = squeek_layout_get_keymap(keyboard->layout); - - struct xkb_keymap *keymap = xkb_keymap_new_from_string(context, keymap_str, - XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS); - - if (!keymap) - g_error("Bad keymap:\n%s", keymap_str); - - xkb_context_unref(context); - keyboard->keymap = keymap; - - 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); - for (unsigned i = 0; i < 6; i++) { - r[i] = (r[i] & 0b1111111) | 0b1000000; // A-z - r[i] = r[i] > 'z' ? '?' : r[i]; // The randomizer doesn't need to be good... - } - int keymap_fd = shm_open(path, O_RDWR | O_CREAT | O_EXCL, 0600); - if (keymap_fd < 0) { - g_error("Failed to set up keymap fd"); - } - keyboard->keymap_fd = keymap_fd; - shm_unlink(path); - if (ftruncate(keymap_fd, (off_t)keyboard->keymap_len)) { - g_error("Failed to increase keymap fd size"); - } - char *ptr = mmap(NULL, keyboard->keymap_len, PROT_WRITE, MAP_SHARED, - keymap_fd, 0); - if ((void*)ptr == (void*)-1) { - g_error("Failed to set up mmap"); - } - strncpy(ptr, keymap_str, keyboard->keymap_len); - munmap(ptr, keyboard->keymap_len); - return keyboard; -} - static void eekboard_context_service_set_property (GObject *object, guint prop_id, @@ -219,14 +149,12 @@ eekboard_context_service_update_layout(EekboardContextService *context, enum squ } // generic part follows - LevelKeyboard *keyboard = eekboard_context_service_real_create_keyboard(keyboard_layout, t); + LevelKeyboard *keyboard = level_keyboard_new(keyboard_layout, t); // set as current LevelKeyboard *previous_keyboard = context->priv->keyboard; context->priv->keyboard = keyboard; - // Update the keymap if necessary. - // Done directly here instead of in "keyboard" update handler - // to keep keymap-related actions close together. + // TODO: Update submission on change event if (context->priv->submission) { submission_set_keyboard(context->priv->submission, keyboard); } diff --git a/src/meson.build b/src/meson.build index 0eda4cf2..00d92a89 100644 --- a/src/meson.build +++ b/src/meson.build @@ -20,10 +20,8 @@ sources = [ '../eek/eek-element.c', '../eek/eek-gtk-keyboard.c', '../eek/eek-keyboard.c', - '../eek/eek-layout.c', '../eek/eek-renderer.c', '../eek/eek-types.c', - '../eek/eek-xml-layout.c', '../eek/layersurface.c', dbus_src, '../eekboard/eekboard-context-service.c',