]>
Commit | Line | Data |
---|---|---|
1b0fb34b JS |
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 | |
65571936 | 9 | // License: wxWindows licence |
1b0fb34b JS |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
14f355c2 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
1b0fb34b JS |
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" | |
7266b672 JS |
28 | #if wxUSE_THREADS |
29 | #include "wx/thread.h" | |
30 | #endif | |
b513212d | 31 | #include "wx/timer.h" |
52127426 JS |
32 | #include "wx/hash.h" |
33 | #include "wx/module.h" | |
1b0fb34b JS |
34 | #include "wx/x11/private.h" |
35 | #include "X11/Xlib.h" | |
36 | ||
1016f0de JS |
37 | #include <sys/time.h> |
38 | #include <unistd.h> | |
39 | ||
2d1084ea | 40 | #if wxUSE_SOCKETS |
52127426 JS |
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 | { | |
ac32ba44 | 76 | WX_CLEAR_HASH_TABLE(*this) |
52127426 JS |
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(); | |
ac32ba44 | 174 | wxHashTable::compatibility_iterator node = Next(); |
52127426 JS |
175 | while (node) |
176 | { | |
09a1dffa | 177 | wxSocketTableEntry* entry = (wxSocketTableEntry*) node->GetData(); |
2b5f62a0 | 178 | |
52127426 JS |
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(); | |
ac32ba44 | 200 | wxHashTable::compatibility_iterator node = Next(); |
52127426 JS |
201 | while (node) |
202 | { | |
09a1dffa | 203 | wxSocketTableEntry* entry = (wxSocketTableEntry*) node->GetData(); |
2b5f62a0 | 204 | |
52127426 JS |
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 | } | |
2d1084ea | 250 | #endif |
52127426 | 251 | |
1b0fb34b JS |
252 | // ---------------------------------------------------------------------------- |
253 | // wxEventLoopImpl | |
254 | // ---------------------------------------------------------------------------- | |
255 | ||
256 | class WXDLLEXPORT wxEventLoopImpl | |
257 | { | |
258 | public: | |
259 | // ctor | |
260 | wxEventLoopImpl() { SetExitCode(0); m_keepGoing = FALSE; } | |
261 | ||
086fd560 JS |
262 | // process an XEvent, return TRUE if it was processed |
263 | bool ProcessEvent(XEvent* event); | |
1b0fb34b JS |
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) | |
7266b672 | 275 | bool PreProcessEvent(XEvent* event); |
1b0fb34b JS |
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 | ||
086fd560 | 291 | bool wxEventLoopImpl::ProcessEvent(XEvent *event) |
1b0fb34b JS |
292 | { |
293 | // give us the chance to preprocess the message first | |
086fd560 JS |
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; | |
1b0fb34b JS |
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 | { | |
e39af974 | 338 | return wxTheApp->ProcessIdle(); |
1b0fb34b JS |
339 | } |
340 | ||
341 | // ============================================================================ | |
342 | // wxEventLoop implementation | |
343 | // ============================================================================ | |
344 | ||
345 | wxEventLoop *wxEventLoop::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 | bool wxEventLoop::IsRunning() const | |
357 | { | |
358 | return m_impl != NULL; | |
359 | } | |
360 | ||
361 | int wxEventLoop::Run() | |
362 | { | |
363 | // event loops are not recursive, you need to create another loop! | |
364 | wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") ); | |
365 | ||
366 | m_impl = new wxEventLoopImpl; | |
367 | ||
368 | wxEventLoop *oldLoop = ms_activeLoop; | |
369 | ms_activeLoop = this; | |
370 | ||
371 | m_impl->m_keepGoing = TRUE; | |
372 | while ( m_impl->m_keepGoing ) | |
373 | { | |
7266b672 | 374 | #if 0 // wxUSE_THREADS |
2b5f62a0 | 375 | wxMutexGuiLeaveOrEnter(); |
1b0fb34b JS |
376 | #endif // wxUSE_THREADS |
377 | ||
378 | // generate and process idle events for as long as we don't have | |
379 | // anything else to do | |
380 | while ( ! Pending() ) | |
381 | { | |
b555c37c | 382 | #if wxUSE_TIMER |
b513212d | 383 | wxTimer::NotifyTimers(); // TODO: is this the correct place for it? |
b555c37c | 384 | #endif |
1b0fb34b JS |
385 | if (!m_impl->SendIdleEvent()) |
386 | { | |
2b5f62a0 | 387 | #if 0 // wxUSE_THREADS |
1b0fb34b JS |
388 | // leave the main loop to give other threads a chance to |
389 | // perform their GUI work | |
390 | wxMutexGuiLeave(); | |
391 | wxUsleep(20); | |
392 | wxMutexGuiEnter(); | |
393 | #endif | |
394 | // Break out of while loop | |
395 | break; | |
396 | } | |
397 | } | |
398 | ||
399 | // a message came or no more idle processing to do, sit in Dispatch() | |
400 | // waiting for the next message | |
401 | if ( !Dispatch() ) | |
402 | { | |
403 | break; | |
404 | } | |
405 | } | |
406 | ||
407 | int exitcode = m_impl->GetExitCode(); | |
408 | delete m_impl; | |
409 | m_impl = NULL; | |
410 | ||
411 | ms_activeLoop = oldLoop; | |
412 | ||
413 | return exitcode; | |
414 | } | |
415 | ||
416 | void wxEventLoop::Exit(int rc) | |
417 | { | |
418 | wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") ); | |
419 | ||
420 | m_impl->SetExitCode(rc); | |
421 | m_impl->m_keepGoing = FALSE; | |
422 | } | |
423 | ||
424 | // ---------------------------------------------------------------------------- | |
425 | // wxEventLoop message processing dispatching | |
426 | // ---------------------------------------------------------------------------- | |
427 | ||
428 | bool wxEventLoop::Pending() const | |
429 | { | |
2b5f62a0 VZ |
430 | XFlush( wxGlobalDisplay() ); |
431 | return (XPending( wxGlobalDisplay() ) > 0); | |
1b0fb34b JS |
432 | } |
433 | ||
434 | bool wxEventLoop::Dispatch() | |
435 | { | |
436 | XEvent event; | |
437 | ||
438 | // TODO allowing for threads, as per e.g. wxMSW | |
439 | ||
1016f0de JS |
440 | // This now waits until either an X event is received, |
441 | // or the select times out. So we should now process | |
442 | // wxTimers in a reasonably timely fashion. However it | |
443 | // does also mean that idle processing will happen more | |
444 | // often, so we should probably limit idle processing to | |
445 | // not be repeated more than every N milliseconds. | |
446 | ||
2b5f62a0 | 447 | if (XPending( wxGlobalDisplay() ) == 0) |
1016f0de | 448 | { |
868741e9 JS |
449 | #if wxUSE_NANOX |
450 | GR_TIMEOUT timeout = 10; // Milliseconds | |
451 | // Wait for next event, or timeout | |
452 | GrGetNextEventTimeout(& event, timeout); | |
453 | ||
454 | // Fall through to ProcessEvent. | |
455 | // we'll assume that ProcessEvent will just ignore | |
456 | // the event if there was a timeout and no event. | |
457 | ||
458 | #else | |
1016f0de JS |
459 | struct timeval tv; |
460 | tv.tv_sec=0; | |
461 | tv.tv_usec=10000; // TODO make this configurable | |
2b5f62a0 VZ |
462 | int fd = ConnectionNumber( wxGlobalDisplay() ); |
463 | ||
1016f0de | 464 | fd_set readset; |
52127426 JS |
465 | fd_set writeset; |
466 | int highest = fd; | |
1016f0de | 467 | FD_ZERO(&readset); |
52127426 JS |
468 | FD_ZERO(&writeset); |
469 | ||
1016f0de | 470 | FD_SET(fd, &readset); |
52127426 | 471 | |
2d1084ea | 472 | #if wxUSE_SOCKETS |
52127426 | 473 | if (wxTheSocketTable) |
2b5f62a0 | 474 | wxTheSocketTable->FillSets( &readset, &writeset, &highest ); |
2d1084ea | 475 | #endif |
52127426 | 476 | |
2b5f62a0 | 477 | if (select( highest+1, &readset, &writeset, NULL, &tv ) == 0) |
1016f0de JS |
478 | { |
479 | // Timed out, so no event to process | |
480 | return TRUE; | |
481 | } | |
482 | else | |
483 | { | |
52127426 | 484 | // An X11 event was pending, so get it |
2b5f62a0 VZ |
485 | if (FD_ISSET( fd, &readset )) |
486 | XNextEvent( wxGlobalDisplay(), &event ); | |
52127426 | 487 | |
2d1084ea | 488 | #if wxUSE_SOCKETS |
52127426 JS |
489 | // Check if any socket events were pending, |
490 | // and if so, call their callbacks | |
491 | if (wxTheSocketTable) | |
2b5f62a0 | 492 | wxTheSocketTable->ProcessEvents( &readset, &writeset ); |
2d1084ea | 493 | #endif |
1016f0de | 494 | } |
868741e9 | 495 | #endif |
2b5f62a0 VZ |
496 | } |
497 | else | |
1016f0de | 498 | { |
2b5f62a0 | 499 | XNextEvent( wxGlobalDisplay(), &event ); |
1016f0de JS |
500 | } |
501 | ||
2b5f62a0 VZ |
502 | |
503 | (void) m_impl->ProcessEvent( &event ); | |
1b0fb34b JS |
504 | return TRUE; |
505 | } | |
506 |