-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboard.c
171 lines (146 loc) · 4.84 KB
/
keyboard.c
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Another Wii U Soft-Keyboard
*
* Copyright (C) 2017 Jonathan Neuschäfer <[email protected]>
*
* [3-clause BSD license:]
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* (1) Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* (2) Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* (3)The name of the author may not be used to
* endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "keyboard.h"
void keyboard_init(struct keyboard *keyb, int x, int y)
{
keyb->x = x;
keyb->y = y;
keyb->flags = KEYB_SCREEN_DRC;
keyb->keymap = &keyboard_map_us;
}
static void keyboard_putstr(struct keyboard *keyb,
uint32_t xoff, uint32_t yoff, const char *str)
{
int x = keyb->x + xoff;
int y = keyb->y + yoff;
if (keyb->flags & KEYB_SCREEN_TV)
OSScreenPutFontEx(0, x, y, str);
if (keyb->flags & KEYB_SCREEN_DRC)
OSScreenPutFontEx(1, x, y, str);
}
static const char chars_us[] = {
'`', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',
0, 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']',
0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'','\\',
0, 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0, 0
};
static const char chars_us_shifted[] = {
'~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',
0, 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}',
0, 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '|',
0, 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 0, 0
};
const struct keyboard_map keyboard_map_us = {
.width = 13,
.height = 4,
.chars = chars_us,
.chars_shifted = chars_us_shifted
};
int touched_x, touched_y;
void keyboard_draw(struct keyboard *keyb)
{
int x, y;
const char *chars;
if (keyb->flags & KEYB_SHIFT)
chars = keyb->keymap->chars_shifted;
else
chars = keyb->keymap->chars;
for (x = 0; x < keyb->keymap->width; x++) {
for (y = 0; y < keyb->keymap->height; y++) {
char str[2] = {
chars[x + keyb->keymap->width*y],
'\0'
};
keyboard_putstr(keyb, 5*x + y, 2*y, str);
}
}
//keyboard_putstr(keyb, 0, 2, "-->"); (tab)
keyboard_putstr(keyb, 0, 4, (keyb->flags & KEYB_SHIFT)? "SHIFT" : "shift");
keyboard_putstr(keyb, 0, 6, "ctrl");
//OSScreenPutFontEx(1, touched_x, touched_y, "X");
}
static void translate_touch_coords(int touchx, int touchy, int *charx, int *chary)
{
/* char( 0, 0) = touch( 330, 3500)
* char(49, 17) = touch(3000, 380) */
*charx = (touchx - 330) * 0.018148;
*chary = (touchy - 3500) * -0.005448717;
}
static char keymap_lookup(const struct keyboard_map *keymap, int shifted, int x, int y)
{
if (x < 0 || y < 0 || x >= keymap->width || y >= keymap->height)
return 0;
const char *chars = shifted? keymap->chars_shifted : keymap->chars;
return chars[x + y*keymap->width];
}
void keyboard_handle_vpad(struct keyboard *keyb,
const VPADData *vpad, keyboard_callback_t cb)
{
/* L/R: shift */
if (vpad->btns_h & VPAD_BUTTON_L || vpad->btns_h & VPAD_BUTTON_R)
keyb->flags |= KEYB_SHIFT;
else
keyb->flags &= ~KEYB_SHIFT;
/* A: enter */
if (vpad->btns_d & VPAD_BUTTON_A)
cb(keyb, '\n');
/* B: backspace */
if (vpad->btns_d & VPAD_BUTTON_B)
cb(keyb, KEYB_BACKSPACE);
/* ZL: space */
if (vpad->btns_d & VPAD_BUTTON_ZL)
cb(keyb, ' ');
/* handle touch input */
if (vpad->tpdata.touched && vpad->tpdata.invalid == 0) {
if (!(keyb->flags & KEYB_TOUCHED)) {
int x, y, ch;
translate_touch_coords(vpad->tpdata.x, vpad->tpdata.y,
&x, &y);
touched_x = x;
touched_y = y;
x -= keyb->x;
y -= keyb->y;
y = (y + 1) / 2;
x = (x - y + 2) / 5;
ch = keymap_lookup(keyb->keymap,
keyb->flags & KEYB_SHIFT, x, y);
if (ch)
cb(keyb, ch);
keyb->flags |= KEYB_TOUCHED;
}
} else {
keyb->flags &= ~KEYB_TOUCHED;
}
}