1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/dfb/wrapdfb.h
3 // Purpose: wx wrappers for DirectFB interfaces
4 // Author: Vaclav Slavik
6 // Copyright: (c) 2006 REA Elektronik GmbH
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #ifndef _WX_DFB_WRAPDFB_H_
11 #define _WX_DFB_WRAPDFB_H_
13 #include "wx/dfb/dfbptr.h"
14 #include "wx/gdicmn.h"
15 #include "wx/vidmode.h"
18 #include <directfb_version.h>
20 // DFB < 1.0 didn't have u8 type, only __u8
21 #if DIRECTFB_MAJOR_VERSION == 0
26 wxDFB_DECLARE_INTERFACE(IDirectFB
);
27 wxDFB_DECLARE_INTERFACE(IDirectFBDisplayLayer
);
28 wxDFB_DECLARE_INTERFACE(IDirectFBFont
);
29 wxDFB_DECLARE_INTERFACE(IDirectFBWindow
);
30 wxDFB_DECLARE_INTERFACE(IDirectFBSurface
);
31 wxDFB_DECLARE_INTERFACE(IDirectFBPalette
);
32 wxDFB_DECLARE_INTERFACE(IDirectFBEventBuffer
);
36 Checks the @a code of a DirectFB call and returns true if it was
37 successful and false if it failed, logging the errors as appropriate
38 (asserts for programming errors, wxLogError for runtime failures).
40 bool wxDfbCheckReturn(DFBResult code
);
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
47 The struct defined by this macro is a thin wrapper around DFB*Event type.
48 It is needed because DFB*Event are typedefs and so we can't forward declare
49 them, but we need to pass them to methods declared in public headers where
50 <directfb.h> cannot be included. So this struct just holds the event value,
51 it's sole purpose is that it can be forward declared.
53 #define WXDFB_DEFINE_EVENT_WRAPPER(T) \
57 wx##T(const T& event) : m_event(event) {} \
59 operator T&() { return m_event; } \
60 operator const T&() const { return m_event; } \
61 T* operator&() { return &m_event; } \
63 DFBEventClass GetClass() const { return m_event.clazz; } \
69 WXDFB_DEFINE_EVENT_WRAPPER(DFBEvent
)
70 WXDFB_DEFINE_EVENT_WRAPPER(DFBWindowEvent
)
73 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
77 /// Base class for wxDfbWrapper<T>
78 class wxDfbWrapperBase
81 /// Increases reference count of the object
87 /// Decreases reference count and if it reaches zero, deletes the object
90 if ( --m_refCnt
== 0 )
94 /// Returns result code of the last call
95 DFBResult
GetLastResult() const { return m_lastResult
; }
98 wxDfbWrapperBase() : m_refCnt(1), m_lastResult(DFB_OK
) {}
100 /// Dtor may only be called from Release()
101 virtual ~wxDfbWrapperBase() {}
104 Checks the @a result of a DirectFB call and returns true if it was
105 successful and false if it failed. Also stores result of the call
106 so that it can be obtained by calling GetLastResult().
108 bool Check(DFBResult result
)
110 m_lastResult
= result
;
111 return wxDfbCheckReturn(result
);
118 /// Result of the last DirectFB call
119 DFBResult m_lastResult
;
123 This template is base class for friendly C++ wrapper around DirectFB
126 The wrapper provides same API as DirectFB, with a few exceptions:
127 - methods return true/false instead of error code
128 - methods that return or create another interface return pointer to the
129 interface (or NULL on failure) instead of storing it in the last
131 - interface arguments use wxFooPtr type instead of raw DirectFB pointer
132 - methods taking flags use int type instead of an enum when the flags
133 can be or-combination of enum elements (this is workaround for
134 C++-unfriendly DirectFB API)
137 class wxDfbWrapper
: public wxDfbWrapperBase
140 /// "Raw" DirectFB interface type
141 typedef T DirectFBIface
;
143 /// Returns raw DirectFB pointer
144 T
*GetRaw() const { return m_ptr
; }
147 /// To be called from ctor. Takes ownership of raw object.
148 void Init(T
*ptr
) { m_ptr
= ptr
; }
150 /// Dtor may only be used from Release
154 m_ptr
->Release(m_ptr
);
158 // pointer to DirectFB object
163 //-----------------------------------------------------------------------------
165 //-----------------------------------------------------------------------------
167 struct wxIDirectFBFont
: public wxDfbWrapper
<IDirectFBFont
>
169 wxIDirectFBFont(IDirectFBFont
*s
) { Init(s
); }
171 bool GetStringWidth(const char *text
, int bytes
, int *w
)
172 { return Check(m_ptr
->GetStringWidth(m_ptr
, text
, bytes
, w
)); }
174 bool GetStringExtents(const char *text
, int bytes
,
175 DFBRectangle
*logicalRect
, DFBRectangle
*inkRect
)
177 return Check(m_ptr
->GetStringExtents(m_ptr
, text
, bytes
,
178 logicalRect
, inkRect
));
181 bool GetHeight(int *h
)
182 { return Check(m_ptr
->GetHeight(m_ptr
, h
)); }
184 bool GetDescender(int *descender
)
185 { return Check(m_ptr
->GetDescender(m_ptr
, descender
)); }
189 //-----------------------------------------------------------------------------
190 // wxIDirectFBPalette
191 //-----------------------------------------------------------------------------
193 struct wxIDirectFBPalette
: public wxDfbWrapper
<IDirectFBPalette
>
195 wxIDirectFBPalette(IDirectFBPalette
*s
) { Init(s
); }
199 //-----------------------------------------------------------------------------
200 // wxIDirectFBSurface
201 //-----------------------------------------------------------------------------
203 struct wxIDirectFBSurface
: public wxDfbWrapper
<IDirectFBSurface
>
205 wxIDirectFBSurface(IDirectFBSurface
*s
) { Init(s
); }
207 bool GetSize(int *w
, int *h
)
208 { return Check(m_ptr
->GetSize(m_ptr
, w
, h
)); }
210 bool GetCapabilities(DFBSurfaceCapabilities
*caps
)
211 { return Check(m_ptr
->GetCapabilities(m_ptr
, caps
)); }
213 bool GetPixelFormat(DFBSurfacePixelFormat
*caps
)
214 { return Check(m_ptr
->GetPixelFormat(m_ptr
, caps
)); }
216 // convenience version of GetPixelFormat, returns DSPF_UNKNOWN if fails
217 DFBSurfacePixelFormat
GetPixelFormat();
219 bool SetClip(const DFBRegion
*clip
)
220 { return Check(m_ptr
->SetClip(m_ptr
, clip
)); }
222 bool SetColor(u8 r
, u8 g
, u8 b
, u8 a
)
223 { return Check(m_ptr
->SetColor(m_ptr
, r
, g
, b
, a
)); }
225 bool Clear(u8 r
, u8 g
, u8 b
, u8 a
)
226 { return Check(m_ptr
->Clear(m_ptr
, r
, g
, b
, a
)); }
228 bool DrawLine(int x1
, int y1
, int x2
, int y2
)
229 { return Check(m_ptr
->DrawLine(m_ptr
, x1
, y1
, x2
, y2
)); }
231 bool DrawRectangle(int x
, int y
, int w
, int h
)
232 { return Check(m_ptr
->DrawRectangle(m_ptr
, x
, y
, w
, h
)); }
234 bool FillRectangle(int x
, int y
, int w
, int h
)
235 { return Check(m_ptr
->FillRectangle(m_ptr
, x
, y
, w
, h
)); }
237 bool SetFont(const wxIDirectFBFontPtr
& font
)
238 { return Check(m_ptr
->SetFont(m_ptr
, font
->GetRaw())); }
240 bool DrawString(const char *text
, int bytes
, int x
, int y
, int flags
)
242 return Check(m_ptr
->DrawString(m_ptr
, text
, bytes
, x
, y
,
243 (DFBSurfaceTextFlags
)flags
));
247 Updates the front buffer from the back buffer. If @a region is not
248 NULL, only given rectangle is updated.
250 bool FlipToFront(const DFBRegion
*region
= NULL
);
252 wxIDirectFBSurfacePtr
GetSubSurface(const DFBRectangle
*rect
)
255 if ( Check(m_ptr
->GetSubSurface(m_ptr
, rect
, &s
)) )
256 return new wxIDirectFBSurface(s
);
261 wxIDirectFBPalettePtr
GetPalette()
264 if ( Check(m_ptr
->GetPalette(m_ptr
, &s
)) )
265 return new wxIDirectFBPalette(s
);
270 bool SetPalette(const wxIDirectFBPalettePtr
& pal
)
271 { return Check(m_ptr
->SetPalette(m_ptr
, pal
->GetRaw())); }
273 bool SetBlittingFlags(int flags
)
276 m_ptr
->SetBlittingFlags(m_ptr
, (DFBSurfaceBlittingFlags
)flags
));
279 bool Blit(const wxIDirectFBSurfacePtr
& source
,
280 const DFBRectangle
*source_rect
,
282 { return Blit(source
->GetRaw(), source_rect
, x
, y
); }
284 bool Blit(IDirectFBSurface
*source
,
285 const DFBRectangle
*source_rect
,
287 { return Check(m_ptr
->Blit(m_ptr
, source
, source_rect
, x
, y
)); }
289 bool StretchBlit(const wxIDirectFBSurfacePtr
& source
,
290 const DFBRectangle
*source_rect
,
291 const DFBRectangle
*dest_rect
)
293 return Check(m_ptr
->StretchBlit(m_ptr
, source
->GetRaw(),
294 source_rect
, dest_rect
));
297 /// Returns bit depth used by the surface or -1 on error
301 Creates a new surface by cloning this one. New surface will have same
302 capabilities, pixel format and pixel data as the existing one.
304 @see CreateCompatible
306 wxIDirectFBSurfacePtr
Clone();
308 /// Flags for CreateCompatible()
309 enum CreateCompatibleFlags
311 /// Don't create double-buffered surface
312 CreateCompatible_NoBackBuffer
= 1
316 Creates a surface compatible with this one, i.e. surface with the same
317 capabilities and pixel format, but with different and size.
319 @param size Size of the surface to create. If wxDefaultSize, use the
320 size of this surface.
321 @param flags Or-combination of CreateCompatibleFlags values
323 wxIDirectFBSurfacePtr
CreateCompatible(const wxSize
& size
= wxDefaultSize
,
326 bool Lock(DFBSurfaceLockFlags flags
, void **ret_ptr
, int *ret_pitch
)
327 { return Check(m_ptr
->Lock(m_ptr
, flags
, ret_ptr
, ret_pitch
)); }
330 { return Check(m_ptr
->Unlock(m_ptr
)); }
332 /// Helper struct for safe locking & unlocking of surfaces
335 Locked(const wxIDirectFBSurfacePtr
& surface
, DFBSurfaceLockFlags flags
)
338 if ( !surface
->Lock(flags
, &ptr
, &pitch
) )
352 wxIDirectFBSurfacePtr m_surface
;
357 // this is private because we want user code to use FlipToFront()
358 bool Flip(const DFBRegion
*region
, int flags
);
362 //-----------------------------------------------------------------------------
363 // wxIDirectFBEventBuffer
364 //-----------------------------------------------------------------------------
366 struct wxIDirectFBEventBuffer
: public wxDfbWrapper
<IDirectFBEventBuffer
>
368 wxIDirectFBEventBuffer(IDirectFBEventBuffer
*s
) { Init(s
); }
370 bool CreateFileDescriptor(int *ret_fd
)
372 return Check(m_ptr
->CreateFileDescriptor(m_ptr
, ret_fd
));
377 //-----------------------------------------------------------------------------
379 //-----------------------------------------------------------------------------
381 struct wxIDirectFBWindow
: public wxDfbWrapper
<IDirectFBWindow
>
383 wxIDirectFBWindow(IDirectFBWindow
*s
) { Init(s
); }
385 bool GetID(DFBWindowID
*id
)
386 { return Check(m_ptr
->GetID(m_ptr
, id
)); }
388 bool GetPosition(int *x
, int *y
)
389 { return Check(m_ptr
->GetPosition(m_ptr
, x
, y
)); }
391 bool GetSize(int *w
, int *h
)
392 { return Check(m_ptr
->GetSize(m_ptr
, w
, h
)); }
394 bool MoveTo(int x
, int y
)
395 { return Check(m_ptr
->MoveTo(m_ptr
, x
, y
)); }
397 bool Resize(int w
, int h
)
398 { return Check(m_ptr
->Resize(m_ptr
, w
, h
)); }
400 bool SetOpacity(u8 opacity
)
401 { return Check(m_ptr
->SetOpacity(m_ptr
, opacity
)); }
403 bool SetStackingClass(DFBWindowStackingClass klass
)
404 { return Check(m_ptr
->SetStackingClass(m_ptr
, klass
)); }
407 { return Check(m_ptr
->RaiseToTop(m_ptr
)); }
410 { return Check(m_ptr
->LowerToBottom(m_ptr
)); }
412 wxIDirectFBSurfacePtr
GetSurface()
415 if ( Check(m_ptr
->GetSurface(m_ptr
, &s
)) )
416 return new wxIDirectFBSurface(s
);
421 bool AttachEventBuffer(const wxIDirectFBEventBufferPtr
& buffer
)
422 { return Check(m_ptr
->AttachEventBuffer(m_ptr
, buffer
->GetRaw())); }
425 { return Check(m_ptr
->RequestFocus(m_ptr
)); }
428 { return Check(m_ptr
->Destroy(m_ptr
)); }
432 //-----------------------------------------------------------------------------
433 // wxIDirectFBDisplayLayer
434 //-----------------------------------------------------------------------------
436 struct wxIDirectFBDisplayLayer
: public wxDfbWrapper
<IDirectFBDisplayLayer
>
438 wxIDirectFBDisplayLayer(IDirectFBDisplayLayer
*s
) { Init(s
); }
440 wxIDirectFBWindowPtr
CreateWindow(const DFBWindowDescription
*desc
)
443 if ( Check(m_ptr
->CreateWindow(m_ptr
, desc
, &w
)) )
444 return new wxIDirectFBWindow(w
);
449 bool GetConfiguration(DFBDisplayLayerConfig
*config
)
450 { return Check(m_ptr
->GetConfiguration(m_ptr
, config
)); }
452 wxVideoMode
GetVideoMode();
454 bool GetCursorPosition(int *x
, int *y
)
455 { return Check(m_ptr
->GetCursorPosition(m_ptr
, x
, y
)); }
457 bool WarpCursor(int x
, int y
)
458 { return Check(m_ptr
->WarpCursor(m_ptr
, x
, y
)); }
462 //-----------------------------------------------------------------------------
464 //-----------------------------------------------------------------------------
466 struct wxIDirectFB
: public wxDfbWrapper
<IDirectFB
>
469 Returns pointer to DirectFB singleton object, it never returns NULL
470 after wxApp was initialized. The object is cached, so calling this
473 static wxIDirectFBPtr
Get()
475 if ( !ms_ptr
) CreateDirectFB();
479 bool SetVideoMode(int w
, int h
, int bpp
)
480 { return Check(m_ptr
->SetVideoMode(m_ptr
, w
, h
, bpp
)); }
482 wxIDirectFBSurfacePtr
CreateSurface(const DFBSurfaceDescription
*desc
)
485 if ( Check(m_ptr
->CreateSurface(m_ptr
, desc
, &s
)) )
486 return new wxIDirectFBSurface(s
);
491 wxIDirectFBEventBufferPtr
CreateEventBuffer()
493 IDirectFBEventBuffer
*b
;
494 if ( Check(m_ptr
->CreateEventBuffer(m_ptr
, &b
)) )
495 return new wxIDirectFBEventBuffer(b
);
500 wxIDirectFBFontPtr
CreateFont(const char *filename
,
501 const DFBFontDescription
*desc
)
504 if ( Check(m_ptr
->CreateFont(m_ptr
, filename
, desc
, &f
)) )
505 return new wxIDirectFBFont(f
);
510 wxIDirectFBDisplayLayerPtr
511 GetDisplayLayer(DFBDisplayLayerID id
= DLID_PRIMARY
)
513 IDirectFBDisplayLayer
*l
;
514 if ( Check(m_ptr
->GetDisplayLayer(m_ptr
, id
, &l
)) )
515 return new wxIDirectFBDisplayLayer(l
);
520 /// Returns primary surface
521 wxIDirectFBSurfacePtr
GetPrimarySurface();
524 wxIDirectFB(IDirectFB
*ptr
) { Init(ptr
); }
526 // creates ms_ptr instance
527 static void CreateDirectFB();
529 static void CleanUp();
530 friend class wxApp
; // calls CleanUp
532 // pointer to the singleton IDirectFB object
533 static wxIDirectFBPtr ms_ptr
;
536 #endif // _WX_DFB_WRAPDFB_H_