Moving the X11-specific code from "freeglut_input_devices.c" into its own file (thank...
[freeglut] / src / x11 / freeglut_input_devices_x11.c
index e69de29..ebd99eb 100644 (file)
@@ -0,0 +1,132 @@
+/*\r
+ * freeglut_input_devices_x11.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
+ * Copied for Platform code by Evan Felix <karcaw at gmail.com>\r
+ * Creation date: Thur Feb 2 2012\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
+#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 _serialport {\r
+   int fd;\r
+   struct termios termio, termio_save;\r
+};\r
+\r
+typedef struct _serialport SERIALPORT;\r
+\r
+void serial_flush ( SERIALPORT *port );\r
+\r
+/* local variables */\r
+static SERIALPORT *dialbox_port=NULL;\r
+\r
+/*****************************************************************/\r
+\r
+/*\r
+ * Try initializing the input device(s)\r
+ */\r
+void fgPlatformRegisterDialDevice ( const char *dial_device )\r
+{\r
+}\r
+\r
+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
+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
+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
+int serial_putchar(SERIALPORT *port, unsigned char ch)\r
+{\r
+    if (!port) return 0;\r
+    return write(port->fd,&ch,1);\r
+}\r
+\r
+void serial_flush ( SERIALPORT *port )\r
+{\r
+    tcflush ( port->fd, TCIOFLUSH );\r
+}\r