Fix more compiler warnings
[freeglut] / src / x11 / fg_input_devices_x11.c
1 /*
2  * freeglut_input_devices_x11.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  * Copied for Platform code by Evan Felix <karcaw at gmail.com>
12  * Creation date: Thur Feb 2 2012
13  *
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:
20  *
21  * The above copyright notice and this permission notice shall be included
22  * in all copies or substantial portions of the Software.
23  *
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.
31  */
32
33 #ifdef HAVE_CONFIG_H
34 #    include "config.h"
35 #endif
36
37 #include <GL/freeglut.h>
38 #include "../fg_internal.h"
39
40 #ifdef HAVE_ERRNO_H
41 #include <errno.h>
42 #endif
43 #include <sys/ioctl.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <termios.h>
48 #include <fcntl.h>
49
50 struct _serialport {
51    int fd;
52    struct termios termio, termio_save;
53 };
54
55 typedef struct _serialport SERIALPORT;
56
57 void serial_flush ( SERIALPORT *port );
58
59 /*****************************************************************/
60
61 /*
62  * Try initializing the input device(s)
63  */
64 void fgPlatformRegisterDialDevice ( const char *dial_device )
65 {
66 }
67
68 SERIALPORT *serial_open ( const char *device )
69 {
70     int fd;
71     struct termios termio;
72     SERIALPORT *port;
73
74     fd = open(device, O_RDWR | O_NONBLOCK );
75     if (fd <0) {
76         perror(device);
77         return NULL;
78     }
79
80     port = malloc(sizeof(SERIALPORT));
81     memset(port, 0, sizeof(SERIALPORT));
82     port->fd = fd;
83
84     /* save current port settings */
85     tcgetattr(fd,&port->termio_save);
86
87     memset(&termio, 0, sizeof(termio));
88     termio.c_cflag = CS8 | CREAD | HUPCL ;
89     termio.c_iflag = IGNPAR | IGNBRK ;
90     termio.c_cc[VTIME]    = 0;   /* inter-character timer */
91     termio.c_cc[VMIN]     = 1;   /* block read until 1 chars received, when blocking I/O */
92
93     cfsetispeed(&termio, B9600);
94     cfsetospeed(&termio, B9600);
95     tcsetattr(fd,TCSANOW,&termio);
96
97     serial_flush(port);
98     return port;
99 }
100
101 void serial_close(SERIALPORT *port)
102 {
103     if (port)
104     {
105         /* restore old port settings */
106         tcsetattr(port->fd,TCSANOW,&port->termio_save);
107         close(port->fd);
108         free(port);
109     }
110 }
111
112 int serial_getchar(SERIALPORT *port)
113 {
114     unsigned char ch;
115     if (!port) return EOF;
116     if (read(port->fd,&ch,1)) return ch;
117     return EOF;
118 }
119
120 int serial_putchar(SERIALPORT *port, unsigned char ch)
121 {
122     if (!port) return 0;
123     return write(port->fd,&ch,1);
124 }
125
126 void serial_flush ( SERIALPORT *port )
127 {
128     tcflush ( port->fd, TCIOFLUSH );
129 }