1 // Copyright 2005-6 Ben Hutchings <ben@decadentplace.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();
172 void on_net_state_change(const char * uri, gint flags, guint status);
173 bool browser_is_busy() const
175 return pending_window_update_ || pending_req_count_;
178 void save_screenshot();
179 void process_links(nsIPresShell * pres_shell,
180 nsIPresContext * pres_context,
181 nsIDOMWindow * dom_window);
183 video::frame_params frame_params_;
184 std::string output_dir_;
185 mpeg_encoder encoder_;
186 browser_widget browser_widget_;
187 nsCOMPtr<nsIStyleSheet> stylesheet_;
189 dvd_contents contents_;
190 typedef std::map<std::string, dvd_contents::pgc_ref> resource_map_type;
191 resource_map_type resource_map_;
193 std::queue<std::string> page_queue_;
194 bool pending_window_update_;
195 int pending_req_count_;
196 bool have_tweaked_page_;
197 std::auto_ptr<temp_file> background_temp_;
199 std::auto_ptr<page_state> page_state_;
204 webdvd_window::webdvd_window(
205 const video::frame_params & frame_params,
206 const std::string & main_page_uri,
207 const std::string & output_dir,
208 mpeg_encoder encoder)
209 : frame_params_(frame_params),
210 output_dir_(output_dir),
212 stylesheet_(load_css("file://" WEBDVD_LIB_DIR "/webdvd.css")),
213 pending_window_update_(false),
214 pending_req_count_(0),
215 have_tweaked_page_(false),
218 set_size_request(frame_params_.width, frame_params_.height);
219 set_resizable(false);
221 add(browser_widget_);
222 browser_widget_.show();
223 browser_widget_.signal_net_state().connect(
224 SigC::slot(*this, &webdvd_window::on_net_state_change));
226 add_menu(main_page_uri);
230 bool webdvd_window::is_finished() const
235 dvd_contents::pgc_ref webdvd_window::add_menu(const std::string & uri)
237 dvd_contents::pgc_ref next_menu(dvd_contents::menu_pgc,
238 contents_.menus.size());
239 std::pair<resource_map_type::iterator, bool> insert_result(
240 resource_map_.insert(std::make_pair(uri, next_menu)));
242 if (!insert_result.second)
244 return insert_result.first->second;
248 page_queue_.push(uri);
249 contents_.menus.resize(contents_.menus.size() + 1);
254 dvd_contents::pgc_ref webdvd_window::add_title(const std::string & uri)
256 dvd_contents::pgc_ref next_title(dvd_contents::title_pgc,
257 contents_.titles.size());
258 std::pair<resource_map_type::iterator, bool> insert_result(
259 resource_map_.insert(std::make_pair(uri, next_title)));
261 if (!insert_result.second)
263 return insert_result.first->second;
267 Glib::ustring hostname;
268 std::string path(Glib::filename_from_uri(uri, hostname));
269 // FIXME: Should check the hostname
273 // Store a reference to a linked VOB file, or the contents
274 // of a linked VOB list file.
275 if (path.compare(path.size() - 4, 4, ".vob") == 0)
277 if (!Glib::file_test(path, Glib::FILE_TEST_IS_REGULAR))
278 throw std::runtime_error(
279 path + " is missing or not a regular file");
286 assert(path.compare(path.size() - 8, 8, ".voblist") == 0);
287 read_vob_list(path).swap(list);
290 contents_.titles.resize(contents_.titles.size() + 1);
291 contents_.titles.back().swap(list);
296 void webdvd_window::load_next_page()
298 assert(!page_queue_.empty());
299 const std::string & uri = page_queue_.front();
300 std::cout << "loading " << uri << std::endl;
302 browser_widget_.load_uri(uri);
305 void webdvd_window::on_net_state_change(const char * uri,
306 gint flags, guint status)
308 # ifdef DEBUG_ON_NET_STATE_CHANGE
309 std::cout << "webdvd_window::on_net_state_change(";
311 std::cout << '"' << uri << '"';
316 gint flags_left = flags;
317 static const struct {
321 { GTK_MOZ_EMBED_FLAG_START, "STATE_START" },
322 { GTK_MOZ_EMBED_FLAG_REDIRECTING, "STATE_REDIRECTING" },
323 { GTK_MOZ_EMBED_FLAG_TRANSFERRING, "STATE_TRANSFERRING" },
324 { GTK_MOZ_EMBED_FLAG_NEGOTIATING, "STATE_NEGOTIATING" },
325 { GTK_MOZ_EMBED_FLAG_STOP, "STATE_STOP" },
326 { GTK_MOZ_EMBED_FLAG_IS_REQUEST, "STATE_IS_REQUEST" },
327 { GTK_MOZ_EMBED_FLAG_IS_DOCUMENT, "STATE_IS_DOCUMENT" },
328 { GTK_MOZ_EMBED_FLAG_IS_NETWORK, "STATE_IS_NETWORK" },
329 { GTK_MOZ_EMBED_FLAG_IS_WINDOW, "STATE_IS_WINDOW" }
331 for (int i = 0; i != sizeof(flag_names)/sizeof(flag_names[0]); ++i)
333 if (flags & flag_names[i].value)
335 std::cout << flag_names[i].name;
336 flags_left -= flag_names[i].value;
342 std::cout << "0x" << std::setbase(16) << flags_left;
344 std::cout << ", " << "0x" << std::setbase(16) << status << ")\n";
345 # endif // DEBUG_ON_NET_STATE_CHANGE
347 if (flags & GTK_MOZ_EMBED_FLAG_IS_REQUEST)
349 if (flags & GTK_MOZ_EMBED_FLAG_START)
350 ++pending_req_count_;
352 if (flags & GTK_MOZ_EMBED_FLAG_STOP)
354 assert(pending_req_count_ != 0);
355 --pending_req_count_;
359 if (flags & GTK_MOZ_EMBED_FLAG_IS_DOCUMENT
360 && flags & GTK_MOZ_EMBED_FLAG_START)
362 pending_window_update_ = true;
363 have_tweaked_page_ = false;
366 if (flags & GTK_MOZ_EMBED_FLAG_IS_WINDOW
367 && flags & GTK_MOZ_EMBED_FLAG_STOP)
369 // Check whether the load was successful, ignoring this
371 if (status != NS_IMAGELIB_ERROR_LOAD_ABORTED)
374 pending_window_update_ = false;
377 if (!browser_is_busy())
387 catch (std::exception & e)
389 std::cerr << "Fatal error";
390 if (!page_queue_.empty())
391 std::cerr << " while processing <" << page_queue_.front()
393 std::cerr << ": " << e.what() << "\n";
396 catch (Glib::Exception & e)
398 std::cerr << "Fatal error";
399 if (!page_queue_.empty())
400 std::cerr << " while processing <" << page_queue_.front()
402 std::cerr << ": " << e.what() << "\n";
408 bool webdvd_window::process_page()
410 assert(!page_queue_.empty());
412 nsCOMPtr<nsIWebBrowser> browser(browser_widget_.get_browser());
413 nsCOMPtr<nsIDocShell> doc_shell(do_GetInterface(browser));
415 nsCOMPtr<nsIPresShell> pres_shell;
416 check(doc_shell->GetPresShell(getter_AddRefs(pres_shell)));
417 nsCOMPtr<nsIPresContext> pres_context;
418 check(doc_shell->GetPresContext(getter_AddRefs(pres_context)));
419 nsCOMPtr<nsIDOMWindow> dom_window;
420 check(browser->GetContentDOMWindow(getter_AddRefs(dom_window)));
422 // If we haven't done so already, apply the stylesheet and
423 // disable scrollbars.
424 if (!have_tweaked_page_)
426 apply_style_sheet(stylesheet_, pres_shell);
428 // This actually only needs to be done once.
429 nsCOMPtr<nsIDOMBarProp> dom_bar_prop;
430 check(dom_window->GetScrollbars(getter_AddRefs(dom_bar_prop)));
431 check(dom_bar_prop->SetVisible(false));
433 have_tweaked_page_ = true;
435 // Might need to wait a while for things to load or more
436 // likely for a re-layout.
437 if (browser_is_busy())
441 // All further work should only be done if we're not in preview mode.
442 if (!output_dir_.empty())
444 // If we haven't already started work on this menu, save a
445 // screenshot of its normal appearance.
446 if (!page_state_.get())
449 // Start or continue processing links.
450 process_links(pres_shell, pres_context, dom_window);
452 // If we've finished work on the links, move on to the
453 // next page, if any, or else generate the DVD filesystem.
454 if (!page_state_.get())
457 if (page_queue_.empty())
459 generate_dvd(contents_, output_dir_);
472 void webdvd_window::save_screenshot()
474 Glib::RefPtr<Gdk::Window> window(get_window());
476 window->process_updates(true);
478 background_temp_.reset(new temp_file("webdvd-back-"));
479 background_temp_->close();
480 std::cout << "saving " << background_temp_->get_name() << std::endl;
481 Gdk::Pixbuf::create(Glib::RefPtr<Gdk::Drawable>(window),
482 window->get_colormap(),
484 frame_params_.width, frame_params_.height)
485 ->save(background_temp_->get_name(), "png");
488 struct webdvd_window::page_state
490 page_state(nsIDOMDocument * doc, int width, int height)
491 : diff_pixbuf(Gdk::Pixbuf::create(
493 true, 8, // has_alpha, bits_per_sample
495 spumux_temp("webdvd-spumux-"),
496 links_temp("webdvd-links-"),
505 Glib::RefPtr<Gdk::Pixbuf> diff_pixbuf;
507 temp_file spumux_temp;
508 std::ofstream spumux_file;
510 temp_file links_temp;
513 link_iterator links_it, links_end;
517 Glib::RefPtr<Gdk::Pixbuf> norm_pixbuf;
520 void webdvd_window::process_links(nsIPresShell * pres_shell,
521 nsIPresContext * pres_context,
522 nsIDOMWindow * dom_window)
524 Glib::RefPtr<Gdk::Window> window(get_window());
527 nsCOMPtr<nsIDOMDocument> basic_doc;
528 check(dom_window->GetDocument(getter_AddRefs(basic_doc)));
529 nsCOMPtr<nsIDOMNSDocument> ns_doc(do_QueryInterface(basic_doc));
531 nsCOMPtr<nsIEventStateManager> event_state_man(
532 pres_context->EventStateManager()); // does not AddRef
533 assert(event_state_man);
534 nsCOMPtr<nsIDOMDocumentEvent> event_factory(
535 do_QueryInterface(basic_doc));
536 assert(event_factory);
537 nsCOMPtr<nsIDOMDocumentView> doc_view(do_QueryInterface(basic_doc));
539 nsCOMPtr<nsIDOMAbstractView> view;
540 check(doc_view->GetDefaultView(getter_AddRefs(view)));
542 // Set up or recover our iteration state.
543 std::auto_ptr<page_state> state(page_state_);
548 basic_doc, frame_params_.width, frame_params_.height));
550 state->spumux_file.open(state->spumux_temp.get_name().c_str());
551 state->spumux_file <<
554 " <spu force='yes' start='00:00:00.00'\n"
555 " highlight='" << state->links_temp.get_name() << "'\n"
556 " select='" << state->links_temp.get_name() << "'>\n";
559 rectangle window_rect = {
560 0, 0, frame_params_.width, frame_params_.height
563 unsigned menu_num = resource_map_[page_queue_.front()].index;
565 for (/* no initialisation */;
566 state->links_it != state->links_end;
569 nsCOMPtr<nsIDOMNode> node(*state->links_it);
571 // Find the link URI and separate any fragment from it.
572 nsCOMPtr<nsILink> link(do_QueryInterface(node));
574 nsCOMPtr<nsIURI> uri_iface;
575 check(link->GetHrefURI(getter_AddRefs(uri_iface)));
576 std::string uri_and_fragment, uri, fragment;
578 nsCString uri_and_fragment_ns;
579 check(uri_iface->GetSpec(uri_and_fragment_ns));
580 uri_and_fragment.assign(uri_and_fragment_ns.BeginReading(),
581 uri_and_fragment_ns.EndReading());
583 std::size_t hash_pos = uri_and_fragment.find('#');
584 uri.assign(uri_and_fragment, 0, hash_pos);
585 if (hash_pos != std::string::npos)
586 fragment.assign(uri_and_fragment,
587 hash_pos + 1, std::string::npos);
590 // Is this a new link?
591 if (!state->link_changing)
593 // Find a rectangle enclosing the link and clip it to the
595 nsCOMPtr<nsIDOMElement> elem(do_QueryInterface(node));
597 state->link_rect = get_elem_rect(ns_doc, elem);
598 state->link_rect &= window_rect;
600 if (state->link_rect.empty())
602 std::cerr << "Ignoring invisible link to "
603 << uri_and_fragment << "\n";
609 if (state->link_num >= unsigned(dvd::menu_buttons_max))
611 if (state->link_num == unsigned(dvd::menu_buttons_max))
612 std::cerr << "No more than " << dvd::menu_buttons_max
613 << " buttons can be placed on a menu\n";
614 std::cerr << "Ignoring link to " << uri_and_fragment
619 state->spumux_file <<
620 " <button x0='" << state->link_rect.left << "'"
621 " y0='" << state->link_rect.top << "'"
622 " x1='" << state->link_rect.right - 1 << "'"
623 " y1='" << state->link_rect.bottom - 1 << "'/>\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_contents::pgc_ref target;
629 // FIXME: This is a bit of a hack. Perhaps we could decide
630 // later based on the MIME type determined by Mozilla?
632 && uri.compare(uri.size() - 4, 4, ".vob") == 0)
634 && uri.compare(uri.size() - 8, 8, ".voblist") == 0))
637 check(uri_iface->SchemeIs("file", &is_file));
640 std::cerr << "Links to video must use the file:"
644 target = add_title(uri);
646 std::strtoul(fragment.c_str(), NULL, 10);
650 target = add_menu(uri);
651 // TODO: If there's a fragment, work out which button
652 // is closest and set target.sub_index.
654 contents_.menus[menu_num].entries.push_back(target);
656 nsCOMPtr<nsIContent> content(do_QueryInterface(node));
658 nsCOMPtr<nsIDOMEventTarget> event_target(
659 do_QueryInterface(node));
660 assert(event_target);
662 state->norm_pixbuf = Gdk::Pixbuf::create(
663 Glib::RefPtr<Gdk::Drawable>(window),
664 window->get_colormap(),
665 state->link_rect.left,
666 state->link_rect.top,
669 state->link_rect.right - state->link_rect.left,
670 state->link_rect.bottom - state->link_rect.top);
672 nsCOMPtr<nsIDOMEvent> event;
673 check(event_factory->CreateEvent(
674 NS_ConvertASCIItoUTF16("MouseEvents"),
675 getter_AddRefs(event)));
676 nsCOMPtr<nsIDOMMouseEvent> mouse_event(
677 do_QueryInterface(event));
679 check(mouse_event->InitMouseEvent(
680 NS_ConvertASCIItoUTF16("mouseover"),
684 0, // detail: mouse click count
685 state->link_rect.left, // screenX
686 state->link_rect.top, // screenY
687 state->link_rect.left, // clientX
688 state->link_rect.top, // clientY
689 false, false, false, false, // qualifiers
690 0, // button: left (or primary)
691 0)); // related target
693 check(event_target->DispatchEvent(mouse_event,
695 check(event_state_man->SetContentState(content,
696 NS_EVENT_STATE_HOVER));
698 pres_shell->FlushPendingNotifications(true);
700 // We may have to exit and wait for image loading
701 // to complete, at which point we will be called
703 if (browser_is_busy())
705 state->link_changing = true;
711 window->process_updates(true);
713 Glib::RefPtr<Gdk::Pixbuf> changed_pixbuf(
715 Glib::RefPtr<Gdk::Drawable>(window),
716 window->get_colormap(),
717 state->link_rect.left,
718 state->link_rect.top,
721 state->link_rect.right - state->link_rect.left,
722 state->link_rect.bottom - state->link_rect.top));
727 state->link_rect.left,
728 state->link_rect.top,
729 state->link_rect.right - state->link_rect.left,
730 state->link_rect.bottom - state->link_rect.top);
733 quantise_rgba_pixbuf(state->diff_pixbuf, dvd::button_n_colours);
735 std::cout << "saving " << state->links_temp.get_name()
737 state->diff_pixbuf->save(state->links_temp.get_name(), "png");
739 state->spumux_file <<
744 state->spumux_file.close();
746 // TODO: if (!state->spumux_file) throw ...
749 std::ostringstream command_stream;
750 if (encoder_ == mpeg_encoder_ffmpeg)
754 << " -f image2 -vcodec png -i "
755 << background_temp_->get_name()
756 << " -target " << frame_params_.ffmpeg_name << "-dvd"
757 << " -vcodec mpeg2video -an -y /dev/stdout"
758 << " | spumux -v0 -mdvd " << state->spumux_temp.get_name()
759 << " > " << contents_.menus[menu_num].vob_temp->get_name();
763 assert(encoder_ == mpeg_encoder_mjpegtools_old
764 || encoder_ == mpeg_encoder_mjpegtools_new);
767 << background_temp_->get_name()
768 << " | ppmtoy4m -v0 -n1 -F"
769 << frame_params_.rate_numer
770 << ":" << frame_params_.rate_denom
771 << " -A" << frame_params_.pixel_ratio_width
772 << ":" << frame_params_.pixel_ratio_height
774 // The chroma subsampling keywords changed between
775 // versions 1.6.2 and 1.8 of mjpegtools. There is no
776 // keyword that works with both.
777 if (encoder_ == mpeg_encoder_mjpegtools_old)
778 command_stream << "-S420_mpeg2";
780 command_stream << "-S420mpeg2";
782 << (" | mpeg2enc -v0 -f8 -a2 -o/dev/stdout"
783 " | mplex -v0 -f8 -o/dev/stdout /dev/stdin"
784 " | spumux -v0 -mdvd ")
785 << state->spumux_temp.get_name()
787 << contents_.menus[menu_num].vob_temp->get_name();
789 std::string command(command_stream.str());
790 const char * argv[] = {
791 "/bin/sh", "-c", command.c_str(), 0
793 std::cout << "running " << argv[2] << std::endl;
795 Glib::spawn_sync(".",
796 Glib::ArrayHandle<std::string>(
797 argv, sizeof(argv)/sizeof(argv[0]),
798 Glib::OWNERSHIP_NONE),
799 Glib::SPAWN_STDOUT_TO_DEV_NULL,
803 if (command_result != 0)
804 throw std::runtime_error("spumux pipeline failed");
808 const video::frame_params & lookup_frame_params(const char * str)
811 static const char * const known_strings[] = {
817 for (std::size_t i = 0;
818 i != sizeof(known_strings)/sizeof(known_strings[0]);
820 if (std::strcmp(str, known_strings[i]) == 0)
822 ? video::frame_params_625
823 : video::frame_params_525;
824 throw std::runtime_error(
825 std::string("Invalid video standard: ").append(str));
828 void print_usage(std::ostream & stream, const char * command_name)
831 "Usage: " << command_name << " [gtk-options] [--preview]\n"
832 " [--video-std {525|525/60|NTSC|ntsc"
833 " | 625|625/50|PAL|pal}]\n"
834 " [--encoder {mjpegtools|mjpegtools-old}]\n"
835 " menu-url [output-dir]\n";
838 void set_browser_preferences()
840 nsCOMPtr<nsIPrefService> pref_service;
841 static const nsCID pref_service_cid = NS_PREFSERVICE_CID;
842 check(CallGetService<nsIPrefService>(pref_service_cid,
843 getter_AddRefs(pref_service)));
844 nsCOMPtr<nsIPrefBranch> pref_branch;
846 // Disable IE-compatibility kluge that causes backgrounds to
847 // sometimes/usually be missing from snapshots. This is only
848 // effective from Mozilla 1.8 onward.
849 # if MOZ_VERSION_MAJOR > 1 \
850 || (MOZ_VERSION_MAJOR == 1 && MOZ_VERSION_MINOR >= 8)
851 check(pref_service->GetDefaultBranch("layout",
852 getter_AddRefs(pref_branch)));
853 check(pref_branch->SetBoolPref(
854 "fire_onload_after_image_background_loads",
858 // Set display resolution. With standard-definition video we
859 // will be fitting ~600 pixels across a screen typically
860 // ranging from 10 to 25 inches wide, for a resolution of
861 // 24-60 dpi. I therefore declare the average horizontal
862 // resolution to be 40 dpi. The vertical resolution will be
863 // slightly higher (PAL/SECAM) or lower (NTSC), but
864 // unfortunately Mozilla doesn't support non-square pixels
865 // (and neither do fontconfig or Xft anyway).
866 check(pref_service->GetDefaultBranch("browser.display",
867 getter_AddRefs(pref_branch)));
868 check(pref_branch->SetIntPref("screen_resolution", 40));
873 void fatal_error(const std::string & message)
875 std::cerr << "Fatal error: " << message << "\n";
879 int main(int argc, char ** argv)
883 video::frame_params frame_params = video::frame_params_625;
884 bool preview_mode = false;
885 std::string menu_url;
886 std::string output_dir;
887 mpeg_encoder encoder = mpeg_encoder_ffmpeg;
889 // Do initial option parsing. We have to do this before
890 // letting Gtk parse the arguments since we may need to spawn
895 if (std::strcmp(argv[argi], "--") == 0)
899 else if (std::strcmp(argv[argi], "--help") == 0)
901 print_usage(std::cout, argv[0]);
904 else if (std::strcmp(argv[argi], "--preview") == 0)
909 else if (std::strcmp(argv[argi], "--video-std") == 0)
911 if (argi + 1 == argc)
913 std::cerr << "Missing argument to --video-std\n";
914 print_usage(std::cerr, argv[0]);
917 frame_params = lookup_frame_params(argv[argi + 1]);
926 std::auto_ptr<x_frame_buffer> fb;
929 // Spawn Xvfb and set env variables so that Xlib will use it
930 // Use 8 bits each for RGB components, which should translate into
931 // "enough" bits for YUV components.
932 fb.reset(new x_frame_buffer(frame_params.width,
935 setenv("XAUTHORITY", fb->get_authority().c_str(), true);
936 setenv("DISPLAY", fb->get_display().c_str(), true);
940 Gtk::Main kit(argc, argv);
942 // Complete option parsing with Gtk's options out of the way.
946 if (std::strcmp(argv[argi], "--") == 0)
951 else if (std::strcmp(argv[argi], "--preview") == 0)
955 else if (std::strcmp(argv[argi], "--video-std") == 0)
959 else if (std::strcmp(argv[argi], "--save-temps") == 0)
961 temp_file::keep_all(true);
964 else if (std::strcmp(argv[argi], "--encoder") == 0)
966 if (argi + 1 == argc)
968 std::cerr << "Missing argument to --encoder\n";
969 print_usage(std::cerr, argv[0]);
972 if (std::strcmp(argv[argi + 1], "ffmpeg") == 0)
974 encoder = mpeg_encoder_ffmpeg;
976 else if (std::strcmp(argv[argi + 1], "mjpegtools-old") == 0)
978 encoder = mpeg_encoder_mjpegtools_old;
980 else if (std::strcmp(argv[argi + 1], "mjpegtools") == 0
981 || std::strcmp(argv[argi + 1], "mjpegtools-new") == 0)
983 encoder = mpeg_encoder_mjpegtools_new;
987 std::cerr << "Invalid argument to --encoder\n";
988 print_usage(std::cerr, argv[0]);
993 else if (argv[argi][0] == '-')
995 std::cerr << "Invalid option: " << argv[argi] << "\n";
996 print_usage(std::cerr, argv[0]);
1005 // Look for a starting URL or filename and (except in preview
1006 // mode) an output directory after the options.
1007 if (argc - argi != (preview_mode ? 1 : 2))
1009 print_usage(std::cerr, argv[0]);
1010 return EXIT_FAILURE;
1012 if (std::strstr(argv[argi], "://"))
1014 // It appears to be an absolute URL, so use it as-is.
1015 menu_url = argv[argi];
1019 // Assume it's a filename. Resolve it to an absolute URL.
1020 std::string path(argv[argi]);
1021 if (!Glib::path_is_absolute(path))
1022 path = Glib::build_filename(Glib::get_current_dir(), path);
1023 menu_url = Glib::filename_to_uri(path);
1026 output_dir = argv[argi + 1];
1028 // Initialise Mozilla
1029 browser_widget::initialiser browser_init;
1030 set_browser_preferences();
1032 null_prompt_service::install();
1034 // Run the browser/converter
1035 webdvd_window window(frame_params, menu_url, output_dir, encoder);
1036 Gtk::Main::run(window);
1038 return ((preview_mode || window.is_finished())
1042 catch (std::exception & e)
1044 std::cerr << "Fatal error: " << e.what() << "\n";
1045 return EXIT_FAILURE;