test/testgamecontroller.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 12 Aug 2016 19:59:00 -0400
changeset 10266 c09f06c4e8c8
parent 10162 562b819414b5
permissions -rw-r--r--
emscripten: send fake mouse events for touches, like other targets do. (This really should be handled at the higher level and not in the individual targets, but this fixes the immediate bug.)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
9998
f67cf37e9cd4 Updated copyright to 2016
Sam Lantinga <slouken@libsdl.org>
parents: 9922
diff changeset
     2
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     3
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
  This software is provided 'as-is', without any express or implied
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     5
  warranty.  In no event will the authors be held liable for any damages
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     6
  arising from the use of this software.
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     7
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
  Permission is granted to anyone to use this software for any purpose,
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     9
  including commercial applications, and to alter it and redistribute it
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    10
  freely.
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    11
*/
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    13
/* Simple program to test the SDL game controller routines */
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    14
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    15
#include <stdio.h>
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    16
#include <stdlib.h>
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    17
#include <string.h>
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    18
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    19
#include "SDL.h"
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    21
#ifdef __EMSCRIPTEN__
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    22
#include <emscripten/emscripten.h>
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    23
#endif
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    24
6771
55337ff4256f Fix compiler warning; minor harness driver cleanup; fix test build for SDL versions compiled with --disable-joystick or --disable-haptic
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6690
diff changeset
    25
#ifndef SDL_JOYSTICK_DISABLED
55337ff4256f Fix compiler warning; minor harness driver cleanup; fix test build for SDL versions compiled with --disable-joystick or --disable-haptic
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6690
diff changeset
    26
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
#ifdef __IPHONEOS__
9876
1496e502e51d Added initial support for MFi game controllers on iOS.
Alex Szpakowski <slime73@gmail.com>
parents: 9619
diff changeset
    28
#define SCREEN_WIDTH    480
1496e502e51d Added initial support for MFi game controllers on iOS.
Alex Szpakowski <slime73@gmail.com>
parents: 9619
diff changeset
    29
#define SCREEN_HEIGHT    320
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    30
#else
8199
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    31
#define SCREEN_WIDTH    512
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    32
#define SCREEN_HEIGHT   317
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    33
#endif
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    34
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    35
/* This is indexed by SDL_GameControllerButton. */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    36
static const struct { int x; int y; } button_positions[] = {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    37
    {387, 167},  /* A */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    38
    {431, 132},  /* B */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    39
    {342, 132},  /* X */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    40
    {389, 101},  /* Y */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    41
    {174, 132},  /* BACK */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    42
    {233, 132},  /* GUIDE */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    43
    {289, 132},  /* START */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    44
    {75,  154},  /* LEFTSTICK */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    45
    {305, 230},  /* RIGHTSTICK */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    46
    {77,  40},   /* LEFTSHOULDER */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    47
    {396, 36},   /* RIGHTSHOULDER */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    48
    {154, 188},  /* DPAD_UP */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    49
    {154, 249},  /* DPAD_DOWN */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    50
    {116, 217},  /* DPAD_LEFT */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    51
    {186, 217},  /* DPAD_RIGHT */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    52
};
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    53
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    54
/* This is indexed by SDL_GameControllerAxis. */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    55
static const struct { int x; int y; double angle; } axis_positions[] = {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    56
    {75,  154, 0.0},  /* LEFTX */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    57
    {75,  154, 90.0},  /* LEFTY */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    58
    {305, 230, 0.0},  /* RIGHTX */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    59
    {305, 230, 90.0},  /* RIGHTY */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    60
    {91, 0, 90.0},     /* TRIGGERLEFT */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    61
    {375, 0, 90.0},    /* TRIGGERRIGHT */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    62
};
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    63
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    64
SDL_Renderer *screen = NULL;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    65
SDL_bool retval = SDL_FALSE;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    66
SDL_bool done = SDL_FALSE;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    67
SDL_Texture *background, *button, *axis;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
    68
