2 * freeglut_input_devices.c
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.
8 * Written by Joe Krahn <krahn@niehs.nih.gov> 2005
10 * Copyright (c) 2005 Stephen J. Baker. All Rights Reserved.
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:
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
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.
35 #include <GL/freeglut.h>
36 #include "freeglut_internal.h"
38 #if TARGET_HOST_POSIX_X11
40 #include <sys/ioctl.h>
49 #include <sys/types.h>
53 struct termios termio, termio_save;
56 #elif TARGET_HOST_MS_WINDOWS
57 #include <sys/types.h>
61 COMMTIMEOUTS timeouts_save;
67 /********************* Dialbox definitions ***********************/
69 #define DIAL_NUM_VALUATORS 8
71 /* dial parser state machine states */
73 #define DIAL_WHICH_DEVICE 0
74 #define DIAL_VALUE_HIGH 1
75 #define DIAL_VALUE_LOW 2
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
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
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)
102 /*****************************************************************/
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 );
110 static void send_dial_event(int dial, int value);
111 static void poll_dials(int id);
113 /* local variables */
114 static SERIALPORT *dialbox_port=NULL;
116 /*****************************************************************/
119 * Implementation for glutDeviceGet(GLUT_HAS_DIAL_AND_BUTTON_BOX)
121 int fgInputDeviceDetect( void )
123 fgInitialiseInputDevices ();
128 if ( !fgState.InputDevsInitialised )
135 * Try initializing the input device(s)
137 void fgInitialiseInputDevices ( void )
139 if( !fgState.InputDevsInitialised )
141 /* will return true for VC8 (VC2005) and higher */
142 #if TARGET_HOST_MS_WINDOWS && ( _MSC_VER >= 1400 )
143 char *dial_device=NULL;
145 errno_t err = _dupenv_s( &dial_device, &sLen, "GLUT_DIALS_SERIAL" );
147 fgError("Error getting GLUT_DIALS_SERIAL environment variable");
149 const char *dial_device=NULL;
150 dial_device = getenv ( "GLUT_DIALS_SERIAL" );
152 #if TARGET_HOST_MS_WINDOWS
154 static char devname[256];
155 DWORD size=sizeof(devname);
158 if (RegOpenKeyA(HKEY_LOCAL_MACHINE,"SOFTWARE\\FreeGLUT",&key)==ERROR_SUCCESS) {
159 if (RegQueryValueExA(key,"DialboxSerialPort",NULL,&type,(LPBYTE)devname,&size)==ERROR_SUCCESS){
166 if ( !dial_device ) return;
167 if ( !( dialbox_port = serial_open ( dial_device ) ) ) return;
168 /* will return true for VC8 (VC2005) and higher */
169 #if TARGET_HOST_MS_WINDOWS && ( _MSC_VER >= 1400 )
170 free ( dial_device ); dial_device = NULL; /* dupenv_s allocates a string that we must free */
172 serial_putchar(dialbox_port,DIAL_INITIALIZE);
173 glutTimerFunc ( 10, poll_dials, 0 );
174 fgState.InputDevsInitialised = GL_TRUE;
181 void fgInputDeviceClose( void )
183 if ( fgState.InputDevsInitialised )
185 serial_close ( dialbox_port );
187 fgState.InputDevsInitialised = GL_FALSE;
191 /********************************************************************/
193 /* Check all windows for dialbox callbacks */
194 static void fghcbEnumDialCallbacks ( SFG_Window *window, SFG_Enumerator *enumerator )
196 /* Built-in to INVOKE_WCB(): if window->Callbacks[CB_Dials] */
197 INVOKE_WCB ( *window,Dials, ( ((int*)enumerator->data)[0], ((int*)enumerator->data)[1]) );
198 fgEnumSubWindows ( window, fghcbEnumDialCallbacks, enumerator );
201 static void send_dial_event ( int num, int value )
203 SFG_Enumerator enumerator;
207 enumerator.found = GL_FALSE;
208 enumerator.data = data;
209 fgEnumWindows ( fghcbEnumDialCallbacks, &enumerator );
212 /********************************************************************/
213 static void poll_dials ( int id )
216 static int dial_state = DIAL_NEW;
217 static int dial_which;
218 static int dial_value;
219 static int dials[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
221 if ( !dialbox_port ) return;
223 while ( (data=serial_getchar(dialbox_port)) != EOF )
225 if ( ( dial_state > DIAL_WHICH_DEVICE ) || IS_DIAL_EVENT ( data ) )
227 switch ( dial_state )
229 case DIAL_WHICH_DEVICE:
230 dial_which = data - DIAL_BASE;
233 case DIAL_VALUE_HIGH:
234 dial_value = ( data << 8 );
239 if ( dial_value & 0x8000 ) dial_value -= 0x10000;
240 dials[dial_which] = dial_value;
241 send_dial_event ( dial_which + 1, dial_value * 360 / 256 );
242 dial_state = DIAL_WHICH_DEVICE;
245 /* error: Impossible state value! */
249 else if ( data == DIAL_INITIALIZED )
251 fgState.InputDevsInitialised = GL_TRUE;
252 dial_state = DIAL_WHICH_DEVICE;
253 serial_putchar(dialbox_port,DIAL_SET_AUTO_DIALS);
254 serial_putchar(dialbox_port,0xff);
255 serial_putchar(dialbox_port,0xff);
257 else /* Unknown data; try flushing. */
258 serial_flush(dialbox_port);
261 glutTimerFunc ( 2, poll_dials, 0 );
265 /******** OS Specific Serial I/O routines *******/
266 #if TARGET_HOST_POSIX_X11 /* ==> Linux/BSD/UNIX POSIX serial I/O */
267 static SERIALPORT *serial_open ( const char *device )
270 struct termios termio;
273 fd = open(device, O_RDWR | O_NONBLOCK );
279 port = malloc(sizeof(SERIALPORT));
280 memset(port, 0, sizeof(SERIALPORT));
283 /* save current port settings */
284 tcgetattr(fd,&port->termio_save);
286 memset(&termio, 0, sizeof(termio));
287 termio.c_cflag = CS8 | CREAD | HUPCL ;
288 termio.c_iflag = IGNPAR | IGNBRK ;
289 termio.c_cc[VTIME] = 0; /* inter-character timer */
290 termio.c_cc[VMIN] = 1; /* block read until 1 chars received, when blocking I/O */
292 cfsetispeed(&termio, B9600);
293 cfsetospeed(&termio, B9600);
294 tcsetattr(fd,TCSANOW,&termio);
300 static void serial_close(SERIALPORT *port)
304 /* restore old port settings */
305 tcsetattr(port->fd,TCSANOW,&port->termio_save);
311 static int serial_getchar(SERIALPORT *port)
314 if (!port) return EOF;
315 if (read(port->fd,&ch,1)) return ch;
319 static int serial_putchar(SERIALPORT *port, unsigned char ch){
321 return write(port->fd,&ch,1);
324 static void serial_flush ( SERIALPORT *port )
326 tcflush ( port->fd, TCIOFLUSH );
329 #elif TARGET_HOST_MS_WINDOWS
331 static SERIALPORT *serial_open(const char *device){
333 DCB dcb={sizeof(DCB)};
334 COMMTIMEOUTS timeouts;
337 fh = CreateFile(device,GENERIC_READ|GENERIC_WRITE,0,NULL,
338 OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
339 if (!fh) return NULL;
341 port = malloc(sizeof(SERIALPORT));
342 ZeroMemory(port, sizeof(SERIALPORT));
345 /* save current port settings */
346 GetCommState(fh,&port->dcb_save);
347 GetCommTimeouts(fh,&port->timeouts_save);
349 dcb.DCBlength=sizeof(DCB);
350 BuildCommDCB("96,n,8,1",&dcb);
351 SetCommState(fh,&dcb);
353 ZeroMemory(&timeouts,sizeof(timeouts));
354 timeouts.ReadTotalTimeoutConstant=1;
355 timeouts.WriteTotalTimeoutConstant=1;
356 SetCommTimeouts(fh,&timeouts);
363 static void serial_close(SERIALPORT *port){
365 /* restore old port settings */
366 SetCommState(port->fh,&port->dcb_save);
367 SetCommTimeouts(port->fh,&port->timeouts_save);
368 CloseHandle(port->fh);
373 static int serial_getchar(SERIALPORT *port){
376 if (!port) return EOF;
377 if (!ReadFile(port->fh,&ch,1,&n,NULL)) return EOF;
382 static int serial_putchar(SERIALPORT *port, unsigned char ch){
385 return WriteFile(port->fh,&ch,1,&n,NULL);
388 static void serial_flush ( SERIALPORT *port )
390 FlushFileBuffers(port->fh);