1 // Copyright 2005-6 Ben Hutchings <ben@decadent.org.uk>.
2 // See the file "COPYING" for licence details.
18 #include <gdkmm/pixbuf.h>
19 #include <glibmm/convert.h>
20 #include <glibmm/spawn.h>
21 #include <gtkmm/main.h>
22 #include <gtkmm/window.h>
24 #include <imglib2/ImageErrors.h>
25 #include <nsGUIEvent.h>
26 #include <nsIBoxObject.h>
27 #include <nsIContent.h>
28 #include <nsIDocShell.h>
29 #include <nsIDOMAbstractView.h>
30 #include <nsIDOMBarProp.h>
31 #include <nsIDOMDocumentEvent.h>
32 #include <nsIDOMDocumentView.h>
33 #include <nsIDOMElement.h>
34 #include <nsIDOMEventTarget.h>
35 #include <nsIDOMHTMLDocument.h>
36 #include <nsIDOMMouseEvent.h>
37 #include <nsIDOMNSDocument.h>
38 #include <nsIDOMWindow.h>
39 #include <nsIEventStateManager.h>
40 #include <nsIInterfaceRequestorUtils.h>
41 #include <nsIURI.h> // required before nsILink.h
43 #include <nsIPrefBranch.h>
44 #if MOZ_VERSION_MAJOR > 1 || (MOZ_VERSION_MAJOR == 1 && MOZ_VERSION_MINOR >= 8)
45 # include <nsPresContext.h>
47 # include <nsIPresContext.h>
48 typedef nsIPresContext nsPresContext; // ugh
50 #include <nsIPrefService.h>
51 #include <nsIPresShell.h>
52 #if MOZ_VERSION_MAJOR > 1 || (MOZ_VERSION_MAJOR == 1 && MOZ_VERSION_MINOR >= 8)
53 # include <nsServiceManagerUtils.h>
55 # include <nsIServiceManagerUtils.h>
57 #include <nsIWebBrowser.h>
60 #include "browser_widget.hpp"
61 #include "child_iterator.hpp"
63 #include "generate_dvd.hpp"
64 #include "geometry.hpp"
65 #include "link_iterator.hpp"
66 #include "null_prompt_service.hpp"
67 #include "pixbufs.hpp"
68 #include "style_sheets.hpp"
69 #include "temp_file.hpp"
71 #include "warp_pointer.hpp"
72 #include "x_frame_buffer.hpp"
73 #include "xml_utils.hpp"
74 #include "xpcom_support.hpp"
76 using xpcom_support::check;
80 rectangle get_elem_rect(nsIDOMNSDocument * ns_doc,
85 // Start with this element's bounding box
86 nsCOMPtr<nsIBoxObject> box;
87 check(ns_doc->GetBoxObjectFor(elem, getter_AddRefs(box)));
89 check(box->GetScreenX(&result.left));
90 check(box->GetScreenY(&result.top));
91 check(box->GetWidth(&width));
92 check(box->GetHeight(&height));
93 result.right = result.left + width;
94 result.bottom = result.top + height;
96 // Merge bounding boxes of all child elements
97 for (child_iterator it = child_iterator(elem), end; it != end; ++it)
99 nsCOMPtr<nsIDOMNode> child_node(*it);
101 if (check(child_node->GetNodeType(&child_type)),
102 child_type == nsIDOMNode::ELEMENT_NODE)
104 nsCOMPtr<nsIDOMElement> child_elem(
105 do_QueryInterface(child_node));
106 result |= get_elem_rect(ns_doc, child_elem);
117 video_format_mpeg2_ps,
118 video_format_vob_list
121 video_format video_format_from_uri(const std::string & uri)
123 // FIXME: This is a bit of a hack. Perhaps we could decide
124 // later based on the MIME type determined by Mozilla?
126 const char * extension;
128 } const mapping[] = {
129 {".vob", video_format_mpeg2_ps},
130 {".mpeg", video_format_mpeg2_ps},
131 {".mpeg2", video_format_mpeg2_ps},
132 {".voblist", video_format_vob_list}
134 for (std::size_t i = 0;
135 i != sizeof(mapping) / sizeof(mapping[0]);
138 std::size_t ext_len = std::strlen(mapping[i].extension);
139 if (uri.size() > ext_len
140 && uri.compare(uri.size() - ext_len, ext_len,
141 mapping[i].extension) == 0)
142 return mapping[i].format;
144 return video_format_none;
148 class videolink_window : public Gtk::Window
152 const video::frame_params & frame_params,
153 const std::string & main_page_uri,
154 const std::string & output_dir,
155 dvd_generator::mpeg_encoder encoder);
157 bool is_finished() const;
162 dvd_generator::pgc_ref add_menu(const std::string & uri);
163 dvd_generator::pgc_ref add_title(const std::string & uri,
164 video_format format);
165 void load_next_page();
167 void on_net_state_change(const char * uri, gint flags, guint status);
168 bool browser_is_busy() const
170 return pending_window_update_ || pending_req_count_;
172 // Do as much processing as possible. Return a flag indicating
173 // whether to call again once the browser is idle.
175 // Return a Pixbuf containing a copy of the window contents.
176 Glib::RefPtr<Gdk::Pixbuf> get_screenshot();
177 // Do as much processing as possible on the page links. Return
178 // a flag indicating whether to call again once the browser is
182 nsIDOMDocument * basic_doc,
183 nsIPresShell * pres_shell,
184 nsPresContext * pres_context,
185 nsIDOMWindow * dom_window);
187 video::frame_params frame_params_;
188 std::string output_dir_;
189 browser_widget browser_widget_;
190 agent_style_sheet_holder main_style_sheet_, frame_style_sheet_;
192 dvd_generator generator_;
193 typedef std::map<std::string, dvd_generator::pgc_ref>
195 resource_map_type resource_map_;
197 std::queue<std::string> page_queue_;
198 bool pending_window_update_;
199 int pending_req_count_;
200 bool have_tweaked_page_;
201 std::auto_ptr<page_state> page_state_;
206 videolink_window::videolink_window(
207 const video::frame_params & frame_params,
208 const std::string & main_page_uri,
209 const std::string & output_dir,
210 dvd_generator::mpeg_encoder encoder)
211 : frame_params_(frame_params),
212 output_dir_(output_dir),
214 init_agent_style_sheet(
215 "file://" VIDEOLINK_SHARE_DIR "/videolink.css")),
217 init_agent_style_sheet(
218 std::string("file://" VIDEOLINK_SHARE_DIR "/")
219 .append(frame_params.common_name)
222 generator_(frame_params, encoder),
223 pending_window_update_(false),
224 pending_req_count_(0),
225 have_tweaked_page_(false),
228 set_size_request(frame_params_.width, frame_params_.height);
229 set_resizable(false);
231 add(browser_widget_);
232 browser_widget_.show();
233 Glib::signal_idle().connect(
234 SigC::slot(*this, &videolink_window::on_idle));
235 browser_widget_.signal_net_state().connect(
236 SigC::slot(*this, &videolink_window::on_net_state_change));
238 add_menu(main_page_uri);
241 bool videolink_window::is_finished() const
246 dvd_generator::pgc_ref videolink_window::add_menu(const std::string & uri)
248 dvd_generator::pgc_ref & pgc_ref = resource_map_[uri];
249 if (pgc_ref.type == dvd_generator::unknown_pgc)
251 pgc_ref = generator_.add_menu();
252 page_queue_.push(uri);
257 dvd_generator::pgc_ref videolink_window::add_title(const std::string & uri,
260 dvd_generator::pgc_ref & pgc_ref = resource_map_[uri];
262 if (pgc_ref.type == dvd_generator::unknown_pgc)
264 Glib::ustring hostname;
265 std::string path(Glib::filename_from_uri(uri, hostname));
266 // FIXME: Should check the hostname
270 // Store a reference to a linked VOB file, or the contents
271 // of a linked VOB list file.
272 if (format == video_format_mpeg2_ps)
274 if (!Glib::file_test(path, Glib::FILE_TEST_IS_REGULAR))
275 throw std::runtime_error(
276 path + " is missing or not a regular file");
281 else if (format == video_format_vob_list)
283 read_vob_list(path).swap(list);
287 assert(!"unrecognised format in add_title");
290 pgc_ref = generator_.add_title(list);
296 void videolink_window::load_next_page()
298 assert(!page_queue_.empty());
299 const std::string & uri = page_queue_.front();
300 std::cout << "loading " << uri << std::endl;
302 browser_widget_.load_uri(uri);
305 bool videolink_window::on_idle()
307 if (!output_dir_.empty())
309 // Put pointer in the top-left so that no links appear in
310 // the hover state when we take a screenshot.
311 warp_pointer(get_window(),
312 -frame_params_.width, -frame_params_.height);
316 return false; // don't call again thankyou
319 void videolink_window::on_net_state_change(const char * uri,
320 gint flags, guint status)
322 # ifdef DEBUG_ON_NET_STATE_CHANGE
323 std::cout << "videolink_window::on_net_state_change(";
325 std::cout << '"' << uri << '"';
330 gint flags_left = flags;
331 static const struct {
335 { GTK_MOZ_EMBED_FLAG_START, "STATE_START" },
336 { GTK_MOZ_EMBED_FLAG_REDIRECTING, "STATE_REDIRECTING" },
337 { GTK_MOZ_EMBED_FLAG_TRANSFERRING, "STATE_TRANSFERRING" },
338 { GTK_MOZ_EMBED_FLAG_NEGOTIATING, "STATE_NEGOTIATING" },
339 { GTK_MOZ_EMBED_FLAG_STOP, "STATE_STOP" },
340 { GTK_MOZ_EMBED_FLAG_IS_REQUEST, "STATE_IS_REQUEST" },
341 { GTK_MOZ_EMBED_FLAG_IS_DOCUMENT, "STATE_IS_DOCUMENT" },
342 { GTK_MOZ_EMBED_FLAG_IS_NETWORK, "STATE_IS_NETWORK" },
343 { GTK_MOZ_EMBED_FLAG_IS_WINDOW, "STATE_IS_WINDOW" }
345 for (int i = 0; i != sizeof(flag_names)/sizeof(flag_names[0]); ++i)
347 if (flags & flag_names[i].value)
349 std::cout << flag_names[i].name;
350 flags_left -= flag_names[i].value;
356 std::cout << "0x" << std::setbase(16) << flags_left;
358 std::cout << ", " << "0x" << std::setbase(16) << status << ")\n";
359 # endif // DEBUG_ON_NET_STATE_CHANGE
361 if (flags & GTK_MOZ_EMBED_FLAG_IS_REQUEST)
363 if (flags & GTK_MOZ_EMBED_FLAG_START)
364 ++pending_req_count_;
366 if (flags & GTK_MOZ_EMBED_FLAG_STOP)
368 assert(pending_req_count_ != 0);
369 --pending_req_count_;
373 if (flags & GTK_MOZ_EMBED_FLAG_IS_DOCUMENT
374 && flags & GTK_MOZ_EMBED_FLAG_START)
376 pending_window_update_ = true;
377 have_tweaked_page_ = false;
380 if (flags & GTK_MOZ_EMBED_FLAG_IS_WINDOW
381 && flags & GTK_MOZ_EMBED_FLAG_STOP)
383 // Check whether the load was successful, ignoring this
385 if (status != NS_IMAGELIB_ERROR_LOAD_ABORTED)
388 pending_window_update_ = false;
391 if (!browser_is_busy())
401 catch (std::exception & e)
403 std::cerr << "Fatal error";
404 if (!page_queue_.empty())
405 std::cerr << " while processing <" << page_queue_.front()
407 std::cerr << ": " << e.what() << "\n";
410 catch (Glib::Exception & e)
412 std::cerr << "Fatal error";
413 if (!page_queue_.empty())
414 std::cerr << " while processing <" << page_queue_.front()
416 std::cerr << ": " << e.what() << "\n";
422 struct videolink_window::page_state
424 page_state(Glib::RefPtr<Gdk::Pixbuf> norm_pixbuf,
425 nsIDOMDocument * doc, int width, int height)
426 : norm_pixbuf(norm_pixbuf),
427 diff_pixbuf(Gdk::Pixbuf::create(
429 true, 8, // has_alpha, bits_per_sample
436 Glib::RefPtr<Gdk::Pixbuf> norm_pixbuf;
437 Glib::RefPtr<Gdk::Pixbuf> diff_pixbuf;
439 link_iterator links_it, links_end;
445 bool videolink_window::process()
447 assert(!page_queue_.empty());
449 nsCOMPtr<nsIWebBrowser> browser(browser_widget_.get_browser());
450 nsCOMPtr<nsIDocShell> doc_shell(do_GetInterface(browser));
452 nsCOMPtr<nsIPresShell> pres_shell;
453 check(doc_shell->GetPresShell(getter_AddRefs(pres_shell)));
454 nsCOMPtr<nsPresContext> pres_context;
455 check(doc_shell->GetPresContext(getter_AddRefs(pres_context)));
456 nsCOMPtr<nsIDOMWindow> dom_window;
457 check(browser->GetContentDOMWindow(getter_AddRefs(dom_window)));
459 // If we haven't done so already, apply the stylesheet and
460 // disable scrollbars.
461 if (!have_tweaked_page_)
463 apply_agent_style_sheet(main_style_sheet_, pres_shell);
464 apply_agent_style_sheet(frame_style_sheet_, pres_shell);
466 // This actually only needs to be done once.
467 nsCOMPtr<nsIDOMBarProp> dom_bar_prop;
468 check(dom_window->GetScrollbars(getter_AddRefs(dom_bar_prop)));
469 check(dom_bar_prop->SetVisible(false));
471 have_tweaked_page_ = true;
473 // Might need to wait a while for things to load or more
474 // likely for a re-layout.
475 if (browser_is_busy())
479 // All further work should only be done if we're not in preview mode.
480 if (!output_dir_.empty())
482 nsCOMPtr<nsIDOMDocument> basic_doc;
483 check(dom_window->GetDocument(getter_AddRefs(basic_doc)));
485 // Start or continue processing links.
486 std::auto_ptr<page_state> state(page_state_);
491 basic_doc, frame_params_.width, frame_params_.height));
494 basic_doc, pres_shell, pres_context, dom_window))
496 // Save iteration state for later.
501 // We've finished work on the links so generate the
503 quantise_rgba_pixbuf(state->diff_pixbuf,
504 dvd::button_n_colours);
505 generator_.generate_menu_vob(
506 resource_map_[page_queue_.front()].index,
507 state->norm_pixbuf, state->diff_pixbuf);
509 // Move on to the next page, if any, or else generate
510 // the DVD filesystem.
512 if (!page_queue_.empty())
518 generator_.generate(output_dir_);
527 Glib::RefPtr<Gdk::Pixbuf> videolink_window::get_screenshot()
529 Glib::RefPtr<Gdk::Window> window(get_window());
531 window->process_updates(true);
533 return Gdk::Pixbuf::create(Glib::RefPtr<Gdk::Drawable>(window),
534 window->get_colormap(),
536 frame_params_.width, frame_params_.height);
539 bool videolink_window::process_links(
541 nsIDOMDocument * basic_doc,
542 nsIPresShell * pres_shell,
543 nsPresContext * pres_context,
544 nsIDOMWindow * dom_window)
546 Glib::RefPtr<Gdk::Window> window(get_window());
549 nsCOMPtr<nsIDOMNSDocument> ns_doc(do_QueryInterface(basic_doc));
551 nsCOMPtr<nsIEventStateManager> event_state_man(
552 pres_context->EventStateManager()); // does not AddRef
553 assert(event_state_man);
554 nsCOMPtr<nsIDOMDocumentEvent> event_factory(
555 do_QueryInterface(basic_doc));
556 assert(event_factory);
557 nsCOMPtr<nsIDOMDocumentView> doc_view(do_QueryInterface(basic_doc));
559 nsCOMPtr<nsIDOMAbstractView> view;
560 check(doc_view->GetDefaultView(getter_AddRefs(view)));
562 rectangle window_rect = {
563 0, 0, frame_params_.width, frame_params_.height
566 unsigned menu_index = resource_map_[page_queue_.front()].index;
568 for (/* no initialisation */;
569 state->links_it != state->links_end;
572 nsCOMPtr<nsIDOMNode> node(*state->links_it);
574 // Find the link URI and separate any fragment from it.
575 nsCOMPtr<nsILink> link(do_QueryInterface(node));
577 nsCOMPtr<nsIURI> uri_iface;
578 check(link->GetHrefURI(getter_AddRefs(uri_iface)));
579 std::string uri_and_fragment, uri, fragment;
581 nsCString uri_and_fragment_ns;
582 check(uri_iface->GetSpec(uri_and_fragment_ns));
583 uri_and_fragment.assign(uri_and_fragment_ns.BeginReading(),
584 uri_and_fragment_ns.EndReading());
586 std::size_t hash_pos = uri_and_fragment.find('#');
587 uri.assign(uri_and_fragment, 0, hash_pos);
588 if (hash_pos != std::string::npos)
589 fragment.assign(uri_and_fragment,
590 hash_pos + 1, std::string::npos);
593 // Is this a new link?
594 if (!state->link_changing)
596 // Find a rectangle enclosing the link and clip it to the
598 nsCOMPtr<nsIDOMElement> elem(do_QueryInterface(node));
600 state->link_rect = get_elem_rect(ns_doc, elem);
601 state->link_rect &= window_rect;
603 if (state->link_rect.empty())
605 std::cerr << "Ignoring invisible link to "
606 << uri_and_fragment << "\n";
610 // Check whether this is a link to a video or a page then
611 // add it to the known resources if not already seen; then
612 // add it to the menu entries.
613 dvd_generator::pgc_ref target;
614 video_format format = video_format_from_uri(uri);
615 if (format != video_format_none)
618 check(uri_iface->SchemeIs("file", &is_file));
621 std::cerr << "Links to video must use the file:"
625 target = add_title(uri, format);
627 std::strtoul(fragment.c_str(), NULL, 10);
629 else // video_format == video_format_none
631 target = add_menu(uri);
632 // TODO: If there's a fragment, work out which button
633 // is closest and set target.sub_index.
636 generator_.add_menu_entry(menu_index,
637 state->link_rect, target);
639 nsCOMPtr<nsIContent> content(do_QueryInterface(node));
641 nsCOMPtr<nsIDOMEventTarget> event_target(
642 do_QueryInterface(node));
643 assert(event_target);
645 nsCOMPtr<nsIDOMEvent> event;
646 check(event_factory->CreateEvent(
647 NS_ConvertASCIItoUTF16("MouseEvents"),
648 getter_AddRefs(event)));
649 nsCOMPtr<nsIDOMMouseEvent> mouse_event(
650 do_QueryInterface(event));
652 check(mouse_event->InitMouseEvent(
653 NS_ConvertASCIItoUTF16("mouseover"),
657 0, // detail: mouse click count
658 state->link_rect.left, // screenX
659 state->link_rect.top, // screenY
660 state->link_rect.left, // clientX
661 state->link_rect.top, // clientY
662 false, false, false, false, // qualifiers
663 0, // button: left (or primary)
664 0)); // related target
666 check(event_target->DispatchEvent(mouse_event,
668 check(event_state_man->SetContentState(content,
669 NS_EVENT_STATE_HOVER));
671 # if MOZ_VERSION_MAJOR > 1 \
672 || (MOZ_VERSION_MAJOR == 1 && MOZ_VERSION_MINOR >= 8)
673 pres_shell->FlushPendingNotifications(Flush_Display);
675 pres_shell->FlushPendingNotifications(true);
678 // We may have to exit and wait for image loading
679 // to complete, at which point we will be called
681 if (browser_is_busy())
683 state->link_changing = true;
688 window->process_updates(true);
690 Glib::RefPtr<Gdk::Pixbuf> changed_pixbuf(
692 Glib::RefPtr<Gdk::Drawable>(window),
693 window->get_colormap(),
694 state->link_rect.left,
695 state->link_rect.top,
698 state->link_rect.right - state->link_rect.left,
699 state->link_rect.bottom - state->link_rect.top));
704 state->link_rect.left,
705 state->link_rect.top,
706 state->link_rect.right - state->link_rect.left,
707 state->link_rect.bottom - state->link_rect.top);
713 const video::frame_params & lookup_frame_params(const char * str)
716 static const char * const known_strings[] = {
722 for (std::size_t i = 0;
723 i != sizeof(known_strings)/sizeof(known_strings[0]);
725 if (std::strcmp(str, known_strings[i]) == 0)
727 ? video::frame_params_625
728 : video::frame_params_525;
729 throw std::runtime_error(
730 std::string("Invalid video standard: ").append(str));
733 void print_usage(std::ostream & stream, const char * command_name)
736 "Usage: " << command_name << " [gtk-options] [--preview]\n"
737 " [--video-std {525|525/60|NTSC|ntsc"
738 " | 625|625/50|PAL|pal}]\n"
739 " [--encoder {mjpegtools|mjpegtools-old}]\n"
740 " menu-url [output-dir]\n";
743 void set_browser_preferences()
745 nsCOMPtr<nsIPrefService> pref_service;
746 static const nsCID pref_service_cid = NS_PREFSERVICE_CID;
747 check(CallGetService<nsIPrefService>(pref_service_cid,
748 getter_AddRefs(pref_service)));
749 nsCOMPtr<nsIPrefBranch> pref_branch;
750 check(pref_service->GetBranch("", getter_AddRefs(pref_branch)));
752 # if MOZ_VERSION_MAJOR > 1 \
753 || (MOZ_VERSION_MAJOR == 1 && MOZ_VERSION_MINOR >= 8)
754 // Disable IE-compatibility kluge that causes backgrounds to
755 // sometimes/usually be missing from snapshots. This is only
756 // effective from Mozilla 1.8 onward.
757 check(pref_branch->SetBoolPref(
758 "layout.fire_onload_after_image_background_loads",
762 // Set display resolution. With standard-definition video we
763 // will be fitting ~600 pixels across a screen typically
764 // ranging from 10 to 25 inches wide, for a resolution of
765 // 24-60 dpi. I therefore declare the average horizontal
766 // resolution to be 40 dpi. The vertical resolution will be
767 // slightly different but unfortunately Mozilla doesn't
768 // support non-square pixels (and neither do fontconfig or Xft
771 // The browser.display.screen_resolution preference sets the
772 // the nominal resolution for dimensions expressed in pixels.
773 // (They may be scaled!) In Mozilla 1.7 it also sets the
774 // assumed resolution of the display - hence pixel sizes are
775 // respected on-screen - but this is no longer the case in
776 // 1.8. Therefore it was renamed to layout.css.dpi in 1.8.1.
777 // In 1.8 we need to set the assumed screen resolution
778 // separately, but don't know how yet. Setting one to 40
779 // but not the other is *bad*, so currently we set neither.
781 # if MOZ_VERSION_MAJOR == 1 && MOZ_VERSION_MINOR < 8
782 check(pref_branch->SetIntPref("browser.display.screen_resolution",
789 void fatal_error(const std::string & message)
791 std::cerr << "Fatal error: " << message << "\n";
795 int main(int argc, char ** argv)
799 video::frame_params frame_params = video::frame_params_625;
800 bool preview_mode = false;
801 std::string menu_url;
802 std::string output_dir;
803 dvd_generator::mpeg_encoder encoder =
804 dvd_generator::mpeg_encoder_ffmpeg;
806 // Do initial option parsing. We have to do this before
807 // letting Gtk parse the arguments since we may need to spawn
812 if (std::strcmp(argv[argi], "--") == 0)
816 else if (std::strcmp(argv[argi], "--help") == 0)
818 print_usage(std::cout, argv[0]);
821 else if (std::strcmp(argv[argi], "--preview") == 0)
826 else if (std::strcmp(argv[argi], "--video-std") == 0)
828 if (argi + 1 == argc)
830 std::cerr << "Missing argument to --video-std\n";
831 print_usage(std::cerr, argv[0]);
834 frame_params = lookup_frame_params(argv[argi + 1]);
843 std::auto_ptr<x_frame_buffer> fb;
846 // Spawn Xvfb and set env variables so that Xlib will use it
847 // Use 8 bits each for RGB components, which should translate into
848 // "enough" bits for YUV components.
849 fb.reset(new x_frame_buffer(frame_params.width,
852 setenv("XAUTHORITY", fb->get_authority().c_str(), true);
853 setenv("DISPLAY", fb->get_display().c_str(), true);
857 Gtk::Main kit(argc, argv);
859 // Complete option parsing with Gtk's options out of the way.
863 if (std::strcmp(argv[argi], "--") == 0)
868 else if (std::strcmp(argv[argi], "--preview") == 0)
872 else if (std::strcmp(argv[argi], "--video-std") == 0)
876 else if (std::strcmp(argv[argi], "--save-temps") == 0)
878 temp_file::keep_all(true);
881 else if (std::strcmp(argv[argi], "--encoder") == 0)
883 if (argi + 1 == argc)
885 std::cerr << "Missing argument to --encoder\n";
886 print_usage(std::cerr, argv[0]);
889 if (std::strcmp(argv[argi + 1], "ffmpeg") == 0)
891 encoder = dvd_generator::mpeg_encoder_ffmpeg;
893 else if (std::strcmp(argv[argi + 1], "mjpegtools-old") == 0)
895 encoder = dvd_generator::mpeg_encoder_mjpegtools_old;
897 else if (std::strcmp(argv[argi + 1], "mjpegtools") == 0
898 || std::strcmp(argv[argi + 1], "mjpegtools-new") == 0)
900 encoder = dvd_generator::mpeg_encoder_mjpegtools_new;
904 std::cerr << "Invalid argument to --encoder\n";
905 print_usage(std::cerr, argv[0]);
910 else if (argv[argi][0] == '-')
912 std::cerr << "Invalid option: " << argv[argi] << "\n";
913 print_usage(std::cerr, argv[0]);
922 // Look for a starting URL or filename and (except in preview
923 // mode) an output directory after the options.
924 if (argc - argi != (preview_mode ? 1 : 2))
926 print_usage(std::cerr, argv[0]);
929 if (std::strstr(argv[argi], "://"))
931 // It appears to be an absolute URL, so use it as-is.
932 menu_url = argv[argi];
936 // Assume it's a filename. Resolve it to an absolute URL.
937 std::string path(argv[argi]);
938 if (!Glib::path_is_absolute(path))
939 path = Glib::build_filename(Glib::get_current_dir(), path);
940 menu_url = Glib::filename_to_uri(path);
943 output_dir = argv[argi + 1];
945 // Initialise Mozilla
946 browser_widget::initialiser browser_init;
947 set_browser_preferences();
949 null_prompt_service::install();
951 // Run the browser/converter
952 videolink_window window(frame_params, menu_url, output_dir, encoder);
954 window.signal_hide().connect(SigC::slot(&Gtk::Main::quit));
957 return ((preview_mode || window.is_finished())
961 catch (std::exception & e)
963 std::cerr << "Fatal error: " << e.what() << "\n";