0914fb171c1991853d2ebf2af575e07bcf1064f1
[freeglut] / src / mswin / fg_input_devices_mswin.c
1 /*
2  * fg_input_devices_mswin.c
3  *
4  * The Windows-specific mouse cursor related stuff.
5  *
6  * Copyright (c) 2012 Stephen J. Baker. All Rights Reserved.
7  * Written by John F. Fay, <fayjf@sourceforge.net>
8  * Creation date: Sat Jan 21, 2012
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23  * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  */
27
28 #include <GL/freeglut.h>
29 #include "../fg_internal.h"
30
31 #include <sys/types.h>
32 #include <winbase.h>
33
34 typedef struct {
35    HANDLE fh;
36    COMMTIMEOUTS timeouts_save;
37    DCB dcb_save;
38 } SERIALPORT;
39
40 /* Serial Port Prototypes */
41 SERIALPORT *serial_open ( const char *device );
42 void serial_close ( SERIALPORT *port );
43 int serial_getchar ( SERIALPORT *port );
44 int serial_putchar ( SERIALPORT *port, unsigned char ch );
45 void serial_flush ( SERIALPORT *port );
46
47
48 void fgPlatformRegisterDialDevice ( const char *dial_device )
49 {
50         if (!dial_device){
51             static char devname[256];
52             DWORD size=sizeof(devname);
53             DWORD type = REG_SZ;
54             HKEY key;
55             if (RegOpenKeyA(HKEY_LOCAL_MACHINE,"SOFTWARE\\FreeGLUT",&key)==ERROR_SUCCESS) {
56                 if (RegQueryValueExA(key,"DialboxSerialPort",NULL,&type,(LPBYTE)devname,&size)==ERROR_SUCCESS){
57                     dial_device=devname;
58                 }
59                 RegCloseKey(key);
60             }
61         }
62 }
63
64
65 /*  Serial Port Functions */
66 SERIALPORT *serial_open(const char *device){
67     HANDLE fh;
68     DCB dcb={sizeof(DCB)};
69     COMMTIMEOUTS timeouts;
70     SERIALPORT *port;
71
72     fh = CreateFile(device,GENERIC_READ|GENERIC_WRITE,0,NULL,
73       OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
74     if (!fh) return NULL;
75
76     port = malloc(sizeof(SERIALPORT));
77     ZeroMemory(port, sizeof(SERIALPORT));
78     port->fh = fh;
79
80     /* save current port settings */
81     GetCommState(fh,&port->dcb_save);
82     GetCommTimeouts(fh,&port->timeouts_save);
83
84     dcb.DCBlength=sizeof(DCB);
85     BuildCommDCB("96,n,8,1",&dcb);
86     SetCommState(fh,&dcb);
87
88     ZeroMemory(&timeouts,sizeof(timeouts));
89     timeouts.ReadTotalTimeoutConstant=1;
90     timeouts.WriteTotalTimeoutConstant=1;
91     SetCommTimeouts(fh,&timeouts);
92
93     serial_flush(port);
94
95     return port;
96 }
97
98 void serial_close(SERIALPORT *port){
99     if (port){
100         /* restore old port settings */
101         SetCommState(port->fh,&port->dcb_save);
102         SetCommTimeouts(port->fh,&port->timeouts_save);
103         CloseHandle(port->fh);
104         free(port);
105     }
106 }
107
108 int serial_getchar(SERIALPORT *port){
109     DWORD n;
110     unsigned char ch;
111     if (!port) return EOF;
112     if (!ReadFile(port->fh,&ch,1,&n,NULL)) return EOF;
113     if (n==1) return ch;
114     return EOF;
115 }
116
117 int serial_putchar(SERIALPORT *port, unsigned char ch){
118     DWORD n;
119     if (!port) return 0;
120     return WriteFile(port->fh,&ch,1,&n,NULL);
121 }
122
123 void serial_flush ( SERIALPORT *port )
124 {
125     FlushFileBuffers(port->fh);
126 }
127