1 // Copyright 2005-6 Ben Hutchings <ben@decadent.org.uk>.
2 // See the file "COPYING" for licence details.
18 #include <gdk/gdkkeysyms.h>
19 #include <gdkmm/pixbuf.h>
20 #include <glibmm/convert.h>
21 #include <glibmm/spawn.h>
22 #include <gtkmm/main.h>
23 #include <gtkmm/window.h>
25 #include <imglib2/ImageErrors.h>
26 #include <nsGUIEvent.h>
27 #include <nsIBoxObject.h>
28 #include <nsIContent.h>
29 #include <nsIDocShell.h>
30 #include <nsIDOMAbstractView.h>
31 #include <nsIDOMBarProp.h>
32 #include <nsIDOMDocumentEvent.h>
33 #include <nsIDOMDocumentView.h>
34 #include <nsIDOMElement.h>
35 #include <nsIDOMEventTarget.h>
36 #include <nsIDOMHTMLDocument.h>
37 #include <nsIDOMMouseEvent.h>
38 #include <nsIDOMNSDocument.h>
39 #include <nsIDOMWindow.h>
40 #include <nsIEventStateManager.h>
41 #include <nsIInterfaceRequestorUtils.h>
42 #include <nsIURI.h> // required before nsILink.h
44 #include <nsIPrefBranch.h>
45 #include <nsPresContext.h>
46 #include <nsIPrefService.h>
47 #include <nsIPresShell.h>
48 #include <nsServiceManagerUtils.h>
49 #include <nsIWebBrowser.h>
52 #include "browser_widget.hpp"
53 #include "child_iterator.hpp"
55 #include "generate_dvd.hpp"
56 #include "geometry.hpp"
57 #include "link_iterator.hpp"
58 #include "null_prompt_service.hpp"
59 #include "pixbufs.hpp"
60 #include "style_sheets.hpp"
61 #include "temp_file.hpp"
63 #include "warp_pointer.hpp"
64 #include "x_frame_buffer.hpp"
65 #include "xml_utils.hpp"
66 #include "xpcom_support.hpp"
68 using xpcom_support::check;
72 rectangle get_elem_rect(nsIDOMNSDocument * ns_doc,
77 // Start with this element's bounding box
78 nsCOMPtr<nsIBoxObject> box;
79 check(ns_doc->GetBoxObjectFor(elem, getter_AddRefs(box)));
81 check(box->GetScreenX(&result.left));
82 check(box->GetScreenY(&result.top));
83 check(box->GetWidth(&width));
84 check(box->GetHeight(&height));
85 result.right = result.left + width;
86 result.bottom = result.top + height;
88 // Merge bounding boxes of all child elements
89 for (child_iterator it = child_iterator(elem), end; it != end; ++it)
91 nsCOMPtr<nsIDOMNode> child_node(*it);
93 if (check(child_node->GetNodeType(&child_type)),
94 child_type == nsIDOMNode::ELEMENT_NODE)
96 nsCOMPtr<nsIDOMElement> child_elem(
97 do_QueryInterface(child_node));
98 result |= get_elem_rect(ns_doc, child_elem);
109 video_format_mpeg2_ps,
110 video_format_vob_list
113 video_format video_format_from_uri(const std::string & uri)
115 // FIXME: This is a bit of a hack. Perhaps we could decide
116 // later based on the MIME type determined by Mozilla?
118 const char * extension;
120 } const mapping[] = {
121 {".vob", video_format_mpeg2_ps},
122 {".mpeg", video_format_mpeg2_ps},
123 {".mpeg2", video_format_mpeg2_ps},
124 {".mpg", video_format_mpeg2_ps},
125 {".voblist", video_format_vob_list}
127 for (std::size_t i = 0;
128 i != sizeof(mapping) / sizeof(mapping[0]);
131 std::size_t ext_len = std::strlen(mapping[i].extension);
132 if (uri.size() > ext_len
133 && uri.compare(uri.size() - ext_len, ext_len,
134 mapping[i].extension) == 0)
135 return mapping[i].format;
137 return video_format_none;
141 class base_window : public Gtk::Window
144 base_window(const video::frame_params & frame_params);
147 video::frame_params frame_params_;
148 browser_widget browser_widget_;
152 virtual void do_late_initialisation() = 0;
155 base_window::base_window(const video::frame_params & frame_params)
156 : frame_params_(frame_params)
158 set_size_request(frame_params_.width, frame_params_.height);
159 set_resizable(false);
161 add(browser_widget_);
162 browser_widget_.show();
164 Glib::signal_idle().connect(
165 sigc::mem_fun(this, &base_window::on_idle));
168 bool base_window::on_idle()
170 do_late_initialisation();
171 return false; // don't call again thankyou
174 class preview_window : public base_window
177 preview_window(const video::frame_params & frame_params,
178 const std::string & main_page_uri);
181 virtual void do_late_initialisation();
182 bool on_key_press(GdkEventKey *);
184 std::string main_page_uri_;
187 preview_window::preview_window(const video::frame_params & frame_params,
188 const std::string & main_page_uri)
189 : base_window(frame_params),
190 main_page_uri_(main_page_uri)
192 signal_key_press_event().connect(
193 sigc::mem_fun(this, &preview_window::on_key_press));
196 void preview_window::do_late_initialisation()
198 browser_widget_.load_uri(main_page_uri_);
201 bool preview_window::on_key_press(GdkEventKey * event)
203 switch (event->keyval)
205 case GDK_t: // = top menu
206 browser_widget_.load_uri(main_page_uri_);
208 case GDK_q: // = quit
216 class conversion_window : public base_window
219 conversion_window(const video::frame_params & frame_params,
220 const std::string & main_page_uri,
221 const std::string & output_dir,
222 dvd_generator::mpeg_encoder encoder);
224 bool is_finished() const;
229 dvd_generator::pgc_ref add_menu(const std::string & uri);
230 dvd_generator::pgc_ref add_title(const std::string & uri,
231 video_format format);
232 void load_next_page();
233 void do_late_initialisation();
234 void on_net_state_change(const char * uri, gint flags, guint status);
235 bool browser_is_busy() const
237 return pending_window_update_ || pending_req_count_;
239 // Do as much processing as possible. Return a flag indicating
240 // whether to call again once the browser is idle.
242 // Return a Pixbuf containing a copy of the window contents.
243 Glib::RefPtr<Gdk::Pixbuf> get_screenshot();
244 // Do as much processing as possible on the page links. Return
245 // a flag indicating whether to call again once the browser is
249 nsIDOMDocument * basic_doc,
250 nsIPresShell * pres_shell,
251 nsPresContext * pres_context,
252 nsIDOMWindow * dom_window);
254 std::string output_dir_;
256 dvd_generator generator_;
257 typedef std::map<std::string, dvd_generator::pgc_ref>
259 resource_map_type resource_map_;
261 std::queue<std::string> page_queue_;
262 bool pending_window_update_;
263 int pending_req_count_;
264 std::auto_ptr<page_state> page_state_;
269 conversion_window::conversion_window(
270 const video::frame_params & frame_params,
271 const std::string & main_page_uri,
272 const std::string & output_dir,
273 dvd_generator::mpeg_encoder encoder)
274 : base_window(frame_params),
275 output_dir_(output_dir),
276 generator_(frame_params, encoder),
277 pending_window_update_(false),
278 pending_req_count_(0),
281 browser_widget_.signal_net_state().connect(
282 sigc::mem_fun(this, &conversion_window::on_net_state_change));
284 add_menu(main_page_uri);
287 bool conversion_window::is_finished() const
292 dvd_generator::pgc_ref conversion_window::add_menu(const std::string & uri)
294 dvd_generator::pgc_ref & pgc_ref = resource_map_[uri];
295 if (pgc_ref.type == dvd_generator::unknown_pgc)
297 pgc_ref = generator_.add_menu();
298 page_queue_.push(uri);
303 dvd_generator::pgc_ref conversion_window::add_title(const std::string & uri,
306 dvd_generator::pgc_ref & pgc_ref = resource_map_[uri];
308 if (pgc_ref.type == dvd_generator::unknown_pgc)
310 Glib::ustring hostname;
311 std::string path(Glib::filename_from_uri(uri, hostname));
312 // FIXME: Should check the hostname
316 // Store a reference to a linked VOB file, or the contents
317 // of a linked VOB list file.
318 if (format == video_format_mpeg2_ps)
320 if (!Glib::file_test(path, Glib::FILE_TEST_IS_REGULAR))
321 throw std::runtime_error(
322 path + " is missing or not a regular file");
327 else if (format == video_format_vob_list)
329 read_vob_list(path).swap(list);
333 assert(!"unrecognised format in add_title");
336 pgc_ref = generator_.add_title(list);
342 void conversion_window::load_next_page()
344 assert(!page_queue_.empty());
345 const std::string & uri = page_queue_.front();
346 std::cout << "INFO: Loading " << uri << std::endl;
348 browser_widget_.load_uri(uri);
351 void conversion_window::do_late_initialisation()
353 // Put pointer in the top-left so that no links appear in
354 // the hover state when we take a screenshot.
355 warp_pointer(get_window(),
356 -frame_params_.width, -frame_params_.height);
361 void conversion_window::on_net_state_change(const char * uri,
362 gint flags, guint status)
364 # ifdef DEBUG_ON_NET_STATE_CHANGE
365 std::cout << "conversion_window::on_net_state_change(";
367 std::cout << '"' << uri << '"';
372 gint flags_left = flags;
373 static const struct {
377 { GTK_MOZ_EMBED_FLAG_START, "STATE_START" },
378 { GTK_MOZ_EMBED_FLAG_REDIRECTING, "STATE_REDIRECTING" },
379 { GTK_MOZ_EMBED_FLAG_TRANSFERRING, "STATE_TRANSFERRING" },
380 { GTK_MOZ_EMBED_FLAG_NEGOTIATING, "STATE_NEGOTIATING" },
381 { GTK_MOZ_EMBED_FLAG_STOP, "STATE_STOP" },
382 { GTK_MOZ_EMBED_FLAG_IS_REQUEST, "STATE_IS_REQUEST" },
383 { GTK_MOZ_EMBED_FLAG_IS_DOCUMENT, "STATE_IS_DOCUMENT" },
384 { GTK_MOZ_EMBED_FLAG_IS_NETWORK, "STATE_IS_NETWORK" },
385 { GTK_MOZ_EMBED_FLAG_IS_WINDOW, "STATE_IS_WINDOW" }
387 for (int i = 0; i != sizeof(flag_names)/sizeof(flag_names[0]); ++i)
389 if (flags & flag_names[i].value)
391 std::cout << flag_names[i].name;
392 flags_left -= flag_names[i].value;
398 std::cout << "0x" << std::setbase(16) << flags_left;
400 std::cout << ", " << "0x" << std::setbase(16) << status << ")\n";
401 # endif // DEBUG_ON_NET_STATE_CHANGE
403 if (flags & GTK_MOZ_EMBED_FLAG_IS_REQUEST)
405 if (flags & GTK_MOZ_EMBED_FLAG_START)
406 ++pending_req_count_;
408 if (flags & GTK_MOZ_EMBED_FLAG_STOP)
410 assert(pending_req_count_ != 0);
411 --pending_req_count_;
415 if (flags & GTK_MOZ_EMBED_FLAG_IS_DOCUMENT
416 && flags & GTK_MOZ_EMBED_FLAG_START)
418 pending_window_update_ = true;
421 if (flags & GTK_MOZ_EMBED_FLAG_IS_WINDOW
422 && flags & GTK_MOZ_EMBED_FLAG_STOP)
424 // Check whether the load was successful, ignoring this
426 if (status != NS_IMAGELIB_ERROR_LOAD_ABORTED)
429 pending_window_update_ = false;
432 if (!browser_is_busy())
442 catch (std::exception & e)
444 if (!page_queue_.empty())
445 std::cerr << "ERROR: While processing <"
446 << page_queue_.front() << ">:\n";
447 std::cerr << "ERROR: " << e.what() << "\n";
450 catch (Glib::Exception & e)
452 if (!page_queue_.empty())
453 std::cerr << "ERROR: While processing <"
454 << page_queue_.front() << ">:\n";
455 std::cerr << "ERROR: " << e.what() << "\n";
461 struct conversion_window::page_state
463 page_state(Glib::RefPtr<Gdk::Pixbuf> norm_pixbuf,
464 nsIDOMDocument * doc, int width, int height)
465 : norm_pixbuf(norm_pixbuf),
466 diff_pixbuf(Gdk::Pixbuf::create(
468 true, 8, // has_alpha, bits_per_sample
475 Glib::RefPtr<Gdk::Pixbuf> norm_pixbuf;
476 Glib::RefPtr<Gdk::Pixbuf> diff_pixbuf;
478 link_iterator links_it, links_end;
484 bool conversion_window::process()
486 assert(!page_queue_.empty());
488 nsCOMPtr<nsIWebBrowser> browser(browser_widget_.get_browser());
489 nsCOMPtr<nsIDocShell> doc_shell(do_GetInterface(browser));
491 nsCOMPtr<nsIPresShell> pres_shell;
492 check(doc_shell->GetPresShell(getter_AddRefs(pres_shell)));
493 nsCOMPtr<nsPresContext> pres_context;
494 check(doc_shell->GetPresContext(getter_AddRefs(pres_context)));
495 nsCOMPtr<nsIDOMWindow> dom_window;
496 check(browser->GetContentDOMWindow(getter_AddRefs(dom_window)));
498 nsCOMPtr<nsIDOMDocument> basic_doc;
499 check(dom_window->GetDocument(getter_AddRefs(basic_doc)));
501 // Start or continue processing links.
502 std::auto_ptr<page_state> state(page_state_);
507 basic_doc, frame_params_.width, frame_params_.height));
510 basic_doc, pres_shell, pres_context, dom_window))
512 // Save iteration state for later.
517 // We've finished work on the links so generate the
519 quantise_rgba_pixbuf(state->diff_pixbuf,
520 dvd::button_n_colours);
521 generator_.generate_menu_vob(
522 resource_map_[page_queue_.front()].index,
523 state->norm_pixbuf, state->diff_pixbuf);
525 // Move on to the next page, if any, or else generate
526 // the DVD filesystem.
528 if (!page_queue_.empty())
534 generator_.generate(output_dir_);
542 Glib::RefPtr<Gdk::Pixbuf> conversion_window::get_screenshot()
544 Glib::RefPtr<Gdk::Window> window(get_window());
546 window->process_updates(true);
548 return Gdk::Pixbuf::create(Glib::RefPtr<Gdk::Drawable>(window),
549 window->get_colormap(),
551 frame_params_.width, frame_params_.height);
554 bool conversion_window::process_links(
556 nsIDOMDocument * basic_doc,
557 nsIPresShell * pres_shell,
558 nsPresContext * pres_context,
559 nsIDOMWindow * dom_window)
561 Glib::RefPtr<Gdk::Window> window(get_window());
564 nsCOMPtr<nsIDOMNSDocument> ns_doc(do_QueryInterface(basic_doc));
566 nsCOMPtr<nsIEventStateManager> event_state_man(
567 pres_context->EventStateManager()); // does not AddRef
568 assert(event_state_man);
569 nsCOMPtr<nsIDOMDocumentEvent> event_factory(
570 do_QueryInterface(basic_doc));
571 assert(event_factory);
572 nsCOMPtr<nsIDOMDocumentView> doc_view(do_QueryInterface(basic_doc));
574 nsCOMPtr<nsIDOMAbstractView> view;
575 check(doc_view->GetDefaultView(getter_AddRefs(view)));
577 rectangle window_rect = {
578 0, 0, frame_params_.width, frame_params_.height
581 unsigned menu_index = resource_map_[page_queue_.front()].index;
583 for (/* no initialisation */;
584 state->links_it != state->links_end;
587 nsCOMPtr<nsIDOMNode> node(*state->links_it);
589 // Find the link URI and separate any fragment from it.
590 nsCOMPtr<nsILink> link(do_QueryInterface(node));
592 nsCOMPtr<nsIURI> uri_iface;
593 check(link->GetHrefURI(getter_AddRefs(uri_iface)));
594 std::string uri_and_fragment, uri, fragment;
596 nsCString uri_and_fragment_ns;
597 check(uri_iface->GetSpec(uri_and_fragment_ns));
598 uri_and_fragment.assign(uri_and_fragment_ns.BeginReading(),
599 uri_and_fragment_ns.EndReading());
601 std::size_t hash_pos = uri_and_fragment.find('#');
602 uri.assign(uri_and_fragment, 0, hash_pos);
603 if (hash_pos != std::string::npos)
604 fragment.assign(uri_and_fragment,
605 hash_pos + 1, std::string::npos);
608 // Is this a new link?
609 if (!state->link_changing)
611 // Find a rectangle enclosing the link and clip it to the
613 nsCOMPtr<nsIDOMElement> elem(do_QueryInterface(node));
615 state->link_rect = get_elem_rect(ns_doc, elem);
616 state->link_rect &= window_rect;
618 if (state->link_rect.empty())
620 std::cerr << "WARN: Ignoring invisible link to "
621 << uri_and_fragment << "\n";
625 // Check whether this is a link to a video or a page then
626 // add it to the known resources if not already seen; then
627 // add it to the menu entries.
628 dvd_generator::pgc_ref target;
629 video_format format = video_format_from_uri(uri);
630 if (format != video_format_none)
633 check(uri_iface->SchemeIs("file", &is_file));
636 std::cerr << "WARN: Ignoring non-file link to "
637 << uri_and_fragment << "\n";
640 target = add_title(uri, format);
642 std::strtoul(fragment.c_str(), NULL, 10);
644 else // video_format == video_format_none
646 target = add_menu(uri);
647 // TODO: If there's a fragment, work out which button
648 // is closest and set target.sub_index.
651 generator_.add_menu_entry(menu_index,
652 state->link_rect, target);
654 nsCOMPtr<nsIContent> content(do_QueryInterface(node));
656 nsCOMPtr<nsIDOMEventTarget> event_target(
657 do_QueryInterface(node));
658 assert(event_target);
660 nsCOMPtr<nsIDOMEvent> event;
661 check(event_factory->CreateEvent(
662 NS_ConvertASCIItoUTF16("MouseEvents"),
663 getter_AddRefs(event)));
664 nsCOMPtr<nsIDOMMouseEvent> mouse_event(
665 do_QueryInterface(event));
667 check(mouse_event->InitMouseEvent(
668 NS_ConvertASCIItoUTF16("mouseover"),
672 0, // detail: mouse click count
673 state->link_rect.left, // screenX
674 state->link_rect.top, // screenY
675 state->link_rect.left, // clientX
676 state->link_rect.top, // clientY
677 false, false, false, false, // qualifiers
678 0, // button: left (or primary)
679 0)); // related target
681 check(event_target->DispatchEvent(mouse_event,
683 check(event_state_man->SetContentState(content,
684 NS_EVENT_STATE_HOVER));
686 pres_shell->FlushPendingNotifications(Flush_Display);
688 // We may have to exit and wait for image loading
689 // to complete, at which point we will be called
691 if (browser_is_busy())
693 state->link_changing = true;
698 window->process_updates(true);
700 Glib::RefPtr<Gdk::Pixbuf> changed_pixbuf(
702 Glib::RefPtr<Gdk::Drawable>(window),
703 window->get_colormap(),
704 state->link_rect.left,
705 state->link_rect.top,
708 state->link_rect.right - state->link_rect.left,
709 state->link_rect.bottom - state->link_rect.top));
714 state->link_rect.left,
715 state->link_rect.top,
716 state->link_rect.right - state->link_rect.left,
717 state->link_rect.bottom - state->link_rect.top);
723 const video::frame_params & lookup_frame_params(const char * str)
726 static const char * const known_strings[] = {
732 for (std::size_t i = 0;
733 i != sizeof(known_strings)/sizeof(known_strings[0]);
735 if (std::strcmp(str, known_strings[i]) == 0)
737 ? video::frame_params_625
738 : video::frame_params_525;
739 throw std::runtime_error(
740 std::string("Invalid video standard: ").append(str));
743 void print_usage(std::ostream & stream, const char * command_name)
746 "Usage: " << command_name << " [gtk-options] [--preview]\n"
747 " [--video-std {525|525/60|NTSC|ntsc"
748 " | 625|625/50|PAL|pal}]\n"
749 " [--encoder {mjpegtools|mjpegtools-old}]\n"
750 " menu-url [output-dir]\n";
753 void set_browser_preferences()
755 nsCOMPtr<nsIPrefService> pref_service;
756 static const nsCID pref_service_cid = NS_PREFSERVICE_CID;
757 check(CallGetService<nsIPrefService>(pref_service_cid,
758 getter_AddRefs(pref_service)));
759 nsCOMPtr<nsIPrefBranch> pref_branch;
760 check(pref_service->GetBranch("", getter_AddRefs(pref_branch)));
762 // Disable IE-compatibility kluge that causes backgrounds to
763 // sometimes/usually be missing from snapshots. This is only
764 // effective from Mozilla 1.8 onward.
765 check(pref_branch->SetBoolPref(
766 "layout.fire_onload_after_image_background_loads",
769 // Set display resolution. With standard-definition video we
770 // will be fitting ~600 pixels across a screen typically
771 // ranging from 10 to 25 inches wide, for a resolution of
772 // 24-60 dpi. I therefore declare the average horizontal
773 // resolution to be 40 dpi. The vertical resolution will be
774 // slightly different but unfortunately Mozilla doesn't
775 // support non-square pixels (and neither do fontconfig or Xft
778 // The browser.display.screen_resolution preference sets the
779 // the nominal resolution for dimensions expressed in pixels.
780 // (They may be scaled!) In Mozilla 1.7 it also sets the
781 // assumed resolution of the display - hence pixel sizes are
782 // respected on-screen - but this is no longer the case in
783 // 1.8. Therefore it was renamed to layout.css.dpi in 1.8.1.
784 // In 1.8 we need to set the assumed screen resolution
785 // separately, but don't know how yet. Setting one to 40
786 // but not the other is *bad*, so currently we set neither.
789 check(pref_branch->SetIntPref("browser.display.screen_resolution",
796 void fatal_error(const std::string & message)
798 std::cerr << "ERROR: " << message << "\n";
802 int main(int argc, char ** argv)
806 video::frame_params frame_params = video::frame_params_625;
807 bool preview_mode = false;
808 std::string menu_url;
809 std::string output_dir;
810 dvd_generator::mpeg_encoder encoder =
811 dvd_generator::mpeg_encoder_ffmpeg;
813 // Do initial option parsing. We have to do this before
814 // letting Gtk parse the arguments since we may need to spawn
819 if (std::strcmp(argv[argi], "--") == 0)
823 else if (std::strcmp(argv[argi], "--help") == 0)
825 print_usage(std::cout, argv[0]);
828 else if (std::strcmp(argv[argi], "--preview") == 0)
833 else if (std::strcmp(argv[argi], "--video-std") == 0)
835 if (argi + 1 == argc)
837 std::cerr << "Missing argument to --video-std\n";
838 print_usage(std::cerr, argv[0]);
841 frame_params = lookup_frame_params(argv[argi + 1]);
850 std::auto_ptr<x_frame_buffer> fb;
853 // Spawn Xvfb and set env variables so that Xlib will use it
854 // Use 8 bits each for RGB components, which should translate into
855 // "enough" bits for YUV components.
856 fb.reset(new x_frame_buffer(frame_params.width,
859 setenv("XAUTHORITY", fb->get_authority().c_str(), true);
860 setenv("DISPLAY", fb->get_display().c_str(), true);
864 Gtk::Main kit(argc, argv);
866 // Complete option parsing with Gtk's options out of the way.
870 if (std::strcmp(argv[argi], "--") == 0)
875 else if (std::strcmp(argv[argi], "--preview") == 0)
879 else if (std::strcmp(argv[argi], "--video-std") == 0)
883 else if (std::strcmp(argv[argi], "--save-temps") == 0)
885 temp_file::keep_all(true);
888 else if (std::strcmp(argv[argi], "--encoder") == 0)
890 if (argi + 1 == argc)
892 std::cerr << "Missing argument to --encoder\n";
893 print_usage(std::cerr, argv[0]);
896 if (std::strcmp(argv[argi + 1], "ffmpeg") == 0)
898 encoder = dvd_generator::mpeg_encoder_ffmpeg;
900 else if (std::strcmp(argv[argi + 1], "mjpegtools-old") == 0)
902 encoder = dvd_generator::mpeg_encoder_mjpegtools_old;
904 else if (std::strcmp(argv[argi + 1], "mjpegtools") == 0
905 || std::strcmp(argv[argi + 1], "mjpegtools-new") == 0)
907 encoder = dvd_generator::mpeg_encoder_mjpegtools_new;
911 std::cerr << "Invalid argument to --encoder\n";
912 print_usage(std::cerr, argv[0]);
917 else if (argv[argi][0] == '-')
919 std::cerr << "Invalid option: " << argv[argi] << "\n";
920 print_usage(std::cerr, argv[0]);
929 // Look for a starting URL or filename and (except in preview
930 // mode) an output directory after the options.
931 if (argc - argi != (preview_mode ? 1 : 2))
933 print_usage(std::cerr, argv[0]);
936 if (std::strstr(argv[argi], "://"))
938 // It appears to be an absolute URL, so use it as-is.
939 menu_url = argv[argi];
943 // Assume it's a filename. Resolve it to an absolute URL.
944 std::string path(argv[argi]);
945 if (!Glib::path_is_absolute(path))
946 path = Glib::build_filename(Glib::get_current_dir(), path);
947 menu_url = Glib::filename_to_uri(path);
950 output_dir = argv[argi + 1];
952 // Initialise Mozilla
953 browser_widget::initialiser browser_init;
954 set_browser_preferences();
955 init_agent_style_sheet("file://" VIDEOLINK_SHARE_DIR "/videolink.css");
956 init_agent_style_sheet(std::string("file://" VIDEOLINK_SHARE_DIR "/")
957 .append(frame_params.common_name).append(".css")
960 null_prompt_service::install();
962 // Run the browser/converter
965 preview_window window(frame_params, menu_url);
967 window.signal_hide().connect(sigc::ptr_fun(Gtk::Main::quit));
973 conversion_window window(frame_params, menu_url, output_dir, encoder);
975 window.signal_hide().connect(sigc::ptr_fun(Gtk::Main::quit));
977 return window.is_finished() ? EXIT_SUCCESS : EXIT_FAILURE;
980 catch (std::exception & e)
982 std::cerr << "ERROR: " << e.what() << "\n";