8199
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    69
static SDL_Texture *
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    70
LoadTexture(SDL_Renderer *renderer, char *file, SDL_bool transparent)
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    71
{
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    72
    SDL_Surface *temp = NULL;
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    73
    SDL_Texture *texture = NULL;
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    74
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    75
    temp = SDL_LoadBMP(file);
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    76
    if (temp == NULL) {
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    77
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    78
    } else {
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    79
        /* Set transparent pixel as the pixel at (0,0) */
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    80
        if (transparent) {
8687
1d2ca48523a7 Fixed assertion loading button.bmp in testgamecontroller
Sam Lantinga <slouken@libsdl.org>
parents: 8202
diff changeset
    81
            if (temp->format->BytesPerPixel == 1) {
1d2ca48523a7 Fixed assertion loading button.bmp in testgamecontroller
Sam Lantinga <slouken@libsdl.org>
parents: 8202
diff changeset
    82
                SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *)temp->pixels);
1d2ca48523a7 Fixed assertion loading button.bmp in testgamecontroller
Sam Lantinga <slouken@libsdl.org>
parents: 8202
diff changeset
    83
            } else {
1d2ca48523a7 Fixed assertion loading button.bmp in testgamecontroller
Sam Lantinga <slouken@libsdl.org>
parents: 8202
diff changeset
    84
                SDL_assert(!temp->format->palette);
1d2ca48523a7 Fixed assertion loading button.bmp in testgamecontroller
Sam Lantinga <slouken@libsdl.org>
parents: 8202
diff changeset
    85
                SDL_assert(temp->format->BitsPerPixel == 24);
1d2ca48523a7 Fixed assertion loading button.bmp in testgamecontroller
Sam Lantinga <slouken@libsdl.org>
parents: 8202
diff changeset
    86
                SDL_SetColorKey(temp, SDL_TRUE, (*(Uint32 *)temp->pixels) & 0x00FFFFFF);
1d2ca48523a7 Fixed assertion loading button.bmp in testgamecontroller
Sam Lantinga <slouken@libsdl.org>
parents: 8202
diff changeset
    87
            }
8199
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    88
        }
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    89
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    90
        texture = SDL_CreateTextureFromSurface(renderer, temp);
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    91
        if (!texture) {
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    92
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    93
        }
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    94
    }
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    95
    if (temp) {
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    96
        SDL_FreeSurface(temp);
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    97
    }
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    98
    return texture;
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    99
}
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   100
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   101
void
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   102
loop(void *arg)
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   103
{
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   104
    SDL_Event event;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   105
    int i;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   106
    SDL_GameController *gamecontroller = (SDL_GameController *)arg;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   107
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   108
    /* blank screen, set up for drawing this frame. */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   109
    SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   110
    SDL_RenderClear(screen);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   111
    SDL_RenderCopy(screen, background, NULL, NULL);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   112
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   113
    while (SDL_PollEvent(&event)) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   114
        switch (event.type) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   115
        case SDL_KEYDOWN:
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   116
            if (event.key.keysym.sym != SDLK_ESCAPE) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   117
                break;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   118
            }
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   119
            /* Fall through to signal quit */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   120
        case SDL_QUIT:
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   121
            done = SDL_TRUE;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   122
            break;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   123
        default:
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   124
            break;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   125
        }
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   126
    }
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   127
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   128
    /* Update visual controller state */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   129
    for (i = 0; i < SDL_CONTROLLER_BUTTON_MAX; ++i) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   130
        if (SDL_GameControllerGetButton(gamecontroller, (SDL_GameControllerButton)i) == SDL_PRESSED) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   131
            const SDL_Rect dst = { button_positions[i].x, button_positions[i].y, 50, 50 };
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   132
            SDL_RenderCopyEx(screen, button, NULL, &dst, 0, NULL, 0);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   133
        }
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   134
    }
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   135
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   136
    for (i = 0; i < SDL_CONTROLLER_AXIS_MAX; ++i) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   137
        const Sint16 deadzone = 8000;  /* !!! FIXME: real deadzone */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   138
        const Sint16 value = SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i));
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   139
        if (value < -deadzone) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   140
            const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   141
            const double angle = axis_positions[i].angle;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   142
            SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, 0);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   143
        } else if (value > deadzone) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   144
            const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   145
            const double angle = axis_positions[i].angle + 180.0;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   146
            SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, 0);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   147
        }
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   148
    }
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   149
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   150
    SDL_RenderPresent(screen);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   151
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   152
    if (!SDL_GameControllerGetAttached(gamecontroller)) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   153
        done = SDL_TRUE;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   154
        retval = SDL_TRUE;  /* keep going, wait for reattach. */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   155
    }
9607
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9340
diff changeset
   156
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9340
diff changeset
   157
#ifdef __EMSCRIPTEN__
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9340
diff changeset
   158
    if (done) {
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9340
diff changeset
   159
        emscripten_cancel_main_loop();
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9340
diff changeset
   160
    }
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9340
diff changeset
   161
#endif
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   162
}
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   163
8068
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   164
SDL_bool
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   165
WatchGameController(SDL_GameController * gamecontroller)
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   166
{
6916
92e978d6dafa Minor cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 6914
diff changeset
   167
    const char *name = SDL_GameControllerName(gamecontroller);
6914
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
   168
    const char *basetitle = "Game Controller Test: ";
6916
92e978d6dafa Minor cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 6914
diff changeset
   169
    const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + 1;
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   170
    char *title = (char *)SDL_malloc(titlelen);
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   171
    SDL_Window *window = NULL;
9340
af5be9462bb4 Fixed test programs for joystick not exiting on events after first disconnect.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9290
diff changeset
   172
af5be9462bb4 Fixed test programs for joystick not exiting on events after first disconnect.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9290
diff changeset
   173
    retval = SDL_FALSE;
af5be9462bb4 Fixed test programs for joystick not exiting on events after first disconnect.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9290
diff changeset
   174
    done = SDL_FALSE;
af5be9462bb4 Fixed test programs for joystick not exiting on events after first disconnect.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9290
diff changeset
   175
6914
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
   176
    if (title) {
6916
92e978d6dafa Minor cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 6914
diff changeset
   177
        SDL_snprintf(title, titlelen, "%s%s", basetitle, name);
6914
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
   178
    }
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
   179
8199
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   180
    /* Create a window to display controller state */
6914
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
   181
    window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   182
                              SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
7478
0d02f6a323f5 Removed not needed SDL_WINDOW_SHOWN from test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7191
diff changeset
   183
                              SCREEN_HEIGHT, 0);
10162
562b819414b5 Fixed memory leak in game controller test program.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9998
diff changeset
   184
    SDL_free(title);
562b819414b5 Fixed memory leak in game controller test program.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9998
diff changeset
   185
    title = NULL;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   186
    if (window == NULL) {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   187
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
8068
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   188
        return SDL_FALSE;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   189
    }
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   190
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   191
    screen = SDL_CreateRenderer(window, -1, 0);
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   192
    if (screen == NULL) {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   193
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   194
        SDL_DestroyWindow(window);
8068
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   195
        return SDL_FALSE;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   196
    }
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   197
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   198
    SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   199
    SDL_RenderClear(screen);
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   200
    SDL_RenderPresent(screen);
7056
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   201
    SDL_RaiseWindow(window);
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   202
8202
b84a1f55075e Make controllermap, etc, work on platforms with hardcoded window sizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 8199
diff changeset
   203
    /* scale for platforms that don't give you the window size you asked for. */
b84a1f55075e Make controllermap, etc, work on platforms with hardcoded window sizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 8199
diff changeset
   204
    SDL_RenderSetLogicalSize(screen, SCREEN_WIDTH, SCREEN_HEIGHT);
b84a1f55075e Make controllermap, etc, work on platforms with hardcoded window sizes.
Ryan C. Gordon <icculus@icculus.org>
parents: 8199
diff changeset
   205
8199
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   206
    background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   207
    button = LoadTexture(screen, "button.bmp", SDL_TRUE);
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   208
    axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   209
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   210
    if (!background || !button || !axis) {
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   211
        SDL_DestroyRenderer(screen);
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   212
        SDL_DestroyWindow(window);
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   213
        return SDL_FALSE;
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   214
    }
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   215
    SDL_SetTextureColorMod(button, 10, 255, 21);
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   216
    SDL_SetTextureColorMod(axis, 10, 255, 21);
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   217
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   218
    /* !!! FIXME: */
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   219
    /*SDL_RenderSetLogicalSize(screen, background->w, background->h);*/
a060c6a7ae4d Replace testgamecontroller visualization mode with something more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   220
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   221
    /* Print info about the controller we are watching */
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   222
    SDL_Log("Watching controller %s\n",  name ? name : "Unknown Controller");
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7056
diff changeset
   223
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   224
    /* Loop, getting controller events! */
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   225
#ifdef __EMSCRIPTEN__
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   226
    emscripten_set_main_loop_arg(loop, gamecontroller, 0, 1);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   227
#else
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   228
    while (!done) {
9290
fe975b2313fa Fixed compile errors in example programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
   229
        loop(gamecontroller);
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   230
    }
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9102
diff changeset
   231
#endif
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   232
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   233
    SDL_DestroyRenderer(screen);
9340
af5be9462bb4 Fixed test programs for joystick not exiting on events after first disconnect.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9290
diff changeset
   234
    screen = NULL;
af5be9462bb4 Fixed test programs for joystick not exiting on events after first disconnect.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9290
diff changeset
   235
    background = NULL;
