1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/dfb/wrapdfb.h
3 // Purpose: wx wrappers for DirectFB interfaces
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2006 REA Elektronik GmbH
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_DFB_WRAPDFB_H_
12 #define _WX_DFB_WRAPDFB_H_
14 #include "wx/dfb/dfbptr.h"
18 wxDFB_DECLARE_INTERFACE(IDirectFB
);
19 wxDFB_DECLARE_INTERFACE(IDirectFBDisplayLayer
);
20 wxDFB_DECLARE_INTERFACE(IDirectFBWindow
);
21 wxDFB_DECLARE_INTERFACE(IDirectFBSurface
);
22 wxDFB_DECLARE_INTERFACE(IDirectFBPalette
);
23 wxDFB_DECLARE_INTERFACE(IDirectFBEventBuffer
);
27 Checks the @a code of a DirectFB call and returns true if it was
28 successful and false if it failed, logging the errors as appropriate
29 (asserts for programming errors, wxLogError for runtime failures).
31 bool wxDfbCheckReturn(DFBResult code
);
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
38 The struct defined by this macro is a thin wrapper around DFB*Event type.
39 It is needed because DFB*Event are typedefs and so we can't forward declare
40 them, but we need to pass them to methods declared in public headers where
41 <directfb.h> cannot be included. So this struct just holds the event value,
42 it's sole purpose is that it can be forward declared.
44 #define WXDFB_DEFINE_EVENT_WRAPPER(T) \
48 wx##T(const T& event) : m_event(event) {} \
50 operator T&() { return m_event; } \
51 operator const T&() const { return m_event; } \
52 T* operator&() { return &m_event; } \
54 DFBEventClass GetClass() const { return m_event.clazz; } \
60 WXDFB_DEFINE_EVENT_WRAPPER(DFBEvent
)
61 WXDFB_DEFINE_EVENT_WRAPPER(DFBWindowEvent
)
64 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
68 /// Base class for wxDfbWrapper<T>
69 class wxDfbWrapperBase
72 /// Increases reference count of the object
78 /// Decreases reference count and if it reaches zero, deletes the object
81 if ( --m_refCnt
== 0 )
85 /// Returns result code of the last call
86 DFBResult
GetLastResult() const { return m_lastResult
; }
89 wxDfbWrapperBase() : m_refCnt(1), m_lastResult(DFB_OK
) {}
91 /// Dtor may only be called from Release()
92 virtual ~wxDfbWrapperBase() {}
95 Checks the @a result of a DirectFB call and returns true if it was
96 successful and false if it failed. Also stores result of the call
97 so that it can be obtained by calling GetLastResult().
99 bool Check(DFBResult result
)
101 m_lastResult
= result
;
102 return wxDfbCheckReturn(result
);
109 /// Result of the last DirectFB call
110 DFBResult m_lastResult
;
114 This template is base class for friendly C++ wrapper around DirectFB
117 The wrapper provides same API as DirectFB, with a few exceptions:
118 - methods return true/false instead of error code
119 - methods that return or create another interface return pointer to the
120 interface (or NULL on failure) instead of storing it in the last
122 - interface arguments use wxFooPtr type instead of raw DirectFB pointer
123 - methods taking flags use int type instead of an enum when the flags
124 can be or-combination of enum elements (this is workaround for
125 C++-unfriendly DirectFB API)
128 class wxDfbWrapper
: public wxDfbWrapperBase
131 /// "Raw" DirectFB interface type
132 typedef T DirectFBIface
;
134 /// Returns raw DirectFB pointer
135 T
*GetRaw() const { return m_ptr
; }
138 /// To be called from ctor. Takes ownership of raw object.
139 void Init(T
*ptr
) { m_ptr
= ptr
; }
141 /// Dtor may only be used from Release
145 m_ptr
->Release(m_ptr
);
149 // pointer to DirectFB object
154 //-----------------------------------------------------------------------------
156 //-----------------------------------------------------------------------------
158 struct wxIDirectFBFont
: public wxDfbWrapper
<IDirectFBFont
>
160 wxIDirectFBFont(IDirectFBFont
*s
) { Init(s
); }
162 bool GetStringWidth(const char *text
, int bytes
, int *w
)
163 { return Check(m_ptr
->GetStringWidth(m_ptr
, text
, bytes
, w
)); }
165 bool GetStringExtents(const char *text
, int bytes
,
166 DFBRectangle
*logicalRect
, DFBRectangle
*inkRect
)
168 return Check(m_ptr
->GetStringExtents(m_ptr
, text
, bytes
,
169 logicalRect
, inkRect
));
172 bool GetHeight(int *h
)
173 { return Check(m_ptr
->GetHeight(m_ptr
, h
)); }
175 bool GetDescender(int *descender
)
176 { return Check(m_ptr
->GetDescender(m_ptr
, descender
)); }
180 //-----------------------------------------------------------------------------
181 // wxIDirectFBPalette
182 //-----------------------------------------------------------------------------
184 struct wxIDirectFBPalette
: public wxDfbWrapper
<IDirectFBPalette
>
186 wxIDirectFBPalette(IDirectFBPalette
*s
) { Init(s
); }
190 //-----------------------------------------------------------------------------
191 // wxIDirectFBSurface
192 //-----------------------------------------------------------------------------
194 struct wxIDirectFBSurface
: public wxDfbWrapper
<IDirectFBSurface
>
196 wxIDirectFBSurface(IDirectFBSurface
*s
) { Init(s
); }
198 bool GetSize(int *w
, int *h
)
199 { return Check(m_ptr
->GetSize(m_ptr
, w
, h
)); }
201 bool GetCapabilities(DFBSurfaceCapabilities
*caps
)
202 { return Check(m_ptr
->GetCapabilities(m_ptr
, caps
)); }
204 bool GetPixelFormat(DFBSurfacePixelFormat
*caps
)
205 { return Check(m_ptr
->GetPixelFormat(m_ptr
, caps
)); }
207 bool SetClip(const DFBRegion
*clip
)
208 { return Check(m_ptr
->SetClip(m_ptr
, clip
)); }
210 bool SetColor(__u8 r
, __u8 g
, __u8 b
, __u8 a
)
211 { return Check(m_ptr
->SetColor(m_ptr
, r
, g
, b
, a
)); }
213 bool Clear(__u8 r
, __u8 g
, __u8 b
, __u8 a
)
214 { return Check(m_ptr
->Clear(m_ptr
, r
, g
, b
, a
)); }
216 bool DrawLine(int x1
, int y1
, int x2
, int y2
)
217 { return Check(m_ptr
->DrawLine(m_ptr
, x1
, y1
, x2
, y2
)); }
219 bool DrawRectangle(int x
, int y
, int w
, int h
)
220 { return Check(m_ptr
->DrawRectangle(m_ptr
, x
, y
, w
, h
)); }
222 bool FillRectangle(int x
, int y
, int w
, int h
)
223 { return Check(m_ptr
->FillRectangle(m_ptr
, x
, y
, w
, h
)); }
225 bool SetFont(const wxIDirectFBFontPtr
& font
)
226 { return Check(m_ptr
->SetFont(m_ptr
, font
->GetRaw())); }
228 bool DrawString(const char *text
, int bytes
, int x
, int y
, int flags
)
230 return Check(m_ptr
->DrawString(m_ptr
, text
, bytes
, x
, y
,
231 (DFBSurfaceTextFlags
)flags
));
235 Updates the front buffer from the back buffer. If @a region is not
236 NULL, only given rectangle is updated.
238 bool FlipToFront(const DFBRegion
*region
= NULL
);
240 wxIDirectFBSurfacePtr
GetSubSurface(const DFBRectangle
*rect
)
243 if ( Check(m_ptr
->GetSubSurface(m_ptr
, rect
, &s
)) )
244 return new wxIDirectFBSurface(s
);
249 wxIDirectFBPalettePtr
GetPalette()
252 if ( Check(m_ptr
->GetPalette(m_ptr
, &s
)) )
253 return new wxIDirectFBPalette(s
);
258 bool SetPalette(const wxIDirectFBPalettePtr
& pal
)
259 { return Check(m_ptr
->SetPalette(m_ptr
, pal
->GetRaw())); }
261 bool SetBlittingFlags(int flags
)
264 m_ptr
->SetBlittingFlags(m_ptr
, (DFBSurfaceBlittingFlags
)flags
));
267 bool Blit(const wxIDirectFBSurfacePtr
& source
,
268 const DFBRectangle
*source_rect
,
270 { return Blit(source
->GetRaw(), source_rect
, x
, y
); }
272 bool Blit(IDirectFBSurface
*source
,
273 const DFBRectangle
*source_rect
,
275 { return Check(m_ptr
->Blit(m_ptr
, source
, source_rect
, x
, y
)); }
278 /// Returns bit depth used by the surface or -1 on error
282 Creates a new surface by cloning this one. New surface will have same
283 capabilities, pixel format and pixel data as the existing one.
285 @see CreateCompatible
287 wxIDirectFBSurfacePtr
Clone();
290 Creates a surface compatible with this one, i.e. surface with the same
291 capabilities and pixel format, but with different and size.
293 @param size Size of the surface to create. If wxDefaultSize, use the
294 size of this surface.
296 wxIDirectFBSurfacePtr
CreateCompatible(const wxSize
& size
= wxDefaultSize
);
299 // this is private because we want user code to use FlipToFront()
300 bool Flip(const DFBRegion
*region
, int flags
);
304 //-----------------------------------------------------------------------------
305 // wxIDirectFBEventBuffer
306 //-----------------------------------------------------------------------------
308 struct wxIDirectFBEventBuffer
: public wxDfbWrapper
<IDirectFBEventBuffer
>
310 wxIDirectFBEventBuffer(IDirectFBEventBuffer
*s
) { Init(s
); }
314 return Check(m_ptr
->WakeUp(m_ptr
));
319 // returns DFB_OK if there is >=1 event, DFB_BUFFEREMPTY otherwise
320 DFBResult r
= m_ptr
->HasEvent(m_ptr
);
322 // NB: Check() also returns true for DFB_BUFFEREMPTY, so we can't just
323 // return it's return value:
325 return (r
== DFB_OK
);
328 bool WaitForEventWithTimeout(unsigned secs
, unsigned millisecs
)
330 DFBResult r
= m_ptr
->WaitForEventWithTimeout(m_ptr
, secs
, millisecs
);
332 // DFB_TIMEOUT is not an error in this function:
333 if ( r
== DFB_TIMEOUT
)
335 m_lastResult
= DFB_TIMEOUT
;
342 bool GetEvent(wxDFBEvent
& event
)
344 return Check(m_ptr
->GetEvent(m_ptr
, &event
));
349 //-----------------------------------------------------------------------------
351 //-----------------------------------------------------------------------------
353 struct wxIDirectFBWindow
: public wxDfbWrapper
<IDirectFBWindow
>
355 wxIDirectFBWindow(IDirectFBWindow
*s
) { Init(s
); }
357 bool GetID(DFBWindowID
*id
)
358 { return Check(m_ptr
->GetID(m_ptr
, id
)); }
360 bool GetPosition(int *x
, int *y
)
361 { return Check(m_ptr
->GetPosition(m_ptr
, x
, y
)); }
363 bool GetSize(int *w
, int *h
)
364 { return Check(m_ptr
->GetSize(m_ptr
, w
, h
)); }
366 bool MoveTo(int x
, int y
)
367 { return Check(m_ptr
->MoveTo(m_ptr
, x
, y
)); }
369 bool Resize(int w
, int h
)
370 { return Check(m_ptr
->Resize(m_ptr
, w
, h
)); }
372 bool SetOpacity(__u8 opacity
)
373 { return Check(m_ptr
->SetOpacity(m_ptr
, opacity
)); }
375 bool SetStackingClass(DFBWindowStackingClass klass
)
376 { return Check(m_ptr
->SetStackingClass(m_ptr
, klass
)); }
378 wxIDirectFBSurfacePtr
GetSurface()
381 if ( Check(m_ptr
->GetSurface(m_ptr
, &s
)) )
382 return new wxIDirectFBSurface(s
);
387 bool AttachEventBuffer(const wxIDirectFBEventBufferPtr
& buffer
)
388 { return Check(m_ptr
->AttachEventBuffer(m_ptr
, buffer
->GetRaw())); }
391 { return Check(m_ptr
->RequestFocus(m_ptr
)); }
395 //-----------------------------------------------------------------------------
396 // wxIDirectFBDisplayLayer
397 //-----------------------------------------------------------------------------
399 struct wxIDirectFBDisplayLayer
: public wxDfbWrapper
<IDirectFBDisplayLayer
>
401 wxIDirectFBDisplayLayer(IDirectFBDisplayLayer
*s
) { Init(s
); }
403 wxIDirectFBWindowPtr
CreateWindow(const DFBWindowDescription
*desc
)
406 if ( Check(m_ptr
->CreateWindow(m_ptr
, desc
, &w
)) )
407 return new wxIDirectFBWindow(w
);
412 wxIDirectFBSurfacePtr
GetSurface()
415 if ( Check(m_ptr
->GetSurface(m_ptr
, &s
)) )
416 return new wxIDirectFBSurface(s
);
421 bool GetCursorPosition(int *x
, int *y
)
422 { return Check(m_ptr
->GetCursorPosition(m_ptr
, x
, y
)); }
424 bool WarpCursor(int x
, int y
)
425 { return Check(m_ptr
->WarpCursor(m_ptr
, x
, y
)); }
429 //-----------------------------------------------------------------------------
431 //-----------------------------------------------------------------------------
433 struct wxIDirectFB
: public wxDfbWrapper
<IDirectFB
>
436 Returns pointer to DirectFB singleton object, it never returns NULL
437 after wxApp was initialized. The object is cached, so calling this
440 static wxIDirectFBPtr
Get()
442 if ( !ms_ptr
) CreateDirectFB();
446 bool SetVideoMode(int w
, int h
, int bpp
)
447 { return Check(m_ptr
->SetVideoMode(m_ptr
, w
, h
, bpp
)); }
449 wxIDirectFBSurfacePtr
CreateSurface(const DFBSurfaceDescription
*desc
)
452 if ( Check(m_ptr
->CreateSurface(m_ptr
, desc
, &s
)) )
453 return new wxIDirectFBSurface(s
);
458 wxIDirectFBEventBufferPtr
CreateEventBuffer()
460 IDirectFBEventBuffer
*b
;
461 if ( Check(m_ptr
->CreateEventBuffer(m_ptr
, &b
)) )
462 return new wxIDirectFBEventBuffer(b
);
467 wxIDirectFBFontPtr
CreateFont(const char *filename
,
468 const DFBFontDescription
*desc
)
471 if ( Check(m_ptr
->CreateFont(m_ptr
, filename
, desc
, &f
)) )
472 return new wxIDirectFBFont(f
);
477 wxIDirectFBDisplayLayerPtr
478 GetDisplayLayer(DFBDisplayLayerID id
= DLID_PRIMARY
)
480 IDirectFBDisplayLayer
*l
;
481 if ( Check(m_ptr
->GetDisplayLayer(m_ptr
, id
, &l
)) )
482 return new wxIDirectFBDisplayLayer(l
);
487 /// Returns primary surface
488 wxIDirectFBSurfacePtr
GetPrimarySurface();
491 wxIDirectFB(IDirectFB
*ptr
) { Init(ptr
); }
493 // creates ms_ptr instance
494 static void CreateDirectFB();
496 static void CleanUp();
497 friend class wxApp
; // calls CleanUp
499 // pointer to the singleton IDirectFB object
500 static wxIDirectFBPtr ms_ptr
;
503 #endif // _WX_DFB_WRAPDFB_H_