2 * fg_input_devices_x11.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.
11 * Copied for Platform code by Evan Felix <karcaw at gmail.com>
12 * Creation date: Thur Feb 2 2012
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the "Software"),
16 * to deal in the Software without restriction, including without limitation
17 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 * and/or sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice shall be included
22 * in all copies or substantial portions of the Software.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 * PAWEL W. OLSZTA OR STEPHEN J. BAKER BE LIABLE FOR ANY CLAIM, DAMAGES OR
28 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
29 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 * DEALINGS IN THE SOFTWARE.
33 #include <GL/freeglut.h>
34 #include "../fg_internal.h"
37 #include <sys/ioctl.h>
46 struct termios termio, termio_save;
49 typedef struct _serialport SERIALPORT;
51 void serial_flush ( SERIALPORT *port );
53 /*****************************************************************/
56 * Try initializing the input device(s)
58 void fgPlatformRegisterDialDevice ( const char *dial_device )
62 SERIALPORT *serial_open ( const char *device )
65 struct termios termio;
68 fd = open(device, O_RDWR | O_NONBLOCK );
74 port = malloc(sizeof(SERIALPORT));
75 memset(port, 0, sizeof(SERIALPORT));
78 /* save current port settings */
79 tcgetattr(fd,&port->termio_save);
81 memset(&termio, 0, sizeof(termio));
82 termio.c_cflag = CS8 | CREAD | HUPCL ;
83 termio.c_iflag = IGNPAR | IGNBRK ;
84 termio.c_cc[VTIME] = 0; /* inter-character timer */
85 termio.c_cc[VMIN] = 1; /* block read until 1 chars received, when blocking I/O */
87 cfsetispeed(&termio, B9600);
88 cfsetospeed(&termio, B9600);
89 tcsetattr(fd,TCSANOW,&termio);
95 void serial_close(SERIALPORT *port)
99 /* restore old port settings */
100 tcsetattr(port->fd,TCSANOW,&port->termio_save);
106 int serial_getchar(SERIALPORT *port)
109 if (!port) return EOF;
110 if (read(port->fd,&ch,1)) return ch;
114 int serial_putchar(SERIALPORT *port, unsigned char ch)
117 return write(port->fd,&ch,1);
120 void serial_flush ( SERIALPORT *port )
122 tcflush ( port->fd, TCIOFLUSH );