test/testjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 19 Mar 2013 22:25:02 -0700
changeset 7017 7c2eb015a6d7
parent 6771 55337ff4256f
child 7191 75360622e65f
permissions -rw-r--r--
Fixed bug 1759 - Updates to testjoystick.c ny00 A minor patch is attached, with the following few changes to testjoystick.c: - Unused constant definitions have been removed. - Output for all analog axes is drawn, even when there is an odd number of axes. (I have a controller with 5 analog axes.) - Buttons are now drawn on two rows, so there's room for more. In fact, it has been used for testing a proposed joystick patch for Android, where large button ID numbers have been involved (20 and up). For more details see http://bugzilla.libsdl.org/show_bug.cgi?id=1700. - A few adaptations have been done for the Android platform, assuming joystick support is ever applied to it. One of them is that the very first joystick (in the enumeration of all joysticks) is opened for testing, if there is any. - It is now possible to quit from the calibration by pressing on a mouse button, tapping on a touchscreen or pressing/tapping on the "Back" button of an Android device. Technically, a press on a key identified by key code SDLK_AC_BACK results in that.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 2765
diff changeset
     1
/*
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 2765
diff changeset
     2
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 2765
diff changeset
     3
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 2765
diff changeset
     4
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 2765
diff changeset
     5
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 2765
diff changeset
     6
  arising from the use of this software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 2765
diff changeset
     7
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 2765
diff changeset
     8
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 2765
diff changeset
     9
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 2765
diff changeset
    10
  freely.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 2765
diff changeset
    11
*/
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    12
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    13
/* Simple program to test the SDL joystick routines */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    14
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    15
#include <stdio.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    16
#include <stdlib.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    17
#include <string.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    18
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    19
#include "SDL.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
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: 6747
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: 6747
diff changeset
    22
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
    23
#ifdef __IPHONEOS__
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
    24
#define SCREEN_WIDTH	320
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
    25
#define SCREEN_HEIGHT	480
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
    26
#else
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
    27
#define SCREEN_WIDTH	640
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
    28
#define SCREEN_HEIGHT	480
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
    29
#endif
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    30
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: 6747
diff changeset
    31
6398
c294faf5fce5 Cleaned up testjoystick.c, improved usage of renderer API, added colors!
Ryan C. Gordon <icculus@icculus.org>
parents: 6397
diff changeset
    32
static void
c294faf5fce5 Cleaned up testjoystick.c, improved usage of renderer API, added colors!
Ryan C. Gordon <icculus@icculus.org>
parents: 6397
diff changeset
    33
DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
c294faf5fce5 Cleaned up testjoystick.c, improved usage of renderer API, added colors!
Ryan C. Gordon <icculus@icculus.org>
parents: 6397
diff changeset
    34
{
c294faf5fce5 Cleaned up testjoystick.c, improved usage of renderer API, added colors!
Ryan C. Gordon <icculus@icculus.org>
parents: 6397
diff changeset
    35
    const SDL_Rect area = { x, y, w, h };
c294faf5fce5 Cleaned up testjoystick.c, improved usage of renderer API, added colors!
Ryan C. Gordon <icculus@icculus.org>
parents: 6397
diff changeset
    36
    SDL_RenderFillRect(r, &area);
c294faf5fce5 Cleaned up testjoystick.c, improved usage of renderer API, added colors!
Ryan C. Gordon <icculus@icculus.org>
parents: 6397
diff changeset
    37
}
c294faf5fce5 Cleaned up testjoystick.c, improved usage of renderer API, added colors!
Ryan C. Gordon <icculus@icculus.org>
parents: 6397
diff changeset
    38
6730
36158e4061f4 Allow testjoystick to quit, instead of looping on hotplug events forever.
Ryan C. Gordon <icculus@icculus.org>
parents: 6690
diff changeset
    39
static SDL_bool
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
    40
