1/*
2    SDL - Simple DirectMedia Layer
3    Copyright (C) 1997-2012 Sam Lantinga
4
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with this library; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19    Sam Lantinga
20    slouken@libsdl.org
21*/
22#include "SDL_config.h"
23
24/* This is the system specific header for the SDL joystick API */
25#include <nds.h>
26//#include <nds/registers_alt.h>
27
28#include "SDL_error.h"
29#include "SDL_events.h"
30#include "SDL_joystick.h"
31#include "../SDL_sysjoystick.h"
32#include "../SDL_joystick_c.h"
33
34#include "../../video/nds/SDL_ndsevents_c.h"
35
36/* Function to scan the system for joysticks.
37 * This function should set SDL_numjoysticks to the number of available
38 * joysticks.  Joystick 0 should be the system default joystick.
39 * It should return 0, or -1 on an unrecoverable fatal error.
40 */
41int SDL_SYS_JoystickInit(void)
42{
43	SDL_numjoysticks = 1;
44    //keysInit();
45
46	return(1);
47}
48
49/* Function to get the device-dependent name of a joystick */
50const char *SDL_SYS_JoystickName(int index)
51{
52	if(!index)
53		return "NDS builtin joypad";
54	SDL_SetError("No joystick available with that index");
55	return (NULL);
56}
57
58/* Function to open a joystick for use.
59   The joystick to open is specified by the index field of the joystick.
60   This should fill the nbuttons and naxes fields of the joystick structure.
61   It returns 0, or -1 if there is an error.
62 */
63int SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
64{
65	joystick->nbuttons=8;
66	joystick->nhats=0;
67	joystick->nballs=0;
68	joystick->naxes=2;
69	return 0;
70}
71
72
73/* Function to update the state of a joystick - called as a device poll.
74 * This function shouldn't update the joystick structure directly,
75 * but instead should call SDL_PrivateJoystick*() to deliver events
76 * and update joystick device state.
77 */
78
79int prevbutton=0;
80int prevkey=0;
81
82int dc=0;int ldc=0;
83u32 keysd,keysu=0;
84void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
85{
86    //dc=keysd;
87	//if (dc)
88	//{
89		//fprintf(stderr,"heartbeat= %d\n",REG_VCOUNT);
90		//swiWaitForVBlank();
91		//scanKeys();
92		//keysd = keysDown();
93		//keysu = keysUp();
94		//ldc=keysd;
95
96	//}
97	/*if (prevkey && prevbutton)
98	{
99		scanKeys();
100	}
101	*/
102
103	//scanKeys();
104		keysd = keysDown();
105		keysu = keysUp();
106
107
108	short ax=0,v=0,h=0;
109	if((keysd&KEY_UP)) {ax=1;v=-10;SDL_PrivateJoystickAxis(joystick,ax,v);prevkey=KEY_UP;}//fprintf(stderr,"KEY_UP\n");}
110	if((keysd&KEY_DOWN)) {ax=1;v=10;SDL_PrivateJoystickAxis(joystick,ax,v);prevkey=KEY_DOWN;}//fprintf(stderr,"KEY_DOWN\n");}
111	if((keysd&KEY_LEFT)) {ax=0;h=-10;SDL_PrivateJoystickAxis(joystick,ax,h);prevkey=KEY_LEFT;}//fprintf(stderr,"KEY_LEFT\n");}
112	if((keysd&KEY_RIGHT)) {ax=0;h=10;SDL_PrivateJoystickAxis(joystick,ax,h);prevkey=KEY_RIGHT;}//fprintf(stderr,"KEY_RIGHT\n");}
113
114	if((keysu&KEY_UP)) {ax=1;v=0;SDL_PrivateJoystickAxis(joystick,ax,v);prevkey=0;}//fprintf(stderr,"KEY_UP\n");}
115	if((keysu&KEY_DOWN)) {ax=1;v=0;SDL_PrivateJoystickAxis(joystick,ax,v);prevkey=0;}//fprintf(stderr,"KEY_DOWN\n");}
116	if((keysu&KEY_LEFT)) {ax=0;h=0;SDL_PrivateJoystickAxis(joystick,ax,h);prevkey=0;}//fprintf(stderr,"KEY_LEFT\n");}
117	if((keysu&KEY_RIGHT)) {ax=0;h=0;SDL_PrivateJoystickAxis(joystick,ax,h);prevkey=0;}//fprintf(stderr,"KEY_RIGHT\n");}
118
119	if((keysd&KEY_A))		{SDL_PrivateJoystickButton(joystick,0,SDL_PRESSED);prevbutton=KEY_A;}
120	if((keysd&KEY_B))		{SDL_PrivateJoystickButton(joystick,1,SDL_PRESSED);prevbutton=KEY_B;}
121	if((keysd&KEY_X))		{SDL_PrivateJoystickButton(joystick,2,SDL_PRESSED);prevbutton=KEY_X;}
122	if((keysd&KEY_Y))		{SDL_PrivateJoystickButton(joystick,3,SDL_PRESSED);prevbutton=KEY_Y;}
123	if((keysd&KEY_SELECT))	{SDL_PrivateJoystickButton(joystick,6,SDL_PRESSED);prevbutton=KEY_SELECT;}
124	if((keysd&KEY_START))	{SDL_PrivateJoystickButton(joystick,7,SDL_PRESSED);prevbutton=KEY_START;}
125	if((keysd&KEY_L))		{SDL_PrivateJoystickButton(joystick,4,SDL_PRESSED);prevbutton=KEY_L;}
126	if((keysd&KEY_R))		{SDL_PrivateJoystickButton(joystick,5,SDL_PRESSED);prevbutton=KEY_R;}
127
128	if((keysu&KEY_A))		{SDL_PrivateJoystickButton(joystick,0,SDL_RELEASED);prevbutton=0;}
129	if((keysu&KEY_B))		{SDL_PrivateJoystickButton(joystick,1,SDL_RELEASED);prevbutton=0;}
130	if((keysu&KEY_X))		{SDL_PrivateJoystickButton(joystick,2,SDL_RELEASED);prevbutton=0;}
131	if((keysu&KEY_Y))		{SDL_PrivateJoystickButton(joystick,3,SDL_RELEASED);prevbutton=0;}
132	if((keysu&KEY_SELECT))  {SDL_PrivateJoystickButton(joystick,6,SDL_RELEASED);prevbutton=0;}
133	if((keysu&KEY_START))	{SDL_PrivateJoystickButton(joystick,7,SDL_RELEASED);prevbutton=0;}
134	if((keysu&KEY_L))		{SDL_PrivateJoystickButton(joystick,4,SDL_RELEASED);prevbutton=0;}
135	if((keysu&KEY_R))		{SDL_PrivateJoystickButton(joystick,5,SDL_RELEASED);prevbutton=0;}
136
137
138
139}
140
141/* Function to close a joystick after use */
142void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
143{
144}
145
146/* Function to perform any system-specific joystick related cleanup */
147void SDL_SYS_JoystickQuit(void)
148{
149}
150
151