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;
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  //
568  void markReachableResources() const;
569 
573  //
579  {
580  // Don't register the object in the list twice
581 #if GNASH_PARANOIA_LEVEL > 1
582  assert(std::find(_liveChars.begin(), _liveChars.end(), ch) ==
583  _liveChars.end());
584 #endif
585  _liveChars.push_front(ch);
586  }
587 
589  void reset();
590 
592  //
602  void disableScripts();
603 
605  bool scriptsDisabled() const { return _disableScripts; };
606 
609  //
615  void flushHigherPriorityActionQueues();
616 
617  DisplayObject* findCharacterByTarget(const std::string& tgtstr) const;
618 
620  //
624  //
636  void loadMovie(const std::string& url, const std::string& target,
637  const std::string& data, MovieClip::VariablesMethod method,
638  as_object* handler=nullptr)
639  {
640  _movieLoader.loadMovie(url, target, data, method, handler);
641  }
642 
644  //
648  //
656  void getURL(const std::string& urlstr, const std::string& target,
657  const std::string& data, MovieClip::VariablesMethod method);
658 
659 
661  return _lastKeyEvent;
662  }
663 
664  const Keys& unreleasedKeys() const {
665  return _unreleasedKeys;
666  }
667 
669  //
673  void registerClass(const SWF::DefinitionTag* sprite, as_function* cls);
674 
676  //
680  as_function* getRegisteredClass(const SWF::DefinitionTag* sprite) const;
681 
684  void setHostFD(int fd) {
685  assert(fd >= 0);
686  _hostfd = fd;
687  }
688 
691  void setControlFD(int fd) {
692  _controlfd = fd;
693  }
694 
699  int getHostFD() const {
700  return _hostfd;
701  }
702 
703  int getControlFD() const {
704  return _controlfd;
705  }
706 
717  _fsCommandHandler = handler;
718  }
719 
721  DSOEXPORT void handleFsCommand(const std::string& cmd,
722  const std::string& arg) const;
723 
731  _interfaceHandler = handler;
732  }
733 
735  //
737  void callInterface(const HostInterface::Message& e) const;
738 
740  //
745  //
748  template<typename T> T callInterface(const HostInterface::Message& e) const;
749 
754  //
765  void setScriptLimits(std::uint16_t recursion, std::uint16_t timeout);
766 
769  std::uint16_t getRecursionLimit() const {
770  return _recursionLimit;
771  }
772 
775  std::uint16_t getTimeoutLimit() const
776  {
777  return _timeoutLimit;
778  }
779 
780 #ifdef USE_SWFTREE
781  typedef tree<std::pair<std::string, std::string> > InfoTree;
782  void getMovieInfo(InfoTree& tr, InfoTree::iterator it);
783  void getCharacterTree(InfoTree& tr, InfoTree::iterator it);
784 #endif
785 
786  const RunResources& runResources() const { return _runResources; }
787 
789  void addExternalCallback(const std::string& name, as_object* callback);
790 
791  bool processInvoke(ExternalInterface::invoke_t *);
792 
793  std::string callExternalCallback(const std::string &name,
794  const std::vector<as_value>& args);
795 
796  std::string callExternalJavascript(const std::string &name,
797  const std::vector<as_value>& args);
798 
800  //
805  void removeQueuedConstructor(MovieClip* target);
806 
807  GC& gc() {
808  return _gc;
809  }
810 
812  //
815  bool queryInterface(const std::string& what) const;
816 
818  //
822  //
828  void setStreamBlock(int id, int block);
829 
831  //
834  //
836  void stopStream(int id);
837 
838 private:
839 
841  //
863  void setRootMovie(Movie* movie);
864 
866  bool notify_mouse_listeners(const event_id& event);
867 
871  bool fire_mouse_event();
872 
874  void doMouseDrag();
875 
877  void executeAdvanceCallbacks();
878 
880  void executeTimers();
881 
883  void cleanupAndCollect();
884 
888  //
900  InteractiveObject* getTopmostMouseEntity(std::int32_t x,
901  std::int32_t y) const;
902 
905  void cleanupDisplayList();
906 
908  void advanceLiveChars();
909 
913  void setInvalidated() { _invalidated = true; }
914 
916  //
919  void clearInvalidated() { _invalidated = false; }
920 
922  //
929  bool isInvalidated() { return _invalidated; }
930 
932  //
935  size_t minPopulatedPriorityQueue() const;
936 
940  size_t processActionQueue(size_t lvl);
941 
942  bool processingActions() const {
943  return (_processingActionLevel < PRIORITY_SIZE);
944  }
945 
946  const DisplayObject* findDropTarget(std::int32_t x, std::int32_t y,
947  DisplayObject* dragging) const;
948 
949  void handleActionLimitHit(const std::string& ref);
950 
951  typedef std::forward_list<Button*> ButtonListeners;
952  ButtonListeners _buttonListeners;
953 
954  GC _gc;
955 
956  const RunResources& _runResources;
957 
960  VM _vm;
961 
963  HostInterface* _interfaceHandler;
964 
966  FsCallback* _fsCommandHandler;
967 
969  //
974  typedef std::forward_list<MovieClip*> LiveChars;
975 
977  LiveChars _liveChars;
978 
979  ActionQueue _actionQueue;
980 
982  void processActionQueue();
983 
985  size_t _stageWidth;
986  size_t _stageHeight;
987 
988  rgba m_background_color;
989  bool m_background_color_set;
990 
991  std::int32_t _mouseX;
992  std::int32_t _mouseY;
993 
994  MouseButtonState _mouseButtonState;
995 
997  typedef std::set<ActiveRelay*> ObjectCallbacks;
998  ObjectCallbacks _objectCallbacks;
999 
1000  LoadCallbacks _loadCallbacks;
1001 
1002  typedef std::map<std::uint32_t, std::unique_ptr<Timer>> TimerMap;
1003 
1004  TimerMap _intervalTimers;
1005 
1006  size_t _lastTimerId;
1007 
1009  Keys _unreleasedKeys;
1010 
1011  key::code _lastKeyEvent;
1012 
1014  DisplayObject* _currentFocus;
1015 
1017  boost::optional<DragState> _dragState;
1018 
1019  typedef std::map<int, MovieClip*> Levels;
1020 
1022  //
1026  Levels _movies;
1027 
1028  typedef std::map<const SWF::DefinitionTag*, as_function*> RegisteredClasses;
1029  RegisteredClasses _registeredClasses;
1030 
1034  Movie* _rootMovie;
1035 
1037  bool _invalidated;
1038 
1041  bool _disableScripts;
1042  int _processingActionLevel;
1043 
1045  //
1047  int _hostfd;
1048  int _controlfd;
1049 
1051  //
1054  Quality _quality;
1055 
1057  std::bitset<4u> _alignMode;
1058 
1059  AllowScriptAccessMode _allowScriptAccess;
1060 
1062  bool _showMenu;
1063 
1065  ScaleMode _scaleMode;
1066 
1068  DisplayState _displayState;
1069 
1070  // Maximum number of recursions set in the ScriptLimits tag.
1071  std::uint16_t _recursionLimit;
1072 
1073  // Timeout in seconds for script execution, set in the ScriptLimits tag.
1074  std::uint16_t _timeoutLimit;
1075 
1076  // delay between movie advancement, in milliseconds
1077  size_t _movieAdvancementDelay;
1078 
1079  // time of last movie advancement, in milliseconds
1080  size_t _lastMovieAdvancement;
1081 
1083  size_t _unnamedInstance;
1084 
1085  MovieLoader _movieLoader;
1086 
1087  struct SoundStream {
1088  SoundStream(int i, int b) : id(i), block(b) {}
1089  int id;
1090  int block;
1091  };
1092 
1093  boost::optional<SoundStream> _timelineSound;
1094 };
1095 
1097 //
1105 bool isLevelTarget(int version, const std::string& name, unsigned int& levelno);
1106 
1107 DSOEXPORT short stringToStageAlign(const std::string& s);
1108 
1109 template<typename T>
1110 T
1112 {
1113  if (!_interfaceHandler) {
1114  log_error("Hosting application registered no callback for "
1115  "messages, can't call %s(%s)");
1116  return T();
1117  }
1118 
1119  try {
1120  return boost::any_cast<T>(_interfaceHandler->call(e));
1121  }
1122  catch (const boost::bad_any_cast&) {
1123  log_error(_("Unexpected type from host interface when requesting "
1124  "%1%"), e);
1125  return T();
1126  }
1127 }
1128 
1129 } // namespace gnash
1130 
1131 #endif // GNASH_MOVIE_ROOT_H
1132 
1133 // Local Variables:
1134 // mode: C++
1135 // indent-tabs-mode: nil
1136 // End:
int getControlFD() const
Definition: movie_root.h:703
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:699
bool operator()(const LevelMovie &d1, const LevelMovie &d2) const
Definition: movie_root.h:129
Client program's interface to the definition of a movie or sprite.
Definition: movie_definition.h:95
bool scriptsDisabled() const
Return true if scripts execution is disabled.
Definition: movie_root.h:605
std::list< LoadCallback > LoadCallbacks
Definition: movie_root.h:168
A class used to virtualize time flow.
Definition: VirtualClock.h:33
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 log_error(StringType msg, Args...args)
Definition: log.h:283
SWFStream & s
Definition: DefineBitsTag.cpp:71
void setControlFD(int fd)
Definition: movie_root.h:691
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
VM & _vm
Definition: Array_as.cpp:861
Definition: movie_root.h:481
std::uint16_t getTimeoutLimit() const
Definition: movie_root.h:775
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
const Keys & unreleasedKeys() const
Definition: movie_root.h:664
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:807
std::map< std::string, std::string > MovieVariables
Definition: MovieClip.h:93
Definition: movie_root.h:126
void addLiveChar(MovieClip *ch)
Register a newly born advanceable DisplayObject to the list of DisplayObjects to be advanced on next ...
Definition: movie_root.h:578
short stringToStageAlign(const std::string &str)
Definition: movie_root.cpp:2359
key::code lastKeyEvent() const
Definition: movie_root.h:660
The base class for all ActionScript objects.
Definition: as_object.h:161
Definition: GnashKey.h:161
DSOEXPORT void registerEventCallback(HostInterface *handler)
Definition: movie_root.h:730
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
as_object & _obj
Definition: Array_as.cpp:137
#define _(String)
Definition: log.h:44
size_t nextUnnamedInstance()
Get a unique number for unnamed instances.
Definition: movie_root.h:390
agg::rendering_buffer _buf
Definition: Renderer_agg.cpp:640
Class to group together per-run and external resources for Gnash.
Definition: RunResources.h:53
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:2340
StageVerticalAlign
The possible vertical position of the Stage.
Definition: movie_root.h:465
ScaleMode getStageScaleMode() const
Returns the Stage object's align mode.
Definition: movie_root.h:521
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:636
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
const RunResources & runResources() const
Definition: movie_root.h:786
Definition: GnashKey.h:148
ActionPriorityLevel
Action priority levels.
Definition: movie_root.h:533
#define DSOEXPORT
Definition: dsodefs.h:55
void setHostFD(int fd)
Definition: movie_root.h:684
virtual boost::any call(const Message &e)=0
Pass a message to the hosting application with an optional return.
Definition: movie_root.h:452
The base class for interactive objects.
Definition: InteractiveObject.h:44
Quality getQuality() const
Get the current display quality.
Definition: movie_root.h:490
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
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
Abstract base class for hosting app handler.
Definition: HostInterface.h:207
tuple w
Definition: test.py:8
void callInterface(const HostInterface::Message &e) const
Call the hosting application without expecting a reply.
Definition: movie_root.cpp:2469
A native type that requires periodic updates from the core (movie_root).
Definition: Relay.h:79
int get_depth() const
Definition: DisplayObject.h:268
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 'static' SWF events (system events).
Definition: event_id.h:52
const Quality _quality
Quality of renderering.
Definition: Renderer_agg.cpp:653
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
DisplayState getStageDisplayState() const
Returns the Stage object's align mode.
Definition: movie_root.h:527
std::uint16_t getRecursionLimit() const
Definition: movie_root.h:769
std::pair< StageHorizontalAlign, StageVerticalAlign > StageAlign
Definition: movie_root.h:503
DSOEXPORT void registerFSCommandCallback(FsCallback *handler)
Definition: movie_root.h:716
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
This class represents the 'Stage' and top-level movie.
Definition: movie_root.h:150
A basic RGBA type.
Definition: RGBA.h:35