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
); } 
 371     bool CreateFileDescriptor(int *ret_fd
) 
 373         return Check(m_ptr
->CreateFileDescriptor(m_ptr
, ret_fd
)); 
 378 //----------------------------------------------------------------------------- 
 380 //----------------------------------------------------------------------------- 
 382 struct wxIDirectFBWindow 
: public wxDfbWrapper
<IDirectFBWindow
> 
 384     wxIDirectFBWindow(IDirectFBWindow 
*s
) { Init(s
); } 
 386     bool GetID(DFBWindowID 
*id
) 
 387         { return Check(m_ptr
->GetID(m_ptr
, id
)); } 
 389     bool GetPosition(int *x
, int *y
) 
 390         { return Check(m_ptr
->GetPosition(m_ptr
, x
, y
)); } 
 392     bool GetSize(int *w
, int *h
) 
 393         { return Check(m_ptr
->GetSize(m_ptr
, w
, h
)); } 
 395     bool MoveTo(int x
, int y
) 
 396         { return Check(m_ptr
->MoveTo(m_ptr
, x
, y
)); } 
 398     bool Resize(int w
, int h
) 
 399         { return Check(m_ptr
->Resize(m_ptr
, w
, h
)); } 
 401     bool SetOpacity(u8 opacity
) 
 402         { return Check(m_ptr
->SetOpacity(m_ptr
, opacity
)); } 
 404     bool SetStackingClass(DFBWindowStackingClass klass
) 
 405         { return Check(m_ptr
->SetStackingClass(m_ptr
, klass
)); } 
 408         { return Check(m_ptr
->RaiseToTop(m_ptr
)); } 
 411         { return Check(m_ptr
->LowerToBottom(m_ptr
)); } 
 413     wxIDirectFBSurfacePtr 
GetSurface() 
 416         if ( Check(m_ptr
->GetSurface(m_ptr
, &s
)) ) 
 417             return new wxIDirectFBSurface(s
); 
 422     bool AttachEventBuffer(const wxIDirectFBEventBufferPtr
& buffer
) 
 423         { return Check(m_ptr
->AttachEventBuffer(m_ptr
, buffer
->GetRaw())); } 
 426         { return Check(m_ptr
->RequestFocus(m_ptr
)); } 
 429         { return Check(m_ptr
->Destroy(m_ptr
)); } 
 433 //----------------------------------------------------------------------------- 
 434 // wxIDirectFBDisplayLayer 
 435 //----------------------------------------------------------------------------- 
 437 struct wxIDirectFBDisplayLayer 
: public wxDfbWrapper
<IDirectFBDisplayLayer
> 
 439     wxIDirectFBDisplayLayer(IDirectFBDisplayLayer 
*s
) { Init(s
); } 
 441     wxIDirectFBWindowPtr 
CreateWindow(const DFBWindowDescription 
*desc
) 
 444         if ( Check(m_ptr
->CreateWindow(m_ptr
, desc
, &w
)) ) 
 445             return new wxIDirectFBWindow(w
); 
 450     bool GetConfiguration(DFBDisplayLayerConfig 
*config
) 
 451         { return Check(m_ptr
->GetConfiguration(m_ptr
, config
)); } 
 453     wxVideoMode 
GetVideoMode(); 
 455     bool GetCursorPosition(int *x
, int *y
) 
 456         { return Check(m_ptr
->GetCursorPosition(m_ptr
, x
, y
)); } 
 458     bool WarpCursor(int x
, int y
) 
 459         { return Check(m_ptr
->WarpCursor(m_ptr
, x
, y
)); } 
 463 //----------------------------------------------------------------------------- 
 465 //----------------------------------------------------------------------------- 
 467 struct wxIDirectFB 
: public wxDfbWrapper
<IDirectFB
> 
 470         Returns pointer to DirectFB singleton object, it never returns NULL 
 471         after wxApp was initialized. The object is cached, so calling this 
 474     static wxIDirectFBPtr 
Get() 
 476         if ( !ms_ptr 
) CreateDirectFB(); 
 480     bool SetVideoMode(int w
, int h
, int bpp
) 
 481         { return Check(m_ptr
->SetVideoMode(m_ptr
, w
, h
, bpp
)); } 
 483     wxIDirectFBSurfacePtr 
CreateSurface(const DFBSurfaceDescription 
*desc
) 
 486         if ( Check(m_ptr
->CreateSurface(m_ptr
, desc
, &s
)) ) 
 487             return new wxIDirectFBSurface(s
); 
 492     wxIDirectFBEventBufferPtr 
CreateEventBuffer() 
 494         IDirectFBEventBuffer 
*b
; 
 495         if ( Check(m_ptr
->CreateEventBuffer(m_ptr
, &b
)) ) 
 496             return new wxIDirectFBEventBuffer(b
); 
 501     wxIDirectFBFontPtr 
CreateFont(const char *filename
, 
 502                                   const DFBFontDescription 
*desc
) 
 505         if ( Check(m_ptr
->CreateFont(m_ptr
, filename
, desc
, &f
)) ) 
 506             return new wxIDirectFBFont(f
); 
 511     wxIDirectFBDisplayLayerPtr
 
 512     GetDisplayLayer(DFBDisplayLayerID id 
= DLID_PRIMARY
) 
 514         IDirectFBDisplayLayer 
*l
; 
 515         if ( Check(m_ptr
->GetDisplayLayer(m_ptr
, id
, &l
)) ) 
 516             return new wxIDirectFBDisplayLayer(l
); 
 521     /// Returns primary surface 
 522     wxIDirectFBSurfacePtr 
GetPrimarySurface(); 
 525     wxIDirectFB(IDirectFB 
*ptr
) { Init(ptr
); } 
 527     // creates ms_ptr instance 
 528     static void CreateDirectFB(); 
 530     static void CleanUp(); 
 531     friend class wxApp
; // calls CleanUp 
 533     // pointer to the singleton IDirectFB object 
 534     static wxIDirectFBPtr ms_ptr
; 
 537 #endif // _WX_DFB_WRAPDFB_H_