]>
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 | } | |
3754265e | 56 | |
52127426 JS |
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(); |
3754265e | 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(); |
3754265e | 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; | |
3754265e | 296 | |
086fd560 JS |
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 | ||
3754265e | 345 | wxEventLoop *wxEventLoopBase::ms_activeLoop = NULL; |
1b0fb34b JS |
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 | ||
1b0fb34b JS |
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; | |
3754265e | 362 | |
1b0fb34b JS |
363 | wxEventLoop *oldLoop = ms_activeLoop; |
364 | ms_activeLoop = this; | |
365 | ||
366 | m_impl->m_keepGoing = TRUE; | |
367 | while ( m_impl->m_keepGoing ) | |
368 | { | |
7266b672 | 369 | #if 0 // wxUSE_THREADS |
2b5f62a0 | 370 | wxMutexGuiLeaveOrEnter(); |
1b0fb34b JS |
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 | { | |
b555c37c | 377 | #if wxUSE_TIMER |
b513212d | 378 | wxTimer::NotifyTimers(); // TODO: is this the correct place for it? |
b555c37c | 379 | #endif |
1b0fb34b JS |
380 | if (!m_impl->SendIdleEvent()) |
381 | { | |
2b5f62a0 | 382 | #if 0 // wxUSE_THREADS |
1b0fb34b JS |
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 | { | |
2b5f62a0 VZ |
425 | XFlush( wxGlobalDisplay() ); |
426 | return (XPending( wxGlobalDisplay() ) > 0); | |
1b0fb34b JS |
427 | } |
428 | ||
429 | bool wxEventLoop::Dispatch() | |
430 | { | |
431 | XEvent event; | |
432 | ||
433 | // TODO allowing for threads, as per e.g. wxMSW | |
434 | ||
1016f0de JS |
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. | |
3754265e | 441 | |
2b5f62a0 | 442 | if (XPending( wxGlobalDisplay() ) == 0) |
1016f0de | 443 | { |
868741e9 JS |
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. | |
3754265e | 452 | |
868741e9 | 453 | #else |
1016f0de JS |
454 | struct timeval tv; |
455 | tv.tv_sec=0; | |
456 | tv.tv_usec=10000; // TODO make this configurable | |
2b5f62a0 | 457 | int fd = ConnectionNumber( wxGlobalDisplay() ); |
3754265e | 458 | |
1016f0de | 459 | fd_set readset; |
52127426 JS |
460 | fd_set writeset; |
461 | int highest = fd; | |
1016f0de | 462 | FD_ZERO(&readset); |
52127426 | 463 | FD_ZERO(&writeset); |
3754265e | 464 | |
1016f0de | 465 | FD_SET(fd, &readset); |
52127426 | 466 | |
2d1084ea | 467 | #if wxUSE_SOCKETS |
52127426 | 468 | if (wxTheSocketTable) |
2b5f62a0 | 469 | wxTheSocketTable->FillSets( &readset, &writeset, &highest ); |
2d1084ea | 470 | #endif |
3754265e | 471 | |
2b5f62a0 | 472 | if (select( highest+1, &readset, &writeset, NULL, &tv ) == 0) |
1016f0de JS |
473 | { |
474 | // Timed out, so no event to process | |
475 | return TRUE; | |
476 | } | |
477 | else | |
478 | { | |
52127426 | 479 | // An X11 event was pending, so get it |
2b5f62a0 VZ |
480 | if (FD_ISSET( fd, &readset )) |
481 | XNextEvent( wxGlobalDisplay(), &event ); | |
52127426 | 482 | |
2d1084ea | 483 | #if wxUSE_SOCKETS |
52127426 JS |
484 | // Check if any socket events were pending, |
485 | // and if so, call their callbacks | |
486 | if (wxTheSocketTable) | |
2b5f62a0 | 487 | wxTheSocketTable->ProcessEvents( &readset, &writeset ); |
2d1084ea | 488 | #endif |
1016f0de | 489 | } |
868741e9 | 490 | #endif |
3754265e | 491 | } |
2b5f62a0 | 492 | else |
1016f0de | 493 | { |
2b5f62a0 | 494 | XNextEvent( wxGlobalDisplay(), &event ); |
1016f0de | 495 | } |
3754265e VZ |
496 | |
497 | ||
2b5f62a0 | 498 | (void) m_impl->ProcessEvent( &event ); |
1b0fb34b JS |
499 | return TRUE; |
500 | } | |
501 |