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