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