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