WatchJoystick(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    41
{
6398
c294faf5fce5 Cleaned up testjoystick.c, improved usage of renderer API, added colors!
Ryan C. Gordon <icculus@icculus.org>
parents: 6397
diff changeset
    42
    SDL_Window *window = NULL;
c294faf5fce5 Cleaned up testjoystick.c, improved usage of renderer API, added colors!
Ryan C. Gordon <icculus@icculus.org>
parents: 6397
diff changeset
    43
    SDL_Renderer *screen = NULL;
c294faf5fce5 Cleaned up testjoystick.c, improved usage of renderer API, added colors!
Ryan C. Gordon <icculus@icculus.org>
parents: 6397
diff changeset
    44
    const char *name = NULL;
6730
36158e4061f4 Allow testjoystick to quit, instead of looping on hotplug events forever.
Ryan C. Gordon <icculus@icculus.org>
parents: 6690
diff changeset
    45
    SDL_bool retval = SDL_FALSE;
36158e4061f4 Allow testjoystick to quit, instead of looping on hotplug events forever.
Ryan C. Gordon <icculus@icculus.org>
parents: 6690
diff changeset
    46
    SDL_bool done = SDL_FALSE;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
    47
    SDL_Event event;
6398
c294faf5fce5 Cleaned up testjoystick.c, improved usage of renderer API, added colors!
Ryan C. Gordon <icculus@icculus.org>
parents: 6397
diff changeset
    48
    int i;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    49
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
    50
    /* Create a window to display joystick axis position */
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
    51
    window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED,
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
    52
                              SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
    53
                              SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
    54
    if (window == NULL) {
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
    55
        fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
6730
36158e4061f4 Allow testjoystick to quit, instead of looping on hotplug events forever.
Ryan C. Gordon <icculus@icculus.org>
parents: 6690
diff changeset
    56
        return SDL_FALSE;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
    57
    }
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
    58
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
    59
    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: 6586
diff changeset
    60
    if (screen == NULL) {
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
    61
        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: 6586
diff changeset
    62
        SDL_DestroyWindow(window);
6730
36158e4061f4 Allow testjoystick to quit, instead of looping on hotplug events forever.
Ryan C. Gordon <icculus@icculus.org>
parents: 6690
diff changeset
    63
        return SDL_FALSE;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
    64
    }
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
    65
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
    66
    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: 6586
diff changeset
    67
    SDL_RenderClear(screen);
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
    68
    SDL_RenderPresent(screen);
6731
572559876818 Cleaned out tabstops, other minor style nitpicks.
Ryan C. Gordon <icculus@icculus.org>
parents: 6730
diff changeset
    69
    SDL_RaiseWindow(window);
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
    70
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
    71
    /* Print info about the joystick we are watching */
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
    72
    name = SDL_JoystickName(joystick);
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
    73
    printf("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
    74
           name ? name : "Unknown Joystick");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
    75
    printf("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
    76
           SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
    77
           SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    78
6398
c294faf5fce5 Cleaned up testjoystick.c, improved usage of renderer API, added colors!
Ryan C. Gordon <icculus@icculus.org>
parents: 6397
diff changeset
    79
    /* Loop, getting joystick events! */
c294faf5fce5 Cleaned up testjoystick.c, improved usage of renderer API, added colors!
Ryan C. Gordon <icculus@icculus.org>
parents: 6397
diff changeset
    80
    while (!done) {
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
    81
        /* 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: 6586
diff changeset
    82
        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: 6586
diff changeset
    83
        SDL_RenderClear(screen);
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
    84
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
    85
        while (SDL_PollEvent(&event)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
    86
            switch (event.type) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
    87
            case SDL_JOYAXISMOTION:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
    88
                printf("Joystick %d axis %d value: %d\n",
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
    89
                       event.jaxis.which,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
    90
                       event.jaxis.axis, event.jaxis.value);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
    91
                break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
    92
            case SDL_JOYHATMOTION:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
    93
                printf("Joystick %d hat %d value:",
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
    94
                       event.jhat.which, event.jhat.hat);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
    95
                if (event.jhat.value == SDL_HAT_CENTERED)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
    96
                    printf(" centered");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
    97
                if (event.jhat.value & SDL_HAT_UP)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
    98
                    printf(" up");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
    99
                if (event.jhat.value & SDL_HAT_RIGHT)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   100
                    printf(" right");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   101
                if (event.jhat.value & SDL_HAT_DOWN)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   102
                    printf(" down");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   103
                if (event.jhat.value & SDL_HAT_LEFT)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   104
                    printf(" left");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   105
                printf("\n");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   106
                break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   107
            case SDL_JOYBALLMOTION:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   108
                printf("Joystick %d ball %d delta: (%d,%d)\n",
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   109
                       event.jball.which,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   110
                       event.jball.ball, event.jball.xrel, event.jball.yrel);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   111
                break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   112
            case SDL_JOYBUTTONDOWN:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   113
                printf("Joystick %d button %d down\n",
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   114
                       event.jbutton.which, event.jbutton.button);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   115
                break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   116
            case SDL_JOYBUTTONUP:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   117
                printf("Joystick %d button %d up\n",
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   118
                       event.jbutton.which, event.jbutton.button);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   119
                break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   120
            case SDL_KEYDOWN:
7017
7c2eb015a6d7 Fixed bug 1759 - Updates to testjoystick.c
Sam Lantinga <slouken@libsdl.org>
parents: 6771
diff changeset
   121
                if ((event.key.keysym.sym != SDLK_ESCAPE) &&
7c2eb015a6d7 Fixed bug 1759 - Updates to testjoystick.c
Sam Lantinga <slouken@libsdl.org>
parents: 6771
diff changeset
   122
                    (event.key.keysym.sym != SDLK_AC_BACK)) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   123
                    break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   124
                }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   125
                /* Fall through to signal quit */
7017
7c2eb015a6d7 Fixed bug 1759 - Updates to testjoystick.c
Sam Lantinga <slouken@libsdl.org>
parents: 6771
diff changeset
   126
            case SDL_FINGERDOWN:
7c2eb015a6d7 Fixed bug 1759 - Updates to testjoystick.c
Sam Lantinga <slouken@libsdl.org>
parents: 6771
diff changeset
   127
            case SDL_MOUSEBUTTONDOWN:
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   128
            case SDL_QUIT:
6730
36158e4061f4 Allow testjoystick to quit, instead of looping on hotplug events forever.
Ryan C. Gordon <icculus@icculus.org>
parents: 6690
diff changeset
   129
                done = SDL_TRUE;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   130
                break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   131
            default:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   132
                break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   133
            }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   134
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   135
        /* Update visual joystick state */
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   136
        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: 6586
diff changeset
   137
        for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   138
            if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
7017
7c2eb015a6d7 Fixed bug 1759 - Updates to testjoystick.c
Sam Lantinga <slouken@libsdl.org>
parents: 6771
diff changeset
   139
                DrawRect(screen, (i%20) * 34, SCREEN_HEIGHT - 68 + (i/20) * 34, 32, 32);
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   140
            }
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   141
        }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   142
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   143
        SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
7017
7c2eb015a6d7 Fixed bug 1759 - Updates to testjoystick.c
Sam Lantinga <slouken@libsdl.org>
parents: 6771
diff changeset
   144
        for (i = 0; i < SDL_JoystickNumAxes(joystick); ++i) {
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   145
            /* 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: 6586
diff changeset
   146
            int x, y;
7017
7c2eb015a6d7 Fixed bug 1759 - Updates to testjoystick.c
Sam Lantinga <slouken@libsdl.org>
parents: 6771
diff changeset
   147
            x = (((int) SDL_JoystickGetAxis(joystick, i)) + 32768);
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   148
            x *= SCREEN_WIDTH;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   149
            x /= 65535;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   150
            if (x < 0) {
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   151
                x = 0;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   152
            } 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: 6586
diff changeset
   153
                x = SCREEN_WIDTH - 16;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   154
            }
7017
7c2eb015a6d7 Fixed bug 1759 - Updates to testjoystick.c
Sam Lantinga <slouken@libsdl.org>
parents: 6771
diff changeset
   155
            ++i;
7c2eb015a6d7 Fixed bug 1759 - Updates to testjoystick.c
Sam Lantinga <slouken@libsdl.org>
parents: 6771
diff changeset
   156
            if (i < SDL_JoystickNumAxes(joystick)) {
7c2eb015a6d7 Fixed bug 1759 - Updates to testjoystick.c
Sam Lantinga <slouken@libsdl.org>
parents: 6771
diff changeset
   157
                y = (((int) SDL_JoystickGetAxis(joystick, i)) + 32768);
7c2eb015a6d7 Fixed bug 1759 - Updates to testjoystick.c
Sam Lantinga <slouken@libsdl.org>
parents: 6771
diff changeset
   158
            } else {
7c2eb015a6d7 Fixed bug 1759 - Updates to testjoystick.c
Sam Lantinga <slouken@libsdl.org>
parents: 6771
diff changeset
   159
                y = 32768;
7c2eb015a6d7 Fixed bug 1759 - Updates to testjoystick.c
Sam Lantinga <slouken@libsdl.org>
parents: 6771
diff changeset
   160
            }
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   161
            y *= SCREEN_HEIGHT;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   162
            y /= 65535;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   163
            if (y < 0) {
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   164
                y = 0;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   165
            } 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: 6586
diff changeset
   166
                y = SCREEN_HEIGHT - 16;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   167
            }
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2201
diff changeset
   168
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   169
            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: 6586
diff changeset
   170
        }
6397
efdf44f20799 Updated testjoystick.c for SDL2 API and draw more information.
Ryan C. Gordon <icculus@icculus.org>
parents: 6256
diff changeset
   171
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   172
        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: 6586
diff changeset
   173
        for (i = 0; i < SDL_JoystickNumHats(joystick); ++i) {
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   174
            /* Derive the new position */
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   175
            int x = SCREEN_WIDTH/2;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   176
            int y = SCREEN_HEIGHT/2;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   177
            const Uint8 hat_pos = SDL_JoystickGetHat(joystick, i);
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   178
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   179
            if (hat_pos & SDL_HAT_UP) {
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   180
                y = 0;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   181
            } else if (hat_pos & SDL_HAT_DOWN) {
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   182
                y = SCREEN_HEIGHT-8;
6397
efdf44f20799 Updated testjoystick.c for SDL2 API and draw more information.
Ryan C. Gordon <icculus@icculus.org>
parents: 6256
diff changeset
   183
            }
efdf44f20799 Updated testjoystick.c for SDL2 API and draw more information.
Ryan C. Gordon <icculus@icculus.org>
parents: 6256
diff changeset
   184
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   185
            if (hat_pos & SDL_HAT_LEFT) {
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   186
                x = 0;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   187
            } else if (hat_pos & SDL_HAT_RIGHT) {
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   188
                x = SCREEN_WIDTH-8;
6397
efdf44f20799 Updated testjoystick.c for SDL2 API and draw more information.
Ryan C. Gordon <icculus@icculus.org>
parents: 6256
diff changeset
   189
            }
efdf44f20799 Updated testjoystick.c for SDL2 API and draw more information.
Ryan C. Gordon <icculus@icculus.org>
parents: 6256
diff changeset
   190
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   191
            DrawRect(screen, x, y, 8, 8);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   192
        }
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   193
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   194
        SDL_RenderPresent(screen);
6730
36158e4061f4 Allow testjoystick to quit, instead of looping on hotplug events forever.
Ryan C. Gordon <icculus@icculus.org>
parents: 6690
diff changeset
   195
36158e4061f4 Allow testjoystick to quit, instead of looping on hotplug events forever.
Ryan C. Gordon <icculus@icculus.org>
parents: 6690
diff changeset
   196
        if (SDL_JoystickGetAttached( joystick ) == 0) {
36158e4061f4 Allow testjoystick to quit, instead of looping on hotplug events forever.
Ryan C. Gordon <icculus@icculus.org>
parents: 6690
diff changeset
   197
            done = SDL_TRUE;
36158e4061f4 Allow testjoystick to quit, instead of looping on hotplug events forever.
Ryan C. Gordon <icculus@icculus.org>
parents: 6690
diff changeset
   198
            retval = SDL_TRUE;  /* keep going, wait for reattach. */
36158e4061f4 Allow testjoystick to quit, instead of looping on hotplug events forever.
Ryan C. Gordon <icculus@icculus.org>
parents: 6690
diff changeset
   199
        }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   200
    }
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   201
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   202
    SDL_DestroyRenderer(screen);
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   203
    SDL_DestroyWindow(window);
