]> git.saurik.com Git - wxWidgets.git/blob - src/x11/evtloop.cpp
added some missing mac headers
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "evtloop.h"
22 #endif
23
24 #include "wx/window.h"
25 #include "wx/app.h"
26 #include "wx/evtloop.h"
27 #include "wx/tooltip.h"
28 #if wxUSE_THREADS
29 #include "wx/thread.h"
30 #endif
31 #include "wx/timer.h"
32 #include "wx/hash.h"
33 #include "wx/module.h"
34 #include "wx/x11/private.h"
35 #include "X11/Xlib.h"
36
37 #include <sys/time.h>
38 #include <unistd.h>
39
40 #if wxUSE_SOCKETS
41 // ----------------------------------------------------------------------------
42 // wxSocketTable
43 // ----------------------------------------------------------------------------
44
45 typedef void (*wxSocketCallback) (int fd, void* data);
46
47 class wxSocketTableEntry: public wxObject
48 {
49 public:
50 wxSocketTableEntry()
51 {
52 m_fdInput = -1; m_fdOutput = -1;
53 m_callbackInput = NULL; m_callbackOutput = NULL;
54 m_dataInput = NULL; m_dataOutput = NULL;
55 }
56
57 int m_fdInput;
58 int m_fdOutput;
59 wxSocketCallback m_callbackInput;
60 wxSocketCallback m_callbackOutput;
61 void* m_dataInput;
62 void* m_dataOutput;
63 };
64
65 typedef enum
66 { wxSocketTableInput, wxSocketTableOutput } wxSocketTableType ;
67
68 class wxSocketTable: public wxHashTable
69 {
70 public:
71 wxSocketTable(): wxHashTable(wxKEY_INTEGER)
72 {
73 }
74 ~wxSocketTable()
75 {
76 WX_CLEAR_HASH_TABLE(*this)
77 }
78
79 wxSocketTableEntry* FindEntry(int fd);
80
81 void RegisterCallback(int fd, wxSocketTableType socketType, wxSocketCallback callback, void* data);
82
83 void UnregisterCallback(int fd, wxSocketTableType socketType);
84
85 bool CallCallback(int fd, wxSocketTableType socketType);
86
87 void FillSets(fd_set* readset, fd_set* writeset, int* highest);
88
89 void ProcessEvents(fd_set* readset, fd_set* writeset);
90 };
91
92 wxSocketTableEntry* wxSocketTable::FindEntry(int fd)
93 {
94 wxSocketTableEntry* entry = (wxSocketTableEntry*) Get(fd);
95 return entry;
96 }
97
98 void wxSocketTable::RegisterCallback(int fd, wxSocketTableType socketType, wxSocketCallback callback, void* data)
99 {
100 wxSocketTableEntry* entry = FindEntry(fd);
101 if (!entry)
102 {
103 entry = new wxSocketTableEntry();
104 Put(fd, entry);
105 }
106
107 if (socketType == wxSocketTableInput)
108 {
109 entry->m_fdInput = fd;
110 entry->m_dataInput = data;
111 entry->m_callbackInput = callback;
112 }
113 else
114 {
115 entry->m_fdOutput = fd;
116 entry->m_dataOutput = data;
117 entry->m_callbackOutput = callback;
118 }
119 }
120
121 void wxSocketTable::UnregisterCallback(int fd, wxSocketTableType socketType)
122 {
123 wxSocketTableEntry* entry = FindEntry(fd);
124 if (entry)
125 {
126 if (socketType == wxSocketTableInput)
127 {
128 entry->m_fdInput = -1;
129 entry->m_dataInput = NULL;
130 entry->m_callbackInput = NULL;
131 }
132 else
133 {
134 entry->m_fdOutput = -1;
135 entry->m_dataOutput = NULL;
136 entry->m_callbackOutput = NULL;
137 }
138 if (entry->m_fdInput == -1 && entry->m_fdOutput == -1)
139 {
140 Delete(fd);
141 delete entry;
142 }
143 }
144 }
145
146 bool wxSocketTable::CallCallback(int fd, wxSocketTableType socketType)
147 {
148 wxSocketTableEntry* entry = FindEntry(fd);
149 if (entry)
150 {
151 if (socketType == wxSocketTableInput)
152 {
153 if (entry->m_fdInput != -1 && entry->m_callbackInput)
154 {
155 (entry->m_callbackInput) (entry->m_fdInput, entry->m_dataInput);
156 }
157 }
158 else
159 {
160 if (entry->m_fdOutput != -1 && entry->m_callbackOutput)
161 {
162 (entry->m_callbackOutput) (entry->m_fdOutput, entry->m_dataOutput);
163 }
164 }
165 return TRUE;
166 }
167 else
168 return FALSE;
169 }
170
171 void wxSocketTable::FillSets(fd_set* readset, fd_set* writeset, int* highest)
172 {
173 BeginFind();
174 wxHashTable::compatibility_iterator node = Next();
175 while (node)
176 {
177 wxSocketTableEntry* entry = (wxSocketTableEntry*) node->GetData();
178
179 if (entry->m_fdInput != -1)
180 {
181 FD_SET(entry->m_fdInput, readset);
182 if (entry->m_fdInput > *highest)
183 * highest = entry->m_fdInput;
184 }
185
186 if (entry->m_fdOutput != -1)
187 {
188 FD_SET(entry->m_fdOutput, writeset);
189 if (entry->m_fdOutput > *highest)
190 * highest = entry->m_fdOutput;
191 }
192
193 node = Next();
194 }
195 }
196
197 void wxSocketTable::ProcessEvents(fd_set* readset, fd_set* writeset)
198 {
199 BeginFind();
200 wxHashTable::compatibility_iterator node = Next();
201 while (node)
202 {
203 wxSocketTableEntry* entry = (wxSocketTableEntry*) node->GetData();
204
205 if (entry->m_fdInput != -1 && FD_ISSET(entry->m_fdInput, readset))
206 {
207 (entry->m_callbackInput) (entry->m_fdInput, entry->m_dataInput);
208 }
209
210 if (entry->m_fdOutput != -1 && FD_ISSET(entry->m_fdOutput, writeset))
211 {
212 (entry->m_callbackOutput) (entry->m_fdOutput, entry->m_dataOutput);
213 }
214
215 node = Next();
216 }
217 }
218
219 wxSocketTable* wxTheSocketTable = NULL;
220
221 class wxSocketTableModule: public wxModule
222 {
223 DECLARE_DYNAMIC_CLASS(wxSocketTableModule)
224 public:
225 wxSocketTableModule() {}
226 bool OnInit() { wxTheSocketTable = new wxSocketTable; return TRUE; };
227 void OnExit() { delete wxTheSocketTable; wxTheSocketTable = NULL; };
228 };
229
230 IMPLEMENT_DYNAMIC_CLASS(wxSocketTableModule, wxModule)
231
232 // Implement registration functions as C functions so they
233 // can be called from gsock11.c
234
235 extern "C" void wxRegisterSocketCallback(int fd, wxSocketTableType socketType, wxSocketCallback callback, void* data)
236 {
237 if (wxTheSocketTable)
238 {
239 wxTheSocketTable->RegisterCallback(fd, socketType, callback, data);
240 }
241 }
242
243 extern "C" void wxUnregisterSocketCallback(int fd, wxSocketTableType socketType)
244 {
245 if (wxTheSocketTable)
246 {
247 wxTheSocketTable->UnregisterCallback(fd, socketType);
248 }
249 }
250 #endif
251
252 // ----------------------------------------------------------------------------
253 // wxEventLoopImpl
254 // ----------------------------------------------------------------------------
255
256 class WXDLLEXPORT wxEventLoopImpl
257 {
258 public:
259 // ctor
260 wxEventLoopImpl() { SetExitCode(0); m_keepGoing = FALSE; }
261
262 // process an XEvent, return TRUE if it was processed
263 bool ProcessEvent(XEvent* event);
264
265 // generate an idle message, return TRUE if more idle time requested
266 bool SendIdleEvent();
267
268 // set/get the exit code
269 void SetExitCode(int exitcode) { m_exitcode = exitcode; }
270 int GetExitCode() const { return m_exitcode; }
271
272 public:
273 // preprocess an event, return TRUE if processed (i.e. no further
274 // dispatching required)
275 bool PreProcessEvent(XEvent* event);
276
277 // the exit code of the event loop
278 int m_exitcode;
279
280 bool m_keepGoing;
281 };
282
283 // ============================================================================
284 // wxEventLoopImpl implementation
285 // ============================================================================
286
287 // ----------------------------------------------------------------------------
288 // wxEventLoopImpl message processing
289 // ----------------------------------------------------------------------------
290
291 bool wxEventLoopImpl::ProcessEvent(XEvent *event)
292 {
293 // give us the chance to preprocess the message first
294 if ( PreProcessEvent(event) )
295 return TRUE;
296
297 // if it wasn't done, dispatch it to the corresponding window
298 if (wxTheApp)
299 return wxTheApp->ProcessXEvent((WXEvent*) event);
300
301 return FALSE;
302 }
303
304 bool wxEventLoopImpl::PreProcessEvent(XEvent *event)
305 {
306 // TODO
307 #if 0
308 HWND hWnd = msg->hwnd;
309 wxWindow *wndThis = wxGetWindowFromHWND((WXHWND)hWnd);
310
311
312 // try translations first; find the youngest window with a translation
313 // table.
314 wxWindow *wnd;
315 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
316 {
317 if ( wnd->MSWTranslateMessage((WXMSG *)msg) )
318 return TRUE;
319 }
320
321 // Anyone for a non-translation message? Try youngest descendants first.
322 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
323 {
324 if ( wnd->MSWProcessMessage((WXMSG *)msg) )
325 return TRUE;
326 }
327 #endif
328
329 return FALSE;
330 }
331
332 // ----------------------------------------------------------------------------
333 // wxEventLoopImpl idle event processing
334 // ----------------------------------------------------------------------------
335
336 bool wxEventLoopImpl::SendIdleEvent()
337 {
338 return wxTheApp->ProcessIdle();
339 }
340
341 // ============================================================================
342 // wxEventLoop implementation
343 // ============================================================================
344
345 wxEventLoop *wxEventLoopBase::ms_activeLoop = NULL;
346
347 // ----------------------------------------------------------------------------
348 // wxEventLoop running and exiting
349 // ----------------------------------------------------------------------------
350
351 wxEventLoop::~wxEventLoop()
352 {
353 wxASSERT_MSG( !m_impl, _T("should have been deleted in Run()") );
354 }
355
356 int wxEventLoop::Run()
357 {
358 // event loops are not recursive, you need to create another loop!
359 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
360
361 m_impl = new wxEventLoopImpl;
362
363 wxEventLoop *oldLoop = ms_activeLoop;
364 ms_activeLoop = this;
365
366 m_impl->m_keepGoing = TRUE;
367 while ( m_impl->m_keepGoing )
368 {
369 #if 0 // wxUSE_THREADS
370 wxMutexGuiLeaveOrEnter();
371 #endif // wxUSE_THREADS
372
373 // generate and process idle events for as long as we don't have
374 // anything else to do
375 while ( ! Pending() )
376 {
377 #if wxUSE_TIMER
378 wxTimer::NotifyTimers(); // TODO: is this the correct place for it?
379 #endif
380 if (!m_impl->SendIdleEvent())
381 {
382 #if 0 // wxUSE_THREADS
383 // leave the main loop to give other threads a chance to
384 // perform their GUI work
385 wxMutexGuiLeave();
386 wxUsleep(20);
387 wxMutexGuiEnter();
388 #endif
389 // Break out of while loop
390 break;
391 }
392 }
393
394 // a message came or no more idle processing to do, sit in Dispatch()
395 // waiting for the next message
396 if ( !Dispatch() )
397 {
398 break;
399 }
400 }
401
402 int exitcode = m_impl->GetExitCode();
403 delete m_impl;
404 m_impl = NULL;
405
406 ms_activeLoop = oldLoop;
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 FD_ZERO(&readset);
463 FD_ZERO(&writeset);
464
465 FD_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 (FD_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 }
501