summaryrefslogtreecommitdiffstats
path: root/apps/core_keymap.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/core_keymap.c')
-rw-r--r--apps/core_keymap.c133
1 files changed, 41 insertions, 92 deletions
diff --git a/apps/core_keymap.c b/apps/core_keymap.c
index dbe7ae0072..89e7913c33 100644
--- a/apps/core_keymap.c
+++ b/apps/core_keymap.c
@@ -27,119 +27,68 @@
#include "logf.h"
#if !defined(__PCTOOL__) || defined(CHECKWPS)
-static int keymap_handle = -1;
-
-static int core_alloc_keymap(size_t bufsz)
+int core_set_keyremap(struct button_mapping* core_keymap, int count)
{
- keymap_handle = core_alloc_ex("key remap", bufsz, &buflib_ops_locked);
- return keymap_handle;
+ return action_set_keymap(core_keymap, count);
}
-static void core_free_keymap(void)
+static int open_key_remap(const char *filename, int *countp)
{
- action_set_keymap(NULL, -1);
- if (keymap_handle > 0) /* free old buffer */
+ int fd = open(filename, O_RDONLY);
+ if (fd < 0)
+ return fd;
+
+ size_t fsize = filesize(fd);
+ int count = fsize / sizeof(struct button_mapping);
+ if (count == 0 || (size_t)(count * sizeof(struct button_mapping)) != fsize)
{
- keymap_handle = core_free(keymap_handle);
+ logf("core_keyremap: bad filesize %d / %lu", count, (unsigned long)fsize);
+ goto error;
}
-}
-
-/* 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)
+ struct button_mapping header;
+ if(read(fd, &header, sizeof(header)) != (ssize_t)sizeof(header))
{
- 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;
+ logf("core_keyremap: read error");
+ goto error;
}
- return count;
-}
-int core_load_key_remap(const char *filename)
-{
- char *buf;
- int fd = -1;
- int count = 0;
- size_t fsize = 0;
- core_free_keymap();
-
- if (filename != NULL)
- count = open_key_remap(filename, &fd, &fsize);
- while (count > 0)
+ if (header.action_code != KEYREMAP_VERSION ||
+ header.button_code != KEYREMAP_HEADERID ||
+ header.pre_button_code != count)
{
- if (core_alloc_keymap(fsize) <= 0)
- {
- count = -30;
- logf("core_keymap: %d Failed to allocate buffer", count);
- break;
- }
- buf = core_get_data(keymap_handle);
- if (read(fd, buf, fsize) == (ssize_t) fsize)
- {
- count = action_set_keymap((struct button_mapping *) buf, count);
- }
- else
- {
- count = -40;
- logf("core_keymap: %d Failed to read", count);
- }
- break;
+ logf("core_keyremap: bad header %d", count);
+ goto error;
}
+
+ *countp = count - 1;
+ return fd;
+
+ error:
close(fd);
- return count;
+ return -1;
}
-int open_key_remap(const char *filename, int *fd, size_t *fsize)
+int core_load_key_remap(const char *filename)
{
- int count = 0;
+ int count = 0; /* gcc falsely believes this may be used uninitialized */
+ int fd = open_key_remap(filename, &count);
+ if (fd < 0)
+ return -1;
- while (filename && fd && fsize)
+ size_t bufsize = count * sizeof(struct button_mapping);
+ int handle = core_alloc(bufsize);
+ if (handle > 0)
{
- *fsize = 0;
- *fd = open(filename, O_RDONLY);
- if (*fd)
- {
- *fsize = filesize(*fd);
-
- count = *fsize / sizeof(struct button_mapping);
+ void *data = core_get_data_pinned(handle);
- if (count * sizeof(struct button_mapping) != *fsize)
- {
- count = -10;
- logf("core_keymap: %d Size mismatch", count);
- break;
- }
+ if (read(fd, data, bufsize) == (ssize_t)bufsize)
+ count = action_set_keymap_handle(handle, count);
- if (count > 1)
- {
- struct button_mapping header = {0};
- read(*fd, &header, sizeof(struct button_mapping));
- if (KEYREMAP_VERSION == header.action_code &&
- KEYREMAP_HEADERID == header.button_code &&
- header.pre_button_code == count)
- {
- count--;
- *fsize -= sizeof(struct button_mapping);
- }
- else /* Header mismatch */
- {
- count = -20;
- logf("core_keymap: %d Header mismatch", count);
- break;
- }
- }
- }
- break;
+ core_put_data_pinned(data);
}
+
+ close(fd);
return count;
}