src/joystick/psp/SDL_sysjoystick.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 24 Dec 2014 22:22:35 -0500
changeset 9289 5f857c00d25d
parent 8860 c4133d635375
child 9311 a4b031e28de8
permissions -rw-r--r--
Maybe actually patched to compile this time?
kimonline@7009
     1
/*
kimonline@7009
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
kimonline@7009
     4
kimonline@7009
     5
  This software is provided 'as-is', without any express or implied
kimonline@7009
     6
  warranty.  In no event will the authors be held liable for any damages
kimonline@7009
     7
  arising from the use of this software.
kimonline@7009
     8
kimonline@7009
     9
  Permission is granted to anyone to use this software for any purpose,
kimonline@7009
    10
  including commercial applications, and to alter it and redistribute it
kimonline@7009
    11
  freely, subject to the following restrictions:
kimonline@7009
    12
kimonline@7009
    13
  1. The origin of this software must not be misrepresented; you must not
kimonline@7009
    14
     claim that you wrote the original software. If you use this software
kimonline@7009
    15
     in a product, an acknowledgment in the product documentation would be
kimonline@7009
    16
     appreciated but is not required.
kimonline@7009
    17
  2. Altered source versions must be plainly marked as such, and must not be
kimonline@7009
    18
     misrepresented as being the original software.
kimonline@7009
    19
  3. This notice may not be removed or altered from any source distribution.
kimonline@7009
    20
*/
kimonline@7009
    21
philipp@8138
    22
/* This is the PSP implementation of the SDL joystick API */
kimonline@7009
    23
#include <pspctrl.h>
kimonline@7009
    24
#include <pspkernel.h>
kimonline@7009
    25
slouken@7191
    26
#include <stdio.h>      /* For the definition of NULL */
kimonline@7009
    27
#include <stdlib.h>
kimonline@7009
    28
kimonline@7009
    29
#include "../SDL_sysjoystick.h"
kimonline@7009
    30
#include "../SDL_joystick_c.h"
kimonline@7009
    31
kimonline@7009
    32
#include "SDL_events.h"
kimonline@7009
    33
#include "SDL_error.h"
kimonline@7009
    34
#include "SDL_thread.h"
kimonline@7009
    35
#include "SDL_mutex.h"
kimonline@7009
    36
#include "SDL_timer.h"
kimonline@7009
    37
kimonline@7009
    38
/* Current pad state */
kimonline@7009
    39
static SceCtrlData pad = { .Lx = 0, .Ly = 0, .Buttons = 0 };
kimonline@7009
    40
static SDL_sem *pad_sem = NULL;
kimonline@7009
    41
static SDL_Thread *thread = NULL;
kimonline@7009
    42
static int running = 0;
kimonline@7009
    43
static const enum PspCtrlButtons button_map[] = {
slouken@7191
    44
    PSP_CTRL_TRIANGLE, PSP_CTRL_CIRCLE, PSP_CTRL_CROSS, PSP_CTRL_SQUARE,
slouken@7191
    45
    PSP_CTRL_LTRIGGER, PSP_CTRL_RTRIGGER,
slouken@7191
    46
    PSP_CTRL_DOWN, PSP_CTRL_LEFT, PSP_CTRL_UP, PSP_CTRL_RIGHT,
slouken@7191
    47
    PSP_CTRL_SELECT, PSP_CTRL_START, PSP_CTRL_HOME, PSP_CTRL_HOLD };
kimonline@7009
    48
static int analog_map[256];  /* Map analog inputs to -32768 -> 32767 */
kimonline@7009
    49
kimonline@7009
    50
typedef struct
kimonline@7009
    51
{
kimonline@7009
    52
  int x;
kimonline@7009
    53
  int y;
kimonline@7009
    54
} point;
kimonline@7009
    55
slouken@7191
    56
/* 4 points define the bezier-curve. */
kimonline@7009
    57
static point a = { 0, 0 };
kimonline@7009
    58
static point b = { 50, 0  };
kimonline@7009
    59
static point c = { 78, 32767 };
kimonline@7009
    60
static point d = { 128, 32767 };
kimonline@7009
    61
slouken@7191
    62
/* simple linear interpolation between two points */
slouken@7860
    63
static SDL_INLINE void lerp (point *dest, point *a, point *b, float t)
kimonline@7009
    64
{
slouken@7191
    65
    dest->x = a->x + (b->x - a->x)*t;
slouken@7191
    66
    dest->y = a->y + (b->y - a->y)*t;
kimonline@7009
    67
}
kimonline@7009
    68
slouken@7191
    69
/* evaluate a point on a bezier-curve. t goes from 0 to 1.0 */
kimonline@7009
    70
static int calc_bezier_y(float t)
kimonline@7009
    71
{
slouken@7191
    72
    point ab, bc, cd, abbc, bccd, dest;
slouken@7191
    73
    lerp (&ab, &a, &b, t);           /* point between a and b */
slouken@7191
    74
    lerp (&bc, &b, &c, t);           /* point between b and c */
slouken@7191
    75
    lerp (&cd, &c, &d, t);           /* point between c and d */
slouken@7191
    76
    lerp (&abbc, &ab, &bc, t);       /* point between ab and bc */
slouken@7191
    77
    lerp (&bccd, &bc, &cd, t);       /* point between bc and cd */
slouken@7191
    78
    lerp (&dest, &abbc, &bccd, t);   /* point on the bezier-curve */
slouken@7191
    79
    return dest.y;
kimonline@7009
    80
}
kimonline@7009
    81
