1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/dfb/evtloop.cpp
3 // Purpose: wxEventLoop implementation
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2006 REA Elektronik GmbH
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ===========================================================================
13 // ===========================================================================
15 // ---------------------------------------------------------------------------
17 // ---------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
22 #include "wx/evtloop.h"
28 #include "wx/thread.h"
29 #include "wx/private/fdiodispatcher.h"
30 #include "wx/dfb/private.h"
31 #include "wx/nonownedwnd.h"
32 #include "wx/buffer.h"
34 #define TRACE_EVENTS "events"
36 // ===========================================================================
38 // ===========================================================================
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 // This handler is installed to process input on DirectFB's events socket (
45 // obtained using CreateFileDescriptor()). When IDirectFBEventBuffer is used
46 // in this mode, events are written to the file descriptor and we read them
47 // in OnReadWaiting() below.
48 class wxDFBEventsHandler
: public wxFDIOHandler
52 : m_fd(-1), m_offset(0)
55 void SetFD(int fd
) { m_fd
= fd
; }
63 // implement wxFDIOHandler pure virtual methods
64 virtual void OnReadWaiting();
65 virtual void OnWriteWaiting()
66 { wxFAIL_MSG("OnWriteWaiting shouldn't be called"); }
67 virtual void OnExceptionWaiting()
68 { wxFAIL_MSG("OnExceptionWaiting shouldn't be called"); }
71 // DirectFB -> wxWidgets events translation
72 void HandleDFBEvent(const wxDFBEvent
& event
);
79 void wxDFBEventsHandler::OnReadWaiting()
84 ((char*)&m_event
) + m_offset
,
85 sizeof(m_event
) - m_offset
);
87 if ( size
== 0 || (size
== -1 && (errno
== EAGAIN
|| errno
== EINTR
)) )
89 // nothing left in the pipe (EAGAIN is expected for an FD with
96 wxLogSysError(_("Failed to read event from DirectFB pipe"));
103 if ( size
!= sizeof(m_event
) )
109 HandleDFBEvent(m_event
);
113 void wxDFBEventsHandler::HandleDFBEvent(const wxDFBEvent
& event
)
115 switch ( event
.GetClass() )
119 wxDFBWindowEvent
winevent(((const DFBEvent
&)event
).window
);
120 wxNonOwnedWindow::HandleDFBWindowEvent(winevent
);
127 #if wxCHECK_DFB_VERSION(0,9,23)
131 wxLogTrace(TRACE_EVENTS
,
132 "ignoring event of unsupported class %i",
133 (int)event
.GetClass());
138 //-----------------------------------------------------------------------------
139 // wxEventLoop initialization
140 //-----------------------------------------------------------------------------
142 wxIDirectFBEventBufferPtr
wxGUIEventLoop::ms_buffer
;
143 int wxGUIEventLoop::ms_bufferFd
;
144 static wxDFBEventsHandler gs_DFBEventsHandler
;
146 wxGUIEventLoop::wxGUIEventLoop()
148 // Note that this has to be done here so that the buffer is ready when
149 // an event loop runs; GetDirectFBEventBuffer(), which also calls
150 // InitBuffer(), may be called before or after the first wxGUIEventLoop
151 // instance is created.
157 void wxGUIEventLoop::InitBuffer()
159 // create DirectFB events buffer:
160 ms_buffer
= wxIDirectFB::Get()->CreateEventBuffer();
162 // and setup a file descriptor that we can watch for new events:
164 ms_buffer
->CreateFileDescriptor(&ms_bufferFd
);
165 int flags
= fcntl(ms_bufferFd
, F_GETFL
, 0);
166 if ( flags
== -1 || fcntl(ms_bufferFd
, F_SETFL
, flags
| O_NONBLOCK
) == -1 )
168 wxLogSysError(_("Failed to switch DirectFB pipe to non-blocking mode"));
172 wxFDIODispatcher
*dispatcher
= wxFDIODispatcher::Get();
173 wxCHECK_RET( dispatcher
, "wxDFB requires wxFDIODispatcher" );
175 gs_DFBEventsHandler
.SetFD(ms_bufferFd
);
176 dispatcher
->RegisterFD(ms_bufferFd
, &gs_DFBEventsHandler
, wxFDIO_INPUT
);
180 void wxGUIEventLoop::CleanUp()
182 wxFDIODispatcher
*dispatcher
= wxFDIODispatcher::Get();
183 wxCHECK_RET( dispatcher
, "wxDFB requires wxFDIODispatcher" );
184 dispatcher
->UnregisterFD(ms_bufferFd
);
187 gs_DFBEventsHandler
.Reset();
191 wxIDirectFBEventBufferPtr
wxGUIEventLoop::GetDirectFBEventBuffer()
199 //-----------------------------------------------------------------------------
200 // events dispatch and loop handling
201 //-----------------------------------------------------------------------------
203 void wxGUIEventLoop::Yield()
205 // process all pending events:
209 // handle timers, sockets etc.