af5be9462bb4 Fixed test programs for joystick not exiting on events after first disconnect.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9290
diff changeset
   236
    button = NULL;
af5be9462bb4 Fixed test programs for joystick not exiting on events after first disconnect.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9290
diff changeset
   237
    axis = NULL;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   238
    SDL_DestroyWindow(window);
8068
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   239
    return retval;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   240
}
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   241
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   242
int
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   243
main(int argc, char *argv[])
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   244
{
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   245
    int i;
7056
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   246
    int nController = 0;
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   247
    int retcode = 0;
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   248
    char guid[64];
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   249
    SDL_GameController *gamecontroller;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   250
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   251
    /* Enable standard application logging */
9922
b066d5fd5fa7 Replaced tabs with spaces in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9916
diff changeset
   252
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   253
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   254
    /* Initialize SDL (Note: video is required to start event loop) */
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   255
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER ) < 0) {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   256
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
7056
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   257
        return 1;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   258
    }
8043
74fcbf6987ba Adds controllermap utility to test suite.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7705
diff changeset
   259
    
74fcbf6987ba Adds controllermap utility to test suite.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7705
diff changeset
   260
    SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt");
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   261
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   262
    /* Print information about the controller */
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   263
    for (i = 0; i < SDL_NumJoysticks(); ++i) {
7056
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   264
        const char *name;
7705
e70dc8b38f26 Fixed testgamecontroller output to make sense.
Ryan C. Gordon <icculus@icculus.org>
parents: 7639
diff changeset
   265
        const char *description;
7056
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   266
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   267
        SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(i),
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   268
                                  guid, sizeof (guid));
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   269
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   270
        if ( SDL_IsGameController(i) )
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   271
        {
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   272
            nController++;
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   273
            name = SDL_GameControllerNameForIndex(i);
7705
e70dc8b38f26 Fixed testgamecontroller output to make sense.
Ryan C. Gordon <icculus@icculus.org>
parents: 7639
diff changeset
   274
            description = "Controller";
7056
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   275
        } else {
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   276
            name = SDL_JoystickNameForIndex(i);
7705
e70dc8b38f26 Fixed testgamecontroller output to make sense.
Ryan C. Gordon <icculus@icculus.org>
parents: 7639
diff changeset
   277
            description = "Joystick";
7056
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   278
        }
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   279
        SDL_Log("%s %d: %s (guid %s)\n", description, i, name ? name : "Unknown", guid);
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   280
    }
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   281
    SDL_Log("There are %d game controller(s) attached (%d joystick(s))\n", nController, SDL_NumJoysticks());
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   282
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   283
    if (argv[1]) {
8068
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   284
        SDL_bool reportederror = SDL_FALSE;
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   285
        SDL_bool keepGoing = SDL_TRUE;
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   286
        SDL_Event event;
7056
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   287
        int device = atoi(argv[1]);
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   288
        if (device >= SDL_NumJoysticks()) {
9922
b066d5fd5fa7 Replaced tabs with spaces in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9916
diff changeset
   289
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%i is an invalid joystick index.\n", device);
7056
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   290
            retcode = 1;
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   291
        } else {
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   292
            SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(device),
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   293
                                      guid, sizeof (guid));
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   294
            SDL_Log("Attempting to open device %i, guid %s\n", device, guid);
7056
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   295
            gamecontroller = SDL_GameControllerOpen(device);
9916
9429bc102632 Added SDL_JoystickFromInstanceID() and SDL_GameControllerFromInstanceID().
Ryan C. Gordon <icculus@icculus.org>
parents: 9876
diff changeset
   296
9429bc102632 Added SDL_JoystickFromInstanceID() and SDL_GameControllerFromInstanceID().
Ryan C. Gordon <icculus@icculus.org>
parents: 9876
diff changeset
   297
            if (gamecontroller != NULL) {
9429bc102632 Added SDL_JoystickFromInstanceID() and SDL_GameControllerFromInstanceID().
Ryan C. Gordon <icculus@icculus.org>
parents: 9876
diff changeset
   298
                SDL_assert(SDL_GameControllerFromInstanceID(SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(gamecontroller))) == gamecontroller);
9429bc102632 Added SDL_JoystickFromInstanceID() and SDL_GameControllerFromInstanceID().
Ryan C. Gordon <icculus@icculus.org>
parents: 9876
diff changeset
   299
            }
