Fixing "freeglut_input_devices.c" for MSVS2005 and later per e-mail from Diederick...
[freeglut] / src / freeglut_input_devices.c
1 /*
2  * freeglut_input_devices.c
3  *
4  * Handles miscellaneous input devices via direct serial-port access.
5  * Proper X11 XInput device support is not yet supported.
6  * Also lacks Mac support.
7  *
8  * Written by Joe Krahn <krahn@niehs.nih.gov> 2005
9  *
10  * Copyright (c) 2005 Stephen J. Baker. All Rights Reserved.
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a
13  * copy of this software and associated documentation files (the "Software"),
14  * to deal in the Software without restriction, including without limitation
15  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16  * and/or sell copies of the Software, and to permit persons to whom the
17  * Software is furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included
20  * in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
25  * PAWEL W. OLSZTA OR STEPHEN J. BAKER BE LIABLE FOR ANY CLAIM, DAMAGES OR
26  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28  * DEALINGS IN THE SOFTWARE.
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #    include "config.h"
33 #endif
34
35 #include <GL/freeglut.h>
36 #include "freeglut_internal.h"
37
38 #if TARGET_HOST_POSIX_X11
39 #include <errno.h>
40 #include <sys/ioctl.h>
41 #include <sys/time.h>
42 #include <time.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <string.h>
47 #include <termios.h>
48 #include <fcntl.h>
49 #include <sys/types.h>
50
51 typedef struct {
52    int fd;
53    struct termios termio, termio_save;
54 } SERIALPORT;
55
56 #elif TARGET_HOST_MS_WINDOWS
57 #include <sys/types.h>
58 #include <winbase.h>
59 typedef struct {
60    HANDLE fh;
61    COMMTIMEOUTS timeouts_save;
62    DCB dcb_save;
63 } SERIALPORT;
64
65 #endif
66
67 /********************* Dialbox definitions ***********************/
68
69 #define DIAL_NUM_VALUATORS 8
70
71 /* dial parser state machine states */
72 #define DIAL_NEW                (-1)
73 #define DIAL_WHICH_DEVICE       0
74 #define DIAL_VALUE_HIGH         1
75 #define DIAL_VALUE_LOW          2
76
77 /* dial/button box commands */
78 #define DIAL_INITIALIZE                 0x20
79 #define DIAL_SET_LEDS                   0x75
80 #define DIAL_SET_TEXT                   0x61
81 #define DIAL_SET_AUTO_DIALS             0x50
82 #define DIAL_SET_AUTO_DELTA_DIALS       0x51
83 #define DIAL_SET_FILTER                 0x53
84 #define DIAL_SET_BUTTONS_MOM_TYPE       0x71
85 #define DIAL_SET_AUTO_MOM_BUTTONS       0x73
86 #define DIAL_SET_ALL_LEDS               0x4b
87 #define DIAL_CLEAR_ALL_LEDS             0x4c
88
89 /* dial/button box replies and events */
90 #define DIAL_INITIALIZED        0x20
91 #define DIAL_BASE               0x30
92 #define DIAL_DELTA_BASE         0x40
93 #define DIAL_PRESS_BASE         0xc0
94 #define DIAL_RELEASE_BASE       0xe0
95
96 /* macros to determine reply type */
97 #define IS_DIAL_EVENT(ch)       (((ch)>=DIAL_BASE)&&((ch)<DIAL_BASE+DIAL_NUM_VALUATORS))
98 #define IS_KEY_PRESS(ch)        (((ch)>=DIAL_PRESS_BASE)&&((ch)<DIAL_PRESS_BASE+DIAL_NUM_BUTTONS))
99 #define IS_KEY_RELEASE(ch)      (((ch)>=DIAL_RELEASE_BASE)&&((ch)<DIAL_RELEASE_BASE+DIAL_NUM_BUTTONS))
100 #define IS_INIT_EVENT(ch)       ((ch)==DIAL_INITIALIZED)
101
102 /*****************************************************************/
103
104 static SERIALPORT *serial_open ( const char *device );
105 static void serial_close ( SERIALPORT *port );
106 static int serial_getchar ( SERIALPORT *port );
107 static int serial_putchar ( SERIALPORT *port, unsigned char ch );
108 static void serial_flush ( SERIALPORT *port );
109
110 static void send_dial_event(int dial, int value);
111 static void poll_dials(int id);
112
113 /* local variables */
114 static SERIALPORT *dialbox_port=NULL;
115
116 /*****************************************************************/
117
118 /*
119  * Implementation for glutDeviceGet(GLUT_HAS_DIAL_AND_BUTTON_BOX)
120  */
121 int fgInputDeviceDetect( void )
122 {
123     fgInitialiseInputDevices ();
124
125     if ( !dialbox_port )
126         return 0;
127
128     if ( !fgState.InputDevsInitialised )
129         return 0;
130
131     return 1;
132 }
133
134 /*
135  * Try initializing the input device(s)
136  */
137 void fgInitialiseInputDevices ( void )
138 {
139     if( !fgState.InputDevsInitialised )
140     {
141 #if TARGET_HOST_MS_WINDOWS && ( _MSC_VER >= 1400 ) // will return true for VC8 (VC2005) and higher
142         char *dial_device=NULL;
143         size_t sLen;
144         errno_t err = _dupenv_s( &dial_device, &sLen, "GLUT_DIALS_SERIAL" );
145         if (err)
146             fgError("Error getting GLUT_DIALS_SERIAL environment variable");
147 #else
148         const char *dial_device=NULL;
149         dial_device = getenv ( "GLUT_DIALS_SERIAL" );
150 #endif
151 #if TARGET_HOST_MS_WINDOWS
152         if (!dial_device){
153             static char devname[256];
154             DWORD size=sizeof(devname);
155             DWORD type = REG_SZ;
156             HKEY key;
157             if (RegOpenKeyA(HKEY_LOCAL_MACHINE,"SOFTWARE\\FreeGLUT",&key)==ERROR_SUCCESS) {
158                 if (RegQueryValueExA(key,"DialboxSerialPort",NULL,&type,(LPBYTE)devname,&size)==ERROR_SUCCESS){
159                     dial_device=devname;
160                 }
161                 RegCloseKey(key);
162             }
163         }
164 #endif
165         if ( !dial_device ) return;
166         if ( !( dialbox_port = serial_open ( dial_device ) ) ) return;
167 #if TARGET_HOST_MS_WINDOWS && ( _MSC_VER >= 1400 ) // will return true for VC8 (VC2005) and higher
168         free ( dial_device );  dial_device = NULL;  /* dupenv_s allocates a string that we must free */
169 #endif
170         serial_putchar(dialbox_port,DIAL_INITIALIZE);
171         glutTimerFunc ( 10, poll_dials, 0 );
172         fgState.InputDevsInitialised = GL_TRUE;
173     }
174 }
175
176 /*
177  *
178  */
179 void fgInputDeviceClose( void )
180 {
181     if ( fgState.InputDevsInitialised )
182     {
183         serial_close ( dialbox_port );
184         dialbox_port = NULL;
185         fgState.InputDevsInitialised = GL_FALSE;
186     }
187 }
188
189 /********************************************************************/
190
191 /* Check all windows for dialbox callbacks */
192 static void fghcbEnumDialCallbacks ( SFG_Window *window, SFG_Enumerator *enumerator )
193 {
194     /* Built-in to INVOKE_WCB():  if window->Callbacks[CB_Dials] */
195     INVOKE_WCB ( *window,Dials, ( ((int*)enumerator->data)[0], ((int*)enumerator->data)[1]) );
196     fgEnumSubWindows ( window, fghcbEnumDialCallbacks, enumerator );
197 }
198
199 static void send_dial_event ( int num, int value )
200 {
201     SFG_Enumerator enumerator;
202     int data[2];
203     data[0] = num;
204     data[1] = value;
205     enumerator.found = GL_FALSE;
206     enumerator.data  =  data;
207     fgEnumWindows ( fghcbEnumDialCallbacks, &enumerator );
208 }
209
210 /********************************************************************/
211 static void poll_dials ( int id )
212 {
213     int data;
214     static int dial_state = DIAL_NEW;
215     static int dial_which;
216     static int dial_value;
217     static int dials[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
218
219     if ( !dialbox_port ) return;
220
221     while ( (data=serial_getchar(dialbox_port)) != EOF )
222     {
223         if ( ( dial_state > DIAL_WHICH_DEVICE ) || IS_DIAL_EVENT ( data ) )
224         {
225             switch ( dial_state )
226             {
227             case DIAL_WHICH_DEVICE:
228                 dial_which = data - DIAL_BASE;
229                 dial_state++;
230                 break;
231             case DIAL_VALUE_HIGH:
232                 dial_value = ( data << 8 );
233                 dial_state++;
234                 break;
235             case DIAL_VALUE_LOW:
236                 dial_value |= data;
237                 if ( dial_value & 0x8000 ) dial_value -= 0x10000;
238                 dials[dial_which] = dial_value;
239                 send_dial_event ( dial_which + 1, dial_value * 360 / 256 );
240                 dial_state = DIAL_WHICH_DEVICE;
241                 break;
242             default:
243                 /* error: Impossible state value! */
244                 break;
245             }
246         }
247         else if ( data == DIAL_INITIALIZED )
248         {
249             fgState.InputDevsInitialised = GL_TRUE;
250             dial_state = DIAL_WHICH_DEVICE;
251             serial_putchar(dialbox_port,DIAL_SET_AUTO_DIALS);
252             serial_putchar(dialbox_port,0xff);
253             serial_putchar(dialbox_port,0xff);
254         }
255         else  /* Unknown data; try flushing. */
256             serial_flush(dialbox_port);
257     }
258
259     glutTimerFunc ( 2, poll_dials, 0 );
260 }
261
262
263 /******** OS Specific Serial I/O routines *******/
264 #if TARGET_HOST_POSIX_X11 /* ==> Linux/BSD/UNIX POSIX serial I/O */
265 static SERIALPORT *serial_open ( const char *device )
266 {
267     int fd;
268     struct termios termio;
269     SERIALPORT *port;
270
271     fd = open(device, O_RDWR | O_NONBLOCK );
272     if (fd <0) {
273         perror(device);
274         return NULL;
275     }
276
277     port = malloc(sizeof(SERIALPORT));
278     memset(port, 0, sizeof(SERIALPORT));
279     port->fd = fd;
280
281     /* save current port settings */
282     tcgetattr(fd,&port->termio_save);
283
284     memset(&termio, 0, sizeof(termio));
285     termio.c_cflag = CS8 | CREAD | HUPCL ;
286     termio.c_iflag = IGNPAR | IGNBRK ;
287     termio.c_cc[VTIME]    = 0;   /* inter-character timer */
288     termio.c_cc[VMIN]     = 1;   /* block read until 1 chars received, when blocking I/O */
289
290     cfsetispeed(&termio, B9600);
291     cfsetospeed(&termio, B9600);
292     tcsetattr(fd,TCSANOW,&termio);
293
294     serial_flush(port);
295     return port;
296 }
297
298 static void serial_close(SERIALPORT *port)
299 {
300     if (port)
301     {
302         /* restore old port settings */
303         tcsetattr(port->fd,TCSANOW,&port->termio_save);
304         close(port->fd);
305         free(port);
306     }
307 }
308
309 static int serial_getchar(SERIALPORT *port)
310 {
311     unsigned char ch;
312     if (!port) return EOF;
313     if (read(port->fd,&ch,1)) return ch;
314     return EOF;
315 }
316
317 static int serial_putchar(SERIALPORT *port, unsigned char ch){
318     if (!port) return 0;
319     return write(port->fd,&ch,1);
320 }
321
322 static void serial_flush ( SERIALPORT *port )
323 {
324     tcflush ( port->fd, TCIOFLUSH );
325 }
326
327 #elif TARGET_HOST_MS_WINDOWS
328
329 static SERIALPORT *serial_open(const char *device){
330     HANDLE fh;
331     DCB dcb={sizeof(DCB)};
332     COMMTIMEOUTS timeouts;
333     SERIALPORT *port;
334
335     fh = CreateFile(device,GENERIC_READ|GENERIC_WRITE,0,NULL,
336       OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
337     if (!fh) return NULL;
338
339     port = malloc(sizeof(SERIALPORT));
340     ZeroMemory(port, sizeof(SERIALPORT));
341     port->fh = fh;
342
343     /* save current port settings */
344     GetCommState(fh,&port->dcb_save);
345     GetCommTimeouts(fh,&port->timeouts_save);
346
347     dcb.DCBlength=sizeof(DCB);
348     BuildCommDCB("96,n,8,1",&dcb);
349     SetCommState(fh,&dcb);
350
351     ZeroMemory(&timeouts,sizeof(timeouts));
352     timeouts.ReadTotalTimeoutConstant=1;
353     timeouts.WriteTotalTimeoutConstant=1;
354     SetCommTimeouts(fh,&timeouts);
355
356     serial_flush(port);
357
358     return port;
359 }
360
361 static void serial_close(SERIALPORT *port){
362     if (port){
363         /* restore old port settings */
364         SetCommState(port->fh,&port->dcb_save);
365         SetCommTimeouts(port->fh,&port->timeouts_save);
366         CloseHandle(port->fh);
367         free(port);
368     }
369 }
370
371 static int serial_getchar(SERIALPORT *port){
372     DWORD n;
373     unsigned char ch;
374     if (!port) return EOF;
375     if (!ReadFile(port->fh,&ch,1,&n,NULL)) return EOF;
376     if (n==1) return ch;
377     return EOF;
378 }
379
380 static int serial_putchar(SERIALPORT *port, unsigned char ch){
381     DWORD n;
382     if (!port) return 0;
383     return WriteFile(port->fh,&ch,1,&n,NULL);
384 }
385
386 static void serial_flush ( SERIALPORT *port )
387 {
388     FlushFileBuffers(port->fh);
389 }
390
391 #endif