src/joystick/psp/SDL_sysjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 20 Oct 2013 21:56:15 -0700
changeset 7860 2b0bcdea3a79
parent 7191 75360622e65f
child 8138 0fbc97d82043
permissions -rw-r--r--
Fixed bug 2129 - fix for bug 2121 breaks linking for mingw and throws multiple warnings

Andreas Ertelt

The problem in question is caused by changeset 7771 (http://hg.libsdl.org/SDL/rev/4434498bf4b9 / https://bugzilla.libsdl.org/show_bug.cgi?id=2121)

The redefinition of __inline__ (introduced by the addition of begin_code.h:128's "|| __STRICT_ANSI__") results in mingw's gcc throwing multiple

warning: always_inline function might not be inlinable [-Wattributes]

as well as a whole bunch of redefinitions of mingw internals which break linking of projects including the SDL2 headers.
kimonline@7009
     1
/*
kimonline@7009
     2
  Simple DirectMedia Layer
kimonline@7009
     3
  Copyright (C) 1997-2013 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
kimonline@7009
    22
/* This is the system specific header for 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
SDL_bool SDL_SYS_JoystickNeedsPolling()
kimonline@7009
   145
{
kimonline@7009
   146
    return SDL_FALSE;
kimonline@7009
   147
}
kimonline@7009
   148
kimonline@7009
   149
/* Function to get the device-dependent name of a joystick */
kimonline@7009
   150
const char * SDL_SYS_JoystickNameForDeviceIndex(int device_index)
kimonline@7009
   151
{
kimonline@7009
   152
    return "PSP builtin joypad";
kimonline@7009
   153
}
kimonline@7009
   154
kimonline@7009
   155
/* Function to perform the mapping from device index to the instance id for this index */
kimonline@7009
   156
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
kimonline@7009
   157
{
kimonline@7009
   158
    return device_index;
kimonline@7009
   159
}
kimonline@7009
   160
kimonline@7009
   161
/* Function to get the device-dependent name of a joystick */
kimonline@7009
   162
const char *SDL_SYS_JoystickName(int index)
kimonline@7009
   163
{
slouken@7191
   164
    if (index == 0)
slouken@7191
   165
        return "PSP controller";
kimonline@7009
   166
slouken@7191
   167
    SDL_SetError("No joystick available with that index");
slouken@7191
   168
    return(NULL);
kimonline@7009
   169
}
kimonline@7009
   170
kimonline@7009
   171
/* Function to open a joystick for use.
kimonline@7009
   172
   The joystick to open is specified by the index field of the joystick.
kimonline@7009
   173
   This should fill the nbuttons and naxes fields of the joystick structure.
kimonline@7009
   174
   It returns 0, or -1 if there is an error.
kimonline@7009
   175
 */
kimonline@7009
   176
int SDL_SYS_JoystickOpen(SDL_Joystick *joystick, int device_index)
kimonline@7009
   177
{
slouken@7191
   178
    joystick->nbuttons = 14;
slouken@7191
   179
    joystick->naxes = 2;
slouken@7191
   180
    joystick->nhats = 0;
kimonline@7009
   181
slouken@7191
   182
    return 0;
kimonline@7009
   183
}
kimonline@7009
   184
kimonline@7009
   185
/* Function to determine is this joystick is attached to the system right now */
kimonline@7009
   186
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
kimonline@7009
   187
{
kimonline@7009
   188
    return SDL_TRUE;
kimonline@7009
   189
}
kimonline@7009
   190
/* Function to update the state of a joystick - called as a device poll.
kimonline@7009
   191
 * This function shouldn't update the joystick structure directly,
kimonline@7009
   192
 * but instead should call SDL_PrivateJoystick*() to deliver events
kimonline@7009
   193
 * and update joystick device state.
kimonline@7009
   194
 */
kimonline@7009
   195
kimonline@7009
   196
void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
kimonline@7009
   197
{
slouken@7191
   198
    int i;
slouken@7191
   199
    enum PspCtrlButtons buttons;
slouken@7191
   200
    enum PspCtrlButtons changed;
slouken@7191
   201
    unsigned char x, y;
slouken@7191
   202
    static enum PspCtrlButtons old_buttons = 0;
slouken@7191
   203
    static unsigned char old_x = 0, old_y = 0;
kimonline@7009
   204
slouken@7191
   205
    SDL_SemWait(pad_sem);
slouken@7191
   206
    buttons = pad.Buttons;
slouken@7191
   207
    x = pad.Lx;
slouken@7191
   208
    y = pad.Ly;
slouken@7191
   209
    SDL_SemPost(pad_sem);
kimonline@7009
   210
slouken@7191
   211
    /* Axes */
slouken@7191
   212
    if(old_x != x) {
slouken@7191
   213
        SDL_PrivateJoystickAxis(joystick, 0, analog_map[x]);
slouken@7191
   214
        old_x = x;
slouken@7191
   215
    }
slouken@7191
   216
    if(old_y != y) {
slouken@7191
   217
        SDL_PrivateJoystickAxis(joystick, 1, analog_map[y]);
slouken@7191
   218
        old_y = y;
slouken@7191
   219
    }
kimonline@7009
   220
slouken@7191
   221
    /* Buttons */
slouken@7191
   222
    changed = old_buttons ^ buttons;
slouken@7191
   223
    old_buttons = buttons;
slouken@7191
   224
    if(changed) {
slouken@7191
   225
        for(i=0; i<sizeof(button_map)/sizeof(button_map[0]); i++) {
slouken@7191
   226
            if(changed & button_map[i]) {
slouken@7191
   227
                SDL_PrivateJoystickButton(
slouken@7191
   228
                    joystick, i,
slouken@7191
   229
                    (buttons & button_map[i]) ?
slouken@7191
   230
                    SDL_PRESSED : SDL_RELEASED);
slouken@7191
   231
            }
slouken@7191
   232
        }
slouken@7191
   233
    }
kimonline@7009
   234
slouken@7191
   235
    sceKernelDelayThread(0);
kimonline@7009
   236
}
kimonline@7009
   237
kimonline@7009
   238
/* Function to close a joystick after use */
kimonline@7009
   239
void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
kimonline@7009
   240
{
slouken@7191
   241
    /* Do nothing. */
kimonline@7009
   242
}
kimonline@7009
   243
kimonline@7009
   244
/* Function to perform any system-specific joystick related cleanup */
kimonline@7009
   245
void SDL_SYS_JoystickQuit(void)
kimonline@7009
   246
{
slouken@7191
   247
    /* Cleanup Threads and Semaphore. */
slouken@7191
   248
    running = 0;
slouken@7191
   249
    SDL_WaitThread(thread, NULL);
slouken@7191
   250
    SDL_DestroySemaphore(pad_sem);
kimonline@7009
   251
}
kimonline@7009
   252
kimonline@7009
   253
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
kimonline@7009
   254
{
kimonline@7009
   255
    SDL_JoystickGUID guid;
slouken@7191
   256
    /* the GUID is just the first 16 chars of the name for now */
kimonline@7009
   257
    const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
kimonline@7009
   258
    SDL_zero( guid );
kimonline@7009
   259
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
kimonline@7009
   260
    return guid;
kimonline@7009
   261
}
kimonline@7009
   262
kimonline@7009
   263
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
kimonline@7009
   264
{
kimonline@7009
   265
    SDL_JoystickGUID guid;
slouken@7191
   266
    /* the GUID is just the first 16 chars of the name for now */
kimonline@7009
   267
    const char *name = joystick->name;
kimonline@7009
   268
    SDL_zero( guid );
kimonline@7009
   269
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
kimonline@7009
   270
    return guid;
kimonline@7009
   271
}
kimonline@7009
   272
kimonline@7009
   273
/* vim: ts=4 sw=4
kimonline@7009
   274
 */