timers internally now use 64bit unsigned int, if available
[freeglut] / src / Common / freeglut_input_devices.c
index bfb6c8c..1dab040 100644 (file)
-/*\r
- * freeglut_input_devices.c\r
- *\r
- * Handles miscellaneous input devices via direct serial-port access.\r
- * Proper X11 XInput device support is not yet supported.\r
- * Also lacks Mac support.\r
- *\r
- * Written by Joe Krahn <krahn@niehs.nih.gov> 2005\r
- *\r
- * Copyright (c) 2005 Stephen J. Baker. All Rights Reserved.\r
- *\r
- * Permission is hereby granted, free of charge, to any person obtaining a\r
- * copy of this software and associated documentation files (the "Software"),\r
- * to deal in the Software without restriction, including without limitation\r
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,\r
- * and/or sell copies of the Software, and to permit persons to whom the\r
- * Software is furnished to do so, subject to the following conditions:\r
- *\r
- * The above copyright notice and this permission notice shall be included\r
- * in all copies or substantial portions of the Software.\r
- *\r
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS\r
- * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL\r
- * PAWEL W. OLSZTA OR STEPHEN J. BAKER BE LIABLE FOR ANY CLAIM, DAMAGES OR\r
- * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,\r
- * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER\r
- * DEALINGS IN THE SOFTWARE.\r
- */\r
-\r
-#ifdef HAVE_CONFIG_H\r
-#    include "config.h"\r
-#endif\r
-\r
-#include <GL/freeglut.h>\r
-#include "freeglut_internal.h"\r
-\r
-#if TARGET_HOST_POSIX_X11\r
-#ifdef HAVE_ERRNO_H\r
-#include <errno.h>\r
-#endif\r
-#include <sys/ioctl.h>\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <string.h>\r
-#include <termios.h>\r
-#include <fcntl.h>\r
-\r
-struct {\r
-   int fd;\r
-   struct termios termio, termio_save;\r
-} _serialport;\r
-\r
-#endif\r
-\r
-typedef struct _serialport SERIALPORT;\r
-\r
-\r
-/********************* Dialbox definitions ***********************/\r
-\r
-#define DIAL_NUM_VALUATORS 8\r
-\r
-/* dial parser state machine states */\r
-#define DIAL_NEW                (-1)\r
-#define DIAL_WHICH_DEVICE       0\r
-#define DIAL_VALUE_HIGH         1\r
-#define DIAL_VALUE_LOW          2\r
-\r
-/* dial/button box commands */\r
-#define DIAL_INITIALIZE                 0x20\r
-#define DIAL_SET_LEDS                   0x75\r
-#define DIAL_SET_TEXT                   0x61\r
-#define DIAL_SET_AUTO_DIALS             0x50\r
-#define DIAL_SET_AUTO_DELTA_DIALS       0x51\r
-#define DIAL_SET_FILTER                 0x53\r
-#define DIAL_SET_BUTTONS_MOM_TYPE       0x71\r
-#define DIAL_SET_AUTO_MOM_BUTTONS       0x73\r
-#define DIAL_SET_ALL_LEDS               0x4b\r
-#define DIAL_CLEAR_ALL_LEDS             0x4c\r
-\r
-/* dial/button box replies and events */\r
-#define DIAL_INITIALIZED        0x20\r
-#define DIAL_BASE               0x30\r
-#define DIAL_DELTA_BASE         0x40\r
-#define DIAL_PRESS_BASE         0xc0\r
-#define DIAL_RELEASE_BASE       0xe0\r
-\r
-/* macros to determine reply type */\r
-#define IS_DIAL_EVENT(ch)       (((ch)>=DIAL_BASE)&&((ch)<DIAL_BASE+DIAL_NUM_VALUATORS))\r
-#define IS_KEY_PRESS(ch)        (((ch)>=DIAL_PRESS_BASE)&&((ch)<DIAL_PRESS_BASE+DIAL_NUM_BUTTONS))\r
-#define IS_KEY_RELEASE(ch)      (((ch)>=DIAL_RELEASE_BASE)&&((ch)<DIAL_RELEASE_BASE+DIAL_NUM_BUTTONS))\r
-#define IS_INIT_EVENT(ch)       ((ch)==DIAL_INITIALIZED)\r
-\r
-/*****************************************************************/\r
-\r
-extern SERIALPORT *serial_open ( const char *device );\r
-extern void serial_close ( SERIALPORT *port );\r
-extern int serial_getchar ( SERIALPORT *port );\r
-extern int serial_putchar ( SERIALPORT *port, unsigned char ch );\r
-extern void serial_flush ( SERIALPORT *port );\r
-\r
-extern void fgPlatformRegisterDialDevice ( const char *dial_device );\r
-static void send_dial_event(int dial, int value);\r
-static void poll_dials(int id);\r
-\r
-/* local variables */\r
-static SERIALPORT *dialbox_port=NULL;\r
-\r
-/*****************************************************************/\r
-\r
-/*\r
- * Implementation for glutDeviceGet(GLUT_HAS_DIAL_AND_BUTTON_BOX)\r
- */\r
-int fgInputDeviceDetect( void )\r
-{\r
-    fgInitialiseInputDevices ();\r
-\r
-    if ( !dialbox_port )\r
-        return 0;\r
-\r
-    if ( !fgState.InputDevsInitialised )\r
-        return 0;\r
-\r
-    return 1;\r
-}\r
-\r
-/*\r
- * Try initializing the input device(s)\r
- */\r
-#if TARGET_HOST_POSIX_X11\r
-void fgPlatformRegisterDialDevice ( const char *dial_device )\r
-{\r
-}\r
-#endif\r
-\r
-void fgInitialiseInputDevices ( void )\r
-{\r
-    if( !fgState.InputDevsInitialised )\r
-    {\r
-        const char *dial_device=NULL;\r
-        dial_device = getenv ( "GLUT_DIALS_SERIAL" );\r
-               fgPlatformRegisterDialDevice ( dial_device );\r
-\r
-        if ( !dial_device ) return;\r
-        if ( !( dialbox_port = serial_open ( dial_device ) ) ) return;\r
-        serial_putchar(dialbox_port,DIAL_INITIALIZE);\r
-        glutTimerFunc ( 10, poll_dials, 0 );\r
-        fgState.InputDevsInitialised = GL_TRUE;\r
-    }\r
-}\r
-\r
-/*\r
- *\r
- */\r
-void fgInputDeviceClose( void )\r
-{\r
-    if ( fgState.InputDevsInitialised )\r
-    {\r
-        serial_close ( dialbox_port );\r
-        dialbox_port = NULL;\r
-        fgState.InputDevsInitialised = GL_FALSE;\r
-    }\r
-}\r
-\r
-/********************************************************************/\r
-\r
-/* Check all windows for dialbox callbacks */\r
-static void fghcbEnumDialCallbacks ( SFG_Window *window, SFG_Enumerator *enumerator )\r
-{\r
-    /* Built-in to INVOKE_WCB():  if window->Callbacks[CB_Dials] */\r
-    INVOKE_WCB ( *window,Dials, ( ((int*)enumerator->data)[0], ((int*)enumerator->data)[1]) );\r
-    fgEnumSubWindows ( window, fghcbEnumDialCallbacks, enumerator );\r
-}\r
-\r
-static void send_dial_event ( int num, int value )\r
-{\r
-    SFG_Enumerator enumerator;\r
-    int data[2];\r
-    data[0] = num;\r
-    data[1] = value;\r
-    enumerator.found = GL_FALSE;\r
-    enumerator.data  =  data;\r
-    fgEnumWindows ( fghcbEnumDialCallbacks, &enumerator );\r
-}\r
-\r
-/********************************************************************/\r
-static void poll_dials ( int id )\r
-{\r
-    int data;\r
-    static int dial_state = DIAL_NEW;\r
-    static int dial_which;\r
-    static int dial_value;\r
-    static int dials[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };\r
-\r
-    if ( !dialbox_port ) return;\r
-\r
-    while ( (data=serial_getchar(dialbox_port)) != EOF )\r
-    {\r
-        if ( ( dial_state > DIAL_WHICH_DEVICE ) || IS_DIAL_EVENT ( data ) )\r
-        {\r
-            switch ( dial_state )\r
-            {\r
-            case DIAL_WHICH_DEVICE:\r
-                dial_which = data - DIAL_BASE;\r
-                dial_state++;\r
-                break;\r
-            case DIAL_VALUE_HIGH:\r
-                dial_value = ( data << 8 );\r
-                dial_state++;\r
-                break;\r
-            case DIAL_VALUE_LOW:\r
-                dial_value |= data;\r
-                if ( dial_value & 0x8000 ) dial_value -= 0x10000;\r
-                dials[dial_which] = dial_value;\r
-                send_dial_event ( dial_which + 1, dial_value * 360 / 256 );\r
-                dial_state = DIAL_WHICH_DEVICE;\r
-                break;\r
-            default:\r
-                /* error: Impossible state value! */\r
-                break;\r
-            }\r
-        }\r
-        else if ( data == DIAL_INITIALIZED )\r
-        {\r
-            fgState.InputDevsInitialised = GL_TRUE;\r
-            dial_state = DIAL_WHICH_DEVICE;\r
-            serial_putchar(dialbox_port,DIAL_SET_AUTO_DIALS);\r
-            serial_putchar(dialbox_port,0xff);\r
-            serial_putchar(dialbox_port,0xff);\r
-        }\r
-        else  /* Unknown data; try flushing. */\r
-            serial_flush(dialbox_port);\r
-    }\r
-\r
-    glutTimerFunc ( 2, poll_dials, 0 );\r
-}\r
-\r
-\r
-/******** OS Specific Serial I/O routines *******/\r
-#if TARGET_HOST_POSIX_X11 /* ==> Linux/BSD/UNIX POSIX serial I/O */\r
-static SERIALPORT *serial_open ( const char *device )\r
-{\r
-    int fd;\r
-    struct termios termio;\r
-    SERIALPORT *port;\r
-\r
-    fd = open(device, O_RDWR | O_NONBLOCK );\r
-    if (fd <0) {\r
-        perror(device);\r
-        return NULL;\r
-    }\r
-\r
-    port = malloc(sizeof(SERIALPORT));\r
-    memset(port, 0, sizeof(SERIALPORT));\r
-    port->fd = fd;\r
-\r
-    /* save current port settings */\r
-    tcgetattr(fd,&port->termio_save);\r
-\r
-    memset(&termio, 0, sizeof(termio));\r
-    termio.c_cflag = CS8 | CREAD | HUPCL ;\r
-    termio.c_iflag = IGNPAR | IGNBRK ;\r
-    termio.c_cc[VTIME]    = 0;   /* inter-character timer */\r
-    termio.c_cc[VMIN]     = 1;   /* block read until 1 chars received, when blocking I/O */\r
-\r
-    cfsetispeed(&termio, B9600);\r
-    cfsetospeed(&termio, B9600);\r
-    tcsetattr(fd,TCSANOW,&termio);\r
-\r
-    serial_flush(port);\r
-    return port;\r
-}\r
-\r
-static void serial_close(SERIALPORT *port)\r
-{\r
-    if (port)\r
-    {\r
-        /* restore old port settings */\r
-        tcsetattr(port->fd,TCSANOW,&port->termio_save);\r
-        close(port->fd);\r
-        free(port);\r
-    }\r
-}\r
-\r
-static int serial_getchar(SERIALPORT *port)\r
-{\r
-    unsigned char ch;\r
-    if (!port) return EOF;\r
-    if (read(port->fd,&ch,1)) return ch;\r
-    return EOF;\r
-}\r
-\r
-static int serial_putchar(SERIALPORT *port, unsigned char ch){\r
-    if (!port) return 0;\r
-    return write(port->fd,&ch,1);\r
-}\r
-\r
-static void serial_flush ( SERIALPORT *port )\r
-{\r
-    tcflush ( port->fd, TCIOFLUSH );\r
-}\r
-\r
-#endif\r
+/*
+ * freeglut_input_devices.c
+ *
+ * Handles miscellaneous input devices via direct serial-port access.
+ * Proper X11 XInput device support is not yet supported.
+ * Also lacks Mac support.
+ *
+ * Written by Joe Krahn <krahn@niehs.nih.gov> 2005
+ *
+ * Copyright (c) 2005 Stephen J. Baker. All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * PAWEL W. OLSZTA OR STEPHEN J. BAKER BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifdef HAVE_CONFIG_H
+#    include "config.h"
+#endif
+
+#include <GL/freeglut.h>
+#include "freeglut_internal.h"
+
+typedef struct _serialport SERIALPORT;
+
+
+/********************* Dialbox definitions ***********************/
+
+#define DIAL_NUM_VALUATORS 8
+
+/* dial parser state machine states */
+#define DIAL_NEW                (-1)
+#define DIAL_WHICH_DEVICE       0
+#define DIAL_VALUE_HIGH         1
+#define DIAL_VALUE_LOW          2
+
+/* dial/button box commands */
+#define DIAL_INITIALIZE                 0x20
+#define DIAL_SET_LEDS                   0x75
+#define DIAL_SET_TEXT                   0x61
+#define DIAL_SET_AUTO_DIALS             0x50
+#define DIAL_SET_AUTO_DELTA_DIALS       0x51
+#define DIAL_SET_FILTER                 0x53
+#define DIAL_SET_BUTTONS_MOM_TYPE       0x71
+#define DIAL_SET_AUTO_MOM_BUTTONS       0x73
+#define DIAL_SET_ALL_LEDS               0x4b
+#define DIAL_CLEAR_ALL_LEDS             0x4c
+
+/* dial/button box replies and events */
+#define DIAL_INITIALIZED        0x20
+#define DIAL_BASE               0x30
+#define DIAL_DELTA_BASE         0x40
+#define DIAL_PRESS_BASE         0xc0
+#define DIAL_RELEASE_BASE       0xe0
+
+/* macros to determine reply type */
+#define IS_DIAL_EVENT(ch)       (((ch)>=DIAL_BASE)&&((ch)<DIAL_BASE+DIAL_NUM_VALUATORS))
+#define IS_KEY_PRESS(ch)        (((ch)>=DIAL_PRESS_BASE)&&((ch)<DIAL_PRESS_BASE+DIAL_NUM_BUTTONS))
+#define IS_KEY_RELEASE(ch)      (((ch)>=DIAL_RELEASE_BASE)&&((ch)<DIAL_RELEASE_BASE+DIAL_NUM_BUTTONS))
+#define IS_INIT_EVENT(ch)       ((ch)==DIAL_INITIALIZED)
+
+/*****************************************************************/
+
+extern SERIALPORT *serial_open ( const char *device );
+extern void serial_close ( SERIALPORT *port );
+extern int serial_getchar ( SERIALPORT *port );
+extern int serial_putchar ( SERIALPORT *port, unsigned char ch );
+extern void serial_flush ( SERIALPORT *port );
+
+extern void fgPlatformRegisterDialDevice ( const char *dial_device );
+static void send_dial_event(int dial, int value);
+static void poll_dials(int id);
+
+/* local variables */
+static SERIALPORT *dialbox_port=NULL;
+
+/*****************************************************************/
+
+/*
+ * Implementation for glutDeviceGet(GLUT_HAS_DIAL_AND_BUTTON_BOX)
+ */
+int fgInputDeviceDetect( void )
+{
+    fgInitialiseInputDevices ();
+
+    if ( !dialbox_port )
+        return 0;
+
+    if ( !fgState.InputDevsInitialised )
+        return 0;
+
+    return 1;
+}
+
+/*
+ * Try initializing the input device(s)
+ */
+void fgInitialiseInputDevices ( void )
+{
+    if( !fgState.InputDevsInitialised )
+    {
+        const char *dial_device=NULL;
+        dial_device = getenv ( "GLUT_DIALS_SERIAL" );
+               fgPlatformRegisterDialDevice ( dial_device );
+
+        if ( !dial_device ) return;
+        if ( !( dialbox_port = serial_open ( dial_device ) ) ) return;
+        serial_putchar(dialbox_port,DIAL_INITIALIZE);
+        glutTimerFunc ( 10, poll_dials, 0 );
+        fgState.InputDevsInitialised = GL_TRUE;
+    }
+}
+
+/*
+ *
+ */
+void fgInputDeviceClose( void )
+{
+    if ( fgState.InputDevsInitialised )
+    {
+        serial_close ( dialbox_port );
+        dialbox_port = NULL;
+        fgState.InputDevsInitialised = GL_FALSE;
+    }
+}
+
+/********************************************************************/
+
+/* Check all windows for dialbox callbacks */
+static void fghcbEnumDialCallbacks ( SFG_Window *window, SFG_Enumerator *enumerator )
+{
+    /* Built-in to INVOKE_WCB():  if window->Callbacks[CB_Dials] */
+    INVOKE_WCB ( *window,Dials, ( ((int*)enumerator->data)[0], ((int*)enumerator->data)[1]) );
+    fgEnumSubWindows ( window, fghcbEnumDialCallbacks, enumerator );
+}
+
+static void send_dial_event ( int num, int value )
+{
+    SFG_Enumerator enumerator;
+    int data[2];
+    data[0] = num;
+    data[1] = value;
+    enumerator.found = GL_FALSE;
+    enumerator.data  =  data;
+    fgEnumWindows ( fghcbEnumDialCallbacks, &enumerator );
+}
+
+/********************************************************************/
+static void poll_dials ( int id )
+{
+    int data;
+    static int dial_state = DIAL_NEW;
+    static int dial_which;
+    static int dial_value;
+    static int dials[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
+
+    if ( !dialbox_port ) return;
+
+    while ( (data=serial_getchar(dialbox_port)) != EOF )
+    {
+        if ( ( dial_state > DIAL_WHICH_DEVICE ) || IS_DIAL_EVENT ( data ) )
+        {
+            switch ( dial_state )
+            {
+            case DIAL_WHICH_DEVICE:
+                dial_which = data - DIAL_BASE;
+                dial_state++;
+                break;
+            case DIAL_VALUE_HIGH:
+                dial_value = ( data << 8 );
+                dial_state++;
+                break;
+            case DIAL_VALUE_LOW:
+                dial_value |= data;
+                if ( dial_value & 0x8000 ) dial_value -= 0x10000;
+                dials[dial_which] = dial_value;
+                send_dial_event ( dial_which + 1, dial_value * 360 / 256 );
+                dial_state = DIAL_WHICH_DEVICE;
+                break;
+            default:
+                /* error: Impossible state value! */
+                break;
+            }
+        }
+        else if ( data == DIAL_INITIALIZED )
+        {
+            fgState.InputDevsInitialised = GL_TRUE;
+            dial_state = DIAL_WHICH_DEVICE;
+            serial_putchar(dialbox_port,DIAL_SET_AUTO_DIALS);
+            serial_putchar(dialbox_port,0xff);
+            serial_putchar(dialbox_port,0xff);
+        }
+        else  /* Unknown data; try flushing. */
+            serial_flush(dialbox_port);
+    }
+
+    glutTimerFunc ( 2, poll_dials, 0 );
+}
+