]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/x11/evtloop.cpp
call gtk_toolbar_set_tooltips() from GtkSetStyle(); removed erroneous wxTB_TOOLTIPS
[wxWidgets.git] / src / x11 / evtloop.cpp
... / ...
CommitLineData
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/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
9// License: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20// for compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#include "wx/evtloop.h"
24
25#ifndef WX_PRECOMP
26 #include "wx/hash.h"
27 #include "wx/app.h"
28 #include "wx/window.h"
29 #include "wx/timer.h"
30#endif
31
32#include "wx/tooltip.h"
33#include "wx/module.h"
34#include "wx/unix/private.h"
35#include "wx/x11/private.h"
36#include "X11/Xlib.h"
37
38#if wxUSE_THREADS
39 #include "wx/thread.h"
40#endif
41
42#include <sys/time.h>
43#include <unistd.h>
44
45#if wxUSE_SOCKETS
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 }
61
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 {
81 WX_CLEAR_HASH_TABLE(*this)
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 }
170 return true;
171 }
172 else
173 return false;
174}
175
176void wxSocketTable::FillSets(fd_set* readset, fd_set* writeset, int* highest)
177{
178 BeginFind();
179 wxHashTable::compatibility_iterator node = Next();
180 while (node)
181 {
182 wxSocketTableEntry* entry = (wxSocketTableEntry*) node->GetData();
183
184 if (entry->m_fdInput != -1)
185 {
186 wxFD_SET(entry->m_fdInput, readset);
187 if (entry->m_fdInput > *highest)
188 * highest = entry->m_fdInput;
189 }
190
191 if (entry->m_fdOutput != -1)
192 {
193 wxFD_SET(entry->m_fdOutput, writeset);
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();
205 wxHashTable::compatibility_iterator node = Next();
206 while (node)
207 {
208 wxSocketTableEntry* entry = (wxSocketTableEntry*) node->GetData();
209
210 if (entry->m_fdInput != -1 && wxFD_ISSET(entry->m_fdInput, readset))
211 {
212 (entry->m_callbackInput) (entry->m_fdInput, entry->m_dataInput);
213 }
214
215 if (entry->m_fdOutput != -1 && wxFD_ISSET(entry->m_fdOutput, writeset))
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() {}
231 bool OnInit() { wxTheSocketTable = new wxSocketTable; return true; };
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}
255#endif
256
257// ----------------------------------------------------------------------------
258// wxEventLoopImpl
259// ----------------------------------------------------------------------------
260
261class WXDLLEXPORT wxEventLoopImpl
262{
263public:
264 // ctor
265 wxEventLoopImpl() { SetExitCode(0); m_keepGoing = false; }
266
267 // process an XEvent, return true if it was processed
268 bool ProcessEvent(XEvent* event);
269
270 // generate an idle message, return true if more idle time requested
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:
278 // preprocess an event, return true if processed (i.e. no further
279 // dispatching required)
280 bool PreProcessEvent(XEvent* event);
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
296bool wxEventLoopImpl::ProcessEvent(XEvent *event)
297{
298 // give us the chance to preprocess the message first
299 if ( PreProcessEvent(event) )
300 return true;
301
302 // if it wasn't done, dispatch it to the corresponding window
303 if (wxTheApp)
304 return wxTheApp->ProcessXEvent((WXEvent*) event);
305
306 return false;
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) )
323 return true;
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) )
330 return true;
331 }
332#endif
333
334 return false;
335}
336
337// ----------------------------------------------------------------------------
338// wxEventLoopImpl idle event processing
339// ----------------------------------------------------------------------------
340
341bool wxEventLoopImpl::SendIdleEvent()
342{
343 return wxTheApp->ProcessIdle();
344}
345
346// ============================================================================
347// wxEventLoop implementation
348// ============================================================================
349
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
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;
365
366 wxEventLoopActivator activate(this);
367
368 m_impl->m_keepGoing = true;
369 while ( m_impl->m_keepGoing )
370 {
371#if 0 // wxUSE_THREADS
372 wxMutexGuiLeaveOrEnter();
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 {
379#if wxUSE_TIMER
380 wxTimer::NotifyTimers(); // TODO: is this the correct place for it?
381#endif
382 if (!m_impl->SendIdleEvent())
383 {
384#if 0 // wxUSE_THREADS
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
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);
416 m_impl->m_keepGoing = false;
417}
418
419// ----------------------------------------------------------------------------
420// wxEventLoop message processing dispatching
421// ----------------------------------------------------------------------------
422
423bool wxEventLoop::Pending() const
424{
425 XFlush( wxGlobalDisplay() );
426 return (XPending( wxGlobalDisplay() ) > 0);
427}
428
429bool wxEventLoop::Dispatch()
430{
431 XEvent event;
432
433 // Start off by checking if any of our child processes have finished.
434 wxCheckForFinishedChildren();
435
436 // TODO allowing for threads, as per e.g. wxMSW
437
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.
444
445 if (XPending( wxGlobalDisplay() ) == 0)
446 {
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.
455
456#else
457 struct timeval tv;
458 tv.tv_sec=0;
459 tv.tv_usec=10000; // TODO make this configurable
460 int fd = ConnectionNumber( wxGlobalDisplay() );
461
462 fd_set readset;
463 fd_set writeset;
464 int highest = fd;
465 wxFD_ZERO(&readset);
466 wxFD_ZERO(&writeset);
467
468 wxFD_SET(fd, &readset);
469
470#if wxUSE_SOCKETS
471 if (wxTheSocketTable)
472 wxTheSocketTable->FillSets( &readset, &writeset, &highest );
473#endif
474
475 if (select( highest+1, &readset, &writeset, NULL, &tv ) == 0)
476 {
477 // Timed out, so no event to process
478 return true;
479 }
480 else
481 {
482 // An X11 event was pending, so get it
483 if (wxFD_ISSET( fd, &readset ))
484 XNextEvent( wxGlobalDisplay(), &event );
485
486#if wxUSE_SOCKETS
487 // Check if any socket events were pending,
488 // and if so, call their callbacks
489 if (wxTheSocketTable)
490 wxTheSocketTable->ProcessEvents( &readset, &writeset );
491#endif
492 }
493#endif
494 }
495 else
496 {
497 XNextEvent( wxGlobalDisplay(), &event );
498 }
499
500
501 (void) m_impl->ProcessEvent( &event );
502 return true;
503}