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 "fg_internal.h"
38 typedef struct _serialport SERIALPORT;
41 /********************* Dialbox definitions ***********************/
43 #define DIAL_NUM_VALUATORS 8
45 /* dial parser state machine states */
47 #define DIAL_WHICH_DEVICE 0
48 #define DIAL_VALUE_HIGH 1
49 #define DIAL_VALUE_LOW 2
51 /* dial/button box commands */
52 #define DIAL_INITIALIZE 0x20
53 #define DIAL_SET_LEDS 0x75
54 #define DIAL_SET_TEXT 0x61
55 #define DIAL_SET_AUTO_DIALS 0x50
56 #define DIAL_SET_AUTO_DELTA_DIALS 0x51
57 #define DIAL_SET_FILTER 0x53
58 #define DIAL_SET_BUTTONS_MOM_TYPE 0x71
59 #define DIAL_SET_AUTO_MOM_BUTTONS 0x73
60 #define DIAL_SET_ALL_LEDS 0x4b
61 #define DIAL_CLEAR_ALL_LEDS 0x4c
63 /* dial/button box replies and events */
64 #define DIAL_INITIALIZED 0x20
65 #define DIAL_BASE 0x30
66 #define DIAL_DELTA_BASE 0x40
67 #define DIAL_PRESS_BASE 0xc0
68 #define DIAL_RELEASE_BASE 0xe0
70 /* macros to determine reply type */
71 #define IS_DIAL_EVENT(ch) (((ch)>=DIAL_BASE)&&((ch)<DIAL_BASE+DIAL_NUM_VALUATORS))
72 #define IS_KEY_PRESS(ch) (((ch)>=DIAL_PRESS_BASE)&&((ch)<DIAL_PRESS_BASE+DIAL_NUM_BUTTONS))
73 #define IS_KEY_RELEASE(ch) (((ch)>=DIAL_RELEASE_BASE)&&((ch)<DIAL_RELEASE_BASE+DIAL_NUM_BUTTONS))
74 #define IS_INIT_EVENT(ch) ((ch)==DIAL_INITIALIZED)
76 /*****************************************************************/
78 extern SERIALPORT *serial_open ( const char *device );
79 extern void serial_close ( SERIALPORT *port );
80 extern int serial_getchar ( SERIALPORT *port );
81 extern int serial_putchar ( SERIALPORT *port, unsigned char ch );
82 extern void serial_flush ( SERIALPORT *port );
84 extern void fgPlatformRegisterDialDevice ( const char *dial_device );
85 static void send_dial_event(int dial, int value);
86 static void poll_dials(int id);
89 static SERIALPORT *dialbox_port=NULL;
91 /*****************************************************************/
94 * Implementation for glutDeviceGet(GLUT_HAS_DIAL_AND_BUTTON_BOX)
96 int fgInputDeviceDetect( void )
98 fgInitialiseInputDevices ();
103 if ( !fgState.InputDevsInitialised )
110 * Try initializing the input device(s)
112 void fgInitialiseInputDevices ( void )
114 if( !fgState.InputDevsInitialised )
116 const char *dial_device=NULL;
117 dial_device = getenv ( "GLUT_DIALS_SERIAL" );
118 fgPlatformRegisterDialDevice ( dial_device );
120 if ( !dial_device ) return;
121 if ( !( dialbox_port = serial_open ( dial_device ) ) ) return;
122 serial_putchar(dialbox_port,DIAL_INITIALIZE);
123 glutTimerFunc ( 10, poll_dials, 0 );
124 fgState.InputDevsInitialised = GL_TRUE;
131 void fgInputDeviceClose( void )
133 if ( fgState.InputDevsInitialised )
135 serial_close ( dialbox_port );
137 fgState.InputDevsInitialised = GL_FALSE;
141 /********************************************************************/
143 /* Check all windows for dialbox callbacks */
144 static void fghcbEnumDialCallbacks ( SFG_Window *window, SFG_Enumerator *enumerator )
146 /* Built-in to INVOKE_WCB(): if window->Callbacks[CB_Dials] */
147 INVOKE_WCB ( *window,Dials, ( ((int*)enumerator->data)[0], ((int*)enumerator->data)[1]) );
148 fgEnumSubWindows ( window, fghcbEnumDialCallbacks, enumerator );
151 static void send_dial_event ( int num, int value )
153 SFG_Enumerator enumerator;
157 enumerator.found = GL_FALSE;
158 enumerator.data = data;
159 fgEnumWindows ( fghcbEnumDialCallbacks, &enumerator );
162 /********************************************************************/
163 static void poll_dials ( int id )
166 static int dial_state = DIAL_NEW;
167 static int dial_which;
168 static int dial_value;
169 static int dials[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
171 if ( !dialbox_port ) return;
173 while ( (data=serial_getchar(dialbox_port)) != EOF )
175 if ( ( dial_state > DIAL_WHICH_DEVICE ) || IS_DIAL_EVENT ( data ) )
177 switch ( dial_state )
179 case DIAL_WHICH_DEVICE:
180 dial_which = data - DIAL_BASE;
183 case DIAL_VALUE_HIGH:
184 dial_value = ( data << 8 );
189 if ( dial_value & 0x8000 ) dial_value -= 0x10000;
190 dials[dial_which] = dial_value;
191 send_dial_event ( dial_which + 1, dial_value * 360 / 256 );
192 dial_state = DIAL_WHICH_DEVICE;
195 /* error: Impossible state value! */
199 else if ( data == DIAL_INITIALIZED )
201 fgState.InputDevsInitialised = GL_TRUE;
202 dial_state = DIAL_WHICH_DEVICE;
203 serial_putchar(dialbox_port,DIAL_SET_AUTO_DIALS);
204 serial_putchar(dialbox_port,0xff);
205 serial_putchar(dialbox_port,0xff);
207 else /* Unknown data; try flushing. */
208 serial_flush(dialbox_port);
211 glutTimerFunc ( 2, poll_dials, 0 );