winmm_joystick.c
Go to the documentation of this file.
00001 //========================================================================
00002 // GLFW 3.1 WinMM - www.glfw.org
00003 //------------------------------------------------------------------------
00004 // Copyright (c) 2002-2006 Marcus Geelnard
00005 // Copyright (c) 2006-2010 Camilla Berglund <elmindreda@elmindreda.org>
00006 //
00007 // This software is provided 'as-is', without any express or implied
00008 // warranty. In no event will the authors be held liable for any damages
00009 // arising from the use of this software.
00010 //
00011 // Permission is granted to anyone to use this software for any purpose,
00012 // including commercial applications, and to alter it and redistribute it
00013 // freely, subject to the following restrictions:
00014 //
00015 // 1. The origin of this software must not be misrepresented; you must not
00016 //    claim that you wrote the original software. If you use this software
00017 //    in a product, an acknowledgment in the product documentation would
00018 //    be appreciated but is not required.
00019 //
00020 // 2. Altered source versions must be plainly marked as such, and must not
00021 //    be misrepresented as being the original software.
00022 //
00023 // 3. This notice may not be removed or altered from any source
00024 //    distribution.
00025 //
00026 //========================================================================
00027 
00028 #include "internal.h"
00029 
00030 #include <stdlib.h>
00031 
00032 
00036 
00037 // Convert axis value to the [-1,1] range
00038 //
00039 static float normalizeAxis(DWORD pos, DWORD min, DWORD max)
00040 {
00041     float fpos = (float) pos;
00042     float fmin = (float) min;
00043     float fmax = (float) max;
00044 
00045     return (2.f * (fpos - fmin) / (fmax - fmin)) - 1.f;
00046 }
00047 
00048 
00052 
00053 // Initialize joystick interface
00054 //
00055 void _glfwInitJoysticks(void)
00056 {
00057 }
00058 
00059 // Close all opened joystick handles
00060 //
00061 void _glfwTerminateJoysticks(void)
00062 {
00063     int i;
00064 
00065     for (i = 0;  i < GLFW_JOYSTICK_LAST;  i++)
00066         free(_glfw.winmm_js[i].name);
00067 }
00068 
00069 
00073 
00074 int _glfwPlatformJoystickPresent(int joy)
00075 {
00076     JOYINFO ji;
00077 
00078     if (_glfw_joyGetPos(joy, &ji) != JOYERR_NOERROR)
00079         return GL_FALSE;
00080 
00081     return GL_TRUE;
00082 }
00083 
00084 const float* _glfwPlatformGetJoystickAxes(int joy, int* count)
00085 {
00086     JOYCAPS jc;
00087     JOYINFOEX ji;
00088     float* axes = _glfw.winmm_js[joy].axes;
00089 
00090     if (_glfw_joyGetDevCaps(joy, &jc, sizeof(JOYCAPS)) != JOYERR_NOERROR)
00091         return NULL;
00092 
00093     ji.dwSize = sizeof(JOYINFOEX);
00094     ji.dwFlags = JOY_RETURNX | JOY_RETURNY | JOY_RETURNZ |
00095                  JOY_RETURNR | JOY_RETURNU | JOY_RETURNV;
00096     if (_glfw_joyGetPosEx(joy, &ji) != JOYERR_NOERROR)
00097         return NULL;
00098 
00099     axes[(*count)++] = normalizeAxis(ji.dwXpos, jc.wXmin, jc.wXmax);
00100     axes[(*count)++] = normalizeAxis(ji.dwYpos, jc.wYmin, jc.wYmax);
00101 
00102     if (jc.wCaps & JOYCAPS_HASZ)
00103         axes[(*count)++] = normalizeAxis(ji.dwZpos, jc.wZmin, jc.wZmax);
00104 
00105     if (jc.wCaps & JOYCAPS_HASR)
00106         axes[(*count)++] = normalizeAxis(ji.dwRpos, jc.wRmin, jc.wRmax);
00107 
00108     if (jc.wCaps & JOYCAPS_HASU)
00109         axes[(*count)++] = normalizeAxis(ji.dwUpos, jc.wUmin, jc.wUmax);
00110 
00111     if (jc.wCaps & JOYCAPS_HASV)
00112         axes[(*count)++] = normalizeAxis(ji.dwVpos, jc.wVmin, jc.wVmax);
00113 
00114     return axes;
00115 }
00116 
00117 const unsigned char* _glfwPlatformGetJoystickButtons(int joy, int* count)
00118 {
00119     JOYCAPS jc;
00120     JOYINFOEX ji;
00121     unsigned char* buttons = _glfw.winmm_js[joy].buttons;
00122 
00123     if (_glfw_joyGetDevCaps(joy, &jc, sizeof(JOYCAPS)) != JOYERR_NOERROR)
00124         return NULL;
00125 
00126     ji.dwSize = sizeof(JOYINFOEX);
00127     ji.dwFlags = JOY_RETURNBUTTONS | JOY_RETURNPOV;
00128     if (_glfw_joyGetPosEx(joy, &ji) != JOYERR_NOERROR)
00129         return NULL;
00130 
00131     while (*count < (int) jc.wNumButtons)
00132     {
00133         buttons[*count] = (unsigned char)
00134             (ji.dwButtons & (1UL << *count) ? GLFW_PRESS : GLFW_RELEASE);
00135         (*count)++;
00136     }
00137 
00138     // Virtual buttons - Inject data from hats
00139     // Each hat is exposed as 4 buttons which exposes 8 directions with
00140     // concurrent button presses
00141     // NOTE: this API exposes only one hat
00142 
00143     if ((jc.wCaps & JOYCAPS_HASPOV) && (jc.wCaps & JOYCAPS_POV4DIR))
00144     {
00145         int i, value = ji.dwPOV / 100 / 45;
00146 
00147         // Bit fields of button presses for each direction, including nil
00148         const int directions[9] = { 1, 3, 2, 6, 4, 12, 8, 9, 0 };
00149 
00150         if (value < 0 || value > 8)
00151             value = 8;
00152 
00153         for (i = 0;  i < 4;  i++)
00154         {
00155             if (directions[value] & (1 << i))
00156                 buttons[(*count)++] = GLFW_PRESS;
00157             else
00158                 buttons[(*count)++] = GLFW_RELEASE;
00159         }
00160     }
00161 
00162     return buttons;
00163 }
00164 
00165 const char* _glfwPlatformGetJoystickName(int joy)
00166 {
00167     JOYCAPS jc;
00168 
00169     if (_glfw_joyGetDevCaps(joy, &jc, sizeof(JOYCAPS)) != JOYERR_NOERROR)
00170         return NULL;
00171 
00172     free(_glfw.winmm_js[joy].name);
00173     _glfw.winmm_js[joy].name = _glfwCreateUTF8FromWideString(jc.szPname);
00174 
00175     return _glfw.winmm_js[joy].name;
00176 }
00177 


librealsense
Author(s): Sergey Dorodnicov , Mark Horn , Reagan Lopez
autogenerated on Tue Jun 25 2019 19:54:39