+//-----------------------------------------------------------------------------
+// wxDFBEventsHandler
+//-----------------------------------------------------------------------------
+
+// This handler is installed to process input on DirectFB's events socket (
+// obtained using CreateFileDescriptor()). When IDirectFBEventBuffer is used
+// in this mode, events are written to the file descriptor and we read them
+// in OnReadWaiting() below.
+class wxDFBEventsHandler : public wxFDIOHandler
+{
+public:
+ wxDFBEventsHandler()
+ : m_fd(-1), m_offset(0)
+ {}
+
+ void SetFD(int fd) { m_fd = fd; }
+
+ void Reset()
+ {
+ m_fd = -1;
+ m_offset = 0;
+ }
+
+ // implement wxFDIOHandler pure virtual methods
+ virtual void OnReadWaiting();
+ virtual void OnWriteWaiting()
+ { wxFAIL_MSG("OnWriteWaiting shouldn't be called"); }
+ virtual void OnExceptionWaiting()
+ { wxFAIL_MSG("OnExceptionWaiting shouldn't be called"); }
+
+private:
+ // DirectFB -> wxWidgets events translation
+ void HandleDFBEvent(const wxDFBEvent& event);
+
+ int m_fd;
+ size_t m_offset;
+ DFBEvent m_event;
+};
+
+void wxDFBEventsHandler::OnReadWaiting()
+{
+ for ( ;; )
+ {
+ int size = read(m_fd,
+ ((char*)&m_event) + m_offset,
+ sizeof(m_event) - m_offset);
+
+ if ( size == 0 || (size == -1 && (errno == EAGAIN || errno == EINTR)) )
+ {
+ // nothing left in the pipe (EAGAIN is expected for an FD with
+ // O_NONBLOCK)
+ break;
+ }
+
+ if ( size == -1 )
+ {
+ wxLogSysError(_("Failed to read event from DirectFB pipe"));
+ break;
+ }
+
+ size += m_offset;
+ m_offset = 0;
+
+ if ( size != sizeof(m_event) )
+ {
+ m_offset = size;
+ break;
+ }
+
+ HandleDFBEvent(m_event);
+ }
+}
+
+void wxDFBEventsHandler::HandleDFBEvent(const wxDFBEvent& event)
+{
+ switch ( event.GetClass() )
+ {
+ case DFEC_WINDOW:
+ {
+ wxDFBWindowEvent winevent(((const DFBEvent&)event).window);
+ wxNonOwnedWindow::HandleDFBWindowEvent(winevent);
+ break;
+ }
+
+ case DFEC_NONE:
+ case DFEC_INPUT:
+ case DFEC_USER:
+#if wxCHECK_DFB_VERSION(0,9,23)
+ case DFEC_UNIVERSAL:
+#endif
+ {
+ wxLogTrace(TRACE_EVENTS,
+ "ignoring event of unsupported class %i",
+ (int)event.GetClass());
+ }
+ }
+}
+