]> git.saurik.com Git - wxWidgets.git/blob - src/x11/evtloop.cpp
use SubclassWin() insteaf of just wxAssociateWinWithHandle() to make sure we generate...
[wxWidgets.git] / src / x11 / evtloop.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/evtloop.cpp
3 // Purpose: implements wxEventLoop for X11
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01.06.01
7 // RCS-ID: $Id$
8 // Copyright: (c) 2002 Julian Smart
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #include "wx/evtloop.h"
24
25 #ifndef WX_PRECOMP
26 #include "wx/hash.h"
27 #include "wx/app.h"
28 #include "wx/window.h"
29 #endif
30
31 #include "wx/tooltip.h"
32 #include "wx/timer.h"
33 #include "wx/module.h"
34 #include "wx/unix/private.h"
35 #include "wx/x11/private.h"
36 #include "X11/Xlib.h"
37
38 #if wxUSE_THREADS
39 #include "wx/thread.h"
40 #endif
41
42 #include <sys/time.h>
43 #include <unistd.h>
44
45 #if wxUSE_SOCKETS
46 // ----------------------------------------------------------------------------
47 // wxSocketTable
48 // ----------------------------------------------------------------------------
49
50 typedef void (*wxSocketCallback) (int fd, void* data);
51
52 class wxSocketTableEntry: public wxObject
53 {
54 public:
55 wxSocketTableEntry()
56 {
57 m_fdInput = -1; m_fdOutput = -1;
58 m_callbackInput = NULL; m_callbackOutput = NULL;
59 m_dataInput = NULL; m_dataOutput = NULL;
60 }
61
62 int m_fdInput;
63 int m_fdOutput;
64 wxSocketCallback m_callbackInput;
65 wxSocketCallback m_callbackOutput;
66 void* m_dataInput;
67 void* m_dataOutput;
68 };
69
70 typedef enum
71 { wxSocketTableInput, wxSocketTableOutput } wxSocketTableType ;
72
73 class wxSocketTable: public wxHashTable
74 {
75 public:
76 wxSocketTable(): wxHashTable(wxKEY_INTEGER)
77 {
78 }
79 ~wxSocketTable()
80 {
81 WX_CLEAR_HASH_TABLE(*this)
82 }
83
84 wxSocketTableEntry* FindEntry(int fd);
85
86 void RegisterCallback(int fd, wxSocketTableType socketType, wxSocketCallback callback, void* data);
87
88 void UnregisterCallback(int fd, wxSocketTableType socketType);
89
90 bool CallCallback(int fd, wxSocketTableType socketType);
91
92 void FillSets(fd_set* readset, fd_set* writeset, int* highest);
93
94 void ProcessEvents(fd_set* readset, fd_set* writeset);
95 };
96
97 wxSocketTableEntry* wxSocketTable::FindEntry(int fd)
98 {
99 wxSocketTableEntry* entry = (wxSocketTableEntry*) Get(fd);
100 return entry;
101 }
102
103 void wxSocketTable::RegisterCallback(int fd, wxSocketTableType socketType, wxSocketCallback callback, void* data)
104 {
105 wxSocketTableEntry* entry = FindEntry(fd);
106 if (!entry)
107 {
108 entry = new wxSocketTableEntry();
109 Put(fd, entry);
110 }
111
112 if (socketType == wxSocketTableInput)
113 {
114 entry->m_fdInput = fd;
115 entry->m_dataInput = data;
116 entry->m_callbackInput = callback;
117 }
118 else
119 {
120 entry->m_fdOutput = fd;
121 entry->m_dataOutput = data;
122 entry->m_callbackOutput = callback;
123 }
124 }
125
126 void wxSocketTable::UnregisterCallback(int fd, wxSocketTableType socketType)
127 {
128 wxSocketTableEntry* entry = FindEntry(fd);
129 if (entry)
130 {
131 if (socketType == wxSocketTableInput)
132 {
133 entry->m_fdInput = -1;
134 entry->m_dataInput = NULL;
135 entry->m_callbackInput = NULL;
136 }
137 else
138 {
139 entry->m_fdOutput = -1;
140 entry->m_dataOutput = NULL;
141 entry->m_callbackOutput = NULL;
142 }
143 if (entry->m_fdInput == -1 && entry->m_fdOutput == -1)
144 {
145 Delete(fd);
146 delete entry;
147 }
148 }
149 }
150
151 bool wxSocketTable::CallCallback(int fd, wxSocketTableType socketType)
152 {
153 wxSocketTableEntry* entry = FindEntry(fd);
154 if (entry)
155 {
156 if (socketType == wxSocketTableInput)
157 {
158 if (entry->m_fdInput != -1 && entry->m_callbackInput)
159 {
160 (entry->m_callbackInput) (entry->m_fdInput, entry->m_dataInput);
161 }
162 }
163 else
164 {
165 if (entry->m_fdOutput != -1 && entry->m_callbackOutput)
166 {
167 (entry->m_callbackOutput) (entry->m_fdOutput, entry->m_dataOutput);
168 }
169 }
170 return true;
171 }
172 else
173 return false;
174 }
175
176 void wxSocketTable::FillSets(fd_set* readset, fd_set* writeset, int* highest)
177 {
178 BeginFind();
179 wxHashTable::compatibility_iterator node = Next();
180 while (node)
181 {
182 wxSocketTableEntry* entry = (wxSocketTableEntry*) node->GetData();
183
184 if (entry->m_fdInput != -1)
185 {
186 wxFD_SET(entry->m_fdInput, readset);
187 if (entry->m_fdInput > *highest)
188 * highest = entry->m_fdInput;
189 }
190
191 if (entry->m_fdOutput != -1)
192 {
193 wxFD_SET(entry->m_fdOutput, writeset);
194 if (entry->m_fdOutput > *highest)
195 * highest = entry->m_fdOutput;
196 }
197
198 node = Next();
199 }
200 }
201
202 void wxSocketTable::ProcessEvents(fd_set* readset, fd_set* writeset)
203 {
204 BeginFind();
205 wxHashTable::compatibility_iterator node = Next();
206 while (node)
207 {
208 wxSocketTableEntry* entry = (wxSocketTableEntry*) node->GetData();
209
210 if (entry->m_fdInput != -1 && wxFD_ISSET(entry->m_fdInput, readset))
211 {
212 (entry->m_callbackInput) (entry->m_fdInput, entry->m_dataInput);
213 }
214
215 if (entry->m_fdOutput != -1 && wxFD_ISSET(entry->m_fdOutput, writeset))
216 {
217 (entry->m_callbackOutput) (entry->m_fdOutput, entry->m_dataOutput);
218 }
219
220 node = Next();
221 }
222 }
223
224 wxSocketTable* wxTheSocketTable = NULL;
225
226 class wxSocketTableModule: public wxModule
227 {
228 DECLARE_DYNAMIC_CLASS(wxSocketTableModule)
229 public:
230 wxSocketTableModule() {}
231 bool OnInit() { wxTheSocketTable = new wxSocketTable; return true; };
232 void OnExit() { delete wxTheSocketTable; wxTheSocketTable = NULL; };
233 };
234
235 IMPLEMENT_DYNAMIC_CLASS(wxSocketTableModule, wxModule)
236
237 // Implement registration functions as C functions so they
238 // can be called from gsock11.c
239
240 extern "C" void wxRegisterSocketCallback(int fd, wxSocketTableType socketType, wxSocketCallback callback, void* data)
241 {
242 if (wxTheSocketTable)
243 {
244 wxTheSocketTable->RegisterCallback(fd, socketType, callback, data);
245 }
246 }
247
248 extern "C" void wxUnregisterSocketCallback(int fd, wxSocketTableType socketType)
249 {
250 if (wxTheSocketTable)
251 {
252 wxTheSocketTable->UnregisterCallback(fd, socketType);
253 }
254 }
255 #endif
256
257 // ----------------------------------------------------------------------------
258 // wxEventLoopImpl
259 // ----------------------------------------------------------------------------
260
261 class WXDLLEXPORT wxEventLoopImpl
262 {
263 public:
264 // ctor
265 wxEventLoopImpl() { SetExitCode(0); m_keepGoing = false; }
266
267 // process an XEvent, return true if it was processed
268 bool ProcessEvent(XEvent* event);
269
270 // generate an idle message, return true if more idle time requested
271 bool SendIdleEvent();
272
273 // set/get the exit code
274 void SetExitCode(int exitcode) { m_exitcode = exitcode; }
275 int GetExitCode() const { return m_exitcode; }
276
277 public:
278 // preprocess an event, return true if processed (i.e. no further
279 // dispatching required)
280 bool PreProcessEvent(XEvent* event);
281
282 // the exit code of the event loop
283 int m_exitcode;
284
285 bool m_keepGoing;
286 };
287
288 // ============================================================================
289 // wxEventLoopImpl implementation
290 // ============================================================================
291
292 // ----------------------------------------------------------------------------
293 // wxEventLoopImpl message processing
294 // ----------------------------------------------------------------------------
295
296 bool wxEventLoopImpl::ProcessEvent(XEvent *event)
297 {
298 // give us the chance to preprocess the message first
299 if ( PreProcessEvent(event) )
300 return true;
301
302 // if it wasn't done, dispatch it to the corresponding window
303 if (wxTheApp)
304 return wxTheApp->ProcessXEvent((WXEvent*) event);
305
306 return false;
307 }
308
309 bool wxEventLoopImpl::PreProcessEvent(XEvent *event)
310 {
311 // TODO
312 #if 0
313 HWND hWnd = msg->hwnd;
314 wxWindow *wndThis = wxGetWindowFromHWND((WXHWND)hWnd);
315
316
317 // try translations first; find the youngest window with a translation
318 // table.
319 wxWindow *wnd;
320 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
321 {
322 if ( wnd->MSWTranslateMessage((WXMSG *)msg) )
323 return true;
324 }
325
326 // Anyone for a non-translation message? Try youngest descendants first.
327 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
328 {
329 if ( wnd->MSWProcessMessage((WXMSG *)msg) )
330 return true;
331 }
332 #endif
333
334 return false;
335 }
336
337 // ----------------------------------------------------------------------------
338 // wxEventLoopImpl idle event processing
339 // ----------------------------------------------------------------------------
340
341 bool wxEventLoopImpl::SendIdleEvent()
342 {
343 return wxTheApp->ProcessIdle();
344 }
345
346 // ============================================================================
347 // wxEventLoop implementation
348 // ============================================================================
349
350 // ----------------------------------------------------------------------------
351 // wxEventLoop running and exiting
352 // ----------------------------------------------------------------------------
353
354 wxEventLoop::~wxEventLoop()
355 {
356 wxASSERT_MSG( !m_impl, _T("should have been deleted in Run()") );
357 }
358
359 int wxEventLoop::Run()
360 {
361 // event loops are not recursive, you need to create another loop!
362 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
363
364 m_impl = new wxEventLoopImpl;
365
366 wxEventLoopActivator activate(this);
367
368 m_impl->m_keepGoing = true;
369 while ( m_impl->m_keepGoing )
370 {
371 #if 0 // wxUSE_THREADS
372 wxMutexGuiLeaveOrEnter();
373 #endif // wxUSE_THREADS
374
375 // generate and process idle events for as long as we don't have
376 // anything else to do
377 while ( ! Pending() )
378 {
379 #if wxUSE_TIMER
380 wxTimer::NotifyTimers(); // TODO: is this the correct place for it?
381 #endif
382 if (!m_impl->SendIdleEvent())
383 {
384 #if 0 // wxUSE_THREADS
385 // leave the main loop to give other threads a chance to
386 // perform their GUI work
387 wxMutexGuiLeave();
388 wxUsleep(20);
389 wxMutexGuiEnter();
390 #endif
391 // Break out of while loop
392 break;
393 }
394 }
395
396 // a message came or no more idle processing to do, sit in Dispatch()
397 // waiting for the next message
398 if ( !Dispatch() )
399 {
400 break;
401 }
402 }
403
404 int exitcode = m_impl->GetExitCode();
405 delete m_impl;
406 m_impl = NULL;
407
408 return exitcode;
409 }
410
411 void wxEventLoop::Exit(int rc)
412 {
413 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
414
415 m_impl->SetExitCode(rc);
416 m_impl->m_keepGoing = false;
417 }
418
419 // ----------------------------------------------------------------------------
420 // wxEventLoop message processing dispatching
421 // ----------------------------------------------------------------------------
422
423 bool wxEventLoop::Pending() const
424 {
425 XFlush( wxGlobalDisplay() );
426 return (XPending( wxGlobalDisplay() ) > 0);
427 }
428
429 bool wxEventLoop::Dispatch()
430 {
431 XEvent event;
432
433 // TODO allowing for threads, as per e.g. wxMSW
434
435 // This now waits until either an X event is received,
436 // or the select times out. So we should now process
437 // wxTimers in a reasonably timely fashion. However it
438 // does also mean that idle processing will happen more
439 // often, so we should probably limit idle processing to
440 // not be repeated more than every N milliseconds.
441
442 if (XPending( wxGlobalDisplay() ) == 0)
443 {
444 #if wxUSE_NANOX
445 GR_TIMEOUT timeout = 10; // Milliseconds
446 // Wait for next event, or timeout
447 GrGetNextEventTimeout(& event, timeout);
448
449 // Fall through to ProcessEvent.
450 // we'll assume that ProcessEvent will just ignore
451 // the event if there was a timeout and no event.
452
453 #else
454 struct timeval tv;
455 tv.tv_sec=0;
456 tv.tv_usec=10000; // TODO make this configurable
457 int fd = ConnectionNumber( wxGlobalDisplay() );
458
459 fd_set readset;
460 fd_set writeset;
461 int highest = fd;
462 wxFD_ZERO(&readset);
463 wxFD_ZERO(&writeset);
464
465 wxFD_SET(fd, &readset);
466
467 #if wxUSE_SOCKETS
468 if (wxTheSocketTable)
469 wxTheSocketTable->FillSets( &readset, &writeset, &highest );
470 #endif
471
472 if (select( highest+1, &readset, &writeset, NULL, &tv ) == 0)
473 {
474 // Timed out, so no event to process
475 return true;
476 }
477 else
478 {
479 // An X11 event was pending, so get it
480 if (wxFD_ISSET( fd, &readset ))
481 XNextEvent( wxGlobalDisplay(), &event );
482
483 #if wxUSE_SOCKETS
484 // Check if any socket events were pending,
485 // and if so, call their callbacks
486 if (wxTheSocketTable)
487 wxTheSocketTable->ProcessEvents( &readset, &writeset );
488 #endif
489 }
490 #endif
491 }
492 else
493 {
494 XNextEvent( wxGlobalDisplay(), &event );
495 }
496
497
498 (void) m_impl->ProcessEvent( &event );
499 return true;
500 }