src/joystick/psp/SDL_sysjoystick.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Wed, 16 Nov 2016 22:08:51 +0100
changeset 10617 346c02ff71b6
parent 10147 ddbdc9c1b92f
child 10737 3406a0f8b041
permissions -rw-r--r--
Fixed empty parameter list in signatures of internal functions.
kimonline@7009
     1
/*
kimonline@7009
     2
  Simple DirectMedia Layer
slouken@9998
     3
  Copyright (C) 1997-2016 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
*/
philipp@9328
    21
#include "../../SDL_internal.h"
kimonline@7009
    22
philipp@9329
    23
#if SDL_JOYSTICK_PSP
philipp@9329
    24
philipp@8138
    25
/* This is the PSP implementation of the SDL joystick API */
kimonline@7009
    26
#include <pspctrl.h>
kimonline@7009
    27
#include <pspkernel.h>
kimonline@7009
    28
slouken@7191
    29
#include <stdio.h>      /* For the definition of NULL */
kimonline@7009
    30
#include <stdlib.h>
kimonline@7009
    31
kimonline@7009
    32
#include "../SDL_sysjoystick.h"
kimonline@7009
    33
#include "../SDL_joystick_c.h"
kimonline@7009
    34
kimonline@7009
    35
#include "SDL_events.h"
kimonline@7009
    36
#include "SDL_error.h"
kimonline@7009
    37
#include "SDL_mutex.h"
kimonline@7009
    38
#include "SDL_timer.h"
icculus@10147
    39
#include "../../thread/SDL_systhread.h"
kimonline@7009
    40
kimonline@7009
    41
/* Current pad state */
kimonline@7009
    42
static SceCtrlData pad = { .Lx = 0, .Ly = 0, .Buttons = 0 };
kimonline@7009
    43
static SDL_sem *pad_sem = NULL;
kimonline@7009
    44
static SDL_Thread *thread = NULL;
kimonline@7009
    45
static int running = 0;
kimonline@7009
    46
static const enum PspCtrlButtons button_map[] = {
slouken@7191
    47
    PSP_CTRL_TRIANGLE, PSP_CTRL_CIRCLE, PSP_CTRL_CROSS, PSP_CTRL_SQUARE,
slouken@7191
    48
    PSP_CTRL_LTRIGGER, PSP_CTRL_RTRIGGER,
slouken@7191
    49
    PSP_CTRL_DOWN, PSP_CTRL_LEFT, PSP_CTRL_UP, PSP_CTRL_RIGHT,
slouken@7191
    50
    PSP_CTRL_SELECT, PSP_CTRL_START, PSP_CTRL_HOME, PSP_CTRL_HOLD };
kimonline@7009
    51
static int analog_map[256];  /* Map analog inputs to -32768 -> 32767 */
kimonline@7009
    52
kimonline@7009
    53
typedef struct
kimonline@7009
    54
{
kimonline@7009
    55
  int x;
kimonline@7009
    56
  int y;
kimonline@7009
    57
} point;
kimonline@7009
    58
slouken@7191
    59
/* 4 points define the bezier-curve. */
kimonline@7009
    60
static point a = { 0, 0 };
kimonline@7009
    61
static point b = { 50, 0  };
kimonline@7009
    62
static point c = { 78, 32767 };
kimonline@7009
    63
static point d = { 128, 32767 };
kimonline@7009
    64
slouken@7191
    65
/* simple linear interpolation between two points */
slouken@7860
    66
static SDL_INLINE void lerp (point *dest, point *a, point *b, float t)
kimonline@7009
    67
{
slouken@7191
    68
    dest->x = a->x + (b->x - a->x)*t;
slouken@7191
    69
    dest->y = a->y + (b->y - a->y)*t;
kimonline@7009
    70
}
kimonline@7009
    71
slouken@7191
    72
/* evaluate a point on a bezier-curve. t goes from 0 to 1.0 */
kimonline@7009
    73
