void es_joystick_index(sdl_data *sd, int len,char *buff) { int sendlen; char *bp, *start; SDL_Joystick *joy; int index; bp = buff; POPGLPTR(joy, bp); bp = start = sdl_get_temp_buff(sd, 1); index = SDL_JoystickIndex(joy); put8(bp,index); sendlen = bp - start; sdl_send(sd, sendlen); }
// // I_CloseJoystick // void I_CloseJoystick() { extern int joyforward, joystrafe, joyturn, joylook; int ndx; #ifndef _XBOX // This is to avoid a bug in SDLx if(!I_GetJoystickCount() || !openedjoy) return; ndx = SDL_JoystickIndex(openedjoy); if(SDL_JoystickOpened(ndx)) SDL_JoystickClose(openedjoy); openedjoy = NULL; #endif // Reset joy position values. Wouldn't want to get stuck in a turn or something. -- Hyper_Eye joyforward = joystrafe = joyturn = joylook = 0; }
void Engine::Shutdown(void) { // this should not be called from inside Engine::Run() sndCore.StopMusic(); delete objmgr; delete physmgr; delete _layermgr; resMgr.pool.Cleanup(true); // force deletion of everything resMgr.DropUnused(); // at this point, all resources should have a refcount of 0, so this removes all. sndCore.Destroy(); // must be deleted after all sounds were dropped by the ResourceMgr if(_screen) SDL_FreeSurface(_screen); for(uint32 i = 0; i < s_joysticks.size(); ++i) if(s_joysticks[i] && SDL_JoystickOpened(SDL_JoystickIndex(s_joysticks[i]))) // this is maybe a bit overcomplicated, but safe at least SDL_JoystickClose(s_joysticks[i]); // be sure we did a clean shutdown resMgr.DbgCheckEmpty(); }
void main_2(void* arg) { // TODO: At the moment, we only support joystick support through polling. emscripten_run_script("window.addNewGamepad('Pad Thai', 4, 16)"); emscripten_run_script("window.addNewGamepad('Pad Kee Mao', 0, 4)"); // Check that the joysticks exist properly. assert(SDL_NumJoysticks() == 2); assert(!SDL_JoystickOpened(0)); assert(!SDL_JoystickOpened(1)); SDL_Joystick* pad1 = SDL_JoystickOpen(0); assert(SDL_JoystickOpened(0)); assert(SDL_JoystickIndex(pad1) == 0); assert(strncmp(SDL_JoystickName(0), "Pad Thai", 9) == 0); assert(strncmp(SDL_JoystickName(1), "Pad Kee Mao", 12) == 0); assert(SDL_JoystickNumAxes(pad1) == 4); assert(SDL_JoystickNumButtons(pad1) == 16); // Button events. emscripten_run_script("window.simulateGamepadButtonDown(0, 1)"); // We didn't tell SDL to automatically update this joystick's state. assertNoJoystickEvent(); SDL_JoystickUpdate(); assertJoystickEvent(0, SDL_JOYBUTTONDOWN, 1, SDL_PRESSED); assert(SDL_JoystickGetButton(pad1, 1) == 1); // Enable automatic updates. SDL_JoystickEventState(SDL_ENABLE); assert(SDL_JoystickEventState(SDL_QUERY) == SDL_ENABLE); emscripten_run_script("window.simulateGamepadButtonUp(0, 1)"); assertJoystickEvent(0, SDL_JOYBUTTONUP, 1, SDL_RELEASED); assert(SDL_JoystickGetButton(pad1, 1) == 0); // No button change: Should not result in a new event. emscripten_run_script("window.simulateGamepadButtonUp(0, 1)"); assertNoJoystickEvent(); // Joystick 1 is not opened; should not result in a new event. emscripten_run_script("window.simulateGamepadButtonDown(1, 1)"); assertNoJoystickEvent(); // Joystick wiggling emscripten_run_script("window.simulateAxisMotion(0, 0, 1)"); assertJoystickEvent(0, SDL_JOYAXISMOTION, 0, 32767); assert(SDL_JoystickGetAxis(pad1, 0) == 32767); emscripten_run_script("window.simulateAxisMotion(0, 0, 0)"); assertJoystickEvent(0, SDL_JOYAXISMOTION, 0, 0); assert(SDL_JoystickGetAxis(pad1, 0) == 0); emscripten_run_script("window.simulateAxisMotion(0, 1, -1)"); assertJoystickEvent(0, SDL_JOYAXISMOTION, 1, -32768); assert(SDL_JoystickGetAxis(pad1, 1) == -32768); emscripten_run_script("window.simulateAxisMotion(0, 1, -1)"); // No joystick change: Should not result in a new event. assertNoJoystickEvent(); // Joystick 1 is not opened; should not result in a new event. emscripten_run_script("window.simulateAxisMotion(1, 1, -1)"); assertNoJoystickEvent(); SDL_JoystickClose(pad1); assert(!SDL_JoystickOpened(0)); // Joystick 0 is closed; we should not process any new gamepad events from it. emscripten_run_script("window.simulateGamepadButtonDown(0, 1)"); assertNoJoystickEvent(); // End test. result = 2; printf("Test passed!\n"); REPORT_RESULT(); }
void WatchJoystick(SDL_Joystick *joystick) { SDL_Surface *screen; const char *name; int i, done; SDL_Event event; int x, y, draw; SDL_Rect axis_area[2]; /* Set a video mode to display joystick axis position */ screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, 0); if ( screen == NULL ) { fprintf(stderr, "Couldn't set video mode: %s\n",SDL_GetError()); return; } /* Print info about the joystick we are watching */ name = SDL_JoystickName(SDL_JoystickIndex(joystick)); printf("Watching joystick %d: (%s)\n", SDL_JoystickIndex(joystick), name ? name : "Unknown Joystick"); printf("Joystick has %d axes, %d hats, %d balls, and %d buttons\n", SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick), SDL_JoystickNumBalls(joystick),SDL_JoystickNumButtons(joystick)); /* Initialize drawing rectangles */ memset(axis_area, 0, (sizeof axis_area)); draw = 0; /* Loop, getting joystick events! */ done = 0; while ( ! done ) { while ( SDL_PollEvent(&event) ) { switch (event.type) { case SDL_JOYAXISMOTION: printf("Joystick %d axis %d value: %d\n", event.jaxis.which, event.jaxis.axis, event.jaxis.value); break; case SDL_JOYHATMOTION: printf("Joystick %d hat %d value:", event.jhat.which, event.jhat.hat); if ( event.jhat.value == SDL_HAT_CENTERED ) printf(" centered"); if ( event.jhat.value & SDL_HAT_UP ) printf(" up"); if ( event.jhat.value & SDL_HAT_RIGHT ) printf(" right"); if ( event.jhat.value & SDL_HAT_DOWN ) printf(" down"); if ( event.jhat.value & SDL_HAT_LEFT ) printf(" left"); printf("\n"); break; case SDL_JOYBALLMOTION: printf("Joystick %d ball %d delta: (%d,%d)\n", event.jball.which, event.jball.ball, event.jball.xrel, event.jball.yrel); break; case SDL_JOYBUTTONDOWN: printf("Joystick %d button %d down\n", event.jbutton.which, event.jbutton.button); break; case SDL_JOYBUTTONUP: printf("Joystick %d button %d up\n", event.jbutton.which, event.jbutton.button); break; case SDL_KEYDOWN: if ( event.key.keysym.sym != SDLK_ESCAPE ) { break; } /* Fall through to signal quit */ case SDL_QUIT: done = 1; break; default: break; } } /* Update visual joystick state */ for ( i=0; i<SDL_JoystickNumButtons(joystick); ++i ) { SDL_Rect area; area.x = i*34; area.y = SCREEN_HEIGHT-34; area.w = 32; area.h = 32; if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) { SDL_FillRect(screen, &area, 0xFFFF); } else { SDL_FillRect(screen, &area, 0x0000); } SDL_UpdateRects(screen, 1, &area); } /* Erase previous axes */ SDL_FillRect(screen, &axis_area[draw], 0x0000); /* Draw the X/Y axis */ draw = !draw; x = (((int)SDL_JoystickGetAxis(joystick, 0))+32768); x *= SCREEN_WIDTH; x /= 65535; if ( x < 0 ) { x = 0; } else if ( x > (SCREEN_WIDTH-16) ) { x = SCREEN_WIDTH-16; } y = (((int)SDL_JoystickGetAxis(joystick, 1))+32768); y *= SCREEN_HEIGHT; y /= 65535; if ( y < 0 ) { y = 0; } else if ( y > (SCREEN_HEIGHT-16) ) { y = SCREEN_HEIGHT-16; } axis_area[draw].x = (Sint16)x; axis_area[draw].y = (Sint16)y; axis_area[draw].w = 16; axis_area[draw].h = 16; SDL_FillRect(screen, &axis_area[draw], 0xFFFF); SDL_UpdateRects(screen, 2, axis_area); } }
static int l_joystick_index(lua_State *L) { Joystick *self = luaL_checkudata(L, 1, CLASS_NAME); lua_pushnumber(L, SDL_JoystickIndex(self->joystick)); return 1; }
int init_SDL(void) { joys[0]=0; joys[1]=0; joys[2]=0; joys[3]=0; if (SDL_Init(SDL_INIT_JOYSTICK) < 0) { fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); return(0); } sdlscreen = SDL_SetVideoMode(0,0, 16, SDL_SWSURFACE); //We handle up to four joysticks if(SDL_NumJoysticks()) { int joystickIndex; int configuratedJoysticks[4] = {-1,-1,-1,-1}; SDL_JoystickEventState(SDL_ENABLE); // Try to configure joysticks from config indexes for(int player = 0; player< 4; player++){ joystickIndex = joy_indexes[player]; if(joystickIndex >= 0){ if(SDL_NumJoysticks() > joystickIndex){ SDL_Joystick* joystick = SDL_JoystickOpen(joystickIndex); //Check for valid joystick, some keyboards //aren't SDL compatible if(joystick) { if (SDL_JoystickNumAxes(myjoy[joystickIndex]) > 6) { SDL_JoystickClose(myjoy[joystickIndex]); joystick=0; logoutput("Error detected invalid joystick/keyboard\n"); break; } } configuratedJoysticks[player] = joystickIndex; joys[player] = joystick; logoutput("Configured joystick %s at %d for player %d\n", SDL_JoystickName(joystickIndex), SDL_JoystickIndex(joystick), player+1); joyCount++; } } } // Finish configuration for(joystickIndex=0;joystickIndex<SDL_NumJoysticks();joystickIndex++) { // If already configured skip bool alreadyConfig = false; for(int player = 0; player< 4; player++){ logoutput("Checking if joystick at %d is configured for player %d : %d\n", joystickIndex,player ,configuratedJoysticks[player]); if(configuratedJoysticks[player] == joystickIndex){ alreadyConfig = true; break; } } if(alreadyConfig) continue; SDL_Joystick* joystick = SDL_JoystickOpen(joystickIndex); //Check for valid joystick, some keyboards //aren't SDL compatible if(joystick) { if (SDL_JoystickNumAxes(myjoy[joystickIndex]) > 6) { SDL_JoystickClose(myjoy[joystickIndex]); joystick=0; logoutput("Error detected invalid joystick/keyboard\n"); break; } // Set the first free joystick for(int player = 0; player< 4; player++){ if(configuratedJoysticks[player] == -1){ joy_indexes[player] = joystickIndex; joys[player] = joystick; configuratedJoysticks[player] = joystickIndex; logoutput("Default Configured joystick at %d for player %d\n", joystickIndex ,player+1); joyCount++; break; } } } } if(joys[0]) logoutput("Found %d joysticks\n",joyCount); } else joyCount=1; //sq frig number of players for keyboard //joyCount=2; SDL_EventState(SDL_ACTIVEEVENT,SDL_IGNORE); SDL_EventState(SDL_SYSWMEVENT,SDL_IGNORE); SDL_EventState(SDL_VIDEORESIZE,SDL_IGNORE); SDL_EventState(SDL_USEREVENT,SDL_IGNORE); SDL_ShowCursor(SDL_DISABLE); //Initialise dispmanx bcm_host_init(); //Clean exits, hopefully! atexit(exitfunc); return(1); }
int QJoystick::currentJoystick() { return SDL_JoystickIndex(m_joystick); }
/*read input events*/ SE_input_state *SE_input_poll(SE_input_joystick **joy){ int joyid; joyid = SDL_JoystickIndex(*joy); // internal_input_state[eventjoy.jaxis.which].buttons=0; while(SDL_PollEvent(&eventjoy)) { switch(eventjoy.type) { case SDL_JOYAXISMOTION: if( eventjoy.jaxis.axis == 0){ if(eventjoy.jaxis.value < -3200){ internal_input_state[eventjoy.jaxis.which].buttons&=~JOY_RIGHT; internal_input_state[eventjoy.jaxis.which].buttons|=JOY_LEFT; }else if(eventjoy.jaxis.value > 3200){ internal_input_state[eventjoy.jaxis.which].buttons&=~JOY_LEFT; internal_input_state[eventjoy.jaxis.which].buttons|=JOY_RIGHT; }else{ internal_input_state[eventjoy.jaxis.which].buttons&=~JOY_LEFT; internal_input_state[eventjoy.jaxis.which].buttons&=~JOY_RIGHT; } } if( eventjoy.jaxis.axis == 1){ if(eventjoy.jaxis.value < -3200){ internal_input_state[eventjoy.jaxis.which].buttons&=~JOY_DOWN; internal_input_state[eventjoy.jaxis.which].buttons|=JOY_UP; }else if(eventjoy.jaxis.value > 3200){ internal_input_state[eventjoy.jaxis.which].buttons&=~JOY_UP; internal_input_state[eventjoy.jaxis.which].buttons|=JOY_DOWN; }else{ internal_input_state[eventjoy.jaxis.which].buttons&=~JOY_UP; internal_input_state[eventjoy.jaxis.which].buttons&=~JOY_DOWN; } } break; case SDL_JOYBUTTONDOWN: if(eventjoy.jbutton.button==internal_input_map[eventjoy.jbutton.button].button0) { internal_input_state[eventjoy.jbutton.which].buttons|=JOY_BUTTON0; } else if(eventjoy.jbutton.button==internal_input_map[eventjoy.jbutton.which].button1) { internal_input_state[eventjoy.jbutton.which].buttons|=JOY_BUTTON1; } else if(eventjoy.jbutton.button==internal_input_map[eventjoy.jbutton.which].button2) { internal_input_state[eventjoy.jbutton.which].buttons|=JOY_BUTTON2; } else if(eventjoy.jbutton.button==internal_input_map[eventjoy.jbutton.which].button3) { internal_input_state[eventjoy.jbutton.which].buttons|=JOY_BUTTON3; } else if(eventjoy.jbutton.button==internal_input_map[eventjoy.jbutton.which].button4) { internal_input_state[eventjoy.jbutton.which].buttons|=JOY_BUTTON4; } else if(eventjoy.jbutton.button==internal_input_map[eventjoy.jbutton.which].button5) { internal_input_state[eventjoy.jbutton.which].buttons|=JOY_BUTTON5; } else if(eventjoy.jbutton.button==internal_input_map[eventjoy.jbutton.which].button6) { internal_input_state[eventjoy.jbutton.which].buttons|=JOY_BUTTON6; } else if(eventjoy.jbutton.button==internal_input_map[eventjoy.jbutton.which].button7) { internal_input_state[eventjoy.jbutton.which].buttons|=JOY_BUTTON7; } else if(eventjoy.jbutton.button==internal_input_map[eventjoy.jbutton.which].button8) { internal_input_state[eventjoy.jbutton.which].buttons|=JOY_BUTTON8; } else if(eventjoy.jbutton.button==internal_input_map[eventjoy.jbutton.which].button9) { internal_input_state[eventjoy.jbutton.which].buttons|=JOY_BUTTON9; } break; case SDL_JOYBUTTONUP: if(eventjoy.jbutton.button==internal_input_map[eventjoy.jbutton.button].button0) { internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_BUTTON0; } else if(eventjoy.jbutton.button==internal_input_map[eventjoy.jbutton.which].button1) { internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_BUTTON1; } else if(eventjoy.jbutton.button==internal_input_map[eventjoy.jbutton.which].button2) { internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_BUTTON2; } else if(eventjoy.jbutton.button==internal_input_map[eventjoy.jbutton.which].button3) { internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_BUTTON3; } else if(eventjoy.jbutton.button==internal_input_map[eventjoy.jbutton.which].button4) { internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_BUTTON4; } else if(eventjoy.jbutton.button==internal_input_map[eventjoy.jbutton.which].button5) { internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_BUTTON5; } else if(eventjoy.jbutton.button==internal_input_map[eventjoy.jbutton.which].button6) { internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_BUTTON6; } else if(eventjoy.jbutton.button==internal_input_map[eventjoy.jbutton.which].button7) { internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_BUTTON7; } else if(eventjoy.jbutton.button==internal_input_map[eventjoy.jbutton.which].button8) { internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_BUTTON8; } else if(eventjoy.jbutton.button==internal_input_map[eventjoy.jbutton.which].button9) { internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_BUTTON9; } break; case SDL_QUIT: internal_input_state[joyid].buttons|=SE_QUIT;break; case SDL_KEYDOWN: switch( eventjoy.key.keysym.sym ){ case SDLK_LEFT: internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_RIGHT; internal_input_state[eventjoy.jbutton.which].buttons|=JOY_LEFT;break; case SDLK_RIGHT: internal_input_state[eventjoy.jbutton.which].buttons|=JOY_RIGHT; internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_LEFT;break; case SDLK_UP: internal_input_state[eventjoy.jbutton.which].buttons|=JOY_UP; internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_DOWN;break; case SDLK_DOWN: internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_UP; internal_input_state[eventjoy.jbutton.which].buttons|=JOY_DOWN;break; case SDLK_z: internal_input_state[eventjoy.jbutton.which].buttons|=JOY_BUTTON0;break; case SDLK_x: internal_input_state[eventjoy.jbutton.which].buttons|=JOY_BUTTON1;break; case SDLK_c: internal_input_state[eventjoy.jbutton.which].buttons|=JOY_BUTTON2;break; case SDLK_v: internal_input_state[eventjoy.jbutton.which].buttons|=JOY_BUTTON3;break; case SDLK_a: internal_input_state[eventjoy.jbutton.which].buttons|=JOY_BUTTON4;break; case SDLK_s: internal_input_state[eventjoy.jbutton.which].buttons|=JOY_BUTTON5;break; case SDLK_d: internal_input_state[eventjoy.jbutton.which].buttons|=JOY_BUTTON6;break; case SDLK_f: internal_input_state[eventjoy.jbutton.which].buttons|=JOY_BUTTON7;break; default: break; }break; case SDL_KEYUP: switch( eventjoy.key.keysym.sym ){ case SDLK_LEFT: internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_RIGHT; internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_LEFT;break; case SDLK_RIGHT: internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_RIGHT; internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_LEFT;break; case SDLK_UP: internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_UP; internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_DOWN;break; case SDLK_DOWN: internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_UP; internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_DOWN;break; case SDLK_z: internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_BUTTON0;break; case SDLK_x: internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_BUTTON1;break; case SDLK_c: internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_BUTTON2;break; case SDLK_v: internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_BUTTON3;break; case SDLK_a: internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_BUTTON4;break; case SDLK_s: internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_BUTTON5;break; case SDLK_d: internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_BUTTON6;break; case SDLK_f: internal_input_state[eventjoy.jbutton.which].buttons&=~JOY_BUTTON7;break; default: break; }break; } } return &internal_input_state[joyid]; }
JNIEXPORT jint JNICALL Java_com_santiance_sdlbits_SDL_joystickIndex(JNIEnv *env, jclass c, jobject jjoy) { SDL_Joystick *joy = UnwrapJoy(jjoy); if (!joy) return 0; return SDL_JoystickIndex(joy); }