src/touchscreen/nds/SDL_systouchscreen.c
branchgsoc2008_nds
changeset 2678 3895761db26a
parent 2677 6386764eb222
child 2679 bc3e3e889f6d
equal deleted inserted replaced
2677:6386764eb222 2678:3895761db26a
     1 /*
       
     2     SDL - Simple DirectMedia Layer
       
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
       
     4 
       
     5     This library is free software; you can redistribute it and/or
       
     6     modify it under the terms of the GNU Library General Public
       
     7     License as published by the Free Software Foundation; either
       
     8     version 2 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     Library General Public License for more details.
       
    14 
       
    15     You should have received a copy of the GNU Library General Public
       
    16     License along with this library; if not, write to the Free
       
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    18 
       
    19     Sam Lantinga
       
    20     slouken@devolution.com
       
    21 */
       
    22 
       
    23 #include "SDL_config.h"
       
    24 
       
    25 #ifdef SDL_TOUCHSCREEN_NDS
       
    26 
       
    27 /* This is the system specific header for the SDL touchscreen 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_touchscreen.h"
       
    34 #include "../SDL_systouchscreen.h"
       
    35 #include "../SDL_touchscreen_c.h"
       
    36 
       
    37 #include "../../video/nds/SDL_ndsevents_c.h"
       
    38 
       
    39 /* Function to scan the system for touchscreen.
       
    40  * This function should set SDL_numtouchscreens to the number of available
       
    41  * touchscreen.  Touchscreen 0 should be the system default touchscreen.
       
    42  * It should return 0, or -1 on an unrecoverable fatal error.
       
    43  */
       
    44 int
       
    45 SDL_SYS_TouchscreenInit(void)
       
    46 {
       
    47     SDL_numtouchscreens = 1;
       
    48     return (1);
       
    49 }
       
    50 
       
    51 /* Function to get the device-dependent name of a touchscreen */
       
    52 const char *
       
    53 SDL_SYS_TouchscreenName(int index)
       
    54 {
       
    55     if (!index)
       
    56         return "NDS builtin touchscreen";
       
    57     SDL_SetError("No touchscreen available with that index");
       
    58     return (NULL);
       
    59 }
       
    60 
       
    61 /* Function to open a touchscreen for use.
       
    62    The touchscreen to open is specified by the index field of the touchscreen.
       
    63    This should fill the maxpoints field of the touchscreen structure.
       
    64    It returns 0, or -1 if there is an error.
       
    65  */
       
    66 int
       
    67 SDL_SYS_TouchscreenOpen(SDL_Touchscreen * touchscreen)
       
    68 {
       
    69     touchscreen->maxpoints = 1;
       
    70     touchscreen->npoints = 0;
       
    71     /* do I call SDL_TouchscreenOpen here? */
       
    72     return 0;
       
    73 }
       
    74 
       
    75 
       
    76 /* Function to update the state of a touchscreen - called as a device poll.
       
    77  * This function shouldn't update the touchscreen structure directly,
       
    78  * but instead should call SDL_PrivateTouch*() to deliver events
       
    79  * and update touchscreen device state.
       
    80  */
       
    81 void
       
    82 SDL_SYS_TouchscreenUpdate(SDL_Touchscreen * touchscreen)
       
    83 {
       
    84     u32 keysd, keysu, keysh;
       
    85     Uint16 xpos=0, ypos=0, pressure = 32767;
       
    86     touchPosition touch;
       
    87 
       
    88     /*scanKeys();*/
       
    89     keysd = keysDown();
       
    90     keysh = keysHeld();
       
    91     keysu = keysUp();
       
    92     touch=touchReadXY();
       
    93     xpos = (touch.px << 8) + 1;
       
    94     ypos = (touch.py << 16) / 192 + 1;
       
    95     /* TODO uses touch.x and touch.y for something.
       
    96        we discussed in the mailing list having both "hardware x/y" and
       
    97        "screen x/y" coordinates.  maybe modify structs for that too
       
    98        also, find out how Colors! gets pressure and see if it's practical here
       
    99      */
       
   100 
       
   101     if ((keysd & KEY_TOUCH)) {
       
   102         SDL_PrivateTouchPress(touchscreen, 0, xpos, ypos, pressure);
       
   103     }
       
   104     if ((keysh & KEY_TOUCH)) {
       
   105     	/* sometimes the touch jumps to y=1 before it reports as keysUp */
       
   106     	if(ypos > 1) {
       
   107 	    SDL_PrivateTouchMove(touchscreen, 0, xpos, ypos, pressure);
       
   108 	}
       
   109     }
       
   110     if ((keysu & KEY_TOUCH)) {
       
   111         SDL_PrivateTouchRelease(touchscreen, 0);
       
   112     }
       
   113 }
       
   114 
       
   115 /* Function to close a touchscreen after use */
       
   116 void
       
   117 SDL_SYS_TouchscreenClose(SDL_Touchscreen * touchscreen)
       
   118 {
       
   119 }
       
   120 
       
   121 /* Function to perform any system-specific touchscreen related cleanup */
       
   122 void
       
   123 SDL_SYS_TouchscreenQuit(void)
       
   124 {
       
   125 }
       
   126 #endif /* SDL_TOUCHSCREEN_NDS */
       
   127