src/joystick/psp/SDL_sysjoystick.c
changeset 7191 75360622e65f
parent 7037 3fedf1f25b94
child 7860 2b0bcdea3a79
equal deleted inserted replaced
7190:11612d544fcd 7191:75360622e65f
    21 
    21 
    22 /* This is the system specific header for the SDL joystick API */
    22 /* This is the system specific header for the SDL joystick API */
    23 #include <pspctrl.h>
    23 #include <pspctrl.h>
    24 #include <pspkernel.h>
    24 #include <pspkernel.h>
    25 
    25 
    26 #include <stdio.h>		/* For the definition of NULL */
    26 #include <stdio.h>      /* For the definition of NULL */
    27 #include <stdlib.h>
    27 #include <stdlib.h>
    28 
    28 
    29 #include "../SDL_sysjoystick.h"
    29 #include "../SDL_sysjoystick.h"
    30 #include "../SDL_joystick_c.h"
    30 #include "../SDL_joystick_c.h"
    31 
    31 
    39 static SceCtrlData pad = { .Lx = 0, .Ly = 0, .Buttons = 0 };
    39 static SceCtrlData pad = { .Lx = 0, .Ly = 0, .Buttons = 0 };
    40 static SDL_sem *pad_sem = NULL;
    40 static SDL_sem *pad_sem = NULL;
    41 static SDL_Thread *thread = NULL;
    41 static SDL_Thread *thread = NULL;
    42 static int running = 0;
    42 static int running = 0;
    43 static const enum PspCtrlButtons button_map[] = {
    43 static const enum PspCtrlButtons button_map[] = {
    44 	PSP_CTRL_TRIANGLE, PSP_CTRL_CIRCLE, PSP_CTRL_CROSS, PSP_CTRL_SQUARE,
    44     PSP_CTRL_TRIANGLE, PSP_CTRL_CIRCLE, PSP_CTRL_CROSS, PSP_CTRL_SQUARE,
    45 	PSP_CTRL_LTRIGGER, PSP_CTRL_RTRIGGER,
    45     PSP_CTRL_LTRIGGER, PSP_CTRL_RTRIGGER,
    46 	PSP_CTRL_DOWN, PSP_CTRL_LEFT, PSP_CTRL_UP, PSP_CTRL_RIGHT,
    46     PSP_CTRL_DOWN, PSP_CTRL_LEFT, PSP_CTRL_UP, PSP_CTRL_RIGHT,
    47 	PSP_CTRL_SELECT, PSP_CTRL_START, PSP_CTRL_HOME, PSP_CTRL_HOLD };
    47     PSP_CTRL_SELECT, PSP_CTRL_START, PSP_CTRL_HOME, PSP_CTRL_HOLD };
    48 static int analog_map[256];  /* Map analog inputs to -32768 -> 32767 */
    48 static int analog_map[256];  /* Map analog inputs to -32768 -> 32767 */
    49 
    49 
    50 typedef struct
    50 typedef struct
    51 {
    51 {
    52   int x;
    52   int x;
    53   int y;
    53   int y;
    54 } point;
    54 } point;
    55 
    55 
    56 // 4 points define the bezier-curve.  
    56 /* 4 points define the bezier-curve. */
    57 static point a = { 0, 0 };
    57 static point a = { 0, 0 };
    58 static point b = { 50, 0  };
    58 static point b = { 50, 0  };
    59 static point c = { 78, 32767 };
    59 static point c = { 78, 32767 };
    60 static point d = { 128, 32767 };
    60 static point d = { 128, 32767 };
    61 
    61 
    62 // simple linear interpolation between two points
    62 /* simple linear interpolation between two points */
    63 static __inline__ void lerp (point *dest, point *a, point *b, float t)
    63 static __inline__ void lerp (point *dest, point *a, point *b, float t)
    64 {
    64 {
    65 	dest->x = a->x + (b->x - a->x)*t;
    65     dest->x = a->x + (b->x - a->x)*t;
    66 	dest->y = a->y + (b->y - a->y)*t;
    66     dest->y = a->y + (b->y - a->y)*t;
    67 }
    67 }
    68 
    68 
    69 // evaluate a point on a bezier-curve. t goes from 0 to 1.0
    69 /* evaluate a point on a bezier-curve. t goes from 0 to 1.0 */
    70 static int calc_bezier_y(float t)
    70 static int calc_bezier_y(float t)
    71 {
    71 {
    72 	point ab, bc, cd, abbc, bccd, dest;
    72     point ab, bc, cd, abbc, bccd, dest;
    73 	lerp (&ab, &a, &b, t);           // point between a and b
    73     lerp (&ab, &a, &b, t);           /* point between a and b */
    74 	lerp (&bc, &b, &c, t);           // point between b and c
    74     lerp (&bc, &b, &c, t);           /* point between b and c */
    75 	lerp (&cd, &c, &d, t);           // point between c and d
    75     lerp (&cd, &c, &d, t);           /* point between c and d */
    76 	lerp (&abbc, &ab, &bc, t);       // point between ab and bc
    76     lerp (&abbc, &ab, &bc, t);       /* point between ab and bc */
    77 	lerp (&bccd, &bc, &cd, t);       // point between bc and cd
    77     lerp (&bccd, &bc, &cd, t);       /* point between bc and cd */
    78 	lerp (&dest, &abbc, &bccd, t);   // point on the bezier-curve
    78     lerp (&dest, &abbc, &bccd, t);   /* point on the bezier-curve */
    79 	return dest.y;
    79     return dest.y;
    80 }
    80 }
    81 
    81 
    82 /*
    82 /*
    83  * Collect pad data about once per frame
    83  * Collect pad data about once per frame
    84  */
    84  */
    85 int JoystickUpdate(void *data)
    85 int JoystickUpdate(void *data)
    86 {
    86 {
    87 	while (running) {
    87     while (running) {
    88 		SDL_SemWait(pad_sem);
    88         SDL_SemWait(pad_sem);
    89 		sceCtrlPeekBufferPositive(&pad, 1); 
    89         sceCtrlPeekBufferPositive(&pad, 1);
    90 		SDL_SemPost(pad_sem);
    90         SDL_SemPost(pad_sem);
    91 		/* Delay 1/60th of a second */
    91         /* Delay 1/60th of a second */
    92 		sceKernelDelayThread(1000000 / 60);  
    92         sceKernelDelayThread(1000000 / 60);
    93 	}
    93     }
    94 	return 0;
    94     return 0;
    95 }
    95 }
    96 
    96 
    97 
    97 
    98 
    98 
    99 /* Function to scan the system for joysticks.
    99 /* Function to scan the system for joysticks.
   101  * joysticks.  Joystick 0 should be the system default joystick.
   101  * joysticks.  Joystick 0 should be the system default joystick.
   102  * It should return number of joysticks, or -1 on an unrecoverable fatal error.
   102  * It should return number of joysticks, or -1 on an unrecoverable fatal error.
   103  */
   103  */
   104 int SDL_SYS_JoystickInit(void)
   104 int SDL_SYS_JoystickInit(void)
   105 {
   105 {
   106 	int i;
   106     int i;
   107 
   107 
   108 //	SDL_numjoysticks = 1;
   108 /*  SDL_numjoysticks = 1; */
   109 
   109 
   110 	/* Setup input */
   110     /* Setup input */
   111 	sceCtrlSetSamplingCycle(0);
   111     sceCtrlSetSamplingCycle(0);
   112 	sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
   112     sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
   113 
   113 
   114 	/* Start thread to read data */
   114     /* Start thread to read data */
   115 	if((pad_sem =  SDL_CreateSemaphore(1)) == NULL) {
   115     if((pad_sem =  SDL_CreateSemaphore(1)) == NULL) {
   116 		return SDL_SetError("Can't create input semaphore");
   116         return SDL_SetError("Can't create input semaphore");
   117 	}
   117     }
   118 	running = 1;
   118     running = 1;
   119 	if((thread = SDL_CreateThread(JoystickUpdate, "JoySitckThread",NULL)) == NULL) {
   119     if((thread = SDL_CreateThread(JoystickUpdate, "JoySitckThread",NULL)) == NULL) {
   120 		return SDL_SetError("Can't create input thread");
   120         return SDL_SetError("Can't create input thread");
   121 	}
   121     }
   122 
   122 
   123 	/* Create an accurate map from analog inputs (0 to 255) 
   123     /* Create an accurate map from analog inputs (0 to 255)
   124 	   to SDL joystick positions (-32768 to 32767) */
   124        to SDL joystick positions (-32768 to 32767) */
   125 	for (i = 0; i < 128; i++)
   125     for (i = 0; i < 128; i++)
   126 	{
   126     {
   127 		float t = (float)i/127.0f;
   127         float t = (float)i/127.0f;
   128 		analog_map[i+128] = calc_bezier_y(t);
   128         analog_map[i+128] = calc_bezier_y(t);
   129 		analog_map[127-i] = -1 * analog_map[i+128];
   129         analog_map[127-i] = -1 * analog_map[i+128];
   130 	}
   130     }
   131 
   131 
   132 	return 1;
   132     return 1;
   133 }
   133 }
   134 
   134 
   135 int SDL_SYS_NumJoysticks()
   135 int SDL_SYS_NumJoysticks()
   136 {
   136 {
   137     return 1;
   137     return 1;
   159 }
   159 }
   160 
   160 
   161 /* Function to get the device-dependent name of a joystick */
   161 /* Function to get the device-dependent name of a joystick */
   162 const char *SDL_SYS_JoystickName(int index)
   162 const char *SDL_SYS_JoystickName(int index)
   163 {
   163 {
   164 	if (index == 0)
   164     if (index == 0)
   165 		return "PSP controller";
   165         return "PSP controller";
   166 
   166 
   167 	SDL_SetError("No joystick available with that index");
   167     SDL_SetError("No joystick available with that index");
   168 	return(NULL);
   168     return(NULL);
   169 }
   169 }
   170 
   170 
   171 /* Function to open a joystick for use.
   171 /* Function to open a joystick for use.
   172    The joystick to open is specified by the index field of the joystick.
   172    The joystick to open is specified by the index field of the joystick.
   173    This should fill the nbuttons and naxes fields of the joystick structure.
   173    This should fill the nbuttons and naxes fields of the joystick structure.
   174    It returns 0, or -1 if there is an error.
   174    It returns 0, or -1 if there is an error.
   175  */
   175  */
   176 int SDL_SYS_JoystickOpen(SDL_Joystick *joystick, int device_index)
   176 int SDL_SYS_JoystickOpen(SDL_Joystick *joystick, int device_index)
   177 {
   177 {
   178 	joystick->nbuttons = 14;
   178     joystick->nbuttons = 14;
   179 	joystick->naxes = 2;
   179     joystick->naxes = 2;
   180 	joystick->nhats = 0;
   180     joystick->nhats = 0;
   181 
   181 
   182 	return 0;
   182     return 0;
   183 }
   183 }
   184 
   184 
   185 /* Function to determine is this joystick is attached to the system right now */
   185 /* Function to determine is this joystick is attached to the system right now */
   186 SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
   186 SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
   187 {
   187 {
   193  * and update joystick device state.
   193  * and update joystick device state.
   194  */
   194  */
   195 
   195 
   196 void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
   196 void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
   197 {
   197 {
   198 	int i;
   198     int i;
   199 	enum PspCtrlButtons buttons;
   199     enum PspCtrlButtons buttons;
   200 	enum PspCtrlButtons changed;
   200     enum PspCtrlButtons changed;
   201 	unsigned char x, y;
   201     unsigned char x, y;
   202 	static enum PspCtrlButtons old_buttons = 0;
   202     static enum PspCtrlButtons old_buttons = 0;
   203 	static unsigned char old_x = 0, old_y = 0;
   203     static unsigned char old_x = 0, old_y = 0;
   204 
   204 
   205 	SDL_SemWait(pad_sem);
   205     SDL_SemWait(pad_sem);
   206 	buttons = pad.Buttons;
   206     buttons = pad.Buttons;
   207 	x = pad.Lx;
   207     x = pad.Lx;
   208 	y = pad.Ly;
   208     y = pad.Ly;
   209 	SDL_SemPost(pad_sem);
   209     SDL_SemPost(pad_sem);
   210 
   210 
   211 	/* Axes */
   211     /* Axes */
   212 	if(old_x != x) {
   212     if(old_x != x) {
   213 		SDL_PrivateJoystickAxis(joystick, 0, analog_map[x]);
   213         SDL_PrivateJoystickAxis(joystick, 0, analog_map[x]);
   214 		old_x = x;
   214         old_x = x;
   215 	}
   215     }
   216 	if(old_y != y) {
   216     if(old_y != y) {
   217 		SDL_PrivateJoystickAxis(joystick, 1, analog_map[y]);
   217         SDL_PrivateJoystickAxis(joystick, 1, analog_map[y]);
   218 		old_y = y;
   218         old_y = y;
   219 	}
   219     }
   220 
   220 
   221 	/* Buttons */
   221     /* Buttons */
   222 	changed = old_buttons ^ buttons;
   222     changed = old_buttons ^ buttons;
   223 	old_buttons = buttons;
   223     old_buttons = buttons;
   224 	if(changed) {
   224     if(changed) {
   225 		for(i=0; i<sizeof(button_map)/sizeof(button_map[0]); i++) {
   225         for(i=0; i<sizeof(button_map)/sizeof(button_map[0]); i++) {
   226 			if(changed & button_map[i]) {
   226             if(changed & button_map[i]) {
   227 				SDL_PrivateJoystickButton(
   227                 SDL_PrivateJoystickButton(
   228 					joystick, i, 
   228                     joystick, i,
   229 					(buttons & button_map[i]) ? 
   229                     (buttons & button_map[i]) ?
   230 					SDL_PRESSED : SDL_RELEASED);
   230                     SDL_PRESSED : SDL_RELEASED);
   231 			}
   231             }
   232 		}
   232         }
   233 	}
   233     }
   234 
   234 
   235 	sceKernelDelayThread(0);
   235     sceKernelDelayThread(0);
   236 }
   236 }
   237 
   237 
   238 /* Function to close a joystick after use */
   238 /* Function to close a joystick after use */
   239 void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
   239 void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
   240 {
   240 {
   241 	/* Do nothing. */
   241     /* Do nothing. */
   242 }
   242 }
   243 
   243 
   244 /* Function to perform any system-specific joystick related cleanup */
   244 /* Function to perform any system-specific joystick related cleanup */
   245 void SDL_SYS_JoystickQuit(void)
   245 void SDL_SYS_JoystickQuit(void)
   246 {
   246 {
   247 	/* Cleanup Threads and Semaphore. */
   247     /* Cleanup Threads and Semaphore. */
   248 	running = 0;
   248     running = 0;
   249 	SDL_WaitThread(thread, NULL);
   249     SDL_WaitThread(thread, NULL);
   250 	SDL_DestroySemaphore(pad_sem);
   250     SDL_DestroySemaphore(pad_sem);
   251 }
   251 }
   252 
   252 
   253 SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
   253 SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
   254 {
   254 {
   255     SDL_JoystickGUID guid;
   255     SDL_JoystickGUID guid;
   256     // the GUID is just the first 16 chars of the name for now
   256     /* the GUID is just the first 16 chars of the name for now */
   257     const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
   257     const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
   258     SDL_zero( guid );
   258     SDL_zero( guid );
   259     SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
   259     SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
   260     return guid;
   260     return guid;
   261 }
   261 }
   262 
   262 
   263 SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
   263 SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
   264 {
   264 {
   265     SDL_JoystickGUID guid;
   265     SDL_JoystickGUID guid;
   266     // the GUID is just the first 16 chars of the name for now
   266     /* the GUID is just the first 16 chars of the name for now */
   267     const char *name = joystick->name;
   267     const char *name = joystick->name;
   268     SDL_zero( guid );
   268     SDL_zero( guid );
   269     SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
   269     SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
   270     return guid;
   270     return guid;
   271 }
   271 }