test/testhotplug.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sun, 18 May 2014 21:11:30 +0200
changeset 8778 d67a044fbec6
parent 8776 29aac8b813d9
child 9356 e87d6e1e812a
permissions -rw-r--r--
Changed C++ style comments.
urkle@8164
     1
/*
icculus@8180
     2
  Copyright (C) 1997-2014 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@8188
    22
#if !defined SDL_JOYSTICK_DISABLED && !defined SDL_HAPTIC_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@8188
    31
    int i;
urkle@8188
    32
    SDL_bool enable_haptic = SDL_TRUE;
urkle@8188
    33
    Uint32 init_subsystems = SDL_INIT_VIDEO | SDL_INIT_JOYSTICK;
urkle@8188
    34
    
urkle@8188
    35
    for (i = 1; i < argc; ++i) {
urkle@8188
    36
        if (SDL_strcasecmp(argv[i], "--nohaptic") == 0) {
urkle@8188
    37
            enable_haptic = SDL_FALSE;
urkle@8188
    38
        }
urkle@8188
    39
    }
urkle@8164
    40
urkle@8188
    41
    if(enable_haptic) {
urkle@8188
    42
        init_subsystems |= SDL_INIT_HAPTIC;
urkle@8188
    43
    }
urkle@8188
    44
    
urkle@8164
    45
    /* Enable standard application logging */
urkle@8164
    46
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);	
urkle@8164
    47
urkle@8164
    48
    SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
urkle@8164
    49
urkle@8164
    50
    /* Initialize SDL (Note: video is required to start event loop) */
urkle@8188
    51
    if (SDL_Init(init_subsystems) < 0) {
urkle@8164
    52
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
urkle@8164
    53
        exit(1);
urkle@8164
    54
    }
urkle@8164
    55
philipp@8776
    56
    /*
urkle@8164
    57
    //SDL_CreateWindow("Dummy", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 128, 128, 0);
philipp@8776
    58
    */
urkle@8164
    59
urkle@8164
    60
    SDL_Log("There are %d joysticks at startup\n", SDL_NumJoysticks());
urkle@8188
    61
    if (enable_haptic)
urkle@8188
    62
        SDL_Log("There are %d haptic devices at startup\n", SDL_NumHaptics());
urkle@8164
    63
urkle@8164
    64
    while(keepGoing)
urkle@8164
    65
    {
urkle@8164
    66
        SDL_Event event;
urkle@8164
    67
        while(SDL_PollEvent(&event))
urkle@8164
    68
        {
urkle@8164
    69
            switch(event.type)
urkle@8164
    70
            {
urkle@8164
    71
                case SDL_QUIT:
urkle@8164
    72
                    keepGoing = SDL_FALSE;
urkle@8164
    73
                    break;
urkle@8164
    74
                case SDL_JOYDEVICEADDED:
urkle@8164
    75
                    if (joystick != NULL)
urkle@8164
    76
                    {
urkle@8164
    77
                        SDL_Log("Only one joystick supported by this test\n");
urkle@8164
    78
                    }
urkle@8164
    79
                    else
urkle@8164
    80
                    {
urkle@8164
    81
                        joystick = SDL_JoystickOpen(event.jdevice.which);
urkle@8164
    82
                        instance = SDL_JoystickInstanceID(joystick);
urkle@8164
    83
                        SDL_Log("Joy Added  : %d : %s\n", event.jdevice.which, SDL_JoystickName(joystick));
urkle@8188
    84
                        if (enable_haptic)
urkle@8164
    85
                        {
urkle@8188
    86
                            if (SDL_JoystickIsHaptic(joystick))
urkle@8164
    87
                            {
urkle@8188
    88
                                haptic = SDL_HapticOpenFromJoystick(joystick);
urkle@8188
    89
                                if (haptic)
urkle@8164
    90
                                {
urkle@8188
    91
                                    SDL_Log("Joy Haptic Opened\n");
urkle@8188
    92
                                    if (SDL_HapticRumbleInit( haptic ) != 0)
urkle@8188
    93
                                    {
urkle@8188
    94
                                        SDL_Log("Could not init Rumble!: %s\n", SDL_GetError());
urkle@8188
    95
                                        SDL_HapticClose(haptic);
urkle@8188
    96
                                        haptic = NULL;
urkle@8188
    97
                                    }
urkle@8188
    98
                                } else {
urkle@8188
    99
                                    SDL_Log("Joy haptic open FAILED!: %s\n", SDL_GetError());
urkle@8164
   100
                                }
urkle@8164
   101
                            }
urkle@8188
   102
                            else
urkle@8188
   103
                            {
urkle@8188
   104
                                SDL_Log("No haptic found\n");
urkle@8188
   105
                            }
urkle@8165
   106
                        }
urkle@8164
   107
                    }
urkle@8164
   108
                    break;
urkle@8164
   109
                case SDL_JOYDEVICEREMOVED:
urkle@8164
   110
                    if (instance == event.jdevice.which)
urkle@8164
   111
                    {
urkle@8164
   112
                        SDL_Log("Joy Removed: %d\n", event.jdevice.which);
urkle@8164
   113
                        instance = -1;
urkle@8188
   114
                        if(enable_haptic && haptic)
urkle@8164
   115
                        {
urkle@8164
   116
                            SDL_HapticClose(haptic);
urkle@8165
   117
                            haptic = NULL;
urkle@8164
   118
                        }
urkle@8164
   119
                        SDL_JoystickClose(joystick);
urkle@8164
   120
                        joystick = NULL;
urkle@8164
   121
                    } else {
urkle@8164
   122
                        SDL_Log("Unknown joystick diconnected\n");
urkle@8164
   123
                    }
urkle@8164
   124
                    break;
urkle@8164
   125
                case SDL_JOYAXISMOTION:
philipp@8778
   126
/*
urkle@8164
   127
//                    SDL_Log("Axis Move: %d\n", event.jaxis.axis);
philipp@8778
   128
*/
urkle@8188
   129
                    if (enable_haptic)
urkle@8188
   130
                        SDL_HapticRumblePlay(haptic, 0.25, 250);
urkle@8164
   131
                    break;
urkle@8164
   132
                case SDL_JOYBUTTONDOWN:
urkle@8164
   133
                    SDL_Log("Button Press: %d\n", event.jbutton.button);
urkle@8188
   134
                    if(enable_haptic && haptic)
urkle@8164
   135
                    {
urkle@8188
   136
                        SDL_HapticRumblePlay(haptic, 0.25, 250);
urkle@8164
   137
                    }
urkle@8169
   138
					if (event.jbutton.button == 0) {
urkle@8169
   139
						SDL_Log("Exiting due to button press of button 0\n");
urkle@8169
   140
						keepGoing = SDL_FALSE;
urkle@8169
   141
					}
urkle@8164
   142
                    break;
urkle@8164
   143
                case SDL_JOYBUTTONUP:
urkle@8164
   144
                    SDL_Log("Button Release: %d\n", event.jbutton.button);
urkle@8164
   145
                    break;
urkle@8164
   146
            }
urkle@8164
   147
        }
urkle@8164
   148
    }
urkle@8164
   149
urkle@8188
   150
    SDL_Quit();
icculus@8181
   151
icculus@8181
   152
    return 0;
urkle@8164
   153
}
urkle@8164
   154
#else
urkle@8164
   155
urkle@8164
   156
int
urkle@8164
   157
main(int argc, char *argv[])
urkle@8164
   158
{
urkle@8188
   159
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick and haptic support.\n");
icculus@8181
   160
    return 1;
urkle@8164
   161
}
urkle@8164
   162
urkle@8164
   163
#endif