]> git.saurik.com Git - wxWidgets.git/blame - src/x11/evtloop.cpp
call gtk_toolbar_set_tooltips() from GtkSetStyle(); removed erroneous wxTB_TOOLTIPS
[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
bd00fe32
VZ
433 // Start off by checking if any of our child processes have finished.
434 wxCheckForFinishedChildren();
435
1b0fb34b
JS
436 // TODO allowing for threads, as per e.g. wxMSW
437
1016f0de
JS
438 // This now waits until either an X event is received,
439 // or the select times out. So we should now process
440 // wxTimers in a reasonably timely fashion. However it
441 // does also mean that idle processing will happen more
442 // often, so we should probably limit idle processing to
443 // not be repeated more than every N milliseconds.
3754265e 444
2b5f62a0 445 if (XPending( wxGlobalDisplay() ) == 0)
1016f0de 446 {
868741e9
JS
447#if wxUSE_NANOX
448 GR_TIMEOUT timeout = 10; // Milliseconds
449 // Wait for next event, or timeout
450 GrGetNextEventTimeout(& event, timeout);
451
452 // Fall through to ProcessEvent.
453 // we'll assume that ProcessEvent will just ignore
454 // the event if there was a timeout and no event.
3754265e 455
868741e9 456#else
1016f0de
JS
457 struct timeval tv;
458 tv.tv_sec=0;
459 tv.tv_usec=10000; // TODO make this configurable
2b5f62a0 460 int fd = ConnectionNumber( wxGlobalDisplay() );
3754265e 461
1016f0de 462 fd_set readset;
52127426
JS
463 fd_set writeset;
464 int highest = fd;
17a1ebd1
VZ
465 wxFD_ZERO(&readset);
466 wxFD_ZERO(&writeset);
3754265e 467
17a1ebd1 468 wxFD_SET(fd, &readset);
52127426 469
2d1084ea 470#if wxUSE_SOCKETS
52127426 471 if (wxTheSocketTable)
2b5f62a0 472 wxTheSocketTable->FillSets( &readset, &writeset, &highest );
2d1084ea 473#endif
3754265e 474
2b5f62a0 475 if (select( highest+1, &readset, &writeset, NULL, &tv ) == 0)
1016f0de
JS
476 {
477 // Timed out, so no event to process
32d4c30a 478 return true;
1016f0de
JS
479 }
480 else
481 {
52127426 482 // An X11 event was pending, so get it
17a1ebd1 483 if (wxFD_ISSET( fd, &readset ))
2b5f62a0 484 XNextEvent( wxGlobalDisplay(), &event );
52127426 485
2d1084ea 486#if wxUSE_SOCKETS
52127426
JS
487 // Check if any socket events were pending,
488 // and if so, call their callbacks
489 if (wxTheSocketTable)
2b5f62a0 490 wxTheSocketTable->ProcessEvents( &readset, &writeset );
2d1084ea 491#endif
1016f0de 492 }
868741e9 493#endif
3754265e 494 }
2b5f62a0 495 else
1016f0de 496 {
2b5f62a0 497 XNextEvent( wxGlobalDisplay(), &event );
1016f0de 498 }
3754265e
VZ
499
500
2b5f62a0 501 (void) m_impl->ProcessEvent( &event );
32d4c30a 502 return true;
1b0fb34b 503}