summaryrefslogtreecommitdiff
path: root/apps/core_keymap.c
diff options
context:
space:
mode:
authorAidan MacDonald <amachronic@protonmail.com>2022-04-03 13:48:52 +0100
committerAidan MacDonald <amachronic@protonmail.com>2022-09-25 11:05:59 +0100
commite4aec7d648de2553653b378518b5e90f29aeeac3 (patch)
tree37c1f0f542742032573d1a99cc4b68c061fedb10 /apps/core_keymap.c
parentf47aa584a8b447d8225fc5b09afb2d1fe6764c1d (diff)
downloadrockbox-e4aec7d648de2553653b378518b5e90f29aeeac3.tar.gz
rockbox-e4aec7d648de2553653b378518b5e90f29aeeac3.zip
key remap: simplify and use movable allocations
Have action.c control the key remap buflib allocation so that it can be made movable. With memory management offloaded, core_keymap.c only needs to deal with loading keymap files. Simplify the code there and use buflib pinning so the file can be loaded directly into the buffer. Change-Id: Ia654cc05ce6b286f96c1031fa4f7d4b3859a2c1a
Diffstat (limited to 'apps/core_keymap.c')
-rw-r--r--apps/core_keymap.c134
1 files changed, 41 insertions, 93 deletions
diff --git a/apps/core_keymap.c b/apps/core_keymap.c
index dbe7ae0072..9d54fcffac 100644
--- a/apps/core_keymap.c
+++ b/apps/core_keymap.c
@@ -27,119 +27,67 @@
27#include "logf.h" 27#include "logf.h"
28 28
29#if !defined(__PCTOOL__) || defined(CHECKWPS) 29#if !defined(__PCTOOL__) || defined(CHECKWPS)
30static int keymap_handle = -1; 30int core_set_keyremap(struct button_mapping* core_keymap, int count)
31
32static int core_alloc_keymap(size_t bufsz)
33{ 31{
34 keymap_handle = core_alloc_ex("key remap", bufsz, &buflib_ops_locked); 32 return action_set_keymap(core_keymap, count);
35 return keymap_handle;
36} 33}
37 34
38static void core_free_keymap(void) 35static int open_key_remap(const char *filename, int *countp)
39{ 36{
40 action_set_keymap(NULL, -1); 37 int fd = open(filename, O_RDONLY);
41 if (keymap_handle > 0) /* free old buffer */ 38 if (fd < 0)
39 return fd;
40
41 size_t fsize = filesize(fd);
42 int count = fsize / sizeof(struct button_mapping);
43 if (count == 0 || (size_t)(count * sizeof(struct button_mapping)) != fsize)
42 { 44 {
43 keymap_handle = core_free(keymap_handle); 45 logf("core_keyremap: bad filesize %d / %lu", count, (unsigned long)fsize);
46 goto error;
44 } 47 }
45}
46
47/* Allocates buffer from core and copies keymap into it */
48int core_set_keyremap(struct button_mapping* core_keymap, int count)
49{
50 48
51 core_free_keymap(); 49 struct button_mapping header;
52 if (count > 0) 50 if(read(fd, &header, sizeof(header)) != (ssize_t)sizeof(header))
53 { 51 {
54 size_t bufsize = count * sizeof(struct button_mapping); 52 logf("core_keyremap: read error");
55 if (core_keymap != NULL && core_alloc_keymap(bufsize) > 0) 53 goto error;
56 {
57 char *buf = core_get_data(keymap_handle);
58 memcpy(buf, core_keymap, bufsize);
59 count = action_set_keymap((struct button_mapping *) buf, count);
60 }
61 else
62 count = -1;
63 } 54 }
64 return count;
65}
66
67int core_load_key_remap(const char *filename)
68{
69 char *buf;
70 int fd = -1;
71 int count = 0;
72 size_t fsize = 0;
73 core_free_keymap();
74 55
75 if (filename != NULL) 56 if (header.action_code != KEYREMAP_VERSION ||
76 count = open_key_remap(filename, &fd, &fsize); 57 header.button_code != KEYREMAP_HEADERID ||
77 while (count > 0) 58 header.pre_button_code != count)
78 { 59 {
79 if (core_alloc_keymap(fsize) <= 0) 60 logf("core_keyremap: bad header %d", count);
80 { 61 goto error;
81 count = -30;
82 logf("core_keymap: %d Failed to allocate buffer", count);
83 break;
84 }
85 buf = core_get_data(keymap_handle);
86 if (read(fd, buf, fsize) == (ssize_t) fsize)
87 {
88 count = action_set_keymap((struct button_mapping *) buf, count);
89 }
90 else
91 {
92 count = -40;
93 logf("core_keymap: %d Failed to read", count);
94 }
95 break;
96 } 62 }
63
64 *countp = count - 1;
65 return fd;
66
67 error:
97 close(fd); 68 close(fd);
98 return count; 69 return -1;
99} 70}
100 71
101int open_key_remap(const char *filename, int *fd, size_t *fsize) 72int core_load_key_remap(const char *filename)
102{ 73{
103 int count = 0; 74 int count = 0; /* gcc falsely believes this may be used uninitialized */
75 int fd = open_key_remap(filename, &count);
76 if (fd < 0)
77 return -1;
104 78
105 while (filename && fd && fsize) 79 size_t bufsize = count * sizeof(struct button_mapping);
80 int handle = core_alloc("keyremap", bufsize);
81 if (handle > 0)
106 { 82 {
107 *fsize = 0; 83 core_pin(handle);
108 *fd = open(filename, O_RDONLY); 84 if (read(fd, core_get_data(handle), bufsize) == (ssize_t)bufsize)
109 if (*fd) 85 count = action_set_keymap_handle(handle, count);
110 {
111 *fsize = filesize(*fd);
112
113 count = *fsize / sizeof(struct button_mapping);
114
115 if (count * sizeof(struct button_mapping) != *fsize)
116 {
117 count = -10;
118 logf("core_keymap: %d Size mismatch", count);
119 break;
120 }
121 86
122 if (count > 1) 87 core_unpin(handle);
123 {
124 struct button_mapping header = {0};
125 read(*fd, &header, sizeof(struct button_mapping));
126 if (KEYREMAP_VERSION == header.action_code &&
127 KEYREMAP_HEADERID == header.button_code &&
128 header.pre_button_code == count)
129 {
130 count--;
131 *fsize -= sizeof(struct button_mapping);
132 }
133 else /* Header mismatch */
134 {
135 count = -20;
136 logf("core_keymap: %d Header mismatch", count);
137 break;
138 }
139 }
140 }
141 break;
142 } 88 }
89
90 close(fd);
143 return count; 91 return count;
144} 92}
145 93