]>
Commit | Line | Data |
---|---|---|
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 | #include "wx/gdicmn.h" | |
16 | #include "wx/vidmode.h" | |
17 | ||
18 | #include <directfb.h> | |
19 | ||
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 | ||
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 | // convenience version of GetPixelFormat, returns DSPF_UNKNOWN if fails | |
211 | DFBSurfacePixelFormat GetPixelFormat(); | |
212 | ||
213 | bool SetClip(const DFBRegion *clip) | |
214 | { return Check(m_ptr->SetClip(m_ptr, clip)); } | |
215 | ||
216 | bool SetColor(__u8 r, __u8 g, __u8 b, __u8 a) | |
217 | { return Check(m_ptr->SetColor(m_ptr, r, g, b, a)); } | |
218 | ||
219 | bool Clear(__u8 r, __u8 g, __u8 b, __u8 a) | |
220 | { return Check(m_ptr->Clear(m_ptr, r, g, b, a)); } | |
221 | ||
222 | bool DrawLine(int x1, int y1, int x2, int y2) | |
223 | { return Check(m_ptr->DrawLine(m_ptr, x1, y1, x2, y2)); } | |
224 | ||
225 | bool DrawRectangle(int x, int y, int w, int h) | |
226 | { return Check(m_ptr->DrawRectangle(m_ptr, x, y, w, h)); } | |
227 | ||
228 | bool FillRectangle(int x, int y, int w, int h) | |
229 | { return Check(m_ptr->FillRectangle(m_ptr, x, y, w, h)); } | |
230 | ||
231 | bool SetFont(const wxIDirectFBFontPtr& font) | |
232 | { return Check(m_ptr->SetFont(m_ptr, font->GetRaw())); } | |
233 | ||
234 | bool DrawString(const char *text, int bytes, int x, int y, int flags) | |
235 | { | |
236 | return Check(m_ptr->DrawString(m_ptr, text, bytes, x, y, | |
237 | (DFBSurfaceTextFlags)flags)); | |
238 | } | |
239 | ||
240 | /** | |
241 | Updates the front buffer from the back buffer. If @a region is not | |
242 | NULL, only given rectangle is updated. | |
243 | */ | |
244 | bool FlipToFront(const DFBRegion *region = NULL); | |
245 | ||
246 | wxIDirectFBSurfacePtr GetSubSurface(const DFBRectangle *rect) | |
247 | { | |
248 | IDirectFBSurface *s; | |
249 | if ( Check(m_ptr->GetSubSurface(m_ptr, rect, &s)) ) | |
250 | return new wxIDirectFBSurface(s); | |
251 | else | |
252 | return NULL; | |
253 | } | |
254 | ||
255 | wxIDirectFBPalettePtr GetPalette() | |
256 | { | |
257 | IDirectFBPalette *s; | |
258 | if ( Check(m_ptr->GetPalette(m_ptr, &s)) ) | |
259 | return new wxIDirectFBPalette(s); | |
260 | else | |
261 | return NULL; | |
262 | } | |
263 | ||
264 | bool SetPalette(const wxIDirectFBPalettePtr& pal) | |
265 | { return Check(m_ptr->SetPalette(m_ptr, pal->GetRaw())); } | |
266 | ||
267 | bool SetBlittingFlags(int flags) | |
268 | { | |
269 | return Check( | |
270 | m_ptr->SetBlittingFlags(m_ptr, (DFBSurfaceBlittingFlags)flags)); | |
271 | } | |
272 | ||
273 | bool Blit(const wxIDirectFBSurfacePtr& source, | |
274 | const DFBRectangle *source_rect, | |
275 | int x, int y) | |
276 | { return Blit(source->GetRaw(), source_rect, x, y); } | |
277 | ||
278 | bool Blit(IDirectFBSurface *source, | |
279 | const DFBRectangle *source_rect, | |
280 | int x, int y) | |
281 | { return Check(m_ptr->Blit(m_ptr, source, source_rect, x, y)); } | |
282 | ||
283 | bool StretchBlit(const wxIDirectFBSurfacePtr& source, | |
284 | const DFBRectangle *source_rect, | |
285 | const DFBRectangle *dest_rect) | |
286 | { | |
287 | return Check(m_ptr->StretchBlit(m_ptr, source->GetRaw(), | |
288 | source_rect, dest_rect)); | |
289 | } | |
290 | ||
291 | /// Returns bit depth used by the surface or -1 on error | |
292 | int GetDepth(); | |
293 | ||
294 | /** | |
295 | Creates a new surface by cloning this one. New surface will have same | |
296 | capabilities, pixel format and pixel data as the existing one. | |
297 | ||
298 | @see CreateCompatible | |
299 | */ | |
300 | wxIDirectFBSurfacePtr Clone(); | |
301 | ||
302 | /// Flags for CreateCompatible() | |
303 | enum CreateCompatibleFlags | |
304 | { | |
305 | /// Don't create double-buffered surface | |
306 | CreateCompatible_NoBackBuffer = 1 | |
307 | }; | |
308 | ||
309 | /** | |
310 | Creates a surface compatible with this one, i.e. surface with the same | |
311 | capabilities and pixel format, but with different and size. | |
312 | ||
313 | @param size Size of the surface to create. If wxDefaultSize, use the | |
314 | size of this surface. | |
315 | @param flags Or-combination of CreateCompatibleFlags values | |
316 | */ | |
317 | wxIDirectFBSurfacePtr CreateCompatible(const wxSize& size = wxDefaultSize, | |
318 | int flags = 0); | |
319 | ||
320 | bool Lock(DFBSurfaceLockFlags flags, void **ret_ptr, int *ret_pitch) | |
321 | { return Check(m_ptr->Lock(m_ptr, flags, ret_ptr, ret_pitch)); } | |
322 | ||
323 | bool Unlock() | |
324 | { return Check(m_ptr->Unlock(m_ptr)); } | |
325 | ||
326 | /// Helper struct for safe locking & unlocking of surfaces | |
327 | struct Locked | |
328 | { | |
329 | Locked(const wxIDirectFBSurfacePtr& surface, DFBSurfaceLockFlags flags) | |
330 | : m_surface(surface) | |
331 | { | |
332 | if ( !surface->Lock(flags, &ptr, &pitch) ) | |
333 | ptr = NULL; | |
334 | } | |
335 | ||
336 | ~Locked() | |
337 | { | |
338 | if ( ptr ) | |
339 | m_surface->Unlock(); | |
340 | } | |
341 | ||
342 | void *ptr; | |
343 | int pitch; | |
344 | ||
345 | private: | |
346 | wxIDirectFBSurfacePtr m_surface; | |
347 | }; | |
348 | ||
349 | ||
350 | private: | |
351 | // this is private because we want user code to use FlipToFront() | |
352 | bool Flip(const DFBRegion *region, int flags); | |
353 | }; | |
354 | ||
355 | ||
356 | //----------------------------------------------------------------------------- | |
357 | // wxIDirectFBEventBuffer | |
358 | //----------------------------------------------------------------------------- | |
359 | ||
360 | struct wxIDirectFBEventBuffer : public wxDfbWrapper<IDirectFBEventBuffer> | |
361 | { | |
362 | wxIDirectFBEventBuffer(IDirectFBEventBuffer *s) { Init(s); } | |
363 | ||
364 | bool WakeUp() | |
365 | { | |
366 | return Check(m_ptr->WakeUp(m_ptr)); | |
367 | } | |
368 | ||
369 | bool HasEvent() | |
370 | { | |
371 | // returns DFB_OK if there is >=1 event, DFB_BUFFEREMPTY otherwise | |
372 | DFBResult r = m_ptr->HasEvent(m_ptr); | |
373 | ||
374 | // NB: Check() also returns true for DFB_BUFFEREMPTY, so we can't just | |
375 | // return it's return value: | |
376 | Check(r); | |
377 | return (r == DFB_OK); | |
378 | } | |
379 | ||
380 | bool WaitForEventWithTimeout(unsigned secs, unsigned millisecs) | |
381 | { | |
382 | DFBResult r = m_ptr->WaitForEventWithTimeout(m_ptr, secs, millisecs); | |
383 | ||
384 | // DFB_TIMEOUT is not an error in this function: | |
385 | if ( r == DFB_TIMEOUT ) | |
386 | { | |
387 | m_lastResult = DFB_TIMEOUT; | |
388 | return true; | |
389 | } | |
390 | ||
391 | return Check(r); | |
392 | } | |
393 | ||
394 | bool GetEvent(wxDFBEvent& event) | |
395 | { | |
396 | return Check(m_ptr->GetEvent(m_ptr, &event)); | |
397 | } | |
398 | }; | |
399 | ||
400 | ||
401 | //----------------------------------------------------------------------------- | |
402 | // wxIDirectFBWindow | |
403 | //----------------------------------------------------------------------------- | |
404 | ||
405 | struct wxIDirectFBWindow : public wxDfbWrapper<IDirectFBWindow> | |
406 | { | |
407 | wxIDirectFBWindow(IDirectFBWindow *s) { Init(s); } | |
408 | ||
409 | bool GetID(DFBWindowID *id) | |
410 | { return Check(m_ptr->GetID(m_ptr, id)); } | |
411 | ||
412 | bool GetPosition(int *x, int *y) | |
413 | { return Check(m_ptr->GetPosition(m_ptr, x, y)); } | |
414 | ||
415 | bool GetSize(int *w, int *h) | |
416 | { return Check(m_ptr->GetSize(m_ptr, w, h)); } | |
417 | ||
418 | bool MoveTo(int x, int y) | |
419 | { return Check(m_ptr->MoveTo(m_ptr, x, y)); } | |
420 | ||
421 | bool Resize(int w, int h) | |
422 | { return Check(m_ptr->Resize(m_ptr, w, h)); } | |
423 | ||
424 | bool SetOpacity(__u8 opacity) | |
425 | { return Check(m_ptr->SetOpacity(m_ptr, opacity)); } | |
426 | ||
427 | bool SetStackingClass(DFBWindowStackingClass klass) | |
428 | { return Check(m_ptr->SetStackingClass(m_ptr, klass)); } | |
429 | ||
430 | wxIDirectFBSurfacePtr GetSurface() | |
431 | { | |
432 | IDirectFBSurface *s; | |
433 | if ( Check(m_ptr->GetSurface(m_ptr, &s)) ) | |
434 | return new wxIDirectFBSurface(s); | |
435 | else | |
436 | return NULL; | |
437 | } | |
438 | ||
439 | bool AttachEventBuffer(const wxIDirectFBEventBufferPtr& buffer) | |
440 | { return Check(m_ptr->AttachEventBuffer(m_ptr, buffer->GetRaw())); } | |
441 | ||
442 | bool RequestFocus() | |
443 | { return Check(m_ptr->RequestFocus(m_ptr)); } | |
444 | ||
445 | bool Destroy() | |
446 | { return Check(m_ptr->Destroy(m_ptr)); } | |
447 | }; | |
448 | ||
449 | ||
450 | //----------------------------------------------------------------------------- | |
451 | // wxIDirectFBDisplayLayer | |
452 | //----------------------------------------------------------------------------- | |
453 | ||
454 | struct wxIDirectFBDisplayLayer : public wxDfbWrapper<IDirectFBDisplayLayer> | |
455 | { | |
456 | wxIDirectFBDisplayLayer(IDirectFBDisplayLayer *s) { Init(s); } | |
457 | ||
458 | wxIDirectFBWindowPtr CreateWindow(const DFBWindowDescription *desc) | |
459 | { | |
460 | IDirectFBWindow *w; | |
461 | if ( Check(m_ptr->CreateWindow(m_ptr, desc, &w)) ) | |
462 | return new wxIDirectFBWindow(w); | |
463 | else | |
464 | return NULL; | |
465 | } | |
466 | ||
467 | bool GetConfiguration(DFBDisplayLayerConfig *config) | |
468 | { return Check(m_ptr->GetConfiguration(m_ptr, config)); } | |
469 | ||
470 | wxVideoMode GetVideoMode(); | |
471 | ||
472 | bool GetCursorPosition(int *x, int *y) | |
473 | { return Check(m_ptr->GetCursorPosition(m_ptr, x, y)); } | |
474 | ||
475 | bool WarpCursor(int x, int y) | |
476 | { return Check(m_ptr->WarpCursor(m_ptr, x, y)); } | |
477 | }; | |
478 | ||
479 | ||
480 | //----------------------------------------------------------------------------- | |
481 | // wxIDirectFB | |
482 | //----------------------------------------------------------------------------- | |
483 | ||
484 | struct wxIDirectFB : public wxDfbWrapper<IDirectFB> | |
485 | { | |
486 | /** | |
487 | Returns pointer to DirectFB singleton object, it never returns NULL | |
488 | after wxApp was initialized. The object is cached, so calling this | |
489 | method is cheap. | |
490 | */ | |
491 | static wxIDirectFBPtr Get() | |
492 | { | |
493 | if ( !ms_ptr ) CreateDirectFB(); | |
494 | return ms_ptr; | |
495 | } | |
496 | ||
497 | bool SetVideoMode(int w, int h, int bpp) | |
498 | { return Check(m_ptr->SetVideoMode(m_ptr, w, h, bpp)); } | |
499 | ||
500 | wxIDirectFBSurfacePtr CreateSurface(const DFBSurfaceDescription *desc) | |
501 | { | |
502 | IDirectFBSurface *s; | |
503 | if ( Check(m_ptr->CreateSurface(m_ptr, desc, &s)) ) | |
504 | return new wxIDirectFBSurface(s); | |
505 | else | |
506 | return NULL; | |
507 | } | |
508 | ||
509 | wxIDirectFBEventBufferPtr CreateEventBuffer() | |
510 | { | |
511 | IDirectFBEventBuffer *b; | |
512 | if ( Check(m_ptr->CreateEventBuffer(m_ptr, &b)) ) | |
513 | return new wxIDirectFBEventBuffer(b); | |
514 | else | |
515 | return NULL; | |
516 | } | |
517 | ||
518 | wxIDirectFBFontPtr CreateFont(const char *filename, | |
519 | const DFBFontDescription *desc) | |
520 | { | |
521 | IDirectFBFont *f; | |
522 | if ( Check(m_ptr->CreateFont(m_ptr, filename, desc, &f)) ) | |
523 | return new wxIDirectFBFont(f); | |
524 | else | |
525 | return NULL; | |
526 | } | |
527 | ||
528 | wxIDirectFBDisplayLayerPtr | |
529 | GetDisplayLayer(DFBDisplayLayerID id = DLID_PRIMARY) | |
530 | { | |
531 | IDirectFBDisplayLayer *l; | |
532 | if ( Check(m_ptr->GetDisplayLayer(m_ptr, id, &l)) ) | |
533 | return new wxIDirectFBDisplayLayer(l); | |
534 | else | |
535 | return NULL; | |
536 | } | |
537 | ||
538 | /// Returns primary surface | |
539 | wxIDirectFBSurfacePtr GetPrimarySurface(); | |
540 | ||
541 | private: | |
542 | wxIDirectFB(IDirectFB *ptr) { Init(ptr); } | |
543 | ||
544 | // creates ms_ptr instance | |
545 | static void CreateDirectFB(); | |
546 | ||
547 | static void CleanUp(); | |
548 | friend class wxApp; // calls CleanUp | |
549 | ||
550 | // pointer to the singleton IDirectFB object | |
551 | static wxIDirectFBPtr ms_ptr; | |
552 | }; | |
553 | ||
554 | #endif // _WX_DFB_WRAPDFB_H_ |