use C++ wrappers around DirectFB API for easier use
[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/dfb/private.h"
30
31 #define TRACE_EVENTS _T("events")
32
33 // ===========================================================================
34 // implementation
35 // ===========================================================================
36
37 //-----------------------------------------------------------------------------
38 // wxEventLoop initialization
39 //-----------------------------------------------------------------------------
40
41 wxIDirectFBEventBufferPtr wxEventLoop::ms_buffer;
42
43 wxEventLoop::wxEventLoop()
44 {
45 if ( !ms_buffer )
46 InitBuffer();
47 }
48
49 /* static */
50 void wxEventLoop::InitBuffer()
51 {
52 ms_buffer = wxIDirectFB::Get()->CreateEventBuffer();
53 }
54
55 /* static */
56 wxIDirectFBEventBufferPtr wxEventLoop::GetDirectFBEventBuffer()
57 {
58 if ( !ms_buffer )
59 InitBuffer();
60
61 return ms_buffer;
62 }
63
64 //-----------------------------------------------------------------------------
65 // events dispatch and loop handling
66 //-----------------------------------------------------------------------------
67
68 bool wxEventLoop::Pending() const
69 {
70 wxCHECK_MSG( ms_buffer, false, _T("invalid event buffer") );
71
72 return ms_buffer->HasEvent();
73 }
74
75 bool wxEventLoop::Dispatch()
76 {
77 wxCHECK_MSG( ms_buffer, false, _T("invalid event buffer") );
78
79 // NB: we don't block indefinitely waiting for an event, but instead
80 // time out after a brief period in order to make sure that
81 // OnNextIteration() will be called frequently enough
82 //
83 // FIXME: call NotifyTimers() from here (and loop) instead?
84 const int TIMEOUT = 100;
85
86 if ( ms_buffer->WaitForEventWithTimeout(0, TIMEOUT) )
87 {
88 switch ( ms_buffer->GetLastResult() )
89 {
90 case DFB_OK:
91 {
92 wxDFBEvent e;
93 ms_buffer->GetEvent(e);
94 HandleDFBEvent(e);
95 break;
96 }
97
98 case DFB_TIMEOUT:
99 // timed out, pretend we processed an event so that
100 // OnNextIteration is called
101 break;
102
103 default:
104 // don't terminate the loop due to errors (they were reported
105 // already by ms_buffer)
106 break;
107 }
108 }
109
110 return true;
111 }
112
113 void wxEventLoop::WakeUp()
114 {
115 wxCHECK_RET( ms_buffer, _T("invalid event buffer") );
116
117 ms_buffer->WakeUp();
118 }
119
120 void wxEventLoop::OnNextIteration()
121 {
122 #if wxUSE_TIMER
123 // see the comment in Dispatch
124 wxTimer::NotifyTimers();
125 #endif
126 }
127
128 #warning "FIXME: cleanup wxEventLoop::ms_buffer before exiting"
129
130
131 //-----------------------------------------------------------------------------
132 // DirectFB -> wxWidgets events translation
133 //-----------------------------------------------------------------------------
134
135 void wxEventLoop::HandleDFBEvent(const wxDFBEvent& event)
136 {
137 switch ( event.GetClass() )
138 {
139 case DFEC_WINDOW:
140 {
141 wxDFBWindowEvent winevent(((const DFBEvent&)event).window);
142 wxTopLevelWindowDFB::HandleDFBWindowEvent(winevent);
143 break;
144 }
145
146 case DFEC_NONE:
147 case DFEC_INPUT:
148 case DFEC_USER:
149 #if wxCHECK_DFB_VERSION(0,9,23)
150 case DFEC_UNIVERSAL:
151 #endif
152 {
153 wxLogTrace(TRACE_EVENTS,
154 _T("ignoring event of unsupported class %i"),
155 (int)event.GetClass());
156 }
157 }
158 }