]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/x11/evtloop.cpp
include wrapwin.h to get HMONITOR and friends declarations
[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 #include "wx/module.h"
31#endif
32
33#include "wx/tooltip.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#ifdef HAVE_SYS_SELECT_H
46# include <sys/select.h>
47#endif
48
49#if wxUSE_SOCKETS
50// ----------------------------------------------------------------------------
51// wxSocketTable
52// ----------------------------------------------------------------------------
53
54typedef void (*wxSocketCallback) (int fd, void* data);
55
56class wxSocketTableEntry: public wxObject
57{
58 public:
59 wxSocketTableEntry()
60 {
61 m_fdInput = -1; m_fdOutput = -1;
62 m_callbackInput = NULL; m_callbackOutput = NULL;
63 m_dataInput = NULL; m_dataOutput = NULL;
64 }
65
66 int m_fdInput;
67 int m_fdOutput;
68 wxSocketCallback m_callbackInput;
69 wxSocketCallback m_callbackOutput;
70 void* m_dataInput;
71 void* m_dataOutput;
72};
73
74typedef enum
75{ wxSocketTableInput, wxSocketTableOutput } wxSocketTableType ;
76
77class wxSocketTable: public wxHashTable
78{
79 public:
80 wxSocketTable(): wxHashTable(wxKEY_INTEGER)
81 {
82 }
83 virtual ~wxSocketTable()
84 {
85 WX_CLEAR_HASH_TABLE(*this)
86 }
87
88 wxSocketTableEntry* FindEntry(int fd);
89
90 void RegisterCallback(int fd, wxSocketTableType socketType, wxSocketCallback callback, void* data);
91
92 void UnregisterCallback(int fd, wxSocketTableType socketType);
93
94 bool CallCallback(int fd, wxSocketTableType socketType);
95
96 void FillSets(fd_set* readset, fd_set* writeset, int* highest);
97
98 void ProcessEvents(fd_set* readset, fd_set* writeset);
99};
100
101wxSocketTableEntry* wxSocketTable::FindEntry(int fd)
102{
103 wxSocketTableEntry* entry = (wxSocketTableEntry*) Get(fd);
104 return entry;
105}
106
107void wxSocketTable::RegisterCallback(int fd, wxSocketTableType socketType, wxSocketCallback callback, void* data)
108{
109 wxSocketTableEntry* entry = FindEntry(fd);
110 if (!entry)
111 {
112 entry = new wxSocketTableEntry();
113 Put(fd, entry);
114 }
115
116 if (socketType == wxSocketTableInput)
117 {
118 entry->m_fdInput = fd;
119 entry->m_dataInput = data;
120 entry->m_callbackInput = callback;
121 }
122 else
123 {
124 entry->m_fdOutput = fd;
125 entry->m_dataOutput = data;
126 entry->m_callbackOutput = callback;
127 }
128}
129
130void wxSocketTable::UnregisterCallback(int fd, wxSocketTableType socketType)
131{
132 wxSocketTableEntry* entry = FindEntry(fd);
133 if (entry)
134 {
135 if (socketType == wxSocketTableInput)
136 {
137 entry->m_fdInput = -1;
138 entry->m_dataInput = NULL;
139 entry->m_callbackInput = NULL;
140 }
141 else
142 {
143 entry->m_fdOutput = -1;
144 entry->m_dataOutput = NULL;
145 entry->m_callbackOutput = NULL;
146 }
147 if (entry->m_fdInput == -1 && entry->m_fdOutput == -1)
148 {
149 Delete(fd);
150 delete entry;
151 }
152 }
153}
154
155bool wxSocketTable::CallCallback(int fd, wxSocketTableType socketType)
156{
157 wxSocketTableEntry* entry = FindEntry(fd);
158 if (entry)
159 {
160 if (socketType == wxSocketTableInput)
161 {
162 if (entry->m_fdInput != -1 && entry->m_callbackInput)
163 {
164 (entry->m_callbackInput) (entry->m_fdInput, entry->m_dataInput);
165 }
166 }
167 else
168 {
169 if (entry->m_fdOutput != -1 && entry->m_callbackOutput)
170 {
171 (entry->m_callbackOutput) (entry->m_fdOutput, entry->m_dataOutput);
172 }
173 }
174 return true;
175 }
176 else
177 return false;
178}
179
180void wxSocketTable::FillSets(fd_set* readset, fd_set* writeset, int* highest)
181{
182 BeginFind();
183 wxHashTable::compatibility_iterator node = Next();
184 while (node)
185 {
186 wxSocketTableEntry* entry = (wxSocketTableEntry*) node->GetData();
187
188 if (entry->m_fdInput != -1)
189 {
190 wxFD_SET(entry->m_fdInput, readset);
191 if (entry->m_fdInput > *highest)
192 * highest = entry->m_fdInput;
193 }
194
195 if (entry->m_fdOutput != -1)
196 {
197 wxFD_SET(entry->m_fdOutput, writeset);
198 if (entry->m_fdOutput > *highest)
199 * highest = entry->m_fdOutput;
200 }
201
202 node = Next();
203 }
204}
205
206void wxSocketTable::ProcessEvents(fd_set* readset, fd_set* writeset)
207{
208 BeginFind();
209 wxHashTable::compatibility_iterator node = Next();
210 while (node)
211 {
212 wxSocketTableEntry* entry = (wxSocketTableEntry*) node->GetData();
213
214 if (entry->m_fdInput != -1 && wxFD_ISSET(entry->m_fdInput, readset))
215 {
216 (entry->m_callbackInput) (entry->m_fdInput, entry->m_dataInput);
217 }
218
219 if (entry->m_fdOutput != -1 && wxFD_ISSET(entry->m_fdOutput, writeset))
220 {
221 (entry->m_callbackOutput) (entry->m_fdOutput, entry->m_dataOutput);
222 }
223
224 node = Next();
225 }
226}
227
228wxSocketTable* wxTheSocketTable = NULL;
229
230class wxSocketTableModule: public wxModule
231{
232DECLARE_DYNAMIC_CLASS(wxSocketTableModule)
233public:
234 wxSocketTableModule() {}
235 bool OnInit() { wxTheSocketTable = new wxSocketTable; return true; };
236 void OnExit() { delete wxTheSocketTable; wxTheSocketTable = NULL; };
237};
238
239IMPLEMENT_DYNAMIC_CLASS(wxSocketTableModule, wxModule)
240
241// Implement registration functions as C functions so they
242// can be called from gsock11.c
243
244extern "C" void wxRegisterSocketCallback(int fd, wxSocketTableType socketType, wxSocketCallback callback, void* data)
245{
246 if (wxTheSocketTable)
247 {
248 wxTheSocketTable->RegisterCallback(fd, socketType, callback, data);
249 }
250}
251
252extern "C" void wxUnregisterSocketCallback(int fd, wxSocketTableType socketType)
253{
254 if (wxTheSocketTable)
255 {
256 wxTheSocketTable->UnregisterCallback(fd, socketType);
257 }
258}
259#endif
260
261// ----------------------------------------------------------------------------
262// wxEventLoopImpl
263// ----------------------------------------------------------------------------
264
265class WXDLLEXPORT wxEventLoopImpl
266{
267public:
268 // ctor
269 wxEventLoopImpl() { SetExitCode(0); m_keepGoing = false; }
270
271 // process an XEvent, return true if it was processed
272 bool ProcessEvent(XEvent* event);
273
274 // generate an idle message, return true if more idle time requested
275 bool SendIdleEvent();
276
277 // set/get the exit code
278 void SetExitCode(int exitcode) { m_exitcode = exitcode; }
279 int GetExitCode() const { return m_exitcode; }
280
281public:
282 // preprocess an event, return true if processed (i.e. no further
283 // dispatching required)
284 bool PreProcessEvent(XEvent* event);
285
286 // the exit code of the event loop
287 int m_exitcode;
288
289 bool m_keepGoing;
290};
291
292// ============================================================================
293// wxEventLoopImpl implementation
294// ============================================================================
295
296// ----------------------------------------------------------------------------
297// wxEventLoopImpl message processing
298// ----------------------------------------------------------------------------
299
300bool wxEventLoopImpl::ProcessEvent(XEvent *event)
301{
302 // give us the chance to preprocess the message first
303 if ( PreProcessEvent(event) )
304 return true;
305
306 // if it wasn't done, dispatch it to the corresponding window
307 if (wxTheApp)
308 return wxTheApp->ProcessXEvent((WXEvent*) event);
309
310 return false;
311}
312
313bool wxEventLoopImpl::PreProcessEvent(XEvent *event)
314{
315 // TODO
316#if 0
317 HWND hWnd = msg->hwnd;
318 wxWindow *wndThis = wxGetWindowFromHWND((WXHWND)hWnd);
319
320
321 // try translations first; find the youngest window with a translation
322 // table.
323 wxWindow *wnd;
324 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
325 {
326 if ( wnd->MSWTranslateMessage((WXMSG *)msg) )
327 return true;
328 }
329
330 // Anyone for a non-translation message? Try youngest descendants first.
331 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
332 {
333 if ( wnd->MSWProcessMessage((WXMSG *)msg) )
334 return true;
335 }
336#endif
337
338 return false;
339}
340
341// ----------------------------------------------------------------------------
342// wxEventLoopImpl idle event processing
343// ----------------------------------------------------------------------------
344
345bool wxEventLoopImpl::SendIdleEvent()
346{
347 return wxTheApp->ProcessIdle();
348}
349
350// ============================================================================
351// wxEventLoop implementation
352// ============================================================================
353
354// ----------------------------------------------------------------------------
355// wxEventLoop running and exiting
356// ----------------------------------------------------------------------------
357
358wxEventLoop::~wxEventLoop()
359{
360 wxASSERT_MSG( !m_impl, _T("should have been deleted in Run()") );
361}
362
363int wxEventLoop::Run()
364{
365 // event loops are not recursive, you need to create another loop!
366 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
367
368 m_impl = new wxEventLoopImpl;
369
370 wxEventLoopActivator activate(this);
371
372 m_impl->m_keepGoing = true;
373 while ( m_impl->m_keepGoing )
374 {
375#if 0 // wxUSE_THREADS
376 wxMutexGuiLeaveOrEnter();
377#endif // wxUSE_THREADS
378
379 // generate and process idle events for as long as we don't have
380 // anything else to do
381 while ( ! Pending() )
382 {
383#if wxUSE_TIMER
384 wxTimer::NotifyTimers(); // TODO: is this the correct place for it?
385#endif
386 if (!m_impl->SendIdleEvent())
387 {
388#if 0 // wxUSE_THREADS
389 // leave the main loop to give other threads a chance to
390 // perform their GUI work
391 wxMutexGuiLeave();
392 wxUsleep(20);
393 wxMutexGuiEnter();
394#endif
395 // Break out of while loop
396 break;
397 }
398 }
399
400 // a message came or no more idle processing to do, sit in Dispatch()
401 // waiting for the next message
402 if ( !Dispatch() )
403 {
404 break;
405 }
406 }
407
408 int exitcode = m_impl->GetExitCode();
409 delete m_impl;
410 m_impl = NULL;
411
412 return exitcode;
413}
414
415void wxEventLoop::Exit(int rc)
416{
417 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
418
419 m_impl->SetExitCode(rc);
420 m_impl->m_keepGoing = false;
421}
422
423// ----------------------------------------------------------------------------
424// wxEventLoop message processing dispatching
425// ----------------------------------------------------------------------------
426
427bool wxEventLoop::Pending() const
428{
429 XFlush( wxGlobalDisplay() );
430 return (XPending( wxGlobalDisplay() ) > 0);
431}
432
433bool wxEventLoop::Dispatch()
434{
435 XEvent event;
436
437 // Start off by checking if any of our child processes have finished.
438 wxCheckForFinishedChildren();
439
440 // TODO allowing for threads, as per e.g. wxMSW
441
442 // This now waits until either an X event is received,
443 // or the select times out. So we should now process
444 // wxTimers in a reasonably timely fashion. However it
445 // does also mean that idle processing will happen more
446 // often, so we should probably limit idle processing to
447 // not be repeated more than every N milliseconds.
448
449 if (XPending( wxGlobalDisplay() ) == 0)
450 {
451#if wxUSE_NANOX
452 GR_TIMEOUT timeout = 10; // Milliseconds
453 // Wait for next event, or timeout
454 GrGetNextEventTimeout(& event, timeout);
455
456 // Fall through to ProcessEvent.
457 // we'll assume that ProcessEvent will just ignore
458 // the event if there was a timeout and no event.
459
460#else
461 struct timeval tv;
462 tv.tv_sec=0;
463 tv.tv_usec=10000; // TODO make this configurable
464 int fd = ConnectionNumber( wxGlobalDisplay() );
465
466 fd_set readset;
467 fd_set writeset;
468 int highest = fd;
469 wxFD_ZERO(&readset);
470 wxFD_ZERO(&writeset);
471
472 wxFD_SET(fd, &readset);
473
474#if wxUSE_SOCKETS
475 if (wxTheSocketTable)
476 wxTheSocketTable->FillSets( &readset, &writeset, &highest );
477#endif
478
479 if (select( highest+1, &readset, &writeset, NULL, &tv ) == 0)
480 {
481 // Timed out, so no event to process
482 return true;
483 }
484 else
485 {
486 // An X11 event was pending, so get it
487 if (wxFD_ISSET( fd, &readset ))
488 XNextEvent( wxGlobalDisplay(), &event );
489
490#if wxUSE_SOCKETS
491 // Check if any socket events were pending,
492 // and if so, call their callbacks
493 if (wxTheSocketTable)
494 wxTheSocketTable->ProcessEvents( &readset, &writeset );
495#endif
496 }
497#endif
498 }
499 else
500 {
501 XNextEvent( wxGlobalDisplay(), &event );
502 }
503
504
505 (void) m_impl->ProcessEvent( &event );
506 return true;
507}