Ginga  0.13.6
 All Classes Namespaces Functions Variables
SDLDeviceScreen.h
1 /******************************************************************************
2 Este arquivo eh parte da implementacao do ambiente declarativo do middleware
3 Ginga (Ginga-NCL).
4 
5 Direitos Autorais Reservados (c) 1989-2007 PUC-Rio/Laboratorio TeleMidia
6 
7 Este programa eh software livre; voce pode redistribui-lo e/ou modificah-lo sob
8 os termos da Licenca Publica Geral GNU versao 2 conforme publicada pela Free
9 Software Foundation.
10 
11 Este programa eh distribuido na expectativa de que seja util, porem, SEM
12 NENHUMA GARANTIA; nem mesmo a garantia implicita de COMERCIABILIDADE OU
13 ADEQUACAO A UMA FINALIDADE ESPECIFICA. Consulte a Licenca Publica Geral do
14 GNU versao 2 para mais detalhes.
15 
16 Voce deve ter recebido uma copia da Licenca Publica Geral do GNU versao 2 junto
17 com este programa; se nao, escreva para a Free Software Foundation, Inc., no
18 endereco 59 Temple Street, Suite 330, Boston, MA 02111-1307 USA.
19 
20 Para maiores informacoes:
21 ncl @ telemidia.puc-rio.br
22 http://www.ncl.org.br
23 http://www.ginga.org.br
24 http://www.telemidia.puc-rio.br
25 ******************************************************************************
26 This file is part of the declarative environment of middleware Ginga (Ginga-NCL)
27 
28 Copyright: 1989-2007 PUC-RIO/LABORATORIO TELEMIDIA, All Rights Reserved.
29 
30 This program is free software; you can redistribute it and/or modify it under
31 the terms of the GNU General Public License version 2 as published by
32 the Free Software Foundation.
33 
34 This program is distributed in the hope that it will be useful, but WITHOUT ANY
35 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
36 PARTICULAR PURPOSE. See the GNU General Public License version 2 for more
37 details.
38 
39 You should have received a copy of the GNU General Public License version 2
40 along with this program; if not, write to the Free Software
41 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
42 
43 For further information contact:
44 ncl @ telemidia.puc-rio.br
45 http://www.ncl.org.br
46 http://www.ginga.org.br
47 http://www.telemidia.puc-rio.br
48 *******************************************************************************/
49 
50 #ifndef SDLDEVICESCREEN_H_
51 #define SDLDEVICESCREEN_H_
52 
53 #include "config.h"
54 
55 #include "util/functions.h"
56 using namespace ::br::pucrio::telemidia::util;
57 
58 #if HAVE_COMPONENTS
59 #include "cm/IComponentManager.h"
60 using namespace ::br::pucrio::telemidia::ginga::core::cm;
61 #endif
62 
63 #include "mb/interface/IDiscreteMediaProvider.h"
64 #include "mb/interface/IDeviceScreen.h"
65 
66 #include "SDL.h"
67 
68 #include <pthread.h>
69 #include <time.h>
70 
71 #include <set>
72 #include <map>
73 #include <iostream>
74 using namespace std;
75 
76 #ifndef GINGA_PIXEL_FMT
77 #define GINGA_PIXEL_FMT SDL_PIXELFORMAT_RGB24
78 #endif
79 
80 namespace br {
81 namespace pucrio {
82 namespace telemidia {
83 namespace ginga {
84 namespace core {
85 namespace mb {
86 
87 typedef struct {
88  IMediaProvider* iDec;
89  SDL_Surface* uSur;
90  SDL_Texture* uTex;
92 
93  class SDLDeviceScreen : public IDeviceScreen {
94  public:
95  static const unsigned int DSA_UNKNOWN;
96  static const unsigned int DSA_4x3;
97  static const unsigned int DSA_16x9;
98 
99  private:
100  /* SDL Pending Tasks*/
101  static const short SPT_NONE = 0;
102  static const short SPT_INIT = 1;
103  static const short SPT_CLEAR = 2;
104  static const short SPT_RELEASE = 3;
105 
106  public:
107  /* SDL Underlying Window Tasks*/
108  static const short SUW_SHOW = 0;
109  static const short SUW_HIDE = 1;
110  static const short SUW_RAISETOTOP = 2;
111  static const short SUW_LOWERTOBOTTOM = 3;
112 
113  static const short SDS_FPS = 35;
114  static const int uSleepTime = (int)(1000000/SDS_FPS);
115 
116  private:
117  static bool hasRenderer;
118  static bool mutexInit;
119  static map<SDLDeviceScreen*, short> sdlScreens;
120 
121  string aSystem;
122  string mbMode;
123  string mbSubSystem;
124  unsigned int aspect;
125  unsigned int hSize;
126  unsigned int vSize;
127  int hRes;
128  int wRes;
129 
130  unsigned long winIdRefCounter;
131  map<GingaWindowID, IWindow*> windowRefs;
132 
133 #if HAVE_COMPONENTS
134  static IComponentManager* cm;
135 #endif
136  set<IWindow*> windowPool;
137  set<ISurface*> surfacePool;
138  set<IContinuousMediaProvider*> cmpPool;
139  set<IDiscreteMediaProvider*> dmpPool;
140 
141  GingaScreenID id;
142  GingaWindowID uParentId;
143  GingaWindowID uEmbedId;
144  bool uEmbedFocused;
145  bool mustGainFocus;
146 
147  IInputManager* im;
148  bool useStdin;
149 
150  bool waitingCreator;
151  pthread_mutex_t condMutex;
152  pthread_cond_t cond;
153 
154  IWindow* backgroundLayer;
155  bool fullScreen;
156  SDL_Window* screen;
157  Uint32 sdlId;
158  SDL_Renderer* renderer;
159 
160  static bool hasERC; //external renderer controller
161 
162  static map<int, int> gingaToSDLCodeMap;
163  static map<int, int> sdlToGingaCodeMap;
164  static map<string, int> sdlStrToSdlCode;
165 
166  static set<SDL_Surface*> uSurPool;
167  static set<SDL_Texture*> uTexPool;
168  static vector<ReleaseContainer*> releaseList;
169  static map<GingaScreenID, map<float, set<IWindow*>*>*> renderMap;
170  static set<IContinuousMediaProvider*> cmpRenderList;
171 
172  static pthread_mutex_t sdlMutex; //mutex for SDL structures
173  static pthread_mutex_t sieMutex; //mutex for SDL input event Map
174  static pthread_mutex_t renMutex; //mutex for C++ STL SDL Render Map
175  static pthread_mutex_t scrMutex; //mutex for C++ STL SDL Screens
176  static pthread_mutex_t recMutex; //mutex for C++ STL release structures
177  static pthread_mutex_t winMutex; //mutex for C++ STL Window
178  static pthread_mutex_t surMutex; //mutex for C++ STL Surface
179  static pthread_mutex_t proMutex; //mutex for C++ STL Providers
180  static pthread_mutex_t cstMutex; //mutex for the others C++ STL structures
181 
182  public:
184  int numArgs, char** args,
185  GingaScreenID myId, GingaWindowID embedId,
186  bool externalRenderer);
187 
188  virtual ~SDLDeviceScreen();
189 
190  private:
191  static void checkMutexInit();
192 
193  public:
194  static void lockSDL();
195  static void unlockSDL();
196 
197  static void updateRenderMap(
198  GingaScreenID screenId, IWindow* window,
199  float oldZIndex, float newZIndex);
200 
201  void releaseScreen();
202 
203  void releaseMB();
204 
205  void clearWidgetPools();
206 
207  string getScreenName();
208 
209  private:
210  void setEmbedFromParent(string parentCoords);
211 
212  public:
213  void setBackgroundImage(string uri);
214 
215  unsigned int getWidthResolution();
216  void setWidthResolution(unsigned int wRes);
217  unsigned int getHeightResolution();
218  void setHeightResolution(unsigned int hRes);
219 
220  void setColorKey(int r, int g, int b);
221 
222  IWindow* getIWindowFromId(GingaWindowID winId);
223  bool mergeIds(GingaWindowID destId, vector<GingaWindowID>* srcIds);
224  void blitScreen(ISurface* destination);
225  void blitScreen(string fileUri);
226 
227  private:
228  void blitScreen(SDL_Surface* destination);
229  void setInitScreenFlag();
230 
231  public:
232  void refreshScreen();
233 
234 
235  /* interfacing output */
236 
237  IWindow* createWindow(int x, int y, int w, int h, float z);
238 
239  GingaWindowID createUnderlyingSubWindow(
240  int x, int y, int w, int h, float z);
241 
242  private:
243  GingaWindowID createUnderlyingSubWindow(
244  GingaWindowID parent,
245  string spec,
246  int x, int y, int w, int h, float z);
247 
248  public:
249  GingaWindowID getScreenUnderlyingWindow();
250 
251  IWindow* createWindowFrom(GingaWindowID underlyingWindow);
252  bool hasWindow(IWindow* win);
253  void releaseWindow(IWindow* win);
254 
255  ISurface* createSurface();
256  ISurface* createSurface(int w, int h);
257  ISurface* createSurfaceFrom(void* underlyingSurface);
258  bool hasSurface(ISurface* sur);
259  bool releaseSurface(ISurface* sur);
260 
261 
262  /* interfacing content */
263 
264  IContinuousMediaProvider* createContinuousMediaProvider(
265  const char* mrl, bool* hasVisual, bool isRemote);
266 
267  void releaseContinuousMediaProvider(
268  IContinuousMediaProvider* provider);
269 
270  IFontProvider* createFontProvider(
271  const char* mrl, int fontSize);
272 
273  void releaseFontProvider(IFontProvider* provider);
274 
275  IImageProvider* createImageProvider(const char* mrl);
276  void releaseImageProvider(IImageProvider* provider);
277 
278  ISurface* createRenderedSurfaceFromImageFile(const char* mrl);
279 
280  static void addCMPToRendererList(IContinuousMediaProvider* cmp);
281  static void removeCMPToRendererList(IContinuousMediaProvider* cmp);
282 
283  static void createReleaseContainer(
284  SDL_Surface* uSur,
285  SDL_Texture* uTex,
286  IMediaProvider* iDec);
287 
288  private:
289  static void checkSDLInit();
290  static void notifyQuit();
291  static void sdlQuit();
292 
293  static void checkWindowFocus(SDLDeviceScreen* s, SDL_Event* event);
294  static bool notifyEvent(
295  SDLDeviceScreen* screen,
296  SDL_Event* event,
297  bool capsOn,
298  bool shiftOn);
299 
300  static void* checkStdin(void* ptr);
301  static void processCmd(
302  SDLDeviceScreen* s,
303  string cmd,
304  string type,
305  string args);
306 
307  static bool checkEvents();
308  static void* rendererT(void* ptr);
309 
310  static void refreshRC(SDLDeviceScreen* screen);
311  static int refreshCMP(SDLDeviceScreen* screen);
312  static void refreshWin(SDLDeviceScreen* screen);
313 
314  static void initEmbed(SDLDeviceScreen* s, GingaWindowID uWin);
315  static void forceInputFocus(
316  SDLDeviceScreen* screen, GingaWindowID uWin);
317 
318  static void initScreen(SDLDeviceScreen* screen);
319  static void clearScreen(SDLDeviceScreen* screen);
320  static void releaseScreen(SDLDeviceScreen* screen);
321 
322  static void releaseAll();
323 
324  static void initCMP(
326 
327  static bool blitFromWindow(IWindow* iWin, SDL_Surface* dest);
328 
329  public:
330 
331  /* interfacing input */
332 
333  IInputManager* getInputManager();
334 
335  IEventBuffer* createEventBuffer();
336 
337  IInputEvent* createInputEvent(void* event, const int symbol);
338  IInputEvent* createApplicationEvent(int type, void* data);
339 
340  int fromMBToGinga(int keyCode);
341  int fromGingaToMB(int keyCode);
342 
343 
344  /* interfacing underlying multimedia system */
345 
346  void* getGfxRoot();
347 
348 
349  /* SDL MB internal use*/
350  private:
351  /* input */
352  static int convertEventCodeStrToInt(string strEvent);
353  static void initCodeMaps();
354  static bool checkEventFocus(SDLDeviceScreen* s);
355 
356  public:
357  /* output */
358  static void renderMapInsertWindow(
359  GingaScreenID screenId, IWindow* iWin, float z);
360 
361  static void renderMapRemoveWindow(
362  GingaScreenID screenId, IWindow* iWin, float z);
363 
364 // static void updateWindowState(
365 // GingaScreenID screenId, IWindow* win, short status);
366 
367  private:
368 // static void updateWindowList(
369 // vector<IWindow*>* windows, IWindow* win, short status);
370 
371  static void removeFromWindowList(
372  vector<IWindow*>* windows, IWindow* win);
373 
374  public:
375  static SDL_Window* getUnderlyingWindow(GingaWindowID winId);
376 
377  private:
378  static bool drawWindow(
379  SDL_Renderer* renderer,
380  SDL_Texture* texture,
381  IWindow* iWin);
382 
383  static void insertWindowFromRenderList(
384  IWindow* win, vector<IWindow*>* windows);
385 
386  static void removeWindowFromRenderList(
387  IWindow* win, vector<IWindow*>* windows);
388 
389  public:
390  /* CAUTION: call this method only from main SDL thread */
391  static SDL_Texture* createTextureFromSurface(
392  SDL_Renderer* renderer, SDL_Surface* surface);
393 
394  private:
395  static SDL_Texture* createTexture(
396  SDL_Renderer* renderer, int w, int h);
397 
398  public:
399  static bool hasTexture(SDL_Texture* uTex);
400  static void releaseTexture(SDL_Texture* uTex);
401 
402  static void addUnderlyingSurface(SDL_Surface* uSur);
403  static SDL_Surface* createUnderlyingSurface(int width, int height);
404 
405  static SDL_Surface* createUnderlyingSurfaceFromTexture(
406  SDL_Texture* texture);
407 
408  static bool hasUnderlyingSurface(SDL_Surface* uSur);
409 
410  private:
411  static void releaseUnderlyingSurface(SDL_Surface* uSur);
412  };
413 }
414 }
415 }
416 }
417 }
418 }
419 
420 #endif /*SDLDEVICESCREEN_H_*/