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"
20 wxDFB_DECLARE_INTERFACE(IDirectFB
);
21 wxDFB_DECLARE_INTERFACE(IDirectFBDisplayLayer
);
22 wxDFB_DECLARE_INTERFACE(IDirectFBFont
);
23 wxDFB_DECLARE_INTERFACE(IDirectFBWindow
);
24 wxDFB_DECLARE_INTERFACE(IDirectFBSurface
);
25 wxDFB_DECLARE_INTERFACE(IDirectFBPalette
);
26 wxDFB_DECLARE_INTERFACE(IDirectFBEventBuffer
);
30 Checks the @a code of a DirectFB call and returns true if it was
31 successful and false if it failed, logging the errors as appropriate
32 (asserts for programming errors, wxLogError for runtime failures).
34 bool wxDfbCheckReturn(DFBResult code
);
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
41 The struct defined by this macro is a thin wrapper around DFB*Event type.
42 It is needed because DFB*Event are typedefs and so we can't forward declare
43 them, but we need to pass them to methods declared in public headers where
44 <directfb.h> cannot be included. So this struct just holds the event value,
45 it's sole purpose is that it can be forward declared.
47 #define WXDFB_DEFINE_EVENT_WRAPPER(T) \
51 wx##T(const T& event) : m_event(event) {} \
53 operator T&() { return m_event; } \
54 operator const T&() const { return m_event; } \
55 T* operator&() { return &m_event; } \
57 DFBEventClass GetClass() const { return m_event.clazz; } \
63 WXDFB_DEFINE_EVENT_WRAPPER(DFBEvent
)
64 WXDFB_DEFINE_EVENT_WRAPPER(DFBWindowEvent
)
67 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
71 /// Base class for wxDfbWrapper<T>
72 class wxDfbWrapperBase
75 /// Increases reference count of the object
81 /// Decreases reference count and if it reaches zero, deletes the object
84 if ( --m_refCnt
== 0 )
88 /// Returns result code of the last call
89 DFBResult
GetLastResult() const { return m_lastResult
; }
92 wxDfbWrapperBase() : m_refCnt(1), m_lastResult(DFB_OK
) {}
94 /// Dtor may only be called from Release()
95 virtual ~wxDfbWrapperBase() {}
98 Checks the @a result of a DirectFB call and returns true if it was
99 successful and false if it failed. Also stores result of the call
100 so that it can be obtained by calling GetLastResult().
102 bool Check(DFBResult result
)
104 m_lastResult
= result
;
105 return wxDfbCheckReturn(result
);
112 /// Result of the last DirectFB call
113 DFBResult m_lastResult
;
117 This template is base class for friendly C++ wrapper around DirectFB
120 The wrapper provides same API as DirectFB, with a few exceptions:
121 - methods return true/false instead of error code
122 - methods that return or create another interface return pointer to the
123 interface (or NULL on failure) instead of storing it in the last
125 - interface arguments use wxFooPtr type instead of raw DirectFB pointer
126 - methods taking flags use int type instead of an enum when the flags
127 can be or-combination of enum elements (this is workaround for
128 C++-unfriendly DirectFB API)
131 class wxDfbWrapper
: public wxDfbWrapperBase
134 /// "Raw" DirectFB interface type
135 typedef T DirectFBIface
;
137 /// Returns raw DirectFB pointer
138 T
*GetRaw() const { return m_ptr
; }
141 /// To be called from ctor. Takes ownership of raw object.
142 void Init(T
*ptr
) { m_ptr
= ptr
; }
144 /// Dtor may only be used from Release
148 m_ptr
->Release(m_ptr
);
152 // pointer to DirectFB object
157 //-----------------------------------------------------------------------------
159 //-----------------------------------------------------------------------------
161 struct wxIDirectFBFont
: public wxDfbWrapper
<IDirectFBFont
>
163 wxIDirectFBFont(IDirectFBFont
*s
) { Init(s
); }
165 bool GetStringWidth(const char *text
, int bytes
, int *w
)
166 { return Check(m_ptr
->GetStringWidth(m_ptr
, text
, bytes
, w
)); }
168 bool GetStringExtents(const char *text
, int bytes
,
169 DFBRectangle
*logicalRect
, DFBRectangle
*inkRect
)
171 return Check(m_ptr
->GetStringExtents(m_ptr
, text
, bytes
,
172 logicalRect
, inkRect
));
175 bool GetHeight(int *h
)
176 { return Check(m_ptr
->GetHeight(m_ptr
, h
)); }
178 bool GetDescender(int *descender
)
179 { return Check(m_ptr
->GetDescender(m_ptr
, descender
)); }
183 //-----------------------------------------------------------------------------
184 // wxIDirectFBPalette
185 //-----------------------------------------------------------------------------
187 struct wxIDirectFBPalette
: public wxDfbWrapper
<IDirectFBPalette
>
189 wxIDirectFBPalette(IDirectFBPalette
*s
) { Init(s
); }
193 //-----------------------------------------------------------------------------
194 // wxIDirectFBSurface
195 //-----------------------------------------------------------------------------
197 struct wxIDirectFBSurface
: public wxDfbWrapper
<IDirectFBSurface
>
199 wxIDirectFBSurface(IDirectFBSurface
*s
) { Init(s
); }
201 bool GetSize(int *w
, int *h
)
202 { return Check(m_ptr
->GetSize(m_ptr
, w
, h
)); }
204 bool GetCapabilities(DFBSurfaceCapabilities
*caps
)
205 { return Check(m_ptr
->GetCapabilities(m_ptr
, caps
)); }
207 bool GetPixelFormat(DFBSurfacePixelFormat
*caps
)
208 { return Check(m_ptr
->GetPixelFormat(m_ptr
, caps
)); }
210 bool SetClip(const DFBRegion
*clip
)
211 { return Check(m_ptr
->SetClip(m_ptr
, clip
)); }
213 bool SetColor(__u8 r
, __u8 g
, __u8 b
, __u8 a
)
214 { return Check(m_ptr
->SetColor(m_ptr
, r
, g
, b
, a
)); }
216 bool Clear(__u8 r
, __u8 g
, __u8 b
, __u8 a
)
217 { return Check(m_ptr
->Clear(m_ptr
, r
, g
, b
, a
)); }
219 bool DrawLine(int x1
, int y1
, int x2
, int y2
)
220 { return Check(m_ptr
->DrawLine(m_ptr
, x1
, y1
, x2
, y2
)); }
222 bool DrawRectangle(int x
, int y
, int w
, int h
)
223 { return Check(m_ptr
->DrawRectangle(m_ptr
, x
, y
, w
, h
)); }
225 bool FillRectangle(int x
, int y
, int w
, int h
)
226 { return Check(m_ptr
->FillRectangle(m_ptr
, x
, y
, w
, h
)); }
228 bool SetFont(const wxIDirectFBFontPtr
& font
)
229 { return Check(m_ptr
->SetFont(m_ptr
, font
->GetRaw())); }
231 bool DrawString(const char *text
, int bytes
, int x
, int y
, int flags
)
233 return Check(m_ptr
->DrawString(m_ptr
, text
, bytes
, x
, y
,
234 (DFBSurfaceTextFlags
)flags
));
238 Updates the front buffer from the back buffer. If @a region is not
239 NULL, only given rectangle is updated.
241 bool FlipToFront(const DFBRegion
*region
= NULL
);
243 wxIDirectFBSurfacePtr
GetSubSurface(const DFBRectangle
*rect
)
246 if ( Check(m_ptr
->GetSubSurface(m_ptr
, rect
, &s
)) )
247 return new wxIDirectFBSurface(s
);
252 wxIDirectFBPalettePtr
GetPalette()
255 if ( Check(m_ptr
->GetPalette(m_ptr
, &s
)) )
256 return new wxIDirectFBPalette(s
);
261 bool SetPalette(const wxIDirectFBPalettePtr
& pal
)
262 { return Check(m_ptr
->SetPalette(m_ptr
, pal
->GetRaw())); }
264 bool SetBlittingFlags(int flags
)
267 m_ptr
->SetBlittingFlags(m_ptr
, (DFBSurfaceBlittingFlags
)flags
));
270 bool Blit(const wxIDirectFBSurfacePtr
& source
,
271 const DFBRectangle
*source_rect
,
273 { return Blit(source
->GetRaw(), source_rect
, x
, y
); }
275 bool Blit(IDirectFBSurface
*source
,
276 const DFBRectangle
*source_rect
,
278 { return Check(m_ptr
->Blit(m_ptr
, source
, source_rect
, x
, y
)); }
280 bool StretchBlit(const wxIDirectFBSurfacePtr
& source
,
281 const DFBRectangle
*source_rect
,
282 const DFBRectangle
*dest_rect
)
284 return Check(m_ptr
->StretchBlit(m_ptr
, source
->GetRaw(),
285 source_rect
, dest_rect
));
289 /// Returns bit depth used by the surface or -1 on error
293 Creates a new surface by cloning this one. New surface will have same
294 capabilities, pixel format and pixel data as the existing one.
296 @see CreateCompatible
298 wxIDirectFBSurfacePtr
Clone();
300 /// Flags for CreateCompatible()
301 enum CreateCompatibleFlags
303 /// Don't create double-buffered surface
304 CreateCompatible_NoBackBuffer
= 1
308 Creates a surface compatible with this one, i.e. surface with the same
309 capabilities and pixel format, but with different and size.
311 @param size Size of the surface to create. If wxDefaultSize, use the
312 size of this surface.
313 @param flags Or-combination of CreateCompatibleFlags values
315 wxIDirectFBSurfacePtr
CreateCompatible(const wxSize
& size
= wxDefaultSize
,
319 // this is private because we want user code to use FlipToFront()
320 bool Flip(const DFBRegion
*region
, int flags
);
324 //-----------------------------------------------------------------------------
325 // wxIDirectFBEventBuffer
326 //-----------------------------------------------------------------------------
328 struct wxIDirectFBEventBuffer
: public wxDfbWrapper
<IDirectFBEventBuffer
>
330 wxIDirectFBEventBuffer(IDirectFBEventBuffer
*s
) { Init(s
); }
334 return Check(m_ptr
->WakeUp(m_ptr
));
339 // returns DFB_OK if there is >=1 event, DFB_BUFFEREMPTY otherwise
340 DFBResult r
= m_ptr
->HasEvent(m_ptr
);
342 // NB: Check() also returns true for DFB_BUFFEREMPTY, so we can't just
343 // return it's return value:
345 return (r
== DFB_OK
);
348 bool WaitForEventWithTimeout(unsigned secs
, unsigned millisecs
)
350 DFBResult r
= m_ptr
->WaitForEventWithTimeout(m_ptr
, secs
, millisecs
);
352 // DFB_TIMEOUT is not an error in this function:
353 if ( r
== DFB_TIMEOUT
)
355 m_lastResult
= DFB_TIMEOUT
;
362 bool GetEvent(wxDFBEvent
& event
)
364 return Check(m_ptr
->GetEvent(m_ptr
, &event
));
369 //-----------------------------------------------------------------------------
371 //-----------------------------------------------------------------------------
373 struct wxIDirectFBWindow
: public wxDfbWrapper
<IDirectFBWindow
>
375 wxIDirectFBWindow(IDirectFBWindow
*s
) { Init(s
); }
377 bool GetID(DFBWindowID
*id
)
378 { return Check(m_ptr
->GetID(m_ptr
, id
)); }
380 bool GetPosition(int *x
, int *y
)
381 { return Check(m_ptr
->GetPosition(m_ptr
, x
, y
)); }
383 bool GetSize(int *w
, int *h
)
384 { return Check(m_ptr
->GetSize(m_ptr
, w
, h
)); }
386 bool MoveTo(int x
, int y
)
387 { return Check(m_ptr
->MoveTo(m_ptr
, x
, y
)); }
389 bool Resize(int w
, int h
)
390 { return Check(m_ptr
->Resize(m_ptr
, w
, h
)); }
392 bool SetOpacity(__u8 opacity
)
393 { return Check(m_ptr
->SetOpacity(m_ptr
, opacity
)); }
395 bool SetStackingClass(DFBWindowStackingClass klass
)
396 { return Check(m_ptr
->SetStackingClass(m_ptr
, klass
)); }
398 wxIDirectFBSurfacePtr
GetSurface()
401 if ( Check(m_ptr
->GetSurface(m_ptr
, &s
)) )
402 return new wxIDirectFBSurface(s
);
407 bool AttachEventBuffer(const wxIDirectFBEventBufferPtr
& buffer
)
408 { return Check(m_ptr
->AttachEventBuffer(m_ptr
, buffer
->GetRaw())); }
411 { return Check(m_ptr
->RequestFocus(m_ptr
)); }
414 { return Check(m_ptr
->Destroy(m_ptr
)); }
418 //-----------------------------------------------------------------------------
419 // wxIDirectFBDisplayLayer
420 //-----------------------------------------------------------------------------
422 struct wxIDirectFBDisplayLayer
: public wxDfbWrapper
<IDirectFBDisplayLayer
>
424 wxIDirectFBDisplayLayer(IDirectFBDisplayLayer
*s
) { Init(s
); }
426 wxIDirectFBWindowPtr
CreateWindow(const DFBWindowDescription
*desc
)
429 if ( Check(m_ptr
->CreateWindow(m_ptr
, desc
, &w
)) )
430 return new wxIDirectFBWindow(w
);
435 bool GetConfiguration(DFBDisplayLayerConfig
*config
)
436 { return Check(m_ptr
->GetConfiguration(m_ptr
, config
)); }
438 wxVideoMode
GetVideoMode();
440 bool GetCursorPosition(int *x
, int *y
)
441 { return Check(m_ptr
->GetCursorPosition(m_ptr
, x
, y
)); }
443 bool WarpCursor(int x
, int y
)
444 { return Check(m_ptr
->WarpCursor(m_ptr
, x
, y
)); }
448 //-----------------------------------------------------------------------------
450 //-----------------------------------------------------------------------------
452 struct wxIDirectFB
: public wxDfbWrapper
<IDirectFB
>
455 Returns pointer to DirectFB singleton object, it never returns NULL
456 after wxApp was initialized. The object is cached, so calling this
459 static wxIDirectFBPtr
Get()
461 if ( !ms_ptr
) CreateDirectFB();
465 bool SetVideoMode(int w
, int h
, int bpp
)
466 { return Check(m_ptr
->SetVideoMode(m_ptr
, w
, h
, bpp
)); }
468 wxIDirectFBSurfacePtr
CreateSurface(const DFBSurfaceDescription
*desc
)
471 if ( Check(m_ptr
->CreateSurface(m_ptr
, desc
, &s
)) )
472 return new wxIDirectFBSurface(s
);
477 wxIDirectFBEventBufferPtr
CreateEventBuffer()
479 IDirectFBEventBuffer
*b
;
480 if ( Check(m_ptr
->CreateEventBuffer(m_ptr
, &b
)) )
481 return new wxIDirectFBEventBuffer(b
);
486 wxIDirectFBFontPtr
CreateFont(const char *filename
,
487 const DFBFontDescription
*desc
)
490 if ( Check(m_ptr
->CreateFont(m_ptr
, filename
, desc
, &f
)) )
491 return new wxIDirectFBFont(f
);
496 wxIDirectFBDisplayLayerPtr
497 GetDisplayLayer(DFBDisplayLayerID id
= DLID_PRIMARY
)
499 IDirectFBDisplayLayer
*l
;
500 if ( Check(m_ptr
->GetDisplayLayer(m_ptr
, id
, &l
)) )
501 return new wxIDirectFBDisplayLayer(l
);
506 /// Returns primary surface
507 wxIDirectFBSurfacePtr
GetPrimarySurface();
510 wxIDirectFB(IDirectFB
*ptr
) { Init(ptr
); }
512 // creates ms_ptr instance
513 static void CreateDirectFB();
515 static void CleanUp();
516 friend class wxApp
; // calls CleanUp
518 // pointer to the singleton IDirectFB object
519 static wxIDirectFBPtr ms_ptr
;
522 #endif // _WX_DFB_WRAPDFB_H_