]> git.saurik.com Git - wxWidgets.git/blame - src/dfb/evtloop.cpp
no real change: rename wxPendingEvents to wxHandlersWithPendingEvents since its curre...
[wxWidgets.git] / src / dfb / evtloop.cpp
CommitLineData
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"
c2ca375c 29#include "wx/generic/private/timer.h"
4eba3923 30#include "wx/private/fdiodispatcher.h"
b3c86150 31#include "wx/dfb/private.h"
0edbcd60 32#include "wx/nonownedwnd.h"
b3c86150 33
a5001e93 34#define TRACE_EVENTS "events"
b3c86150
VS
35
36// ===========================================================================
37// implementation
38// ===========================================================================
39
40//-----------------------------------------------------------------------------
41// wxEventLoop initialization
42//-----------------------------------------------------------------------------
43
b46b1d59 44wxIDirectFBEventBufferPtr wxGUIEventLoop::ms_buffer;
b3c86150 45
b46b1d59 46wxGUIEventLoop::wxGUIEventLoop()
b3c86150
VS
47{
48 if ( !ms_buffer )
49 InitBuffer();
50}
51
52/* static */
b46b1d59 53void wxGUIEventLoop::InitBuffer()
b3c86150 54{
52c8d32a 55 ms_buffer = wxIDirectFB::Get()->CreateEventBuffer();
b3c86150
VS
56}
57
e48a3055 58/* static */
b46b1d59 59void wxGUIEventLoop::CleanUp()
e48a3055
VS
60{
61 ms_buffer.Reset();
62}
63
b3c86150 64/* static */
b46b1d59 65wxIDirectFBEventBufferPtr wxGUIEventLoop::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
b46b1d59 77bool wxGUIEventLoop::Pending() const
b3c86150 78{
a5001e93 79 wxCHECK_MSG( ms_buffer, false, "invalid event buffer" );
b3c86150 80
52c8d32a 81 return ms_buffer->HasEvent();
b3c86150
VS
82}
83
b46b1d59 84bool wxGUIEventLoop::Dispatch()
b3c86150 85{
b3c86150
VS
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
9af42efd
VZ
89 //
90 // TODO: remove this hack, instead use CreateFileDescriptor() to properly
91 // multiplex GUI and socket input
b3c86150
VS
92 const int TIMEOUT = 100;
93
9af42efd
VZ
94 // treat time out (-1 return value) as normal successful return so that
95 // OnNextIteration() is called
96 return !!DispatchTimeout(TIMEOUT);
97}
98
99int wxGUIEventLoop::DispatchTimeout(unsigned long timeout)
100{
101 wxCHECK_MSG( ms_buffer, 0, "invalid event buffer" );
102
f7a50432
VS
103 // release the GUI mutex so that other threads have a chance to post
104 // events:
105 wxMutexGuiLeave();
106
9af42efd 107 bool rv = ms_buffer->WaitForEventWithTimeout(0, timeout);
f7a50432
VS
108
109 // and acquire it back before calling any event handlers:
110 wxMutexGuiEnter();
111
112 if ( rv )
b3c86150 113 {
52c8d32a 114 switch ( ms_buffer->GetLastResult() )
b3c86150 115 {
52c8d32a
VS
116 case DFB_OK:
117 {
118 wxDFBEvent e;
119 ms_buffer->GetEvent(e);
120 HandleDFBEvent(e);
121 break;
122 }
123
124 case DFB_TIMEOUT:
9af42efd 125 return -1;
52c8d32a
VS
126
127 default:
128 // don't terminate the loop due to errors (they were reported
129 // already by ms_buffer)
130 break;
b3c86150 131 }
b3c86150
VS
132 }
133
9af42efd 134 return 1;
b3c86150
VS
135}
136
b46b1d59 137void wxGUIEventLoop::WakeUp()
b3c86150 138{
a5001e93 139 wxCHECK_RET( ms_buffer, "invalid event buffer" );
b3c86150 140
52c8d32a 141 ms_buffer->WakeUp();
b3c86150
VS
142}
143
b46b1d59 144void wxGUIEventLoop::OnNextIteration()
b3c86150 145{
6b44a335 146#if wxUSE_TIMER
c2ca375c 147 wxGenericTimerImpl::NotifyTimers();
b3c86150 148#endif
6b44a335
VS
149
150#if wxUSE_SOCKETS
151 // handle any pending socket events:
4eba3923 152 wxFDIODispatcher::DispatchPending();
6b44a335 153#endif
b3c86150
VS
154}
155
b46b1d59 156void wxGUIEventLoop::Yield()
757b694b
VS
157{
158 // process all pending events:
159 while ( Pending() )
160 Dispatch();
161
162 // handle timers, sockets etc.
163 OnNextIteration();
164}
165
b3c86150
VS
166
167//-----------------------------------------------------------------------------
168// DirectFB -> wxWidgets events translation
169//-----------------------------------------------------------------------------
170
b46b1d59 171void wxGUIEventLoop::HandleDFBEvent(const wxDFBEvent& event)
b3c86150
VS
172{
173 switch ( event.GetClass() )
174 {
175 case DFEC_WINDOW:
176 {
177 wxDFBWindowEvent winevent(((const DFBEvent&)event).window);
0edbcd60 178 wxNonOwnedWindow::HandleDFBWindowEvent(winevent);
b3c86150
VS
179 break;
180 }
181
182 case DFEC_NONE:
183 case DFEC_INPUT:
184 case DFEC_USER:
dc4451c2 185#if wxCHECK_DFB_VERSION(0,9,23)
b3c86150 186 case DFEC_UNIVERSAL:
dc4451c2 187#endif
b3c86150
VS
188 {
189 wxLogTrace(TRACE_EVENTS,
a5001e93 190 "ignoring event of unsupported class %i",
b3c86150
VS
191 (int)event.GetClass());
192 }
193 }
194}