Commit 2a9757e8 authored by Dorota Czaplejewicz's avatar Dorota Czaplejewicz
Browse files

Add layout-dependent keymaps

Keymaps used by the virtual-keyboard protocol will now be changed whenever the keyboard layout changes. That allows for the symbols on the keys to match what the virtual-keyboard protocol produces.
parent 0ef182eb
......@@ -132,6 +132,8 @@ struct layout {
const struct key *keys;
uint32_t count;
const char *keymap_str;
uint32_t columns;
uint32_t rows;
......@@ -303,6 +305,7 @@ static const struct key arabic_keys[] = {
static const struct layout normal_layout = {
normal_keys,
sizeof(normal_keys) / sizeof(*normal_keys),
keymap_normal,
12,
4,
"en",
......@@ -312,6 +315,7 @@ static const struct layout normal_layout = {
static const struct layout numeric_layout = {
numeric_keys,
sizeof(numeric_keys) / sizeof(*numeric_keys),
NULL,
12,
2,
"en",
......@@ -321,6 +325,7 @@ static const struct layout numeric_layout = {
static const struct layout arabic_layout = {
arabic_keys,
sizeof(arabic_keys) / sizeof(*arabic_keys),
NULL,
13,
4,
"ar",
......@@ -330,12 +335,23 @@ static const struct layout arabic_layout = {
static const struct layout polish_layout = {
polish_keys,
sizeof(polish_keys) / sizeof(*polish_keys),
NULL,
12,
4,
"pl",
true,
};
static const struct layout* layouts[] = {
&normal_layout,
&polish_layout,
&arabic_layout,
};
static const unsigned layouts_count = sizeof(layouts) / sizeof (*layouts);
static const char* default_keymap_str = keymap_normal;
static const double key_width = 60;
static const double key_height = 50;
......@@ -849,21 +865,36 @@ keyboard_handle_key(struct keyboard *keyboard, const struct layout *layout,
}
}
static void
make_virtual_keyboard(struct virtual_keyboard *virtual_keyboard) {
struct zwp_virtual_keyboard_v1 *vkeyboard;
vkeyboard = zwp_virtual_keyboard_manager_v1_create_virtual_keyboard(
virtual_keyboard->keyboard_manager,
virtual_keyboard->seat);
const char *keymap_str = keymap_data;
static int
update_keymap(struct virtual_keyboard *virtual_keyboard)
{
const char *keymap_str = default_keymap_str;
for (unsigned i = 0; i < layouts_count; i++) {
if (strcmp(layouts[i]->language,
virtual_keyboard->preferred_layout) == 0) {
keymap_str = layouts[i]->keymap_str;
break;
}
}
#ifdef KEYMAP_FILE
// Testing only
char fname[80];
snprintf(fname, 79, "./keymap_%s",
virtual_keyboard->preferred_layout
? virtual_keyboard->preferred_layout : "en");
int ffd;
struct stat sb;
ffd = open("./keymap", O_RDONLY);
ffd = open(fname, O_RDONLY);
if (ffd < 0) {
return -1;
}
fstat(ffd, &sb);
keymap_str = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, ffd, 0);
if (!keymap_str) {
close(ffd);
return -1;
}
#endif
size_t keymap_size = strlen(keymap_str) + 1;
......@@ -871,20 +902,31 @@ make_virtual_keyboard(struct virtual_keyboard *virtual_keyboard) {
if (keymap_fd < 0) {
goto err;
}
// TODO: save those inside layouts
void *ptr = mmap(NULL, keymap_size, PROT_READ | PROT_WRITE, MAP_SHARED,
keymap_fd, 0);
if (ptr == (void*)-1) {
goto err2;
}
strcpy(ptr, keymap_str);
zwp_virtual_keyboard_v1_keymap(vkeyboard, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
keymap_fd, keymap_size);
virtual_keyboard->virtual_keyboard = vkeyboard;
return;
zwp_virtual_keyboard_v1_keymap(virtual_keyboard->virtual_keyboard,
WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
keymap_fd, keymap_size);
return 0;
err2:
close(keymap_fd);
err:
return;
return 1;
}
static void
make_virtual_keyboard(struct virtual_keyboard *virtual_keyboard)
{
struct zwp_virtual_keyboard_v1 *vkeyboard;
vkeyboard = zwp_virtual_keyboard_manager_v1_create_virtual_keyboard(
virtual_keyboard->keyboard_manager,
virtual_keyboard->seat);
virtual_keyboard->virtual_keyboard = vkeyboard;
}
......@@ -1399,6 +1441,7 @@ settings_handle_layout_changed(GSettings *s,
{
struct virtual_keyboard *keyboard = user_data;
settings_set_layout(keyboard);
update_keymap(keyboard);
if (!keyboard->keyboard->window) {
return TRUE;
}
......@@ -1417,6 +1460,7 @@ settings_listen(struct virtual_keyboard *virtual_keyboard)
G_CALLBACK(settings_handle_layout_changed),
virtual_keyboard) == 0;
settings_set_layout(virtual_keyboard);
update_keymap(virtual_keyboard);
return conn_id;
}
......@@ -1463,6 +1507,10 @@ main(int argc, char *argv[])
make_input_method(&virtual_keyboard);
}
make_virtual_keyboard(&virtual_keyboard);
if (settings_listen(&virtual_keyboard) != 0) {
fprintf(stderr, "Could not connect to gsettings updates, layout"
" changing unavailable\n");
}
keyboard_create(&virtual_keyboard);
if (dbus_osk_setup(&virtual_keyboard) != 0) {
......@@ -1472,11 +1520,6 @@ main(int argc, char *argv[])
session_register();
if (settings_listen(&virtual_keyboard) != 0) {
fprintf(stderr, "Could not connect to gsettings updates, layout"
" changing unavailable\n");
}
display_run(virtual_keyboard.display);
......
static const char keymap_data[] = "xkb_keymap {\
static const char keymap_normal[] = "xkb_keymap {\
xkb_keycodes \"(unnamed)\" {\
minimum = 8;\
maximum = 255;\
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment