summaryrefslogtreecommitdiff
path: root/source/keyboard.c
blob: 8853dd18fccb2c76d2945339564638b05b770021 (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
   ds81 - Nintendo DS ZX81 emulator.

   Copyright (C) 2006  Ian Cowburn <ianc@noddybox.co.uk>
   
   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 program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  
   $Id$
*/

#include <nds.h>

#include "keyboard.h"
#include "framebuffer.h"
#include "touchwrap.h"
#include "keyb_bin.h"

/* ---------------------------------------- STATIC DATA
*/

static struct
{
    int	state;
    int	new_state;
    int	handled;
    int is_sticky;
} key_state[NUM_SOFT_KEYS];

static SoftKey	pad_left_key	= SK_5;
static SoftKey	pad_right_key	= SK_8;
static SoftKey	pad_up_key	= SK_7;
static SoftKey	pad_down_key	= SK_6;
static SoftKey	pad_A_key	= SK_0;
static SoftKey	pad_B_key	= SK_NEWLINE;
static SoftKey	pad_X_key	= NUM_SOFT_KEYS;
static SoftKey	pad_Y_key	= NUM_SOFT_KEYS;
static SoftKey	pad_R_key	= NUM_SOFT_KEYS;
static SoftKey	pad_L_key	= NUM_SOFT_KEYS;
static SoftKey	pad_start_key	= NUM_SOFT_KEYS;
static SoftKey	pad_select_key	= NUM_SOFT_KEYS;

#define CLEAR_STATE(SHORTCUT)					\
	do							\
	{							\
	    if (SHORTCUT != NUM_SOFT_KEYS &&			\
		!key_state[SHORTCUT].handled)			\
	    {							\
		key_state[SHORTCUT].new_state = FALSE;		\
	    }							\
	} while(0)

#define CHECK_STATE(KEYS,BIT,CODE,SHORTCUT,USE_SHORTCUT)	\
	do							\
	{							\
	    key_state[CODE].new_state = (KEYS & BIT);		\
	    if (USE_SHORTCUT && SHORTCUT != NUM_SOFT_KEYS &&	\
		!key_state[SHORTCUT].handled && (KEYS & BIT))	\
	    {							\
		key_state[SHORTCUT].new_state = TRUE;		\
	    }							\
	} while(0)


static const char *keynames[]=
{
    "1", "2", "3", "4", "5",
    "6", "7", "8", "9", "0",
    "Q", "W", "E", "R", "T",
    "Y", "U", "I", "O", "P",
    "A", "S", "D", "F", "G",
    "H", "J", "K", "L", "NEWLINE",
    "SHIFT", "Z", "X", "C", "V",
    "B", "N", "M", "PERIOD", "SPACE",

    "ABOUT",
    "CONFIG",
    "JOYPAD UP",
    "JOYPAD DOWN",
    "JOYPAD LEFT",
    "JOYPAD RIGHT",
    "A BUTTON",
    "B BUTTON",
    "X BUTTON",
    "Y BUTTON",
    "RIGHT SHOULDER BUTTON",
    "LEFT SHOULDER BUTTON",
    "START BUTTON",
    "SELECT BUTTON"
};

/* ---------------------------------------- PRIVATE INTERFACES
*/
static SoftKey LocatePress(const touchPosition *p)
{
    int kx=0,ky=0;
    int py=0;
    SoftKey key = NUM_SOFT_KEYS;

    if (p->py > 36 && p->px > 2)
    {
	kx = (p->px - 3) / 25;
	ky = p->py - 37;

	py = ky % 30;
	ky /= 30;

	if (py<17 && kx >= 0 && kx<10 && ky>=0 && ky<=4)
	{
	    key = kx + ky * 10;
	}
    }

    if (key>SK_SPACE)
    {
	key = NUM_SOFT_KEYS;
    }

    return key;
}


static int GetEvent(SoftKeyEvent *ev, int map)
{
    static SoftKey last = NUM_SOFT_KEYS;
    static int poll_index = -1;

    /* Read the keys if this is a new loop
    */
    if (poll_index == -1)
    {
	int f;
	uint32 keys;

	keys = keysHeld();

	/* Clear the non-sticky keys
	*/
	for(f=SK_1; f<=SK_CONFIG; f++)
	{
	    key_state[f].handled = FALSE;

	    if (key_state[f].is_sticky)
	    {
		key_state[f].new_state = key_state[f].state;
	    }
	    else
	    {
		key_state[f].new_state = FALSE;
	    }
	}

	/* Check the soft keyboard
	*/
	if (keys & KEY_TOUCH)
	{
	    touchPosition tp;

	    if (AllowTouch(&tp))
	    {
		if (tp.py<21 || tp.py>165)
		{
		    key_state[SK_CONFIG].new_state = TRUE;
		}
		else
		{
		    SoftKey press;

		    press = LocatePress(&tp);

		    if (press != NUM_SOFT_KEYS)
		    {
			key_state[press].handled = TRUE;

			if (key_state[press].is_sticky)
			{
			    if (last != press)
			    {
				key_state[press].new_state =
					    !key_state[press].state;
			    }
			}
			else
			{
			    key_state[press].new_state = TRUE;
			}

			last = press;
		    }
		}
	    }
	}
	else
	{
	    last = NUM_SOFT_KEYS;
	}

	/* Check non soft-keyboard controls
	*/
	CHECK_STATE(keys, KEY_A,	SK_PAD_A,	pad_A_key,	map);
	CHECK_STATE(keys, KEY_B,	SK_PAD_B,	pad_B_key,	map);
	CHECK_STATE(keys, KEY_X,	SK_PAD_X,	pad_X_key,	map);
	CHECK_STATE(keys, KEY_Y,	SK_PAD_Y,	pad_Y_key,	map);
	CHECK_STATE(keys, KEY_R,	SK_PAD_R,	pad_R_key,	map);
	CHECK_STATE(keys, KEY_L,	SK_PAD_L,	pad_L_key,	map);
	CHECK_STATE(keys, KEY_START,	SK_PAD_START,	pad_start_key,	map);
	CHECK_STATE(keys, KEY_SELECT,	SK_PAD_SELECT,	pad_select_key,	map);
	CHECK_STATE(keys, KEY_UP,	SK_PAD_UP,	pad_up_key,	map);
	CHECK_STATE(keys, KEY_DOWN,	SK_PAD_DOWN,	pad_down_key,	map);
	CHECK_STATE(keys, KEY_LEFT,	SK_PAD_LEFT,	pad_left_key,	map);
	CHECK_STATE(keys, KEY_RIGHT,	SK_PAD_RIGHT,	pad_right_key,	map);

	/* Reset key event poll index
	*/
	poll_index = 0;

	/* Update any on-screen indicators
	*/
	for(f=SK_1; f<SK_CONFIG; f++)
	{
	    if (key_state[f].state != key_state[f].new_state)
	    {
		int x,y;

		x = 3 + (f % 10) * 25;
		y = 37 + (f / 10) * 30;
	    	
		FB_Box(x, y, 25, 18, key_state[f].new_state ?
						    COL_WHITE : COL_BLACK);
	    }
	}
    }

    while(poll_index < NUM_SOFT_KEYS &&
    		key_state[poll_index].state == key_state[poll_index].new_state)
    {
    	poll_index++;
    }

    if (poll_index < NUM_SOFT_KEYS)
    {
	key_state[poll_index].state = key_state[poll_index].new_state;

	ev->key = poll_index;
	ev->pressed = key_state[poll_index].state;

	return TRUE;
    }
    else
    {
    	poll_index = -1;
	return FALSE;
    }
}


/* ---------------------------------------- PUBLIC INTERFACES
*/
void SK_DisplayKeyboard(uint16 *vram)
{
    static sImage img;
    static int loaded;

    if (!loaded)
    {
	loadPCX(keyb_bin,&img);
	loaded = true;
    }

    FB_Blit(&img,0,0,110);
}


int SK_GetEvent(SoftKeyEvent *ev)
{
    return GetEvent(ev,TRUE);
}


int SK_GetBareEvent(SoftKeyEvent *ev)
{
    return GetEvent(ev,FALSE);
}


void SK_SetSticky(SoftKey key, int is_sticky)
{
    key_state[key].is_sticky = is_sticky;

    if (!is_sticky)
    {
	key_state[key].new_state = FALSE;
    }
}


void SK_DefinePad(SoftKey pad, SoftKey key)
{
    switch(pad)
    {
	case SK_PAD_LEFT:
	    pad_left_key = key;
	    break;
	case SK_PAD_RIGHT:
	    pad_right_key = key;
	    break;
	case SK_PAD_UP:
	    pad_up_key = key;
	    break;
	case SK_PAD_DOWN:
	    pad_down_key = key;
	    break;
	case SK_PAD_A:
	    pad_A_key = key;
	    break;
	case SK_PAD_B:
	    pad_B_key = key;
	    break;
	case SK_PAD_X:
	    pad_X_key = key;
	    break;
	case SK_PAD_Y:
	    pad_Y_key = key;
	    break;
	case SK_PAD_R:
	    pad_R_key = key;
	    break;
	case SK_PAD_L:
	    pad_L_key = key;
	    break;
	case SK_PAD_START:
	    pad_start_key = key;
	    break;
	case SK_PAD_SELECT:
	    pad_select_key = key;
	    break;
	default:
	    break;
    }
}


const char *SK_KeyName(SoftKey k)
{
    return keynames[k];
}