1 // Copyright 2005-8 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 "wchar_t_short.h"
26 #include <ImageErrors.h>
27 #if MOZ_VERSION_MAJOR == 1 && MOZ_VERSION_MINOR == 9
28 #include <nsWeakPtr.h>
29 /* For some reason <nsWeakPtr.h> no longer defines this */
30 typedef nsCOMPtr<nsIWeakReference> nsWeakPtr;
32 #include <nsGUIEvent.h>
33 #include <nsIBoxObject.h>
34 #include <nsIContent.h>
35 #include <nsIDocShell.h>
36 #include <nsIDOMAbstractView.h>
37 #include <nsIDOMBarProp.h>
38 #include <nsIDOMDocumentEvent.h>
39 #include <nsIDOMDocumentView.h>
40 #include <nsIDOMElement.h>
41 #include <nsIDOMEventTarget.h>
42 #include <nsIDOMHTMLDocument.h>
43 #include <nsIDOMMouseEvent.h>
44 #include <nsIDOMNSDocument.h>
45 #include <nsIDOMWindow.h>
46 #include <nsIEventStateManager.h>
47 #include <nsIInterfaceRequestorUtils.h>
48 #include <nsIURI.h> // required before nsILink.h
50 #include <nsIPrefBranch.h>
51 #include <nsIPrefService.h>
52 #include <nsIPresShell.h>
53 #include <nsServiceManagerUtils.h>
54 #include <nsIWebBrowser.h>
55 #ifdef MOZILLA_INTERNAL_API
58 #include <nsStringAPI.h>
60 #include "wchar_t_default.h"
62 #include "browser_widget.hpp"
63 #include "child_iterator.hpp"
65 #include "event_state_manager.hpp"
66 #include "generate_dvd.hpp"
67 #include "geometry.hpp"
68 #include "link_iterator.hpp"
69 #include "null_prompt_service.hpp"
70 #include "pixbufs.hpp"
71 #include "style_sheets.hpp"
72 #include "temp_file.hpp"
74 #include "warp_pointer.hpp"
75 #include "x_frame_buffer.hpp"
76 #include "xml_utils.hpp"
77 #include "xpcom_support.hpp"
79 using xpcom_support::check;
83 rectangle get_elem_rect(nsIDOMNSDocument * ns_doc,
88 // Start with this element's bounding box
89 nsCOMPtr<nsIBoxObject> box;
90 check(ns_doc->GetBoxObjectFor(elem, getter_AddRefs(box)));
92 check(box->GetScreenX(&result.left));
93 check(box->GetScreenY(&result.top));
94 check(box->GetWidth(&width));
95 check(box->GetHeight(&height));
96 result.right = result.left + width;
97 result.bottom = result.top + height;
99 // Merge bounding boxes of all child elements
100 for (child_iterator it = child_iterator(elem), end; it != end; ++it)
102 nsCOMPtr<nsIDOMNode> child_node(*it);
104 if (check(child_node->GetNodeType(&child_type)),
105 child_type == nsIDOMNode::ELEMENT_NODE)
107 nsCOMPtr<nsIDOMElement> child_elem(
108 do_QueryInterface(child_node));
109 result |= get_elem_rect(ns_doc, child_elem);
120 video_format_mpeg2_ps,
121 video_format_vob_list
124 video_format video_format_from_uri(const std::string & uri)
126 // FIXME: This is a bit of a hack. Perhaps we could decide
127 // later based on the MIME type determined by Mozilla?
129 const char * extension;
131 } const mapping[] = {
132 {".vob", video_format_mpeg2_ps},
133 {".mpeg", video_format_mpeg2_ps},
134 {".mpeg2", video_format_mpeg2_ps},
135 {".mpg", video_format_mpeg2_ps},
136 {".voblist", video_format_vob_list}
138 for (std::size_t i = 0;
139 i != sizeof(mapping) / sizeof(mapping[0]);
142 std::size_t ext_len = std::strlen(mapping[i].extension);
143 if (uri.size() > ext_len
144 && uri.compare(uri.size() - ext_len, ext_len,
145 mapping[i].extension) == 0)
146 return mapping[i].format;
148 return video_format_none;
152 class base_window : public Gtk::Window
155 base_window(const video::frame_params & frame_params);
158 video::frame_params frame_params_;
159 browser_widget browser_widget_;
162 base_window::base_window(const video::frame_params & frame_params)
163 : frame_params_(frame_params)
165 set_size_request(frame_params_.width, frame_params_.height);
166 set_resizable(false);
168 add(browser_widget_);
169 browser_widget_.show();
172 class preview_window : public base_window
175 preview_window(const video::frame_params & frame_params,
176 const std::string & main_page_uri);
180 bool on_key_press(GdkEventKey *);
182 std::string main_page_uri_;
185 preview_window::preview_window(const video::frame_params & frame_params,
186 const std::string & main_page_uri)
187 : base_window(frame_params),
188 main_page_uri_(main_page_uri)
190 Glib::signal_idle().connect(
191 sigc::mem_fun(this, &preview_window::on_idle));
192 signal_key_press_event().connect(
193 sigc::mem_fun(this, &preview_window::on_key_press));
196 bool preview_window::on_idle()
198 browser_widget_.load_uri(main_page_uri_);
199 return false; // don't call again
202 bool preview_window::on_key_press(GdkEventKey * event)
204 switch (event->keyval)
206 case GDK_t: // = top menu
207 browser_widget_.load_uri(main_page_uri_);
209 case GDK_q: // = quit
217 class conversion_window : public base_window
220 conversion_window(const video::frame_params & frame_params,
221 const std::string & main_page_uri,
222 const std::string & output_dir,
223 dvd_generator::mpeg_encoder encoder);
225 bool is_finished() const;
230 dvd_generator::pgc_ref add_menu(const std::string & uri);
231 dvd_generator::pgc_ref add_title(const std::string & uri,
232 video_format format);
233 void load_next_page();
235 void on_net_state_change(const char * uri, gint flags, guint status);
236 bool browser_is_busy() const
238 return pending_window_update_ || pending_req_count_;
240 // Do as much processing as possible. Return a flag indicating
241 // whether to call again once the browser is idle.
243 // Return a Pixbuf containing a copy of the window contents.
244 Glib::RefPtr<Gdk::Pixbuf> get_screenshot();
245 // Do as much processing as possible on the page links. Return
246 // a flag indicating whether to call again once the browser is
250 nsIDOMDocument * basic_doc,
251 nsIDocShell * doc_shell,
252 nsIDOMWindow * dom_window);
254 std::string output_dir_;
262 dvd_generator generator_;
263 typedef std::map<std::string, dvd_generator::pgc_ref>
265 resource_map_type resource_map_;
267 std::queue<std::string> page_queue_;
268 bool pending_window_update_;
269 int pending_req_count_;
270 std::auto_ptr<page_state> page_state_;
273 conversion_window::conversion_window(
274 const video::frame_params & frame_params,
275 const std::string & main_page_uri,
276 const std::string & output_dir,
277 dvd_generator::mpeg_encoder encoder)
278 : base_window(frame_params),
279 output_dir_(output_dir),
280 state_(state_initial),
281 generator_(frame_params, encoder),
282 pending_window_update_(false),
283 pending_req_count_(0)
285 Glib::signal_idle().connect(
286 sigc::mem_fun(this, &conversion_window::on_idle));
287 browser_widget_.signal_net_state().connect(
288 sigc::mem_fun(this, &conversion_window::on_net_state_change));
290 add_menu(main_page_uri);
293 bool conversion_window::is_finished() const
295 return state_ == state_finished;
298 dvd_generator::pgc_ref conversion_window::add_menu(const std::string & uri)
300 dvd_generator::pgc_ref & pgc_ref = resource_map_[uri];
301 if (pgc_ref.type == dvd_generator::unknown_pgc)
303 pgc_ref = generator_.add_menu();
304 page_queue_.push(uri);
309 dvd_generator::pgc_ref conversion_window::add_title(const std::string & uri,
312 dvd_generator::pgc_ref & pgc_ref = resource_map_[uri];
314 if (pgc_ref.type == dvd_generator::unknown_pgc)
316 Glib::ustring hostname;
317 std::string path(Glib::filename_from_uri(uri, hostname));
318 // FIXME: Should check the hostname
322 // Store a reference to a linked VOB file, or the contents
323 // of a linked VOB list file.
324 if (format == video_format_mpeg2_ps)
326 if (!Glib::file_test(path, Glib::FILE_TEST_IS_REGULAR))
327 throw std::runtime_error(
328 path + " is missing or not a regular file");
333 else if (format == video_format_vob_list)
335 read_vob_list(path).swap(list);
339 assert(!"unrecognised format in add_title");
342 pgc_ref = generator_.add_title(list);
348 void conversion_window::load_next_page()
350 assert(!page_queue_.empty());
351 const std::string & uri = page_queue_.front();
352 std::cout << "INFO: Loading <" << uri << ">" << std::endl;
354 browser_widget_.load_uri(uri);
357 void conversion_window::on_net_state_change(const char * uri,
358 gint flags, guint status)
360 # ifdef DEBUG_ON_NET_STATE_CHANGE
361 std::cout << "conversion_window::on_net_state_change(";
363 std::cout << '"' << uri << '"';
368 gint flags_left = flags;
369 static const struct {
373 { GTK_MOZ_EMBED_FLAG_START, "STATE_START" },
374 { GTK_MOZ_EMBED_FLAG_REDIRECTING, "STATE_REDIRECTING" },
375 { GTK_MOZ_EMBED_FLAG_TRANSFERRING, "STATE_TRANSFERRING" },
376 { GTK_MOZ_EMBED_FLAG_NEGOTIATING, "STATE_NEGOTIATING" },
377 { GTK_MOZ_EMBED_FLAG_STOP, "STATE_STOP" },
378 { GTK_MOZ_EMBED_FLAG_IS_REQUEST, "STATE_IS_REQUEST" },
379 { GTK_MOZ_EMBED_FLAG_IS_DOCUMENT, "STATE_IS_DOCUMENT" },
380 { GTK_MOZ_EMBED_FLAG_IS_NETWORK, "STATE_IS_NETWORK" },
381 { GTK_MOZ_EMBED_FLAG_IS_WINDOW, "STATE_IS_WINDOW" }
383 for (int i = 0; i != sizeof(flag_names)/sizeof(flag_names[0]); ++i)
385 if (flags & flag_names[i].value)
387 std::cout << flag_names[i].name;
388 flags_left -= flag_names[i].value;
394 std::cout << "0x" << std::setbase(16) << flags_left;
396 std::cout << ", " << "0x" << std::setbase(16) << status << ")\n";
397 # endif // DEBUG_ON_NET_STATE_CHANGE
399 if (flags & GTK_MOZ_EMBED_FLAG_IS_REQUEST)
401 if (flags & GTK_MOZ_EMBED_FLAG_START)
402 ++pending_req_count_;
404 if (flags & GTK_MOZ_EMBED_FLAG_STOP)
406 assert(pending_req_count_ != 0);
407 --pending_req_count_;
411 if (flags & GTK_MOZ_EMBED_FLAG_IS_DOCUMENT
412 && flags & GTK_MOZ_EMBED_FLAG_START)
414 pending_window_update_ = true;
417 if (flags & GTK_MOZ_EMBED_FLAG_IS_WINDOW
418 && flags & GTK_MOZ_EMBED_FLAG_STOP)
420 // Check whether the load was successful, ignoring this
422 if (status != NS_IMAGELIB_ERROR_LOAD_ABORTED)
425 pending_window_update_ = false;
429 struct conversion_window::page_state
431 page_state(Glib::RefPtr<Gdk::Pixbuf> norm_pixbuf,
432 nsIDOMDocument * doc, int width, int height)
433 : norm_pixbuf(norm_pixbuf),
434 diff_pixbuf(Gdk::Pixbuf::create(
436 true, 8, // has_alpha, bits_per_sample
443 Glib::RefPtr<Gdk::Pixbuf> norm_pixbuf;
444 Glib::RefPtr<Gdk::Pixbuf> diff_pixbuf;
446 link_iterator links_it, links_end;
449 std::string link_target;
453 bool conversion_window::on_idle()
455 if (state_ == state_initial)
457 // Put pointer in the top-left so that no links appear in
458 // the hover state when we take a screenshot.
459 warp_pointer(get_window(),
460 -frame_params_.width, -frame_params_.height);
464 state_ = state_processing;
466 else if (state_ == state_processing && !browser_is_busy())
472 state_ = state_finished;
478 // Print context of exception.
479 if (!page_queue_.empty())
481 std::cerr << "ERROR: While processing page <"
482 << page_queue_.front() << ">:\n";
483 if (page_state_.get() && !page_state_->link_target.empty())
484 std::cerr << "ERROR: While processing link to <"
485 << page_state_->link_target << ">:\n";
488 // Print exception message.
493 catch (std::exception & e)
495 std::cerr << "ERROR: " << e.what() << "\n";
497 catch (Glib::Exception & e)
499 std::cerr << "ERROR: " << e.what() << "\n";
503 std::cerr << "ERROR: Unknown exception\n";
510 // Call again if we're not done.
511 return state_ != state_finished;
514 bool conversion_window::process()
516 assert(!page_queue_.empty());
518 nsCOMPtr<nsIWebBrowser> browser(browser_widget_.get_browser());
519 nsCOMPtr<nsIDocShell> doc_shell(do_GetInterface(browser));
521 nsCOMPtr<nsIDOMWindow> dom_window;
522 check(browser->GetContentDOMWindow(getter_AddRefs(dom_window)));
524 nsCOMPtr<nsIDOMDocument> basic_doc;
525 check(dom_window->GetDocument(getter_AddRefs(basic_doc)));
527 // Start or continue processing links.
528 if (!page_state_.get())
532 basic_doc, frame_params_.width, frame_params_.height));
533 if (!process_links(page_state_.get(), basic_doc, doc_shell, dom_window))
535 // We've finished work on the links so generate the
537 quantise_rgba_pixbuf(page_state_->diff_pixbuf,
538 dvd::button_n_colours);
539 generator_.generate_menu_vob(
540 resource_map_[page_queue_.front()].index,
541 page_state_->norm_pixbuf, page_state_->diff_pixbuf);
543 // Move on to the next page, if any, or else generate
544 // the DVD filesystem.
547 if (!page_queue_.empty())
553 generator_.generate(output_dir_);
561 Glib::RefPtr<Gdk::Pixbuf> conversion_window::get_screenshot()
563 Glib::RefPtr<Gdk::Window> window(get_window());
565 window->process_updates(true);
567 return Gdk::Pixbuf::create(Glib::RefPtr<Gdk::Drawable>(window),
568 window->get_colormap(),
570 frame_params_.width, frame_params_.height);
573 bool conversion_window::process_links(
575 nsIDOMDocument * basic_doc,
576 nsIDocShell * doc_shell,
577 nsIDOMWindow * dom_window)
579 Glib::RefPtr<Gdk::Window> window(get_window());
582 nsCOMPtr<nsIDOMNSDocument> ns_doc(do_QueryInterface(basic_doc));
584 nsCOMPtr<nsIPresShell> pres_shell;
585 check(doc_shell->GetPresShell(getter_AddRefs(pres_shell)));
586 nsCOMPtr<nsIEventStateManager> event_state_man(
587 get_event_state_manager(doc_shell));
588 assert(event_state_man);
589 nsCOMPtr<nsIDOMDocumentEvent> event_factory(
590 do_QueryInterface(basic_doc));
591 assert(event_factory);
592 nsCOMPtr<nsIDOMDocumentView> doc_view(do_QueryInterface(basic_doc));
594 nsCOMPtr<nsIDOMAbstractView> view;
595 check(doc_view->GetDefaultView(getter_AddRefs(view)));
597 rectangle window_rect = {
598 0, 0, frame_params_.width, frame_params_.height
601 unsigned menu_index = resource_map_[page_queue_.front()].index;
603 for (/* no initialisation */;
604 state->links_it != state->links_end;
607 nsCOMPtr<nsIDOMNode> node(*state->links_it);
609 // Find the link URI and separate any fragment from it.
610 nsCOMPtr<nsILink> link(do_QueryInterface(node));
612 nsCOMPtr<nsIURI> uri_iface;
613 check(link->GetHrefURI(getter_AddRefs(uri_iface)));
614 std::string uri, fragment;
616 nsCString link_target_ns;
617 check(uri_iface->GetSpec(link_target_ns));
619 PRUint32 len = NS_CStringGetData(link_target_ns, &str);
620 state->link_target.assign(str, len);
622 std::size_t hash_pos = state->link_target.find('#');
623 uri.assign(state->link_target, 0, hash_pos);
624 if (hash_pos != std::string::npos)
625 fragment.assign(state->link_target,
626 hash_pos + 1, std::string::npos);
629 // Is this a new link?
630 if (!state->link_changing)
632 // Find a rectangle enclosing the link and clip it to the
634 nsCOMPtr<nsIDOMElement> elem(do_QueryInterface(node));
636 state->link_rect = get_elem_rect(ns_doc, elem);
637 state->link_rect &= window_rect;
639 if (state->link_rect.empty())
641 std::cerr << "WARN: Ignoring invisible link to <"
642 << state->link_target << ">\n";
646 // Check whether this is a link to a video or a page then
647 // add it to the known resources if not already seen; then
648 // add it to the menu entries.
649 dvd_generator::pgc_ref target;
650 video_format format = video_format_from_uri(uri);
651 if (format != video_format_none)
654 check(uri_iface->SchemeIs("file", &is_file));
656 throw std::runtime_error(
657 "Link to video does not use file: scheme");
658 target = add_title(uri, format);
660 std::strtoul(fragment.c_str(), NULL, 10);
662 else // video_format == video_format_none
664 target = add_menu(uri);
665 // TODO: If there's a fragment, work out which button
666 // is closest and set target.sub_index.
669 generator_.add_menu_entry(menu_index,
670 state->link_rect, target);
672 nsCOMPtr<nsIContent> content(do_QueryInterface(node));
674 nsCOMPtr<nsIDOMEventTarget> event_target(
675 do_QueryInterface(node));
676 assert(event_target);
678 nsCOMPtr<nsIDOMEvent> event;
679 check(event_factory->CreateEvent(
680 NS_ConvertASCIItoUTF16("MouseEvents"),
681 getter_AddRefs(event)));
682 nsCOMPtr<nsIDOMMouseEvent> mouse_event(
683 do_QueryInterface(event));
685 check(mouse_event->InitMouseEvent(
686 NS_ConvertASCIItoUTF16("mouseover"),
690 0, // detail: mouse click count
691 state->link_rect.left, // screenX
692 state->link_rect.top, // screenY
693 state->link_rect.left, // clientX
694 state->link_rect.top, // clientY
695 false, false, false, false, // qualifiers
696 0, // button: left (or primary)
697 0)); // related target
699 check(event_target->DispatchEvent(mouse_event,
701 check(event_state_man->SetContentState(content,
702 NS_EVENT_STATE_HOVER));
704 pres_shell->FlushPendingNotifications(Flush_Display);
706 // We may have to exit and wait for image loading
707 // to complete, at which point we will be called
709 if (browser_is_busy())
711 state->link_changing = true;
716 window->process_updates(true);
718 Glib::RefPtr<Gdk::Pixbuf> changed_pixbuf(
720 Glib::RefPtr<Gdk::Drawable>(window),
721 window->get_colormap(),
722 state->link_rect.left,
723 state->link_rect.top,
726 state->link_rect.right - state->link_rect.left,
727 state->link_rect.bottom - state->link_rect.top));
732 state->link_rect.left,
733 state->link_rect.top,
734 state->link_rect.right - state->link_rect.left,
735 state->link_rect.bottom - state->link_rect.top);
741 const video::frame_params & lookup_frame_params(const char * str)
744 static const char * const known_strings[] = {
750 for (std::size_t i = 0;
751 i != sizeof(known_strings)/sizeof(known_strings[0]);
753 if (std::strcmp(str, known_strings[i]) == 0)
755 ? video::frame_params_625
756 : video::frame_params_525;
757 throw std::runtime_error(
758 std::string("Invalid video standard: ").append(str));
761 void print_usage(std::ostream & stream, const char * command_name)
764 "Usage: " << command_name << " [gtk-options] [--preview]\n"
765 " [--video-std {525|525/60|NTSC|ntsc"
766 " | 625|625/50|PAL|pal}]\n"
767 " [--encoder {ffmpeg|mjpegtools}]\n"
768 " menu-url [output-dir]\n";
771 void set_browser_preferences()
773 nsCOMPtr<nsIPrefService> pref_service;
774 static const nsCID pref_service_cid = NS_PREFSERVICE_CID;
775 check(CallGetService<nsIPrefService>(pref_service_cid,
776 getter_AddRefs(pref_service)));
777 nsCOMPtr<nsIPrefBranch> pref_branch;
778 check(pref_service->GetBranch("", getter_AddRefs(pref_branch)));
780 // Disable IE-compatibility kluge that causes backgrounds to
781 // sometimes/usually be missing from snapshots. This is only
782 // effective from Mozilla 1.8 onward.
783 check(pref_branch->SetBoolPref(
784 "layout.fire_onload_after_image_background_loads",
787 // Set display resolution. With standard-definition video we
788 // will be fitting ~600 pixels across a screen typically
789 // ranging from 10 to 25 inches wide, for a resolution of
790 // 24-60 dpi. I therefore declare the average horizontal
791 // resolution to be 40 dpi. The vertical resolution will be
792 // slightly different but unfortunately Mozilla doesn't
793 // support non-square pixels (and neither do fontconfig or Xft
796 // The browser.display.screen_resolution preference sets the
797 // the nominal resolution for dimensions expressed in pixels.
798 // (They may be scaled!) In Mozilla 1.7 it also sets the
799 // assumed resolution of the display - hence pixel sizes are
800 // respected on-screen - but this is no longer the case in
801 // 1.8. Therefore it was renamed to layout.css.dpi in 1.8.1.
802 // In 1.8 we need to set the assumed screen resolution
803 // separately, but don't know how yet. Setting one to 40
804 // but not the other is *bad*, so currently we set neither.
807 check(pref_branch->SetIntPref("browser.display.screen_resolution",
814 void fatal_error(const std::string & message)
816 std::cerr << "ERROR: " << message << "\n";
820 int main(int argc, char ** argv)
824 video::frame_params frame_params = video::frame_params_625;
825 bool preview_mode = false;
826 std::string menu_url;
827 std::string output_dir;
828 dvd_generator::mpeg_encoder encoder =
829 dvd_generator::mpeg_encoder_ffmpeg;
831 // Do initial option parsing. We have to do this before
832 // letting Gtk parse the arguments since we may need to spawn
837 if (std::strcmp(argv[argi], "--") == 0)
841 else if (std::strcmp(argv[argi], "--help") == 0)
843 print_usage(std::cout, argv[0]);
846 else if (std::strcmp(argv[argi], "--preview") == 0)
851 else if (std::strcmp(argv[argi], "--video-std") == 0)
853 if (argi + 1 == argc)
855 std::cerr << "Missing argument to --video-std\n";
856 print_usage(std::cerr, argv[0]);
859 frame_params = lookup_frame_params(argv[argi + 1]);
868 std::auto_ptr<x_frame_buffer> fb;
871 // Spawn Xvfb and set env variables so that Xlib will use it
872 // Use 8 bits each for RGB components, which should translate into
873 // "enough" bits for YUV components.
874 fb.reset(new x_frame_buffer(frame_params.width,
877 setenv("XAUTHORITY", fb->get_authority().c_str(), true);
878 setenv("DISPLAY", fb->get_display().c_str(), true);
882 Gtk::Main kit(argc, argv);
884 // Complete option parsing with Gtk's options out of the way.
888 if (std::strcmp(argv[argi], "--") == 0)
893 else if (std::strcmp(argv[argi], "--preview") == 0)
897 else if (std::strcmp(argv[argi], "--video-std") == 0)
901 else if (std::strcmp(argv[argi], "--save-temps") == 0)
903 temp_file::keep_all(true);
906 else if (std::strcmp(argv[argi], "--encoder") == 0)
908 if (argi + 1 == argc)
910 std::cerr << "Missing argument to --encoder\n";
911 print_usage(std::cerr, argv[0]);
914 if (std::strcmp(argv[argi + 1], "ffmpeg") == 0)
916 encoder = dvd_generator::mpeg_encoder_ffmpeg;
918 else if (std::strcmp(argv[argi + 1], "mjpegtools") == 0)
920 encoder = dvd_generator::mpeg_encoder_mjpegtools;
924 std::cerr << "Invalid argument to --encoder\n";
925 print_usage(std::cerr, argv[0]);
930 else if (argv[argi][0] == '-')
932 std::cerr << "Invalid option: " << argv[argi] << "\n";
933 print_usage(std::cerr, argv[0]);
942 // Look for a starting URL or filename and (except in preview
943 // mode) an output directory after the options.
944 if (argc - argi != (preview_mode ? 1 : 2))
946 print_usage(std::cerr, argv[0]);
949 if (std::strstr(argv[argi], "://"))
951 // It appears to be an absolute URL, so use it as-is.
952 menu_url = argv[argi];
956 // Assume it's a filename. Resolve it to an absolute URL.
957 std::string path(argv[argi]);
958 if (!Glib::path_is_absolute(path))
959 path = Glib::build_filename(Glib::get_current_dir(), path);
960 menu_url = Glib::filename_to_uri(path);
963 output_dir = argv[argi + 1];
965 // Initialise Mozilla
966 browser_widget::initialiser browser_init;
967 set_browser_preferences();
968 init_agent_style_sheet("file://" VIDEOLINK_SHARE_DIR "/videolink.css");
969 init_agent_style_sheet(std::string("file://" VIDEOLINK_SHARE_DIR "/")
970 .append(frame_params.common_name).append(".css")
973 null_prompt_service::install();
975 // Run the browser/converter
978 preview_window window(frame_params, menu_url);
980 window.signal_hide().connect(sigc::ptr_fun(Gtk::Main::quit));
986 conversion_window window(frame_params, menu_url, output_dir, encoder);
988 window.signal_hide().connect(sigc::ptr_fun(Gtk::Main::quit));
990 return window.is_finished() ? EXIT_SUCCESS : EXIT_FAILURE;
993 catch (std::exception & e)
995 std::cerr << "ERROR: " << e.what() << "\n";