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