6730
36158e4061f4 Allow testjoystick to quit, instead of looping on hotplug events forever.
Ryan C. Gordon <icculus@icculus.org>
parents: 6690
diff changeset
   204
    return retval;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   205
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   206
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   207
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   208
main(int argc, char *argv[])
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   209
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   210
    const char *name;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   211
    int i;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   212
    SDL_Joystick *joystick;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   213
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   214
    /* Initialize SDL (Note: video is required to start event loop) */
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   215
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   216
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   217
        exit(1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   218
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   219
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   220
    /* Print information about the joysticks */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   221
    printf("There are %d joysticks attached\n", SDL_NumJoysticks());
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   222
    for (i = 0; i < SDL_NumJoysticks(); ++i) {
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   223
        name = SDL_JoystickNameForIndex(i);
2201
Sam Lantinga <slouken@libsdl.org>
parents: 2200
diff changeset
   224
        printf("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
2200
893c862eed86 Merged r3292:3293 from branches/SDL-1.2: testjoystick verbose info.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   225
        joystick = SDL_JoystickOpen(i);
893c862eed86 Merged r3292:3293 from branches/SDL-1.2: testjoystick verbose info.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   226
        if (joystick == NULL) {
2201
Sam Lantinga <slouken@libsdl.org>
parents: 2200
diff changeset
   227
            fprintf(stderr, "SDL_JoystickOpen(%d) failed: %s\n", i,
Sam Lantinga <slouken@libsdl.org>
parents: 2200
diff changeset
   228
                    SDL_GetError());
2200
893c862eed86 Merged r3292:3293 from branches/SDL-1.2: testjoystick verbose info.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   229
        } else {
6747
b617821795bf Patched testjoystick.c to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 6736
diff changeset
   230
            char guid[64];
b617821795bf Patched testjoystick.c to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 6736
diff changeset
   231
            SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick),
b617821795bf Patched testjoystick.c to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 6736
diff changeset
   232
                                      guid, sizeof (guid));
2200
893c862eed86 Merged r3292:3293 from branches/SDL-1.2: testjoystick verbose info.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   233
            printf("       axes: %d\n", SDL_JoystickNumAxes(joystick));
893c862eed86 Merged r3292:3293 from branches/SDL-1.2: testjoystick verbose info.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   234
            printf("      balls: %d\n", SDL_JoystickNumBalls(joystick));
893c862eed86 Merged r3292:3293 from branches/SDL-1.2: testjoystick verbose info.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   235
            printf("       hats: %d\n", SDL_JoystickNumHats(joystick));
893c862eed86 Merged r3292:3293 from branches/SDL-1.2: testjoystick verbose info.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   236
            printf("    buttons: %d\n", SDL_JoystickNumButtons(joystick));
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   237
            printf("instance id: %d\n", SDL_JoystickInstanceID(joystick));
6747
b617821795bf Patched testjoystick.c to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 6736
diff changeset
   238
            printf("       guid: %s\n", guid);
2200
893c862eed86 Merged r3292:3293 from branches/SDL-1.2: testjoystick verbose info.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   239
            SDL_JoystickClose(joystick);
893c862eed86 Merged r3292:3293 from branches/SDL-1.2: testjoystick verbose info.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   240
        }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1855
diff changeset
   241
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   242
7017
7c2eb015a6d7 Fixed bug 1759 - Updates to testjoystick.c
Sam Lantinga <slouken@libsdl.org>
parents: 6771
diff changeset
   243
#ifdef ANDROID
7c2eb015a6d7 Fixed bug 1759 - Updates to testjoystick.c
Sam Lantinga <slouken@libsdl.org>
parents: 6771
diff changeset
   244
    if (SDL_NumJoysticks() > 0) {
7c2eb015a6d7 Fixed bug 1759 - Updates to testjoystick.c
Sam Lantinga <slouken@libsdl.org>
parents: 6771
diff changeset
   245
#else
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   246
    if (argv[1]) {
7017
7c2eb015a6d7 Fixed bug 1759 - Updates to testjoystick.c
Sam Lantinga <slouken@libsdl.org>
parents: 6771
diff changeset
   247
#endif
6731
572559876818 Cleaned out tabstops, other minor style nitpicks.
Ryan C. Gordon <icculus@icculus.org>
parents: 6730
diff changeset
   248
        SDL_bool reportederror = SDL_FALSE;
6730
36158e4061f4 Allow testjoystick to quit, instead of looping on hotplug events forever.
Ryan C. Gordon <icculus@icculus.org>
parents: 6690
diff changeset
   249
        SDL_bool keepGoing = SDL_TRUE;
6731
572559876818 Cleaned out tabstops, other minor style nitpicks.
Ryan C. Gordon <icculus@icculus.org>
parents: 6730
diff changeset
   250
        SDL_Event event;
7017
7c2eb015a6d7 Fixed bug 1759 - Updates to testjoystick.c
Sam Lantinga <slouken@libsdl.org>
parents: 6771
diff changeset
   251
#ifdef ANDROID
7c2eb015a6d7 Fixed bug 1759 - Updates to testjoystick.c
Sam Lantinga <slouken@libsdl.org>
parents: 6771
diff changeset
   252
        joystick = SDL_JoystickOpen(0);
7c2eb015a6d7 Fixed bug 1759 - Updates to testjoystick.c
Sam Lantinga <slouken@libsdl.org>
parents: 6771
diff changeset
   253
#else
6731
572559876818 Cleaned out tabstops, other minor style nitpicks.
Ryan C. Gordon <icculus@icculus.org>
parents: 6730
diff changeset
   254
        joystick = SDL_JoystickOpen(atoi(argv[1]));
7017
7c2eb015a6d7 Fixed bug 1759 - Updates to testjoystick.c
Sam Lantinga <slouken@libsdl.org>
parents: 6771
diff changeset
   255
#endif
6731
572559876818 Cleaned out tabstops, other minor style nitpicks.
Ryan C. Gordon <icculus@icculus.org>
parents: 6730
diff changeset
   256
        while ( keepGoing ) {
572559876818 Cleaned out tabstops, other minor style nitpicks.
Ryan C. Gordon <icculus@icculus.org>
parents: 6730
diff changeset
   257
            if (joystick == NULL) {
572559876818 Cleaned out tabstops, other minor style nitpicks.
Ryan C. Gordon <icculus@icculus.org>
parents: 6730
diff changeset
   258
                if ( !reportederror ) {
572559876818 Cleaned out tabstops, other minor style nitpicks.
Ryan C. Gordon <icculus@icculus.org>
parents: 6730
diff changeset
   259
                    printf("Couldn't open joystick %d: %s\n", atoi(argv[1]), SDL_GetError());
6732
b4201930fb9f Don't hang testjoystick if we failed to open the device in the first place.
Ryan C. Gordon <icculus@icculus.org>
parents: 6731
diff changeset
   260
                    keepGoing = SDL_FALSE;
6731
572559876818 Cleaned out tabstops, other minor style nitpicks.
Ryan C. Gordon <icculus@icculus.org>
parents: 6730
diff changeset
   261
                    reportederror = SDL_TRUE;
572559876818 Cleaned out tabstops, other minor style nitpicks.
Ryan C. Gordon <icculus@icculus.org>
parents: 6730
diff changeset
   262
                }
572559876818 Cleaned out tabstops, other minor style nitpicks.
Ryan C. Gordon <icculus@icculus.org>
parents: 6730
diff changeset
   263
            } else {
572559876818 Cleaned out tabstops, other minor style nitpicks.
Ryan C. Gordon <icculus@icculus.org>
parents: 6730
diff changeset
   264
                reportederror = SDL_FALSE;
572559876818 Cleaned out tabstops, other minor style nitpicks.
Ryan C. Gordon <icculus@icculus.org>
parents: 6730
diff changeset
   265
                keepGoing = WatchJoystick(joystick);
572559876818 Cleaned out tabstops, other minor style nitpicks.
Ryan C. Gordon <icculus@icculus.org>
parents: 6730
diff changeset
   266
                SDL_JoystickClose(joystick);
572559876818 Cleaned out tabstops, other minor style nitpicks.
Ryan C. Gordon <icculus@icculus.org>
parents: 6730
diff changeset
   267
            }
572559876818 Cleaned out tabstops, other minor style nitpicks.
Ryan C. Gordon <icculus@icculus.org>
parents: 6730
diff changeset
   268
6736
03dd4a1d06e9 You can get window events after shutting down the window, so don't abort the program if that happens.
Sam Lantinga <slouken@libsdl.org>
parents: 6733
diff changeset
   269
            joystick = NULL;
6730
36158e4061f4 Allow testjoystick to quit, instead of looping on hotplug events forever.
Ryan C. Gordon <icculus@icculus.org>
parents: 6690
diff changeset
   270
            if (keepGoing) {
6736
03dd4a1d06e9 You can get window events after shutting down the window, so don't abort the program if that happens.
Sam Lantinga <slouken@libsdl.org>
parents: 6733
diff changeset
   271
                printf("Waiting for attach\n");
03dd4a1d06e9 You can get window events after shutting down the window, so don't abort the program if that happens.
Sam Lantinga <slouken@libsdl.org>
parents: 6733
diff changeset
   272
            }
03dd4a1d06e9 You can get window events after shutting down the window, so don't abort the program if that happens.
Sam Lantinga <slouken@libsdl.org>
parents: 6733
diff changeset
   273
            while (keepGoing) {
03dd4a1d06e9 You can get window events after shutting down the window, so don't abort the program if that happens.
Sam Lantinga <slouken@libsdl.org>
parents: 6733
diff changeset
   274
                SDL_WaitEvent(&event);
7017
7c2eb015a6d7 Fixed bug 1759 - Updates to testjoystick.c
Sam Lantinga <slouken@libsdl.org>
parents: 6771
diff changeset
   275
                if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
7c2eb015a6d7 Fixed bug 1759 - Updates to testjoystick.c
Sam Lantinga <slouken@libsdl.org>
parents: 6771
diff changeset
   276
                    || (event.type == SDL_MOUSEBUTTONDOWN)) {
6736
03dd4a1d06e9 You can get window events after shutting down the window, so don't abort the program if that happens.
Sam Lantinga <slouken@libsdl.org>
parents: 6733
diff changeset
   277
                    keepGoing = SDL_FALSE;
03dd4a1d06e9 You can get window events after shutting down the window, so don't abort the program if that happens.
Sam Lantinga <slouken@libsdl.org>
parents: 6733
diff changeset
   278
                } else if (event.type == SDL_JOYDEVICEADDED) {
6731
572559876818 Cleaned out tabstops, other minor style nitpicks.
Ryan C. Gordon <icculus@icculus.org>
parents: 6730
diff changeset
   279
                    joystick = SDL_JoystickOpen(atoi(argv[1]));
6736
03dd4a1d06e9 You can get window events after shutting down the window, so don't abort the program if that happens.
Sam Lantinga <slouken@libsdl.org>
parents: 6733
diff changeset
   280
                    break;
6731
572559876818 Cleaned out tabstops, other minor style nitpicks.
Ryan C. Gordon <icculus@icculus.org>
parents: 6730
diff changeset
   281
                }
6730
36158e4061f4 Allow testjoystick to quit, instead of looping on hotplug events forever.
Ryan C. Gordon <icculus@icculus.org>
parents: 6690
diff changeset
   282
            }
6731
572559876818 Cleaned out tabstops, other minor style nitpicks.
Ryan C. Gordon <icculus@icculus.org>
parents: 6730
diff changeset
   283
        }
572559876818 Cleaned out tabstops, other minor style nitpicks.
Ryan C. Gordon <icculus@icculus.org>
parents: 6730
diff changeset
   284
    }
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6586
diff changeset
   285
    SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   286
7017
7c2eb015a6d7 Fixed bug 1759 - Updates to testjoystick.c
Sam Lantinga <slouken@libsdl.org>
parents: 6771
diff changeset
   287
#ifdef ANDROID
7c2eb015a6d7 Fixed bug 1759 - Updates to testjoystick.c
Sam Lantinga <slouken@libsdl.org>
parents: 6771
diff changeset
   288
    exit(0);
7c2eb015a6d7 Fixed bug 1759 - Updates to testjoystick.c
Sam Lantinga <slouken@libsdl.org>
parents: 6771
diff changeset
   289
#else
6730
36158e4061f4 Allow testjoystick to quit, instead of looping on hotplug events forever.
Ryan C. Gordon <icculus@icculus.org>
parents: 6690
diff changeset
   290
    return 0;
7017
7c2eb015a6d7 Fixed bug 1759 - Updates to testjoystick.c
Sam Lantinga <slouken@libsdl.org>
parents: 6771
diff changeset
   291
#endif
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   292
}
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: 6747
diff changeset
   293
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: 6747
diff changeset
   294
#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: 6747
diff changeset
   295
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: 6747
diff changeset
   296
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: 6747
diff changeset
   297
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: 6747
diff changeset
   298
{
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: 6747
diff changeset
   299
    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: 6747
diff changeset
   300
    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: 6747
diff changeset
   301
}
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: 6747
diff changeset
   302
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: 6747
diff changeset
   303
#endif