9429bc102632 Added SDL_JoystickFromInstanceID() and SDL_GameControllerFromInstanceID().
Ryan C. Gordon <icculus@icculus.org>
parents: 9876
diff changeset
   300
8068
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   301
            while (keepGoing) {
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   302
                if (gamecontroller == NULL) {
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   303
                    if (!reportederror) {
9611
c23b2fa8d4c2 Removed redundant NULL check in test program.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9607
diff changeset
   304
                        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open gamecontroller %d: %s\n", device, SDL_GetError());
c23b2fa8d4c2 Removed redundant NULL check in test program.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9607
diff changeset
   305
                        retcode = 1;
8068
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   306
                        keepGoing = SDL_FALSE;
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   307
                        reportederror = SDL_TRUE;
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   308
                    }
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   309
                } else {
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   310
                    reportederror = SDL_FALSE;
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   311
                    keepGoing = WatchGameController(gamecontroller);
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   312
                    SDL_GameControllerClose(gamecontroller);
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   313
                }
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   314
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   315
                gamecontroller = NULL;
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   316
                if (keepGoing) {
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   317
                    SDL_Log("Waiting for attach\n");
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   318
                }
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   319
                while (keepGoing) {
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   320
                    SDL_WaitEvent(&event);
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   321
                    if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   322
                        || (event.type == SDL_MOUSEBUTTONDOWN)) {
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   323
                        keepGoing = SDL_FALSE;
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   324
                    } else if (event.type == SDL_CONTROLLERDEVICEADDED) {
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   325
                        gamecontroller = SDL_GameControllerOpen(event.cdevice.which);
9916
9429bc102632 Added SDL_JoystickFromInstanceID() and SDL_GameControllerFromInstanceID().
Ryan C. Gordon <icculus@icculus.org>
parents: 9876
diff changeset
   326
                        if (gamecontroller != NULL) {
9429bc102632 Added SDL_JoystickFromInstanceID() and SDL_GameControllerFromInstanceID().
Ryan C. Gordon <icculus@icculus.org>
parents: 9876
diff changeset
   327
                            SDL_assert(SDL_GameControllerFromInstanceID(SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(gamecontroller))) == gamecontroller);
9429bc102632 Added SDL_JoystickFromInstanceID() and SDL_GameControllerFromInstanceID().
Ryan C. Gordon <icculus@icculus.org>
parents: 9876
diff changeset
   328
                        }
8068
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   329
                        break;
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   330
                    }
54fcab720079 Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
Sam Lantinga <slouken@libsdl.org>
parents: 8043
diff changeset
   331
                }
7056
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   332
            }
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   333
        }
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   334
    }
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   335
7056
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   336
    SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   337
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   338
    return retcode;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   339
}
6771
55337ff4256f Fix compiler warning; minor harness driver cleanup; fix test build for SDL versions compiled with --disable-joystick or --disable-haptic
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6690
diff changeset
   340
55337ff4256f Fix compiler warning; minor harness driver cleanup; fix test build for SDL versions compiled with --disable-joystick or --disable-haptic
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6690
diff changeset
   341
#else
55337ff4256f Fix compiler warning; minor harness driver cleanup; fix test build for SDL versions compiled with --disable-joystick or --disable-haptic
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6690
diff changeset
   342
55337ff4256f Fix compiler warning; minor harness driver cleanup; fix test build for SDL versions compiled with --disable-joystick or --disable-haptic
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6690
diff changeset
   343
int
55337ff4256f Fix compiler warning; minor harness driver cleanup; fix test build for SDL versions compiled with --disable-joystick or --disable-haptic
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6690
diff changeset
   344
main(int argc, char *argv[])
55337ff4256f Fix compiler warning; minor harness driver cleanup; fix test build for SDL versions compiled with --disable-joystick or --disable-haptic
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6690
diff changeset
   345
{
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   346
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
6771
55337ff4256f Fix compiler warning; minor harness driver cleanup; fix test build for SDL versions compiled with --disable-joystick or --disable-haptic
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6690
diff changeset
   347
    exit(1);
55337ff4256f Fix compiler warning; minor harness driver cleanup; fix test build for SDL versions compiled with --disable-joystick or --disable-haptic
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6690
diff changeset
   348
}
55337ff4256f Fix compiler warning; minor harness driver cleanup; fix test build for SDL versions compiled with --disable-joystick or --disable-haptic
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6690
diff changeset
   349
55337ff4256f Fix compiler warning; minor harness driver cleanup; fix test build for SDL versions compiled with --disable-joystick or --disable-haptic
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6690
diff changeset
   350
#endif