summaryrefslogtreecommitdiff
path: root/utils/nwztools/plattools/nwz_lib.c
diff options
context:
space:
mode:
Diffstat (limited to 'utils/nwztools/plattools/nwz_lib.c')
-rw-r--r--utils/nwztools/plattools/nwz_lib.c159
1 files changed, 159 insertions, 0 deletions
diff --git a/utils/nwztools/plattools/nwz_lib.c b/utils/nwztools/plattools/nwz_lib.c
new file mode 100644
index 0000000000..ec862e638b
--- /dev/null
+++ b/utils/nwztools/plattools/nwz_lib.c
@@ -0,0 +1,159 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2016 Amaury Pouly
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#include "nwz_lib.h"
22
23void nwz_run(const char *file, const char *args[], bool wait)
24{
25 pid_t child_pid = fork();
26 if(child_pid != 0)
27 {
28 if(wait)
29 {
30 int status;
31 waitpid(child_pid, &status, 0);
32 }
33 }
34 else
35 {
36 execvp(file, (char * const *)args);
37 _exit(1);
38 }
39}
40
41void nwz_lcdmsg(bool clear, int x, int y, const char *msg)
42{
43 const char *path_lcdmsg = "/usr/local/bin/lcdmsg";
44 const char *args[16];
45 int index = 0;
46 char locate[32];
47 args[index++] = path_lcdmsg;
48 if(clear)
49 args[index++] = "-c";
50 args[index++] = "-f";
51 args[index++] = "/usr/local/bin/font_08x12.bmp";
52 args[index++] = "-l";
53 sprintf(locate, "%d,%d", x, y);
54 args[index++] = locate;
55 args[index++] = msg;
56 args[index++] = NULL;
57 /* wait for lcdmsg to finish to avoid any race conditions in framebuffer
58 * accesses */
59 nwz_run(path_lcdmsg, args, true);
60}
61
62void nwz_lcdmsgf(bool clear, int x, int y, const char *format, ...)
63{
64 char buffer[1024];
65 va_list args;
66 va_start(args, format);
67 vsprintf(buffer, format, args);
68 va_end(args);
69 nwz_lcdmsg(clear, x, y, buffer);
70}
71
72int nwz_key_open(void)
73{
74 return open("/dev/input/event0", O_RDONLY);
75}
76
77void nwz_key_close(int fd)
78{
79 close(fd);
80}
81
82int nwz_key_get_hold_status(int fd)
83{
84 unsigned long led_hold = 0;
85 if(ioctl(fd, EVIOCGLED(sizeof(led_hold)), &led_hold) < 0)
86 return -1;
87 return led_hold;
88}
89
90int nwz_key_wait_event(int fd, long tmo_us)
91{
92 fd_set rfds;
93 struct timeval tv;
94 struct timeval *tv_ptr = NULL;
95 /* watch the input device */
96 FD_ZERO(&rfds);
97 FD_SET(fd, &rfds);
98 /* setup timeout */
99 if(tmo_us >= 0)
100 {
101 tv.tv_sec = 0;
102 tv.tv_usec = tmo_us;
103 tv_ptr = &tv;
104 }
105 return select(fd + 1, &rfds, NULL, NULL, tv_ptr);
106}
107
108int nwz_key_read_event(int fd, struct input_event *evt)
109{
110 int ret = read(fd, evt, sizeof(struct input_event));
111 if(ret != sizeof(struct input_event))
112 return -1;
113 return 1;
114}
115
116int nwz_key_event_get_keycode(struct input_event *evt)
117{
118 return evt->code & NWZ_KEY_MASK;
119}
120
121bool nwz_key_event_is_press(struct input_event *evt)
122{
123 return evt->value == 0;
124}
125
126bool nwz_key_event_get_hold_status(struct input_event *evt)
127{
128 return !!(evt->code & NWZ_KEY_HOLD_MASK);
129}
130
131static const char *nwz_keyname[NWZ_KEY_MASK + 1] =
132{
133 [0 ... NWZ_KEY_MASK] = "unknown",
134 [NWZ_KEY_PLAY] = "PLAY",
135 [NWZ_KEY_RIGHT] = "RIGHT",
136 [NWZ_KEY_LEFT] = "LEFT",
137 [NWZ_KEY_UP] = "UP",
138 [NWZ_KEY_DOWN] = "DOWN",
139 [NWZ_KEY_ZAPPIN] = "ZAPPIN",
140 [NWZ_KEY_AD0_6] = "AD0_6",
141 [NWZ_KEY_AD0_7] = "AD0_7",
142 [NWZ_KEY_NONE] = "NONE",
143 [NWZ_KEY_VOL_DOWN] = "VOL DOWN",
144 [NWZ_KEY_VOL_UP] = "VOL UP",
145 [NWZ_KEY_BACK] = "BACK",
146 [NWZ_KEY_OPTION] = "OPTION",
147 [NWZ_KEY_BT] = "BT",
148 [NWZ_KEY_AD1_5] = "AD1_5",
149 [NWZ_KEY_AD1_6] = "AD1_6",
150 [NWZ_KEY_AD1_7] = "AD1_7",
151};
152
153const char *nwz_key_get_name(int keycode)
154{
155 if(keycode <0 || keycode > NWZ_KEY_MASK)
156 return "invalid";
157 else
158 return nwz_keyname[keycode];
159}