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