Gnash  0.8.11dev
movie_root.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 
19 
62 
63 
64 #ifndef GNASH_MOVIE_ROOT_H
65 #define GNASH_MOVIE_ROOT_H
66 
67 #ifdef HAVE_CONFIG_H
68 #include "gnashconfig.h" //USE_SWFTREE
69 #endif
70 
71 #include <map>
72 #include <string>
73 #include <vector>
74 #include <forward_list>
75 #include <set>
76 #include <bitset>
77 #include <array>
78 #include <boost/ptr_container/ptr_deque.hpp>
79 #include <boost/noncopyable.hpp>
80 #include <boost/any.hpp>
81 #include <boost/optional.hpp>
82 
83 #include "dsodefs.h" // DSOEXPORT
84 #include "DragState.h"
85 #include "MouseButtonState.h" // for composition
86 #include "GnashKey.h" // key::code
87 #include "GnashEnums.h"
88 #include "MovieClip.h"
89 #include "SimpleBuffer.h" // for LoadCallback
90 #include "MovieLoader.h"
91 #include "ExternalInterface.h"
92 #include "GC.h"
93 #include "VM.h"
94 #include "HostInterface.h"
95 #include "log.h"
96 #include "IOChannel.h"
97 
98 #ifdef USE_SWFTREE
99 # include "tree.hh"
100 #endif
101 
102 // GNASH_PARANOIA_LEVEL:
103 // 0 : (not unimplemented)
104 // 1 : quick assertions
105 // 2 : add testInvariant
106 //
107 #ifndef GNASH_PARANOIA_LEVEL
108 # define GNASH_PARANOIA_LEVEL 1
109 #endif
110 
111 // Forward declarations
112 namespace gnash {
113  class ExecutableCode;
114  class URL;
115  class Timer;
116  class MovieClip;
117  class VirtualClock;
118  class RunResources;
119  class Button;
120  class VM;
121  class Movie;
122 }
123 
124 namespace gnash {
125 
127 {
129  bool operator()(const LevelMovie& d1, const LevelMovie& d2) const {
130  return d1->get_depth() < d2->get_depth();
131  }
132 };
133 
135 //
140 //
143 //
148 //
150 class DSOEXPORT movie_root : public GcRoot, boost::noncopyable
151 {
152 public:
153 
154  class LoadCallback {
155  public:
156  LoadCallback(std::unique_ptr<IOChannel> s, as_object* o)
157  :
158  _stream(std::move(s)),
159  _obj(o)
160  {}
161  bool processLoad();
162  void setReachable() const;
163  private:
164  std::unique_ptr<IOChannel> _stream;
165  SimpleBuffer _buf;
166  as_object* _obj;
167  };
168  typedef std::list<LoadCallback> LoadCallbacks;
169 
170  typedef std::bitset<key::KEYCOUNT> Keys;
171 
173  //
176  movie_root(VirtualClock& clock, const RunResources& runResources);
177 
178  ~movie_root();
179 
181  //
183  //
189  Movie* init(movie_definition* def,
190  const MovieClip::MovieVariables& variables);
191 
193  //
197  MovieClip* getLevel(unsigned int num) const;
198 
200  //
205  void setLevel(unsigned int num, Movie* movie);
206 
208  //
216  void replaceLevel(unsigned int num, Movie* external_movie);
217 
219  //
232  void swapLevels(MovieClip* sp, int depth);
233 
235  //
243  void dropLevel(int depth);
244 
246  //
249  //
252  void setDimensions(size_t w, size_t h);
253 
255  size_t getStageWidth() const;
256 
258  size_t getStageHeight() const;
259 
261  //
270  DSOEXPORT bool mouseMoved(std::int32_t x, std::int32_t y);
271 
273  //
276  DSOEXPORT bool mouseClick(bool press);
277 
279  //
284  DSOEXPORT bool mouseWheel(int delta);
285 
287  //
291  DSOEXPORT bool keyEvent(key::code k, bool down);
292 
294  //
296  std::pair<std::int32_t, std::int32_t> mousePosition() const;
297 
298  void setDragState(const DragState& st);
299 
301  //
304  return *_rootMovie;
305  }
306 
307  void stop_drag() {
308  _dragState.reset();
309  }
310 
312  //
322  std::uint32_t addIntervalTimer(std::unique_ptr<Timer> timer);
323 
325  //
329  //
331  //
334  //
337  //
340  void addLoadableObject(as_object* obj, std::unique_ptr<IOChannel> str);
341 
342  void addAdvanceCallback(ActiveRelay* obj);
343 
344  void removeAdvanceCallback(ActiveRelay* obj);
345 
347  //
349  bool clearIntervalTimer(std::uint32_t x);
350 
351  void set_background_color(const rgba& color);
352 
353  void set_background_alpha(float alpha);
354 
356  VM& getVM() { return _vm; }
357 
360  //
364  bool advance();
365 
369  //
372  int timeToNextFrame() const;
373 
375  //
385  void advanceMovie();
386 
387  void display();
388 
391  return ++_unnamedInstance;
392  }
393 
395  void registerButton(Button* listener);
396 
398  void removeButton(Button* listener);
399 
401  //
407  DisplayObject* getFocus();
408 
410  //
416  bool setFocus(DisplayObject* to);
417 
418  DSOEXPORT void add_invalidated_bounds(InvalidatedRanges& ranges,
419  bool force);
420 
422  //
429  DisplayObject* getActiveEntityUnderPointer() const;
430 
432  //
436  const DisplayObject* getEntityUnderPointer() const;
437 
439  DisplayObject* getDraggingCharacter() const;
440 
441  bool testInvariant() const;
442 
446  DISPLAYSTATE_FULLSCREEN
447  };
448 
450  enum ScaleMode {
454  SCALEMODE_NOBORDER
455  };
456 
461  STAGE_H_ALIGN_R
462  };
463 
468  STAGE_V_ALIGN_B
469  };
470 
472  enum AlignMode {
476  STAGE_ALIGN_B
477  };
478 
483  SCRIPT_ACCESS_ALWAYS
484  };
485 
487  void setQuality(Quality q);
488 
490  Quality getQuality() const { return _quality; }
491 
494  void setStageAlignment(short s);
495 
498  void setAllowScriptAccess(AllowScriptAccessMode mode);
499 
501  AllowScriptAccessMode getAllowScriptAccess();
502 
503  typedef std::pair<StageHorizontalAlign, StageVerticalAlign> StageAlign;
504 
507  StageAlign getStageAlignment() const;
508 
511  bool getShowMenuState() const;
512 
515  void setShowMenuState(bool state);
516 
518  void setStageScaleMode(ScaleMode sm);
519 
521  ScaleMode getStageScaleMode() const { return _scaleMode; }
522 
523  // The string representation of the current align mode.
524  std::string getStageAlignMode() const;
525 
527  DisplayState getStageDisplayState() const { return _displayState; }
528 
529  // The string representation of the current align mode.
530  void setStageDisplayState(const DisplayState ds);
531 
541  PRIORITY_SIZE
542  };
543 
545  //
549  typedef std::array<boost::ptr_deque<ExecutableCode>, PRIORITY_SIZE>
551 
553  void pushAction(std::unique_ptr<ExecutableCode> code, size_t lvl);
554 
556  void pushAction(const action_buffer& buf, DisplayObject* target);
557 
559  //
570  void markReachableResources() const;
571 
575  //
581  {
582  // Don't register the object in the list twice
583 #if GNASH_PARANOIA_LEVEL > 1
584  assert(std::find(_liveChars.begin(), _liveChars.end(), ch) ==
585  _liveChars.end());
586 #endif
587  _liveChars.push_front(ch);
588  }
589 
591  void reset();
592 
594  //
604  void disableScripts();
605 
607  bool scriptsDisabled() const { return _disableScripts; };
608 
611  //
617  void flushHigherPriorityActionQueues();
618 
619  DisplayObject* findCharacterByTarget(const std::string& tgtstr) const;
620 
622  //
626  //
638  void loadMovie(const std::string& url, const std::string& target,
639  const std::string& data, MovieClip::VariablesMethod method,
640  as_object* handler=nullptr)
641  {
642  _movieLoader.loadMovie(url, target, data, method, handler);
643  }
644 
646  //
650  //
658  void getURL(const std::string& urlstr, const std::string& target,
659  const std::string& data, MovieClip::VariablesMethod method);
660 
661 
663  return _lastKeyEvent;
664  }
665 
666  const Keys& unreleasedKeys() const {
667  return _unreleasedKeys;
668  }
669 
671  //
675  void registerClass(const SWF::DefinitionTag* sprite, as_function* cls);
676 
678  //
682  as_function* getRegisteredClass(const SWF::DefinitionTag* sprite) const;
683 
686  void setHostFD(int fd) {
687  assert(fd >= 0);
688  _hostfd = fd;
689  }
690 
693  void setControlFD(int fd) {
694  _controlfd = fd;
695  }
696 
701  int getHostFD() const {
702  return _hostfd;
703  }
704 
705  int getControlFD() const {
706  return _controlfd;
707  }
708 
719  _fsCommandHandler = handler;
720  }
721 
723  DSOEXPORT void handleFsCommand(const std::string& cmd,
724  const std::string& arg) const;
725 
733  _interfaceHandler = handler;
734  }
735 
737  //
739  void callInterface(const HostInterface::Message& e) const;
740 
742  //
747  //
750  template<typename T> T callInterface(const HostInterface::Message& e) const;
751 
756  //
767  void setScriptLimits(std::uint16_t recursion, std::uint16_t timeout);
768 
771  std::uint16_t getRecursionLimit() const {
772  return _recursionLimit;
773  }
774 
777  std::uint16_t getTimeoutLimit() const
778  {
779  return _timeoutLimit;
780  }
781 
782 #ifdef USE_SWFTREE
783  typedef tree<std::pair<std::string, std::string> > InfoTree;
784  void getMovieInfo(InfoTree& tr, InfoTree::iterator it);
785  void getCharacterTree(InfoTree& tr, InfoTree::iterator it);
786 #endif
787 
788  const RunResources& runResources() const { return _runResources; }
789 
790  typedef std::map<std::string, as_object*> ExternalCallbackMethods;
791  typedef std::map<std::string, as_object*> ExternalCallbackInstances;
792  ExternalCallbackMethods _externalCallbackMethods;
793  ExternalCallbackInstances _externalCallbackInstances;
794 
796  //
803  void addExternalCallback(const std::string& name, as_object* callback,
804  as_object* instance);
805 
806  bool processInvoke(ExternalInterface::invoke_t *);
807 
808  std::string callExternalCallback(const std::string &name,
809  const std::vector<as_value>& args);
810 
811  std::string callExternalJavascript(const std::string &name,
812  const std::vector<as_value>& args);
813 
815  //
820  void removeQueuedConstructor(MovieClip* target);
821 
822  GC& gc() {
823  return _gc;
824  }
825 
827  //
830  bool queryInterface(const std::string& what) const;
831 
833  //
837  //
843  void setStreamBlock(int id, int block);
844 
846  //
849  //
851  void stopStream(int id);
852 
853 private:
854 
856  //
878  void setRootMovie(Movie* movie);
879 
881  bool notify_mouse_listeners(const event_id& event);
882 
886  bool fire_mouse_event();
887 
889  void doMouseDrag();
890 
892  void executeAdvanceCallbacks();
893 
895  void executeTimers();
896 
898  void cleanupAndCollect();
899 
903  //
915  InteractiveObject* getTopmostMouseEntity(std::int32_t x,
916  std::int32_t y) const;
917 
920  void cleanupDisplayList();
921 
923  void advanceLiveChars();
924 
928  void setInvalidated() { _invalidated = true; }
929 
931  //
934  void clearInvalidated() { _invalidated = false; }
935 
937  //
944  bool isInvalidated() { return _invalidated; }
945 
947  //
950  size_t minPopulatedPriorityQueue() const;
951 
955  size_t processActionQueue(size_t lvl);
956 
957  bool processingActions() const {
958  return (_processingActionLevel < PRIORITY_SIZE);
959  }
960 
961  const DisplayObject* findDropTarget(std::int32_t x, std::int32_t y,
962  DisplayObject* dragging) const;
963 
964  void handleActionLimitHit(const std::string& ref);
965 
966  typedef std::forward_list<Button*> ButtonListeners;
967  ButtonListeners _buttonListeners;
968 
969  GC _gc;
970 
971  const RunResources& _runResources;
972 
975  VM _vm;
976 
978  HostInterface* _interfaceHandler;
979 
981  FsCallback* _fsCommandHandler;
982 
984  //
989  typedef std::forward_list<MovieClip*> LiveChars;
990 
992  LiveChars _liveChars;
993 
994  ActionQueue _actionQueue;
995 
997  void processActionQueue();
998 
1000  size_t _stageWidth;
1001  size_t _stageHeight;
1002 
1003  rgba m_background_color;
1004  bool m_background_color_set;
1005 
1006  std::int32_t _mouseX;
1007  std::int32_t _mouseY;
1008 
1009  MouseButtonState _mouseButtonState;
1010 
1012  typedef std::set<ActiveRelay*> ObjectCallbacks;
1013  ObjectCallbacks _objectCallbacks;
1014 
1015  LoadCallbacks _loadCallbacks;
1016 
1017  typedef std::map<std::uint32_t, std::unique_ptr<Timer>> TimerMap;
1018 
1019  TimerMap _intervalTimers;
1020 
1021  size_t _lastTimerId;
1022 
1024  Keys _unreleasedKeys;
1025 
1026  key::code _lastKeyEvent;
1027 
1029  DisplayObject* _currentFocus;
1030 
1032  boost::optional<DragState> _dragState;
1033 
1034  typedef std::map<int, MovieClip*> Levels;
1035 
1037  //
1041  Levels _movies;
1042 
1043  typedef std::map<const SWF::DefinitionTag*, as_function*> RegisteredClasses;
1044  RegisteredClasses _registeredClasses;
1045 
1049  Movie* _rootMovie;
1050 
1052  bool _invalidated;
1053 
1056  bool _disableScripts;
1057  int _processingActionLevel;
1058 
1060  //
1062  int _hostfd;
1063  int _controlfd;
1064 
1066  //
1069  Quality _quality;
1070 
1072  std::bitset<4u> _alignMode;
1073 
1074  AllowScriptAccessMode _allowScriptAccess;
1075 
1077  bool _showMenu;
1078 
1080  ScaleMode _scaleMode;
1081 
1083  DisplayState _displayState;
1084 
1085  // Maximum number of recursions set in the ScriptLimits tag.
1086  std::uint16_t _recursionLimit;
1087 
1088  // Timeout in seconds for script execution, set in the ScriptLimits tag.
1089  std::uint16_t _timeoutLimit;
1090 
1091  // delay between movie advancement, in milliseconds
1092  size_t _movieAdvancementDelay;
1093 
1094  // time of last movie advancement, in milliseconds
1095  size_t _lastMovieAdvancement;
1096 
1098  size_t _unnamedInstance;
1099 
1100  MovieLoader _movieLoader;
1101 
1102  struct SoundStream {
1103  SoundStream(int i, int b) : id(i), block(b) {}
1104  int id;
1105  int block;
1106  };
1107 
1108  boost::optional<SoundStream> _timelineSound;
1109 };
1110 
1112 //
1120 bool isLevelTarget(int version, const std::string& name, unsigned int& levelno);
1121 
1122 DSOEXPORT short stringToStageAlign(const std::string& s);
1123 
1124 template<typename T>
1125 T
1127 {
1128  if (!_interfaceHandler) {
1129  log_error("Hosting application registered no callback for "
1130  "messages, can't call %s(%s)");
1131  return T();
1132  }
1133 
1134  try {
1135  return boost::any_cast<T>(_interfaceHandler->call(e));
1136  }
1137  catch (const boost::bad_any_cast&) {
1138  log_error(_("Unexpected type from host interface when requesting "
1139  "%1%"), e);
1140  return T();
1141  }
1142 }
1143 
1144 } // namespace gnash
1145 
1146 #endif // GNASH_MOVIE_ROOT_H
1147 
1148 // Local Variables:
1149 // mode: C++
1150 // indent-tabs-mode: nil
1151 // End:
VariablesMethod
The various methods for sending data in requests.
Definition: MovieClip.h:418
Definition: movie_root.h:473
int getHostFD() const
Definition: movie_root.h:701
const RunResources & runResources() const
Definition: movie_root.h:788
key::code lastKeyEvent() const
Definition: movie_root.h:662
std::map< std::string, as_object * > ExternalCallbackMethods
Definition: movie_root.h:790
bool scriptsDisabled() const
Return true if scripts execution is disabled.
Definition: movie_root.h:607
Client program&#39;s interface to the definition of a movie or sprite.
Definition: movie_definition.h:95
std::list< LoadCallback > LoadCallbacks
Definition: movie_root.h:168
ScaleMode getStageScaleMode() const
Returns the Stage object&#39;s align mode.
Definition: movie_root.h:521
A class used to virtualize time flow.
Definition: VirtualClock.h:33
DisplayState getStageDisplayState() const
Returns the Stage object&#39;s align mode.
Definition: movie_root.h:527
A MovieClip is a container for DisplayObjects.
Definition: MovieClip.h:83
DisplayObject is the base class for all DisplayList objects.
Definition: DisplayObject.h:168
Definition: ExternalInterface.h:44
void setControlFD(int fd)
Definition: movie_root.h:693
VM & getVM()
Return the VM used by this movie_root.
Definition: movie_root.h:356
What is being dragged and how.
Definition: DragState.h:35
AllowScriptAccessMode
The possibile values of AllowScriptAccess.
Definition: movie_root.h:480
Definition: movie_root.h:481
Definition: GnashKey.h:163
Definition: GnashKey.h:157
Construct event handlers.
Definition: movie_root.h:537
A top-level, standalone Movie that can be loaded and played.
Definition: Movie.h:46
Abstract base class for FS handlers.
Definition: HostInterface.h:199
Definition: movie_root.h:475
void callInterface(const HostInterface::Message &e) const
Call the hosting application without expecting a reply.
Definition: movie_root.cpp:2516
ExternalCallbackInstances _externalCallbackInstances
Definition: movie_root.h:793
SimpleBuffer data
Definition: LocalConnection_as.cpp:151
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
boost::variant< HostMessage, CustomMessage > Message
Definition: HostInterface.h:213
Immutable data representing the definition of a movie display element.
Definition: DefinitionTag.h:47
AlignMode
The possible elements of a Stage.alignMode.
Definition: movie_root.h:472
Quality
Definition: GnashEnums.h:34
GC & gc()
Definition: movie_root.h:822
std::map< std::string, std::string > MovieVariables
Definition: MovieClip.h:93
Definition: movie_root.h:126
bool operator()(const LevelMovie &d1, const LevelMovie &d2) const
Definition: movie_root.h:129
void addLiveChar(MovieClip *ch)
Register a newly born advanceable DisplayObject to the list of DisplayObjects to be advanced on next ...
Definition: movie_root.h:580
short stringToStageAlign(const std::string &str)
Definition: movie_root.cpp:2406
The base class for all ActionScript objects.
Definition: as_object.h:161
std::uint16_t getTimeoutLimit() const
Definition: movie_root.h:777
Definition: GnashKey.h:161
DSOEXPORT void registerEventCallback(HostInterface *handler)
Definition: movie_root.h:732
Frame actions, load handlers, unload handlers.
Definition: movie_root.h:539
Definition: movie_root.h:466
const VGfloat color[4]
Definition: testr_gtk.cpp:82
code
Definition: GnashKey.h:43
Abstract class to allow the GC to store "roots" into a container.
Definition: GC.h:55
Definition: movie_root.h:460
Definition: movie_root.h:467
int getControlFD() const
Definition: movie_root.h:705
#define _(String)
Definition: log.h:44
size_t nextUnnamedInstance()
Get a unique number for unnamed instances.
Definition: movie_root.h:390
Class to group together per-run and external resources for Gnash.
Definition: RunResources.h:53
void log_error(StringType msg, Args... args)
Definition: log.h:283
bool isLevelTarget(int version, const std::string &name, unsigned int &levelno)
Return true if the given string can be interpreted as a _level name.
Definition: movie_root.cpp:2387
StageVerticalAlign
The possible vertical position of the Stage.
Definition: movie_root.h:465
DisplayState
The possible values of Stage.displayState.
Definition: movie_root.h:444
void loadMovie(const std::string &url, const std::string &target, const std::string &data, MovieClip::VariablesMethod method, as_object *handler=nullptr)
Queue a request for loading a movie.
Definition: movie_root.h:638
Button implements Flash buttons.
Definition: Button.h:44
Definition: movie_root.h:474
std::int32_t x
Definition: BitmapData_as.cpp:434
Definition: movie_root.h:154
Definition: GnashKey.h:148
ActionPriorityLevel
Action priority levels.
Definition: movie_root.h:533
#define DSOEXPORT
Definition: dsodefs.h:55
std::uint16_t getRecursionLimit() const
Definition: movie_root.h:771
void setHostFD(int fd)
Definition: movie_root.h:686
Definition: movie_root.h:452
The base class for interactive objects.
Definition: InteractiveObject.h:44
std::map< std::string, as_object * > ExternalCallbackInstances
Definition: movie_root.h:791
The AVM1 virtual machine.
Definition: VM.h:71
Definition: movie_root.h:459
void stop_drag()
Definition: movie_root.h:307
Definition: GnashKey.h:132
ScaleMode
The possibile values of Stage.scaleMode.
Definition: movie_root.h:450
std::int32_t y
Definition: BitmapData_as.cpp:435
Definition: GnashKey.h:154
w
Definition: test.py:8
Definition: movie_root.h:445
Definition: GnashKey.h:155
std::string url
Definition: gnash.cpp:59
Movie & getRootMovie()
Access the originating root movie (not necessarily _level0)
Definition: movie_root.h:303
Garbage collector singleton.
Definition: GC.h:164
Definition: GnashKey.h:151
Definition: movie_root.h:451
A simple buffer of bytes.
Definition: SimpleBuffer.h:38
LoadCallback(std::unique_ptr< IOChannel > s, as_object *o)
Definition: movie_root.h:156
Init actions, Init event handlers.
Definition: movie_root.h:535
Quality getQuality() const
Get the current display quality.
Definition: movie_root.h:490
Abstract base class for hosting app handler.
Definition: HostInterface.h:207
Movie loader.
Definition: MovieLoader.h:50
A native type that requires periodic updates from the core (movie_root).
Definition: Relay.h:79
std::array< boost::ptr_deque< ExecutableCode >, PRIORITY_SIZE > ActionQueue
A number of queues of code to execute.
Definition: movie_root.h:550
A class to identify &#39;static&#39; SWF events (system events).
Definition: event_id.h:52
ExternalCallbackMethods _externalCallbackMethods
Definition: movie_root.h:792
const Keys & unreleasedKeys() const
Definition: movie_root.h:666
Definition: GnashKey.h:165
StageHorizontalAlign
The possible horizonal positions of the Stage.
Definition: movie_root.h:458
MovieClip * LevelMovie
Definition: movie_root.h:128
std::bitset< key::KEYCOUNT > Keys
Definition: movie_root.h:170
A code segment.
Definition: action_buffer.h:49
std::pair< StageHorizontalAlign, StageVerticalAlign > StageAlign
Definition: movie_root.h:503
DSOEXPORT void registerFSCommandCallback(FsCallback *handler)
Definition: movie_root.h:718
std::string name
Definition: LocalConnection_as.cpp:149
Definition: movie_root.h:453
ActionScript Function, either builtin or SWF-defined.
Definition: as_function.h:62
Helper to generate mouse events, given mouse state & history.
Definition: MouseButtonState.h:20
int get_depth() const
Definition: DisplayObject.h:268
This class represents the &#39;Stage&#39; and top-level movie.
Definition: movie_root.h:150
A basic RGBA type.
Definition: RGBA.h:35