test/testgamecontroller.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sat, 20 Jul 2013 21:47:16 +0200
changeset 7478 0d02f6a323f5
parent 7191 75360622e65f
child 7517 965d57022c01
permissions -rw-r--r--
Removed not needed SDL_WINDOW_SHOWN from test programs. See bug #1667.
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
/*
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     2
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
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
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
    21
#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
    22
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    23
#ifdef __IPHONEOS__
7056
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
    24
#define SCREEN_WIDTH    320
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
    25
#define SCREEN_HEIGHT    480
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    26
#else
7056
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
    27
#define SCREEN_WIDTH    640
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
    28
#define SCREEN_HEIGHT    480
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    29
#endif
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    30
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    31
#define MAX_NUM_AXES 6
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    32
#define MAX_NUM_HATS 2
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    33
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    34
static SDL_bool s_ForceQuit = SDL_FALSE;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    35
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
static void
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    37
DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    38
{
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    39
    const SDL_Rect area = { x, y, w, h };
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    40
    SDL_RenderFillRect(r, &area);
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    41
}
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    42
6914
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    43
static const char *
6917
47dc155b774d Changed the game controller enum types to match SDL naming conventions.
Ryan C. Gordon <icculus@icculus.org>
parents: 6916
diff changeset
    44
ControllerAxisName(const SDL_GameControllerAxis axis)
6914
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    45
{
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    46
    switch (axis)
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    47
    {
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    48
        #define AXIS_CASE(ax) case SDL_CONTROLLER_AXIS_##ax: return #ax
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    49
        AXIS_CASE(INVALID);
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    50
        AXIS_CASE(LEFTX);
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    51
        AXIS_CASE(LEFTY);
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    52
        AXIS_CASE(RIGHTX);
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    53
        AXIS_CASE(RIGHTY);
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    54
        AXIS_CASE(TRIGGERLEFT);
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    55
        AXIS_CASE(TRIGGERRIGHT);
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    56
        #undef AXIS_CASE
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    57
        default: return "???";
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    58
    }
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    59
}
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    60
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    61
static const char *
6917
47dc155b774d Changed the game controller enum types to match SDL naming conventions.
Ryan C. Gordon <icculus@icculus.org>
parents: 6916
diff changeset
    62
ControllerButtonName(const SDL_GameControllerButton button)
6914
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    63
{
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    64
    switch (button)
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    65
    {
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    66
        #define BUTTON_CASE(btn) case SDL_CONTROLLER_BUTTON_##btn: return #btn
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    67
        BUTTON_CASE(INVALID);
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    68
        BUTTON_CASE(A);
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    69
        BUTTON_CASE(B);
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    70
        BUTTON_CASE(X);
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    71
        BUTTON_CASE(Y);
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    72
        BUTTON_CASE(BACK);
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    73
        BUTTON_CASE(GUIDE);
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    74
        BUTTON_CASE(START);
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    75
        BUTTON_CASE(LEFTSTICK);
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    76
        BUTTON_CASE(RIGHTSTICK);
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    77
        BUTTON_CASE(LEFTSHOULDER);
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    78
        BUTTON_CASE(RIGHTSHOULDER);
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    79
        BUTTON_CASE(DPAD_UP);
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    80
        BUTTON_CASE(DPAD_DOWN);
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    81
        BUTTON_CASE(DPAD_LEFT);
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    82
        BUTTON_CASE(DPAD_RIGHT);
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    83
        #undef BUTTON_CASE
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    84
        default: return "???";
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    85
    }
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    86
}
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    87
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    88
void
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    89
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
    90
{
6916
92e978d6dafa Minor cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 6914
diff changeset
    91
    const char *name = SDL_GameControllerName(gamecontroller);
6914
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    92
    const char *basetitle = "Game Controller Test: ";
6916
92e978d6dafa Minor cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 6914
diff changeset
    93
    const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + 1;
6914
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
    94
    char *title = 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
    95
    SDL_Window *window = NULL;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    96
    SDL_Renderer *screen = NULL;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    97
    int done = 0;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    98
    SDL_Event event;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    99
    int i;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   100
6914
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
   101
    if (title) {
6916
92e978d6dafa Minor cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 6914
diff changeset
   102
        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
   103
    }
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
   104
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   105
    /* Create a window to display controller axis position */
6914
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
   106
    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
   107
                              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
   108
                              SCREEN_HEIGHT, 0);
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   109
    if (window == NULL) {
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   110
        fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   111
        return;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   112
    }
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   113
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   114
    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
   115
    if (screen == NULL) {
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   116
        fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   117
        SDL_DestroyWindow(window);
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   118
        return;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   119
    }
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   120
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   121
    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
   122
    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
   123
    SDL_RenderPresent(screen);
