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