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