diff options
Diffstat (limited to 'platform')
-rw-r--r-- | platform/windows/joystick.cpp | 668 | ||||
-rw-r--r-- | platform/windows/joystick.h | 162 | ||||
-rw-r--r-- | platform/x11/joystick_linux.cpp | 546 | ||||
-rw-r--r-- | platform/x11/joystick_linux.h | 74 |
4 files changed, 725 insertions, 725 deletions
diff --git a/platform/windows/joystick.cpp b/platform/windows/joystick.cpp index 32620c7779..68364ea8d5 100644 --- a/platform/windows/joystick.cpp +++ b/platform/windows/joystick.cpp @@ -44,354 +44,354 @@ joystick_windows::joystick_windows() { joystick_windows::joystick_windows(InputDefault* _input, HWND* hwnd) { - input = _input; - hWnd = hwnd; - joystick_count = 0; - dinput = NULL; - xinput_dll = NULL; - xinput_get_state = NULL; + input = _input; + hWnd = hwnd; + joystick_count = 0; + dinput = NULL; + xinput_dll = NULL; + xinput_get_state = NULL; - load_xinput(); + load_xinput(); - for (int i = 0; i < JOYSTICKS_MAX; i++) - attached_joysticks[i] = false; + for (int i = 0; i < JOYSTICKS_MAX; i++) + attached_joysticks[i] = false; - HRESULT result; - result = DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&dinput, NULL); - if (FAILED(result)) { - printf("failed init DINPUT: %ld\n", result); - } - probe_joysticks(); + HRESULT result; + result = DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&dinput, NULL); + if (FAILED(result)) { + printf("failed init DINPUT: %ld\n", result); + } + probe_joysticks(); } joystick_windows::~joystick_windows() { - close_joystick(); - dinput->Release(); - unload_xinput(); + close_joystick(); + dinput->Release(); + unload_xinput(); } bool joystick_windows::have_device(const GUID &p_guid) { - for (int i = 0; i < JOYSTICKS_MAX; i++) { + for (int i = 0; i < JOYSTICKS_MAX; i++) { - if (d_joysticks[i].guid == p_guid) { + if (d_joysticks[i].guid == p_guid) { - d_joysticks[i].confirmed = true; - return true; - } - } - return false; + d_joysticks[i].confirmed = true; + return true; + } + } + return false; } int joystick_windows::check_free_joy_slot() const { - for (int i = 0; i < JOYSTICKS_MAX; i++) { + for (int i = 0; i < JOYSTICKS_MAX; i++) { - if (!attached_joysticks[i]) - return i; - } - return -1; + if (!attached_joysticks[i]) + return i; + } + return -1; } // adapted from SDL2, works a lot better than the MSDN version bool joystick_windows::is_xinput_device(const GUID *p_guid) { - PRAWINPUTDEVICELIST dev_list = NULL; - unsigned int dev_list_count = 0; - - if (GetRawInputDeviceList(NULL, &dev_list_count, sizeof(RAWINPUTDEVICELIST)) == -1) { - return false; - } - dev_list = (PRAWINPUTDEVICELIST) malloc(sizeof(RAWINPUTDEVICELIST) * dev_list_count); - if (!dev_list) return false; - - if (GetRawInputDeviceList(dev_list, &dev_list_count, sizeof(RAWINPUTDEVICELIST)) == -1) { - free(dev_list); - return false; - } - for (int i = 0; i < dev_list_count; i++) { - - RID_DEVICE_INFO rdi; - char dev_name[128]; - UINT rdiSize = sizeof(rdi); - UINT nameSize = sizeof(dev_name); - - rdi.cbSize = rdiSize; - if ( (dev_list[i].dwType == RIM_TYPEHID) && - (GetRawInputDeviceInfoA(dev_list[i].hDevice, RIDI_DEVICEINFO, &rdi, &rdiSize) != (UINT)-1) && - (MAKELONG(rdi.hid.dwVendorId, rdi.hid.dwProductId) == (LONG)p_guid->Data1) && - (GetRawInputDeviceInfoA(dev_list[i].hDevice, RIDI_DEVICENAME, &dev_name, &nameSize) != (UINT)-1) && - (strstr(dev_name, "IG_") != NULL)) { - - free(dev_list); - return true; - } - } - free(dev_list); - return false; + PRAWINPUTDEVICELIST dev_list = NULL; + unsigned int dev_list_count = 0; + + if (GetRawInputDeviceList(NULL, &dev_list_count, sizeof(RAWINPUTDEVICELIST)) == -1) { + return false; + } + dev_list = (PRAWINPUTDEVICELIST) malloc(sizeof(RAWINPUTDEVICELIST) * dev_list_count); + if (!dev_list) return false; + + if (GetRawInputDeviceList(dev_list, &dev_list_count, sizeof(RAWINPUTDEVICELIST)) == -1) { + free(dev_list); + return false; + } + for (int i = 0; i < dev_list_count; i++) { + + RID_DEVICE_INFO rdi; + char dev_name[128]; + UINT rdiSize = sizeof(rdi); + UINT nameSize = sizeof(dev_name); + + rdi.cbSize = rdiSize; + if ( (dev_list[i].dwType == RIM_TYPEHID) && + (GetRawInputDeviceInfoA(dev_list[i].hDevice, RIDI_DEVICEINFO, &rdi, &rdiSize) != (UINT)-1) && + (MAKELONG(rdi.hid.dwVendorId, rdi.hid.dwProductId) == (LONG)p_guid->Data1) && + (GetRawInputDeviceInfoA(dev_list[i].hDevice, RIDI_DEVICENAME, &dev_name, &nameSize) != (UINT)-1) && + (strstr(dev_name, "IG_") != NULL)) { + + free(dev_list); + return true; + } + } + free(dev_list); + return false; } bool joystick_windows::setup_dinput_joystick(const DIDEVICEINSTANCE* instance) { - HRESULT hr; - int num = check_free_joy_slot(); + HRESULT hr; + int num = check_free_joy_slot(); - if (have_device(instance->guidInstance) || num == -1) - return false; + if (have_device(instance->guidInstance) || num == -1) + return false; - d_joysticks[joystick_count] = dinput_gamepad(); - dinput_gamepad* joy = &d_joysticks[num]; + d_joysticks[joystick_count] = dinput_gamepad(); + dinput_gamepad* joy = &d_joysticks[num]; - const DWORD devtype = (instance->dwDevType & 0xFF); + const DWORD devtype = (instance->dwDevType & 0xFF); - if ((devtype != DI8DEVTYPE_JOYSTICK) && (devtype != DI8DEVTYPE_GAMEPAD) && (devtype != DI8DEVTYPE_1STPERSON)) { - //printf("ignore device %s, type %x\n", instance->tszProductName, devtype); - return false; - } + if ((devtype != DI8DEVTYPE_JOYSTICK) && (devtype != DI8DEVTYPE_GAMEPAD) && (devtype != DI8DEVTYPE_1STPERSON)) { + //printf("ignore device %s, type %x\n", instance->tszProductName, devtype); + return false; + } - hr = dinput->CreateDevice(instance->guidInstance, &joy->di_joy, NULL); + hr = dinput->CreateDevice(instance->guidInstance, &joy->di_joy, NULL); - if (FAILED(hr)) { + if (FAILED(hr)) { - //std::wcout << "failed to create device: " << instance->tszProductName << std::endl; - return false; - } + //std::wcout << "failed to create device: " << instance->tszProductName << std::endl; + return false; + } - const GUID &guid = instance->guidProduct; - char uid[128]; - sprintf(uid, "%08lx%04hx%04hx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx", - __builtin_bswap32(guid.Data1), guid.Data2, guid.Data3, - guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3], - guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]); + const GUID &guid = instance->guidProduct; + char uid[128]; + sprintf(uid, "%08lx%04hx%04hx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx", + __builtin_bswap32(guid.Data1), guid.Data2, guid.Data3, + guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3], + guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]); - id_to_change = num; - joy->di_joy->SetDataFormat(&c_dfDIJoystick2); - joy->di_joy->SetCooperativeLevel(*hWnd, DISCL_FOREGROUND); - joy->di_joy->EnumObjects(objectsCallback, this, NULL); - joy->joy_axis.sort(); + id_to_change = num; + joy->di_joy->SetDataFormat(&c_dfDIJoystick2); + joy->di_joy->SetCooperativeLevel(*hWnd, DISCL_FOREGROUND); + joy->di_joy->EnumObjects(objectsCallback, this, NULL); + joy->joy_axis.sort(); - joy->guid = instance->guidInstance; - input->joy_connection_changed(num, true, instance->tszProductName, uid); - joy->attached = true; - joy->id = num; - attached_joysticks[num] = true; - joy->confirmed = true; - joystick_count++; - return true; + joy->guid = instance->guidInstance; + input->joy_connection_changed(num, true, instance->tszProductName, uid); + joy->attached = true; + joy->id = num; + attached_joysticks[num] = true; + joy->confirmed = true; + joystick_count++; + return true; } void joystick_windows::setup_joystick_object(const DIDEVICEOBJECTINSTANCE *ob, int p_joy_id) { - if (ob->dwType & DIDFT_AXIS) { - - HRESULT res; - DIPROPRANGE prop_range; - DIPROPDWORD dilong; - DWORD ofs; - if (ob->guidType == GUID_XAxis) - ofs = DIJOFS_X; - else if (ob->guidType == GUID_YAxis) - ofs = DIJOFS_Y; - else if (ob->guidType == GUID_ZAxis) - ofs = DIJOFS_Z; - else if (ob->guidType == GUID_RxAxis) - ofs = DIJOFS_RX; - else if (ob->guidType == GUID_RyAxis) - ofs = DIJOFS_RY; - else if (ob->guidType == GUID_RzAxis) - ofs = DIJOFS_RZ; - else if (ob->guidType == GUID_Slider) - ofs = DIJOFS_SLIDER(0); - else - return; - prop_range.diph.dwSize = sizeof(DIPROPRANGE); - prop_range.diph.dwHeaderSize = sizeof(DIPROPHEADER); - prop_range.diph.dwObj = ob->dwType; - prop_range.diph.dwHow = DIPH_BYID; - prop_range.lMin = -MAX_JOY_AXIS; - prop_range.lMax = +MAX_JOY_AXIS; - - dinput_gamepad &joy = d_joysticks[p_joy_id]; - - - res = joy.di_joy->SetProperty(DIPROP_RANGE, &prop_range.diph); - if (FAILED(res)) - return; - - dilong.diph.dwSize = sizeof(dilong); - dilong.diph.dwHeaderSize = sizeof(dilong.diph); - dilong.diph.dwObj = ob->dwType; - dilong.diph.dwHow = DIPH_BYID; - dilong.dwData = 0; - - res = IDirectInputDevice8_SetProperty(joy.di_joy, DIPROP_DEADZONE, &dilong.diph); - if (FAILED(res)) - return; - - joy.joy_axis.push_back(ofs); - } + if (ob->dwType & DIDFT_AXIS) { + + HRESULT res; + DIPROPRANGE prop_range; + DIPROPDWORD dilong; + DWORD ofs; + if (ob->guidType == GUID_XAxis) + ofs = DIJOFS_X; + else if (ob->guidType == GUID_YAxis) + ofs = DIJOFS_Y; + else if (ob->guidType == GUID_ZAxis) + ofs = DIJOFS_Z; + else if (ob->guidType == GUID_RxAxis) + ofs = DIJOFS_RX; + else if (ob->guidType == GUID_RyAxis) + ofs = DIJOFS_RY; + else if (ob->guidType == GUID_RzAxis) + ofs = DIJOFS_RZ; + else if (ob->guidType == GUID_Slider) + ofs = DIJOFS_SLIDER(0); + else + return; + prop_range.diph.dwSize = sizeof(DIPROPRANGE); + prop_range.diph.dwHeaderSize = sizeof(DIPROPHEADER); + prop_range.diph.dwObj = ob->dwType; + prop_range.diph.dwHow = DIPH_BYID; + prop_range.lMin = -MAX_JOY_AXIS; + prop_range.lMax = +MAX_JOY_AXIS; + + dinput_gamepad &joy = d_joysticks[p_joy_id]; + + + res = joy.di_joy->SetProperty(DIPROP_RANGE, &prop_range.diph); + if (FAILED(res)) + return; + + dilong.diph.dwSize = sizeof(dilong); + dilong.diph.dwHeaderSize = sizeof(dilong.diph); + dilong.diph.dwObj = ob->dwType; + dilong.diph.dwHow = DIPH_BYID; + dilong.dwData = 0; + + res = IDirectInputDevice8_SetProperty(joy.di_joy, DIPROP_DEADZONE, &dilong.diph); + if (FAILED(res)) + return; + + joy.joy_axis.push_back(ofs); + } } BOOL CALLBACK joystick_windows::enumCallback(const DIDEVICEINSTANCE* instance, void* pContext) { - joystick_windows* self = (joystick_windows*)pContext; - if (self->is_xinput_device(&instance->guidProduct)) {; - return DIENUM_CONTINUE; - } - self->setup_dinput_joystick(instance); - return DIENUM_CONTINUE; + joystick_windows* self = (joystick_windows*)pContext; + if (self->is_xinput_device(&instance->guidProduct)) {; + return DIENUM_CONTINUE; + } + self->setup_dinput_joystick(instance); + return DIENUM_CONTINUE; } BOOL CALLBACK joystick_windows::objectsCallback(const DIDEVICEOBJECTINSTANCE *instance, void *context) { - joystick_windows* self = (joystick_windows*)context; - self->setup_joystick_object(instance, self->id_to_change); + joystick_windows* self = (joystick_windows*)context; + self->setup_joystick_object(instance, self->id_to_change); - return DIENUM_CONTINUE; + return DIENUM_CONTINUE; } void joystick_windows::close_joystick(int id) { - if (id == -1) { + if (id == -1) { - for (int i = 0; i < JOYSTICKS_MAX; i++) { + for (int i = 0; i < JOYSTICKS_MAX; i++) { - close_joystick(i); - } - return; - } + close_joystick(i); + } + return; + } - if (!d_joysticks[id].attached) return; + if (!d_joysticks[id].attached) return; - d_joysticks[id].di_joy->Unacquire(); - d_joysticks[id].di_joy->Release(); - d_joysticks[id].attached = false; - attached_joysticks[d_joysticks[id].id] = false; - d_joysticks[id].guid.Data1 = d_joysticks[id].guid.Data2 = d_joysticks[id].guid.Data3 = 0; - input->joy_connection_changed(id, false, ""); - joystick_count--; + d_joysticks[id].di_joy->Unacquire(); + d_joysticks[id].di_joy->Release(); + d_joysticks[id].attached = false; + attached_joysticks[d_joysticks[id].id] = false; + d_joysticks[id].guid.Data1 = d_joysticks[id].guid.Data2 = d_joysticks[id].guid.Data3 = 0; + input->joy_connection_changed(id, false, ""); + joystick_count--; } void joystick_windows::probe_joysticks() { - DWORD dwResult; - for (DWORD i = 0; i < XUSER_MAX_COUNT; i++) { + DWORD dwResult; + for (DWORD i = 0; i < XUSER_MAX_COUNT; i++) { - ZeroMemory(&x_joysticks[i].state, sizeof(XINPUT_STATE)); + ZeroMemory(&x_joysticks[i].state, sizeof(XINPUT_STATE)); - dwResult = xinput_get_state(i, &x_joysticks[i].state); - if ( dwResult == ERROR_SUCCESS) { + dwResult = xinput_get_state(i, &x_joysticks[i].state); + if ( dwResult == ERROR_SUCCESS) { - int id = check_free_joy_slot(); - if (id != -1 && !x_joysticks[i].attached) { + int id = check_free_joy_slot(); + if (id != -1 && !x_joysticks[i].attached) { - x_joysticks[i].attached = true; - x_joysticks[i].id = id; - attached_joysticks[id] = true; - input->joy_connection_changed(id, true, "XInput Gamepad","__XINPUT_DEVICE__"); - } - } - else if (x_joysticks[i].attached) { + x_joysticks[i].attached = true; + x_joysticks[i].id = id; + attached_joysticks[id] = true; + input->joy_connection_changed(id, true, "XInput Gamepad","__XINPUT_DEVICE__"); + } + } + else if (x_joysticks[i].attached) { - x_joysticks[i].attached = false; - attached_joysticks[x_joysticks[i].id] = false; - input->joy_connection_changed(x_joysticks[i].id, false, ""); - } - } + x_joysticks[i].attached = false; + attached_joysticks[x_joysticks[i].id] = false; + input->joy_connection_changed(x_joysticks[i].id, false, ""); + } + } - for (int i = 0; i < joystick_count; i++) { + for (int i = 0; i < joystick_count; i++) { - d_joysticks[i].confirmed = false; - } + d_joysticks[i].confirmed = false; + } - dinput->EnumDevices(DI8DEVCLASS_GAMECTRL, enumCallback, this, DIEDFL_ATTACHEDONLY); + dinput->EnumDevices(DI8DEVCLASS_GAMECTRL, enumCallback, this, DIEDFL_ATTACHEDONLY); - for (int i = 0; i < joystick_count; i++) { + for (int i = 0; i < joystick_count; i++) { - if (!d_joysticks[i].confirmed) { + if (!d_joysticks[i].confirmed) { - close_joystick(i); - } - } + close_joystick(i); + } + } } unsigned int joystick_windows::process_joysticks(unsigned int p_last_id) { - HRESULT hr; + HRESULT hr; - for (int i = 0; i < XUSER_MAX_COUNT; i++) { + for (int i = 0; i < XUSER_MAX_COUNT; i++) { - xinput_gamepad &joy = x_joysticks[i]; - if (!joy.attached) { - continue; - } - ZeroMemory(&joy.state, sizeof(XINPUT_STATE)); + xinput_gamepad &joy = x_joysticks[i]; + if (!joy.attached) { + continue; + } + ZeroMemory(&joy.state, sizeof(XINPUT_STATE)); - xinput_get_state(i, &joy.state); - if (joy.state.dwPacketNumber != joy.last_packet) { + xinput_get_state(i, &joy.state); + if (joy.state.dwPacketNumber != joy.last_packet) { - int button_mask = XINPUT_GAMEPAD_DPAD_UP; - for (int i = 0; i <= 16; i++) { + int button_mask = XINPUT_GAMEPAD_DPAD_UP; + for (int i = 0; i <= 16; i++) { - p_last_id = input->joy_button(p_last_id, joy.id, i, joy.state.Gamepad.wButtons & button_mask); - button_mask = button_mask * 2; - } + p_last_id = input->joy_button(p_last_id, joy.id, i, joy.state.Gamepad.wButtons & button_mask); + button_mask = button_mask * 2; + } - p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_0, axis_correct(joy.state.Gamepad.sThumbLX, true)); - p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_1, axis_correct(joy.state.Gamepad.sThumbLY, true, false, true)); - p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_2, axis_correct(joy.state.Gamepad.sThumbRX, true)); - p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_3, axis_correct(joy.state.Gamepad.sThumbRY, true, false, true)); - p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_4, axis_correct(joy.state.Gamepad.bLeftTrigger, true, true)); - p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_5, axis_correct(joy.state.Gamepad.bRightTrigger, true, true)); - joy.last_packet = joy.state.dwPacketNumber; - } - } + p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_0, axis_correct(joy.state.Gamepad.sThumbLX, true)); + p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_1, axis_correct(joy.state.Gamepad.sThumbLY, true, false, true)); + p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_2, axis_correct(joy.state.Gamepad.sThumbRX, true)); + p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_3, axis_correct(joy.state.Gamepad.sThumbRY, true, false, true)); + p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_4, axis_correct(joy.state.Gamepad.bLeftTrigger, true, true)); + p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_5, axis_correct(joy.state.Gamepad.bRightTrigger, true, true)); + joy.last_packet = joy.state.dwPacketNumber; + } + } - for (int i = 0; i < JOYSTICKS_MAX; i++) { + for (int i = 0; i < JOYSTICKS_MAX; i++) { - dinput_gamepad* joy = &d_joysticks[i]; + dinput_gamepad* joy = &d_joysticks[i]; - if (!joy->attached) - continue; + if (!joy->attached) + continue; - DIJOYSTATE2 js; - hr = joy->di_joy->Poll(); - if (hr == DIERR_INPUTLOST || hr == DIERR_NOTACQUIRED) { - IDirectInputDevice8_Acquire(joy->di_joy); - joy->di_joy->Poll(); - } - if (FAILED(hr = d_joysticks[i].di_joy->GetDeviceState(sizeof(DIJOYSTATE2), &js))) { + DIJOYSTATE2 js; + hr = joy->di_joy->Poll(); + if (hr == DIERR_INPUTLOST || hr == DIERR_NOTACQUIRED) { + IDirectInputDevice8_Acquire(joy->di_joy); + joy->di_joy->Poll(); + } + if (FAILED(hr = d_joysticks[i].di_joy->GetDeviceState(sizeof(DIJOYSTATE2), &js))) { - //printf("failed to read joy #%d\n", i); - continue; - } + //printf("failed to read joy #%d\n", i); + continue; + } - p_last_id = post_hat(p_last_id, i, js.rgdwPOV[0]); + p_last_id = post_hat(p_last_id, i, js.rgdwPOV[0]); - for (int j = 0; j < 128; j++) { + for (int j = 0; j < 128; j++) { - if (js.rgbButtons[j] & 0x80) { + if (js.rgbButtons[j] & 0x80) { - if (!joy->last_buttons[j]) { + if (!joy->last_buttons[j]) { - p_last_id = input->joy_button(p_last_id, i, j, true); - joy->last_buttons[j] = true; - } - } - else { + p_last_id = input->joy_button(p_last_id, i, j, true); + joy->last_buttons[j] = true; + } + } + else { - if (joy->last_buttons[j]) { + if (joy->last_buttons[j]) { - p_last_id = input->joy_button(p_last_id, i, j, false); - joy->last_buttons[j] = false; - } - } - } + p_last_id = input->joy_button(p_last_id, i, j, false); + joy->last_buttons[j] = false; + } + } + } // on mingw, these constants are not constants int count = 6; @@ -407,122 +407,122 @@ unsigned int joystick_windows::process_joysticks(unsigned int p_last_id) { }; }; }; - } - return p_last_id; + } + return p_last_id; } unsigned int joystick_windows::post_hat(unsigned int p_last_id, int p_device, DWORD p_dpad) { - int dpad_val = 0; + int dpad_val = 0; - if (p_dpad == -1) { - dpad_val = InputDefault::HAT_MASK_CENTER; - } - if (p_dpad == 0) { + if (p_dpad == -1) { + dpad_val = InputDefault::HAT_MASK_CENTER; + } + if (p_dpad == 0) { - dpad_val = InputDefault::HAT_MASK_UP; + dpad_val = InputDefault::HAT_MASK_UP; - } - else if (p_dpad == 4500) { + } + else if (p_dpad == 4500) { - dpad_val = (InputDefault::HAT_MASK_UP | InputDefault::HAT_MASK_RIGHT); + dpad_val = (InputDefault::HAT_MASK_UP | InputDefault::HAT_MASK_RIGHT); - } - else if (p_dpad == 9000) { + } + else if (p_dpad == 9000) { - dpad_val = InputDefault::HAT_MASK_RIGHT; + dpad_val = InputDefault::HAT_MASK_RIGHT; - } - else if (p_dpad == 13500) { + } + else if (p_dpad == 13500) { - dpad_val = (InputDefault::HAT_MASK_RIGHT | InputDefault::HAT_MASK_DOWN); + dpad_val = (InputDefault::HAT_MASK_RIGHT | InputDefault::HAT_MASK_DOWN); - } - else if (p_dpad == 18000) { + } + else if (p_dpad == 18000) { - dpad_val = InputDefault::HAT_MASK_DOWN; + dpad_val = InputDefault::HAT_MASK_DOWN; - } - else if (p_dpad == 22500) { + } + else if (p_dpad == 22500) { - dpad_val = (InputDefault::HAT_MASK_DOWN | InputDefault::HAT_MASK_LEFT); + dpad_val = (InputDefault::HAT_MASK_DOWN | InputDefault::HAT_MASK_LEFT); - } - else if (p_dpad == 27000) { + } + else if (p_dpad == 27000) { - dpad_val = InputDefault::HAT_MASK_LEFT; + dpad_val = InputDefault::HAT_MASK_LEFT; - } - else if (p_dpad == 31500) { + } + else if (p_dpad == 31500) { - dpad_val = (InputDefault::HAT_MASK_LEFT | InputDefault::HAT_MASK_UP); - } - return input->joy_hat(p_last_id, p_device, dpad_val); + dpad_val = (InputDefault::HAT_MASK_LEFT | InputDefault::HAT_MASK_UP); + } + return input->joy_hat(p_last_id, p_device, dpad_val); }; InputDefault::JoyAxis joystick_windows::axis_correct(int p_val, bool p_xinput, bool p_trigger, bool p_negate) const { - InputDefault::JoyAxis jx; - if (Math::abs(p_val) < MIN_JOY_AXIS) { - jx.min = -1; - jx.value = 0.0f; - return jx; - } - if (p_xinput) { - - if (p_trigger) { - jx.min = 0; - jx.value = (float)p_val / MAX_TRIGGER; - return jx; - } - jx.min = -1; - if (p_val < 0) { - jx.value = (float)p_val / MAX_JOY_AXIS; - } - else { - jx.value = (float)p_val / (MAX_JOY_AXIS - 1); - } - if (p_negate) { - jx.value = -jx.value; - } - return jx; - } - jx.min = -1; - jx.value = (float)p_val / MAX_JOY_AXIS; - return jx; + InputDefault::JoyAxis jx; + if (Math::abs(p_val) < MIN_JOY_AXIS) { + jx.min = -1; + jx.value = 0.0f; + return jx; + } + if (p_xinput) { + + if (p_trigger) { + jx.min = 0; + jx.value = (float)p_val / MAX_TRIGGER; + return jx; + } + jx.min = -1; + if (p_val < 0) { + jx.value = (float)p_val / MAX_JOY_AXIS; + } + else { + jx.value = (float)p_val / (MAX_JOY_AXIS - 1); + } + if (p_negate) { + jx.value = -jx.value; + } + return jx; + } + jx.min = -1; + jx.value = (float)p_val / MAX_JOY_AXIS; + return jx; } void joystick_windows::load_xinput() { - xinput_get_state = &_xinput_get_state; - xinput_dll = LoadLibrary( "XInput1_4.dll" ); - if (!xinput_dll) { - xinput_dll = LoadLibrary("XInput1_3.dll"); - if (!xinput_dll) { - xinput_dll = LoadLibrary("XInput9_1_0.dll"); - } - } - - if (!xinput_dll) { - if (OS::get_singleton()->is_stdout_verbose()) { - print_line("Could not find XInput, using DirectInput only"); - } - return; - } - - XInputGetState_t func = (XInputGetState_t)GetProcAddress((HMODULE)xinput_dll, "XInputGetState"); - if (!func) { - unload_xinput(); - return; - } - xinput_get_state = func; - return; + xinput_get_state = &_xinput_get_state; + xinput_dll = LoadLibrary( "XInput1_4.dll" ); + if (!xinput_dll) { + xinput_dll = LoadLibrary("XInput1_3.dll"); + if (!xinput_dll) { + xinput_dll = LoadLibrary("XInput9_1_0.dll"); + } + } + + if (!xinput_dll) { + if (OS::get_singleton()->is_stdout_verbose()) { + print_line("Could not find XInput, using DirectInput only"); + } + return; + } + + XInputGetState_t func = (XInputGetState_t)GetProcAddress((HMODULE)xinput_dll, "XInputGetState"); + if (!func) { + unload_xinput(); + return; + } + xinput_get_state = func; + return; } void joystick_windows::unload_xinput() { - if (xinput_dll) { + if (xinput_dll) { - FreeLibrary((HMODULE)xinput_dll); - } + FreeLibrary((HMODULE)xinput_dll); + } } diff --git a/platform/windows/joystick.h b/platform/windows/joystick.h index d0d85cd2c9..4c706b3766 100644 --- a/platform/windows/joystick.h +++ b/platform/windows/joystick.h @@ -48,90 +48,90 @@ if(x != NULL) \ class joystick_windows { public: - joystick_windows(); - joystick_windows(InputDefault* _input, HWND* hwnd); - ~joystick_windows(); + joystick_windows(); + joystick_windows(InputDefault* _input, HWND* hwnd); + ~joystick_windows(); - void probe_joysticks(); - unsigned int process_joysticks(unsigned int p_last_id); + void probe_joysticks(); + unsigned int process_joysticks(unsigned int p_last_id); private: - enum { - JOYSTICKS_MAX = 16, - JOY_AXIS_COUNT = 6, - MIN_JOY_AXIS = 10, - MAX_JOY_AXIS = 32768, - MAX_JOY_BUTTONS = 128, - KEY_EVENT_BUFFER_SIZE = 512, - MAX_TRIGGER = 255 - }; - - struct dinput_gamepad { - - int id; - bool attached; - bool confirmed; - bool last_buttons[MAX_JOY_BUTTONS]; - DWORD last_pad; - - LPDIRECTINPUTDEVICE8 di_joy; - List<DWORD> joy_axis; - GUID guid; - - dinput_gamepad() { - id = -1; - last_pad = -1; - attached = false; - confirmed = false; - - for (int i = 0; i < MAX_JOY_BUTTONS; i++) - last_buttons[i] = false; - } - }; - - struct xinput_gamepad { - - int id; - bool attached; - DWORD last_packet; - XINPUT_STATE state; - - xinput_gamepad() { - attached = false; - last_packet = 0; - } - }; - - typedef DWORD (WINAPI *XInputGetState_t) (DWORD dwUserIndex, XINPUT_STATE* pState); - - HWND* hWnd; - HANDLE xinput_dll; - LPDIRECTINPUT8 dinput; - InputDefault* input; - - int id_to_change; - int joystick_count; - bool attached_joysticks[JOYSTICKS_MAX]; - dinput_gamepad d_joysticks[JOYSTICKS_MAX]; - xinput_gamepad x_joysticks[XUSER_MAX_COUNT]; - - static BOOL CALLBACK enumCallback(const DIDEVICEINSTANCE* p_instance, void* p_context); - static BOOL CALLBACK objectsCallback(const DIDEVICEOBJECTINSTANCE* instance, void* context); - - void setup_joystick_object(const DIDEVICEOBJECTINSTANCE* ob, int p_joy_id); - void close_joystick(int id = -1); - void load_xinput(); - void unload_xinput(); - - int check_free_joy_slot() const; - unsigned int post_hat(unsigned int p_last_id, int p_device, DWORD p_dpad); - - bool have_device(const GUID &p_guid); - bool is_xinput_device(const GUID* p_guid); - bool setup_dinput_joystick(const DIDEVICEINSTANCE* instance); - - InputDefault::JoyAxis axis_correct(int p_val, bool p_xinput = false, bool p_trigger = false, bool p_negate = false) const; - XInputGetState_t xinput_get_state; + enum { + JOYSTICKS_MAX = 16, + JOY_AXIS_COUNT = 6, + MIN_JOY_AXIS = 10, + MAX_JOY_AXIS = 32768, + MAX_JOY_BUTTONS = 128, + KEY_EVENT_BUFFER_SIZE = 512, + MAX_TRIGGER = 255 + }; + + struct dinput_gamepad { + + int id; + bool attached; + bool confirmed; + bool last_buttons[MAX_JOY_BUTTONS]; + DWORD last_pad; + + LPDIRECTINPUTDEVICE8 di_joy; + List<DWORD> joy_axis; + GUID guid; + + dinput_gamepad() { + id = -1; + last_pad = -1; + attached = false; + confirmed = false; + + for (int i = 0; i < MAX_JOY_BUTTONS; i++) + last_buttons[i] = false; + } + }; + + struct xinput_gamepad { + + int id; + bool attached; + DWORD last_packet; + XINPUT_STATE state; + + xinput_gamepad() { + attached = false; + last_packet = 0; + } + }; + + typedef DWORD (WINAPI *XInputGetState_t) (DWORD dwUserIndex, XINPUT_STATE* pState); + + HWND* hWnd; + HANDLE xinput_dll; + LPDIRECTINPUT8 dinput; + InputDefault* input; + + int id_to_change; + int joystick_count; + bool attached_joysticks[JOYSTICKS_MAX]; + dinput_gamepad d_joysticks[JOYSTICKS_MAX]; + xinput_gamepad x_joysticks[XUSER_MAX_COUNT]; + + static BOOL CALLBACK enumCallback(const DIDEVICEINSTANCE* p_instance, void* p_context); + static BOOL CALLBACK objectsCallback(const DIDEVICEOBJECTINSTANCE* instance, void* context); + + void setup_joystick_object(const DIDEVICEOBJECTINSTANCE* ob, int p_joy_id); + void close_joystick(int id = -1); + void load_xinput(); + void unload_xinput(); + + int check_free_joy_slot() const; + unsigned int post_hat(unsigned int p_last_id, int p_device, DWORD p_dpad); + + bool have_device(const GUID &p_guid); + bool is_xinput_device(const GUID* p_guid); + bool setup_dinput_joystick(const DIDEVICEINSTANCE* instance); + + InputDefault::JoyAxis axis_correct(int p_val, bool p_xinput = false, bool p_trigger = false, bool p_negate = false) const; + XInputGetState_t xinput_get_state; }; #endif diff --git a/platform/x11/joystick_linux.cpp b/platform/x11/joystick_linux.cpp index 3a3f7a57ba..cfc4f7cd2b 100644 --- a/platform/x11/joystick_linux.cpp +++ b/platform/x11/joystick_linux.cpp @@ -43,360 +43,360 @@ static const char* ignore_str = "/dev/input/js"; joystick_linux::Joystick::Joystick() { - fd = -1; - dpad = 0; + fd = -1; + dpad = 0; } void joystick_linux::Joystick::reset() { - num_buttons = 0; - num_axes = 0; - dpad = 0; - fd = -1; - for (int i=0; i < MAX_ABS; i++) { - abs_map[i] = -1; - } + num_buttons = 0; + num_axes = 0; + dpad = 0; + fd = -1; + for (int i=0; i < MAX_ABS; i++) { + abs_map[i] = -1; + } } joystick_linux::joystick_linux(InputDefault *in) { - exit_udev = false; - input = in; - joy_mutex = Mutex::create(); - joy_thread = Thread::create(joy_thread_func, this); + exit_udev = false; + input = in; + joy_mutex = Mutex::create(); + joy_thread = Thread::create(joy_thread_func, this); } joystick_linux::~joystick_linux() { - exit_udev = true; - Thread::wait_to_finish(joy_thread); - close_joystick(); + exit_udev = true; + Thread::wait_to_finish(joy_thread); + close_joystick(); } void joystick_linux::joy_thread_func(void *p_user) { - if (p_user) { - joystick_linux* joy = (joystick_linux*) p_user; - joy->run_joystick_thread(); - } - return; + if (p_user) { + joystick_linux* joy = (joystick_linux*) p_user; + joy->run_joystick_thread(); + } + return; } void joystick_linux::run_joystick_thread() { - udev *_udev = udev_new(); - ERR_FAIL_COND(!_udev); - enumerate_joysticks(_udev); - monitor_joysticks(_udev); - udev_unref(_udev); + udev *_udev = udev_new(); + ERR_FAIL_COND(!_udev); + enumerate_joysticks(_udev); + monitor_joysticks(_udev); + udev_unref(_udev); } void joystick_linux::enumerate_joysticks(udev *p_udev) { - udev_enumerate *enumerate; - udev_list_entry *devices, *dev_list_entry; - udev_device *dev; - - enumerate = udev_enumerate_new(p_udev); - udev_enumerate_add_match_subsystem(enumerate,"input"); - udev_enumerate_add_match_property(enumerate, "ID_INPUT_JOYSTICK", "1"); - - udev_enumerate_scan_devices(enumerate); - devices = udev_enumerate_get_list_entry(enumerate); - udev_list_entry_foreach(dev_list_entry, devices) { - - const char* path = udev_list_entry_get_name(dev_list_entry); - dev = udev_device_new_from_syspath(p_udev, path); - const char* devnode = udev_device_get_devnode(dev); - - if (devnode != NULL && strstr(devnode, ignore_str) == NULL) { - joy_mutex->lock(); - open_joystick(devnode); - joy_mutex->unlock(); - } - udev_device_unref(dev); - } - udev_enumerate_unref(enumerate); + udev_enumerate *enumerate; + udev_list_entry *devices, *dev_list_entry; + udev_device *dev; + + enumerate = udev_enumerate_new(p_udev); + udev_enumerate_add_match_subsystem(enumerate,"input"); + udev_enumerate_add_match_property(enumerate, "ID_INPUT_JOYSTICK", "1"); + + udev_enumerate_scan_devices(enumerate); + devices = udev_enumerate_get_list_entry(enumerate); + udev_list_entry_foreach(dev_list_entry, devices) { + + const char* path = udev_list_entry_get_name(dev_list_entry); + dev = udev_device_new_from_syspath(p_udev, path); + const char* devnode = udev_device_get_devnode(dev); + + if (devnode != NULL && strstr(devnode, ignore_str) == NULL) { + joy_mutex->lock(); + open_joystick(devnode); + joy_mutex->unlock(); + } + udev_device_unref(dev); + } + udev_enumerate_unref(enumerate); } void joystick_linux::monitor_joysticks(udev *p_udev) { - udev_device *dev = NULL; - udev_monitor *mon = udev_monitor_new_from_netlink(p_udev, "udev"); - udev_monitor_filter_add_match_subsystem_devtype(mon, "input", NULL); - udev_monitor_enable_receiving(mon); - int fd = udev_monitor_get_fd(mon); + udev_device *dev = NULL; + udev_monitor *mon = udev_monitor_new_from_netlink(p_udev, "udev"); + udev_monitor_filter_add_match_subsystem_devtype(mon, "input", NULL); + udev_monitor_enable_receiving(mon); + int fd = udev_monitor_get_fd(mon); - while (!exit_udev) { + while (!exit_udev) { - fd_set fds; - struct timeval tv; - int ret; + fd_set fds; + struct timeval tv; + int ret; - FD_ZERO(&fds); - FD_SET(fd, &fds); - tv.tv_sec = 0; - tv.tv_usec = 0; + FD_ZERO(&fds); + FD_SET(fd, &fds); + tv.tv_sec = 0; + tv.tv_usec = 0; - ret = select(fd+1, &fds, NULL, NULL, &tv); + ret = select(fd+1, &fds, NULL, NULL, &tv); - /* Check if our file descriptor has received data. */ - if (ret > 0 && FD_ISSET(fd, &fds)) { - /* Make the call to receive the device. - select() ensured that this will not block. */ - dev = udev_monitor_receive_device(mon); + /* Check if our file descriptor has received data. */ + if (ret > 0 && FD_ISSET(fd, &fds)) { + /* Make the call to receive the device. + select() ensured that this will not block. */ + dev = udev_monitor_receive_device(mon); - if (dev && udev_device_get_devnode(dev) != 0) { + if (dev && udev_device_get_devnode(dev) != 0) { - joy_mutex->lock(); - const char* action = udev_device_get_action(dev); - const char* devnode = udev_device_get_devnode(dev); + joy_mutex->lock(); + const char* action = udev_device_get_action(dev); + const char* devnode = udev_device_get_devnode(dev); - if (strstr(devnode, ignore_str) == NULL) { + if (strstr(devnode, ignore_str) == NULL) { - if (strcmp(action, "add") == 0) - open_joystick(devnode); + if (strcmp(action, "add") == 0) + open_joystick(devnode); - else if (strcmp(action, "remove") == 0) - close_joystick(get_joy_from_path(devnode)); - } + else if (strcmp(action, "remove") == 0) + close_joystick(get_joy_from_path(devnode)); + } - udev_device_unref(dev); - joy_mutex->unlock(); - } - } - usleep(50000); - } - //printf("exit udev\n"); - udev_monitor_unref(mon); + udev_device_unref(dev); + joy_mutex->unlock(); + } + } + usleep(50000); + } + //printf("exit udev\n"); + udev_monitor_unref(mon); } int joystick_linux::get_free_joy_slot() const { - for (int i = 0; i < JOYSTICKS_MAX; i++) { + for (int i = 0; i < JOYSTICKS_MAX; i++) { - if (joysticks[i].fd == -1) return i; - } - return -1; + if (joysticks[i].fd == -1) return i; + } + return -1; } int joystick_linux::get_joy_from_path(String p_path) const { - for (int i = 0; i < JOYSTICKS_MAX; i++) { + for (int i = 0; i < JOYSTICKS_MAX; i++) { - if (joysticks[i].devpath == p_path) { - return i; - } - } - return -2; + if (joysticks[i].devpath == p_path) { + return i; + } + } + return -2; } void joystick_linux::close_joystick(int p_id) { - if (p_id == -1) { - for (int i=0; i<JOYSTICKS_MAX; i++) { + if (p_id == -1) { + for (int i=0; i<JOYSTICKS_MAX; i++) { - close_joystick(i); - }; - return; - } - else if (p_id < 0) return; + close_joystick(i); + }; + return; + } + else if (p_id < 0) return; - Joystick &joy = joysticks[p_id]; + Joystick &joy = joysticks[p_id]; - if (joy.fd != -1) { + if (joy.fd != -1) { - libevdev_free(joy.dev); - close(joy.fd); - joy.fd = -1; - input->joy_connection_changed(p_id, false, ""); - }; + libevdev_free(joy.dev); + close(joy.fd); + joy.fd = -1; + input->joy_connection_changed(p_id, false, ""); + }; }; static String _hex_str(uint8_t p_byte) { - static const char* dict = "0123456789abcdef"; - char ret[3]; - ret[2] = 0; + static const char* dict = "0123456789abcdef"; + char ret[3]; + ret[2] = 0; - ret[0] = dict[p_byte>>4]; - ret[1] = dict[p_byte & 0xF]; + ret[0] = dict[p_byte>>4]; + ret[1] = dict[p_byte & 0xF]; - return ret; + return ret; }; void joystick_linux::setup_joystick_properties(int p_id) { - Joystick* joy = &joysticks[p_id]; + Joystick* joy = &joysticks[p_id]; - libevdev* dev = joy->dev; - for (int i = BTN_JOYSTICK; i < KEY_MAX; ++i) { + libevdev* dev = joy->dev; + for (int i = BTN_JOYSTICK; i < KEY_MAX; ++i) { - if (libevdev_has_event_code(dev, EV_KEY, i)) { + if (libevdev_has_event_code(dev, EV_KEY, i)) { - joy->key_map[i] = joy->num_buttons++; - } - } - for (int i = BTN_MISC; i < BTN_JOYSTICK; ++i) { + joy->key_map[i] = joy->num_buttons++; + } + } + for (int i = BTN_MISC; i < BTN_JOYSTICK; ++i) { - if (libevdev_has_event_code(dev, EV_KEY, i)) { + if (libevdev_has_event_code(dev, EV_KEY, i)) { - joy->key_map[i] = joy->num_buttons++; - } - } - for (int i = 0; i < ABS_MISC; ++i) { - /* Skip hats */ - if (i == ABS_HAT0X) { - i = ABS_HAT3Y; - continue; - } - if (libevdev_has_event_code(dev, EV_ABS, i)) { + joy->key_map[i] = joy->num_buttons++; + } + } + for (int i = 0; i < ABS_MISC; ++i) { + /* Skip hats */ + if (i == ABS_HAT0X) { + i = ABS_HAT3Y; + continue; + } + if (libevdev_has_event_code(dev, EV_ABS, i)) { - joy->abs_map[i] = joy->num_axes++; - } - } + joy->abs_map[i] = joy->num_axes++; + } + } } void joystick_linux::open_joystick(const char *p_path) { - int joy_num = get_free_joy_slot(); - int fd = open(p_path, O_RDONLY | O_NONBLOCK); - if (fd != -1 && joy_num != -1) { - - int rc = libevdev_new_from_fd(fd, &joysticks[joy_num].dev); - if (rc < 0) { - - fprintf(stderr, "Failed to init libevdev (%s)\n", strerror(-rc)); - return; - } - - libevdev *dev = joysticks[joy_num].dev; - - //check if the device supports basic gamepad events, prevents certain keyboards from - //being detected as joysticks - if (libevdev_has_event_type(dev, EV_ABS) && libevdev_has_event_type(dev, EV_KEY) && - (libevdev_has_event_code(dev, EV_KEY, BTN_A) || libevdev_has_event_code(dev, EV_KEY, BTN_THUMBL) || libevdev_has_event_code(dev, EV_KEY, BTN_TOP))) { - - char uid[128]; - String name = libevdev_get_name(dev); - uint16_t bus = __bswap_16(libevdev_get_id_bustype(dev)); - uint16_t vendor = __bswap_16(libevdev_get_id_vendor(dev)); - uint16_t product = __bswap_16(libevdev_get_id_product(dev)); - uint16_t version = __bswap_16(libevdev_get_id_version(dev)); - - joysticks[joy_num].reset(); - - Joystick &joy = joysticks[joy_num]; - joy.fd = fd; - joy.devpath = String(p_path); - setup_joystick_properties(joy_num); - sprintf(uid, "%04x%04x", bus, 0); - if (vendor && product && version) { - - sprintf(uid + String(uid).length(), "%04x%04x%04x%04x%04x%04x", vendor,0,product,0,version,0); - input->joy_connection_changed(joy_num, true, name, uid); - } - else { - String uidname = uid; - int uidlen = MIN(name.length(), 11); - for (int i=0; i<uidlen; i++) { - - uidname = uidname + _hex_str(name[i]); - } - uidname += "00"; - input->joy_connection_changed(joy_num, true, name, uidname); - - } - } - else { - //device is not a gamepad, clean up - libevdev_free(dev); - close(fd); - } - } + int joy_num = get_free_joy_slot(); + int fd = open(p_path, O_RDONLY | O_NONBLOCK); + if (fd != -1 && joy_num != -1) { + + int rc = libevdev_new_from_fd(fd, &joysticks[joy_num].dev); + if (rc < 0) { + + fprintf(stderr, "Failed to init libevdev (%s)\n", strerror(-rc)); + return; + } + + libevdev *dev = joysticks[joy_num].dev; + + //check if the device supports basic gamepad events, prevents certain keyboards from + //being detected as joysticks + if (libevdev_has_event_type(dev, EV_ABS) && libevdev_has_event_type(dev, EV_KEY) && + (libevdev_has_event_code(dev, EV_KEY, BTN_A) || libevdev_has_event_code(dev, EV_KEY, BTN_THUMBL) || libevdev_has_event_code(dev, EV_KEY, BTN_TOP))) { + + char uid[128]; + String name = libevdev_get_name(dev); + uint16_t bus = __bswap_16(libevdev_get_id_bustype(dev)); + uint16_t vendor = __bswap_16(libevdev_get_id_vendor(dev)); + uint16_t product = __bswap_16(libevdev_get_id_product(dev)); + uint16_t version = __bswap_16(libevdev_get_id_version(dev)); + + joysticks[joy_num].reset(); + + Joystick &joy = joysticks[joy_num]; + joy.fd = fd; + joy.devpath = String(p_path); + setup_joystick_properties(joy_num); + sprintf(uid, "%04x%04x", bus, 0); + if (vendor && product && version) { + + sprintf(uid + String(uid).length(), "%04x%04x%04x%04x%04x%04x", vendor,0,product,0,version,0); + input->joy_connection_changed(joy_num, true, name, uid); + } + else { + String uidname = uid; + int uidlen = MIN(name.length(), 11); + for (int i=0; i<uidlen; i++) { + + uidname = uidname + _hex_str(name[i]); + } + uidname += "00"; + input->joy_connection_changed(joy_num, true, name, uidname); + + } + } + else { + //device is not a gamepad, clean up + libevdev_free(dev); + close(fd); + } + } } InputDefault::JoyAxis joystick_linux::axis_correct(const input_absinfo *p_abs, int p_value) const { - int min = p_abs->minimum; - int max = p_abs->maximum; - InputDefault::JoyAxis jx; - - if (min < 0) { - jx.min = -1; - if (p_value < 0) { - jx.value = (float) -p_value / min; - } - jx.value = (float) p_value / max; - } - if (min == 0) { - jx.min = 0; - jx.value = 0.0f + (float) p_value / max; - } - return jx; + int min = p_abs->minimum; + int max = p_abs->maximum; + InputDefault::JoyAxis jx; + + if (min < 0) { + jx.min = -1; + if (p_value < 0) { + jx.value = (float) -p_value / min; + } + jx.value = (float) p_value / max; + } + if (min == 0) { + jx.min = 0; + jx.value = 0.0f + (float) p_value / max; + } + return jx; } uint32_t joystick_linux::process_joysticks(uint32_t p_event_id) { - if (joy_mutex->try_lock() != OK) { - return p_event_id; - } - for (int i=0; i<JOYSTICKS_MAX; i++) { - - if (joysticks[i].fd == -1) continue; - - input_event ev; - Joystick* joy = &joysticks[i]; - libevdev* dev = joy->dev; - int rc = 1; - - rc = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, &ev); - - if (rc < 0 && rc != -EAGAIN) { - continue; - } - - while (rc == LIBEVDEV_READ_STATUS_SYNC || rc == LIBEVDEV_READ_STATUS_SUCCESS) { - - switch (ev.type) { - case EV_KEY: - p_event_id = input->joy_button(p_event_id, i, joy->key_map[ev.code], ev.value); - break; - - case EV_ABS: - - switch (ev.code) { - case ABS_HAT0X: - if (ev.value != 0) { - if (ev.value < 0) joy->dpad |= InputDefault::HAT_MASK_LEFT; - else joy->dpad |= InputDefault::HAT_MASK_RIGHT; - - } - else joy->dpad &= ~(InputDefault::HAT_MASK_LEFT | InputDefault::HAT_MASK_RIGHT); - - p_event_id = input->joy_hat(p_event_id, i, joy->dpad); - break; - - case ABS_HAT0Y: - if (ev.value != 0) { - if (ev.value < 0) joy->dpad |= InputDefault::HAT_MASK_UP; - else joy->dpad |= InputDefault::HAT_MASK_DOWN; - } - else joy->dpad &= ~(InputDefault::HAT_MASK_UP | InputDefault::HAT_MASK_DOWN); - - p_event_id = input->joy_hat(p_event_id, i, joy->dpad); - break; - - default: - if (joy->abs_map[ev.code] != -1) { - InputDefault::JoyAxis value = axis_correct(libevdev_get_abs_info(dev, ev.code), ev.value); - p_event_id = input->joy_axis(p_event_id, i, joy->abs_map[ev.code], value); - } - break; - } - break; - } - rc = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, &ev); - } - } - joy_mutex->unlock(); - return p_event_id; + if (joy_mutex->try_lock() != OK) { + return p_event_id; + } + for (int i=0; i<JOYSTICKS_MAX; i++) { + + if (joysticks[i].fd == -1) continue; + + input_event ev; + Joystick* joy = &joysticks[i]; + libevdev* dev = joy->dev; + int rc = 1; + + rc = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, &ev); + + if (rc < 0 && rc != -EAGAIN) { + continue; + } + + while (rc == LIBEVDEV_READ_STATUS_SYNC || rc == LIBEVDEV_READ_STATUS_SUCCESS) { + + switch (ev.type) { + case EV_KEY: + p_event_id = input->joy_button(p_event_id, i, joy->key_map[ev.code], ev.value); + break; + + case EV_ABS: + + switch (ev.code) { + case ABS_HAT0X: + if (ev.value != 0) { + if (ev.value < 0) joy->dpad |= InputDefault::HAT_MASK_LEFT; + else joy->dpad |= InputDefault::HAT_MASK_RIGHT; + + } + else joy->dpad &= ~(InputDefault::HAT_MASK_LEFT | InputDefault::HAT_MASK_RIGHT); + + p_event_id = input->joy_hat(p_event_id, i, joy->dpad); + break; + + case ABS_HAT0Y: + if (ev.value != 0) { + if (ev.value < 0) joy->dpad |= InputDefault::HAT_MASK_UP; + else joy->dpad |= InputDefault::HAT_MASK_DOWN; + } + else joy->dpad &= ~(InputDefault::HAT_MASK_UP | InputDefault::HAT_MASK_DOWN); + + p_event_id = input->joy_hat(p_event_id, i, joy->dpad); + break; + + default: + if (joy->abs_map[ev.code] != -1) { + InputDefault::JoyAxis value = axis_correct(libevdev_get_abs_info(dev, ev.code), ev.value); + p_event_id = input->joy_axis(p_event_id, i, joy->abs_map[ev.code], value); + } + break; + } + break; + } + rc = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, &ev); + } + } + joy_mutex->unlock(); + return p_event_id; } #endif diff --git a/platform/x11/joystick_linux.h b/platform/x11/joystick_linux.h index 3a4ec5219d..339a5f239d 100644 --- a/platform/x11/joystick_linux.h +++ b/platform/x11/joystick_linux.h @@ -40,53 +40,53 @@ struct input_absinfo; class joystick_linux { public: - joystick_linux(InputDefault *in); - ~joystick_linux(); - uint32_t process_joysticks(uint32_t p_event_id); + joystick_linux(InputDefault *in); + ~joystick_linux(); + uint32_t process_joysticks(uint32_t p_event_id); private: - enum { - JOYSTICKS_MAX = 16, - MAX_ABS = 63, - MAX_KEY = 767, // Hack because <linux/input.h> can't be included here - BT_MISC = 256, - HAT_MAX = 4, - }; + enum { + JOYSTICKS_MAX = 16, + MAX_ABS = 63, + MAX_KEY = 767, // Hack because <linux/input.h> can't be included here + BT_MISC = 256, + HAT_MAX = 4, + }; - struct Joystick { - int key_map[MAX_KEY - BT_MISC]; - int abs_map[MAX_ABS]; - int num_buttons; - int num_axes; - int dpad; - int fd; + struct Joystick { + int key_map[MAX_KEY - BT_MISC]; + int abs_map[MAX_ABS]; + int num_buttons; + int num_axes; + int dpad; + int fd; - String devpath; - struct libevdev *dev; + String devpath; + struct libevdev *dev; - Joystick(); - void reset(); - }; + Joystick(); + void reset(); + }; - bool exit_udev; - Mutex *joy_mutex; - Thread *joy_thread; - InputDefault *input; - Joystick joysticks[JOYSTICKS_MAX]; + bool exit_udev; + Mutex *joy_mutex; + Thread *joy_thread; + InputDefault *input; + Joystick joysticks[JOYSTICKS_MAX]; - static void joy_thread_func(void *p_user); + static void joy_thread_func(void *p_user); - int get_joy_from_path(String path) const; - int get_free_joy_slot() const; + int get_joy_from_path(String path) const; + int get_free_joy_slot() const; - void setup_joystick_properties(int p_id); - void close_joystick(int p_id = -1); - void enumerate_joysticks(struct udev *_udev); - void monitor_joysticks(struct udev *_udev); - void run_joystick_thread(); - void open_joystick(const char* path); + void setup_joystick_properties(int p_id); + void close_joystick(int p_id = -1); + void enumerate_joysticks(struct udev *_udev); + void monitor_joysticks(struct udev *_udev); + void run_joystick_thread(); + void open_joystick(const char* path); - InputDefault::JoyAxis axis_correct(const input_absinfo *abs, int value) const; + InputDefault::JoyAxis axis_correct(const input_absinfo *abs, int value) const; }; #endif |