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 #include <nsIPrefService.h>
47 #include <nsIPresContext.h>
48 #include <nsIPresShell.h>
49 #include <nsIServiceManagerUtils.h>
50 #include <nsIWebBrowser.h>
53 #include "browser_widget.hpp"
54 #include "child_iterator.hpp"
56 #include "generate_dvd.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 "x_frame_buffer.hpp"
64 #include "xml_utils.hpp"
65 #include "xpcom_support.hpp"
67 using xpcom_support::check;
71 // We can try using any of these encoders to convert PNG to MPEG.
74 mpeg_encoder_ffmpeg, // ffmpeg
75 mpeg_encoder_mjpegtools_old, // mjpegtools before version 1.8
76 mpeg_encoder_mjpegtools_new // mjpegtools from version 1.8
81 int left, top; // inclusive
82 int right, bottom; // exclusive
84 rectangle operator|=(const rectangle & other)
88 // use current extents unchanged
97 // find rectangle enclosing both extents
98 left = std::min(left, other.left);
99 top = std::min(top, other.top);
100 right = std::max(right, other.right);
101 bottom = std::max(bottom, other.bottom);
107 rectangle operator&=(const rectangle & other)
109 // find rectangle enclosed in both extents
110 left = std::max(left, other.left);
111 top = std::max(top, other.top);
112 right = std::max(left, std::min(right, other.right));
113 bottom = std::max(top, std::min(bottom, other.bottom));
119 return left == right || bottom == top;
123 rectangle get_elem_rect(nsIDOMNSDocument * ns_doc,
124 nsIDOMElement * elem)
128 // Start with this element's bounding box
129 nsCOMPtr<nsIBoxObject> box;
130 check(ns_doc->GetBoxObjectFor(elem, getter_AddRefs(box)));
132 check(box->GetScreenX(&result.left));
133 check(box->GetScreenY(&result.top));
134 check(box->GetWidth(&width));
135 check(box->GetHeight(&height));
136 result.right = result.left + width;
137 result.bottom = result.top + height;
139 // Merge bounding boxes of all child elements
140 for (child_iterator it = child_iterator(elem), end; it != end; ++it)
142 nsCOMPtr<nsIDOMNode> child_node(*it);
144 if (check(child_node->GetNodeType(&child_type)),
145 child_type == nsIDOMNode::ELEMENT_NODE)
147 nsCOMPtr<nsIDOMElement> child_elem(
148 do_QueryInterface(child_node));
149 result |= get_elem_rect(ns_doc, child_elem);
157 class webdvd_window : public Gtk::Window
161 const video::frame_params & frame_params,
162 const std::string & main_page_uri,
163 const std::string & output_dir,
164 mpeg_encoder encoder);
166 bool is_finished() const;
169 dvd_contents::pgc_ref add_menu(const std::string & uri);
170 dvd_contents::pgc_ref add_title(const std::string & uri);
171 void load_next_page();
173 void on_net_state_change(const char * uri, gint flags, guint status);
174 bool browser_is_busy() const
176 return pending_window_update_ || pending_req_count_;
179 void save_screenshot();
180 void process_links(nsIPresShell * pres_shell,
181 nsIPresContext * pres_context,
182 nsIDOMWindow * dom_window);
184 video::frame_params frame_params_;
185 std::string output_dir_;
186 mpeg_encoder encoder_;
187 browser_widget browser_widget_;
188 nsCOMPtr<nsIStyleSheet> stylesheet_;
190 dvd_contents contents_;
191 typedef std::map<std::string, dvd_contents::pgc_ref> resource_map_type;
192 resource_map_type resource_map_;
194 std::queue<std::string> page_queue_;
195 bool pending_window_update_;
196 int pending_req_count_;
197 bool have_tweaked_page_;
198 std::auto_ptr<temp_file> background_temp_;
200 std::auto_ptr<page_state> page_state_;
205 webdvd_window::webdvd_window(
206 const video::frame_params & frame_params,
207 const std::string & main_page_uri,
208 const std::string & output_dir,
209 mpeg_encoder encoder)
210 : frame_params_(frame_params),
211 output_dir_(output_dir),
213 stylesheet_(load_css("file://" WEBDVD_LIB_DIR "/webdvd.css")),
214 pending_window_update_(false),
215 pending_req_count_(0),
216 have_tweaked_page_(false),
219 set_size_request(frame_params_.width, frame_params_.height);
220 set_resizable(false);
222 add(browser_widget_);
223 browser_widget_.show();
224 Glib::signal_idle().connect(
225 SigC::slot(*this, &webdvd_window::on_idle));
226 browser_widget_.signal_net_state().connect(
227 SigC::slot(*this, &webdvd_window::on_net_state_change));
229 add_menu(main_page_uri);
232 bool webdvd_window::is_finished() const
237 dvd_contents::pgc_ref webdvd_window::add_menu(const std::string & uri)
239 dvd_contents::pgc_ref next_menu(dvd_contents::menu_pgc,
240 contents_.menus.size());
241 std::pair<resource_map_type::iterator, bool> insert_result(
242 resource_map_.insert(std::make_pair(uri, next_menu)));
244 if (!insert_result.second)
246 return insert_result.first->second;
250 page_queue_.push(uri);
251 contents_.menus.resize(contents_.menus.size() + 1);
256 dvd_contents::pgc_ref webdvd_window::add_title(const std::string & uri)
258 dvd_contents::pgc_ref next_title(dvd_contents::title_pgc,
259 contents_.titles.size());
260 std::pair<resource_map_type::iterator, bool> insert_result(
261 resource_map_.insert(std::make_pair(uri, next_title)));
263 if (!insert_result.second)
265 return insert_result.first->second;
269 Glib::ustring hostname;
270 std::string path(Glib::filename_from_uri(uri, hostname));
271 // FIXME: Should check the hostname
275 // Store a reference to a linked VOB file, or the contents
276 // of a linked VOB list file.
277 if (path.compare(path.size() - 4, 4, ".vob") == 0)
279 if (!Glib::file_test(path, Glib::FILE_TEST_IS_REGULAR))
280 throw std::runtime_error(
281 path + " is missing or not a regular file");
288 assert(path.compare(path.size() - 8, 8, ".voblist") == 0);
289 read_vob_list(path).swap(list);
292 contents_.titles.resize(contents_.titles.size() + 1);
293 contents_.titles.back().swap(list);
298 void webdvd_window::load_next_page()
300 assert(!page_queue_.empty());
301 const std::string & uri = page_queue_.front();
302 std::cout << "loading " << uri << std::endl;
304 browser_widget_.load_uri(uri);
307 bool webdvd_window::on_idle()
310 return false; // don't call again thankyou
313 void webdvd_window::on_net_state_change(const char * uri,
314 gint flags, guint status)
316 # ifdef DEBUG_ON_NET_STATE_CHANGE
317 std::cout << "webdvd_window::on_net_state_change(";
319 std::cout << '"' << uri << '"';
324 gint flags_left = flags;
325 static const struct {
329 { GTK_MOZ_EMBED_FLAG_START, "STATE_START" },
330 { GTK_MOZ_EMBED_FLAG_REDIRECTING, "STATE_REDIRECTING" },
331 { GTK_MOZ_EMBED_FLAG_TRANSFERRING, "STATE_TRANSFERRING" },
332 { GTK_MOZ_EMBED_FLAG_NEGOTIATING, "STATE_NEGOTIATING" },
333 { GTK_MOZ_EMBED_FLAG_STOP, "STATE_STOP" },
334 { GTK_MOZ_EMBED_FLAG_IS_REQUEST, "STATE_IS_REQUEST" },
335 { GTK_MOZ_EMBED_FLAG_IS_DOCUMENT, "STATE_IS_DOCUMENT" },
336 { GTK_MOZ_EMBED_FLAG_IS_NETWORK, "STATE_IS_NETWORK" },
337 { GTK_MOZ_EMBED_FLAG_IS_WINDOW, "STATE_IS_WINDOW" }
339 for (int i = 0; i != sizeof(flag_names)/sizeof(flag_names[0]); ++i)
341 if (flags & flag_names[i].value)
343 std::cout << flag_names[i].name;
344 flags_left -= flag_names[i].value;
350 std::cout << "0x" << std::setbase(16) << flags_left;
352 std::cout << ", " << "0x" << std::setbase(16) << status << ")\n";
353 # endif // DEBUG_ON_NET_STATE_CHANGE
355 if (flags & GTK_MOZ_EMBED_FLAG_IS_REQUEST)
357 if (flags & GTK_MOZ_EMBED_FLAG_START)
358 ++pending_req_count_;
360 if (flags & GTK_MOZ_EMBED_FLAG_STOP)
362 assert(pending_req_count_ != 0);
363 --pending_req_count_;
367 if (flags & GTK_MOZ_EMBED_FLAG_IS_DOCUMENT
368 && flags & GTK_MOZ_EMBED_FLAG_START)
370 pending_window_update_ = true;
371 have_tweaked_page_ = false;
374 if (flags & GTK_MOZ_EMBED_FLAG_IS_WINDOW
375 && flags & GTK_MOZ_EMBED_FLAG_STOP)
377 // Check whether the load was successful, ignoring this
379 if (status != NS_IMAGELIB_ERROR_LOAD_ABORTED)
382 pending_window_update_ = false;
385 if (!browser_is_busy())
395 catch (std::exception & e)
397 std::cerr << "Fatal error";
398 if (!page_queue_.empty())
399 std::cerr << " while processing <" << page_queue_.front()
401 std::cerr << ": " << e.what() << "\n";
404 catch (Glib::Exception & e)
406 std::cerr << "Fatal error";
407 if (!page_queue_.empty())
408 std::cerr << " while processing <" << page_queue_.front()
410 std::cerr << ": " << e.what() << "\n";
416 bool webdvd_window::process_page()
418 assert(!page_queue_.empty());
420 nsCOMPtr<nsIWebBrowser> browser(browser_widget_.get_browser());
421 nsCOMPtr<nsIDocShell> doc_shell(do_GetInterface(browser));
423 nsCOMPtr<nsIPresShell> pres_shell;
424 check(doc_shell->GetPresShell(getter_AddRefs(pres_shell)));
425 nsCOMPtr<nsIPresContext> pres_context;
426 check(doc_shell->GetPresContext(getter_AddRefs(pres_context)));
427 nsCOMPtr<nsIDOMWindow> dom_window;
428 check(browser->GetContentDOMWindow(getter_AddRefs(dom_window)));
430 // If we haven't done so already, apply the stylesheet and
431 // disable scrollbars.
432 if (!have_tweaked_page_)
434 apply_style_sheet(stylesheet_, pres_shell);
436 // This actually only needs to be done once.
437 nsCOMPtr<nsIDOMBarProp> dom_bar_prop;
438 check(dom_window->GetScrollbars(getter_AddRefs(dom_bar_prop)));
439 check(dom_bar_prop->SetVisible(false));
441 have_tweaked_page_ = true;
443 // Might need to wait a while for things to load or more
444 // likely for a re-layout.
445 if (browser_is_busy())
449 // All further work should only be done if we're not in preview mode.
450 if (!output_dir_.empty())
452 // If we haven't already started work on this menu, save a
453 // screenshot of its normal appearance.
454 if (!page_state_.get())
457 // Start or continue processing links.
458 process_links(pres_shell, pres_context, dom_window);
460 // If we've finished work on the links, move on to the
461 // next page, if any, or else generate the DVD filesystem.
462 if (!page_state_.get())
465 if (page_queue_.empty())
467 generate_dvd(contents_, output_dir_);
480 void webdvd_window::save_screenshot()
482 Glib::RefPtr<Gdk::Window> window(get_window());
484 window->process_updates(true);
486 background_temp_.reset(new temp_file("webdvd-back-"));
487 background_temp_->close();
488 std::cout << "saving " << background_temp_->get_name() << std::endl;
489 Gdk::Pixbuf::create(Glib::RefPtr<Gdk::Drawable>(window),
490 window->get_colormap(),
492 frame_params_.width, frame_params_.height)
493 ->save(background_temp_->get_name(), "png");
496 struct webdvd_window::page_state
498 page_state(nsIDOMDocument * doc, int width, int height)
499 : diff_pixbuf(Gdk::Pixbuf::create(
501 true, 8, // has_alpha, bits_per_sample
503 spumux_temp("webdvd-spumux-"),
504 links_temp("webdvd-links-"),
513 Glib::RefPtr<Gdk::Pixbuf> diff_pixbuf;
515 temp_file spumux_temp;
516 std::ofstream spumux_file;
518 temp_file links_temp;
521 link_iterator links_it, links_end;
525 Glib::RefPtr<Gdk::Pixbuf> norm_pixbuf;
528 void webdvd_window::process_links(nsIPresShell * pres_shell,
529 nsIPresContext * pres_context,
530 nsIDOMWindow * dom_window)
532 Glib::RefPtr<Gdk::Window> window(get_window());
535 nsCOMPtr<nsIDOMDocument> basic_doc;
536 check(dom_window->GetDocument(getter_AddRefs(basic_doc)));
537 nsCOMPtr<nsIDOMNSDocument> ns_doc(do_QueryInterface(basic_doc));
539 nsCOMPtr<nsIEventStateManager> event_state_man(
540 pres_context->EventStateManager()); // does not AddRef
541 assert(event_state_man);
542 nsCOMPtr<nsIDOMDocumentEvent> event_factory(
543 do_QueryInterface(basic_doc));
544 assert(event_factory);
545 nsCOMPtr<nsIDOMDocumentView> doc_view(do_QueryInterface(basic_doc));
547 nsCOMPtr<nsIDOMAbstractView> view;
548 check(doc_view->GetDefaultView(getter_AddRefs(view)));
550 // Set up or recover our iteration state.
551 std::auto_ptr<page_state> state(page_state_);
556 basic_doc, frame_params_.width, frame_params_.height));
558 state->spumux_file.open(state->spumux_temp.get_name().c_str());
559 state->spumux_file <<
562 " <spu force='yes' start='00:00:00.00'\n"
563 " highlight='" << state->links_temp.get_name() << "'\n"
564 " select='" << state->links_temp.get_name() << "'>\n";
567 rectangle window_rect = {
568 0, 0, frame_params_.width, frame_params_.height
571 unsigned menu_num = resource_map_[page_queue_.front()].index;
573 for (/* no initialisation */;
574 state->links_it != state->links_end;
577 nsCOMPtr<nsIDOMNode> node(*state->links_it);
579 // Find the link URI and separate any fragment from it.
580 nsCOMPtr<nsILink> link(do_QueryInterface(node));
582 nsCOMPtr<nsIURI> uri_iface;
583 check(link->GetHrefURI(getter_AddRefs(uri_iface)));
584 std::string uri_and_fragment, uri, fragment;
586 nsCString uri_and_fragment_ns;
587 check(uri_iface->GetSpec(uri_and_fragment_ns));
588 uri_and_fragment.assign(uri_and_fragment_ns.BeginReading(),
589 uri_and_fragment_ns.EndReading());
591 std::size_t hash_pos = uri_and_fragment.find('#');
592 uri.assign(uri_and_fragment, 0, hash_pos);
593 if (hash_pos != std::string::npos)
594 fragment.assign(uri_and_fragment,
595 hash_pos + 1, std::string::npos);
598 // Is this a new link?
599 if (!state->link_changing)
601 // Find a rectangle enclosing the link and clip it to the
603 nsCOMPtr<nsIDOMElement> elem(do_QueryInterface(node));
605 state->link_rect = get_elem_rect(ns_doc, elem);
606 state->link_rect &= window_rect;
608 if (state->link_rect.empty())
610 std::cerr << "Ignoring invisible link to "
611 << uri_and_fragment << "\n";
617 if (state->link_num >= unsigned(dvd::menu_buttons_max))
619 if (state->link_num == unsigned(dvd::menu_buttons_max))
620 std::cerr << "No more than " << dvd::menu_buttons_max
621 << " buttons can be placed on a menu\n";
622 std::cerr << "Ignoring link to " << uri_and_fragment
627 state->spumux_file <<
628 " <button x0='" << state->link_rect.left << "'"
629 " y0='" << state->link_rect.top << "'"
630 " x1='" << state->link_rect.right - 1 << "'"
631 " y1='" << state->link_rect.bottom - 1 << "'/>\n";
633 // Check whether this is a link to a video or a page then
634 // add it to the known resources if not already seen; then
635 // add it to the menu entries.
636 dvd_contents::pgc_ref target;
637 // FIXME: This is a bit of a hack. Perhaps we could decide
638 // later based on the MIME type determined by Mozilla?
640 && uri.compare(uri.size() - 4, 4, ".vob") == 0)
642 && uri.compare(uri.size() - 8, 8, ".voblist") == 0))
645 check(uri_iface->SchemeIs("file", &is_file));
648 std::cerr << "Links to video must use the file:"
652 target = add_title(uri);
654 std::strtoul(fragment.c_str(), NULL, 10);
658 target = add_menu(uri);
659 // TODO: If there's a fragment, work out which button
660 // is closest and set target.sub_index.
662 contents_.menus[menu_num].entries.push_back(target);
664 nsCOMPtr<nsIContent> content(do_QueryInterface(node));
666 nsCOMPtr<nsIDOMEventTarget> event_target(
667 do_QueryInterface(node));
668 assert(event_target);
670 state->norm_pixbuf = Gdk::Pixbuf::create(
671 Glib::RefPtr<Gdk::Drawable>(window),
672 window->get_colormap(),
673 state->link_rect.left,
674 state->link_rect.top,
677 state->link_rect.right - state->link_rect.left,
678 state->link_rect.bottom - state->link_rect.top);
680 nsCOMPtr<nsIDOMEvent> event;
681 check(event_factory->CreateEvent(
682 NS_ConvertASCIItoUTF16("MouseEvents"),
683 getter_AddRefs(event)));
684 nsCOMPtr<nsIDOMMouseEvent> mouse_event(
685 do_QueryInterface(event));
687 check(mouse_event->InitMouseEvent(
688 NS_ConvertASCIItoUTF16("mouseover"),
692 0, // detail: mouse click count
693 state->link_rect.left, // screenX
694 state->link_rect.top, // screenY
695 state->link_rect.left, // clientX
696 state->link_rect.top, // clientY
697 false, false, false, false, // qualifiers
698 0, // button: left (or primary)
699 0)); // related target
701 check(event_target->DispatchEvent(mouse_event,
703 check(event_state_man->SetContentState(content,
704 NS_EVENT_STATE_HOVER));
706 pres_shell->FlushPendingNotifications(true);
708 // We may have to exit and wait for image loading
709 // to complete, at which point we will be called
711 if (browser_is_busy())
713 state->link_changing = true;
719 window->process_updates(true);
721 Glib::RefPtr<Gdk::Pixbuf> changed_pixbuf(
723 Glib::RefPtr<Gdk::Drawable>(window),
724 window->get_colormap(),
725 state->link_rect.left,
726 state->link_rect.top,
729 state->link_rect.right - state->link_rect.left,
730 state->link_rect.bottom - state->link_rect.top));
735 state->link_rect.left,
736 state->link_rect.top,
737 state->link_rect.right - state->link_rect.left,
738 state->link_rect.bottom - state->link_rect.top);
741 quantise_rgba_pixbuf(state->diff_pixbuf, dvd::button_n_colours);
743 std::cout << "saving " << state->links_temp.get_name()
745 state->diff_pixbuf->save(state->links_temp.get_name(), "png");
747 state->spumux_file <<
752 state->spumux_file.close();
754 // TODO: if (!state->spumux_file) throw ...
757 std::ostringstream command_stream;
758 if (encoder_ == mpeg_encoder_ffmpeg)
762 << " -f image2 -vcodec png -i "
763 << background_temp_->get_name()
764 << " -target " << frame_params_.ffmpeg_name << "-dvd"
765 << " -vcodec mpeg2video -an -y /dev/stdout"
766 << " | spumux -v0 -mdvd " << state->spumux_temp.get_name()
767 << " > " << contents_.menus[menu_num].vob_temp->get_name();
771 assert(encoder_ == mpeg_encoder_mjpegtools_old
772 || encoder_ == mpeg_encoder_mjpegtools_new);
775 << background_temp_->get_name()
776 << " | ppmtoy4m -v0 -n1 -F"
777 << frame_params_.rate_numer
778 << ":" << frame_params_.rate_denom
779 << " -A" << frame_params_.pixel_ratio_width
780 << ":" << frame_params_.pixel_ratio_height
782 // The chroma subsampling keywords changed between
783 // versions 1.6.2 and 1.8 of mjpegtools. There is no
784 // keyword that works with both.
785 if (encoder_ == mpeg_encoder_mjpegtools_old)
786 command_stream << "-S420_mpeg2";
788 command_stream << "-S420mpeg2";
790 << (" | mpeg2enc -v0 -f8 -a2 -o/dev/stdout"
791 " | mplex -v0 -f8 -o/dev/stdout /dev/stdin"
792 " | spumux -v0 -mdvd ")
793 << state->spumux_temp.get_name()
795 << contents_.menus[menu_num].vob_temp->get_name();
797 std::string command(command_stream.str());
798 const char * argv[] = {
799 "/bin/sh", "-c", command.c_str(), 0
801 std::cout << "running " << argv[2] << std::endl;
803 Glib::spawn_sync(".",
804 Glib::ArrayHandle<std::string>(
805 argv, sizeof(argv)/sizeof(argv[0]),
806 Glib::OWNERSHIP_NONE),
807 Glib::SPAWN_STDOUT_TO_DEV_NULL,
811 if (command_result != 0)
812 throw std::runtime_error("spumux pipeline failed");
816 const video::frame_params & lookup_frame_params(const char * str)
819 static const char * const known_strings[] = {
825 for (std::size_t i = 0;
826 i != sizeof(known_strings)/sizeof(known_strings[0]);
828 if (std::strcmp(str, known_strings[i]) == 0)
830 ? video::frame_params_625
831 : video::frame_params_525;
832 throw std::runtime_error(
833 std::string("Invalid video standard: ").append(str));
836 void print_usage(std::ostream & stream, const char * command_name)
839 "Usage: " << command_name << " [gtk-options] [--preview]\n"
840 " [--video-std {525|525/60|NTSC|ntsc"
841 " | 625|625/50|PAL|pal}]\n"
842 " [--encoder {mjpegtools|mjpegtools-old}]\n"
843 " menu-url [output-dir]\n";
846 void set_browser_preferences()
848 nsCOMPtr<nsIPrefService> pref_service;
849 static const nsCID pref_service_cid = NS_PREFSERVICE_CID;
850 check(CallGetService<nsIPrefService>(pref_service_cid,
851 getter_AddRefs(pref_service)));
852 nsCOMPtr<nsIPrefBranch> pref_branch;
854 // Disable IE-compatibility kluge that causes backgrounds to
855 // sometimes/usually be missing from snapshots. This is only
856 // effective from Mozilla 1.8 onward.
857 # if MOZ_VERSION_MAJOR > 1 \
858 || (MOZ_VERSION_MAJOR == 1 && MOZ_VERSION_MINOR >= 8)
859 check(pref_service->GetDefaultBranch("layout",
860 getter_AddRefs(pref_branch)));
861 check(pref_branch->SetBoolPref(
862 "fire_onload_after_image_background_loads",
866 // Set display resolution. With standard-definition video we
867 // will be fitting ~600 pixels across a screen typically
868 // ranging from 10 to 25 inches wide, for a resolution of
869 // 24-60 dpi. I therefore declare the average horizontal
870 // resolution to be 40 dpi. The vertical resolution will be
871 // slightly higher (PAL/SECAM) or lower (NTSC), but
872 // unfortunately Mozilla doesn't support non-square pixels
873 // (and neither do fontconfig or Xft anyway).
874 check(pref_service->GetDefaultBranch("browser.display",
875 getter_AddRefs(pref_branch)));
876 check(pref_branch->SetIntPref("screen_resolution", 40));
881 void fatal_error(const std::string & message)
883 std::cerr << "Fatal error: " << message << "\n";
887 int main(int argc, char ** argv)
891 video::frame_params frame_params = video::frame_params_625;
892 bool preview_mode = false;
893 std::string menu_url;
894 std::string output_dir;
895 mpeg_encoder encoder = mpeg_encoder_ffmpeg;
897 // Do initial option parsing. We have to do this before
898 // letting Gtk parse the arguments since we may need to spawn
903 if (std::strcmp(argv[argi], "--") == 0)
907 else if (std::strcmp(argv[argi], "--help") == 0)
909 print_usage(std::cout, argv[0]);
912 else if (std::strcmp(argv[argi], "--preview") == 0)
917 else if (std::strcmp(argv[argi], "--video-std") == 0)
919 if (argi + 1 == argc)
921 std::cerr << "Missing argument to --video-std\n";
922 print_usage(std::cerr, argv[0]);
925 frame_params = lookup_frame_params(argv[argi + 1]);
934 std::auto_ptr<x_frame_buffer> fb;
937 // Spawn Xvfb and set env variables so that Xlib will use it
938 // Use 8 bits each for RGB components, which should translate into
939 // "enough" bits for YUV components.
940 fb.reset(new x_frame_buffer(frame_params.width,
943 setenv("XAUTHORITY", fb->get_authority().c_str(), true);
944 setenv("DISPLAY", fb->get_display().c_str(), true);
948 Gtk::Main kit(argc, argv);
950 // Complete option parsing with Gtk's options out of the way.
954 if (std::strcmp(argv[argi], "--") == 0)
959 else if (std::strcmp(argv[argi], "--preview") == 0)
963 else if (std::strcmp(argv[argi], "--video-std") == 0)
967 else if (std::strcmp(argv[argi], "--save-temps") == 0)
969 temp_file::keep_all(true);
972 else if (std::strcmp(argv[argi], "--encoder") == 0)
974 if (argi + 1 == argc)
976 std::cerr << "Missing argument to --encoder\n";
977 print_usage(std::cerr, argv[0]);
980 if (std::strcmp(argv[argi + 1], "ffmpeg") == 0)
982 encoder = mpeg_encoder_ffmpeg;
984 else if (std::strcmp(argv[argi + 1], "mjpegtools-old") == 0)
986 encoder = mpeg_encoder_mjpegtools_old;
988 else if (std::strcmp(argv[argi + 1], "mjpegtools") == 0
989 || std::strcmp(argv[argi + 1], "mjpegtools-new") == 0)
991 encoder = mpeg_encoder_mjpegtools_new;
995 std::cerr << "Invalid argument to --encoder\n";
996 print_usage(std::cerr, argv[0]);
1001 else if (argv[argi][0] == '-')
1003 std::cerr << "Invalid option: " << argv[argi] << "\n";
1004 print_usage(std::cerr, argv[0]);
1005 return EXIT_FAILURE;
1013 // Look for a starting URL or filename and (except in preview
1014 // mode) an output directory after the options.
1015 if (argc - argi != (preview_mode ? 1 : 2))
1017 print_usage(std::cerr, argv[0]);
1018 return EXIT_FAILURE;
1020 if (std::strstr(argv[argi], "://"))
1022 // It appears to be an absolute URL, so use it as-is.
1023 menu_url = argv[argi];
1027 // Assume it's a filename. Resolve it to an absolute URL.
1028 std::string path(argv[argi]);
1029 if (!Glib::path_is_absolute(path))
1030 path = Glib::build_filename(Glib::get_current_dir(), path);
1031 menu_url = Glib::filename_to_uri(path);
1034 output_dir = argv[argi + 1];
1036 // Initialise Mozilla
1037 browser_widget::initialiser browser_init;
1038 set_browser_preferences();
1040 null_prompt_service::install();
1042 // Run the browser/converter
1043 webdvd_window window(frame_params, menu_url, output_dir, encoder);
1045 window.signal_hide().connect(SigC::slot(&Gtk::Main::quit));
1048 return ((preview_mode || window.is_finished())
1052 catch (std::exception & e)
1054 std::cerr << "Fatal error: " << e.what() << "\n";
1055 return EXIT_FAILURE;