From 295ec3790d191c41006d04b41db878dc1091b117 Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Wed, 23 Feb 2022 21:26:37 -0500 Subject: Core Keyremap Allow setting keymap from plugin Allow setting and removing keyremap on the fly It was pretty annoying trying to work out a keyremap with a restart required to set the remap and was quite annoying when I was no longer able to navigate to the plugin or filebrowser due to setting the wrong remap now you can try out a keymap and if it doesn't work a restart will sort things out Change-Id: I848fb3bd759f9684ac2497324a371f92b7464f7b --- apps/core_keymap.c | 49 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 9 deletions(-) (limited to 'apps/core_keymap.c') diff --git a/apps/core_keymap.c b/apps/core_keymap.c index 0a7241a9e0..131c48f45d 100644 --- a/apps/core_keymap.c +++ b/apps/core_keymap.c @@ -24,25 +24,56 @@ #include "core_keymap.h" #if !defined(__PCTOOL__) || defined(CHECKWPS) +static int keymap_handle = -1; + +static int core_alloc_keymap(size_t bufsz) +{ + keymap_handle = core_alloc_ex("key remap", bufsz, &buflib_ops_locked); + return keymap_handle; +} + +static void core_free_keymap(void) +{ + action_set_keymap(NULL, -1); + if (keymap_handle > 0) /* free old buffer */ + { + keymap_handle = core_free(keymap_handle); + } +} + +/* Allocates buffer from core and copies keymap into it */ +int core_set_keyremap(struct button_mapping* core_keymap, int count) +{ + + core_free_keymap(); + if (count > 0) + { + size_t bufsize = count * sizeof(struct button_mapping); + if (core_keymap != NULL && core_alloc_keymap(bufsize) > 0) + { + char *buf = core_get_data(keymap_handle); + memcpy(buf, core_keymap, bufsize); + count = action_set_keymap((struct button_mapping *) buf, count); + } + else + count = -1; + } + return count; +} + int core_load_key_remap(const char *filename) { - static int keymap_handle = -1; char *buf; int fd = -1; int count = 0; size_t fsize = 0; - if (keymap_handle > 0) /* free old buffer */ - { - action_set_keymap(NULL, -1); - keymap_handle = core_free(keymap_handle); - } + core_free_keymap(); + if (filename != NULL) count = open_key_remap(filename, &fd, &fsize); while (count > 0) { - - keymap_handle = core_alloc_ex("key remap", fsize, &buflib_ops_locked); - if (keymap_handle <= 0) + if (core_alloc_keymap(fsize) <= 0) { count = -30; break; -- cgit v1.2.3