test/testhotplug.c
changeset 8164 478fbe1f032f
child 8165 2b8ba3d53a67
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testhotplug.c	Mon Feb 03 22:52:18 2014 -0500
     1.3 @@ -0,0 +1,130 @@
     1.4 +/*
     1.5 +  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     1.6 +
     1.7 +  This software is provided 'as-is', without any express or implied
     1.8 +  warranty.  In no event will the authors be held liable for any damages
     1.9 +  arising from the use of this software.
    1.10 +
    1.11 +  Permission is granted to anyone to use this software for any purpose,
    1.12 +  including commercial applications, and to alter it and redistribute it
    1.13 +  freely.
    1.14 +*/
    1.15 +
    1.16 +/* Simple program to test the SDL joystick hotplugging */
    1.17 +
    1.18 +#include <stdio.h>
    1.19 +#include <stdlib.h>
    1.20 +#include <string.h>
    1.21 +
    1.22 +#include "SDL.h"
    1.23 +#include "SDL_haptic.h"
    1.24 +
    1.25 +#ifndef SDL_JOYSTICK_DISABLED
    1.26 +
    1.27 +int
    1.28 +main(int argc, char *argv[])
    1.29 +{
    1.30 +    SDL_Joystick *joystick = NULL;
    1.31 +    SDL_Haptic *haptic = NULL;
    1.32 +    SDL_JoystickID instance = -1;
    1.33 +    SDL_bool keepGoing = SDL_TRUE;
    1.34 +
    1.35 +    /* Enable standard application logging */
    1.36 +    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);	
    1.37 +
    1.38 +    SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
    1.39 +
    1.40 +    /* Initialize SDL (Note: video is required to start event loop) */
    1.41 +    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC) < 0) {
    1.42 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
    1.43 +        exit(1);
    1.44 +    }
    1.45 +
    1.46 +    //SDL_CreateWindow("Dummy", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 128, 128, 0);
    1.47 +
    1.48 +    SDL_Log("There are %d joysticks at startup\n", SDL_NumJoysticks());
    1.49 +    SDL_Log("There are %d haptic devices at startup\n", SDL_NumHaptics());
    1.50 +
    1.51 +    while(keepGoing)
    1.52 +    {
    1.53 +        SDL_Event event;
    1.54 +        while(SDL_PollEvent(&event))
    1.55 +        {
    1.56 +            switch(event.type)
    1.57 +            {
    1.58 +                case SDL_QUIT:
    1.59 +                    keepGoing = SDL_FALSE;
    1.60 +                    break;
    1.61 +                case SDL_JOYDEVICEADDED:
    1.62 +                    if (joystick != NULL)
    1.63 +                    {
    1.64 +                        SDL_Log("Only one joystick supported by this test\n");
    1.65 +                    }
    1.66 +                    else
    1.67 +                    {
    1.68 +                        joystick = SDL_JoystickOpen(event.jdevice.which);
    1.69 +                        instance = SDL_JoystickInstanceID(joystick);
    1.70 +                        SDL_Log("Joy Added  : %d : %s\n", event.jdevice.which, SDL_JoystickName(joystick));
    1.71 +                        if (SDL_JoystickIsHaptic(joystick))
    1.72 +                        {
    1.73 +                            haptic = SDL_HapticOpenFromJoystick(joystick);
    1.74 +                            if (haptic)
    1.75 +                            {
    1.76 +                                SDL_Log("Joy Haptic Opened\n");
    1.77 +                                if (SDL_HapticRumbleInit( haptic ) != 0)
    1.78 +                                {
    1.79 +                                    SDL_Log("Could not init Rumble!");
    1.80 +                                    SDL_HapticClose(haptic);
    1.81 +                                    haptic = NULL;
    1.82 +                                }
    1.83 +                            } else {
    1.84 +                                SDL_Log("Joy haptic FAILED!\n");
    1.85 +                            }
    1.86 +                        }
    1.87 +                    }
    1.88 +                    break;
    1.89 +                case SDL_JOYDEVICEREMOVED:
    1.90 +                    if (instance == event.jdevice.which)
    1.91 +                    {
    1.92 +                        SDL_Log("Joy Removed: %d\n", event.jdevice.which);
    1.93 +                        instance = -1;
    1.94 +                        if(haptic)
    1.95 +                        {
    1.96 +                            SDL_HapticClose(haptic);
    1.97 +                        }
    1.98 +                        SDL_JoystickClose(joystick);
    1.99 +                        joystick = NULL;
   1.100 +                    } else {
   1.101 +                        SDL_Log("Unknown joystick diconnected\n");
   1.102 +                    }
   1.103 +                    break;
   1.104 +                case SDL_JOYAXISMOTION:
   1.105 +//                    SDL_Log("Axis Move: %d\n", event.jaxis.axis);
   1.106 +                    SDL_HapticRumblePlay(haptic, 0.2, 250);
   1.107 +                    break;
   1.108 +                case SDL_JOYBUTTONDOWN:
   1.109 +                    SDL_Log("Button Press: %d\n", event.jbutton.button);
   1.110 +                    if(haptic)
   1.111 +                    {
   1.112 +                        SDL_HapticRumblePlay(haptic, 0.2, 250);
   1.113 +                    }
   1.114 +                    break;
   1.115 +                case SDL_JOYBUTTONUP:
   1.116 +                    SDL_Log("Button Release: %d\n", event.jbutton.button);
   1.117 +                    break;
   1.118 +            }
   1.119 +        }
   1.120 +    }
   1.121 +
   1.122 +    SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
   1.123 +}
   1.124 +#else
   1.125 +
   1.126 +int
   1.127 +main(int argc, char *argv[])
   1.128 +{
   1.129 +    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
   1.130 +    exit(1);
   1.131 +}
   1.132 +
   1.133 +#endif