static int calc_bezier_y(float t)
kimonline@7009
    74
{
slouken@7191
    75
    point ab, bc, cd, abbc, bccd, dest;
slouken@7191
    76
    lerp (&ab, &a, &b, t);           /* point between a and b */
slouken@7191
    77
    lerp (&bc, &b, &c, t);           /* point between b and c */
slouken@7191
    78
    lerp (&cd, &c, &d, t);           /* point between c and d */
slouken@7191
    79
    lerp (&abbc, &ab, &bc, t);       /* point between ab and bc */
slouken@7191
    80
    lerp (&bccd, &bc, &cd, t);       /* point between bc and cd */
slouken@7191
    81
    lerp (&dest, &abbc, &bccd, t);   /* point on the bezier-curve */
slouken@7191
    82
    return dest.y;
kimonline@7009
    83
}
kimonline@7009
    84
kimonline@7009
    85
/*
kimonline@7009
    86
 * Collect pad data about once per frame
kimonline@7009
    87
 */
kimonline@7009
    88
int JoystickUpdate(void *data)
kimonline@7009
    89
{
slouken@7191
    90
    while (running) {
slouken@7191
    91
        SDL_SemWait(pad_sem);
slouken@7191
    92
        sceCtrlPeekBufferPositive(&pad, 1);
slouken@7191
    93
        SDL_SemPost(pad_sem);
slouken@7191
    94
        /* Delay 1/60th of a second */
slouken@7191
    95
        sceKernelDelayThread(1000000 / 60);
slouken@7191
    96
    }
slouken@7191
    97
    return 0;
kimonline@7009
    98
}
kimonline@7009
    99
kimonline@7009
   100
kimonline@7009
   101
kimonline@7009
   102
/* Function to scan the system for joysticks.
philipp@9311
   103
 * Joystick 0 should be the system default joystick.
kimonline@7009
   104
 * It should return number of joysticks, or -1 on an unrecoverable fatal error.
kimonline@7009
   105
 */
kimonline@7009
   106
int SDL_SYS_JoystickInit(void)
kimonline@7009
   107
{
slouken@7191
   108
    int i;
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;
icculus@10146
   119
    if((thread = SDL_CreateThreadInternal(JoystickUpdate, "JoystickThread", 4096, 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
philipp@10617
   135
int SDL_SYS_NumJoysticks(void)
kimonline@7009
   136
{
kimonline@7009
   137
    return 1;
kimonline@7009
   138
}
kimonline@7009
   139
philipp@10617
   140
void SDL_SYS_JoystickDetect(void)
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.
philipp@9380
   167
   The joystick to open is specified by the device index.
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
philipp@9561
   180
/* Function to determine if 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
}
philipp@9380
   185
kimonline@7009
   186
/* Function to update the state of a joystick - called as a device poll.
kimonline@7009
   187
 * This function shouldn't update the joystick structure directly,
kimonline@7009
   188
 * but instead should call SDL_PrivateJoystick*() to deliver events
kimonline@7009
   189
 * and update joystick device state.
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
{
kimonline@7009
   236
}
kimonline@7009
   237
kimonline@7009
   238
/* Function to perform any system-specific joystick related cleanup */
kimonline@7009
   239
void SDL_SYS_JoystickQuit(void)
kimonline@7009
   240
{
slouken@7191
   241
    /* Cleanup Threads and Semaphore. */
slouken@7191
   242
    running = 0;
slouken@7191
   243
    SDL_WaitThread(thread, NULL);
slouken@7191
   244
    SDL_DestroySemaphore(pad_sem);
kimonline@7009
   245
}
kimonline@7009
   246
kimonline@7009
   247
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
kimonline@7009
   248
{
kimonline@7009
   249
    SDL_JoystickGUID guid;
slouken@7191
   250
    /* the GUID is just the first 16 chars of the name for now */
kimonline@7009
   251
    const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
kimonline@7009
   252
    SDL_zero( guid );
kimonline@7009
   253
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
kimonline@7009
   254
    return guid;
kimonline@7009
   255
}
kimonline@7009
   256
kimonline@7009
   257
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
kimonline@7009
   258
{
kimonline@7009
   259
    SDL_JoystickGUID guid;
slouken@7191
   260
    /* the GUID is just the first 16 chars of the name for now */
kimonline@7009
   261
    const char *name = joystick->name;
kimonline@7009
   262
    SDL_zero( guid );
kimonline@7009
   263
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
kimonline@7009
   264
    return guid;
kimonline@7009
   265
}
kimonline@7009
   266
philipp@9329
   267
#endif /* SDL_JOYSTICK_PSP */
philipp@9329
   268
kimonline@7009
   269
/* vim: ts=4 sw=4
kimonline@7009
   270
 */