minor update making code more compact.
[freeglut] / src / mswin / fg_spaceball_mswin.c
1 /*
2  * fg_spaceball_mswin.c
3  *
4  * Spaceball support for Windows
5  * 
6  * Copyright (c) 2012 Stephen J. Baker. All Rights Reserved.
7  * Written by Evan Felix <karcaw at gmail.com>
8  * Creation date: Sat Feb 4, 2012
9  *
10  * Copyright (c) 2014 Jinrong Xie. All Rights Reserved.
11  * Written by Jinrong Xie <stonexjr at gmail.com>
12  * Modification date: Wed Dec 24, 2014
13
14  * Permission is hereby granted, free of charge, to any person obtaining a
15  * copy of this software and associated documentation files (the "Software"),
16  * to deal in the Software without restriction, including without limitation
17  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18  * and/or sell copies of the Software, and to permit persons to whom the
19  * Software is furnished to do so, subject to the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be included
22  * in all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
27  * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
28  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30  */
31
32 /*
33  * Modified by Jinrong Xie (stonexjr@gmail.com) 12/24/2014
34  * for Space Navigator support on Windows.
35  * This code is enhanced by at least supporting 3Dconnexion's 
36  * six degree of freedom navigator.
37  */
38
39 #include <GL/freeglut.h>
40 #include "../fg_internal.h"
41
42 enum {
43     SPNAV_EVENT_ANY,  
44     SPNAV_EVENT_MOTION_TRANSLATION,
45     SPNAV_EVENT_MOTION_ROTATION,
46     SPNAV_EVENT_BUTTON  /* includes both press and release */
47 };
48
49 extern int sball_initialized;
50 unsigned int __fgSpaceKeystate = 0;
51 RAWINPUTDEVICE __fgSpaceball = { 0x01, 0x08, 0x00, 0x00 };
52
53 void fgPlatformInitializeSpaceball(void)
54 {
55         HWND hwnd;
56     sball_initialized = 1;
57         if (!fgStructure.CurrentWindow)
58         {
59                 sball_initialized = 0;
60                 return;
61         }
62         hwnd = fgStructure.CurrentWindow->Window.Handle;
63
64         BOOL ok;
65         UINT cbSize = sizeof(__fgSpaceball);
66         __fgSpaceball.hwndTarget = hwnd;
67         ok = RegisterRawInputDevices(&__fgSpaceball, 1, cbSize);
68
69         if (!ok){
70                 __fgSpaceball.hwndTarget = NULL;
71                 sball_initialized = 0;
72         }
73 }
74
75 void fgPlatformSpaceballClose(void)
76 {
77         return;
78 }
79
80 int fgPlatformHasSpaceball(void)
81 {
82         return __fgSpaceball.hwndTarget ? 1 : 0;
83 }
84
85 int fgPlatformSpaceballNumButtons(void)
86 {
87         return 0;
88 }
89
90 void fgPlatformSpaceballSetWindow(SFG_Window *window)
91 {
92         return;
93 }
94
95 int fgIsSpaceballWinEvent(HWND hwnd, WPARAM wParam, LPARAM lParam)
96 {
97         return 0;
98 }
99
100 void fgSpaceballHandleWinEvent(HWND hwnd, WPARAM wParam, LPARAM lParam)
101 {
102         #define LOGITECH_VENDOR_ID 0x46d
103         HRAWINPUT hRawInput = (HRAWINPUT)lParam;
104         UINT inputCode = (UINT)wParam;
105         UINT size;
106         BYTE *rawInputBuffer;
107         PRAWINPUT pRawInput;
108         UINT res;
109         RID_DEVICE_INFO sRidDeviceInfo;
110
111         if (!sball_initialized)
112         {
113                 fgPlatformInitializeSpaceball();
114                 if (!sball_initialized)
115                 {
116                         return;
117                 }
118         }
119
120         res = GetRawInputData(hRawInput, RID_INPUT, NULL, &size, sizeof(RAWINPUTHEADER));
121         if (res == -1)
122                 return;
123
124         rawInputBuffer = (BYTE*)malloc(size);
125         pRawInput = (PRAWINPUT)rawInputBuffer;
126
127         res = GetRawInputData(hRawInput, RID_INPUT, pRawInput, &size, sizeof(RAWINPUTHEADER));
128         if (res == -1)
129                 return;
130         if (pRawInput->header.dwType != RIM_TYPEHID)
131                 return;
132
133         sRidDeviceInfo.cbSize = sizeof(RID_DEVICE_INFO);
134         size = sizeof(RID_DEVICE_INFO);
135         res = GetRawInputDeviceInfo(pRawInput->header.hDevice, RIDI_DEVICEINFO, &sRidDeviceInfo, &size);
136         if (res == -1)
137                 return;
138
139         SFG_Window* window = fgWindowByHandle(hwnd);
140         if ((window == NULL))
141                 return;
142
143         if (sRidDeviceInfo.hid.dwVendorId == LOGITECH_VENDOR_ID)
144         {
145                 // Motion data comes in two parts: motion type and 
146                 // displacement/rotation along three axis.
147                 // Orientation is a right handed coordinate system with 
148                 // X goes right, Y goes up and Z goes towards viewer, e.g.
149                 // the one used in OpenGL
150                 if (pRawInput->data.hid.bRawData[0] == 
151                         SPNAV_EVENT_MOTION_TRANSLATION)
152                 { // Translation vector
153                         short* pnData = (short*)(&pRawInput->data.hid.bRawData[1]);
154                         short X =  pnData[0];
155                         short Y = -pnData[2];
156                         short Z =  pnData[1];
157                         INVOKE_WCB(*window, SpaceMotion, (X, Y, Z));
158                 }
159                 else if (pRawInput->data.hid.bRawData[0] == 
160                                 SPNAV_EVENT_MOTION_ROTATION)
161                 { // Axis aligned rotation vector
162                         short* pnData = (short*)(&pRawInput->data.hid.bRawData[1]);
163                         short rX =  pnData[0];
164                         short rY = -pnData[2];
165                         short rZ =  pnData[1];
166                         INVOKE_WCB(*window, SpaceRotation, (rX, rY, rZ));
167                 }
168                 else if (pRawInput->data.hid.bRawData[0] == 
169                                  SPNAV_EVENT_BUTTON)
170                 { // State of the keys
171                         unsigned long dwKeystate = *(unsigned long*)(&pRawInput->data.hid.bRawData[1]);
172                         unsigned int state = GLUT_UP;
173                         if (FETCH_WCB(*window, SpaceButton))
174                         {
175                                 int i;
176                                 for (i = 0; i < 32; i++)
177                                 {
178                                         unsigned long stateBefore = __fgSpaceKeystate&(1 << i);
179                                         unsigned long stateNow = dwKeystate&(1 << i);
180
181                                         if (stateBefore && !stateNow)
182                                                 INVOKE_WCB(*window, SpaceButton, (stateBefore, GLUT_DOWN));
183                                         if (!stateBefore && stateNow)
184                                                 INVOKE_WCB(*window, SpaceButton, (stateNow, GLUT_UP));
185
186                                 }
187                         }
188                         __fgSpaceKeystate = dwKeystate;
189                 }
190         }
191 }