]> git.saurik.com Git - wxWidgets.git/blob - src/msw/gsockmsw.cpp
The list ctrl doesn't draw any focus
[wxWidgets.git] / src / msw / gsockmsw.cpp
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
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
18 /*
19 * PLEASE don't put C++ comments here - this is a C source file.
20 */
21
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
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
38 #ifndef __GSOCKET_STANDALONE__
39 # include "wx/platform.h"
40 # include "wx/setup.h"
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
50 HINSTANCE wxGetInstance(void);
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
59 * be available and it must contain the app's instance
60 * handle.
61 */
62 #define INSTANCE hInst
63
64 #endif /* __GSOCKET_STANDALONE__ */
65
66 #ifndef __WXWINCE__
67 #include <assert.h>
68 #else
69 #define assert(x)
70 #include <winsock.h>
71 #include "wx/msw/wince/net.h"
72 #endif
73
74 #include <string.h>
75 #include <stdio.h>
76 #include <stdlib.h>
77 #include <stddef.h>
78 #include <ctype.h>
79
80 #include <winsock.h>
81
82 #ifdef _MSC_VER
83 # pragma warning(default:4115) /* named type definition in parentheses */
84 #endif
85
86 #define CLASSNAME TEXT("_GSocket_Internal_Window_Class")
87
88 /* implemented in utils.cpp */
89 extern WXDLLIMPEXP_BASE HWND
90 wxCreateHiddenWindow(LPCTSTR *pclassname, LPCTSTR classname, WNDPROC wndproc);
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
102
103 /* Global variables */
104
105 extern HINSTANCE INSTANCE;
106 static HWND hWin;
107 static CRITICAL_SECTION critical;
108 static GSocket* socketList[MAXSOCKETS];
109 static int firstAvailable;
110
111 /* Global initializers */
112
113 int _GSocket_GUI_Init(void)
114 {
115 static LPCTSTR pclassname = NULL;
116 int i;
117
118 /* Create internal window for event notifications */
119 hWin = wxCreateHiddenWindow(&pclassname, CLASSNAME, _GSocket_Internal_WinProc);
120 if (!hWin)
121 return FALSE;
122
123 /* Initialize socket list */
124 InitializeCriticalSection(&critical);
125
126 for (i = 0; i < MAXSOCKETS; i++)
127 {
128 socketList[i] = NULL;
129 }
130 firstAvailable = 0;
131
132 return TRUE;
133 }
134
135 void _GSocket_GUI_Cleanup(void)
136 {
137 /* Destroy internal window */
138 DestroyWindow(hWin);
139 UnregisterClass(CLASSNAME, INSTANCE);
140
141 /* Delete critical section */
142 DeleteCriticalSection(&critical);
143 }
144
145 /* Per-socket GUI initialization / cleanup */
146
147 int _GSocket_GUI_Init_Socket(GSocket *socket)
148 {
149 int i;
150
151 /* Allocate a new message number for this socket */
152 EnterCriticalSection(&critical);
153
154 i = firstAvailable;
155 while (socketList[i] != NULL)
156 {
157 i = (i + 1) % MAXSOCKETS;
158
159 if (i == firstAvailable) /* abort! */
160 {
161 LeaveCriticalSection(&critical);
162 return FALSE;
163 }
164 }
165 socketList[i] = socket;
166 firstAvailable = (i + 1) % MAXSOCKETS;
167 socket->m_msgnumber = (i + WM_USER);
168
169 LeaveCriticalSection(&critical);
170
171 return TRUE;
172 }
173
174 void _GSocket_GUI_Destroy_Socket(GSocket *socket)
175 {
176 /* Remove the socket from the list */
177 EnterCriticalSection(&critical);
178 socketList[(socket->m_msgnumber - WM_USER)] = NULL;
179 LeaveCriticalSection(&critical);
180 }
181
182 /* Windows proc for asynchronous event handling */
183
184 LRESULT CALLBACK _GSocket_Internal_WinProc(HWND hWnd,
185 UINT uMsg,
186 WPARAM wParam,
187 LPARAM lParam)
188 {
189 GSocket *socket;
190 GSocketEvent event;
191 GSocketCallback cback;
192 char *data;
193
194 if (uMsg >= WM_USER && uMsg <= (WM_USER + MAXSOCKETS - 1))
195 {
196 EnterCriticalSection(&critical);
197 socket = socketList[(uMsg - WM_USER)];
198 event = (GSocketEvent) -1;
199 cback = NULL;
200 data = NULL;
201
202 /* Check that the socket still exists (it has not been
203 * destroyed) and for safety, check that the m_fd field
204 * is what we expect it to be.
205 */
206 if ((socket != NULL) && (socket->m_fd == wParam))
207 {
208 switch WSAGETSELECTEVENT(lParam)
209 {
210 case FD_READ: event = GSOCK_INPUT; break;
211 case FD_WRITE: event = GSOCK_OUTPUT; break;
212 case FD_ACCEPT: event = GSOCK_CONNECTION; break;
213 case FD_CONNECT:
214 {
215 if (WSAGETSELECTERROR(lParam) != 0)
216 event = GSOCK_LOST;
217 else
218 event = GSOCK_CONNECTION;
219 break;
220 }
221 case FD_CLOSE: event = GSOCK_LOST; break;
222 }
223
224 if (event != -1)
225 {
226 cback = socket->m_cbacks[event];
227 data = socket->m_data[event];
228
229 if (event == GSOCK_LOST)
230 socket->m_detected = GSOCK_LOST_FLAG;
231 else
232 socket->m_detected |= (1 << event);
233 }
234 }
235
236 /* OK, we can now leave the critical section because we have
237 * already obtained the callback address (we make no further
238 * accesses to socket->whatever). However, the app should
239 * be prepared to handle events from a socket that has just
240 * been closed!
241 */
242 LeaveCriticalSection(&critical);
243
244 if (cback != NULL)
245 (cback)(socket, event, data);
246
247 return (LRESULT) 0;
248 }
249 else
250 return DefWindowProc(hWnd, uMsg, wParam, lParam);
251 }
252
253 /* _GSocket_Enable_Events:
254 * Enable all event notifications; we need to be notified of all
255 * events for internal processing, but we will only notify users
256 * when an appropiate callback function has been installed.
257 */
258 void _GSocket_Enable_Events(GSocket *socket)
259 {
260 assert (socket != NULL);
261
262 if (socket->m_fd != INVALID_SOCKET)
263 {
264 /* We could probably just subscribe to all events regardless
265 * of the socket type, but MS recommends to do it this way.
266 */
267 long lEvent = socket->m_server?
268 FD_ACCEPT : (FD_READ | FD_WRITE | FD_CONNECT | FD_CLOSE);
269
270 WSAAsyncSelect(socket->m_fd, hWin, socket->m_msgnumber, lEvent);
271 }
272 }
273
274 /* _GSocket_Disable_Events:
275 * Disable event notifications (when shutdowning the socket)
276 */
277 void _GSocket_Disable_Events(GSocket *socket)
278 {
279 assert (socket != NULL);
280
281 if (socket->m_fd != INVALID_SOCKET)
282 {
283 WSAAsyncSelect(socket->m_fd, hWin, socket->m_msgnumber, 0);
284 }
285 }
286
287 #else /* !wxUSE_SOCKETS */
288
289 /*
290 * Translation unit shouldn't be empty, so include this typedef to make the
291 * compiler (VC++ 6.0, for example) happy
292 */
293 typedef void (*wxDummy)();
294
295 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */