test/testhotplug.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Wed, 18 Feb 2015 21:31:21 +0100
changeset 9356 e87d6e1e812a
parent 8778 d67a044fbec6
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Removed redundant include statements from test programs.

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