7056
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   124
    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
   125
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   126
    /* Print info about the controller we are watching */
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   127
    printf("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
   128
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   129
    /* Loop, getting controller events! */
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   130
    while (!done) {
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   131
        /* blank screen, set up for drawing this frame. */
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   132
        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
   133
        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
   134
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   135
        while (SDL_PollEvent(&event)) {
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   136
            switch (event.type) {
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   137
            case SDL_CONTROLLERAXISMOTION:
6914
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
   138
                printf("Controller %d axis %d ('%s') value: %d\n",
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   139
                       event.caxis.which,
6914
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
   140
                       event.caxis.axis,
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
   141
                       ControllerAxisName(event.caxis.axis),
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
   142
                       event.caxis.value);
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   143
                break;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   144
            case SDL_CONTROLLERBUTTONDOWN:
6914
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
   145
                printf("Controller %d button %d ('%s') down\n",
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
   146
                       event.cbutton.which, event.cbutton.button,
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
   147
                       ControllerButtonName(event.cbutton.button));
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   148
                break;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   149
            case SDL_CONTROLLERBUTTONUP:
6914
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
   150
                printf("Controller %d button %d ('%s') up\n",
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
   151
                       event.cbutton.which, event.cbutton.button,
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
   152
                       ControllerButtonName(event.cbutton.button));
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   153
                break;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   154
            case SDL_KEYDOWN:
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   155
                if (event.key.keysym.sym != SDLK_ESCAPE) {
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   156
                    break;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   157
                }
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   158
                /* Fall through to signal quit */
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   159
            case SDL_QUIT:
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   160
                done = 1;
6914
8a9915ab24f0 Made testgamecontroller minimally more useful.
Ryan C. Gordon <icculus@icculus.org>
parents: 6771
diff changeset
   161
                s_ForceQuit = SDL_TRUE;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   162
                break;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   163
            default:
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   164
                break;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   165
            }
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   166
        }
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   167
        /* Update visual controller state */
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   168
        SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 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
   169
        for (i = 0; i <SDL_CONTROLLER_BUTTON_MAX; ++i) {
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   170
            if (SDL_GameControllerGetButton(gamecontroller, i) == SDL_PRESSED) {
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   171
                DrawRect(screen, i * 34, SCREEN_HEIGHT - 34, 32, 32);
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   172
            }
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   173
        }
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   174
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   175
        SDL_SetRenderDrawColor(screen, 0xFF, 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
   176
        for (i = 0; i < SDL_CONTROLLER_AXIS_MAX / 2; ++i) {
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   177
            /* Draw the X/Y axis */
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   178
            int x, y;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   179
            x = (((int) SDL_GameControllerGetAxis(gamecontroller, i * 2 + 0)) + 32768);
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   180
            x *= SCREEN_WIDTH;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   181
            x /= 65535;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   182
            if (x < 0) {
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   183
                x = 0;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   184
            } else if (x > (SCREEN_WIDTH - 16)) {
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   185
                x = SCREEN_WIDTH - 16;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   186
            }
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   187
            y = (((int) SDL_GameControllerGetAxis(gamecontroller, i * 2 + 1)) + 32768);
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   188
            y *= SCREEN_HEIGHT;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   189
            y /= 65535;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   190
            if (y < 0) {
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   191
                y = 0;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   192
            } else if (y > (SCREEN_HEIGHT - 16)) {
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   193
                y = SCREEN_HEIGHT - 16;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   194
            }
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   195
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   196
            DrawRect(screen, x, y, 16, 16);
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
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   199
        SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, 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
   200
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   201
        SDL_RenderPresent(screen);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7056
diff changeset
   202
7056
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   203
        if ( !done )
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   204
            done = SDL_GameControllerGetAttached( gamecontroller ) == 0;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   205
    }
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   206
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   207
    SDL_DestroyRenderer(screen);
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   208
    SDL_DestroyWindow(window);
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   209
}
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   210
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   211
int
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   212
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
   213
{
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   214
    int i;
7056
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   215
    int nController = 0;
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   216
    int retcode = 0;
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   217
    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
   218
    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
   219
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   220
    /* 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
   221
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER ) < 0) {
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   222
        fprintf(stderr, "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
   223
        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
   224
    }
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   225
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   226
    /* 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
   227
    for (i = 0; i < SDL_NumJoysticks(); ++i) {
7056
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   228
        const char *name;
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   229
        const char *description = "Joystick (not recognized as game controller)";
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   230
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   231
        SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(i),
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   232
                                  guid, sizeof (guid));
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   233
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   234
        if ( SDL_IsGameController(i) )
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   235
        {
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   236
            nController++;
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   237
            name = SDL_GameControllerNameForIndex(i);
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   238
        } else {
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   239
            name = SDL_JoystickNameForIndex(i);
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   240
        }
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   241
        printf("%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
   242
    }
7056
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   243
    printf("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
   244
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   245
    if (argv[1]) {
7056
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   246
        int device = atoi(argv[1]);
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   247
        if (device >= SDL_NumJoysticks()) {
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   248
            printf("%i is an invalid joystick index.\n", device);
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   249
            retcode = 1;
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   250
        } else {
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   251
            SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(device),
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   252
                                      guid, sizeof (guid));
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   253
            printf("Attempting to open device %i, guid %s\n", device, guid);
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   254
            gamecontroller = SDL_GameControllerOpen(device);
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   255
            if (gamecontroller == NULL) {
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   256
                printf("Couldn't open joystick %d: %s\n", device, SDL_GetError());
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   257
                retcode = 1;
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   258
            } else {
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   259
                WatchGameController(gamecontroller);
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   260
                SDL_GameControllerClose(gamecontroller);
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   261
            }
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   262
        }
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   263
    }
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   264
7056
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   265
    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
   266
a5699778d80f Improve testgamecontroller output & robustness.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6917
diff changeset
   267
    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
   268
}
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
   269
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
   270
#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
   271
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
   272
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
   273
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
   274
{
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
   275
    fprintf(stderr, "SDL compiled without Joystick support.\n");
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
   276
    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
   277
}
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
   278
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
   279
#endif