add simple test showing off hotplugging of joysticks w/ haptic.
authorEdward Rudd <urkle@outoforder.cc>
Mon, 03 Feb 2014 22:52:18 -0500
changeset 8164478fbe1f032f
parent 8163 52f8629bdef2
child 8165 2b8ba3d53a67
add simple test showing off hotplugging of joysticks w/ haptic.
test/Makefile.in
test/testhotplug.c
     1.1 --- a/test/Makefile.in	Mon Feb 03 14:47:48 2014 -0500
     1.2 +++ b/test/Makefile.in	Mon Feb 03 22:52:18 2014 -0500
     1.3 @@ -24,6 +24,7 @@
     1.4  	testgles2$(EXE) \
     1.5  	testhaptic$(EXE) \
     1.6  	testrumble$(EXE) \
     1.7 +	testhotplug$(EXE) \
     1.8  	testthread$(EXE) \
     1.9  	testiconv$(EXE) \
    1.10  	testime$(EXE) \
    1.11 @@ -139,6 +140,9 @@
    1.12  testhaptic$(EXE): $(srcdir)/testhaptic.c
    1.13  	$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
    1.14  
    1.15 +testhotplug$(EXE): $(srcdir)/testhotplug.c
    1.16 +	$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
    1.17 +
    1.18  testrumble$(EXE): $(srcdir)/testrumble.c
    1.19  	$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
    1.20  
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/testhotplug.c	Mon Feb 03 22:52:18 2014 -0500
     2.3 @@ -0,0 +1,130 @@
     2.4 +/*
     2.5 +  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     2.6 +
     2.7 +  This software is provided 'as-is', without any express or implied
     2.8 +  warranty.  In no event will the authors be held liable for any damages
     2.9 +  arising from the use of this software.
    2.10 +
    2.11 +  Permission is granted to anyone to use this software for any purpose,
    2.12 +  including commercial applications, and to alter it and redistribute it
    2.13 +  freely.
    2.14 +*/
    2.15 +
    2.16 +/* Simple program to test the SDL joystick hotplugging */
    2.17 +
    2.18 +#include <stdio.h>
    2.19 +#include <stdlib.h>
    2.20 +#include <string.h>
    2.21 +
    2.22 +#include "SDL.h"
    2.23 +#include "SDL_haptic.h"
    2.24 +
    2.25 +#ifndef SDL_JOYSTICK_DISABLED
    2.26 +
    2.27 +int
    2.28 +main(int argc, char *argv[])
    2.29 +{
    2.30 +    SDL_Joystick *joystick = NULL;
    2.31 +    SDL_Haptic *haptic = NULL;
    2.32 +    SDL_JoystickID instance = -1;
    2.33 +    SDL_bool keepGoing = SDL_TRUE;
    2.34 +
    2.35 +    /* Enable standard application logging */
    2.36 +    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);	
    2.37 +
    2.38 +    SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
    2.39 +
    2.40 +    /* Initialize SDL (Note: video is required to start event loop) */
    2.41 +    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC) < 0) {
    2.42 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
    2.43 +        exit(1);
    2.44 +    }
    2.45 +
    2.46 +    //SDL_CreateWindow("Dummy", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 128, 128, 0);
    2.47 +
    2.48 +    SDL_Log("There are %d joysticks at startup\n", SDL_NumJoysticks());
    2.49 +    SDL_Log("There are %d haptic devices at startup\n", SDL_NumHaptics());
    2.50 +
    2.51 +    while(keepGoing)
    2.52 +    {
    2.53 +        SDL_Event event;
    2.54 +        while(SDL_PollEvent(&event))
    2.55 +        {
    2.56 +            switch(event.type)
    2.57 +            {
    2.58 +                case SDL_QUIT:
    2.59 +                    keepGoing = SDL_FALSE;
    2.60 +                    break;
    2.61 +                case SDL_JOYDEVICEADDED:
    2.62 +                    if (joystick != NULL)
    2.63 +                    {
    2.64 +                        SDL_Log("Only one joystick supported by this test\n");
    2.65 +                    }
    2.66 +                    else
    2.67 +                    {
    2.68 +                        joystick = SDL_JoystickOpen(event.jdevice.which);
    2.69 +                        instance = SDL_JoystickInstanceID(joystick);
    2.70 +                        SDL_Log("Joy Added  : %d : %s\n", event.jdevice.which, SDL_JoystickName(joystick));
    2.71 +                        if (SDL_JoystickIsHaptic(joystick))
    2.72 +                        {
    2.73 +                            haptic = SDL_HapticOpenFromJoystick(joystick);
    2.74 +                            if (haptic)
    2.75 +                            {
    2.76 +                                SDL_Log("Joy Haptic Opened\n");
    2.77 +                                if (SDL_HapticRumbleInit( haptic ) != 0)
    2.78 +                                {
    2.79 +                                    SDL_Log("Could not init Rumble!");
    2.80 +                                    SDL_HapticClose(haptic);
    2.81 +                                    haptic = NULL;
    2.82 +                                }
    2.83 +                            } else {
    2.84 +                                SDL_Log("Joy haptic FAILED!\n");
    2.85 +                            }
    2.86 +                        }
    2.87 +                    }
    2.88 +                    break;
    2.89 +                case SDL_JOYDEVICEREMOVED:
    2.90 +                    if (instance == event.jdevice.which)
    2.91 +                    {
    2.92 +                        SDL_Log("Joy Removed: %d\n", event.jdevice.which);
    2.93 +                        instance = -1;
    2.94 +                        if(haptic)
    2.95 +                        {
    2.96 +                            SDL_HapticClose(haptic);
    2.97 +                        }
    2.98 +                        SDL_JoystickClose(joystick);
    2.99 +                        joystick = NULL;
   2.100 +                    } else {
   2.101 +                        SDL_Log("Unknown joystick diconnected\n");
   2.102 +                    }
   2.103 +                    break;
   2.104 +                case SDL_JOYAXISMOTION:
   2.105 +//                    SDL_Log("Axis Move: %d\n", event.jaxis.axis);
   2.106 +                    SDL_HapticRumblePlay(haptic, 0.2, 250);
   2.107 +                    break;
   2.108 +                case SDL_JOYBUTTONDOWN:
   2.109 +                    SDL_Log("Button Press: %d\n", event.jbutton.button);
   2.110 +                    if(haptic)
   2.111 +                    {
   2.112 +                        SDL_HapticRumblePlay(haptic, 0.2, 250);
   2.113 +                    }
   2.114 +                    break;
   2.115 +                case SDL_JOYBUTTONUP:
   2.116 +                    SDL_Log("Button Release: %d\n", event.jbutton.button);
   2.117 +                    break;
   2.118 +            }
   2.119 +        }
   2.120 +    }
   2.121 +
   2.122 +    SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
   2.123 +}
   2.124 +#else
   2.125 +
   2.126 +int
   2.127 +main(int argc, char *argv[])
   2.128 +{
   2.129 +    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
   2.130 +    exit(1);
   2.131 +}
   2.132 +
   2.133 +#endif