]> git.saurik.com Git - wxWidgets.git/blame - src/x11/evtloop.cpp
Implement SortItems for native OS X wxListCtrl to match behavior with other impls...
[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"
02761f6c 30 #include "wx/module.h"
32d4c30a
WS
31#endif
32
1b0fb34b 33#include "wx/tooltip.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
bc023abb
MW
45#ifdef HAVE_SYS_SELECT_H
46# include <sys/select.h>
47#endif
48
2d1084ea 49#if wxUSE_SOCKETS
52127426
JS
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 }
3754265e 65
52127426
JS
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 }
d3c7fc99 83 virtual ~wxSocketTable()
52127426 84 {
ac32ba44 85 WX_CLEAR_HASH_TABLE(*this)
52127426
JS
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 }
32d4c30a 174 return true;
52127426
JS
175 }
176 else
32d4c30a 177 return false;
52127426
JS
178}
179
180void wxSocketTable::FillSets(fd_set* readset, fd_set* writeset, int* highest)
181{
182 BeginFind();
ac32ba44 183 wxHashTable::compatibility_iterator node = Next();
52127426
JS
184 while (node)
185 {
09a1dffa 186 wxSocketTableEntry* entry = (wxSocketTableEntry*) node->GetData();
3754265e 187
52127426
JS
188 if (entry->m_fdInput != -1)
189 {
17a1ebd1 190 wxFD_SET(entry->m_fdInput, readset);
52127426
JS
191 if (entry->m_fdInput > *highest)
192 * highest = entry->m_fdInput;
193 }
194
195 if (entry->m_fdOutput != -1)
196 {
17a1ebd1 197 wxFD_SET(entry->m_fdOutput, writeset);
52127426
JS
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();
ac32ba44 209 wxHashTable::compatibility_iterator node = Next();
52127426
JS
210 while (node)
211 {
09a1dffa 212 wxSocketTableEntry* entry = (wxSocketTableEntry*) node->GetData();
3754265e 213
17a1ebd1 214 if (entry->m_fdInput != -1 && wxFD_ISSET(entry->m_fdInput, readset))
52127426
JS
215 {
216 (entry->m_callbackInput) (entry->m_fdInput, entry->m_dataInput);
217 }
218
17a1ebd1 219 if (entry->m_fdOutput != -1 && wxFD_ISSET(entry->m_fdOutput, writeset))
52127426
JS
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() {}
32d4c30a 235 bool OnInit() { wxTheSocketTable = new wxSocketTable; return true; };
52127426
JS
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}
2d1084ea 259#endif
52127426 260
1b0fb34b
JS
261// ----------------------------------------------------------------------------
262// wxEventLoopImpl
263// ----------------------------------------------------------------------------
264
265class WXDLLEXPORT wxEventLoopImpl
266{
267public:
268 // ctor
32d4c30a 269 wxEventLoopImpl() { SetExitCode(0); m_keepGoing = false; }
1b0fb34b 270
32d4c30a 271 // process an XEvent, return true if it was processed
086fd560 272 bool ProcessEvent(XEvent* event);
1b0fb34b 273
32d4c30a 274 // generate an idle message, return true if more idle time requested
1b0fb34b
JS
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:
32d4c30a 282 // preprocess an event, return true if processed (i.e. no further
1b0fb34b 283 // dispatching required)
7266b672 284 bool PreProcessEvent(XEvent* event);
1b0fb34b
JS
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
086fd560 300bool wxEventLoopImpl::ProcessEvent(XEvent *event)
1b0fb34b
JS
301{
302 // give us the chance to preprocess the message first
086fd560 303 if ( PreProcessEvent(event) )
32d4c30a 304 return true;
3754265e 305
086fd560
JS
306 // if it wasn't done, dispatch it to the corresponding window
307 if (wxTheApp)
308 return wxTheApp->ProcessXEvent((WXEvent*) event);
309
32d4c30a 310 return false;
1b0fb34b
JS
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) )
32d4c30a 327 return true;
1b0fb34b
JS
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) )
32d4c30a 334 return true;
1b0fb34b
JS
335 }
336#endif
337
32d4c30a 338 return false;
1b0fb34b
JS
339}
340
341// ----------------------------------------------------------------------------
342// wxEventLoopImpl idle event processing
343// ----------------------------------------------------------------------------
344
345bool wxEventLoopImpl::SendIdleEvent()
346{
e39af974 347 return wxTheApp->ProcessIdle();
1b0fb34b
JS
348}
349
350// ============================================================================
351// wxEventLoop implementation
352// ============================================================================
353
1b0fb34b
JS
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
1b0fb34b
JS
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;
3754265e 369
77fb1a02 370 wxEventLoopActivator activate(this);
1b0fb34b 371
32d4c30a 372 m_impl->m_keepGoing = true;
1b0fb34b
JS
373 while ( m_impl->m_keepGoing )
374 {
7266b672 375#if 0 // wxUSE_THREADS
2b5f62a0 376 wxMutexGuiLeaveOrEnter();
1b0fb34b
JS
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 {
b555c37c 383#if wxUSE_TIMER
b513212d 384 wxTimer::NotifyTimers(); // TODO: is this the correct place for it?
b555c37c 385#endif
1b0fb34b
JS
386 if (!m_impl->SendIdleEvent())
387 {
2b5f62a0 388#if 0 // wxUSE_THREADS
1b0fb34b
JS
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
1b0fb34b
JS
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);
32d4c30a 420 m_impl->m_keepGoing = false;
1b0fb34b
JS
421}
422
423// ----------------------------------------------------------------------------
424// wxEventLoop message processing dispatching
425// ----------------------------------------------------------------------------
426
427bool wxEventLoop::Pending() const
428{
2b5f62a0
VZ
429 XFlush( wxGlobalDisplay() );
430 return (XPending( wxGlobalDisplay() ) > 0);
1b0fb34b
JS
431}
432
433bool wxEventLoop::Dispatch()
434{
435 XEvent event;
436
bd00fe32
VZ
437 // Start off by checking if any of our child processes have finished.
438 wxCheckForFinishedChildren();
439
1b0fb34b
JS
440 // TODO allowing for threads, as per e.g. wxMSW
441
1016f0de
JS
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.
3754265e 448
2b5f62a0 449 if (XPending( wxGlobalDisplay() ) == 0)
1016f0de 450 {
868741e9
JS
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.
3754265e 459
868741e9 460#else
1016f0de
JS
461 struct timeval tv;
462 tv.tv_sec=0;
463 tv.tv_usec=10000; // TODO make this configurable
2b5f62a0 464 int fd = ConnectionNumber( wxGlobalDisplay() );
3754265e 465
1016f0de 466 fd_set readset;
52127426
JS
467 fd_set writeset;
468 int highest = fd;
17a1ebd1
VZ
469 wxFD_ZERO(&readset);
470 wxFD_ZERO(&writeset);
3754265e 471
17a1ebd1 472 wxFD_SET(fd, &readset);
52127426 473
2d1084ea 474#if wxUSE_SOCKETS
52127426 475 if (wxTheSocketTable)
2b5f62a0 476 wxTheSocketTable->FillSets( &readset, &writeset, &highest );
2d1084ea 477#endif
3754265e 478
2b5f62a0 479 if (select( highest+1, &readset, &writeset, NULL, &tv ) == 0)
1016f0de
JS
480 {
481 // Timed out, so no event to process
32d4c30a 482 return true;
1016f0de
JS
483 }
484 else
485 {
52127426 486 // An X11 event was pending, so get it
17a1ebd1 487 if (wxFD_ISSET( fd, &readset ))
2b5f62a0 488 XNextEvent( wxGlobalDisplay(), &event );
52127426 489
2d1084ea 490#if wxUSE_SOCKETS
52127426
JS
491 // Check if any socket events were pending,
492 // and if so, call their callbacks
493 if (wxTheSocketTable)
2b5f62a0 494 wxTheSocketTable->ProcessEvents( &readset, &writeset );
2d1084ea 495#endif
1016f0de 496 }
868741e9 497#endif
3754265e 498 }
2b5f62a0 499 else
1016f0de 500 {
2b5f62a0 501 XNextEvent( wxGlobalDisplay(), &event );
1016f0de 502 }
3754265e
VZ
503
504
2b5f62a0 505 (void) m_impl->ProcessEvent( &event );
32d4c30a 506 return true;
1b0fb34b 507}