test/testhotplug.c
author Edward Rudd <urkle@outoforder.cc>
Tue, 04 Feb 2014 15:40:51 -0500
changeset 8169 663ba61243ff
parent 8166 d1be9ce49463
child 8180 16afd5b764b3
permissions -rw-r--r--
allow safely quitting the hotplug test by pressing button 0 on the joystick
urkle@8164
     1
/*
urkle@8164
     2
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
urkle@8164
     3
urkle@8164
     4
  This software is provided 'as-is', without any express or implied
urkle@8164
     5
  warranty.  In no event will the authors be held liable for any damages
urkle@8164
     6
  arising from the use of this software.
urkle@8164
     7
urkle@8164
     8
  Permission is granted to anyone to use this software for any purpose,
urkle@8164
     9
  including commercial applications, and to alter it and redistribute it
urkle@8164
    10
  freely.
urkle@8164
    11
*/
urkle@8164
    12
urkle@8164
    13
/* Simple program to test the SDL joystick hotplugging */
urkle@8164
    14
urkle@8164
    15
#include <stdio.h>
urkle@8164
    16
#include <stdlib.h>
urkle@8164
    17
#include <string.h>
urkle@8164
    18
urkle@8164
    19
#include "SDL.h"
urkle@8164
    20
#include "SDL_haptic.h"
urkle@8164
    21
urkle@8164
    22
#ifndef SDL_JOYSTICK_DISABLED
urkle@8164
    23
urkle@8164
    24
int
urkle@8164
    25
main(int argc, char *argv[])
urkle@8164
    26
{
urkle@8164
    27
    SDL_Joystick *joystick = NULL;
urkle@8164
    28
    SDL_Haptic *haptic = NULL;
urkle@8164
    29
    SDL_JoystickID instance = -1;
urkle@8164
    30
    SDL_bool keepGoing = SDL_TRUE;
urkle@8164
    31
urkle@8164
    32
    /* Enable standard application logging */
urkle@8164
    33
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);	
urkle@8164
    34
urkle@8164
    35
    SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
urkle@8164
    36
urkle@8164
    37
    /* Initialize SDL (Note: video is required to start event loop) */
urkle@8164
    38
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC) < 0) {
urkle@8164
    39
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
urkle@8164
    40
        exit(1);
urkle@8164
    41
    }
urkle@8164
    42
urkle@8164
    43
    //SDL_CreateWindow("Dummy", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 128, 128, 0);
urkle@8164
    44
urkle@8164
    45
    SDL_Log("There are %d joysticks at startup\n", SDL_NumJoysticks());
urkle@8164
    46
    SDL_Log("There are %d haptic devices at startup\n", SDL_NumHaptics());
urkle@8164
    47
urkle@8164
    48
    while(keepGoing)
urkle@8164
    49
    {
urkle@8164
    50
        SDL_Event event;
urkle@8164
    51
        while(SDL_PollEvent(&event))
urkle@8164
    52
        {
urkle@8164
    53
            switch(event.type)
urkle@8164
    54
            {
urkle@8164
    55
                case SDL_QUIT:
urkle@8164
    56
                    keepGoing = SDL_FALSE;
urkle@8164
    57
                    break;
urkle@8164
    58
                case SDL_JOYDEVICEADDED:
urkle@8164
    59
                    if (joystick != NULL)
urkle@8164
    60
                    {
urkle@8164
    61
                        SDL_Log("Only one joystick supported by this test\n");
urkle@8164
    62
                    }
urkle@8164
    63
                    else
urkle@8164
    64
                    {
urkle@8164
    65
                        joystick = SDL_JoystickOpen(event.jdevice.which);
urkle@8164
    66
                        instance = SDL_JoystickInstanceID(joystick);
urkle@8164
    67
                        SDL_Log("Joy Added  : %d : %s\n", event.jdevice.which, SDL_JoystickName(joystick));
urkle@8164
    68
                        if (SDL_JoystickIsHaptic(joystick))
urkle@8164
    69
                        {
urkle@8164
    70
                            haptic = SDL_HapticOpenFromJoystick(joystick);
urkle@8164
    71
                            if (haptic)
urkle@8164
    72
                            {
urkle@8164
    73
                                SDL_Log("Joy Haptic Opened\n");
urkle@8164
    74
                                if (SDL_HapticRumbleInit( haptic ) != 0)
urkle@8164
    75
                                {
urkle@8166
    76
                                    SDL_Log("Could not init Rumble!: %s\n", SDL_GetError());
urkle@8164
    77
                                    SDL_HapticClose(haptic);
urkle@8164
    78
                                    haptic = NULL;
urkle@8164
    79
                                }
urkle@8164
    80
                            } else {
urkle@8166
    81
                                SDL_Log("Joy haptic open FAILED!: %s\n", SDL_GetError());
urkle@8164
    82
                            }
urkle@8164
    83
                        }
urkle@8165
    84
                        else
urkle@8165
    85
                        {
urkle@8165
    86
                            SDL_Log("No haptic found\n");
urkle@8165
    87
                        }
urkle@8164
    88
                    }
urkle@8164
    89
                    break;
urkle@8164
    90
                case SDL_JOYDEVICEREMOVED:
urkle@8164
    91
                    if (instance == event.jdevice.which)
urkle@8164
    92
                    {
urkle@8164
    93
                        SDL_Log("Joy Removed: %d\n", event.jdevice.which);
urkle@8164
    94
                        instance = -1;
urkle@8164
    95
                        if(haptic)
urkle@8164
    96
                        {
urkle@8164
    97
                            SDL_HapticClose(haptic);
urkle@8165
    98
                            haptic = NULL;
urkle@8164
    99
                        }
urkle@8164
   100
                        SDL_JoystickClose(joystick);
urkle@8164
   101
                        joystick = NULL;
urkle@8164
   102
                    } else {
urkle@8164
   103
                        SDL_Log("Unknown joystick diconnected\n");
urkle@8164
   104
                    }
urkle@8164
   105
                    break;
urkle@8164
   106
                case SDL_JOYAXISMOTION:
urkle@8164
   107
//                    SDL_Log("Axis Move: %d\n", event.jaxis.axis);
urkle@8164
   108
                    SDL_HapticRumblePlay(haptic, 0.2, 250);
urkle@8164
   109
                    break;
urkle@8164
   110
                case SDL_JOYBUTTONDOWN:
urkle@8164
   111
                    SDL_Log("Button Press: %d\n", event.jbutton.button);
urkle@8164
   112
                    if(haptic)
urkle@8164
   113
                    {
urkle@8164
   114
                        SDL_HapticRumblePlay(haptic, 0.2, 250);
urkle@8164
   115
                    }
urkle@8169
   116
					if (event.jbutton.button == 0) {
urkle@8169
   117
						SDL_Log("Exiting due to button press of button 0\n");
urkle@8169
   118
						keepGoing = SDL_FALSE;
urkle@8169
   119
					}
urkle@8164
   120
                    break;
urkle@8164
   121
                case SDL_JOYBUTTONUP:
urkle@8164
   122
                    SDL_Log("Button Release: %d\n", event.jbutton.button);
urkle@8164
   123
                    break;
urkle@8164
   124
            }
urkle@8164
   125
        }
urkle@8164
   126
    }
urkle@8164
   127
urkle@8164
   128
    SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
urkle@8164
   129
}
urkle@8164
   130
#else
urkle@8164
   131
urkle@8164
   132
int
urkle@8164
   133
main(int argc, char *argv[])
urkle@8164
   134
{
urkle@8164
   135
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
urkle@8164
   136
    exit(1);
urkle@8164
   137
}
urkle@8164
   138
urkle@8164
   139
#endif