1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2020 by William Wilgus
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include "action.h"
#include "core_alloc.h"
#include "core_keymap.h"
/*#define LOGF_ENABLE*/
#include "logf.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)
{
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 (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;
}
close(fd);
return count;
}
int open_key_remap(const char *filename, int *fd, size_t *fsize)
{
int count = 0;
while (filename && fd && fsize)
{
*fsize = 0;
*fd = open(filename, O_RDONLY);
if (*fd)
{
*fsize = filesize(*fd);
count = *fsize / sizeof(struct button_mapping);
if (count * sizeof(struct button_mapping) != *fsize)
{
count = -10;
logf("core_keymap: %d Size mismatch", count);
break;
}
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;
}
return count;
}
#endif /* !defined(__PCTOOL__) */
|