diff options
Diffstat (limited to 'apps/core_keymap.c')
-rw-r--r-- | apps/core_keymap.c | 134 |
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) |
30 | static int keymap_handle = -1; | 30 | int core_set_keyremap(struct button_mapping* core_keymap, int count) |
31 | |||
32 | static 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 | ||
38 | static void core_free_keymap(void) | 35 | static 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 */ | ||
48 | int 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 | |||
67 | int 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 | ||
101 | int open_key_remap(const char *filename, int *fd, size_t *fsize) | 72 | int 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 | ||