]>
Commit | Line | Data |
---|---|---|
b3c86150 VS |
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 | ||
f7a50432 | 28 | #include "wx/thread.h" |
b3c86150 | 29 | #include "wx/timer.h" |
6b44a335 | 30 | #include "wx/private/socketevtdispatch.h" |
b3c86150 | 31 | #include "wx/dfb/private.h" |
0edbcd60 | 32 | #include "wx/nonownedwnd.h" |
b3c86150 VS |
33 | |
34 | #define TRACE_EVENTS _T("events") | |
35 | ||
36 | // =========================================================================== | |
37 | // implementation | |
38 | // =========================================================================== | |
39 | ||
40 | //----------------------------------------------------------------------------- | |
41 | // wxEventLoop initialization | |
42 | //----------------------------------------------------------------------------- | |
43 | ||
52c8d32a | 44 | wxIDirectFBEventBufferPtr wxEventLoop::ms_buffer; |
b3c86150 VS |
45 | |
46 | wxEventLoop::wxEventLoop() | |
47 | { | |
48 | if ( !ms_buffer ) | |
49 | InitBuffer(); | |
50 | } | |
51 | ||
52 | /* static */ | |
53 | void wxEventLoop::InitBuffer() | |
54 | { | |
52c8d32a | 55 | ms_buffer = wxIDirectFB::Get()->CreateEventBuffer(); |
b3c86150 VS |
56 | } |
57 | ||
e48a3055 VS |
58 | /* static */ |
59 | void wxEventLoop::CleanUp() | |
60 | { | |
61 | ms_buffer.Reset(); | |
62 | } | |
63 | ||
b3c86150 | 64 | /* static */ |
52c8d32a | 65 | wxIDirectFBEventBufferPtr wxEventLoop::GetDirectFBEventBuffer() |
b3c86150 VS |
66 | { |
67 | if ( !ms_buffer ) | |
68 | InitBuffer(); | |
69 | ||
70 | return ms_buffer; | |
71 | } | |
72 | ||
73 | //----------------------------------------------------------------------------- | |
74 | // events dispatch and loop handling | |
75 | //----------------------------------------------------------------------------- | |
76 | ||
77 | bool wxEventLoop::Pending() const | |
78 | { | |
79 | wxCHECK_MSG( ms_buffer, false, _T("invalid event buffer") ); | |
80 | ||
52c8d32a | 81 | return ms_buffer->HasEvent(); |
b3c86150 VS |
82 | } |
83 | ||
84 | bool wxEventLoop::Dispatch() | |
85 | { | |
86 | wxCHECK_MSG( ms_buffer, false, _T("invalid event buffer") ); | |
87 | ||
88 | // NB: we don't block indefinitely waiting for an event, but instead | |
89 | // time out after a brief period in order to make sure that | |
90 | // OnNextIteration() will be called frequently enough | |
b3c86150 VS |
91 | const int TIMEOUT = 100; |
92 | ||
f7a50432 VS |
93 | // release the GUI mutex so that other threads have a chance to post |
94 | // events: | |
95 | wxMutexGuiLeave(); | |
96 | ||
97 | bool rv = ms_buffer->WaitForEventWithTimeout(0, TIMEOUT); | |
98 | ||
99 | // and acquire it back before calling any event handlers: | |
100 | wxMutexGuiEnter(); | |
101 | ||
102 | if ( rv ) | |
b3c86150 | 103 | { |
52c8d32a | 104 | switch ( ms_buffer->GetLastResult() ) |
b3c86150 | 105 | { |
52c8d32a VS |
106 | case DFB_OK: |
107 | { | |
108 | wxDFBEvent e; | |
109 | ms_buffer->GetEvent(e); | |
110 | HandleDFBEvent(e); | |
111 | break; | |
112 | } | |
113 | ||
114 | case DFB_TIMEOUT: | |
115 | // timed out, pretend we processed an event so that | |
116 | // OnNextIteration is called | |
117 | break; | |
118 | ||
119 | default: | |
120 | // don't terminate the loop due to errors (they were reported | |
121 | // already by ms_buffer) | |
122 | break; | |
b3c86150 | 123 | } |
b3c86150 VS |
124 | } |
125 | ||
126 | return true; | |
127 | } | |
128 | ||
129 | void wxEventLoop::WakeUp() | |
130 | { | |
131 | wxCHECK_RET( ms_buffer, _T("invalid event buffer") ); | |
132 | ||
52c8d32a | 133 | ms_buffer->WakeUp(); |
b3c86150 VS |
134 | } |
135 | ||
136 | void wxEventLoop::OnNextIteration() | |
137 | { | |
6b44a335 | 138 | #if wxUSE_TIMER |
b3c86150 VS |
139 | wxTimer::NotifyTimers(); |
140 | #endif | |
6b44a335 VS |
141 | |
142 | #if wxUSE_SOCKETS | |
143 | // handle any pending socket events: | |
144 | wxSocketEventDispatcher::Get().RunLoop(); | |
145 | #endif | |
b3c86150 VS |
146 | } |
147 | ||
757b694b VS |
148 | void wxEventLoop::Yield() |
149 | { | |
150 | // process all pending events: | |
151 | while ( Pending() ) | |
152 | Dispatch(); | |
153 | ||
154 | // handle timers, sockets etc. | |
155 | OnNextIteration(); | |
156 | } | |
157 | ||
b3c86150 VS |
158 | |
159 | //----------------------------------------------------------------------------- | |
160 | // DirectFB -> wxWidgets events translation | |
161 | //----------------------------------------------------------------------------- | |
162 | ||
163 | void wxEventLoop::HandleDFBEvent(const wxDFBEvent& event) | |
164 | { | |
165 | switch ( event.GetClass() ) | |
166 | { | |
167 | case DFEC_WINDOW: | |
168 | { | |
169 | wxDFBWindowEvent winevent(((const DFBEvent&)event).window); | |
0edbcd60 | 170 | wxNonOwnedWindow::HandleDFBWindowEvent(winevent); |
b3c86150 VS |
171 | break; |
172 | } | |
173 | ||
174 | case DFEC_NONE: | |
175 | case DFEC_INPUT: | |
176 | case DFEC_USER: | |
dc4451c2 | 177 | #if wxCHECK_DFB_VERSION(0,9,23) |
b3c86150 | 178 | case DFEC_UNIVERSAL: |
dc4451c2 | 179 | #endif |
b3c86150 VS |
180 | { |
181 | wxLogTrace(TRACE_EVENTS, | |
182 | _T("ignoring event of unsupported class %i"), | |
183 | (int)event.GetClass()); | |
184 | } | |
185 | } | |
186 | } |