winmm_joystick.c
Go to the documentation of this file.
1 //========================================================================
2 // GLFW 3.1 WinMM - www.glfw.org
3 //------------------------------------------------------------------------
4 // Copyright (c) 2002-2006 Marcus Geelnard
5 // Copyright (c) 2006-2010 Camilla Berglund <elmindreda@elmindreda.org>
6 //
7 // This software is provided 'as-is', without any express or implied
8 // warranty. In no event will the authors be held liable for any damages
9 // arising from the use of this software.
10 //
11 // Permission is granted to anyone to use this software for any purpose,
12 // including commercial applications, and to alter it and redistribute it
13 // freely, subject to the following restrictions:
14 //
15 // 1. The origin of this software must not be misrepresented; you must not
16 // claim that you wrote the original software. If you use this software
17 // in a product, an acknowledgment in the product documentation would
18 // be appreciated but is not required.
19 //
20 // 2. Altered source versions must be plainly marked as such, and must not
21 // be misrepresented as being the original software.
22 //
23 // 3. This notice may not be removed or altered from any source
24 // distribution.
25 //
26 //========================================================================
27 
28 #include "internal.h"
29 
30 #include <stdlib.h>
31 
32 
36 
37 // Convert axis value to the [-1,1] range
38 //
39 static float normalizeAxis(DWORD pos, DWORD min, DWORD max)
40 {
41  float fpos = (float) pos;
42  float fmin = (float) min;
43  float fmax = (float) max;
44 
45  return (2.f * (fpos - fmin) / (fmax - fmin)) - 1.f;
46 }
47 
48 
52 
53 // Initialize joystick interface
54 //
56 {
57 }
58 
59 // Close all opened joystick handles
60 //
62 {
63  int i;
64 
65  for (i = 0; i < GLFW_JOYSTICK_LAST; i++)
66  free(_glfw.winmm_js[i].name);
67 }
68 
69 
73 
75 {
76  JOYINFO ji;
77 
78  if (_glfw_joyGetPos(joy, &ji) != JOYERR_NOERROR)
79  return GL_FALSE;
80 
81  return GL_TRUE;
82 }
83 
84 const float* _glfwPlatformGetJoystickAxes(int joy, int* count)
85 {
86  JOYCAPS jc;
87  JOYINFOEX ji;
88  float* axes = _glfw.winmm_js[joy].axes;
89 
90  if (_glfw_joyGetDevCaps(joy, &jc, sizeof(JOYCAPS)) != JOYERR_NOERROR)
91  return NULL;
92 
93  ji.dwSize = sizeof(JOYINFOEX);
94  ji.dwFlags = JOY_RETURNX | JOY_RETURNY | JOY_RETURNZ |
95  JOY_RETURNR | JOY_RETURNU | JOY_RETURNV;
96  if (_glfw_joyGetPosEx(joy, &ji) != JOYERR_NOERROR)
97  return NULL;
98 
99  axes[(*count)++] = normalizeAxis(ji.dwXpos, jc.wXmin, jc.wXmax);
100  axes[(*count)++] = normalizeAxis(ji.dwYpos, jc.wYmin, jc.wYmax);
101 
102  if (jc.wCaps & JOYCAPS_HASZ)
103  axes[(*count)++] = normalizeAxis(ji.dwZpos, jc.wZmin, jc.wZmax);
104 
105  if (jc.wCaps & JOYCAPS_HASR)
106  axes[(*count)++] = normalizeAxis(ji.dwRpos, jc.wRmin, jc.wRmax);
107 
108  if (jc.wCaps & JOYCAPS_HASU)
109  axes[(*count)++] = normalizeAxis(ji.dwUpos, jc.wUmin, jc.wUmax);
110 
111  if (jc.wCaps & JOYCAPS_HASV)
112  axes[(*count)++] = normalizeAxis(ji.dwVpos, jc.wVmin, jc.wVmax);
113 
114  return axes;
115 }
116 
117 const unsigned char* _glfwPlatformGetJoystickButtons(int joy, int* count)
118 {
119  JOYCAPS jc;
120  JOYINFOEX ji;
121  unsigned char* buttons = _glfw.winmm_js[joy].buttons;
122 
123  if (_glfw_joyGetDevCaps(joy, &jc, sizeof(JOYCAPS)) != JOYERR_NOERROR)
124  return NULL;
125 
126  ji.dwSize = sizeof(JOYINFOEX);
127  ji.dwFlags = JOY_RETURNBUTTONS | JOY_RETURNPOV;
128  if (_glfw_joyGetPosEx(joy, &ji) != JOYERR_NOERROR)
129  return NULL;
130 
131  while (*count < (int) jc.wNumButtons)
132  {
133  buttons[*count] = (unsigned char)
134  (ji.dwButtons & (1UL << *count) ? GLFW_PRESS : GLFW_RELEASE);
135  (*count)++;
136  }
137 
138  // Virtual buttons - Inject data from hats
139  // Each hat is exposed as 4 buttons which exposes 8 directions with
140  // concurrent button presses
141  // NOTE: this API exposes only one hat
142 
143  if ((jc.wCaps & JOYCAPS_HASPOV) && (jc.wCaps & JOYCAPS_POV4DIR))
144  {
145  int i, value = ji.dwPOV / 100 / 45;
146 
147  // Bit fields of button presses for each direction, including nil
148  const int directions[9] = { 1, 3, 2, 6, 4, 12, 8, 9, 0 };
149 
150  if (value < 0 || value > 8)
151  value = 8;
152 
153  for (i = 0; i < 4; i++)
154  {
155  if (directions[value] & (1 << i))
156  buttons[(*count)++] = GLFW_PRESS;
157  else
158  buttons[(*count)++] = GLFW_RELEASE;
159  }
160  }
161 
162  return buttons;
163 }
164 
165 const char* _glfwPlatformGetJoystickName(int joy)
166 {
167  JOYCAPS jc;
168 
169  if (_glfw_joyGetDevCaps(joy, &jc, sizeof(JOYCAPS)) != JOYERR_NOERROR)
170  return NULL;
171 
172  free(_glfw.winmm_js[joy].name);
173  _glfw.winmm_js[joy].name = _glfwCreateUTF8FromWideString(jc.szPname);
174 
175  return _glfw.winmm_js[joy].name;
176 }
177 
#define _glfw_joyGetPosEx
#define _glfw_joyGetDevCaps
const float * _glfwPlatformGetJoystickAxes(int joy, int *count)
Returns the values of all axes of the specified joystick.
_GLFWlibrary _glfw
All global data protected by _glfwInitialized. This should only be touched after a call to glfwInit t...
#define GLFW_RELEASE
The key or mouse button was released.
Definition: glfw3.h:225
const unsigned char * _glfwPlatformGetJoystickButtons(int joy, int *count)
Returns the state of all buttons of the specified joystick.
void _glfwInitJoysticks(void)
GLuint GLuint GLsizei count
Definition: glext.h:111
GLfloat f
Definition: glext.h:1868
#define GLFW_JOYSTICK_LAST
Definition: glfw3.h:459
void _glfwTerminateJoysticks(void)
int _glfwPlatformJoystickPresent(int joy)
Returns whether the specified joystick is present.
GLsizei const GLfloat * value
Definition: glext.h:693
const char * _glfwPlatformGetJoystickName(int joy)
Returns the name of the specified joystick.
static float normalizeAxis(DWORD pos, DWORD min, DWORD max)
char * _glfwCreateUTF8FromWideString(const WCHAR *source)
Definition: win32_init.c:299
#define GLFW_PRESS
The key or mouse button was pressed.
Definition: glfw3.h:232
#define _glfw_joyGetPos


librealsense
Author(s): Sergey Dorodnicov , Mark Horn , Reagan Lopez
autogenerated on Fri Mar 13 2020 03:16:18