src/joystick/nds/SDL_sysjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 15 Feb 2011 16:41:04 -0800
changeset 5301 248c5c7d6e29
parent 5262 b530ef003506
child 5535 96594ac5fd1a
permissions -rw-r--r--
Patch to fix includes from Frank Zago
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2011 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 
    23 #include "SDL_config.h"
    24 
    25 #ifdef SDL_JOYSTICK_NDS
    26 
    27 /* This is the system specific header for the SDL joystick API */
    28 #include <nds.h>
    29 #include <stdio.h>              /* For the definition of NULL */
    30 
    31 #include "SDL_error.h"
    32 #include "SDL_events.h"
    33 #include "SDL_joystick.h"
    34 #include "../SDL_sysjoystick.h"
    35 #include "../SDL_joystick_c.h"
    36 
    37 #include "../../video/nds/SDL_ndsevents_c.h"
    38 
    39 /* Function to scan the system for joysticks.
    40  * This function should set SDL_numjoysticks to the number of available
    41  * joysticks.  Joystick 0 should be the system default joystick.
    42  * It should return 0, or -1 on an unrecoverable fatal error.
    43  */
    44 int
    45 SDL_SYS_JoystickInit(void)
    46 {
    47     SDL_numjoysticks = 1;
    48     return (1);
    49 }
    50 
    51 /* Function to get the device-dependent name of a joystick */
    52 const char *
    53 SDL_SYS_JoystickName(int index)
    54 {
    55     if (!index)
    56         return "NDS builtin joypad";
    57     SDL_SetError("No joystick available with that index");
    58     return (NULL);
    59 }
    60 
    61 /* Function to open a joystick for use.
    62    The joystick to open is specified by the index field of the joystick.
    63    This should fill the nbuttons and naxes fields of the joystick structure.
    64    It returns 0, or -1 if there is an error.
    65  */
    66 int
    67 SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
    68 {
    69     joystick->nbuttons = 8;
    70     joystick->nhats = 0;
    71     joystick->nballs = 0;
    72     joystick->naxes = 2;
    73     return 0;
    74 }
    75 
    76 
    77 /* Function to update the state of a joystick - called as a device poll.
    78  * This function shouldn't update the joystick structure directly,
    79  * but instead should call SDL_PrivateJoystick*() to deliver events
    80  * and update joystick device state.
    81  */
    82     void
    83 SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
    84 {
    85     u32 keysd, keysu;
    86     int magnitude = 16384;
    87     
    88         /*scanKeys(); - this is done in PumpEvents, because touch uses it too */ 
    89         keysd = keysDown();
    90     keysu = keysUp();
    91 
    92     if ((keysd & KEY_UP)) {
    93         SDL_PrivateJoystickAxis(joystick, 1, -magnitude);
    94     }
    95     if ((keysd & KEY_DOWN)) {
    96         SDL_PrivateJoystickAxis(joystick, 1, magnitude);
    97     }
    98     if ((keysd & KEY_LEFT)) {
    99         SDL_PrivateJoystickAxis(joystick, 0, -magnitude);
   100     }
   101     if ((keysd & KEY_RIGHT)) {
   102         SDL_PrivateJoystickAxis(joystick, 0, magnitude);
   103     }
   104     if ((keysu & (KEY_UP | KEY_DOWN))) {
   105         SDL_PrivateJoystickAxis(joystick, 1, 0);
   106     }
   107     if ((keysu & (KEY_LEFT | KEY_RIGHT))) {
   108         SDL_PrivateJoystickAxis(joystick, 0, 0);
   109     }
   110     if ((keysd & KEY_A)) {
   111         SDL_PrivateJoystickButton(joystick, 0, SDL_PRESSED);
   112     }
   113     if ((keysd & KEY_B)) {
   114         SDL_PrivateJoystickButton(joystick, 1, SDL_PRESSED);
   115     }
   116     if ((keysd & KEY_X)) {
   117         SDL_PrivateJoystickButton(joystick, 2, SDL_PRESSED);
   118     }
   119     if ((keysd & KEY_Y)) {
   120         SDL_PrivateJoystickButton(joystick, 3, SDL_PRESSED);
   121     }
   122     if ((keysd & KEY_L)) {
   123         SDL_PrivateJoystickButton(joystick, 4, SDL_PRESSED);
   124     }
   125     if ((keysd & KEY_R)) {
   126         SDL_PrivateJoystickButton(joystick, 5, SDL_PRESSED);
   127     }
   128     if ((keysd & KEY_SELECT)) {
   129         SDL_PrivateJoystickButton(joystick, 6, SDL_PRESSED);
   130     }
   131     if ((keysd & KEY_START)) {
   132         SDL_PrivateJoystickButton(joystick, 7, SDL_PRESSED);
   133     }
   134     if ((keysu & KEY_A)) {
   135         SDL_PrivateJoystickButton(joystick, 0, SDL_RELEASED);
   136     }
   137     if ((keysu & KEY_B)) {
   138         SDL_PrivateJoystickButton(joystick, 1, SDL_RELEASED);
   139     }
   140     if ((keysu & KEY_X)) {
   141         SDL_PrivateJoystickButton(joystick, 2, SDL_RELEASED);
   142     }
   143     if ((keysu & KEY_Y)) {
   144         SDL_PrivateJoystickButton(joystick, 3, SDL_RELEASED);
   145     }
   146     if ((keysu & KEY_L)) {
   147         SDL_PrivateJoystickButton(joystick, 4, SDL_RELEASED);
   148     }
   149     if ((keysu & KEY_R)) {
   150         SDL_PrivateJoystickButton(joystick, 5, SDL_RELEASED);
   151     }
   152     if ((keysu & KEY_SELECT)) {
   153         SDL_PrivateJoystickButton(joystick, 6, SDL_RELEASED);
   154     }
   155     if ((keysu & KEY_START)) {
   156         SDL_PrivateJoystickButton(joystick, 7, SDL_RELEASED);
   157     }
   158 }
   159 
   160 /* Function to close a joystick after use */
   161 void
   162 SDL_SYS_JoystickClose(SDL_Joystick * joystick)
   163 {
   164 }
   165 
   166 /* Function to perform any system-specific joystick related cleanup */
   167 void
   168 SDL_SYS_JoystickQuit(void)
   169 {
   170 }
   171 
   172 #endif /* SDL_JOYSTICK_NDS */