glfw/src/win32_joystick.c

178 lines
5.3 KiB
C
Raw Normal View History

2010-09-07 17:34:51 +02:00
//========================================================================
// GLFW 3.0 Win32 - www.glfw.org
2010-09-07 17:34:51 +02:00
//------------------------------------------------------------------------
// Copyright (c) 2002-2006 Marcus Geelnard
// Copyright (c) 2006-2010 Camilla Berglund <elmindreda@elmindreda.org>
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would
// be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
//========================================================================
#include "internal.h"
2012-12-12 13:59:53 +01:00
#include <stdlib.h>
2010-09-07 17:34:51 +02:00
2010-09-10 22:03:36 +02:00
//////////////////////////////////////////////////////////////////////////
////// GLFW internal API //////
//////////////////////////////////////////////////////////////////////////
2010-09-07 17:34:51 +02:00
2010-09-13 23:50:04 +02:00
// Calculate normalized joystick position
2013-02-04 13:22:10 +01:00
//
2010-09-10 22:03:36 +02:00
static float calcJoystickPos(DWORD pos, DWORD min, DWORD max)
2010-09-07 17:34:51 +02:00
{
float fpos = (float) pos;
float fmin = (float) min;
float fmax = (float) max;
2010-09-10 22:03:36 +02:00
return (2.f * (fpos - fmin) / (fmax - fmin)) - 1.f;
}
2010-09-07 17:34:51 +02:00
2013-01-02 01:52:28 +01:00
//////////////////////////////////////////////////////////////////////////
////// GLFW internal API //////
//////////////////////////////////////////////////////////////////////////
// Initialize joystick interface
2013-02-04 13:22:10 +01:00
//
2013-01-02 01:52:28 +01:00
void _glfwInitJoysticks(void)
{
}
// Close all opened joystick handles
2013-02-04 13:22:10 +01:00
//
2013-01-02 01:52:28 +01:00
void _glfwTerminateJoysticks(void)
{
int i;
for (i = 0; i < GLFW_JOYSTICK_LAST; i++)
free(_glfw.win32.joystick[i].name);
}
2010-09-10 22:03:36 +02:00
//////////////////////////////////////////////////////////////////////////
////// GLFW platform API //////
//////////////////////////////////////////////////////////////////////////
2010-09-07 17:34:51 +02:00
2013-04-24 19:25:42 +02:00
int _glfwPlatformJoystickPresent(int joy)
2010-09-07 17:34:51 +02:00
{
2013-04-24 19:25:42 +02:00
JOYINFO ji;
2010-09-07 17:34:51 +02:00
2013-04-24 19:25:42 +02:00
if (_glfw_joyGetPos(joy, &ji) != JOYERR_NOERROR)
return GL_FALSE;
2010-09-07 17:34:51 +02:00
2013-04-24 19:25:42 +02:00
return GL_TRUE;
2010-09-07 17:34:51 +02:00
}
2013-06-04 18:20:38 +02:00
const float* _glfwPlatformGetJoystickAxes(int joy, int* count)
2010-09-07 17:34:51 +02:00
{
2010-09-10 22:03:36 +02:00
JOYCAPS jc;
2010-09-07 17:34:51 +02:00
JOYINFOEX ji;
2013-04-24 19:25:42 +02:00
float* axes = _glfw.win32.joystick[joy].axes;
2010-09-07 17:34:51 +02:00
2013-04-24 19:25:42 +02:00
if (_glfw_joyGetDevCaps(joy, &jc, sizeof(JOYCAPS)) != JOYERR_NOERROR)
return NULL;
2010-09-07 17:34:51 +02:00
2010-09-10 22:03:36 +02:00
ji.dwSize = sizeof(JOYINFOEX);
2010-09-07 17:34:51 +02:00
ji.dwFlags = JOY_RETURNX | JOY_RETURNY | JOY_RETURNZ |
JOY_RETURNR | JOY_RETURNU | JOY_RETURNV;
2013-04-24 19:25:42 +02:00
if (_glfw_joyGetPosEx(joy, &ji) != JOYERR_NOERROR)
return NULL;
2010-09-10 22:03:36 +02:00
2013-04-24 19:25:42 +02:00
axes[(*count)++] = calcJoystickPos(ji.dwXpos, jc.wXmin, jc.wXmax);
axes[(*count)++] = -calcJoystickPos(ji.dwYpos, jc.wYmin, jc.wYmax);
2010-09-10 22:03:36 +02:00
2013-04-24 19:25:42 +02:00
if (jc.wCaps & JOYCAPS_HASZ)
axes[(*count)++] = calcJoystickPos(ji.dwZpos, jc.wZmin, jc.wZmax);
2010-09-10 22:03:36 +02:00
2013-04-24 19:25:42 +02:00
if (jc.wCaps & JOYCAPS_HASR)
axes[(*count)++] = calcJoystickPos(ji.dwRpos, jc.wRmin, jc.wRmax);
2010-09-10 22:03:36 +02:00
2013-04-24 19:25:42 +02:00
if (jc.wCaps & JOYCAPS_HASU)
axes[(*count)++] = calcJoystickPos(ji.dwUpos, jc.wUmin, jc.wUmax);
2010-09-10 22:03:36 +02:00
2013-04-24 19:25:42 +02:00
if (jc.wCaps & JOYCAPS_HASV)
axes[(*count)++] = -calcJoystickPos(ji.dwVpos, jc.wVmin, jc.wVmax);
2010-09-07 17:34:51 +02:00
2013-04-24 19:25:42 +02:00
return axes;
2010-09-07 17:34:51 +02:00
}
2013-06-04 18:20:38 +02:00
const unsigned char* _glfwPlatformGetJoystickButtons(int joy, int* count)
2010-09-07 17:34:51 +02:00
{
2010-09-10 22:03:36 +02:00
JOYCAPS jc;
2010-09-07 17:34:51 +02:00
JOYINFOEX ji;
2013-04-24 19:25:42 +02:00
unsigned char* buttons = _glfw.win32.joystick[joy].buttons;
2012-04-17 17:55:11 +02:00
2013-04-24 19:25:42 +02:00
if (_glfw_joyGetDevCaps(joy, &jc, sizeof(JOYCAPS)) != JOYERR_NOERROR)
return NULL;
2010-09-07 17:34:51 +02:00
2010-09-10 22:03:36 +02:00
ji.dwSize = sizeof(JOYINFOEX);
ji.dwFlags = JOY_RETURNBUTTONS | JOY_RETURNPOV;
2013-04-24 19:25:42 +02:00
if (_glfw_joyGetPosEx(joy, &ji) != JOYERR_NOERROR)
return NULL;
2010-09-07 17:34:51 +02:00
2013-06-12 20:36:43 +02:00
while (*count < (int) jc.wNumButtons)
2010-09-07 17:34:51 +02:00
{
2013-04-24 19:25:42 +02:00
buttons[*count] = (unsigned char)
(ji.dwButtons & (1UL << *count) ? GLFW_PRESS : GLFW_RELEASE);
(*count)++;
2010-09-07 17:34:51 +02:00
}
// Virtual buttons - Inject data from hats
2012-04-17 17:55:11 +02:00
// Each hat is exposed as 4 buttons which exposes 8 directions with
// concurrent button presses
// NOTE: this API exposes only one hat
2013-04-24 19:25:42 +02:00
if ((jc.wCaps & JOYCAPS_HASPOV) && (jc.wCaps & JOYCAPS_POV4DIR))
{
2013-04-24 19:25:42 +02:00
int i, value = ji.dwPOV / 100 / 45;
// Bit fields of button presses for each direction, including nil
const int directions[9] = { 1, 3, 2, 6, 4, 12, 8, 9, 0 };
2012-11-08 16:06:23 +01:00
if (value < 0 || value > 8)
value = 8;
2013-04-24 19:25:42 +02:00
for (i = 0; i < 4; i++)
{
2013-04-24 19:25:42 +02:00
if (directions[value] & (1 << i))
buttons[(*count)++] = GLFW_PRESS;
2012-11-08 16:06:23 +01:00
else
2013-04-24 19:25:42 +02:00
buttons[(*count)++] = GLFW_RELEASE;
}
}
2013-04-24 19:25:42 +02:00
return buttons;
2010-09-07 17:34:51 +02:00
}
const char* _glfwPlatformGetJoystickName(int joy)
{
JOYCAPS jc;
2013-04-24 19:25:42 +02:00
if (_glfw_joyGetDevCaps(joy, &jc, sizeof(JOYCAPS)) != JOYERR_NOERROR)
return NULL;
2013-04-24 19:25:42 +02:00
free(_glfw.win32.joystick[joy].name);
_glfw.win32.joystick[joy].name = _glfwCreateUTF8FromWideString(jc.szPname);
2013-04-24 19:25:42 +02:00
return _glfw.win32.joystick[joy].name;
}