kimonline@7009
    82
/*
kimonline@7009
    83
 * Collect pad data about once per frame
kimonline@7009
    84
 */
kimonline@7009
    85
int JoystickUpdate(void *data)
kimonline@7009
    86
{
slouken@7191
    87
    while (running) {
slouken@7191
    88
        SDL_SemWait(pad_sem);
slouken@7191
    89
        sceCtrlPeekBufferPositive(&pad, 1);
slouken@7191
    90
        SDL_SemPost(pad_sem);
slouken@7191
    91
        /* Delay 1/60th of a second */
slouken@7191
    92
        sceKernelDelayThread(1000000 / 60);
slouken@7191
    93
    }
slouken@7191
    94
    return 0;
kimonline@7009
    95
}
kimonline@7009
    96
kimonline@7009
    97
kimonline@7009
    98
kimonline@7009
    99
/* Function to scan the system for joysticks.
kimonline@7009
   100
 * This function should set SDL_numjoysticks to the number of available
kimonline@7009
   101
 * joysticks.  Joystick 0 should be the system default joystick.
kimonline@7009
   102
 * It should return number of joysticks, or -1 on an unrecoverable fatal error.
kimonline@7009
   103
 */
kimonline@7009
   104
int SDL_SYS_JoystickInit(void)
kimonline@7009
   105
{
slouken@7191
   106
    int i;
kimonline@7009
   107
slouken@7191
   108
/*  SDL_numjoysticks = 1; */
kimonline@7009
   109
slouken@7191
   110
    /* Setup input */
slouken@7191
   111
    sceCtrlSetSamplingCycle(0);
slouken@7191
   112
    sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
kimonline@7009
   113
slouken@7191
   114
    /* Start thread to read data */
slouken@7191
   115
    if((pad_sem =  SDL_CreateSemaphore(1)) == NULL) {
slouken@7191
   116
        return SDL_SetError("Can't create input semaphore");
slouken@7191
   117
    }
slouken@7191
   118
    running = 1;
slouken@7191
   119
    if((thread = SDL_CreateThread(JoystickUpdate, "JoySitckThread",NULL)) == NULL) {
slouken@7191
   120
        return SDL_SetError("Can't create input thread");
slouken@7191
   121
    }
kimonline@7009
   122
slouken@7191
   123
    /* Create an accurate map from analog inputs (0 to 255)
slouken@7191
   124
       to SDL joystick positions (-32768 to 32767) */
slouken@7191
   125
    for (i = 0; i < 128; i++)
slouken@7191
   126
    {
slouken@7191
   127
        float t = (float)i/127.0f;
slouken@7191
   128
        analog_map[i+128] = calc_bezier_y(t);
slouken@7191
   129
        analog_map[127-i] = -1 * analog_map[i+128];
slouken@7191
   130
    }
kimonline@7009
   131
slouken@7191
   132
    return 1;
kimonline@7009
   133
}
kimonline@7009
   134
kimonline@7009
   135
int SDL_SYS_NumJoysticks()
kimonline@7009
   136
{
kimonline@7009
   137
    return 1;
kimonline@7009
   138
}
kimonline@7009
   139
kimonline@7009
   140
void SDL_SYS_JoystickDetect()
kimonline@7009
   141
{
kimonline@7009
   142
}
kimonline@7009
   143
kimonline@7009
   144
/* Function to get the device-dependent name of a joystick */
kimonline@7009
   145
const char * SDL_SYS_JoystickNameForDeviceIndex(int device_index)
kimonline@7009
   146
{
kimonline@7009
   147
    return "PSP builtin joypad";
kimonline@7009
   148
}
kimonline@7009
   149
kimonline@7009
   150
/* Function to perform the mapping from device index to the instance id for this index */
kimonline@7009
   151
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
kimonline@7009
   152
{
kimonline@7009
   153
    return device_index;
kimonline@7009
   154
}
kimonline@7009
   155
kimonline@7009
   156
/* Function to get the device-dependent name of a joystick */
kimonline@7009
   157
const char *SDL_SYS_JoystickName(int index)
kimonline@7009
   158
{
slouken@7191
   159
    if (index == 0)
slouken@7191
   160
        return "PSP controller";
kimonline@7009
   161
slouken@7191
   162
    SDL_SetError("No joystick available with that index");
slouken@7191
   163
    return(NULL);
kimonline@7009
   164
}
kimonline@7009
   165
kimonline@7009
   166
/* Function to open a joystick for use.
kimonline@7009
   167
   The joystick to open is specified by the index field of the joystick.
kimonline@7009
   168
   This should fill the nbuttons and naxes fields of the joystick structure.
kimonline@7009
   169
   It returns 0, or -1 if there is an error.
kimonline@7009
   170
 */
