summaryrefslogtreecommitdiff
path: root/src/player/Player.h
blob: 32d0da5a4fe83c6f6f50e2aed2b8d55fed5cb936 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//
//  libavg - Media Playback Engine. 
//  Copyright (C) 2003-2014 Ulrich von Zadow
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de
//

#ifndef _Player_H_
#define _Player_H_

#include "../api.h"
#include "Publisher.h"
#include "Timeout.h"
#include "TypeRegistry.h"
#include "DisplayParams.h"
#include "CursorState.h"
#include "TestHelper.h"
#include "BoostPython.h"

#include "../audio/AudioParams.h"
#include "../graphics/GLConfig.h"

#include <libxml/parser.h>
#include <boost/shared_ptr.hpp>
#include <boost/thread/thread.hpp>

#include <string>
#include <vector>
#include <set>

namespace avg {

class AudioEngine;
class Node;
class Canvas;
class MainCanvas;
class OffscreenCanvas;
class TrackerInputDevice;
class MultitouchInputDevice;
class IFrameEndListener;
class IPlaybackEndListener;
class IPreRenderListener;
class Contact;
class EventDispatcher;
class MouseEvent;
class CursorEvent;
class SDLDisplayEngine;
class Display;

typedef boost::shared_ptr<Node> NodePtr;
typedef boost::weak_ptr<Node> NodeWeakPtr;
typedef boost::shared_ptr<Canvas> CanvasPtr;
typedef boost::shared_ptr<MainCanvas> MainCanvasPtr;
typedef boost::shared_ptr<OffscreenCanvas> OffscreenCanvasPtr;
typedef boost::shared_ptr<class Contact> ContactPtr;
typedef boost::shared_ptr<EventDispatcher> EventDispatcherPtr;
typedef boost::shared_ptr<MouseEvent> MouseEventPtr;
typedef boost::shared_ptr<CursorEvent> CursorEventPtr;
typedef boost::shared_ptr<SDLDisplayEngine> SDLDisplayEnginePtr;
typedef boost::shared_ptr<Display> DisplayPtr;

class AVG_API Player: public Publisher
{
    public:
        Player();
        virtual ~Player();
        static Player* get();
        static bool exists();

        void setResolution(bool bFullscreen,
                int width=0, int height=0, int bpp=0);
        bool isFullscreen();
        void setWindowFrame(bool bHasWindowFrame);
        void setWindowPos(int x=0, int y=0);
        void setWindowTitle(const std::string& sTitle);
        void useGLES(bool bGLES);
        void setOGLOptions(bool bUsePOTTextures, bool bUsePixelBuffers, 
                int multiSampleSamples, GLConfig::ShaderUsage shaderUsage,
                bool bUseDebugContext);
        void setMultiSampleSamples(int multiSampleSamples);
        void setAudioOptions(int samplerate, int channels);
        void enableGLErrorChecks(bool bEnable);
        glm::vec2 getScreenResolution();
        float getPixelsPerMM();
        glm::vec2 getPhysicalScreenDimensions();
        void assumePixelsPerMM(float ppmm);

        CanvasPtr loadFile(const std::string& sFilename);
        CanvasPtr loadString(const std::string& sAVG);

        OffscreenCanvasPtr loadCanvasFile(const std::string& sFilename);
        OffscreenCanvasPtr loadCanvasString(const std::string& sAVG);
        CanvasPtr createMainCanvas(const py::dict& params);
        OffscreenCanvasPtr createCanvas(const py::dict& params);
        void deleteCanvas(const std::string& sID);
        CanvasPtr getMainCanvas() const;
        OffscreenCanvasPtr getCanvas(const std::string& sID) const;
        void newCanvasDependency();

        void play();
        void stop();
        bool isStopping();
        void initPlayback(const std::string& sShaderPath = "");
        void cleanup(bool bIsAbort);
        bool isPlaying();
        void setFramerate(float rate);
        void setVBlankFramerate(int rate);
        float getEffectiveFramerate();
        TestHelper * getTestHelper();
        void setFakeFPS(float fps);
        long long getFrameTime();
        float getFrameDuration();

        NodePtr createNode(const std::string& sType, const py::dict& PyDict,
                const py::object& self=py::object());
        NodePtr createNodeFromXmlString(const std::string& sXML);
        
        int setInterval(int time, PyObject * pyfunc);
        int setTimeout(int time, PyObject * pyfunc);
        int setOnFrameHandler(PyObject * pyfunc);
        bool clearInterval(int id);
        void callFromThread(PyObject * pyfunc);

        void addInputDevice(IInputDevicePtr pSource);
        MouseEventPtr getMouseState() const;
        EventPtr getCurrentEvent() const;
        TrackerInputDevice * getTracker();
        void enableMultitouch();
        void enableMouse(bool enabled);
        bool isMultitouchAvailable() const;
        void setEventCapture(NodePtr pNode, int cursorID);
        void releaseEventCapture(int cursorID);
        bool isCaptured(int cursorID);
        void removeDeadEventCaptures();
        EventPtr getCurEvent() const;
        void setMousePos(const IntPoint& pos);
        int getKeyModifierState() const;

