moved wxX11's wxSocketTable to private wxSocketEventDispatcher class reusable in...
[wxWidgets.git] / src / dfb / evtloop.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/dfb/evtloop.cpp
3 // Purpose: wxEventLoop implementation
4 // Author: Vaclav Slavik
5 // Created: 2006-08-16
6 // RCS-ID: $Id$
7 // Copyright: (c) 2006 REA Elektronik GmbH
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ===========================================================================
12 // declarations
13 // ===========================================================================
14
15 // ---------------------------------------------------------------------------
16 // headers
17 // ---------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #include "wx/evtloop.h"
23
24 #ifndef WX_PRECOMP
25 #include "wx/app.h"
26 #endif
27
28 #include "wx/timer.h"
29 #include "wx/private/socketevtdispatch.h"
30 #include "wx/dfb/private.h"
31
32 #define TRACE_EVENTS _T("events")
33
34 // ===========================================================================
35 // implementation
36 // ===========================================================================
37
38 //-----------------------------------------------------------------------------
39 // wxEventLoop initialization
40 //-----------------------------------------------------------------------------
41
42 wxIDirectFBEventBufferPtr wxEventLoop::ms_buffer;
43
44 wxEventLoop::wxEventLoop()
45 {
46 if ( !ms_buffer )
47 InitBuffer();
48 }
49
50 /* static */
51 void wxEventLoop::InitBuffer()
52 {
53 ms_buffer = wxIDirectFB::Get()->CreateEventBuffer();
54 }
55
56 /* static */
57 void wxEventLoop::CleanUp()
58 {
59 ms_buffer.Reset();
60 }
61
62 /* static */
63 wxIDirectFBEventBufferPtr wxEventLoop::GetDirectFBEventBuffer()
64 {
65 if ( !ms_buffer )
66 InitBuffer();
67
68 return ms_buffer;
69 }
70
71 //-----------------------------------------------------------------------------
72 // events dispatch and loop handling
73 //-----------------------------------------------------------------------------
74
75 bool wxEventLoop::Pending() const
76 {
77 wxCHECK_MSG( ms_buffer, false, _T("invalid event buffer") );
78
79 return ms_buffer->HasEvent();
80 }
81
82 bool wxEventLoop::Dispatch()
83 {
84 wxCHECK_MSG( ms_buffer, false, _T("invalid event buffer") );
85
86 // NB: we don't block indefinitely waiting for an event, but instead
87 // time out after a brief period in order to make sure that
88 // OnNextIteration() will be called frequently enough
89 //
90 // FIXME: call NotifyTimers() and wxSocketEventDispatcher::RunLoop() from here
91 // (and loop) instead?
92 const int TIMEOUT = 100;
93
94 if ( ms_buffer->WaitForEventWithTimeout(0, TIMEOUT) )
95 {
96 switch ( ms_buffer->GetLastResult() )
97 {
98 case DFB_OK:
99 {
100 wxDFBEvent e;
101 ms_buffer->GetEvent(e);
102 HandleDFBEvent(e);
103 break;
104 }
105
106 case DFB_TIMEOUT:
107 // timed out, pretend we processed an event so that
108 // OnNextIteration is called
109 break;
110
111 default:
112 // don't terminate the loop due to errors (they were reported
113 // already by ms_buffer)
114 break;
115 }
116 }
117
118 return true;
119 }
120
121 void wxEventLoop::WakeUp()
122 {
123 wxCHECK_RET( ms_buffer, _T("invalid event buffer") );
124
125 ms_buffer->WakeUp();
126 }
127
128 void wxEventLoop::OnNextIteration()
129 {
130 // see the comment in Dispatch
131
132 #if wxUSE_TIMER
133 wxTimer::NotifyTimers();
134 #endif
135
136 #if wxUSE_SOCKETS
137 // handle any pending socket events:
138 wxSocketEventDispatcher::Get().RunLoop();
139 #endif
140 }
141
142
143 //-----------------------------------------------------------------------------
144 // DirectFB -> wxWidgets events translation
145 //-----------------------------------------------------------------------------
146
147 void wxEventLoop::HandleDFBEvent(const wxDFBEvent& event)
148 {
149 switch ( event.GetClass() )
150 {
151 case DFEC_WINDOW:
152 {
153 wxDFBWindowEvent winevent(((const DFBEvent&)event).window);
154 wxTopLevelWindowDFB::HandleDFBWindowEvent(winevent);
155 break;
156 }
157
158 case DFEC_NONE:
159 case DFEC_INPUT:
160 case DFEC_USER:
161 #if wxCHECK_DFB_VERSION(0,9,23)
162 case DFEC_UNIVERSAL:
163 #endif
164 {
165 wxLogTrace(TRACE_EVENTS,
166 _T("ignoring event of unsupported class %i"),
167 (int)event.GetClass());
168 }
169 }
170 }