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
42 #include <sys/ioctl.h>
51 #include <sys/types.h>
55 struct termios termio, termio_save;
58 #elif TARGET_HOST_MS_WINDOWS
59 #include <sys/types.h>
63 COMMTIMEOUTS timeouts_save;
69 /********************* Dialbox definitions ***********************/
71 #define DIAL_NUM_VALUATORS 8
73 /* dial parser state machine states */
75 #define DIAL_WHICH_DEVICE 0
76 #define DIAL_VALUE_HIGH 1
77 #define DIAL_VALUE_LOW 2
79 /* dial/button box commands */
80 #define DIAL_INITIALIZE 0x20
81 #define DIAL_SET_LEDS 0x75
82 #define DIAL_SET_TEXT 0x61
83 #define DIAL_SET_AUTO_DIALS 0x50
84 #define DIAL_SET_AUTO_DELTA_DIALS 0x51
85 #define DIAL_SET_FILTER 0x53
86 #define DIAL_SET_BUTTONS_MOM_TYPE 0x71
87 #define DIAL_SET_AUTO_MOM_BUTTONS 0x73
88 #define DIAL_SET_ALL_LEDS 0x4b
89 #define DIAL_CLEAR_ALL_LEDS 0x4c
91 /* dial/button box replies and events */
92 #define DIAL_INITIALIZED 0x20
93 #define DIAL_BASE 0x30
94 #define DIAL_DELTA_BASE 0x40
95 #define DIAL_PRESS_BASE 0xc0
96 #define DIAL_RELEASE_BASE 0xe0
98 /* macros to determine reply type */
99 #define IS_DIAL_EVENT(ch) (((ch)>=DIAL_BASE)&&((ch)<DIAL_BASE+DIAL_NUM_VALUATORS))
100 #define IS_KEY_PRESS(ch) (((ch)>=DIAL_PRESS_BASE)&&((ch)<DIAL_PRESS_BASE+DIAL_NUM_BUTTONS))
101 #define IS_KEY_RELEASE(ch) (((ch)>=DIAL_RELEASE_BASE)&&((ch)<DIAL_RELEASE_BASE+DIAL_NUM_BUTTONS))
102 #define IS_INIT_EVENT(ch) ((ch)==DIAL_INITIALIZED)
104 /*****************************************************************/
106 static SERIALPORT *serial_open ( const char *device );
107 static void serial_close ( SERIALPORT *port );
108 static int serial_getchar ( SERIALPORT *port );
109 static int serial_putchar ( SERIALPORT *port, unsigned char ch );
110 static void serial_flush ( SERIALPORT *port );
112 static void send_dial_event(int dial, int value);
113 static void poll_dials(int id);
115 /* local variables */
116 static SERIALPORT *dialbox_port=NULL;
118 /*****************************************************************/
121 * Implementation for glutDeviceGet(GLUT_HAS_DIAL_AND_BUTTON_BOX)
123 int fgInputDeviceDetect( void )
125 fgInitialiseInputDevices ();
130 if ( !fgState.InputDevsInitialised )
137 * Try initializing the input device(s)
139 void fgInitialiseInputDevices ( void )
141 if( !fgState.InputDevsInitialised )
143 const char *dial_device=NULL;
144 dial_device = getenv ( "GLUT_DIALS_SERIAL" );
145 #if TARGET_HOST_MS_WINDOWS
147 static char devname[256];
148 DWORD size=sizeof(devname);
151 if (RegOpenKeyA(HKEY_LOCAL_MACHINE,"SOFTWARE\\FreeGLUT",&key)==ERROR_SUCCESS) {
152 if (RegQueryValueExA(key,"DialboxSerialPort",NULL,&type,(LPBYTE)devname,&size)==ERROR_SUCCESS){
159 if ( !dial_device ) return;
160 if ( !( dialbox_port = serial_open ( dial_device ) ) ) return;
161 serial_putchar(dialbox_port,DIAL_INITIALIZE);
162 glutTimerFunc ( 10, poll_dials, 0 );
163 fgState.InputDevsInitialised = GL_TRUE;
170 void fgInputDeviceClose( void )
172 if ( fgState.InputDevsInitialised )
174 serial_close ( dialbox_port );
176 fgState.InputDevsInitialised = GL_FALSE;
180 /********************************************************************/
182 /* Check all windows for dialbox callbacks */
183 static void fghcbEnumDialCallbacks ( SFG_Window *window, SFG_Enumerator *enumerator )
185 /* Built-in to INVOKE_WCB(): if window->Callbacks[CB_Dials] */
186 INVOKE_WCB ( *window,Dials, ( ((int*)enumerator->data)[0], ((int*)enumerator->data)[1]) );
187 fgEnumSubWindows ( window, fghcbEnumDialCallbacks, enumerator );
190 static void send_dial_event ( int num, int value )
192 SFG_Enumerator enumerator;
196 enumerator.found = GL_FALSE;
197 enumerator.data = data;
198 fgEnumWindows ( fghcbEnumDialCallbacks, &enumerator );
201 /********************************************************************/
202 static void poll_dials ( int id )
205 static int dial_state = DIAL_NEW;
206 static int dial_which;
207 static int dial_value;
208 static int dials[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
210 if ( !dialbox_port ) return;
212 while ( (data=serial_getchar(dialbox_port)) != EOF )
214 if ( ( dial_state > DIAL_WHICH_DEVICE ) || IS_DIAL_EVENT ( data ) )
216 switch ( dial_state )
218 case DIAL_WHICH_DEVICE:
219 dial_which = data - DIAL_BASE;
222 case DIAL_VALUE_HIGH:
223 dial_value = ( data << 8 );
228 if ( dial_value & 0x8000 ) dial_value -= 0x10000;
229 dials[dial_which] = dial_value;
230 send_dial_event ( dial_which + 1, dial_value * 360 / 256 );
231 dial_state = DIAL_WHICH_DEVICE;
234 /* error: Impossible state value! */
238 else if ( data == DIAL_INITIALIZED )
240 fgState.InputDevsInitialised = GL_TRUE;
241 dial_state = DIAL_WHICH_DEVICE;
242 serial_putchar(dialbox_port,DIAL_SET_AUTO_DIALS);
243 serial_putchar(dialbox_port,0xff);
244 serial_putchar(dialbox_port,0xff);
246 else /* Unknown data; try flushing. */
247 serial_flush(dialbox_port);
250 glutTimerFunc ( 2, poll_dials, 0 );
254 /******** OS Specific Serial I/O routines *******/
255 #if TARGET_HOST_POSIX_X11 /* ==> Linux/BSD/UNIX POSIX serial I/O */
256 static SERIALPORT *serial_open ( const char *device )
259 struct termios termio;
262 fd = open(device, O_RDWR | O_NONBLOCK );
268 port = malloc(sizeof(SERIALPORT));
269 memset(port, 0, sizeof(SERIALPORT));
272 /* save current port settings */
273 tcgetattr(fd,&port->termio_save);
275 memset(&termio, 0, sizeof(termio));
276 termio.c_cflag = CS8 | CREAD | HUPCL ;
277 termio.c_iflag = IGNPAR | IGNBRK ;
278 termio.c_cc[VTIME] = 0; /* inter-character timer */
279 termio.c_cc[VMIN] = 1; /* block read until 1 chars received, when blocking I/O */
281 cfsetispeed(&termio, B9600);
282 cfsetospeed(&termio, B9600);
283 tcsetattr(fd,TCSANOW,&termio);
289 static void serial_close(SERIALPORT *port)
293 /* restore old port settings */
294 tcsetattr(port->fd,TCSANOW,&port->termio_save);
300 static int serial_getchar(SERIALPORT *port)
303 if (!port) return EOF;
304 if (read(port->fd,&ch,1)) return ch;
308 static int serial_putchar(SERIALPORT *port, unsigned char ch){
310 return write(port->fd,&ch,1);
313 static void serial_flush ( SERIALPORT *port )
315 tcflush ( port->fd, TCIOFLUSH );
318 #elif TARGET_HOST_MS_WINDOWS
320 static SERIALPORT *serial_open(const char *device){
322 DCB dcb={sizeof(DCB)};
323 COMMTIMEOUTS timeouts;
326 fh = CreateFile(device,GENERIC_READ|GENERIC_WRITE,0,NULL,
327 OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
328 if (!fh) return NULL;
330 port = malloc(sizeof(SERIALPORT));
331 ZeroMemory(port, sizeof(SERIALPORT));
334 /* save current port settings */
335 GetCommState(fh,&port->dcb_save);
336 GetCommTimeouts(fh,&port->timeouts_save);
338 dcb.DCBlength=sizeof(DCB);
339 BuildCommDCB("96,n,8,1",&dcb);
340 SetCommState(fh,&dcb);
342 ZeroMemory(&timeouts,sizeof(timeouts));
343 timeouts.ReadTotalTimeoutConstant=1;
344 timeouts.WriteTotalTimeoutConstant=1;
345 SetCommTimeouts(fh,&timeouts);
352 static void serial_close(SERIALPORT *port){
354 /* restore old port settings */
355 SetCommState(port->fh,&port->dcb_save);
356 SetCommTimeouts(port->fh,&port->timeouts_save);
357 CloseHandle(port->fh);
362 static int serial_getchar(SERIALPORT *port){
365 if (!port) return EOF;
366 if (!ReadFile(port->fh,&ch,1,&n,NULL)) return EOF;
371 static int serial_putchar(SERIALPORT *port, unsigned char ch){
374 return WriteFile(port->fh,&ch,1,&n,NULL);
377 static void serial_flush ( SERIALPORT *port )
379 FlushFileBuffers(port->fh);