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