static void handle_tick(game_context_s *context, void *data, const nothing_s *n) { input_handler_s *input_handler = data; // process the callback-based events target_context = context; glfwPollEvents(); target_context = NULL; // process joysticks for(int i = 0; i < MAX_JOYSTICKS; i++) { if(glfwGetJoystickParam(i, GLFW_PRESENT) == GL_TRUE) { joystick_event_s *joystick = &input_handler->joysticks[i]; int axis_count = glfwGetJoystickParam(i, GLFW_AXES); array_set_length(joystick->axes, axis_count); glfwGetJoystickPos(i, array_get_ptr(joystick->axes), axis_count); int button_count = glfwGetJoystickParam(i, GLFW_BUTTONS); array_set_length(joystick->buttons, button_count); glfwGetJoystickButtons(i, array_get_ptr(joystick->buttons), button_count); broadcast_input_handler_joystick_event(context, *joystick); } } }
void RawController::update(){ // Update buttons flags. const float * rawAxes = glfwGetJoystickAxes(_id, &_rawAxesCount); const unsigned char * rawButtons = glfwGetJoystickButtons(_id, &_rawButtonsCount); if(_rawAxesCount != int(allAxes.size())){ allAxes.resize(_rawAxesCount); } if(_rawButtonsCount != int(allButtons.size())){ allButtons.resize(_rawButtonsCount); } for(int aid = 0; aid < _rawAxesCount; ++aid){ allAxes[aid] = rawAxes[aid]; } for(int bid = 0; bid < _rawButtonsCount; ++bid){ const bool pressed = (rawButtons[bid] == GLFW_PRESS); if(pressed){ if(allButtons[bid].pressed){ // Already pressed. allButtons[bid].first = false; } else { allButtons[bid].pressed = true; allButtons[bid].first = true; } } else { allButtons[bid].pressed = false; allButtons[bid].first = false; } } }
void Window:: pollGamepads() { // NB: GLFW limitation limits operation to a single window Window* window = main_window_.get(); if (window != nullptr && window->controller_ != nullptr) { for (int i=0; i<MAX_GAMEPADS; ++i) { // NB: data is copied into newState object to minimize changes when // porting from GLFW2 to GLFW3 int num_glfw_axes = 0; const float* axes = glfwGetJoystickAxes(i, &num_glfw_axes); int num_axes = num_glfw_axes < MAX_GAMEPAD_AXES ? num_glfw_axes : MAX_GAMEPAD_AXES; int num_glfw_buttons = 0; const unsigned char* buttons = glfwGetJoystickButtons(i, &num_glfw_buttons); int num_buttons = num_glfw_buttons < MAX_GAMEPAD_BUTTONS ? num_glfw_buttons : MAX_GAMEPAD_BUTTONS; GamepadState newState; newState.present = glfwJoystickPresent(i) == GL_TRUE; for (int j=0; j<num_axes; ++j) { newState.axis[j] = axes[j]; } for (int j=0; j<num_buttons; ++j) { newState.button[j] = buttons[j]; } if (newState.present != gamepad_state_[i].present) { window->controller_-> windowControlGamepadPresenceChanged(window->shared_from_this(), i, newState.present); } // a few hacks to standardize results from a Microsoft Gamepad newState.axis[1] = -newState.axis[1]; newState.axis[3] = -newState.axis[3]; newState.axis[7] = newState.axis[2] < 0.f ? -newState.axis[2] : 0.f; newState.axis[2] = newState.axis[2] > 0.f ? newState.axis[2] : 0.f; // TODO: dpad seems to be missing for (int j=0; j<num_axes; ++j) { if (newState.axis[j] != gamepad_state_[i].axis[j]) { window->controller_-> windowControlGamepadAxisMoved(window->shared_from_this(), i, j, newState.axis[j]); } } for (int j=0; j<num_buttons; ++j) { if (newState.button[j] != gamepad_state_[i].button[j]) { window->controller_->windowControlGamepadButtonChanged( window->shared_from_this(), i, j, newState.button[j] == GLFW_PRESS); } } gamepad_state_[i] = newState; } } }
///@brief Check all available joysticks for an Xbox Controller /// and store its idx in g_joystickIdx. /// Unfortunately, this operation is too time-consuming to call every frame /// in a VR app. The workaround is to call it on key press, space or 'G'. void FindPreferredJoystick() { g_joystickIdx = -1; for (int i = GLFW_JOYSTICK_1; i <= GLFW_JOYSTICK_LAST; ++i) { if (GL_FALSE == glfwJoystickPresent(i)) continue; const char* pJoyName = glfwGetJoystickName(i); if (pJoyName == NULL) continue; int numAxes = 0; int numButtons = 0; glfwGetJoystickAxes(i, &numAxes); glfwGetJoystickButtons(i, &numButtons); LOG_INFO("Glfw found Joystick #%d: %s w/ %d axes, %d buttons", i, pJoyName, numAxes, numButtons); // Take an educated guess that this is an Xbox controller - glfw's // id string says "Microsoft PC Joystick" for most gamepad types. ///@todo Why does GLFW on Linux return a different, more descriptive string? if (numAxes == 5 && numButtons == 14) { g_joystickIdx = i; return; } } }
void joystick() { static char s_lastButtons[256] = { 0 }; ///@todo Handle multiple joysticks ///@todo Do these calls take time? We can move them out if so int joyStick1Present = GL_FALSE; joyStick1Present = glfwJoystickPresent(g_joystickIdx); if (joyStick1Present != GL_TRUE) { if (g_joystickIdx == -1) return; } // Poll joystick int numAxes = 0; const float* pAxisStates = glfwGetJoystickAxes(g_joystickIdx, &numAxes); int numButtons = 0; const unsigned char* pButtonStates = glfwGetJoystickButtons(g_joystickIdx, &numButtons); // Take an educated guess that this is an Xbox controller - glfw's // id string says "Microsoft PC Joystick" for most gamepad types. ///@todo Why does GLFW on Linux return a different, more descriptive string? if (numAxes == 5 && numButtons == 14) { joystick_XboxController(g_joystickIdx, pAxisStates, numAxes, pButtonStates, numButtons, s_lastButtons); } memcpy(s_lastButtons, pButtonStates, numButtons); }
void plUpdatePad(CControllerState *state) { if(currentPad >= numPads) return; int n = currentPad; // printf("joy: %s\n", glfwGetJoystickName(GLFW_JOYSTICK_1+n)); int count; const float *axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1+n, &count); state->leftX = axes[0] * 32767; state->leftY = axes[1] * 32767; state->rightX = axes[2] * 32767; state->rightY = axes[3] * 32767; const unsigned char *buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_1+n, &count); state->triangle = buttons[12]; state->circle = buttons[13]; state->cross = buttons[14]; state->square = buttons[15]; state->l1 = buttons[10]; state->l2 = buttons[8]; state->leftshock = buttons[1]; state->r1 = buttons[11]; state->r2 = buttons[9]; state->rightshock = buttons[2]; state->select = buttons[0]; state->start = buttons[3]; state->up = buttons[4]; state->right = buttons[5]; state->down = buttons[6]; state->left = buttons[7]; }
/** Updated joystick info */ static void orxFASTCALL orxJoystick_GLFW_UpdateInfo(orxU32 _u32ID) { /* Checks */ orxASSERT(_u32ID <= GLFW_JOYSTICK_LAST); /* Needs update? */ if(sstJoystick.astJoyInfoList[_u32ID].fTimeStamp != sstJoystick.pstClockInfo->fTime) { /* Is connected? */ if(glfwGetJoystickParam((int)_u32ID, GLFW_PRESENT) != GL_FALSE) { /* Gets axes values */ glfwGetJoystickPos((int)_u32ID, sstJoystick.astJoyInfoList[_u32ID].afAxisInfoList, orxJOYSTICK_AXIS_SINGLE_NUMBER); /* Updates connection status */ sstJoystick.astJoyInfoList[_u32ID].bIsConnected = orxTRUE; /* Gets button values */ glfwGetJoystickButtons((int)_u32ID, sstJoystick.astJoyInfoList[_u32ID].au8ButtonInfoList, orxJOYSTICK_BUTTON_SINGLE_NUMBER); } else { /* Clears info */ orxMemory_Zero(&sstJoystick.astJoyInfoList[_u32ID], sizeof(orxJOYSTICK_INFO)); } /* Updates time stamp */ sstJoystick.astJoyInfoList[_u32ID].fTimeStamp = sstJoystick.pstClockInfo->fTime; } /* Done! */ return; }
static void refresh_joysticks(void) { int i; for (i = 0; i < sizeof(joysticks) / sizeof(Joystick); i++) { Joystick* j = joysticks + i; if (glfwGetJoystickParam(GLFW_JOYSTICK_1 + i, GLFW_PRESENT)) { int axis_count, button_count; free(j->name); j->name = strdup(glfwGetJoystickName(GLFW_JOYSTICK_1 + i)); axis_count = glfwGetJoystickParam(GLFW_JOYSTICK_1 + i, GLFW_AXES); if (axis_count != j->axis_count) { j->axis_count = axis_count; j->axes = realloc(j->axes, j->axis_count * sizeof(float)); } glfwGetJoystickAxes(GLFW_JOYSTICK_1 + i, j->axes, j->axis_count); button_count = glfwGetJoystickParam(GLFW_JOYSTICK_1 + i, GLFW_BUTTONS); if (button_count != j->button_count) { j->button_count = button_count; j->buttons = realloc(j->buttons, j->button_count); } glfwGetJoystickButtons(GLFW_JOYSTICK_1 + i, j->buttons, j->button_count); if (!j->present) { printf("Found joystick %i named \'%s\' with %i axes, %i buttons\n", i + 1, j->name, j->axis_count, j->button_count); joystick_count++; } j->present = GL_TRUE; } else { if (j->present) { printf("Lost joystick %i named \'%s\'\n", i + 1, j->name); free(j->name); free(j->axes); free(j->buttons); memset(j, 0, sizeof(Joystick)); joystick_count--; } } } }
// Joysticks void VogueWindow::UpdateJoySticks() { for (int i = 0; i < sizeof(m_joysticks) / sizeof(Joystick); i++) { Joystick* j = m_joysticks + i; if (glfwJoystickPresent(GLFW_JOYSTICK_1 + i)) { const float* axes; const unsigned char* buttons; int axis_count, button_count; free(j->m_name); j->m_name = strdup(glfwGetJoystickName(GLFW_JOYSTICK_1 + i)); axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1 + i, &axis_count); if (axis_count != j->m_axisCount) { j->m_axisCount = axis_count; j->m_axes = (float*)realloc(j->m_axes, j->m_axisCount * sizeof(float)); } memcpy(j->m_axes, axes, axis_count * sizeof(float)); buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_1 + i, &button_count); if (button_count != j->m_buttonCount) { j->m_buttonCount = button_count; j->m_buttons = (unsigned char*)realloc(j->m_buttons, j->m_buttonCount); } memcpy(j->m_buttons, buttons, button_count * sizeof(unsigned char)); if (!j->m_present) { printf("\nFound joystick %i named \'%s\' with %i axes, %i buttons\n", i + 1, j->m_name, j->m_axisCount, j->m_buttonCount); m_joystickCount++; } j->m_present = GL_TRUE; } else { if (j->m_present) { printf("\nLost joystick %i named \'%s\'\n", i + 1, j->m_name); free(j->m_name); free(j->m_axes); free(j->m_buttons); memset(j, 0, sizeof(Joystick)); m_joystickCount--; } } } }
bool Controller::getDRight() { if (!controllerPresent()) return false; int count = 0; const unsigned char* buttons = glfwGetJoystickButtons(0, &count); return buttons[11]; }
bool Controller::getLeftJoyStickPressed() { if (!controllerPresent()) return false; int count = 0; const unsigned char* buttons = glfwGetJoystickButtons(0, &count); return buttons[8]; }
void ImGui_ImplGlfw_NewFrame() { ImGuiIO& io = ImGui::GetIO(); IM_ASSERT(io.Fonts->IsBuilt()); // Font atlas needs to be built, call renderer _NewFrame() function e.g. ImGui_ImplOpenGL3_NewFrame() // Setup display size int w, h; int display_w, display_h; w = Global.iWindowWidth; h = Global.iWindowHeight; display_w = w; display_h = h; io.DisplaySize = ImVec2((float)w, (float)h); io.DisplayFramebufferScale = ImVec2(w > 0 ? ((float)display_w / w) : 0, h > 0 ? ((float)display_h / h) : 0); // Setup time step double current_time = glfwGetTime(); io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f); g_Time = current_time; ImGui_ImplGlfw_UpdateMousePosAndButtons(); ImGui_ImplGlfw_UpdateMouseCursor(); // Gamepad navigation mapping [BETA] memset(io.NavInputs, 0, sizeof(io.NavInputs)); if (io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) { // Update gamepad inputs #define MAP_BUTTON(NAV_NO, BUTTON_NO) { if (buttons_count > BUTTON_NO && buttons[BUTTON_NO] == GLFW_PRESS) io.NavInputs[NAV_NO] = 1.0f; } #define MAP_ANALOG(NAV_NO, AXIS_NO, V0, V1) { float v = (axes_count > AXIS_NO) ? axes[AXIS_NO] : V0; v = (v - V0) / (V1 - V0); if (v > 1.0f) v = 1.0f; if (io.NavInputs[NAV_NO] < v) io.NavInputs[NAV_NO] = v; } int axes_count = 0, buttons_count = 0; const float* axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1, &axes_count); const unsigned char* buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_1, &buttons_count); MAP_BUTTON(ImGuiNavInput_Activate, 0); // Cross / A MAP_BUTTON(ImGuiNavInput_Cancel, 1); // Circle / B MAP_BUTTON(ImGuiNavInput_Menu, 2); // Square / X MAP_BUTTON(ImGuiNavInput_Input, 3); // Triangle / Y MAP_BUTTON(ImGuiNavInput_DpadLeft, 13); // D-Pad Left MAP_BUTTON(ImGuiNavInput_DpadRight, 11); // D-Pad Right MAP_BUTTON(ImGuiNavInput_DpadUp, 10); // D-Pad Up MAP_BUTTON(ImGuiNavInput_DpadDown, 12); // D-Pad Down MAP_BUTTON(ImGuiNavInput_FocusPrev, 4); // L1 / LB MAP_BUTTON(ImGuiNavInput_FocusNext, 5); // R1 / RB MAP_BUTTON(ImGuiNavInput_TweakSlow, 4); // L1 / LB MAP_BUTTON(ImGuiNavInput_TweakFast, 5); // R1 / RB MAP_ANALOG(ImGuiNavInput_LStickLeft, 0, -0.3f, -0.9f); MAP_ANALOG(ImGuiNavInput_LStickRight,0, +0.3f, +0.9f); MAP_ANALOG(ImGuiNavInput_LStickUp, 1, +0.3f, +0.9f); MAP_ANALOG(ImGuiNavInput_LStickDown, 1, -0.3f, -0.9f); #undef MAP_BUTTON #undef MAP_ANALOG if (axes_count > 0 && buttons_count > 0) io.BackendFlags |= ImGuiBackendFlags_HasGamepad; else io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad; } }
void update() { _prev_buttons.resize(_buttons_count, 0); for (int i = 0; i < _buttons_count; i++) { _prev_buttons[i] = _buttons[i]; } _buttons = glfwGetJoystickButtons(_joy, &_buttons_count); _axes = glfwGetJoystickAxes(_joy, &_axes_count); }
void update(EventQueue& _eventQueue) { int numButtons, numAxes; const unsigned char* buttons = glfwGetJoystickButtons(m_handle.idx, &numButtons); const float* axes = glfwGetJoystickAxes(m_handle.idx, &numAxes); if (NULL == buttons || NULL == axes) { return; } if (numAxes > GamepadAxis::Count) { numAxes = GamepadAxis::Count; } if (numButtons > Key::Count - Key::GamepadA) { numButtons = Key::Count - Key::GamepadA; } WindowHandle defaultWindow = { 0 }; for (int ii = 0; ii < numAxes; ++ii) { GamepadAxis::Enum axis = translateGamepadAxis(ii); int32_t value = (int32_t) (axes[ii] * 32768.f); if (GamepadAxis::LeftY == axis || GamepadAxis::RightY == axis) { value = -value; } if (m_axes[ii] != value) { m_axes[ii] = value; _eventQueue.postAxisEvent(defaultWindow , m_handle , axis , value); } } for (int ii = 0; ii < numButtons; ++ii) { Key::Enum key = translateGamepadButton(ii); if (m_buttons[ii] != buttons[ii]) { m_buttons[ii] = buttons[ii]; _eventQueue.postKeyEvent(defaultWindow , key , 0 , buttons[ii] != 0); } } }
bool Joystick::init(const int index_) { _id = index_; _name = glfwGetJoystickName(_id); _axes = glfwGetJoystickAxes(_id, &_axesNum); _polledButtons = glfwGetJoystickButtons(_id, &_buttonsNum); for (int i = 0; i < _buttonsNum; ++ i) _buttons.push_back(false); return true; }
bool Controller::getB() { if (!controllerPresent()) return false; int count = 0; const unsigned char* buttons = glfwGetJoystickButtons(0, &count); /*for(int i = 0; i < count; i++) if(buttons[i] > 0) return true;*/ return buttons[1]; }
//SGenum SG_EXPORT sgmCoreJoystickButtonSetState(void* joystick, SGbool* state) SGenum SG_EXPORT sgmCoreJoystickButtonGetState(void* joystick, SGbool* state) { if(joystick == NULL) return SG_OK; // SG_INVALID_VALUE Joystick* cjoystick = (Joystick*)joystick; glfwGetJoystickButtons(cjoystick->id, cjoystick->buttons, cjoystick->numbuttons); state = memcpy(state, cjoystick->buttons, cjoystick->numbuttons * sizeof(SGbool)); return SG_OK; }
void Joystick::update(float dt_) { (void)dt_; _axes = glfwGetJoystickAxes(_id, &_axesNum); _leftStick.x() = _axes[0]; _leftStick.y() = _axes[1]; _rightStick.x() = _axes[3]; _rightStick.y() = _axes[4]; _polledButtons = glfwGetJoystickButtons(_id, &_buttonsNum); for (int i = 0; i < _buttonsNum; ++ i) _buttons[i] = (_polledButtons[i] == GLFW_PRESS) ? true : false; }
bool Controller::isButtonDown(const int index, const int button) { #if defined(JOP_OS_DESKTOP) int count = 0; const unsigned char* buttons = glfwGetJoystickButtons(index, &count); return (button < count) ? buttons[button] == GLFW_PRESS : false; #elif defined(JOP_OS_ANDROID) return detail::ActivityState::get()->activeControllerButtons[button]; #endif }
/*======================================================================== * Retrieve joystick states *========================================================================*/ static void updateJoysticksState(void) { int joy; for (joy = GLFW_JOYSTICK_1; joy < GLFW_JOYSTICK_LAST + 1; joy++) { printf("Updating information for joystick %d\n", joy); states[joy].present = glfwGetJoystickParam(joy, GLFW_PRESENT); if (states[joy].present == GL_TRUE) { states[joy].num_axes = glfwGetJoystickPos(joy, states[joy].axes, MAX_AXES); states[joy].num_buttons = glfwGetJoystickButtons(joy, states[joy].buttons, MAX_BUTTONS); } } }
void Joystick_Imp::RefreshInputState() { const float* ax = glfwGetJoystickAxes(m_connectId, &m_axesCount); for (int i = 0; i < m_axesCount; ++i) { m_currentAxes[i] = ax[i]; } const unsigned char* btns = glfwGetJoystickButtons(m_connectId, &m_buttonsCount); for (int i = 0; i < m_buttonsCount; ++i) { m_preButtonHit[i] = m_currentButtonHit[i]; m_currentButtonHit[i] = (bool)btns[i]; } }
bool Input::step() { INIT_ASSERT(Input); int count; for (unsigned i = 0; i < NUM_OF_KEYS; ++i) { keyState[i] = keyPress[i] > keyRelease[i]; if (glfwGetKey(Window::instance().window, i) == GLFW_PRESS) keyPress[i] = glfwGetTime(); else keyRelease[i] = glfwGetTime(); } for (unsigned i = 0; i < NUM_OF_MOUSE_BTNS; ++i) { mouseState[i] = mousePress[i] > mouseRelease[i]; if (glfwGetMouseButton(Window::instance().window, i) == GLFW_PRESS) mousePress[i] = glfwGetTime(); else mouseRelease[i] = glfwGetTime(); } for (unsigned i = 0; i < activeJoysticks; ++i) { for (unsigned j = 0; j < NUM_OF_JOY_BTNS; ++j) { joyState[i][j] = joyPress[i][j] > joyRelease[i][j]; if (glfwGetJoystickButtons(i, &count)[j] == GLFW_PRESS) joyPress[i][j] = glfwGetTime(); else joyRelease[i][j] = glfwGetTime(); } } double x, y; glfwGetCursorPos(Window::instance().window, &x, &y); mouseX = x; mouseY = y; for (unsigned i = 0; i < activeJoysticks; ++i) for (unsigned j = 0; j < NUM_OF_JOY_AXES; ++j) joyAxes[i][j] = glfwGetJoystickAxes(i, &count)[j]; return false; }
void _update() override { int countButtons; const unsigned char *buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_2, &countButtons); static bool reloaded = false; if (buttons[3] == 1 && !reloaded) { print_d("Reloading assets"); flare::asset::reload(); } if (buttons[3] == 0 && reloaded) { reloaded = false; } }
//-----------------------------------------------------------------// void device::service(const bits_t& bits, const locator& poss) { bits_t b = bits; int joy = 0; if(glfwJoystickPresent(joy) == GL_TRUE) { int count; const float* axes = glfwGetJoystickAxes(joy, &count); for(int i = 0; i < count; ++i) { if(i == 1) { if(axes[i] > 0.5f) b.set(key::GAME_UP); else if(axes[i] < -0.5f) b.set(key::GAME_DOWN); } else if(i == 0) { if(axes[i] > 0.5f) b.set(key::GAME_RIGHT); else if(axes[i] < -0.5f) b.set(key::GAME_LEFT); } } const unsigned char* bl = glfwGetJoystickButtons(joy, &count); if(count > 16) count = 16; for(int i = 0; i < count; ++i) { if(bl[i] != 0) b.set(static_cast<key>(static_cast<int>(key::GAME_0) + i)); } } b.set(key::STATE_CAPS_LOCK, level_.test(key::STATE_CAPS_LOCK)); b.set(key::STATE_SCROLL_LOCK, level_.test(key::STATE_SCROLL_LOCK)); b.set(key::STATE_NUM_LOCK, level_.test(key::STATE_NUM_LOCK)); positive_ = b & ~level_; negative_ = ~b & level_; if(positive_.test(key::CAPS_LOCK)) { b.flip(key::STATE_CAPS_LOCK); } if(positive_.test(key::SCROLL_LOCK)) { b.flip(key::STATE_SCROLL_LOCK); } if(positive_.test(key::NUM_LOCK)) { b.flip(key::STATE_NUM_LOCK); } level_ = b; locator_ = poss; }
std::vector<JoystickState> GLFW_App::get_joystick_states() const { std::vector<JoystickState> ret; int count; for(int i = GLFW_JOYSTICK_1; i <= GLFW_JOYSTICK_LAST; i++) { if(!glfwJoystickPresent(i)) continue; const float *glfw_axis = glfwGetJoystickAxes(i, &count); std::vector<float> axis(glfw_axis, glfw_axis + count); const uint8_t *glfw_buttons = glfwGetJoystickButtons(i, &count); std::vector<uint8_t> buttons(glfw_buttons, glfw_buttons + count); std::string name(glfwGetJoystickName(i)); ret.push_back(JoystickState(name, buttons, axis)); } return ret; }
void CApplication::ProcessJoystickInput() { if (g_aJoysticks.size() != MAX_JOYSTICKS) return; for (size_t i = 0; i < MAX_JOYSTICKS; i++) { CJoystick& oJoystick = g_aJoysticks[i]; if (!oJoystick.m_bPresent) continue; static tvector<float> aflAxis; aflAxis.resize(oJoystick.m_aflAxis.size()); glfwGetJoystickPos(i, &aflAxis[0], oJoystick.m_aflAxis.size()); for (size_t j = 0; j < oJoystick.m_aflAxis.size(); j++) { if (aflAxis[j] != oJoystick.m_aflAxis[j]) JoystickAxis(i, j, aflAxis[j], aflAxis[j]-oJoystick.m_aflAxis[j]); } oJoystick.m_aflAxis = aflAxis; static tvector<unsigned char> aiButtons; aiButtons.resize(oJoystick.m_iButtons); glfwGetJoystickButtons(i, &aiButtons[0], oJoystick.m_iButtons); for (size_t j = 0; j < oJoystick.m_iButtons; j++) { unsigned long long iButtonMask = (1<<j); if (aiButtons[j] == GLFW_PRESS && !(oJoystick.m_aiButtonStates&iButtonMask)) JoystickButtonPress(i, MapJoystickKey(j)); else if (aiButtons[j] == GLFW_RELEASE && (oJoystick.m_aiButtonStates&iButtonMask)) JoystickButtonRelease(i, MapJoystickKey(j)); if (aiButtons[j] == GLFW_PRESS) oJoystick.m_aiButtonStates |= iButtonMask; else oJoystick.m_aiButtonStates &= ~iButtonMask; } } }
static void joystick_callback(int joy, int event) { if (event == GLFW_CONNECTED) { int axisCount, buttonCount; glfwGetJoystickAxes(joy, &axisCount); glfwGetJoystickButtons(joy, &buttonCount); printf("%08x at %0.3f: Joystick %i (%s) was connected with %i axes and %i buttons\n", counter++, glfwGetTime(), joy, glfwGetJoystickName(joy), axisCount, buttonCount); } else { printf("%08x at %0.3f: Joystick %i was disconnected\n", counter++, glfwGetTime(), joy); } }
void Joystick::Update() { previous_axes = axes; previous_buttons = buttons; if (glfwJoystickPresent(joystick_id)) { int axis_count = 0; const float *axis_data = glfwGetJoystickAxes(joystick_id, &axis_count); CHECK_STATE(axis_count); CHECK_STATE(axis_data); for (auto i = 0; i < axis_count; ++i) { axes[static_cast<Axis>(i)] = axis_data[i]; } int button_count = 0; const unsigned char *button_data = glfwGetJoystickButtons(joystick_id, &button_count); CHECK_STATE(button_count); CHECK_STATE(button_data); for (auto i = static_cast<int>(Button::kBegin); i < static_cast<int>(Button::kEnd); ++i) { buttons[static_cast<Button>(i)] = button_data[i]; } } auto now = std::chrono::high_resolution_clock::now(); dt = std::chrono::duration_cast<std::chrono::duration<float>>(now - last_update_time).count(); last_update_time = now; }
///@brief Check out what joysticks we have and select a preferred one bool OculusAppSkeleton::initJoysticks() { for (int i=GLFW_JOYSTICK_1; i<GLFW_JOYSTICK_16; ++i) { const int present = glfwJoystickPresent(i); if (present == GL_TRUE) { /// Nostromo: 6 axes, 24 buttons /// Gravis Gamepad Pro: 2 axes, 10 buttons /// Generic wireless dualshock: 4 axes, 12 buttons /// Eliminator Aftershock: 6 axes, 10 buttons int numAxes = 0; int numButs = 0; glfwGetJoystickAxes(i, &numAxes); glfwGetJoystickButtons(i, &numButs); printf("Joystick %d: %d axes, %d buttons\n", i, numAxes, numButs); /// This just finds the first available joystick. if ( (numAxes == 2) && (numButs == 10)) { preferredGamepadID = i; swapGamepadRAxes = false; } else if ( (numAxes == 6) && (numButs == 10)) { preferredGamepadID = i; swapGamepadRAxes = false; } else if ( (numAxes == 4) && (numButs == 12)) { preferredGamepadID = i; swapGamepadRAxes = true; } } } return true; }
void ofxGLFWJoystick::lookForJoysticks(){ int n = 0; for(int i = 0; i < GLFW_JOYSTICK_LAST; i++){ if(glfwJoystickPresent(i)){ joyData[i].available = true; string name = string(glfwGetJoystickName(i)); if(name != joyData[i].name){ joyData[i].name = name; ofLogNotice("ofxGLFWJoystick") << "Joystick Found at index " << i << ": '" << name << "'"; joyData[i].axisData = glfwGetJoystickAxes(i, &joyData[i].numAxis); joyData[i].buttonData = glfwGetJoystickButtons(i, &joyData[i].numButtons); } n++; }else{ if (joyData[i].name.size()){ ofLogNotice("ofxGLFWJoystick") << "Joystick Lost at index " << i << ": '" << joyData[i].name << "'"; } joyData[i].available = false; joyData[i].name = ""; } } numJoysticks = n; }