        BitmapPtr screenshot();
        void setCursor(const Bitmap* pBmp, IntPoint hotSpot);
        void showCursor(bool bShow);
        bool isCursorShown();

        NodePtr getElementByID(const std::string& id);
        AVGNodePtr getRootNode();
        void doFrame(bool bFirstFrame);
        float getFramerate();
        float getVideoRefreshRate();
        size_t getVideoMemInstalled();
        size_t getVideoMemUsed();
        void setGamma(float red, float green, float blue);
        SDLDisplayEngine * getDisplayEngine() const;
        void keepWindowOpen();
        void setStopOnEscape(bool bStop);
        bool getStopOnEscape() const;
        void setVolume(float volume);
        float getVolume() const;
        std::string getConfigOption(const std::string& sSubsys, const std::string& sName)
                const;
        bool isUsingGLES() const;
        bool areFullShadersSupported() const;

        OffscreenCanvasPtr getCanvasFromURL(const std::string& sURL);

        std::string getCurDirName();
        std::string getRootMediaDir();

        void disablePython();
        void startTraversingTree();
        void endTraversingTree();
        bool isTraversingTree() const;

        py::object loadPlugin(const std::string& name);
        void setPluginPath(const std::string& newPath);
        std::string getPluginPath() const;
        
        void setEventHook(PyObject * pyfunc);
        PyObject * getEventHook() const;

        void registerFrameEndListener(IFrameEndListener* pListener);
        void unregisterFrameEndListener(IFrameEndListener* pListener);
        void registerPlaybackEndListener(IPlaybackEndListener* pListener);
        void unregisterPlaybackEndListener(IPlaybackEndListener* pListener);
        void registerPreRenderListener(IPreRenderListener* pListener);
        void unregisterPreRenderListener(IPreRenderListener* pListener);

        bool handleEvent(EventPtr pEvent);

    private:
        void initConfig();
        void initGraphics(const std::string& sShaderPath);
        void initAudio();
        void initMainCanvas(NodePtr pRootNode);

        NodePtr loadMainNodeFromFile(const std::string& sFilename);
        NodePtr loadMainNodeFromString(const std::string& sAVG);
        NodePtr internalLoad(const std::string& sAVG, const std::string& sFilename);
        SDLDisplayEnginePtr safeGetDisplayEngine();

        NodePtr createNodeFromXml(const xmlDocPtr xmlDoc,
                const xmlNodePtr xmlNode);
        OffscreenCanvasPtr registerOffscreenCanvas(NodePtr pNode);
        OffscreenCanvasPtr findCanvas(const std::string& sID) const;
        void endFrame();

        void sendFakeEvents();
        void sendOver(CursorEventPtr pOtherEvent, Event::Type type, NodePtr pNode);
        void handleCursorEvent(CursorEventPtr pEvent, bool bOnlyCheckCursorOver=false);

        void dispatchOffscreenRendering(OffscreenCanvas* pOffscreenCanvas);

        void errorIfPlaying(const std::string& sFunc) const;

        MainCanvasPtr m_pMainCanvas;

        SDLDisplayEnginePtr m_pDisplayEngine;
        bool m_bDisplayEngineBroken;
        TestHelperPtr m_pTestHelper;
       
        std::string m_CurDirName;
        bool m_bIsTraversingTree;
        bool m_bStopping;

        IInputDevicePtr m_pMultitouchInputDevice;

        // Timeout handling
        int internalSetTimeout(int time, PyObject * pyfunc, bool bIsInterval);
        int addTimeout(Timeout* pTimeout);
        void handleTimers();
        bool m_bInHandleTimers;
        bool m_bCurrentTimeoutDeleted;

        std::vector<Timeout *> m_PendingTimeouts;
        std::vector<Timeout *> m_NewTimeouts; // Timeouts to be added this frame.
        std::vector<Timeout *> m_AsyncCalls;
        boost::mutex m_AsyncCallMutex;

        // Configuration variables.
        DisplayParams m_DP;
        AudioParams m_AP;
        GLConfig m_GLConfig;

        bool m_bKeepWindowOpen;
        bool m_bStopOnEscape;
        bool m_bIsPlaying;

        // Time calculation
        bool m_bFakeFPS;
        float m_FakeFPS;
        long long m_FrameTime;
        long long m_PlayStartTime;
        long long m_NumFrames;

        float m_Volume;

        bool m_bPythonAvailable;

        std::vector<OffscreenCanvasPtr> m_pCanvases;

        static Player * s_pPlayer;
        friend void deletePlayer();
        
        EventDispatcherPtr m_pEventDispatcher;
        struct EventCaptureInfo {
            EventCaptureInfo(const NodeWeakPtr& pNode);

            NodePtr m_pNode;
            int m_CaptureCount;
        };
        typedef boost::shared_ptr<EventCaptureInfo> EventCaptureInfoPtr;
        
        std::map<int, EventCaptureInfoPtr> m_EventCaptureInfoMap;

        MouseEventPtr m_pLastMouseEvent;
        EventPtr m_pCurrentEvent;

        // The indexes of this map are cursorids.
        std::map<int, CursorStatePtr> m_pLastCursorStates;

        PyObject * m_EventHookPyFunc;
        bool m_bMouseEnabled;
};

}
#endif