1 // Copyright 2005 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 "link_iterator.hpp"
57 #include "pixbufs.hpp"
58 #include "style_sheets.hpp"
59 #include "temp_file.hpp"
61 #include "x_frame_buffer.hpp"
62 #include "xpcom_support.hpp"
64 using xpcom_support::check;
70 int left, top; // inclusive
71 int right, bottom; // exclusive
73 rectangle operator|=(const rectangle & other)
77 // use current extents unchanged
86 // find rectangle enclosing both extents
87 left = std::min(left, other.left);
88 top = std::min(top, other.top);
89 right = std::max(right, other.right);
90 bottom = std::max(bottom, other.bottom);
96 rectangle operator&=(const rectangle & other)
98 // find rectangle enclosed in both extents
99 left = std::max(left, other.left);
100 top = std::max(top, other.top);
101 right = std::max(left, std::min(right, other.right));
102 bottom = std::max(top, std::min(bottom, other.bottom));
108 return left == right || bottom == top;
112 rectangle get_elem_rect(nsIDOMNSDocument * ns_doc,
113 nsIDOMElement * elem)
117 // Start with this element's bounding box
118 nsCOMPtr<nsIBoxObject> box;
119 check(ns_doc->GetBoxObjectFor(elem, getter_AddRefs(box)));
121 check(box->GetScreenX(&result.left));
122 check(box->GetScreenY(&result.top));
123 check(box->GetWidth(&width));
124 check(box->GetHeight(&height));
125 result.right = result.left + width;
126 result.bottom = result.top + height;
128 // Merge bounding boxes of all child elements
129 for (child_iterator it = child_iterator(elem), end; it != end; ++it)
131 nsCOMPtr<nsIDOMNode> child_node(*it);
133 if (check(child_node->GetNodeType(&child_type)),
134 child_type == nsIDOMNode::ELEMENT_NODE)
136 nsCOMPtr<nsIDOMElement> child_elem(
137 do_QueryInterface(child_node));
138 result |= get_elem_rect(ns_doc, child_elem);
146 std::string xml_escape(const std::string & str)
149 std::size_t begin = 0;
153 std::size_t end = str.find_first_of("\"&'<>", begin);
154 result.append(str, begin, end - begin);
155 if (end == std::string::npos)
158 const char * entity = NULL;
161 case '"': entity = """; break;
162 case '&': entity = "&"; break;
163 case '\'': entity = "'"; break;
164 case '<': entity = "<"; break;
165 case '>': entity = ">"; break;
168 result.append(entity);
177 enum pgc_type { unknown_pgc, menu_pgc, title_pgc };
181 explicit pgc_ref(pgc_type type = unknown_pgc,
184 : type(type), index(index), sub_index(sub_index)
186 bool operator==(const pgc_ref & other) const
188 return type == other.type && index == other.index;
190 bool operator!=(const pgc_ref & other) const
192 return !(*this == other);
195 pgc_type type; // Menu or title reference?
196 unsigned index; // Menu or title index (within resp. vector)
197 unsigned sub_index; // Button or chapter number (1-based; 0 if
198 // unspecified; not compared!)
204 : vob_temp(new temp_file("webdvd-vob-"))
209 boost::shared_ptr<temp_file> vob_temp;
210 std::vector<pgc_ref> entries;
215 explicit title(const std::string & vob_list)
219 std::string vob_list;
222 std::vector<menu> menus;
223 std::vector<title> titles;
226 void generate_dvd(const dvd_contents & contents,
227 const std::string & output_dir);
229 class webdvd_window : public Gtk::Window
233 const video::frame_params & frame_params,
234 const std::string & main_page_uri,
235 const std::string & output_dir);
238 dvd_contents::pgc_ref add_menu(const std::string & uri);
239 dvd_contents::pgc_ref add_title(const std::string & uri);
240 void load_next_page();
241 void on_net_state_change(const char * uri, gint flags, guint status);
242 bool browser_is_busy() const
244 return pending_window_update_ || pending_req_count_;
247 void save_screenshot();
248 void process_links(nsIPresShell * pres_shell,
249 nsIPresContext * pres_context,
250 nsIDOMWindow * dom_window);
252 video::frame_params frame_params_;
253 std::string output_dir_;
254 browser_widget browser_widget_;
255 nsCOMPtr<nsIStyleSheet> stylesheet_;
257 dvd_contents contents_;
258 typedef std::map<std::string, dvd_contents::pgc_ref> resource_map_type;
259 resource_map_type resource_map_;
261 std::queue<std::string> page_queue_;
262 bool pending_window_update_;
263 int pending_req_count_;
264 bool have_tweaked_page_;
265 std::auto_ptr<temp_file> background_temp_;
267 std::auto_ptr<page_state> page_state_;
270 webdvd_window::webdvd_window(
271 const video::frame_params & frame_params,
272 const std::string & main_page_uri,
273 const std::string & output_dir)
274 : frame_params_(frame_params),
275 output_dir_(output_dir),
276 stylesheet_(load_css("file://" WEBDVD_LIB_DIR "/webdvd.css")),
277 pending_window_update_(false),
278 pending_req_count_(0),
279 have_tweaked_page_(false)
281 set_size_request(frame_params_.width, frame_params_.height);
282 set_resizable(false);
284 add(browser_widget_);
285 browser_widget_.show();
286 browser_widget_.signal_net_state().connect(
287 SigC::slot(*this, &webdvd_window::on_net_state_change));
289 add_menu(main_page_uri);
293 dvd_contents::pgc_ref webdvd_window::add_menu(const std::string & uri)
295 dvd_contents::pgc_ref next_menu(dvd_contents::menu_pgc,
296 contents_.menus.size());
297 std::pair<resource_map_type::iterator, bool> insert_result(
298 resource_map_.insert(std::make_pair(uri, next_menu)));
300 if (!insert_result.second)
302 return insert_result.first->second;
306 page_queue_.push(uri);
307 contents_.menus.resize(contents_.menus.size() + 1);
312 dvd_contents::pgc_ref webdvd_window::add_title(const std::string & uri)
314 dvd_contents::pgc_ref next_title(dvd_contents::title_pgc,
315 contents_.titles.size());
316 std::pair<resource_map_type::iterator, bool> insert_result(
317 resource_map_.insert(std::make_pair(uri, next_title)));
319 if (!insert_result.second)
321 return insert_result.first->second;
325 Glib::ustring hostname;
326 std::string filename(Glib::filename_from_uri(uri, hostname));
327 // FIXME: Should check the hostname
329 std::string vob_list;
331 // Store a reference to a linked VOB file, or the contents
332 // of a linked VOB list file.
333 if (filename.compare(filename.size() - 4, 4, ".vob") == 0)
335 if (!Glib::file_test(filename, Glib::FILE_TEST_IS_REGULAR))
336 throw std::runtime_error(
337 filename + " is missing or not a regular file");
339 .append("<vob file='")
340 .append(xml_escape(filename))
345 assert(filename.compare(filename.size() - 8, 8, ".voblist")
347 // TODO: Validate the file contents
348 vob_list.assign(Glib::file_get_contents(filename));
351 contents_.titles.push_back(dvd_contents::title(vob_list));
356 void webdvd_window::load_next_page()
358 assert(!page_queue_.empty());
359 const std::string & uri = page_queue_.front();
360 std::cout << "loading " << uri << std::endl;
362 browser_widget_.load_uri(uri);
365 void webdvd_window::on_net_state_change(const char * uri,
366 gint flags, guint status)
368 # ifdef DEBUG_ON_NET_STATE_CHANGE
369 std::cout << "webdvd_window::on_net_state_change(";
371 std::cout << '"' << uri << '"';
376 gint flags_left = flags;
377 static const struct {
381 { GTK_MOZ_EMBED_FLAG_START, "STATE_START" },
382 { GTK_MOZ_EMBED_FLAG_REDIRECTING, "STATE_REDIRECTING" },
383 { GTK_MOZ_EMBED_FLAG_TRANSFERRING, "STATE_TRANSFERRING" },
384 { GTK_MOZ_EMBED_FLAG_NEGOTIATING, "STATE_NEGOTIATING" },
385 { GTK_MOZ_EMBED_FLAG_STOP, "STATE_STOP" },
386 { GTK_MOZ_EMBED_FLAG_IS_REQUEST, "STATE_IS_REQUEST" },
387 { GTK_MOZ_EMBED_FLAG_IS_DOCUMENT, "STATE_IS_DOCUMENT" },
388 { GTK_MOZ_EMBED_FLAG_IS_NETWORK, "STATE_IS_NETWORK" },
389 { GTK_MOZ_EMBED_FLAG_IS_WINDOW, "STATE_IS_WINDOW" }
391 for (int i = 0; i != sizeof(flag_names)/sizeof(flag_names[0]); ++i)
393 if (flags & flag_names[i].value)
395 std::cout << flag_names[i].name;
396 flags_left -= flag_names[i].value;
402 std::cout << "0x" << std::setbase(16) << flags_left;
404 std::cout << ", " << "0x" << std::setbase(16) << status << ")\n";
405 # endif // DEBUG_ON_NET_STATE_CHANGE
407 if (flags & GTK_MOZ_EMBED_FLAG_IS_REQUEST)
409 if (flags & GTK_MOZ_EMBED_FLAG_START)
410 ++pending_req_count_;
412 if (flags & GTK_MOZ_EMBED_FLAG_STOP)
414 assert(pending_req_count_ != 0);
415 --pending_req_count_;
419 if (flags & GTK_MOZ_EMBED_FLAG_IS_DOCUMENT
420 && flags & GTK_MOZ_EMBED_FLAG_START)
422 pending_window_update_ = true;
423 have_tweaked_page_ = false;
426 if (flags & GTK_MOZ_EMBED_FLAG_IS_WINDOW
427 && flags & GTK_MOZ_EMBED_FLAG_STOP)
429 // Check whether the load was successful, ignoring this
431 if (status != NS_IMAGELIB_ERROR_LOAD_ABORTED)
434 pending_window_update_ = false;
437 if (!browser_is_busy())
444 catch (std::exception & e)
446 std::cerr << "Fatal error";
447 if (!page_queue_.empty())
448 std::cerr << " while processing <" << page_queue_.front()
450 std::cerr << ": " << e.what() << "\n";
453 catch (Glib::Exception & e)
455 std::cerr << "Fatal error";
456 if (!page_queue_.empty())
457 std::cerr << " while processing <" << page_queue_.front()
459 std::cerr << ": " << e.what() << "\n";
465 bool webdvd_window::process_page()
467 assert(!page_queue_.empty());
469 nsCOMPtr<nsIWebBrowser> browser(browser_widget_.get_browser());
470 nsCOMPtr<nsIDocShell> doc_shell(do_GetInterface(browser));
472 nsCOMPtr<nsIPresShell> pres_shell;
473 check(doc_shell->GetPresShell(getter_AddRefs(pres_shell)));
474 nsCOMPtr<nsIPresContext> pres_context;
475 check(doc_shell->GetPresContext(getter_AddRefs(pres_context)));
476 nsCOMPtr<nsIDOMWindow> dom_window;
477 check(browser->GetContentDOMWindow(getter_AddRefs(dom_window)));
479 // If we haven't done so already, apply the stylesheet and
480 // disable scrollbars.
481 if (!have_tweaked_page_)
483 apply_style_sheet(stylesheet_, pres_shell);
485 // This actually only needs to be done once.
486 nsCOMPtr<nsIDOMBarProp> dom_bar_prop;
487 check(dom_window->GetScrollbars(getter_AddRefs(dom_bar_prop)));
488 check(dom_bar_prop->SetVisible(false));
490 have_tweaked_page_ = true;
492 // Might need to wait a while for things to load or more
493 // likely for a re-layout.
494 if (browser_is_busy())
498 // All further work should only be done if we're not in preview mode.
499 if (!output_dir_.empty())
501 // If we haven't already started work on this menu, save a
502 // screenshot of its normal appearance.
503 if (!page_state_.get())
506 // Start or continue processing links.
507 process_links(pres_shell, pres_context, dom_window);
509 // If we've finished work on the links, move on to the
510 // next page, if any, or else generate the DVD filesystem.
511 if (!page_state_.get())
514 if (page_queue_.empty())
516 generate_dvd(contents_, output_dir_);
529 void webdvd_window::save_screenshot()
531 Glib::RefPtr<Gdk::Window> window(get_window());
533 window->process_updates(true);
535 background_temp_.reset(new temp_file("webdvd-back-"));
536 background_temp_->close();
537 std::cout << "saving " << background_temp_->get_name() << std::endl;
538 Gdk::Pixbuf::create(Glib::RefPtr<Gdk::Drawable>(window),
539 window->get_colormap(),
541 frame_params_.width, frame_params_.height)
542 ->save(background_temp_->get_name(), "png");
545 struct webdvd_window::page_state
547 page_state(nsIDOMDocument * doc, int width, int height)
548 : diff_pixbuf(Gdk::Pixbuf::create(
550 true, 8, // has_alpha, bits_per_sample
552 spumux_temp("webdvd-spumux-"),
553 links_temp("webdvd-links-"),
562 Glib::RefPtr<Gdk::Pixbuf> diff_pixbuf;
564 temp_file spumux_temp;
565 std::ofstream spumux_file;
567 temp_file links_temp;
570 link_iterator links_it, links_end;
574 Glib::RefPtr<Gdk::Pixbuf> norm_pixbuf;
577 void webdvd_window::process_links(nsIPresShell * pres_shell,
578 nsIPresContext * pres_context,
579 nsIDOMWindow * dom_window)
581 Glib::RefPtr<Gdk::Window> window(get_window());
584 nsCOMPtr<nsIDOMDocument> basic_doc;
585 check(dom_window->GetDocument(getter_AddRefs(basic_doc)));
586 nsCOMPtr<nsIDOMNSDocument> ns_doc(do_QueryInterface(basic_doc));
588 nsCOMPtr<nsIEventStateManager> event_state_man(
589 pres_context->EventStateManager()); // does not AddRef
590 assert(event_state_man);
591 nsCOMPtr<nsIDOMDocumentEvent> event_factory(
592 do_QueryInterface(basic_doc));
593 assert(event_factory);
594 nsCOMPtr<nsIDOMDocumentView> doc_view(do_QueryInterface(basic_doc));
596 nsCOMPtr<nsIDOMAbstractView> view;
597 check(doc_view->GetDefaultView(getter_AddRefs(view)));
599 // Set up or recover our iteration state.
600 std::auto_ptr<page_state> state(page_state_);
605 basic_doc, frame_params_.width, frame_params_.height));
607 state->spumux_file.open(state->spumux_temp.get_name().c_str());
608 state->spumux_file <<
611 " <spu force='yes' start='00:00:00.00'\n"
612 " highlight='" << state->links_temp.get_name() << "'\n"
613 " select='" << state->links_temp.get_name() << "'>\n";
616 rectangle window_rect = {
617 0, 0, frame_params_.width, frame_params_.height
620 unsigned menu_num = resource_map_[page_queue_.front()].index;
622 for (/* no initialisation */;
623 state->links_it != state->links_end;
626 nsCOMPtr<nsIDOMNode> node(*state->links_it);
628 // Find the link URI and separate any fragment from it.
629 nsCOMPtr<nsILink> link(do_QueryInterface(node));
631 nsCOMPtr<nsIURI> uri_iface;
632 check(link->GetHrefURI(getter_AddRefs(uri_iface)));
633 std::string uri_and_fragment, uri, fragment;
635 nsCString uri_and_fragment_ns;
636 check(uri_iface->GetSpec(uri_and_fragment_ns));
637 uri_and_fragment.assign(uri_and_fragment_ns.BeginReading(),
638 uri_and_fragment_ns.EndReading());
640 std::size_t hash_pos = uri_and_fragment.find('#');
641 uri.assign(uri_and_fragment, 0, hash_pos);
642 if (hash_pos != std::string::npos)
643 fragment.assign(uri_and_fragment,
644 hash_pos + 1, std::string::npos);
647 // Is this a new link?
648 if (!state->link_changing)
650 // Find a rectangle enclosing the link and clip it to the
652 nsCOMPtr<nsIDOMElement> elem(do_QueryInterface(node));
654 state->link_rect = get_elem_rect(ns_doc, elem);
655 state->link_rect &= window_rect;
657 if (state->link_rect.empty())
659 std::cerr << "Ignoring invisible link to "
660 << uri_and_fragment << "\n";
666 if (state->link_num >= unsigned(dvd::menu_buttons_max))
668 if (state->link_num == unsigned(dvd::menu_buttons_max))
669 std::cerr << "No more than " << dvd::menu_buttons_max
670 << " buttons can be placed on a menu\n";
671 std::cerr << "Ignoring link to " << uri_and_fragment
676 state->spumux_file <<
677 " <button x0='" << state->link_rect.left << "'"
678 " y0='" << state->link_rect.top << "'"
679 " x1='" << state->link_rect.right - 1 << "'"
680 " y1='" << state->link_rect.bottom - 1 << "'/>\n";
682 // Check whether this is a link to a video or a page then
683 // add it to the known resources if not already seen; then
684 // add it to the menu entries.
685 dvd_contents::pgc_ref target;
686 // FIXME: This is a bit of a hack. Perhaps we could decide
687 // later based on the MIME type determined by Mozilla?
689 && uri.compare(uri.size() - 4, 4, ".vob") == 0)
691 && uri.compare(uri.size() - 8, 8, ".voblist") == 0))
694 check(uri_iface->SchemeIs("file", &is_file));
697 std::cerr << "Links to video must use the file:"
701 target = add_title(uri);
703 std::strtoul(fragment.c_str(), NULL, 10);
707 target = add_menu(uri);
708 // TODO: If there's a fragment, work out which button
709 // is closest and set target.sub_index.
711 contents_.menus[menu_num].entries.push_back(target);
713 nsCOMPtr<nsIContent> content(do_QueryInterface(node));
715 nsCOMPtr<nsIDOMEventTarget> event_target(
716 do_QueryInterface(node));
717 assert(event_target);
719 state->norm_pixbuf = Gdk::Pixbuf::create(
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);
729 nsCOMPtr<nsIDOMEvent> event;
730 check(event_factory->CreateEvent(
731 NS_ConvertASCIItoUTF16("MouseEvents"),
732 getter_AddRefs(event)));
733 nsCOMPtr<nsIDOMMouseEvent> mouse_event(
734 do_QueryInterface(event));
736 check(mouse_event->InitMouseEvent(
737 NS_ConvertASCIItoUTF16("mouseover"),
741 0, // detail: mouse click count
742 state->link_rect.left, // screenX
743 state->link_rect.top, // screenY
744 state->link_rect.left, // clientX
745 state->link_rect.top, // clientY
746 false, false, false, false, // qualifiers
747 0, // button: left (or primary)
748 0)); // related target
750 check(event_target->DispatchEvent(mouse_event,
752 check(event_state_man->SetContentState(content,
753 NS_EVENT_STATE_HOVER));
755 pres_shell->FlushPendingNotifications(true);
757 // We may have to exit and wait for image loading
758 // to complete, at which point we will be called
760 if (browser_is_busy())
762 state->link_changing = true;
768 window->process_updates(true);
770 Glib::RefPtr<Gdk::Pixbuf> changed_pixbuf(
772 Glib::RefPtr<Gdk::Drawable>(window),
773 window->get_colormap(),
774 state->link_rect.left,
775 state->link_rect.top,
778 state->link_rect.right - state->link_rect.left,
779 state->link_rect.bottom - state->link_rect.top));
784 state->link_rect.left,
785 state->link_rect.top,
786 state->link_rect.right - state->link_rect.left,
787 state->link_rect.bottom - state->link_rect.top);
790 quantise_rgba_pixbuf(state->diff_pixbuf, dvd::button_n_colours);
792 std::cout << "saving " << state->links_temp.get_name()
794 state->diff_pixbuf->save(state->links_temp.get_name(), "png");
796 state->spumux_file <<
801 state->spumux_file.close();
803 // TODO: if (!state->spumux_file) throw ...
806 std::ostringstream command_stream;
807 command_stream << "pngtopnm "
808 << background_temp_->get_name()
809 << " | ppmtoy4m -v0 -n1 -F"
810 << frame_params_.rate_numer
811 << ":" << frame_params_.rate_denom
812 << " -A" << frame_params_.pixel_ratio_width
813 << ":" << frame_params_.pixel_ratio_height
814 << (" -Ip -S420_mpeg2"
815 " | mpeg2enc -v0 -f8 -a2 -o/dev/stdout"
816 " | mplex -v0 -f8 -o/dev/stdout /dev/stdin"
817 " | spumux -v0 -mdvd ")
818 << state->spumux_temp.get_name()
820 << contents_.menus[menu_num].vob_temp->get_name();
821 std::string command(command_stream.str());
822 const char * argv[] = {
823 "/bin/sh", "-c", command.c_str(), 0
825 std::cout << "running " << argv[2] << std::endl;
827 Glib::spawn_sync(".",
828 Glib::ArrayHandle<std::string>(
829 argv, sizeof(argv)/sizeof(argv[0]),
830 Glib::OWNERSHIP_NONE),
831 Glib::SPAWN_STDOUT_TO_DEV_NULL,
835 if (command_result != 0)
836 throw std::runtime_error("spumux pipeline failed");
840 void generate_dvd(const dvd_contents & contents,
841 const std::string & output_dir)
843 temp_file temp("webdvd-dvdauthor-");
845 std::ofstream file(temp.get_name().c_str());
847 // We generate code that uses registers in the following way:
850 // g1: current location
851 // g12: location that last jumped to a video
853 // All locations are divided into two bitfields: the least
854 // significant 10 bits are a page/menu number and the most
855 // significant 6 bits are a link/button number, and numbering
856 // starts at 1, not 0. This is done for compatibility with
857 // the encoding of the s8 (button) register.
859 static const int button_mult = dvd::reg_s8_button_mult;
860 static const int menu_mask = button_mult - 1;
861 static const int button_mask = (1 << dvd::reg_bits) - button_mult;
868 for (unsigned menu_num = 0;
869 menu_num != contents.menus.size();
872 const dvd_contents::menu & menu = contents.menus[menu_num];
876 // This is the first (title) menu, displayed when the
877 // disc is first played.
879 " <pgc entry='title'>\n"
881 // Initialise the current location if it is not set
882 // (all general registers are initially 0).
884 " g1 = " << 1 + button_mult << ";\n";
893 // When a title finishes or the user presses the menu
894 // button, this always jumps to the titleset's root menu.
895 // We want to return the user to the last menu they used.
896 // So we arrange for each titleset's root menu to return
897 // to the vmgm title menu and then dispatch from there to
898 // whatever the correct menu is. We determine the correct
899 // menu by looking at the menu part of g1.
901 file << " g0 = g1 & " << menu_mask << ";\n";
903 // There is a limit of 128 VM instructions in each PGC.
904 // Therefore in each menu's <pre> section we generate
905 // jumps to menus with numbers greater by 512, 256, 128,
906 // ..., 1 where (a) such a menu exists, (b) this menu
907 // number is divisible by twice that increment and (c) the
908 // correct menu is that or a later menu. Thus each menu
909 // has at most 10 such conditional jumps and is reachable
910 // by at most 10 jumps from the title menu. This chain of
911 // jumps might take too long on some players; this has yet
912 // to be investigated.
914 for (std::size_t menu_incr = (menu_mask + 1) / 2;
918 if (menu_num + menu_incr < contents.menus.size()
919 && (menu_num & (menu_incr * 2 - 1)) == 0)
922 " if (g0 ge " << 1 + menu_num + menu_incr
924 " jump menu " << 1 + menu_num + menu_incr
930 // Highlight the appropriate button.
931 " s8 = g1 & " << button_mask << ";\n"
933 " <vob file='" << menu.vob_temp->get_name() << "'/>\n";
935 for (unsigned button_num = 0;
936 button_num != menu.entries.size();
939 const dvd_contents::pgc_ref & target =
940 menu.entries[button_num];
942 file << " <button> ";
944 if (target.type == dvd_contents::menu_pgc)
946 unsigned target_button_num;
948 if (target.sub_index)
950 target_button_num = target.sub_index;
954 // Look for a button on the new menu that links
955 // back to this one. If there is one, set that to
956 // be the highlighted button; otherwise, use the
958 const std::vector<dvd_contents::pgc_ref> &
959 target_menu_entries =
960 contents.menus[target.index].entries;
961 dvd_contents::pgc_ref this_pgc(dvd_contents::menu_pgc,
963 target_button_num = target_menu_entries.size();
964 while (target_button_num != 0
965 && (target_menu_entries[--target_button_num]
968 target_button_num += 1;
973 + target_button_num * button_mult)
974 << "; jump menu " << 1 + target.index << ";";
978 assert(target.type == dvd_contents::title_pgc);
981 << 1 + menu_num + (1 + button_num) * button_mult
984 if (target.sub_index)
985 file << " chapter " << target.sub_index;
989 file << " </button>\n";
999 // Generate a titleset for each title. This appears to make
1000 // jumping to titles a whole lot simpler (but limits us to 99
1002 for (unsigned title_num = 0;
1003 title_num != contents.titles.size();
1008 // Generate a dummy menu so that the menu button on the
1009 // remote control will work.
1011 " <pgc entry='root'>\n"
1012 " <pre> jump vmgm menu; </pre>\n"
1017 // Record calling location.
1018 " <pre> g12 = g1; </pre>\n"
1019 << contents.titles[title_num].vob_list <<
1020 // If the menu location has not been changed during
1021 // the title, set the location to be the following
1022 // button in the menu. In any case, return to some
1024 " <post> if (g1 eq g12) g1 = g1 + " << button_mult
1025 << "; call menu; </post>\n"
1037 const char * argv[] = {
1039 "-o", output_dir.c_str(),
1040 "-x", temp.get_name().c_str(),
1044 Glib::spawn_sync(".",
1045 Glib::ArrayHandle<std::string>(
1046 argv, sizeof(argv)/sizeof(argv[0]),
1047 Glib::OWNERSHIP_NONE),
1048 Glib::SPAWN_SEARCH_PATH
1049 | Glib::SPAWN_STDOUT_TO_DEV_NULL,
1050 SigC::Slot0<void>(),
1053 if (command_result != 0)
1054 throw std::runtime_error("dvdauthor failed");
1058 const video::frame_params & lookup_frame_params(const char * str)
1061 static const struct { const char * str; bool is_ntsc; }
1067 // For DVD purposes, SECAM can be treated identically to PAL.
1071 for (std::size_t i = 0;
1072 i != sizeof(known_strings)/sizeof(known_strings[0]);
1074 if (std::strcmp(str, known_strings[i].str) == 0)
1075 return known_strings[i].is_ntsc ?
1076 video::ntsc_params : video::pal_params;
1077 throw std::runtime_error(
1078 std::string("Invalid video standard: ").append(str));
1081 void print_usage(std::ostream & stream, const char * command_name)
1083 stream << "Usage: " << command_name
1084 << (" [gtk-options] [--video-std std-name]"
1085 " [--preview] menu-url [output-dir]\n");
1088 void set_browser_preferences()
1090 nsCOMPtr<nsIPrefService> pref_service;
1091 static const nsCID pref_service_cid = NS_PREFSERVICE_CID;
1092 check(CallGetService<nsIPrefService>(pref_service_cid,
1093 getter_AddRefs(pref_service)));
1094 nsCOMPtr<nsIPrefBranch> pref_branch;
1096 // Disable IE-compatibility kluge that causes backgrounds to
1097 // sometimes/usually be missing from snapshots. This is only
1098 // effective from Mozilla 1.8 onward.
1099 # if MOZ_VERSION_MAJOR > 1 \
1100 || (MOZ_VERSION_MAJOR == 1 && MOZ_VERSION_MINOR >= 8)
1101 check(pref_service->GetDefaultBranch("layout",
1102 getter_AddRefs(pref_branch)));
1103 check(pref_branch->SetBoolPref(
1104 "fire_onload_after_image_background_loads",
1108 // Set display resolution. With standard-definition video we
1109 // will be fitting ~600 pixels across a screen typically
1110 // ranging from 10 to 25 inches wide, for a resolution of
1111 // 24-60 dpi. I therefore declare the average horizontal
1112 // resolution to be 40 dpi. The vertical resolution will be
1113 // slightly higher (PAL/SECAM) or lower (NTSC), but
1114 // unfortunately Mozilla doesn't support non-square pixels
1115 // (and neither do fontconfig or Xft anyway).
1116 check(pref_service->GetDefaultBranch("browser.display",
1117 getter_AddRefs(pref_branch)));
1118 check(pref_branch->SetIntPref("screen_resolution", 40));
1123 int main(int argc, char ** argv)
1127 video::frame_params frame_params = video::pal_params;
1128 bool preview_mode = false;
1129 std::string menu_url;
1130 std::string output_dir;
1132 // Do initial option parsing. We have to do this before
1133 // letting Gtk parse the arguments since we may need to spawn
1136 while (argi != argc)
1138 if (std::strcmp(argv[argi], "--") == 0)
1142 else if (std::strcmp(argv[argi], "--help") == 0)
1144 print_usage(std::cout, argv[0]);
1145 return EXIT_SUCCESS;
1147 else if (std::strcmp(argv[argi], "--preview") == 0)
1149 preview_mode = true;
1152 else if (std::strcmp(argv[argi], "--video-std") == 0)
1154 if (argi + 1 == argc)
1156 std::cerr << "Missing argument to --video-std\n";
1157 print_usage(std::cerr, argv[0]);
1158 return EXIT_FAILURE;
1160 frame_params = lookup_frame_params(argv[argi + 1]);
1169 std::auto_ptr<x_frame_buffer> fb;
1172 // Spawn Xvfb and set env variables so that Xlib will use it
1173 // Use 8 bits each for RGB components, which should translate into
1174 // "enough" bits for YUV components.
1175 fb.reset(new x_frame_buffer(frame_params.width,
1176 frame_params.height,
1178 setenv("XAUTHORITY", fb->get_authority().c_str(), true);
1179 setenv("DISPLAY", fb->get_display().c_str(), true);
1183 Gtk::Main kit(argc, argv);
1185 // Complete option parsing with Gtk's options out of the way.
1187 while (argi != argc)
1189 if (std::strcmp(argv[argi], "--") == 0)
1194 else if (std::strcmp(argv[argi], "--preview") == 0)
1198 else if (std::strcmp(argv[argi], "--video-std") == 0)
1202 else if (argv[argi][0] == '-')
1204 std::cerr << "Invalid option: " << argv[argi] << "\n";
1205 print_usage(std::cerr, argv[0]);
1206 return EXIT_FAILURE;
1214 // Look for a starting URL or filename and (except in preview
1215 // mode) an output directory after the options.
1216 if (argc - argi != (preview_mode ? 1 : 2))
1218 print_usage(std::cerr, argv[0]);
1219 return EXIT_FAILURE;
1221 if (std::strstr(argv[argi], "://"))
1223 // It appears to be an absolute URL, so use it as-is.
1224 menu_url = argv[argi];
1228 // Assume it's a filename. Resolve it to an absolute URL.
1229 std::string path(argv[argi]);
1230 if (!Glib::path_is_absolute(path))
1231 path = Glib::build_filename(Glib::get_current_dir(), path);
1232 menu_url = Glib::filename_to_uri(path);
1235 output_dir = argv[argi + 1];
1237 // Initialise Mozilla
1238 browser_widget::initialiser browser_init;
1239 set_browser_preferences();
1241 // Run the browser/converter
1242 webdvd_window window(frame_params, menu_url, output_dir);
1243 Gtk::Main::run(window);
1245 catch (std::exception & e)
1247 std::cerr << "Fatal error: " << e.what() << "\n";
1248 return EXIT_FAILURE;
1251 return EXIT_SUCCESS;