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.
31 #include <GL/freeglut.h>
32 #include "fg_internal.h"
34 typedef struct _serialport SERIALPORT;
37 /********************* Dialbox definitions ***********************/
39 #define DIAL_NUM_VALUATORS 8
41 /* dial parser state machine states */
43 #define DIAL_WHICH_DEVICE 0
44 #define DIAL_VALUE_HIGH 1
45 #define DIAL_VALUE_LOW 2
47 /* dial/button box commands */
48 #define DIAL_INITIALIZE 0x20
49 #define DIAL_SET_LEDS 0x75
50 #define DIAL_SET_TEXT 0x61
51 #define DIAL_SET_AUTO_DIALS 0x50
52 #define DIAL_SET_AUTO_DELTA_DIALS 0x51
53 #define DIAL_SET_FILTER 0x53
54 #define DIAL_SET_BUTTONS_MOM_TYPE 0x71
55 #define DIAL_SET_AUTO_MOM_BUTTONS 0x73
56 #define DIAL_SET_ALL_LEDS 0x4b
57 #define DIAL_CLEAR_ALL_LEDS 0x4c
59 /* dial/button box replies and events */
60 #define DIAL_INITIALIZED 0x20
61 #define DIAL_BASE 0x30
62 #define DIAL_DELTA_BASE 0x40
63 #define DIAL_PRESS_BASE 0xc0
64 #define DIAL_RELEASE_BASE 0xe0
66 /* macros to determine reply type */
67 #define IS_DIAL_EVENT(ch) (((ch)>=DIAL_BASE)&&((ch)<DIAL_BASE+DIAL_NUM_VALUATORS))
68 #define IS_KEY_PRESS(ch) (((ch)>=DIAL_PRESS_BASE)&&((ch)<DIAL_PRESS_BASE+DIAL_NUM_BUTTONS))
69 #define IS_KEY_RELEASE(ch) (((ch)>=DIAL_RELEASE_BASE)&&((ch)<DIAL_RELEASE_BASE+DIAL_NUM_BUTTONS))
70 #define IS_INIT_EVENT(ch) ((ch)==DIAL_INITIALIZED)
72 /*****************************************************************/
74 extern SERIALPORT *fg_serial_open ( const char *device );
75 extern void fg_serial_close ( SERIALPORT *port );
76 extern int fg_serial_getchar ( SERIALPORT *port );
77 extern int fg_serial_putchar ( SERIALPORT *port, unsigned char ch );
78 extern void fg_serial_flush ( SERIALPORT *port );
80 extern void fgPlatformRegisterDialDevice ( const char *dial_device );
81 static void send_dial_event(int dial, int value);
82 static void poll_dials(int id);
85 static SERIALPORT *dialbox_port=NULL;
87 /*****************************************************************/
90 * Implementation for glutDeviceGet(GLUT_HAS_DIAL_AND_BUTTON_BOX)
92 int fgInputDeviceDetect( void )
94 fgInitialiseInputDevices ();
99 if ( !fgState.InputDevsInitialised )
106 * Try initializing the input device(s)
108 void fgInitialiseInputDevices ( void )
110 if( !fgState.InputDevsInitialised )
112 const char *dial_device=NULL;
113 dial_device = getenv ( "GLUT_DIALS_SERIAL" );
114 fgPlatformRegisterDialDevice ( dial_device );
116 if ( !dial_device ) return;
117 if ( !( dialbox_port = fg_serial_open ( dial_device ) ) ) return;
118 fg_serial_putchar(dialbox_port,DIAL_INITIALIZE);
119 glutTimerFunc ( 10, poll_dials, 0 );
120 fgState.InputDevsInitialised = GL_TRUE;
127 void fgInputDeviceClose( void )
129 if ( fgState.InputDevsInitialised )
131 fg_serial_close ( dialbox_port );
133 fgState.InputDevsInitialised = GL_FALSE;
137 /********************************************************************/
139 /* Check all windows for dialbox callbacks */
140 static void fghcbEnumDialCallbacks ( SFG_Window *window, SFG_Enumerator *enumerator )
142 /* Built-in to INVOKE_WCB(): if window->Callbacks[CB_Dials] */
143 INVOKE_WCB ( *window,Dials, ( ((int*)enumerator->data)[0], ((int*)enumerator->data)[1]) );
144 fgEnumSubWindows ( window, fghcbEnumDialCallbacks, enumerator );
147 static void send_dial_event ( int num, int value )
149 SFG_Enumerator enumerator;
153 enumerator.found = GL_FALSE;
154 enumerator.data = data;
155 fgEnumWindows ( fghcbEnumDialCallbacks, &enumerator );
158 /********************************************************************/
159 static void poll_dials ( int id )
162 static int dial_state = DIAL_NEW;
163 static int dial_which;
164 static int dial_value;
166 if ( !dialbox_port ) return;
168 while ( (data=fg_serial_getchar(dialbox_port)) != EOF )
170 if ( ( dial_state > DIAL_WHICH_DEVICE ) || IS_DIAL_EVENT ( data ) )
172 switch ( dial_state )
174 case DIAL_WHICH_DEVICE:
175 dial_which = data - DIAL_BASE;
178 case DIAL_VALUE_HIGH:
179 dial_value = ( data << 8 );
184 if ( dial_value & 0x8000 ) dial_value -= 0x10000;
185 send_dial_event ( dial_which + 1, dial_value * 360 / 256 );
186 dial_state = DIAL_WHICH_DEVICE;
189 /* error: Impossible state value! */
193 else if ( data == DIAL_INITIALIZED )
195 fgState.InputDevsInitialised = GL_TRUE;
196 dial_state = DIAL_WHICH_DEVICE;
197 fg_serial_putchar(dialbox_port,DIAL_SET_AUTO_DIALS);
198 fg_serial_putchar(dialbox_port,0xff);
199 fg_serial_putchar(dialbox_port,0xff);
201 else /* Unknown data; try flushing. */
202 fg_serial_flush(dialbox_port);
205 glutTimerFunc ( 2, poll_dials, 0 );