A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita
audio
rust
zig
deno
mpris
rockbox
mpd
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/*#define LOGF_ENABLE*/
27#include "logf.h"
28
29#if !defined(__PCTOOL__) || defined(CHECKWPS)
30int core_set_keyremap(struct button_mapping* core_keymap, int count)
31{
32 return action_set_keymap(core_keymap, count);
33}
34
35static int open_key_remap(const char *filename, int *countp)
36{
37 int fd = open(filename, O_RDONLY);
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)
44 {
45 logf("core_keyremap: bad filesize %d / %lu", count, (unsigned long)fsize);
46 goto error;
47 }
48
49 struct button_mapping header;
50 if(read(fd, &header, sizeof(header)) != (ssize_t)sizeof(header))
51 {
52 logf("core_keyremap: read error");
53 goto error;
54 }
55
56 if (header.action_code != KEYREMAP_VERSION ||
57 header.button_code != KEYREMAP_HEADERID ||
58 header.pre_button_code != count)
59 {
60 logf("core_keyremap: bad header %d", count);
61 goto error;
62 }
63
64 *countp = count - 1;
65 return fd;
66
67 error:
68 close(fd);
69 return -1;
70}
71
72int INIT_ATTR core_load_key_remap(const char *filename)
73{
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;
78
79 size_t bufsize = count * sizeof(struct button_mapping);
80 int handle = core_alloc(bufsize);
81 if (handle > 0)
82 {
83 void *data = core_get_data_pinned(handle);
84
85 if (read(fd, data, bufsize) == (ssize_t)bufsize)
86 count = action_set_keymap_handle(handle, count);
87
88 core_put_data_pinned(data);
89 }
90
91 close(fd);
92 return count;
93}
94
95#endif /* !defined(__PCTOOL__) */