kimonline@7009
   171
int SDL_SYS_JoystickOpen(SDL_Joystick *joystick, int device_index)
kimonline@7009
   172
{
slouken@7191
   173
    joystick->nbuttons = 14;
slouken@7191
   174
    joystick->naxes = 2;
slouken@7191
   175
    joystick->nhats = 0;
kimonline@7009
   176
slouken@7191
   177
    return 0;
kimonline@7009
   178
}
kimonline@7009
   179
kimonline@7009
   180
/* Function to determine is this joystick is attached to the system right now */
kimonline@7009
   181
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
kimonline@7009
   182
{
kimonline@7009
   183
    return SDL_TRUE;
kimonline@7009
   184
}
kimonline@7009
   185
/* Function to update the state of a joystick - called as a device poll.
kimonline@7009
   186
 * This function shouldn't update the joystick structure directly,
kimonline@7009
   187
 * but instead should call SDL_PrivateJoystick*() to deliver events
kimonline@7009
   188
 * and update joystick device state.
kimonline@7009
   189
 */
kimonline@7009
   190
kimonline@7009
   191
void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
kimonline@7009
   192
{
slouken@7191
   193
    int i;
slouken@7191
   194
    enum PspCtrlButtons buttons;
slouken@7191
   195
    enum PspCtrlButtons changed;
slouken@7191
   196
    unsigned char x, y;
slouken@7191
   197
    static enum PspCtrlButtons old_buttons = 0;
slouken@7191
   198
    static unsigned char old_x = 0, old_y = 0;
kimonline@7009
   199
slouken@7191
   200
    SDL_SemWait(pad_sem);
slouken@7191
   201
    buttons = pad.Buttons;
slouken@7191
   202
    x = pad.Lx;
slouken@7191
   203
    y = pad.Ly;
slouken@7191
   204
    SDL_SemPost(pad_sem);
kimonline@7009
   205
slouken@7191
   206
    /* Axes */
slouken@7191
   207
    if(old_x != x) {
slouken@7191
   208
        SDL_PrivateJoystickAxis(joystick, 0, analog_map[x]);
slouken@7191
   209
        old_x = x;
slouken@7191
   210
    }
slouken@7191
   211
    if(old_y != y) {
slouken@7191
   212
        SDL_PrivateJoystickAxis(joystick, 1, analog_map[y]);
slouken@7191
   213
        old_y = y;
slouken@7191
   214
    }
kimonline@7009
   215
slouken@7191
   216
    /* Buttons */
slouken@7191
   217
    changed = old_buttons ^ buttons;
slouken@7191
   218
    old_buttons = buttons;
slouken@7191
   219
    if(changed) {
slouken@7191
   220
        for(i=0; i<sizeof(button_map)/sizeof(button_map[0]); i++) {
slouken@7191
   221
            if(changed & button_map[i]) {
slouken@7191
   222
                SDL_PrivateJoystickButton(
slouken@7191
   223
                    joystick, i,
slouken@7191
   224
                    (buttons & button_map[i]) ?
slouken@7191
   225
                    SDL_PRESSED : SDL_RELEASED);
slouken@7191
   226
            }
slouken@7191
   227
        }
slouken@7191
   228
    }
kimonline@7009
   229
slouken@7191
   230
    sceKernelDelayThread(0);
kimonline@7009
   231
}
kimonline@7009
   232
kimonline@7009
   233
/* Function to close a joystick after use */
kimonline@7009
   234
void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
kimonline@7009
   235
{
slouken@7191
   236
    /* Do nothing. */
kimonline@7009
   237
}
kimonline@7009
   238
kimonline@7009
   239
/* Function to perform any system-specific joystick related cleanup */
kimonline@7009
   240
void SDL_SYS_JoystickQuit(void)
kimonline@7009
   241
{
slouken@7191
   242
    /* Cleanup Threads and Semaphore. */
slouken@7191
   243
    running = 0;
slouken@7191
   244
    SDL_WaitThread(thread, NULL);
slouken@7191
   245
    SDL_DestroySemaphore(pad_sem);
kimonline@7009
   246
}
kimonline@7009
   247
kimonline@7009
   248
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
kimonline@7009
   249
{
kimonline@7009
   250
    SDL_JoystickGUID guid;
slouken@7191
   251
    /* the GUID is just the first 16 chars of the name for now */
kimonline@7009
   252
    const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
kimonline@7009
   253
    SDL_zero( guid );
kimonline@7009
   254
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
kimonline@7009
   255
    return guid;
kimonline@7009
   256
}
kimonline@7009
   257
kimonline@7009
   258
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
kimonline@7009
   259
{
kimonline@7009
   260
    SDL_JoystickGUID guid;
slouken@7191
   261
    /* the GUID is just the first 16 chars of the name for now */
kimonline@7009
   262
    const char *name = joystick->name;
kimonline@7009
   263
    SDL_zero( guid );
kimonline@7009
   264
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
kimonline@7009
   265
    return guid;
kimonline@7009
   266
}
kimonline@7009
   267
kimonline@7009
   268
/* vim: ts=4 sw=4
kimonline@7009
   269
 */