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"
15 #include "wx/gdicmn.h"
16 #include "wx/vidmode.h"
19 #include <directfb_version.h>
21 // DFB < 1.0 didn't have u8 type, only __u8
22 #if DIRECTFB_MAJOR_VERSION == 0
27 wxDFB_DECLARE_INTERFACE(IDirectFB
);
28 wxDFB_DECLARE_INTERFACE(IDirectFBDisplayLayer
);
29 wxDFB_DECLARE_INTERFACE(IDirectFBFont
);
30 wxDFB_DECLARE_INTERFACE(IDirectFBWindow
);
31 wxDFB_DECLARE_INTERFACE(IDirectFBSurface
);
32 wxDFB_DECLARE_INTERFACE(IDirectFBPalette
);
33 wxDFB_DECLARE_INTERFACE(IDirectFBEventBuffer
);
37 Checks the @a code of a DirectFB call and returns true if it was
38 successful and false if it failed, logging the errors as appropriate
39 (asserts for programming errors, wxLogError for runtime failures).
41 bool wxDfbCheckReturn(DFBResult code
);
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
48 The struct defined by this macro is a thin wrapper around DFB*Event type.
49 It is needed because DFB*Event are typedefs and so we can't forward declare
50 them, but we need to pass them to methods declared in public headers where
51 <directfb.h> cannot be included. So this struct just holds the event value,
52 it's sole purpose is that it can be forward declared.
54 #define WXDFB_DEFINE_EVENT_WRAPPER(T) \
58 wx##T(const T& event) : m_event(event) {} \
60 operator T&() { return m_event; } \
61 operator const T&() const { return m_event; } \
62 T* operator&() { return &m_event; } \
64 DFBEventClass GetClass() const { return m_event.clazz; } \
70 WXDFB_DEFINE_EVENT_WRAPPER(DFBEvent
)
71 WXDFB_DEFINE_EVENT_WRAPPER(DFBWindowEvent
)
74 //-----------------------------------------------------------------------------
76 //-----------------------------------------------------------------------------
78 /// Base class for wxDfbWrapper<T>
79 class wxDfbWrapperBase
82 /// Increases reference count of the object
88 /// Decreases reference count and if it reaches zero, deletes the object
91 if ( --m_refCnt
== 0 )
95 /// Returns result code of the last call
96 DFBResult
GetLastResult() const { return m_lastResult
; }
99 wxDfbWrapperBase() : m_refCnt(1), m_lastResult(DFB_OK
) {}
101 /// Dtor may only be called from Release()
102 virtual ~wxDfbWrapperBase() {}
105 Checks the @a result of a DirectFB call and returns true if it was
106 successful and false if it failed. Also stores result of the call
107 so that it can be obtained by calling GetLastResult().
109 bool Check(DFBResult result
)
111 m_lastResult
= result
;
112 return wxDfbCheckReturn(result
);
119 /// Result of the last DirectFB call
120 DFBResult m_lastResult
;
124 This template is base class for friendly C++ wrapper around DirectFB
127 The wrapper provides same API as DirectFB, with a few exceptions:
128 - methods return true/false instead of error code
129 - methods that return or create another interface return pointer to the
130 interface (or NULL on failure) instead of storing it in the last
132 - interface arguments use wxFooPtr type instead of raw DirectFB pointer
133 - methods taking flags use int type instead of an enum when the flags
134 can be or-combination of enum elements (this is workaround for
135 C++-unfriendly DirectFB API)
138 class wxDfbWrapper
: public wxDfbWrapperBase
141 /// "Raw" DirectFB interface type
142 typedef T DirectFBIface
;
144 /// Returns raw DirectFB pointer
145 T
*GetRaw() const { return m_ptr
; }
148 /// To be called from ctor. Takes ownership of raw object.
149 void Init(T
*ptr
) { m_ptr
= ptr
; }
151 /// Dtor may only be used from Release
155 m_ptr
->Release(m_ptr
);
159 // pointer to DirectFB object
164 //-----------------------------------------------------------------------------
166 //-----------------------------------------------------------------------------
168 struct wxIDirectFBFont
: public wxDfbWrapper
<IDirectFBFont
>
170 wxIDirectFBFont(IDirectFBFont
*s
) { Init(s
); }
172 bool GetStringWidth(const char *text
, int bytes
, int *w
)
173 { return Check(m_ptr
->GetStringWidth(m_ptr
, text
, bytes
, w
)); }
175 bool GetStringExtents(const char *text
, int bytes
,
176 DFBRectangle
*logicalRect
, DFBRectangle
*inkRect
)
178 return Check(m_ptr
->GetStringExtents(m_ptr
, text
, bytes
,
179 logicalRect
, inkRect
));
182 bool GetHeight(int *h
)
183 { return Check(m_ptr
->GetHeight(m_ptr
, h
)); }
185 bool GetDescender(int *descender
)
186 { return Check(m_ptr
->GetDescender(m_ptr
, descender
)); }
190 //-----------------------------------------------------------------------------
191 // wxIDirectFBPalette
192 //-----------------------------------------------------------------------------
194 struct wxIDirectFBPalette
: public wxDfbWrapper
<IDirectFBPalette
>
196 wxIDirectFBPalette(IDirectFBPalette
*s
) { Init(s
); }
200 //-----------------------------------------------------------------------------
201 // wxIDirectFBSurface
202 //-----------------------------------------------------------------------------
204 struct wxIDirectFBSurface
: public wxDfbWrapper
<IDirectFBSurface
>
206 wxIDirectFBSurface(IDirectFBSurface
*s
) { Init(s
); }
208 bool GetSize(int *w
, int *h
)
209 { return Check(m_ptr
->GetSize(m_ptr
, w
, h
)); }
211 bool GetCapabilities(DFBSurfaceCapabilities
*caps
)
212 { return Check(m_ptr
->GetCapabilities(m_ptr
, caps
)); }
214 bool GetPixelFormat(DFBSurfacePixelFormat
*caps
)
215 { return Check(m_ptr
->GetPixelFormat(m_ptr
, caps
)); }
217 // convenience version of GetPixelFormat, returns DSPF_UNKNOWN if fails
218 DFBSurfacePixelFormat
GetPixelFormat();
220 bool SetClip(const DFBRegion
*clip
)
221 { return Check(m_ptr
->SetClip(m_ptr
, clip
)); }
223 bool SetColor(u8 r
, u8 g
, u8 b
, u8 a
)
224 { return Check(m_ptr
->SetColor(m_ptr
, r
, g
, b
, a
)); }
226 bool Clear(u8 r
, u8 g
, u8 b
, u8 a
)
227 { return Check(m_ptr
->Clear(m_ptr
, r
, g
, b
, a
)); }
229 bool DrawLine(int x1
, int y1
, int x2
, int y2
)
230 { return Check(m_ptr
->DrawLine(m_ptr
, x1
, y1
, x2
, y2
)); }
232 bool DrawRectangle(int x
, int y
, int w
, int h
)
233 { return Check(m_ptr
->DrawRectangle(m_ptr
, x
, y
, w
, h
)); }
235 bool FillRectangle(int x
, int y
, int w
, int h
)
236 { return Check(m_ptr
->FillRectangle(m_ptr
, x
, y
, w
, h
)); }
238 bool SetFont(const wxIDirectFBFontPtr
& font
)
239 { return Check(m_ptr
->SetFont(m_ptr
, font
->GetRaw())); }
241 bool DrawString(const char *text
, int bytes
, int x
, int y
, int flags
)
243 return Check(m_ptr
->DrawString(m_ptr
, text
, bytes
, x
, y
,
244 (DFBSurfaceTextFlags
)flags
));
248 Updates the front buffer from the back buffer. If @a region is not
249 NULL, only given rectangle is updated.
251 bool FlipToFront(const DFBRegion
*region
= NULL
);
253 wxIDirectFBSurfacePtr
GetSubSurface(const DFBRectangle
*rect
)
256 if ( Check(m_ptr
->GetSubSurface(m_ptr
, rect
, &s
)) )
257 return new wxIDirectFBSurface(s
);
262 wxIDirectFBPalettePtr
GetPalette()
265 if ( Check(m_ptr
->GetPalette(m_ptr
, &s
)) )
266 return new wxIDirectFBPalette(s
);
271 bool SetPalette(const wxIDirectFBPalettePtr
& pal
)
272 { return Check(m_ptr
->SetPalette(m_ptr
, pal
->GetRaw())); }
274 bool SetBlittingFlags(int flags
)
277 m_ptr
->SetBlittingFlags(m_ptr
, (DFBSurfaceBlittingFlags
)flags
));
280 bool Blit(const wxIDirectFBSurfacePtr
& source
,
281 const DFBRectangle
*source_rect
,
283 { return Blit(source
->GetRaw(), source_rect
, x
, y
); }
285 bool Blit(IDirectFBSurface
*source
,
286 const DFBRectangle
*source_rect
,
288 { return Check(m_ptr
->Blit(m_ptr
, source
, source_rect
, x
, y
)); }
290 bool StretchBlit(const wxIDirectFBSurfacePtr
& source
,
291 const DFBRectangle
*source_rect
,
292 const DFBRectangle
*dest_rect
)
294 return Check(m_ptr
->StretchBlit(m_ptr
, source
->GetRaw(),
295 source_rect
, dest_rect
));
298 /// Returns bit depth used by the surface or -1 on error
302 Creates a new surface by cloning this one. New surface will have same
303 capabilities, pixel format and pixel data as the existing one.
305 @see CreateCompatible
307 wxIDirectFBSurfacePtr
Clone();
309 /// Flags for CreateCompatible()
310 enum CreateCompatibleFlags
312 /// Don't create double-buffered surface
313 CreateCompatible_NoBackBuffer
= 1
317 Creates a surface compatible with this one, i.e. surface with the same
318 capabilities and pixel format, but with different and size.
320 @param size Size of the surface to create. If wxDefaultSize, use the
321 size of this surface.
322 @param flags Or-combination of CreateCompatibleFlags values
324 wxIDirectFBSurfacePtr
CreateCompatible(const wxSize
& size
= wxDefaultSize
,
327 bool Lock(DFBSurfaceLockFlags flags
, void **ret_ptr
, int *ret_pitch
)
328 { return Check(m_ptr
->Lock(m_ptr
, flags
, ret_ptr
, ret_pitch
)); }
331 { return Check(m_ptr
->Unlock(m_ptr
)); }
333 /// Helper struct for safe locking & unlocking of surfaces
336 Locked(const wxIDirectFBSurfacePtr
& surface
, DFBSurfaceLockFlags flags
)
339 if ( !surface
->Lock(flags
, &ptr
, &pitch
) )
353 wxIDirectFBSurfacePtr m_surface
;
358 // this is private because we want user code to use FlipToFront()
359 bool Flip(const DFBRegion
*region
, int flags
);
363 //-----------------------------------------------------------------------------
364 // wxIDirectFBEventBuffer
365 //-----------------------------------------------------------------------------
367 struct wxIDirectFBEventBuffer
: public wxDfbWrapper
<IDirectFBEventBuffer
>
369 wxIDirectFBEventBuffer(IDirectFBEventBuffer
*s
) { Init(s
); }
373 return Check(m_ptr
->WakeUp(m_ptr
));
378 // returns DFB_OK if there is >=1 event, DFB_BUFFEREMPTY otherwise
379 DFBResult r
= m_ptr
->HasEvent(m_ptr
);
381 // NB: Check() also returns true for DFB_BUFFEREMPTY, so we can't just
382 // return it's return value:
384 return (r
== DFB_OK
);
387 bool WaitForEventWithTimeout(unsigned secs
, unsigned millisecs
)
389 DFBResult r
= m_ptr
->WaitForEventWithTimeout(m_ptr
, secs
, millisecs
);
391 // DFB_TIMEOUT is not an error in this function:
392 if ( r
== DFB_TIMEOUT
)
394 m_lastResult
= DFB_TIMEOUT
;
401 bool GetEvent(wxDFBEvent
& event
)
403 return Check(m_ptr
->GetEvent(m_ptr
, &event
));
408 //-----------------------------------------------------------------------------
410 //-----------------------------------------------------------------------------
412 struct wxIDirectFBWindow
: public wxDfbWrapper
<IDirectFBWindow
>
414 wxIDirectFBWindow(IDirectFBWindow
*s
) { Init(s
); }
416 bool GetID(DFBWindowID
*id
)
417 { return Check(m_ptr
->GetID(m_ptr
, id
)); }
419 bool GetPosition(int *x
, int *y
)
420 { return Check(m_ptr
->GetPosition(m_ptr
, x
, y
)); }
422 bool GetSize(int *w
, int *h
)
423 { return Check(m_ptr
->GetSize(m_ptr
, w
, h
)); }
425 bool MoveTo(int x
, int y
)
426 { return Check(m_ptr
->MoveTo(m_ptr
, x
, y
)); }
428 bool Resize(int w
, int h
)
429 { return Check(m_ptr
->Resize(m_ptr
, w
, h
)); }
431 bool SetOpacity(u8 opacity
)
432 { return Check(m_ptr
->SetOpacity(m_ptr
, opacity
)); }
434 bool SetStackingClass(DFBWindowStackingClass klass
)
435 { return Check(m_ptr
->SetStackingClass(m_ptr
, klass
)); }
437 wxIDirectFBSurfacePtr
GetSurface()
440 if ( Check(m_ptr
->GetSurface(m_ptr
, &s
)) )
441 return new wxIDirectFBSurface(s
);
446 bool AttachEventBuffer(const wxIDirectFBEventBufferPtr
& buffer
)
447 { return Check(m_ptr
->AttachEventBuffer(m_ptr
, buffer
->GetRaw())); }
450 { return Check(m_ptr
->RequestFocus(m_ptr
)); }
453 { return Check(m_ptr
->Destroy(m_ptr
)); }
457 //-----------------------------------------------------------------------------
458 // wxIDirectFBDisplayLayer
459 //-----------------------------------------------------------------------------
461 struct wxIDirectFBDisplayLayer
: public wxDfbWrapper
<IDirectFBDisplayLayer
>
463 wxIDirectFBDisplayLayer(IDirectFBDisplayLayer
*s
) { Init(s
); }
465 wxIDirectFBWindowPtr
CreateWindow(const DFBWindowDescription
*desc
)
468 if ( Check(m_ptr
->CreateWindow(m_ptr
, desc
, &w
)) )
469 return new wxIDirectFBWindow(w
);
474 bool GetConfiguration(DFBDisplayLayerConfig
*config
)
475 { return Check(m_ptr
->GetConfiguration(m_ptr
, config
)); }
477 wxVideoMode
GetVideoMode();
479 bool GetCursorPosition(int *x
, int *y
)
480 { return Check(m_ptr
->GetCursorPosition(m_ptr
, x
, y
)); }
482 bool WarpCursor(int x
, int y
)
483 { return Check(m_ptr
->WarpCursor(m_ptr
, x
, y
)); }
487 //-----------------------------------------------------------------------------
489 //-----------------------------------------------------------------------------
491 struct wxIDirectFB
: public wxDfbWrapper
<IDirectFB
>
494 Returns pointer to DirectFB singleton object, it never returns NULL
495 after wxApp was initialized. The object is cached, so calling this
498 static wxIDirectFBPtr
Get()
500 if ( !ms_ptr
) CreateDirectFB();
504 bool SetVideoMode(int w
, int h
, int bpp
)
505 { return Check(m_ptr
->SetVideoMode(m_ptr
, w
, h
, bpp
)); }
507 wxIDirectFBSurfacePtr
CreateSurface(const DFBSurfaceDescription
*desc
)
510 if ( Check(m_ptr
->CreateSurface(m_ptr
, desc
, &s
)) )
511 return new wxIDirectFBSurface(s
);
516 wxIDirectFBEventBufferPtr
CreateEventBuffer()
518 IDirectFBEventBuffer
*b
;
519 if ( Check(m_ptr
->CreateEventBuffer(m_ptr
, &b
)) )
520 return new wxIDirectFBEventBuffer(b
);
525 wxIDirectFBFontPtr
CreateFont(const char *filename
,
526 const DFBFontDescription
*desc
)
529 if ( Check(m_ptr
->CreateFont(m_ptr
, filename
, desc
, &f
)) )
530 return new wxIDirectFBFont(f
);
535 wxIDirectFBDisplayLayerPtr
536 GetDisplayLayer(DFBDisplayLayerID id
= DLID_PRIMARY
)
538 IDirectFBDisplayLayer
*l
;
539 if ( Check(m_ptr
->GetDisplayLayer(m_ptr
, id
, &l
)) )
540 return new wxIDirectFBDisplayLayer(l
);
545 /// Returns primary surface
546 wxIDirectFBSurfacePtr
GetPrimarySurface();
549 wxIDirectFB(IDirectFB
*ptr
) { Init(ptr
); }
551 // creates ms_ptr instance
552 static void CreateDirectFB();
554 static void CleanUp();
555 friend class wxApp
; // calls CleanUp
557 // pointer to the singleton IDirectFB object
558 static wxIDirectFBPtr ms_ptr
;
561 #endif // _WX_DFB_WRAPDFB_H_