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