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