]> git.saurik.com Git - wxWidgets.git/blame - src/msw/gsockmsw.c
set device origin when drawing on a frame in order to not overwrite the menu/toolbar...
[wxWidgets.git] / src / msw / gsockmsw.c
CommitLineData
70988afb
GRG
1/* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket)
3 * Name: gsockmsw.c
4 * Author: Guillermo Rodriguez Garcia <guille@iies.es>
5 * Purpose: GSocket GUI-specific MSW code
6 * CVSID: $Id$
7 * -------------------------------------------------------------------------
8 */
9
882dfc67
JS
10/*
11 * TODO: for WinCE we need to replace WSAAsyncSelect
12 * (Windows message-based notification of network events for a socket)
13 * with another mechanism.
14 * We may need to have a separate thread that polls for socket events
15 * using select() and sends a message to the main thread.
16 */
17
70988afb
GRG
18/*
19 * PLEASE don't put C++ comments here - this is a C source file.
20 */
21
8a9c2246
VZ
22/* including rasasync.h (included from windows.h itself included from
23 * wx/setup.h and/or winsock.h results in this warning for
24 * RPCNOTIFICATION_ROUTINE
25 */
26#ifdef _MSC_VER
27# pragma warning(disable:4115) /* named type definition in parentheses */
28#endif
29
882dfc67
JS
30/* This needs to be before the wx/defs/h inclusion
31 * for some reason
32 */
33
34#ifdef __WXWINCE__
35#include <windows.h>
36#endif
37
70988afb 38#ifndef __GSOCKET_STANDALONE__
a3f7fa62
VZ
39# include "wx/platform.h"
40# include "wx/setup.h"
70988afb
GRG
41#endif
42
43#if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
44
45#ifndef __GSOCKET_STANDALONE__
46
47#include "wx/msw/gsockmsw.h"
48#include "wx/gsocket.h"
49
2341cf5f 50HINSTANCE wxGetInstance(void);
70988afb
GRG
51#define INSTANCE wxGetInstance()
52
53#else
54
55#include "gsockmsw.h"
56#include "gsocket.h"
57
58/* If not using wxWindows, a global var called hInst must
f4929e86 59 * be available and it must contain the app's instance
70988afb
GRG
60 * handle.
61 */
62#define INSTANCE hInst
63
64#endif /* __GSOCKET_STANDALONE__ */
65
882dfc67 66#ifndef __WXWINCE__
70988afb 67#include <assert.h>
882dfc67
JS
68#else
69#define assert(x)
70#include <winsock.h>
71#include "wx/msw/wince/net.h"
72#endif
73
70988afb
GRG
74#include <string.h>
75#include <stdio.h>
76#include <stdlib.h>
77#include <stddef.h>
78#include <ctype.h>
8a9c2246 79
70988afb
GRG
80#include <winsock.h>
81
8a9c2246
VZ
82#ifdef _MSC_VER
83# pragma warning(default:4115) /* named type definition in parentheses */
84#endif
85
9aee09a3 86#define CLASSNAME TEXT("_GSocket_Internal_Window_Class")
eccd1992
VZ
87
88/* implemented in utils.cpp */
487f2d58 89extern WXDLLIMPEXP_BASE HWND
eccd1992 90wxCreateHiddenWindow(LPCTSTR *pclassname, LPCTSTR classname, WNDPROC wndproc);
70988afb
GRG
91
92/* Maximum number of different GSocket objects at a given time.
93 * This value can be modified at will, but it CANNOT be greater
94 * than (0x7FFF - WM_USER + 1)
95 */
96#define MAXSOCKETS 1024
97
98#if (MAXSOCKETS > (0x7FFF - WM_USER + 1))
99#error "MAXSOCKETS is too big!"
100#endif
101
fe16045d 102typedef int (PASCAL *WSAAsyncSelectFunc)(SOCKET,HWND,u_int,long);
70988afb
GRG
103
104/* Global variables */
105
106extern HINSTANCE INSTANCE;
107static HWND hWin;
108static CRITICAL_SECTION critical;
109static GSocket* socketList[MAXSOCKETS];
110static int firstAvailable;
fb2e90c1
VS
111static WSAAsyncSelectFunc gs_WSAAsyncSelect = NULL;
112static HMODULE gs_wsock32dll = 0;
70988afb
GRG
113
114/* Global initializers */
115
38bb138f 116int _GSocket_GUI_Init(void)
70988afb 117{
978f48c6 118 static LPCTSTR pclassname = NULL;
70988afb
GRG
119 int i;
120
121 /* Create internal window for event notifications */
eccd1992
VZ
122 hWin = wxCreateHiddenWindow(&pclassname, CLASSNAME, _GSocket_Internal_WinProc);
123 if (!hWin)
124 return FALSE;
70988afb
GRG
125
126 /* Initialize socket list */
127 InitializeCriticalSection(&critical);
128
129 for (i = 0; i < MAXSOCKETS; i++)
130 {
131 socketList[i] = NULL;
132 }
133 firstAvailable = 0;
134
fb2e90c1
VS
135 /* Load WSAAsyncSelect from wsock32.dll (we don't link against it
136 statically to avoid dependency on wsock32.dll for apps that don't use
137 sockets): */
138 gs_wsock32dll = LoadLibraryA("wsock32.dll");
139 if (!gs_wsock32dll)
140 return FALSE;
141 gs_WSAAsyncSelect =(WSAAsyncSelectFunc)GetProcAddress(gs_wsock32dll,
142 "WSAAsyncSelect");
143 if (!gs_WSAAsyncSelect)
144 return FALSE;
145
eccd1992 146 return TRUE;
70988afb
GRG
147}
148
38bb138f 149void _GSocket_GUI_Cleanup(void)
70988afb
GRG
150{
151 /* Destroy internal window */
152 DestroyWindow(hWin);
153 UnregisterClass(CLASSNAME, INSTANCE);
154
fb2e90c1
VS
155 /* Unlock wsock32.dll */
156 if (gs_wsock32dll)
157 {
158 FreeLibrary(gs_wsock32dll);
159 gs_wsock32dll = 0;
160 }
161
70988afb
GRG
162 /* Delete critical section */
163 DeleteCriticalSection(&critical);
70988afb
GRG
164}
165
166/* Per-socket GUI initialization / cleanup */
167
38bb138f 168int _GSocket_GUI_Init_Socket(GSocket *socket)
70988afb
GRG
169{
170 int i;
171
172 /* Allocate a new message number for this socket */
173 EnterCriticalSection(&critical);
174
175 i = firstAvailable;
176 while (socketList[i] != NULL)
177 {
178 i = (i + 1) % MAXSOCKETS;
179
180 if (i == firstAvailable) /* abort! */
181 {
182 LeaveCriticalSection(&critical);
183 return FALSE;
184 }
185 }
186 socketList[i] = socket;
187 firstAvailable = (i + 1) % MAXSOCKETS;
188 socket->m_msgnumber = (i + WM_USER);
189
190 LeaveCriticalSection(&critical);
191
192 return TRUE;
193}
194
38bb138f 195void _GSocket_GUI_Destroy_Socket(GSocket *socket)
70988afb
GRG
196{
197 /* Remove the socket from the list */
198 EnterCriticalSection(&critical);
199 socketList[(socket->m_msgnumber - WM_USER)] = NULL;
200 LeaveCriticalSection(&critical);
201}
202
203/* Windows proc for asynchronous event handling */
204
205LRESULT CALLBACK _GSocket_Internal_WinProc(HWND hWnd,
206 UINT uMsg,
207 WPARAM wParam,
208 LPARAM lParam)
209{
210 GSocket *socket;
211 GSocketEvent event;
212 GSocketCallback cback;
213 char *data;
214
215 if (uMsg >= WM_USER && uMsg <= (WM_USER + MAXSOCKETS - 1))
216 {
217 EnterCriticalSection(&critical);
218 socket = socketList[(uMsg - WM_USER)];
ba14d986 219 event = (GSocketEvent) -1;
70988afb
GRG
220 cback = NULL;
221 data = NULL;
222
223 /* Check that the socket still exists (it has not been
224 * destroyed) and for safety, check that the m_fd field
225 * is what we expect it to be.
226 */
227 if ((socket != NULL) && (socket->m_fd == wParam))
228 {
229 switch WSAGETSELECTEVENT(lParam)
230 {
231 case FD_READ: event = GSOCK_INPUT; break;
232 case FD_WRITE: event = GSOCK_OUTPUT; break;
233 case FD_ACCEPT: event = GSOCK_CONNECTION; break;
234 case FD_CONNECT:
235 {
236 if (WSAGETSELECTERROR(lParam) != 0)
237 event = GSOCK_LOST;
238 else
239 event = GSOCK_CONNECTION;
240 break;
241 }
242 case FD_CLOSE: event = GSOCK_LOST; break;
243 }
244
245 if (event != -1)
246 {
247 cback = socket->m_cbacks[event];
248 data = socket->m_data[event];
249
250 if (event == GSOCK_LOST)
251 socket->m_detected = GSOCK_LOST_FLAG;
252 else
253 socket->m_detected |= (1 << event);
254 }
255 }
256
257 /* OK, we can now leave the critical section because we have
258 * already obtained the callback address (we make no further
259 * accesses to socket->whatever). However, the app should
260 * be prepared to handle events from a socket that has just
261 * been closed!
262 */
263 LeaveCriticalSection(&critical);
264
265 if (cback != NULL)
266 (cback)(socket, event, data);
267
268 return (LRESULT) 0;
269 }
270 else
271 return DefWindowProc(hWnd, uMsg, wParam, lParam);
272}
273
274/* _GSocket_Enable_Events:
275 * Enable all event notifications; we need to be notified of all
276 * events for internal processing, but we will only notify users
277 * when an appropiate callback function has been installed.
278 */
279void _GSocket_Enable_Events(GSocket *socket)
280{
281 assert (socket != NULL);
282
283 if (socket->m_fd != INVALID_SOCKET)
284 {
ed2eb9af
GRG
285 /* We could probably just subscribe to all events regardless
286 * of the socket type, but MS recommends to do it this way.
287 */
288 long lEvent = socket->m_server?
289 FD_ACCEPT : (FD_READ | FD_WRITE | FD_CONNECT | FD_CLOSE);
290
fb2e90c1 291 gs_WSAAsyncSelect(socket->m_fd, hWin, socket->m_msgnumber, lEvent);
70988afb
GRG
292 }
293}
294
295/* _GSocket_Disable_Events:
296 * Disable event notifications (when shutdowning the socket)
297 */
298void _GSocket_Disable_Events(GSocket *socket)
299{
300 assert (socket != NULL);
301
302 if (socket->m_fd != INVALID_SOCKET)
303 {
fb2e90c1 304 gs_WSAAsyncSelect(socket->m_fd, hWin, socket->m_msgnumber, 0);
70988afb
GRG
305 }
306}
307
308#else /* !wxUSE_SOCKETS */
309
33ac7e6f 310/*
70988afb
GRG
311 * Translation unit shouldn't be empty, so include this typedef to make the
312 * compiler (VC++ 6.0, for example) happy
313 */
3a922bb4 314typedef void (*wxDummy)();
70988afb
GRG
315
316#endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */