1 // gnash.cpp: Main routine for top-level SWF player, for Gnash.
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "gnashconfig.h"
29 #include <boost/format.hpp>
30 #include <boost/program_options.hpp>
31 #include <boost/algorithm/string/join.hpp>
32 #include <boost/algorithm/string/split.hpp>
33 #include <boost/algorithm/string/classification.hpp>
34 #include <boost/function.hpp>
44 #include "rc.h" // for use of rcfile
45 #include "GnashNumeric.h" // for clamp
46 #include "GnashException.h"
48 #include "MediaHandler.h"
53 std::vector
<std::string
> infiles
;
61 gnash::LogFile
& dbglogfile
= gnash::LogFile::getDefaultInstance();
62 gnash::RcInitFile
& rcfile
= gnash::RcInitFile::getDefaultInstance();
65 // Forward declarations
67 namespace po
= boost::program_options
;
68 po::options_description
getSupportedOptions(gnash::Player
& p
);
70 void setupSoundAndRendering(gnash::Player
& p
, int i
);
71 void setupFlashVars(gnash::Player
& p
,
72 const std::vector
<std::string
>& params
);
73 void setupFDs(gnash::Player
& p
, const std::string
& fds
);
75 void usage_gui_keys(std::ostream
& os
);
76 void usage(std::ostream
& os
, const po::options_description
& opts
);
77 void build_options(std::ostream
& os
);
78 void version_and_copyright(std::ostream
& os
);
83 /// An accumulating option value to handle multiple -v options.
85 class accumulator_type
: public po::value_semantic
{
88 accumulator_type() : _interval(1), _default(0) {}
90 /// Set the notifier function.
91 accumulator_type
* notifier(boost::function1
<void, const T
&> f
) {
96 /// Set the default value for this option.
97 accumulator_type
* default_value(const T
& t
) {
102 /// Set the implicit value for this option.
104 /// Unlike for program_options::value, this specifies a value
105 /// to be applied on each occurence of the option.
106 accumulator_type
* implicit_value(const T
& t
) {
111 virtual std::string
name() const {
112 return std::string();
115 /// There are no tokens for an accumulator_type
116 virtual unsigned min_tokens() const { return 0; }
117 virtual unsigned max_tokens() const { return 0; }
119 // Accumulating from different sources is silly.
120 virtual bool is_composing() const {
124 virtual bool is_required() const { return false; }
126 virtual void parse(boost::any
& value_store
,
127 const std::vector
<std::string
>& new_tokens
,
130 assert(new_tokens
.empty());
131 if (value_store
.empty()) value_store
= T();
132 boost::any_cast
<T
&>(value_store
) += _interval
;
135 virtual bool apply_default(boost::any
& value_store
) const {
136 value_store
= _default
;
140 virtual void notify(const boost::any
& value_store
) const {
141 _notifier(boost::any_cast
<T
>(value_store
));
144 virtual ~accumulator_type() {}
147 boost::function1
<void, const T
&> _notifier
;
153 accumulator_type
<T
>* accumulator() {
154 return new accumulator_type
<T
>();
160 main(int argc
, char *argv
[])
163 std::ios::sync_with_stdio(false);
165 // Enable native language support, i.e. internationalization
167 std::setlocale (LC_ALL
, "");
168 bindtextdomain (PACKAGE
, LOCALEDIR
);
169 textdomain (PACKAGE
);
172 gnash::Player player
;
174 po::options_description opts
= getSupportedOptions(player
);
176 // Add all positional arguments as input files.
177 po::positional_options_description files
;
178 files
.add("input-file", -1);
180 namespace cls
= po::command_line_style
;
182 po::variables_map vm
;
184 po::store(po::command_line_parser(argc
, argv
)
187 .style(cls::default_style
^ cls::allow_guessing
)
190 catch (const po::error
& e
) {
191 std::cerr
<< boost::format(_("Error parsing options: %s\n"))
198 if (vm
.count("help")) {
199 version_and_copyright(std::cout
);
200 usage(std::cout
, opts
);
204 if (vm
.count("version")) {
205 version_and_copyright(std::cout
);
206 build_options(std::cout
);
210 // Do some extra sanity checks on the options.
211 const bool plugin
= vm
.count("xid");
213 if (plugin
&& vm
.count("height") && vm
.count("width") &&
214 !player
.getHeight() && !player
.getWidth()) {
215 // We were given dimensions of 0x0 to render to (probably the plugin
216 // is playing an "invisible" movie. Disable video rendering.
217 player
.setDoRender(false);
220 if (!vm
.count("render-mode")) {
221 std::cerr
<< "Using rcfile\n";
223 player
.setDoSound(rcfile
.usePluginSound());
226 player
.setDoSound(rcfile
.useSound());
230 // No file name was supplied
231 if (infiles
.empty()) {
232 std::cerr
<< _("Error: no input file was specified. Exiting.\n");
233 usage(std::cerr
, opts
);
237 // We only expect GnashExceptions here. No others should be thrown!
239 player
.run(argc
, argv
, infiles
.front(), url
);
241 catch (const gnash::GnashException
& ex
) {
242 std::cerr
<< "Error: " << ex
.what() << "\n";
251 setupFlashVars(gnash::Player
& p
, const std::vector
<std::string
>& params
)
253 for (std::vector
<std::string
>::const_iterator i
= params
.begin(),
254 e
= params
.end(); i
!= e
; ++i
) {
255 const std::string
& param
= *i
;
256 const size_t eq
= param
.find("=");
257 if (eq
== std::string::npos
) {
258 p
.setParam(param
, "true");
261 const std::string name
= param
.substr(0, eq
);
262 const std::string value
= param
.substr(eq
+ 1);
263 p
.setParam(name
, value
);
268 setupFDs(gnash::Player
& p
, const std::string
& fds
)
270 int hostfd
= 0, controlfd
= 0;
271 hostfd
= std::strtol(fds
.substr(0, fds
.find(":")).c_str(), NULL
, 0);
272 std::string csub
= fds
.substr(fds
.find(":")+1, fds
.size());
273 controlfd
= strtol(csub
.c_str(), 0, 0);
274 gnash::log_debug(_("Host FD #%d, Control FD #%d\n"),
278 std::cerr
<< boost::format(_("Invalid host communication "
279 "filedescriptor %1%\n")) % hostfd
;
280 std::exit(EXIT_FAILURE
);
285 std::cerr
<< boost::format(_("Invalid control communication "
286 "filedescriptor %1%\n")) % controlfd
;
287 std::exit(EXIT_FAILURE
);
289 p
.setControlFD(controlfd
);
293 setupSoundAndRendering(gnash::Player
& p
, int i
)
298 p
.setDoRender(false);
302 // Enable rendering, disable sound
307 // Enable sound, disable rendering
308 p
.setDoRender(false);
312 // Enable render & sound
317 gnash::log_error(_("ERROR: -r must be followed by "
323 po::options_description
324 getSupportedOptions(gnash::Player
& p
)
328 using gnash::LogFile
;
329 using gnash::RcInitFile
;
331 std::vector
<std::string
> handlers
;
332 gnash::media::MediaFactory::instance().listKeys(back_inserter(handlers
));
334 std::vector
<std::string
> renderers
;
335 boost::split(renderers
, RENDERER_CONFIG
,
336 boost::is_any_of(" "), boost::token_compress_on
);
338 po::options_description
desc(_("Options"));
343 _("Print this help and exit"))
346 _("Print version information and exit"))
348 ("scale,s", po::value
<float>()
349 ->notifier(boost::bind(&Player::setScale
, &p
,
350 boost::bind(gnash::clamp
<float>, _1
, 0.01f
, 100.f
))),
351 _("Scale the movie by the specified factor"))
353 ("delay,d", po::value
<int>()
354 ->notifier(boost::bind(&Player::setDelay
, &p
, _1
)),
355 _("Number of milliseconds to delay in main loop"))
357 ("verbose,v", accumulator
<int>()
358 ->notifier(boost::bind(&LogFile::setVerbosity
, &dbglogfile
, _1
)),
359 _("Produce verbose output"))
362 ("verbose-actions,a", po::bool_switch()
363 ->notifier(boost::bind(&LogFile::setActionDump
, &dbglogfile
, _1
)),
364 _("Be (very) verbose about action execution"))
368 ("verbose-parsing,p", po::bool_switch()
369 ->notifier(boost::bind(&LogFile::setParserDump
, &dbglogfile
, _1
)),
370 _("Be (very) verbose about parsing"))
373 ("audio-dump,A", po::value
<string
>()
374 ->notifier(boost::bind(&Player::setAudioDumpfile
, &p
, _1
)),
375 _("Audio dump file (wave format)"))
377 ("hwaccel", po::value
<string
>()
378 ->default_value("none")
379 ->notifier(boost::bind(&Player::setHWAccel
, &p
, _1
)),
380 (string(_("Hardware Video Accelerator to use"))
381 + string("\nnone|vaapi")). c_str())
383 ("xid,x", po::value
<long>()
384 ->notifier(boost::bind(&Player::setWindowId
, &p
, _1
)),
385 _("X11 Window ID for display"))
387 ("writelog,w", po::bool_switch()
388 ->notifier(boost::bind(&RcInitFile::useWriteLog
, &rcfile
, _1
)),
389 _("Produce the disk based debug log"))
391 ("width,j", po::value
<int>()
392 ->notifier(boost::bind(&Player::setWidth
, &p
, _1
)),
393 _("Set window width"))
395 ("height,k", po::value
<int>()
396 ->notifier(boost::bind(&Player::setHeight
, &p
, _1
)),
397 _("Set window height"))
399 ("x-pos,X", po::value
<int>()
400 ->notifier(boost::bind(&Player::setXPosition
, &p
, _1
)),
401 _("Set window x position"))
403 ("y-pos,Y", po::value
<int>()
404 ->notifier(boost::bind(&Player::setYPosition
, &p
, _1
)),
405 _("Set window y position"))
407 ("once,1", po::bool_switch()
408 ->notifier(boost::bind(&Player::setDoLoop
, &p
,
409 boost::bind(std::logical_not
<bool>(), _1
))),
410 _("Exit when/if movie reaches the last frame"))
412 ("render-mode,r", po::value
<int>()
414 ->notifier(boost::bind(&setupSoundAndRendering
, boost::ref(p
), _1
)),
416 + string(_("disable rendering and sound"))
418 + string(_("enable rendering, disable sound"))
420 + string(_("enable sound, disable rendering"))
422 + string(_("enable rendering and sound"))
425 ("media,M", po::value
<string
>()
426 ->default_value(handlers
.front())
427 ->notifier(boost::bind(&Player::setMedia
, &p
, _1
)),
428 (string(_("The media handler to use"))
429 + string("\n") + boost::join(handlers
, "|")
432 ("renderer,R", po::value
<string
>()
433 ->default_value("agg")
434 ->notifier(boost::bind(&Player::setRenderer
, &p
, _1
)),
435 (string(_("The renderer to use"))
436 + string("\n") + boost::join(renderers
, "|")
439 ("timeout,t", po::value
<float>()
440 ->notifier(boost::bind(&Player::setExitTimeout
, &p
, _1
)),
441 _("Exit after the specified number of seconds"))
443 ("real-url,u", po::value
<string
>(&url
),
444 _("Set \"real\" URL of the movie"))
446 ("base-url,U", po::value
<string
>()
447 ->notifier(boost::bind(&Player::setBaseUrl
, &p
, _1
)),
448 _("Set \"base\" URL for resolving relative URLs"))
450 ("param,P", po::value
<std::vector
<std::string
> >()
452 ->notifier(boost::bind(&setupFlashVars
, boost::ref(p
), _1
)),
453 _("Set parameter (e.g. \"FlashVars=A=1&b=2\")"))
455 ("fd,F", po::value
<string
>()
456 ->notifier(boost::bind(&setupFDs
, boost::ref(p
), _1
)),
457 (string(_("Filedescriptor to use for external communications"))
458 + string(" <fd>:<fd>")
461 #ifdef GNASH_FPS_DEBUG
462 ("debug-fps,f", po::value
<float>()
463 ->notifier(boost::bind(&Player::setFpsPrintTime
, &p
, _1
)),
464 _("Print FPS every num seconds"))
467 ("max-advances", po::value
<size_t>()
468 ->notifier(boost::bind(&Player::setMaxAdvances
, &p
, _1
)),
469 _("Exit after specified number of frame advances"))
471 ("fullscreen", po::bool_switch()
472 ->notifier(boost::bind(&Player::setStartFullscreen
, &p
, _1
)),
473 _("Start in fullscreen mode"))
475 // TODO: move to GUIs actually implementing this
476 ("hide-menubar", po::bool_switch()
477 ->notifier(boost::bind(&Player::hideMenu
, &p
, _1
)),
478 _("Start without displaying the menu bar"))
480 // TODO: do this in ScreenShotter class.
481 ("screenshot", po::value
<string
>()
482 ->notifier(boost::bind(&Player::setScreenShots
, &p
, _1
)),
483 _("List of frames to save as screenshots"))
485 ("screenshot-file", po::value
<string
>()
486 ->notifier(boost::bind(&Player::setScreenShotFile
, &p
, _1
)),
487 _("Filename pattern for screenshot images"))
489 ("screenshot-quality", po::value
<size_t>()
490 ->notifier(boost::bind(&Player::setScreenShotQuality
, &p
, _1
)),
491 _("Quality for screenshot output (not all formats)"))
493 ("input-file", po::value
<std::vector
<std::string
> >(&infiles
),
501 usage_gui_keys(std::ostream
& os
)
504 << " CTRL-Q, CTRL-W "
507 << _("Toggle fullscreen\n")
509 << _("Toggle pause\n")
511 << _("Restart the movie\n")
513 << _("Take a screenshot\n")
515 << _("Force immediate redraw\n");
519 usage(std::ostream
& os
, const po::options_description
& opts
)
521 os
<< _("Usage: gnash [options] movie_file.swf\n")
522 << _("Plays a SWF (Shockwave Flash) movie\n")
526 // TODO: stop printing these in here ?
533 version_and_copyright(std::ostream
& os
)
537 << BRANCH_NICK
<< "-" << BRANCH_REVNO
<< "-" << COMMIT_ID
538 << ")" << endl
<< endl
539 << _("Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 "
540 "Free Software Foundation, Inc.\n"
541 "Gnash comes with NO WARRANTY, to the extent permitted "
542 "by law.\nYou may redistribute copies of Gnash under the "
543 "terms of the GNU General\nPublic License. For more "
544 "information, see the file named COPYING.\n\n");
548 build_options(std::ostream
& os
)
550 os
<< _("Build options ") << endl
551 << _(" Renderers: ") << RENDERER_CONFIG
<< endl
552 << _(" Hardware Acceleration: ") << HWACCEL_CONFIG
<< endl
553 << _(" GUI: ") << GUI_CONFIG
<< endl
554 << _(" Media handlers: ") << MEDIA_CONFIG
<< endl
556 << _(" Configured with: ") << CONFIG_CONFIG
<< endl
557 << _(" CXXFLAGS: ") << CXXFLAGS
<< endl
;
561 } // unnamed namespace