cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

testhotplug.c (5523B)


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