wrapdfb.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/dfb/wrapdfb.h
  3. // Purpose: wx wrappers for DirectFB interfaces
  4. // Author: Vaclav Slavik
  5. // Created: 2006-08-23
  6. // Copyright: (c) 2006 REA Elektronik GmbH
  7. // Licence: wxWindows licence
  8. /////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_DFB_WRAPDFB_H_
  10. #define _WX_DFB_WRAPDFB_H_
  11. #include "wx/dfb/dfbptr.h"
  12. #include "wx/gdicmn.h"
  13. #include "wx/vidmode.h"
  14. #include <directfb.h>
  15. #include <directfb_version.h>
  16. // DFB < 1.0 didn't have u8 type, only __u8
  17. #if DIRECTFB_MAJOR_VERSION == 0
  18. typedef __u8 u8;
  19. #endif
  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);
  27. /**
  28. Checks the @a code of a DirectFB call and returns true if it was
  29. successful and false if it failed, logging the errors as appropriate
  30. (asserts for programming errors, wxLogError for runtime failures).
  31. */
  32. bool wxDfbCheckReturn(DFBResult code);
  33. //-----------------------------------------------------------------------------
  34. // wxDfbEvent
  35. //-----------------------------------------------------------------------------
  36. /**
  37. The struct defined by this macro is a thin wrapper around DFB*Event type.
  38. It is needed because DFB*Event are typedefs and so we can't forward declare
  39. them, but we need to pass them to methods declared in public headers where
  40. <directfb.h> cannot be included. So this struct just holds the event value,
  41. it's sole purpose is that it can be forward declared.
  42. */
  43. #define WXDFB_DEFINE_EVENT_WRAPPER(T) \
  44. struct wx##T \
  45. { \
  46. wx##T() {} \
  47. wx##T(const T& event) : m_event(event) {} \
  48. \
  49. operator T&() { return m_event; } \
  50. operator const T&() const { return m_event; } \
  51. T* operator&() { return &m_event; } \
  52. \
  53. DFBEventClass GetClass() const { return m_event.clazz; } \
  54. \
  55. private: \
  56. T m_event; \
  57. };
  58. WXDFB_DEFINE_EVENT_WRAPPER(DFBEvent)
  59. WXDFB_DEFINE_EVENT_WRAPPER(DFBWindowEvent)
  60. //-----------------------------------------------------------------------------
  61. // wxDfbWrapper<T>
  62. //-----------------------------------------------------------------------------
  63. /// Base class for wxDfbWrapper<T>
  64. class wxDfbWrapperBase
  65. {
  66. public:
  67. /// Increases reference count of the object
  68. void AddRef()
  69. {
  70. m_refCnt++;
  71. }
  72. /// Decreases reference count and if it reaches zero, deletes the object
  73. void Release()
  74. {
  75. if ( --m_refCnt == 0 )
  76. delete this;
  77. }
  78. /// Returns result code of the last call
  79. DFBResult GetLastResult() const { return m_lastResult; }
  80. protected:
  81. wxDfbWrapperBase() : m_refCnt(1), m_lastResult(DFB_OK) {}
  82. /// Dtor may only be called from Release()
  83. virtual ~wxDfbWrapperBase() {}
  84. /**
  85. Checks the @a result of a DirectFB call and returns true if it was
  86. successful and false if it failed. Also stores result of the call
  87. so that it can be obtained by calling GetLastResult().
  88. */
  89. bool Check(DFBResult result)
  90. {
  91. m_lastResult = result;
  92. return wxDfbCheckReturn(result);
  93. }
  94. protected:
  95. /// Reference count
  96. unsigned m_refCnt;
  97. /// Result of the last DirectFB call
  98. DFBResult m_lastResult;
  99. };
  100. /**
  101. This template is base class for friendly C++ wrapper around DirectFB
  102. interface T.
  103. The wrapper provides same API as DirectFB, with a few exceptions:
  104. - methods return true/false instead of error code
  105. - methods that return or create another interface return pointer to the
  106. interface (or NULL on failure) instead of storing it in the last
  107. argument
  108. - interface arguments use wxFooPtr type instead of raw DirectFB pointer
  109. - methods taking flags use int type instead of an enum when the flags
  110. can be or-combination of enum elements (this is workaround for
  111. C++-unfriendly DirectFB API)
  112. */
  113. template<typename T>
  114. class wxDfbWrapper : public wxDfbWrapperBase
  115. {
  116. public:
  117. /// "Raw" DirectFB interface type
  118. typedef T DirectFBIface;
  119. /// Returns raw DirectFB pointer
  120. T *GetRaw() const { return m_ptr; }
  121. protected:
  122. /// To be called from ctor. Takes ownership of raw object.
  123. void Init(T *ptr) { m_ptr = ptr; }
  124. /// Dtor may only be used from Release
  125. ~wxDfbWrapper()
  126. {
  127. if ( m_ptr )
  128. m_ptr->Release(m_ptr);
  129. }
  130. protected:
  131. // pointer to DirectFB object
  132. T *m_ptr;
  133. };
  134. //-----------------------------------------------------------------------------
  135. // wxIDirectFBFont
  136. //-----------------------------------------------------------------------------
  137. struct wxIDirectFBFont : public wxDfbWrapper<IDirectFBFont>
  138. {
  139. wxIDirectFBFont(IDirectFBFont *s) { Init(s); }
  140. bool GetStringWidth(const char *text, int bytes, int *w)
  141. { return Check(m_ptr->GetStringWidth(m_ptr, text, bytes, w)); }
  142. bool GetStringExtents(const char *text, int bytes,
  143. DFBRectangle *logicalRect, DFBRectangle *inkRect)
  144. {
  145. return Check(m_ptr->GetStringExtents(m_ptr, text, bytes,
  146. logicalRect, inkRect));
  147. }
  148. bool GetHeight(int *h)
  149. { return Check(m_ptr->GetHeight(m_ptr, h)); }
  150. bool GetDescender(int *descender)
  151. { return Check(m_ptr->GetDescender(m_ptr, descender)); }
  152. };
  153. //-----------------------------------------------------------------------------
  154. // wxIDirectFBPalette
  155. //-----------------------------------------------------------------------------
  156. struct wxIDirectFBPalette : public wxDfbWrapper<IDirectFBPalette>
  157. {
  158. wxIDirectFBPalette(IDirectFBPalette *s) { Init(s); }
  159. };
  160. //-----------------------------------------------------------------------------
  161. // wxIDirectFBSurface
  162. //-----------------------------------------------------------------------------
  163. struct wxIDirectFBSurface : public wxDfbWrapper<IDirectFBSurface>
  164. {
  165. wxIDirectFBSurface(IDirectFBSurface *s) { Init(s); }
  166. bool GetSize(int *w, int *h)
  167. { return Check(m_ptr->GetSize(m_ptr, w, h)); }
  168. bool GetCapabilities(DFBSurfaceCapabilities *caps)
  169. { return Check(m_ptr->GetCapabilities(m_ptr, caps)); }
  170. bool GetPixelFormat(DFBSurfacePixelFormat *caps)
  171. { return Check(m_ptr->GetPixelFormat(m_ptr, caps)); }
  172. // convenience version of GetPixelFormat, returns DSPF_UNKNOWN if fails
  173. DFBSurfacePixelFormat GetPixelFormat();
  174. bool SetClip(const DFBRegion *clip)
  175. { return Check(m_ptr->SetClip(m_ptr, clip)); }
  176. bool SetColor(u8 r, u8 g, u8 b, u8 a)
  177. { return Check(m_ptr->SetColor(m_ptr, r, g, b, a)); }
  178. bool Clear(u8 r, u8 g, u8 b, u8 a)
  179. { return Check(m_ptr->Clear(m_ptr, r, g, b, a)); }
  180. bool DrawLine(int x1, int y1, int x2, int y2)
  181. { return Check(m_ptr->DrawLine(m_ptr, x1, y1, x2, y2)); }
  182. bool DrawRectangle(int x, int y, int w, int h)
  183. { return Check(m_ptr->DrawRectangle(m_ptr, x, y, w, h)); }
  184. bool FillRectangle(int x, int y, int w, int h)
  185. { return Check(m_ptr->FillRectangle(m_ptr, x, y, w, h)); }
  186. bool SetFont(const wxIDirectFBFontPtr& font)
  187. { return Check(m_ptr->SetFont(m_ptr, font->GetRaw())); }
  188. bool DrawString(const char *text, int bytes, int x, int y, int flags)
  189. {
  190. return Check(m_ptr->DrawString(m_ptr, text, bytes, x, y,
  191. (DFBSurfaceTextFlags)flags));
  192. }
  193. /**
  194. Updates the front buffer from the back buffer. If @a region is not
  195. NULL, only given rectangle is updated.
  196. */
  197. bool FlipToFront(const DFBRegion *region = NULL);
  198. wxIDirectFBSurfacePtr GetSubSurface(const DFBRectangle *rect)
  199. {
  200. IDirectFBSurface *s;
  201. if ( Check(m_ptr->GetSubSurface(m_ptr, rect, &s)) )
  202. return new wxIDirectFBSurface(s);
  203. else
  204. return NULL;
  205. }
  206. wxIDirectFBPalettePtr GetPalette()
  207. {
  208. IDirectFBPalette *s;
  209. if ( Check(m_ptr->GetPalette(m_ptr, &s)) )
  210. return new wxIDirectFBPalette(s);
  211. else
  212. return NULL;
  213. }
  214. bool SetPalette(const wxIDirectFBPalettePtr& pal)
  215. { return Check(m_ptr->SetPalette(m_ptr, pal->GetRaw())); }
  216. bool SetBlittingFlags(int flags)
  217. {
  218. return Check(
  219. m_ptr->SetBlittingFlags(m_ptr, (DFBSurfaceBlittingFlags)flags));
  220. }
  221. bool Blit(const wxIDirectFBSurfacePtr& source,
  222. const DFBRectangle *source_rect,
  223. int x, int y)
  224. { return Blit(source->GetRaw(), source_rect, x, y); }
  225. bool Blit(IDirectFBSurface *source,
  226. const DFBRectangle *source_rect,
  227. int x, int y)
  228. { return Check(m_ptr->Blit(m_ptr, source, source_rect, x, y)); }
  229. bool StretchBlit(const wxIDirectFBSurfacePtr& source,
  230. const DFBRectangle *source_rect,
  231. const DFBRectangle *dest_rect)
  232. {
  233. return Check(m_ptr->StretchBlit(m_ptr, source->GetRaw(),
  234. source_rect, dest_rect));
  235. }
  236. /// Returns bit depth used by the surface or -1 on error
  237. int GetDepth();
  238. /**
  239. Creates a new surface by cloning this one. New surface will have same
  240. capabilities, pixel format and pixel data as the existing one.
  241. @see CreateCompatible
  242. */
  243. wxIDirectFBSurfacePtr Clone();
  244. /// Flags for CreateCompatible()
  245. enum CreateCompatibleFlags
  246. {
  247. /// Don't create double-buffered surface
  248. CreateCompatible_NoBackBuffer = 1
  249. };
  250. /**
  251. Creates a surface compatible with this one, i.e. surface with the same
  252. capabilities and pixel format, but with different and size.
  253. @param size Size of the surface to create. If wxDefaultSize, use the
  254. size of this surface.
  255. @param flags Or-combination of CreateCompatibleFlags values
  256. */
  257. wxIDirectFBSurfacePtr CreateCompatible(const wxSize& size = wxDefaultSize,
  258. int flags = 0);
  259. bool Lock(DFBSurfaceLockFlags flags, void **ret_ptr, int *ret_pitch)
  260. { return Check(m_ptr->Lock(m_ptr, flags, ret_ptr, ret_pitch)); }
  261. bool Unlock()
  262. { return Check(m_ptr->Unlock(m_ptr)); }
  263. /// Helper struct for safe locking & unlocking of surfaces
  264. struct Locked
  265. {
  266. Locked(const wxIDirectFBSurfacePtr& surface, DFBSurfaceLockFlags flags)
  267. : m_surface(surface)
  268. {
  269. if ( !surface->Lock(flags, &ptr, &pitch) )
  270. ptr = NULL;
  271. }
  272. ~Locked()
  273. {
  274. if ( ptr )
  275. m_surface->Unlock();
  276. }
  277. void *ptr;
  278. int pitch;
  279. private:
  280. wxIDirectFBSurfacePtr m_surface;
  281. };
  282. private:
  283. // this is private because we want user code to use FlipToFront()
  284. bool Flip(const DFBRegion *region, int flags);
  285. };
  286. //-----------------------------------------------------------------------------
  287. // wxIDirectFBEventBuffer
  288. //-----------------------------------------------------------------------------
  289. struct wxIDirectFBEventBuffer : public wxDfbWrapper<IDirectFBEventBuffer>
  290. {
  291. wxIDirectFBEventBuffer(IDirectFBEventBuffer *s) { Init(s); }
  292. bool CreateFileDescriptor(int *ret_fd)
  293. {
  294. return Check(m_ptr->CreateFileDescriptor(m_ptr, ret_fd));
  295. }
  296. };
  297. //-----------------------------------------------------------------------------
  298. // wxIDirectFBWindow
  299. //-----------------------------------------------------------------------------
  300. struct wxIDirectFBWindow : public wxDfbWrapper<IDirectFBWindow>
  301. {
  302. wxIDirectFBWindow(IDirectFBWindow *s) { Init(s); }
  303. bool GetID(DFBWindowID *id)
  304. { return Check(m_ptr->GetID(m_ptr, id)); }
  305. bool GetPosition(int *x, int *y)
  306. { return Check(m_ptr->GetPosition(m_ptr, x, y)); }
  307. bool GetSize(int *w, int *h)
  308. { return Check(m_ptr->GetSize(m_ptr, w, h)); }
  309. bool MoveTo(int x, int y)
  310. { return Check(m_ptr->MoveTo(m_ptr, x, y)); }
  311. bool Resize(int w, int h)
  312. { return Check(m_ptr->Resize(m_ptr, w, h)); }
  313. bool SetOpacity(u8 opacity)
  314. { return Check(m_ptr->SetOpacity(m_ptr, opacity)); }
  315. bool SetStackingClass(DFBWindowStackingClass klass)
  316. { return Check(m_ptr->SetStackingClass(m_ptr, klass)); }
  317. bool RaiseToTop()
  318. { return Check(m_ptr->RaiseToTop(m_ptr)); }
  319. bool LowerToBottom()
  320. { return Check(m_ptr->LowerToBottom(m_ptr)); }
  321. wxIDirectFBSurfacePtr GetSurface()
  322. {
  323. IDirectFBSurface *s;
  324. if ( Check(m_ptr->GetSurface(m_ptr, &s)) )
  325. return new wxIDirectFBSurface(s);
  326. else
  327. return NULL;
  328. }
  329. bool AttachEventBuffer(const wxIDirectFBEventBufferPtr& buffer)
  330. { return Check(m_ptr->AttachEventBuffer(m_ptr, buffer->GetRaw())); }
  331. bool RequestFocus()
  332. { return Check(m_ptr->RequestFocus(m_ptr)); }
  333. bool Destroy()
  334. { return Check(m_ptr->Destroy(m_ptr)); }
  335. };
  336. //-----------------------------------------------------------------------------
  337. // wxIDirectFBDisplayLayer
  338. //-----------------------------------------------------------------------------
  339. struct wxIDirectFBDisplayLayer : public wxDfbWrapper<IDirectFBDisplayLayer>
  340. {
  341. wxIDirectFBDisplayLayer(IDirectFBDisplayLayer *s) { Init(s); }
  342. wxIDirectFBWindowPtr CreateWindow(const DFBWindowDescription *desc)
  343. {
  344. IDirectFBWindow *w;
  345. if ( Check(m_ptr->CreateWindow(m_ptr, desc, &w)) )
  346. return new wxIDirectFBWindow(w);
  347. else
  348. return NULL;
  349. }
  350. bool GetConfiguration(DFBDisplayLayerConfig *config)
  351. { return Check(m_ptr->GetConfiguration(m_ptr, config)); }
  352. wxVideoMode GetVideoMode();
  353. bool GetCursorPosition(int *x, int *y)
  354. { return Check(m_ptr->GetCursorPosition(m_ptr, x, y)); }
  355. bool WarpCursor(int x, int y)
  356. { return Check(m_ptr->WarpCursor(m_ptr, x, y)); }
  357. };
  358. //-----------------------------------------------------------------------------
  359. // wxIDirectFB
  360. //-----------------------------------------------------------------------------
  361. struct wxIDirectFB : public wxDfbWrapper<IDirectFB>
  362. {
  363. /**
  364. Returns pointer to DirectFB singleton object, it never returns NULL
  365. after wxApp was initialized. The object is cached, so calling this
  366. method is cheap.
  367. */
  368. static wxIDirectFBPtr Get()
  369. {
  370. if ( !ms_ptr ) CreateDirectFB();
  371. return ms_ptr;
  372. }
  373. bool SetVideoMode(int w, int h, int bpp)
  374. { return Check(m_ptr->SetVideoMode(m_ptr, w, h, bpp)); }
  375. wxIDirectFBSurfacePtr CreateSurface(const DFBSurfaceDescription *desc)
  376. {
  377. IDirectFBSurface *s;
  378. if ( Check(m_ptr->CreateSurface(m_ptr, desc, &s)) )
  379. return new wxIDirectFBSurface(s);
  380. else
  381. return NULL;
  382. }
  383. wxIDirectFBEventBufferPtr CreateEventBuffer()
  384. {
  385. IDirectFBEventBuffer *b;
  386. if ( Check(m_ptr->CreateEventBuffer(m_ptr, &b)) )
  387. return new wxIDirectFBEventBuffer(b);
  388. else
  389. return NULL;
  390. }
  391. wxIDirectFBFontPtr CreateFont(const char *filename,
  392. const DFBFontDescription *desc)
  393. {
  394. IDirectFBFont *f;
  395. if ( Check(m_ptr->CreateFont(m_ptr, filename, desc, &f)) )
  396. return new wxIDirectFBFont(f);
  397. else
  398. return NULL;
  399. }
  400. wxIDirectFBDisplayLayerPtr
  401. GetDisplayLayer(DFBDisplayLayerID id = DLID_PRIMARY)
  402. {
  403. IDirectFBDisplayLayer *l;
  404. if ( Check(m_ptr->GetDisplayLayer(m_ptr, id, &l)) )
  405. return new wxIDirectFBDisplayLayer(l);
  406. else
  407. return NULL;
  408. }
  409. /// Returns primary surface
  410. wxIDirectFBSurfacePtr GetPrimarySurface();
  411. private:
  412. wxIDirectFB(IDirectFB *ptr) { Init(ptr); }
  413. // creates ms_ptr instance
  414. static void CreateDirectFB();
  415. static void CleanUp();
  416. friend class wxApp; // calls CleanUp
  417. // pointer to the singleton IDirectFB object
  418. static wxIDirectFBPtr ms_ptr;
  419. };
  420. #endif // _WX_DFB_WRAPDFB_H_