Using the -pedantic flag with gcc uncovered a series of warnings about
[freeglut] / src / fg_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 "fg_internal.h"
37
38 typedef struct _serialport SERIALPORT;
39
40
41 /********************* Dialbox definitions ***********************/
42
43 #define DIAL_NUM_VALUATORS 8
44
45 /* dial parser state machine states */
46 #define DIAL_NEW                (-1)
47 #define DIAL_WHICH_DEVICE       0
48 #define DIAL_VALUE_HIGH         1
49 #define DIAL_VALUE_LOW          2
50
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
62
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
69
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)
75
76 /*****************************************************************/
77
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 );
83
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);
87
88 /* local variables */
89 static SERIALPORT *dialbox_port=NULL;
90
91 /*****************************************************************/
92
93 /*
94  * Implementation for glutDeviceGet(GLUT_HAS_DIAL_AND_BUTTON_BOX)
95  */
96 int fgInputDeviceDetect( void )
97 {
98     fgInitialiseInputDevices ();
99
100     if ( !dialbox_port )
101         return 0;
102
103     if ( !fgState.InputDevsInitialised )
104         return 0;
105
106     return 1;
107 }
108
109 /*
110  * Try initializing the input device(s)
111  */
112 void fgInitialiseInputDevices ( void )
113 {
114     if( !fgState.InputDevsInitialised )
115     {
116         const char *dial_device=NULL;
117         dial_device = getenv ( "GLUT_DIALS_SERIAL" );
118                 fgPlatformRegisterDialDevice ( dial_device );
119
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;
125     }
126 }
127
128 /*
129  *
130  */
131 void fgInputDeviceClose( void )
132 {
133     if ( fgState.InputDevsInitialised )
134     {
135         serial_close ( dialbox_port );
136         dialbox_port = NULL;
137         fgState.InputDevsInitialised = GL_FALSE;
138     }
139 }
140
141 /********************************************************************/
142
143 /* Check all windows for dialbox callbacks */
144 static void fghcbEnumDialCallbacks ( SFG_Window *window, SFG_Enumerator *enumerator )
145 {
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 );
149 }
150
151 static void send_dial_event ( int num, int value )
152 {
153     SFG_Enumerator enumerator;
154     int data[2];
155     data[0] = num;
156     data[1] = value;
157     enumerator.found = GL_FALSE;
158     enumerator.data  =  data;
159     fgEnumWindows ( fghcbEnumDialCallbacks, &enumerator );
160 }
161
162 /********************************************************************/
163 static void poll_dials ( int id )
164 {
165     int data;
166     static int dial_state = DIAL_NEW;
167     static int dial_which;
168     static int dial_value;
169
170     if ( !dialbox_port ) return;
171
172     while ( (data=serial_getchar(dialbox_port)) != EOF )
173     {
174         if ( ( dial_state > DIAL_WHICH_DEVICE ) || IS_DIAL_EVENT ( data ) )
175         {
176             switch ( dial_state )
177             {
178             case DIAL_WHICH_DEVICE:
179                 dial_which = data - DIAL_BASE;
180                 dial_state++;
181                 break;
182             case DIAL_VALUE_HIGH:
183                 dial_value = ( data << 8 );
184                 dial_state++;
185                 break;
186             case DIAL_VALUE_LOW:
187                 dial_value |= data;
188                 if ( dial_value & 0x8000 ) dial_value -= 0x10000;
189                 send_dial_event ( dial_which + 1, dial_value * 360 / 256 );
190                 dial_state = DIAL_WHICH_DEVICE;
191                 break;
192             default:
193                 /* error: Impossible state value! */
194                 break;
195             }
196         }
197         else if ( data == DIAL_INITIALIZED )
198         {
199             fgState.InputDevsInitialised = GL_TRUE;
200             dial_state = DIAL_WHICH_DEVICE;
201             serial_putchar(dialbox_port,DIAL_SET_AUTO_DIALS);
202             serial_putchar(dialbox_port,0xff);
203             serial_putchar(dialbox_port,0xff);
204         }
205         else  /* Unknown data; try flushing. */
206             serial_flush(dialbox_port);
207     }
208
209     glutTimerFunc ( 2, poll_dials, 0 );
210 }
211