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 // convenience version of GetPixelFormat, returns DSPF_UNKNOWN if fails
211 DFBSurfacePixelFormat
GetPixelFormat();
213 bool SetClip(const DFBRegion
*clip
)
214 { return Check(m_ptr
->SetClip(m_ptr
, clip
)); }
216 bool SetColor(__u8 r
, __u8 g
, __u8 b
, __u8 a
)
217 { return Check(m_ptr
->SetColor(m_ptr
, r
, g
, b
, a
)); }
219 bool Clear(__u8 r
, __u8 g
, __u8 b
, __u8 a
)
220 { return Check(m_ptr
->Clear(m_ptr
, r
, g
, b
, a
)); }
222 bool DrawLine(int x1
, int y1
, int x2
, int y2
)
223 { return Check(m_ptr
->DrawLine(m_ptr
, x1
, y1
, x2
, y2
)); }
225 bool DrawRectangle(int x
, int y
, int w
, int h
)
226 { return Check(m_ptr
->DrawRectangle(m_ptr
, x
, y
, w
, h
)); }
228 bool FillRectangle(int x
, int y
, int w
, int h
)
229 { return Check(m_ptr
->FillRectangle(m_ptr
, x
, y
, w
, h
)); }
231 bool SetFont(const wxIDirectFBFontPtr
& font
)
232 { return Check(m_ptr
->SetFont(m_ptr
, font
->GetRaw())); }
234 bool DrawString(const char *text
, int bytes
, int x
, int y
, int flags
)
236 return Check(m_ptr
->DrawString(m_ptr
, text
, bytes
, x
, y
,
237 (DFBSurfaceTextFlags
)flags
));
241 Updates the front buffer from the back buffer. If @a region is not
242 NULL, only given rectangle is updated.
244 bool FlipToFront(const DFBRegion
*region
= NULL
);
246 wxIDirectFBSurfacePtr
GetSubSurface(const DFBRectangle
*rect
)
249 if ( Check(m_ptr
->GetSubSurface(m_ptr
, rect
, &s
)) )
250 return new wxIDirectFBSurface(s
);
255 wxIDirectFBPalettePtr
GetPalette()
258 if ( Check(m_ptr
->GetPalette(m_ptr
, &s
)) )
259 return new wxIDirectFBPalette(s
);
264 bool SetPalette(const wxIDirectFBPalettePtr
& pal
)
265 { return Check(m_ptr
->SetPalette(m_ptr
, pal
->GetRaw())); }
267 bool SetBlittingFlags(int flags
)
270 m_ptr
->SetBlittingFlags(m_ptr
, (DFBSurfaceBlittingFlags
)flags
));
273 bool Blit(const wxIDirectFBSurfacePtr
& source
,
274 const DFBRectangle
*source_rect
,
276 { return Blit(source
->GetRaw(), source_rect
, x
, y
); }
278 bool Blit(IDirectFBSurface
*source
,
279 const DFBRectangle
*source_rect
,
281 { return Check(m_ptr
->Blit(m_ptr
, source
, source_rect
, x
, y
)); }
283 bool StretchBlit(const wxIDirectFBSurfacePtr
& source
,
284 const DFBRectangle
*source_rect
,
285 const DFBRectangle
*dest_rect
)
287 return Check(m_ptr
->StretchBlit(m_ptr
, source
->GetRaw(),
288 source_rect
, dest_rect
));
291 /// Returns bit depth used by the surface or -1 on error
295 Creates a new surface by cloning this one. New surface will have same
296 capabilities, pixel format and pixel data as the existing one.
298 @see CreateCompatible
300 wxIDirectFBSurfacePtr
Clone();
302 /// Flags for CreateCompatible()
303 enum CreateCompatibleFlags
305 /// Don't create double-buffered surface
306 CreateCompatible_NoBackBuffer
= 1
310 Creates a surface compatible with this one, i.e. surface with the same
311 capabilities and pixel format, but with different and size.
313 @param size Size of the surface to create. If wxDefaultSize, use the
314 size of this surface.
315 @param flags Or-combination of CreateCompatibleFlags values
317 wxIDirectFBSurfacePtr
CreateCompatible(const wxSize
& size
= wxDefaultSize
,
320 bool Lock(DFBSurfaceLockFlags flags
, void **ret_ptr
, int *ret_pitch
)
321 { return Check(m_ptr
->Lock(m_ptr
, flags
, ret_ptr
, ret_pitch
)); }
324 { return Check(m_ptr
->Unlock(m_ptr
)); }
326 /// Helper struct for safe locking & unlocking of surfaces
329 Locked(const wxIDirectFBSurfacePtr
& surface
, DFBSurfaceLockFlags flags
)
332 if ( !surface
->Lock(flags
, &ptr
, &pitch
) )
346 wxIDirectFBSurfacePtr m_surface
;
351 // this is private because we want user code to use FlipToFront()
352 bool Flip(const DFBRegion
*region
, int flags
);
356 //-----------------------------------------------------------------------------
357 // wxIDirectFBEventBuffer
358 //-----------------------------------------------------------------------------
360 struct wxIDirectFBEventBuffer
: public wxDfbWrapper
<IDirectFBEventBuffer
>
362 wxIDirectFBEventBuffer(IDirectFBEventBuffer
*s
) { Init(s
); }
366 return Check(m_ptr
->WakeUp(m_ptr
));
371 // returns DFB_OK if there is >=1 event, DFB_BUFFEREMPTY otherwise
372 DFBResult r
= m_ptr
->HasEvent(m_ptr
);
374 // NB: Check() also returns true for DFB_BUFFEREMPTY, so we can't just
375 // return it's return value:
377 return (r
== DFB_OK
);
380 bool WaitForEventWithTimeout(unsigned secs
, unsigned millisecs
)
382 DFBResult r
= m_ptr
->WaitForEventWithTimeout(m_ptr
, secs
, millisecs
);
384 // DFB_TIMEOUT is not an error in this function:
385 if ( r
== DFB_TIMEOUT
)
387 m_lastResult
= DFB_TIMEOUT
;
394 bool GetEvent(wxDFBEvent
& event
)
396 return Check(m_ptr
->GetEvent(m_ptr
, &event
));
401 //-----------------------------------------------------------------------------
403 //-----------------------------------------------------------------------------
405 struct wxIDirectFBWindow
: public wxDfbWrapper
<IDirectFBWindow
>
407 wxIDirectFBWindow(IDirectFBWindow
*s
) { Init(s
); }
409 bool GetID(DFBWindowID
*id
)
410 { return Check(m_ptr
->GetID(m_ptr
, id
)); }
412 bool GetPosition(int *x
, int *y
)
413 { return Check(m_ptr
->GetPosition(m_ptr
, x
, y
)); }
415 bool GetSize(int *w
, int *h
)
416 { return Check(m_ptr
->GetSize(m_ptr
, w
, h
)); }
418 bool MoveTo(int x
, int y
)
419 { return Check(m_ptr
->MoveTo(m_ptr
, x
, y
)); }
421 bool Resize(int w
, int h
)
422 { return Check(m_ptr
->Resize(m_ptr
, w
, h
)); }
424 bool SetOpacity(__u8 opacity
)
425 { return Check(m_ptr
->SetOpacity(m_ptr
, opacity
)); }
427 bool SetStackingClass(DFBWindowStackingClass klass
)
428 { return Check(m_ptr
->SetStackingClass(m_ptr
, klass
)); }
430 wxIDirectFBSurfacePtr
GetSurface()
433 if ( Check(m_ptr
->GetSurface(m_ptr
, &s
)) )
434 return new wxIDirectFBSurface(s
);
439 bool AttachEventBuffer(const wxIDirectFBEventBufferPtr
& buffer
)
440 { return Check(m_ptr
->AttachEventBuffer(m_ptr
, buffer
->GetRaw())); }
443 { return Check(m_ptr
->RequestFocus(m_ptr
)); }
446 { return Check(m_ptr
->Destroy(m_ptr
)); }
450 //-----------------------------------------------------------------------------
451 // wxIDirectFBDisplayLayer
452 //-----------------------------------------------------------------------------
454 struct wxIDirectFBDisplayLayer
: public wxDfbWrapper
<IDirectFBDisplayLayer
>
456 wxIDirectFBDisplayLayer(IDirectFBDisplayLayer
*s
) { Init(s
); }
458 wxIDirectFBWindowPtr
CreateWindow(const DFBWindowDescription
*desc
)
461 if ( Check(m_ptr
->CreateWindow(m_ptr
, desc
, &w
)) )
462 return new wxIDirectFBWindow(w
);
467 bool GetConfiguration(DFBDisplayLayerConfig
*config
)
468 { return Check(m_ptr
->GetConfiguration(m_ptr
, config
)); }
470 wxVideoMode
GetVideoMode();
472 bool GetCursorPosition(int *x
, int *y
)
473 { return Check(m_ptr
->GetCursorPosition(m_ptr
, x
, y
)); }
475 bool WarpCursor(int x
, int y
)
476 { return Check(m_ptr
->WarpCursor(m_ptr
, x
, y
)); }
480 //-----------------------------------------------------------------------------
482 //-----------------------------------------------------------------------------
484 struct wxIDirectFB
: public wxDfbWrapper
<IDirectFB
>
487 Returns pointer to DirectFB singleton object, it never returns NULL
488 after wxApp was initialized. The object is cached, so calling this
491 static wxIDirectFBPtr
Get()
493 if ( !ms_ptr
) CreateDirectFB();
497 bool SetVideoMode(int w
, int h
, int bpp
)
498 { return Check(m_ptr
->SetVideoMode(m_ptr
, w
, h
, bpp
)); }
500 wxIDirectFBSurfacePtr
CreateSurface(const DFBSurfaceDescription
*desc
)
503 if ( Check(m_ptr
->CreateSurface(m_ptr
, desc
, &s
)) )
504 return new wxIDirectFBSurface(s
);
509 wxIDirectFBEventBufferPtr
CreateEventBuffer()
511 IDirectFBEventBuffer
*b
;
512 if ( Check(m_ptr
->CreateEventBuffer(m_ptr
, &b
)) )
513 return new wxIDirectFBEventBuffer(b
);
518 wxIDirectFBFontPtr
CreateFont(const char *filename
,
519 const DFBFontDescription
*desc
)
522 if ( Check(m_ptr
->CreateFont(m_ptr
, filename
, desc
, &f
)) )
523 return new wxIDirectFBFont(f
);
528 wxIDirectFBDisplayLayerPtr
529 GetDisplayLayer(DFBDisplayLayerID id
= DLID_PRIMARY
)
531 IDirectFBDisplayLayer
*l
;
532 if ( Check(m_ptr
->GetDisplayLayer(m_ptr
, id
, &l
)) )
533 return new wxIDirectFBDisplayLayer(l
);
538 /// Returns primary surface
539 wxIDirectFBSurfacePtr
GetPrimarySurface();
542 wxIDirectFB(IDirectFB
*ptr
) { Init(ptr
); }
544 // creates ms_ptr instance
545 static void CreateDirectFB();
547 static void CleanUp();
548 friend class wxApp
; // calls CleanUp
550 // pointer to the singleton IDirectFB object
551 static wxIDirectFBPtr ms_ptr
;
554 #endif // _WX_DFB_WRAPDFB_H_