1 // Copyright 2005-6 Ben Hutchings <ben@decadent.org.uk>.
2 // See the file "COPYING" for licence details.
18 #include <boost/shared_ptr.hpp>
20 #include <gdkmm/pixbuf.h>
21 #include <glibmm/convert.h>
22 #include <glibmm/spawn.h>
23 #include <gtkmm/main.h>
24 #include <gtkmm/window.h>
26 #include <imglib2/ImageErrors.h>
27 #include <nsGUIEvent.h>
28 #include <nsIBoxObject.h>
29 #include <nsIContent.h>
30 #include <nsIDocShell.h>
31 #include <nsIDOMAbstractView.h>
32 #include <nsIDOMBarProp.h>
33 #include <nsIDOMDocumentEvent.h>
34 #include <nsIDOMDocumentView.h>
35 #include <nsIDOMElement.h>
36 #include <nsIDOMEventTarget.h>
37 #include <nsIDOMHTMLDocument.h>
38 #include <nsIDOMMouseEvent.h>
39 #include <nsIDOMNSDocument.h>
40 #include <nsIDOMWindow.h>
41 #include <nsIEventStateManager.h>
42 #include <nsIInterfaceRequestorUtils.h>
43 #include <nsIURI.h> // required before nsILink.h
45 #include <nsIPrefBranch.h>
46 #if MOZ_VERSION_MAJOR > 1 || (MOZ_VERSION_MAJOR == 1 && MOZ_VERSION_MINOR >= 8)
47 # include <nsPresContext.h>
49 # include <nsIPresContext.h>
50 typedef nsIPresContext nsPresContext; // ugh
52 #include <nsIPrefService.h>
53 #include <nsIPresShell.h>
54 #if MOZ_VERSION_MAJOR > 1 || (MOZ_VERSION_MAJOR == 1 && MOZ_VERSION_MINOR >= 8)
55 # include <nsServiceManagerUtils.h>
57 # include <nsIServiceManagerUtils.h>
59 #include <nsIWebBrowser.h>
62 #include "browser_widget.hpp"
63 #include "child_iterator.hpp"
65 #include "generate_dvd.hpp"
66 #include "link_iterator.hpp"
67 #include "null_prompt_service.hpp"
68 #include "pixbufs.hpp"
69 #include "style_sheets.hpp"
70 #include "temp_file.hpp"
72 #include "x_frame_buffer.hpp"
73 #include "xml_utils.hpp"
74 #include "xpcom_support.hpp"
76 using xpcom_support::check;
80 // We can try using any of these encoders to convert PNG to MPEG.
83 mpeg_encoder_ffmpeg, // ffmpeg
84 mpeg_encoder_mjpegtools_old, // mjpegtools before version 1.8
85 mpeg_encoder_mjpegtools_new // mjpegtools from version 1.8
90 int left, top; // inclusive
91 int right, bottom; // exclusive
93 rectangle operator|=(const rectangle & other)
97 // use current extents unchanged
106 // find rectangle enclosing both extents
107 left = std::min(left, other.left);
108 top = std::min(top, other.top);
109 right = std::max(right, other.right);
110 bottom = std::max(bottom, other.bottom);
116 rectangle operator&=(const rectangle & other)
118 // find rectangle enclosed in both extents
119 left = std::max(left, other.left);
120 top = std::max(top, other.top);
121 right = std::max(left, std::min(right, other.right));
122 bottom = std::max(top, std::min(bottom, other.bottom));
128 return left == right || bottom == top;
132 rectangle get_elem_rect(nsIDOMNSDocument * ns_doc,
133 nsIDOMElement * elem)
137 // Start with this element's bounding box
138 nsCOMPtr<nsIBoxObject> box;
139 check(ns_doc->GetBoxObjectFor(elem, getter_AddRefs(box)));
141 check(box->GetScreenX(&result.left));
142 check(box->GetScreenY(&result.top));
143 check(box->GetWidth(&width));
144 check(box->GetHeight(&height));
145 result.right = result.left + width;
146 result.bottom = result.top + height;
148 // Merge bounding boxes of all child elements
149 for (child_iterator it = child_iterator(elem), end; it != end; ++it)
151 nsCOMPtr<nsIDOMNode> child_node(*it);
153 if (check(child_node->GetNodeType(&child_type)),
154 child_type == nsIDOMNode::ELEMENT_NODE)
156 nsCOMPtr<nsIDOMElement> child_elem(
157 do_QueryInterface(child_node));
158 result |= get_elem_rect(ns_doc, child_elem);
166 class videolink_window : public Gtk::Window
170 const video::frame_params & frame_params,
171 const std::string & main_page_uri,
172 const std::string & output_dir,
173 mpeg_encoder encoder);
175 bool is_finished() const;
178 dvd_contents::pgc_ref add_menu(const std::string & uri);
179 dvd_contents::pgc_ref add_title(const std::string & uri);
180 void load_next_page();
182 void on_net_state_change(const char * uri, gint flags, guint status);
183 bool browser_is_busy() const
185 return pending_window_update_ || pending_req_count_;
188 void save_screenshot();
189 void process_links(nsIPresShell * pres_shell,
190 nsPresContext * pres_context,
191 nsIDOMWindow * dom_window);
193 video::frame_params frame_params_;
194 std::string output_dir_;
195 mpeg_encoder encoder_;
196 browser_widget browser_widget_;
197 agent_style_sheet_holder style_sheet_;
199 dvd_contents contents_;
200 typedef std::map<std::string, dvd_contents::pgc_ref> resource_map_type;
201 resource_map_type resource_map_;
203 std::queue<std::string> page_queue_;
204 bool pending_window_update_;
205 int pending_req_count_;
206 bool have_tweaked_page_;
207 std::auto_ptr<temp_file> background_temp_;
209 std::auto_ptr<page_state> page_state_;
214 videolink_window::videolink_window(
215 const video::frame_params & frame_params,
216 const std::string & main_page_uri,
217 const std::string & output_dir,
218 mpeg_encoder encoder)
219 : frame_params_(frame_params),
220 output_dir_(output_dir),
222 style_sheet_(init_agent_style_sheet(
223 "file://"VIDEOLINK_SHARE_DIR"/videolink.css")),
224 pending_window_update_(false),
225 pending_req_count_(0),
226 have_tweaked_page_(false),
229 set_size_request(frame_params_.width, frame_params_.height);
230 set_resizable(false);
232 add(browser_widget_);
233 browser_widget_.show();
234 Glib::signal_idle().connect(
235 SigC::slot(*this, &videolink_window::on_idle));
236 browser_widget_.signal_net_state().connect(
237 SigC::slot(*this, &videolink_window::on_net_state_change));
239 add_menu(main_page_uri);
242 bool videolink_window::is_finished() const
247 dvd_contents::pgc_ref videolink_window::add_menu(const std::string & uri)
249 dvd_contents::pgc_ref next_menu(dvd_contents::menu_pgc,
250 contents_.menus.size());
251 std::pair<resource_map_type::iterator, bool> insert_result(
252 resource_map_.insert(std::make_pair(uri, next_menu)));
254 if (!insert_result.second)
256 return insert_result.first->second;
260 page_queue_.push(uri);
261 contents_.menus.resize(contents_.menus.size() + 1);
266 dvd_contents::pgc_ref videolink_window::add_title(const std::string & uri)
268 dvd_contents::pgc_ref next_title(dvd_contents::title_pgc,
269 contents_.titles.size());
270 std::pair<resource_map_type::iterator, bool> insert_result(
271 resource_map_.insert(std::make_pair(uri, next_title)));
273 if (!insert_result.second)
275 return insert_result.first->second;
279 Glib::ustring hostname;
280 std::string path(Glib::filename_from_uri(uri, hostname));
281 // FIXME: Should check the hostname
285 // Store a reference to a linked VOB file, or the contents
286 // of a linked VOB list file.
287 if (path.compare(path.size() - 4, 4, ".vob") == 0)
289 if (!Glib::file_test(path, Glib::FILE_TEST_IS_REGULAR))
290 throw std::runtime_error(
291 path + " is missing or not a regular file");
298 assert(path.compare(path.size() - 8, 8, ".voblist") == 0);
299 read_vob_list(path).swap(list);
302 contents_.titles.resize(contents_.titles.size() + 1);
303 contents_.titles.back().swap(list);
308 void videolink_window::load_next_page()
310 assert(!page_queue_.empty());
311 const std::string & uri = page_queue_.front();
312 std::cout << "loading " << uri << std::endl;
314 browser_widget_.load_uri(uri);
317 bool videolink_window::on_idle()
320 return false; // don't call again thankyou
323 void videolink_window::on_net_state_change(const char * uri,
324 gint flags, guint status)
326 # ifdef DEBUG_ON_NET_STATE_CHANGE
327 std::cout << "videolink_window::on_net_state_change(";
329 std::cout << '"' << uri << '"';
334 gint flags_left = flags;
335 static const struct {
339 { GTK_MOZ_EMBED_FLAG_START, "STATE_START" },
340 { GTK_MOZ_EMBED_FLAG_REDIRECTING, "STATE_REDIRECTING" },
341 { GTK_MOZ_EMBED_FLAG_TRANSFERRING, "STATE_TRANSFERRING" },
342 { GTK_MOZ_EMBED_FLAG_NEGOTIATING, "STATE_NEGOTIATING" },
343 { GTK_MOZ_EMBED_FLAG_STOP, "STATE_STOP" },
344 { GTK_MOZ_EMBED_FLAG_IS_REQUEST, "STATE_IS_REQUEST" },
345 { GTK_MOZ_EMBED_FLAG_IS_DOCUMENT, "STATE_IS_DOCUMENT" },
346 { GTK_MOZ_EMBED_FLAG_IS_NETWORK, "STATE_IS_NETWORK" },
347 { GTK_MOZ_EMBED_FLAG_IS_WINDOW, "STATE_IS_WINDOW" }
349 for (int i = 0; i != sizeof(flag_names)/sizeof(flag_names[0]); ++i)
351 if (flags & flag_names[i].value)
353 std::cout << flag_names[i].name;
354 flags_left -= flag_names[i].value;
360 std::cout << "0x" << std::setbase(16) << flags_left;
362 std::cout << ", " << "0x" << std::setbase(16) << status << ")\n";
363 # endif // DEBUG_ON_NET_STATE_CHANGE
365 if (flags & GTK_MOZ_EMBED_FLAG_IS_REQUEST)
367 if (flags & GTK_MOZ_EMBED_FLAG_START)
368 ++pending_req_count_;
370 if (flags & GTK_MOZ_EMBED_FLAG_STOP)
372 assert(pending_req_count_ != 0);
373 --pending_req_count_;
377 if (flags & GTK_MOZ_EMBED_FLAG_IS_DOCUMENT
378 && flags & GTK_MOZ_EMBED_FLAG_START)
380 pending_window_update_ = true;
381 have_tweaked_page_ = false;
384 if (flags & GTK_MOZ_EMBED_FLAG_IS_WINDOW
385 && flags & GTK_MOZ_EMBED_FLAG_STOP)
387 // Check whether the load was successful, ignoring this
389 if (status != NS_IMAGELIB_ERROR_LOAD_ABORTED)
392 pending_window_update_ = false;
395 if (!browser_is_busy())
405 catch (std::exception & e)
407 std::cerr << "Fatal error";
408 if (!page_queue_.empty())
409 std::cerr << " while processing <" << page_queue_.front()
411 std::cerr << ": " << e.what() << "\n";
414 catch (Glib::Exception & e)
416 std::cerr << "Fatal error";
417 if (!page_queue_.empty())
418 std::cerr << " while processing <" << page_queue_.front()
420 std::cerr << ": " << e.what() << "\n";
426 bool videolink_window::process_page()
428 assert(!page_queue_.empty());
430 nsCOMPtr<nsIWebBrowser> browser(browser_widget_.get_browser());
431 nsCOMPtr<nsIDocShell> doc_shell(do_GetInterface(browser));
433 nsCOMPtr<nsIPresShell> pres_shell;
434 check(doc_shell->GetPresShell(getter_AddRefs(pres_shell)));
435 nsCOMPtr<nsPresContext> pres_context;
436 check(doc_shell->GetPresContext(getter_AddRefs(pres_context)));
437 nsCOMPtr<nsIDOMWindow> dom_window;
438 check(browser->GetContentDOMWindow(getter_AddRefs(dom_window)));
440 // If we haven't done so already, apply the stylesheet and
441 // disable scrollbars.
442 if (!have_tweaked_page_)
444 apply_agent_style_sheet(style_sheet_, pres_shell);
446 // This actually only needs to be done once.
447 nsCOMPtr<nsIDOMBarProp> dom_bar_prop;
448 check(dom_window->GetScrollbars(getter_AddRefs(dom_bar_prop)));
449 check(dom_bar_prop->SetVisible(false));
451 have_tweaked_page_ = true;
453 // Might need to wait a while for things to load or more
454 // likely for a re-layout.
455 if (browser_is_busy())
459 // All further work should only be done if we're not in preview mode.
460 if (!output_dir_.empty())
462 // If we haven't already started work on this menu, save a
463 // screenshot of its normal appearance.
464 if (!page_state_.get())
467 // Start or continue processing links.
468 process_links(pres_shell, pres_context, dom_window);
470 // If we've finished work on the links, move on to the
471 // next page, if any, or else generate the DVD filesystem.
472 if (!page_state_.get())
475 if (page_queue_.empty())
477 generate_dvd(contents_, output_dir_);
490 void videolink_window::save_screenshot()
492 Glib::RefPtr<Gdk::Window> window(get_window());
494 window->process_updates(true);
496 background_temp_.reset(new temp_file("videolink-back-"));
497 background_temp_->close();
498 std::cout << "saving " << background_temp_->get_name() << std::endl;
499 Gdk::Pixbuf::create(Glib::RefPtr<Gdk::Drawable>(window),
500 window->get_colormap(),
502 frame_params_.width, frame_params_.height)
503 ->save(background_temp_->get_name(), "png");
506 struct videolink_window::page_state
508 page_state(nsIDOMDocument * doc, int width, int height)
509 : diff_pixbuf(Gdk::Pixbuf::create(
511 true, 8, // has_alpha, bits_per_sample
513 spumux_temp("videolink-spumux-"),
514 links_temp("videolink-links-"),
523 Glib::RefPtr<Gdk::Pixbuf> diff_pixbuf;
525 temp_file spumux_temp;
526 std::ofstream spumux_file;
528 temp_file links_temp;
531 link_iterator links_it, links_end;
535 Glib::RefPtr<Gdk::Pixbuf> norm_pixbuf;
538 void videolink_window::process_links(nsIPresShell * pres_shell,
539 nsPresContext * pres_context,
540 nsIDOMWindow * dom_window)
542 Glib::RefPtr<Gdk::Window> window(get_window());
545 nsCOMPtr<nsIDOMDocument> basic_doc;
546 check(dom_window->GetDocument(getter_AddRefs(basic_doc)));
547 nsCOMPtr<nsIDOMNSDocument> ns_doc(do_QueryInterface(basic_doc));
549 nsCOMPtr<nsIEventStateManager> event_state_man(
550 pres_context->EventStateManager()); // does not AddRef
551 assert(event_state_man);
552 nsCOMPtr<nsIDOMDocumentEvent> event_factory(
553 do_QueryInterface(basic_doc));
554 assert(event_factory);
555 nsCOMPtr<nsIDOMDocumentView> doc_view(do_QueryInterface(basic_doc));
557 nsCOMPtr<nsIDOMAbstractView> view;
558 check(doc_view->GetDefaultView(getter_AddRefs(view)));
560 // Set up or recover our iteration state.
561 std::auto_ptr<page_state> state(page_state_);
566 basic_doc, frame_params_.width, frame_params_.height));
568 state->spumux_file.open(state->spumux_temp.get_name().c_str());
569 state->spumux_file <<
572 " <spu force='yes' start='00:00:00.00'\n"
573 " highlight='" << state->links_temp.get_name() << "'\n"
574 " select='" << state->links_temp.get_name() << "'>\n";
577 rectangle window_rect = {
578 0, 0, frame_params_.width, frame_params_.height
581 unsigned menu_num = 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 << "Ignoring invisible link to "
621 << uri_and_fragment << "\n";
627 if (state->link_num >= unsigned(dvd::menu_buttons_max))
629 if (state->link_num == unsigned(dvd::menu_buttons_max))
630 std::cerr << "No more than " << dvd::menu_buttons_max
631 << " buttons can be placed on a menu\n";
632 std::cerr << "Ignoring link to " << uri_and_fragment
637 state->spumux_file <<
638 " <button x0='" << state->link_rect.left << "'"
639 " y0='" << state->link_rect.top << "'"
640 " x1='" << state->link_rect.right - 1 << "'"
641 " y1='" << state->link_rect.bottom - 1 << "'/>\n";
643 // Check whether this is a link to a video or a page then
644 // add it to the known resources if not already seen; then
645 // add it to the menu entries.
646 dvd_contents::pgc_ref target;
647 // FIXME: This is a bit of a hack. Perhaps we could decide
648 // later based on the MIME type determined by Mozilla?
650 && uri.compare(uri.size() - 4, 4, ".vob") == 0)
652 && uri.compare(uri.size() - 8, 8, ".voblist") == 0))
655 check(uri_iface->SchemeIs("file", &is_file));
658 std::cerr << "Links to video must use the file:"
662 target = add_title(uri);
664 std::strtoul(fragment.c_str(), NULL, 10);
668 target = add_menu(uri);
669 // TODO: If there's a fragment, work out which button
670 // is closest and set target.sub_index.
672 contents_.menus[menu_num].entries.push_back(target);
674 nsCOMPtr<nsIContent> content(do_QueryInterface(node));
676 nsCOMPtr<nsIDOMEventTarget> event_target(
677 do_QueryInterface(node));
678 assert(event_target);
680 state->norm_pixbuf = Gdk::Pixbuf::create(
681 Glib::RefPtr<Gdk::Drawable>(window),
682 window->get_colormap(),
683 state->link_rect.left,
684 state->link_rect.top,
687 state->link_rect.right - state->link_rect.left,
688 state->link_rect.bottom - state->link_rect.top);
690 nsCOMPtr<nsIDOMEvent> event;
691 check(event_factory->CreateEvent(
692 NS_ConvertASCIItoUTF16("MouseEvents"),
693 getter_AddRefs(event)));
694 nsCOMPtr<nsIDOMMouseEvent> mouse_event(
695 do_QueryInterface(event));
697 check(mouse_event->InitMouseEvent(
698 NS_ConvertASCIItoUTF16("mouseover"),
702 0, // detail: mouse click count
703 state->link_rect.left, // screenX
704 state->link_rect.top, // screenY
705 state->link_rect.left, // clientX
706 state->link_rect.top, // clientY
707 false, false, false, false, // qualifiers
708 0, // button: left (or primary)
709 0)); // related target
711 check(event_target->DispatchEvent(mouse_event,
713 check(event_state_man->SetContentState(content,
714 NS_EVENT_STATE_HOVER));
716 # if MOZ_VERSION_MAJOR > 1 \
717 || (MOZ_VERSION_MAJOR == 1 && MOZ_VERSION_MINOR >= 8)
718 pres_shell->FlushPendingNotifications(Flush_Display);
720 pres_shell->FlushPendingNotifications(true);
723 // We may have to exit and wait for image loading
724 // to complete, at which point we will be called
726 if (browser_is_busy())
728 state->link_changing = true;
734 window->process_updates(true);
736 Glib::RefPtr<Gdk::Pixbuf> changed_pixbuf(
738 Glib::RefPtr<Gdk::Drawable>(window),
739 window->get_colormap(),
740 state->link_rect.left,
741 state->link_rect.top,
744 state->link_rect.right - state->link_rect.left,
745 state->link_rect.bottom - state->link_rect.top));
750 state->link_rect.left,
751 state->link_rect.top,
752 state->link_rect.right - state->link_rect.left,
753 state->link_rect.bottom - state->link_rect.top);
756 quantise_rgba_pixbuf(state->diff_pixbuf, dvd::button_n_colours);
758 std::cout << "saving " << state->links_temp.get_name()
760 state->diff_pixbuf->save(state->links_temp.get_name(), "png");
762 state->spumux_file <<
767 state->spumux_file.close();
769 // TODO: if (!state->spumux_file) throw ...
772 std::ostringstream command_stream;
773 if (encoder_ == mpeg_encoder_ffmpeg)
777 << " -f image2 -vcodec png -i "
778 << background_temp_->get_name()
779 << " -target " << frame_params_.ffmpeg_name << "-dvd"
780 << " -vcodec mpeg2video -an -y /dev/stdout"
781 << " | spumux -v0 -mdvd " << state->spumux_temp.get_name()
782 << " > " << contents_.menus[menu_num].vob_temp->get_name();
786 assert(encoder_ == mpeg_encoder_mjpegtools_old
787 || encoder_ == mpeg_encoder_mjpegtools_new);
790 << background_temp_->get_name()
791 << " | ppmtoy4m -v0 -n1 -F"
792 << frame_params_.rate_numer
793 << ":" << frame_params_.rate_denom
794 << " -A" << frame_params_.pixel_ratio_width
795 << ":" << frame_params_.pixel_ratio_height
797 // The chroma subsampling keywords changed between
798 // versions 1.6.2 and 1.8 of mjpegtools. There is no
799 // keyword that works with both.
800 if (encoder_ == mpeg_encoder_mjpegtools_old)
801 command_stream << "-S420_mpeg2";
803 command_stream << "-S420mpeg2";
805 << (" | mpeg2enc -v0 -f8 -a2 -o/dev/stdout"
806 " | mplex -v0 -f8 -o/dev/stdout /dev/stdin"
807 " | spumux -v0 -mdvd ")
808 << state->spumux_temp.get_name()
810 << contents_.menus[menu_num].vob_temp->get_name();
812 std::string command(command_stream.str());
813 const char * argv[] = {
814 "/bin/sh", "-c", command.c_str(), 0
816 std::cout << "running " << argv[2] << std::endl;
818 Glib::spawn_sync(".",
819 Glib::ArrayHandle<std::string>(
820 argv, sizeof(argv)/sizeof(argv[0]),
821 Glib::OWNERSHIP_NONE),
822 Glib::SPAWN_STDOUT_TO_DEV_NULL,
826 if (command_result != 0)
827 throw std::runtime_error("spumux pipeline failed");
831 const video::frame_params & lookup_frame_params(const char * str)
834 static const char * const known_strings[] = {
840 for (std::size_t i = 0;
841 i != sizeof(known_strings)/sizeof(known_strings[0]);
843 if (std::strcmp(str, known_strings[i]) == 0)
845 ? video::frame_params_625
846 : video::frame_params_525;
847 throw std::runtime_error(
848 std::string("Invalid video standard: ").append(str));
851 void print_usage(std::ostream & stream, const char * command_name)
854 "Usage: " << command_name << " [gtk-options] [--preview]\n"
855 " [--video-std {525|525/60|NTSC|ntsc"
856 " | 625|625/50|PAL|pal}]\n"
857 " [--encoder {mjpegtools|mjpegtools-old}]\n"
858 " menu-url [output-dir]\n";
861 void set_browser_preferences()
863 nsCOMPtr<nsIPrefService> pref_service;
864 static const nsCID pref_service_cid = NS_PREFSERVICE_CID;
865 check(CallGetService<nsIPrefService>(pref_service_cid,
866 getter_AddRefs(pref_service)));
867 nsCOMPtr<nsIPrefBranch> pref_branch;
868 check(pref_service->GetBranch("", getter_AddRefs(pref_branch)));
870 # if MOZ_VERSION_MAJOR > 1 \
871 || (MOZ_VERSION_MAJOR == 1 && MOZ_VERSION_MINOR >= 8)
872 // Disable IE-compatibility kluge that causes backgrounds to
873 // sometimes/usually be missing from snapshots. This is only
874 // effective from Mozilla 1.8 onward.
875 check(pref_branch->SetBoolPref(
876 "layout.fire_onload_after_image_background_loads",
880 // Set display resolution. With standard-definition video we
881 // will be fitting ~600 pixels across a screen typically
882 // ranging from 10 to 25 inches wide, for a resolution of
883 // 24-60 dpi. I therefore declare the average horizontal
884 // resolution to be 40 dpi. The vertical resolution will be
885 // slightly different but unfortunately Mozilla doesn't
886 // support non-square pixels (and neither do fontconfig or Xft
888 check(pref_branch->SetIntPref("browser.display.screen_resolution",
894 void fatal_error(const std::string & message)
896 std::cerr << "Fatal error: " << message << "\n";
900 int main(int argc, char ** argv)
904 video::frame_params frame_params = video::frame_params_625;
905 bool preview_mode = false;
906 std::string menu_url;
907 std::string output_dir;
908 mpeg_encoder encoder = mpeg_encoder_ffmpeg;
910 // Do initial option parsing. We have to do this before
911 // letting Gtk parse the arguments since we may need to spawn
916 if (std::strcmp(argv[argi], "--") == 0)
920 else if (std::strcmp(argv[argi], "--help") == 0)
922 print_usage(std::cout, argv[0]);
925 else if (std::strcmp(argv[argi], "--preview") == 0)
930 else if (std::strcmp(argv[argi], "--video-std") == 0)
932 if (argi + 1 == argc)
934 std::cerr << "Missing argument to --video-std\n";
935 print_usage(std::cerr, argv[0]);
938 frame_params = lookup_frame_params(argv[argi + 1]);
947 std::auto_ptr<x_frame_buffer> fb;
950 // Spawn Xvfb and set env variables so that Xlib will use it
951 // Use 8 bits each for RGB components, which should translate into
952 // "enough" bits for YUV components.
953 fb.reset(new x_frame_buffer(frame_params.width,
956 setenv("XAUTHORITY", fb->get_authority().c_str(), true);
957 setenv("DISPLAY", fb->get_display().c_str(), true);
961 Gtk::Main kit(argc, argv);
963 // Complete option parsing with Gtk's options out of the way.
967 if (std::strcmp(argv[argi], "--") == 0)
972 else if (std::strcmp(argv[argi], "--preview") == 0)
976 else if (std::strcmp(argv[argi], "--video-std") == 0)
980 else if (std::strcmp(argv[argi], "--save-temps") == 0)
982 temp_file::keep_all(true);
985 else if (std::strcmp(argv[argi], "--encoder") == 0)
987 if (argi + 1 == argc)
989 std::cerr << "Missing argument to --encoder\n";
990 print_usage(std::cerr, argv[0]);
993 if (std::strcmp(argv[argi + 1], "ffmpeg") == 0)
995 encoder = mpeg_encoder_ffmpeg;
997 else if (std::strcmp(argv[argi + 1], "mjpegtools-old") == 0)
999 encoder = mpeg_encoder_mjpegtools_old;
1001 else if (std::strcmp(argv[argi + 1], "mjpegtools") == 0
1002 || std::strcmp(argv[argi + 1], "mjpegtools-new") == 0)
1004 encoder = mpeg_encoder_mjpegtools_new;
1008 std::cerr << "Invalid argument to --encoder\n";
1009 print_usage(std::cerr, argv[0]);
1010 return EXIT_FAILURE;
1014 else if (argv[argi][0] == '-')
1016 std::cerr << "Invalid option: " << argv[argi] << "\n";
1017 print_usage(std::cerr, argv[0]);
1018 return EXIT_FAILURE;
1026 // Look for a starting URL or filename and (except in preview
1027 // mode) an output directory after the options.
1028 if (argc - argi != (preview_mode ? 1 : 2))
1030 print_usage(std::cerr, argv[0]);
1031 return EXIT_FAILURE;
1033 if (std::strstr(argv[argi], "://"))
1035 // It appears to be an absolute URL, so use it as-is.
1036 menu_url = argv[argi];
1040 // Assume it's a filename. Resolve it to an absolute URL.
1041 std::string path(argv[argi]);
1042 if (!Glib::path_is_absolute(path))
1043 path = Glib::build_filename(Glib::get_current_dir(), path);
1044 menu_url = Glib::filename_to_uri(path);
1047 output_dir = argv[argi + 1];
1049 // Initialise Mozilla
1050 browser_widget::initialiser browser_init;
1051 set_browser_preferences();
1053 null_prompt_service::install();
1055 // Run the browser/converter
1056 videolink_window window(frame_params, menu_url, output_dir, encoder);
1058 window.signal_hide().connect(SigC::slot(&Gtk::Main::quit));
1061 return ((preview_mode || window.is_finished())
1065 catch (std::exception & e)
1067 std::cerr << "Fatal error: " << e.what() << "\n";
1068 return EXIT_FAILURE;