]>
Commit | Line | Data |
---|---|---|
1b0fb34b | 1 | /////////////////////////////////////////////////////////////////////////////// |
32d4c30a | 2 | // Name: src/x11/evtloop.cpp |
1b0fb34b JS |
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 | ||
32d4c30a WS |
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 | ||
1b0fb34b JS |
27 | #include "wx/window.h" |
28 | #include "wx/app.h" | |
29 | #include "wx/evtloop.h" | |
30 | #include "wx/tooltip.h" | |
b513212d | 31 | #include "wx/timer.h" |
52127426 | 32 | #include "wx/module.h" |
17a1ebd1 | 33 | #include "wx/unix/private.h" |
1b0fb34b JS |
34 | #include "wx/x11/private.h" |
35 | #include "X11/Xlib.h" | |
36 | ||
32d4c30a WS |
37 | #if wxUSE_THREADS |
38 | #include "wx/thread.h" | |
39 | #endif | |
40 | ||
1016f0de JS |
41 | #include <sys/time.h> |
42 | #include <unistd.h> | |
43 | ||
2d1084ea | 44 | #if wxUSE_SOCKETS |
52127426 JS |
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 | } | |
3754265e | 60 | |
52127426 JS |
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 | { | |
ac32ba44 | 80 | WX_CLEAR_HASH_TABLE(*this) |
52127426 JS |
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 | } | |
32d4c30a | 169 | return true; |
52127426 JS |
170 | } |
171 | else | |
32d4c30a | 172 | return false; |
52127426 JS |
173 | } |
174 | ||
175 | void wxSocketTable::FillSets(fd_set* readset, fd_set* writeset, int* highest) | |
176 | { | |
177 | BeginFind(); | |
ac32ba44 | 178 | wxHashTable::compatibility_iterator node = Next(); |
52127426 JS |
179 | while (node) |
180 | { | |
09a1dffa | 181 | wxSocketTableEntry* entry = (wxSocketTableEntry*) node->GetData(); |
3754265e | 182 | |
52127426 JS |
183 | if (entry->m_fdInput != -1) |
184 | { | |
17a1ebd1 | 185 | wxFD_SET(entry->m_fdInput, readset); |
52127426 JS |
186 | if (entry->m_fdInput > *highest) |
187 | * highest = entry->m_fdInput; | |
188 | } | |
189 | ||
190 | if (entry->m_fdOutput != -1) | |
191 | { | |
17a1ebd1 | 192 | wxFD_SET(entry->m_fdOutput, writeset); |
52127426 JS |
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(); | |
ac32ba44 | 204 | wxHashTable::compatibility_iterator node = Next(); |
52127426 JS |
205 | while (node) |
206 | { | |
09a1dffa | 207 | wxSocketTableEntry* entry = (wxSocketTableEntry*) node->GetData(); |
3754265e | 208 | |
17a1ebd1 | 209 | if (entry->m_fdInput != -1 && wxFD_ISSET(entry->m_fdInput, readset)) |
52127426 JS |
210 | { |
211 | (entry->m_callbackInput) (entry->m_fdInput, entry->m_dataInput); | |
212 | } | |
213 | ||
17a1ebd1 | 214 | if (entry->m_fdOutput != -1 && wxFD_ISSET(entry->m_fdOutput, writeset)) |
52127426 JS |
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() {} | |
32d4c30a | 230 | bool OnInit() { wxTheSocketTable = new wxSocketTable; return true; }; |
52127426 JS |
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 | } | |
2d1084ea | 254 | #endif |
52127426 | 255 | |
1b0fb34b JS |
256 | // ---------------------------------------------------------------------------- |
257 | // wxEventLoopImpl | |
258 | // ---------------------------------------------------------------------------- | |
259 | ||
260 | class WXDLLEXPORT wxEventLoopImpl | |
261 | { | |
262 | public: | |
263 | // ctor | |
32d4c30a | 264 | wxEventLoopImpl() { SetExitCode(0); m_keepGoing = false; } |
1b0fb34b | 265 | |
32d4c30a | 266 | // process an XEvent, return true if it was processed |
086fd560 | 267 | bool ProcessEvent(XEvent* event); |
1b0fb34b | 268 | |
32d4c30a | 269 | // generate an idle message, return true if more idle time requested |
1b0fb34b JS |
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: | |
32d4c30a | 277 | // preprocess an event, return true if processed (i.e. no further |
1b0fb34b | 278 | // dispatching required) |
7266b672 | 279 | bool PreProcessEvent(XEvent* event); |
1b0fb34b JS |
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 | ||
086fd560 | 295 | bool wxEventLoopImpl::ProcessEvent(XEvent *event) |
1b0fb34b JS |
296 | { |
297 | // give us the chance to preprocess the message first | |
086fd560 | 298 | if ( PreProcessEvent(event) ) |
32d4c30a | 299 | return true; |
3754265e | 300 | |
086fd560 JS |
301 | // if it wasn't done, dispatch it to the corresponding window |
302 | if (wxTheApp) | |
303 | return wxTheApp->ProcessXEvent((WXEvent*) event); | |
304 | ||
32d4c30a | 305 | return false; |
1b0fb34b JS |
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) ) | |
32d4c30a | 322 | return true; |
1b0fb34b JS |
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) ) | |
32d4c30a | 329 | return true; |
1b0fb34b JS |
330 | } |
331 | #endif | |
332 | ||
32d4c30a | 333 | return false; |
1b0fb34b JS |
334 | } |
335 | ||
336 | // ---------------------------------------------------------------------------- | |
337 | // wxEventLoopImpl idle event processing | |
338 | // ---------------------------------------------------------------------------- | |
339 | ||
340 | bool wxEventLoopImpl::SendIdleEvent() | |
341 | { | |
e39af974 | 342 | return wxTheApp->ProcessIdle(); |
1b0fb34b JS |
343 | } |
344 | ||
345 | // ============================================================================ | |
346 | // wxEventLoop implementation | |
347 | // ============================================================================ | |
348 | ||
1b0fb34b JS |
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 | ||
1b0fb34b JS |
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; | |
3754265e | 364 | |
77fb1a02 | 365 | wxEventLoopActivator activate(this); |
1b0fb34b | 366 | |
32d4c30a | 367 | m_impl->m_keepGoing = true; |
1b0fb34b JS |
368 | while ( m_impl->m_keepGoing ) |
369 | { | |
7266b672 | 370 | #if 0 // wxUSE_THREADS |
2b5f62a0 | 371 | wxMutexGuiLeaveOrEnter(); |
1b0fb34b JS |
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 | { | |
b555c37c | 378 | #if wxUSE_TIMER |
b513212d | 379 | wxTimer::NotifyTimers(); // TODO: is this the correct place for it? |
b555c37c | 380 | #endif |
1b0fb34b JS |
381 | if (!m_impl->SendIdleEvent()) |
382 | { | |
2b5f62a0 | 383 | #if 0 // wxUSE_THREADS |
1b0fb34b JS |
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 | ||
1b0fb34b JS |
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); | |
32d4c30a | 415 | m_impl->m_keepGoing = false; |
1b0fb34b JS |
416 | } |
417 | ||
418 | // ---------------------------------------------------------------------------- | |
419 | // wxEventLoop message processing dispatching | |
420 | // ---------------------------------------------------------------------------- | |
421 | ||
422 | bool wxEventLoop::Pending() const | |
423 | { | |
2b5f62a0 VZ |
424 | XFlush( wxGlobalDisplay() ); |
425 | return (XPending( wxGlobalDisplay() ) > 0); | |
1b0fb34b JS |
426 | } |
427 | ||
428 | bool wxEventLoop::Dispatch() | |
429 | { | |
430 | XEvent event; | |
431 | ||
432 | // TODO allowing for threads, as per e.g. wxMSW | |
433 | ||
1016f0de JS |
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. | |
3754265e | 440 | |
2b5f62a0 | 441 | if (XPending( wxGlobalDisplay() ) == 0) |
1016f0de | 442 | { |
868741e9 JS |
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. | |
3754265e | 451 | |
868741e9 | 452 | #else |
1016f0de JS |
453 | struct timeval tv; |
454 | tv.tv_sec=0; | |
455 | tv.tv_usec=10000; // TODO make this configurable | |
2b5f62a0 | 456 | int fd = ConnectionNumber( wxGlobalDisplay() ); |
3754265e | 457 | |
1016f0de | 458 | fd_set readset; |
52127426 JS |
459 | fd_set writeset; |
460 | int highest = fd; | |
17a1ebd1 VZ |
461 | wxFD_ZERO(&readset); |
462 | wxFD_ZERO(&writeset); | |
3754265e | 463 | |
17a1ebd1 | 464 | wxFD_SET(fd, &readset); |
52127426 | 465 | |
2d1084ea | 466 | #if wxUSE_SOCKETS |
52127426 | 467 | if (wxTheSocketTable) |
2b5f62a0 | 468 | wxTheSocketTable->FillSets( &readset, &writeset, &highest ); |
2d1084ea | 469 | #endif |
3754265e | 470 | |
2b5f62a0 | 471 | if (select( highest+1, &readset, &writeset, NULL, &tv ) == 0) |
1016f0de JS |
472 | { |
473 | // Timed out, so no event to process | |
32d4c30a | 474 | return true; |
1016f0de JS |
475 | } |
476 | else | |
477 | { | |
52127426 | 478 | // An X11 event was pending, so get it |
17a1ebd1 | 479 | if (wxFD_ISSET( fd, &readset )) |
2b5f62a0 | 480 | XNextEvent( wxGlobalDisplay(), &event ); |
52127426 | 481 | |
2d1084ea | 482 | #if wxUSE_SOCKETS |
52127426 JS |
483 | // Check if any socket events were pending, |
484 | // and if so, call their callbacks | |
485 | if (wxTheSocketTable) | |
2b5f62a0 | 486 | wxTheSocketTable->ProcessEvents( &readset, &writeset ); |
2d1084ea | 487 | #endif |
1016f0de | 488 | } |
868741e9 | 489 | #endif |
3754265e | 490 | } |
2b5f62a0 | 491 | else |
1016f0de | 492 | { |
2b5f62a0 | 493 | XNextEvent( wxGlobalDisplay(), &event ); |
1016f0de | 494 | } |
3754265e VZ |
495 | |
496 | ||
2b5f62a0 | 497 | (void) m_impl->ProcessEvent( &event ); |
32d4c30a | 498 | return true; |
1b0fb34b | 499 | } |