summaryrefslogtreecommitdiff
path: root/apps/core_keymap.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/core_keymap.c')
-rw-r--r--apps/core_keymap.c106
1 files changed, 106 insertions, 0 deletions
diff --git a/apps/core_keymap.c b/apps/core_keymap.c
new file mode 100644
index 0000000000..0a7241a9e0
--- /dev/null
+++ b/apps/core_keymap.c
@@ -0,0 +1,106 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2020 by William Wilgus
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include "action.h"
23#include "core_alloc.h"
24#include "core_keymap.h"
25
26#if !defined(__PCTOOL__) || defined(CHECKWPS)
27int core_load_key_remap(const char *filename)
28{
29 static int keymap_handle = -1;
30 char *buf;
31 int fd = -1;
32 int count = 0;
33 size_t fsize = 0;
34 if (keymap_handle > 0) /* free old buffer */
35 {
36 action_set_keymap(NULL, -1);
37 keymap_handle = core_free(keymap_handle);
38 }
39 if (filename != NULL)
40 count = open_key_remap(filename, &fd, &fsize);
41 while (count > 0)
42 {
43
44 keymap_handle = core_alloc_ex("key remap", fsize, &buflib_ops_locked);
45 if (keymap_handle <= 0)
46 {
47 count = -30;
48 break;
49 }
50 buf = core_get_data(keymap_handle);
51 if (read(fd, buf, fsize) == (ssize_t) fsize)
52 {
53 count = action_set_keymap((struct button_mapping *) buf, count);
54 }
55 else
56 count = -40;
57 break;
58 }
59 close(fd);
60 return count;
61}
62
63int open_key_remap(const char *filename, int *fd, size_t *fsize)
64{
65 int count = 0;
66
67 while (filename && fd && fsize)
68 {
69 *fsize = 0;
70 *fd = open(filename, O_RDONLY);
71 if (*fd)
72 {
73 *fsize = filesize(*fd);
74
75 count = *fsize / sizeof(struct button_mapping);
76
77 if (count * sizeof(struct button_mapping) != *fsize)
78 {
79 count = -10;
80 break;
81 }
82
83 if (count > 1)
84 {
85 struct button_mapping header = {0};
86 read(*fd, &header, sizeof(struct button_mapping));
87 if (KEYREMAP_VERSION == header.action_code &&
88 KEYREMAP_HEADERID == header.button_code &&
89 header.pre_button_code == count)
90 {
91 count--;
92 *fsize -= sizeof(struct button_mapping);
93 }
94 else /* Header mismatch */
95 {
96 count = -20;
97 break;
98 }
99 }
100 }
101 break;
102 }
103 return count;
104}
105
106#endif /* !defined(__PCTOOL__) */