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 { menu_pgc, title_pgc };
178 typedef std::pair<pgc_type, int> pgc_ref;
183 : vob_temp(new temp_file("webdvd-vob-"))
188 boost::shared_ptr<temp_file> vob_temp;
189 std::vector<pgc_ref> entries;
194 explicit title(const std::string & vob_list)
198 std::string vob_list;
201 std::vector<menu> menus;
202 std::vector<title> titles;
205 void generate_dvd(const dvd_contents & contents,
206 const std::string & output_dir);
208 class webdvd_window : public Gtk::Window
212 const video::frame_params & frame_params,
213 const std::string & main_page_uri,
214 const std::string & output_dir);
217 dvd_contents::pgc_ref add_menu(const std::string & uri);
218 dvd_contents::pgc_ref add_title(const std::string & uri);
219 void load_next_page();
220 void on_net_state_change(const char * uri, gint flags, guint status);
221 bool browser_is_busy() const
223 return pending_window_update_ || pending_req_count_;
226 void save_screenshot();
227 void process_links(nsIPresShell * pres_shell,
228 nsIPresContext * pres_context,
229 nsIDOMWindow * dom_window);
231 video::frame_params frame_params_;
232 std::string output_dir_;
233 browser_widget browser_widget_;
234 nsCOMPtr<nsIStyleSheet> stylesheet_;
236 dvd_contents contents_;
237 typedef std::map<std::string, dvd_contents::pgc_ref> resource_map_type;
238 resource_map_type resource_map_;
240 std::queue<std::string> page_queue_;
241 bool pending_window_update_;
242 int pending_req_count_;
243 bool have_tweaked_page_;
244 std::auto_ptr<temp_file> background_temp_;
246 std::auto_ptr<page_state> page_state_;
249 webdvd_window::webdvd_window(
250 const video::frame_params & frame_params,
251 const std::string & main_page_uri,
252 const std::string & output_dir)
253 : frame_params_(frame_params),
254 output_dir_(output_dir),
255 stylesheet_(load_css("file://" WEBDVD_LIB_DIR "/webdvd.css")),
256 pending_window_update_(false),
257 pending_req_count_(0),
258 have_tweaked_page_(false)
260 set_size_request(frame_params_.width, frame_params_.height);
261 set_resizable(false);
263 add(browser_widget_);
264 browser_widget_.show();
265 browser_widget_.signal_net_state().connect(
266 SigC::slot(*this, &webdvd_window::on_net_state_change));
268 add_menu(main_page_uri);
272 dvd_contents::pgc_ref webdvd_window::add_menu(const std::string & uri)
274 dvd_contents::pgc_ref next_menu(dvd_contents::menu_pgc,
275 contents_.menus.size());
276 std::pair<resource_map_type::iterator, bool> insert_result(
277 resource_map_.insert(std::make_pair(uri, next_menu)));
279 if (!insert_result.second)
281 return insert_result.first->second;
285 page_queue_.push(uri);
286 contents_.menus.resize(contents_.menus.size() + 1);
291 dvd_contents::pgc_ref webdvd_window::add_title(const std::string & uri)
293 dvd_contents::pgc_ref next_title(dvd_contents::title_pgc,
294 contents_.titles.size());
295 std::pair<resource_map_type::iterator, bool> insert_result(
296 resource_map_.insert(std::make_pair(uri, next_title)));
298 if (!insert_result.second)
300 return insert_result.first->second;
304 Glib::ustring hostname;
305 std::string filename(Glib::filename_from_uri(uri, hostname));
306 // FIXME: Should check the hostname
308 std::string vob_list;
310 // Store a reference to a linked VOB file, or the contents
311 // of a linked VOB list file.
312 if (filename.compare(filename.size() - 4, 4, ".vob") == 0)
314 if (!Glib::file_test(filename, Glib::FILE_TEST_IS_REGULAR))
315 throw std::runtime_error(
316 filename + " is missing or not a regular file");
318 .append("<vob file='")
319 .append(xml_escape(filename))
324 assert(filename.compare(filename.size() - 8, 8, ".voblist")
326 // TODO: Validate the file contents
327 vob_list.assign(Glib::file_get_contents(filename));
330 contents_.titles.push_back(dvd_contents::title(vob_list));
335 void webdvd_window::load_next_page()
337 assert(!page_queue_.empty());
338 const std::string & uri = page_queue_.front();
339 std::cout << "loading " << uri << std::endl;
341 browser_widget_.load_uri(uri);
344 void webdvd_window::on_net_state_change(const char * uri,
345 gint flags, guint status)
347 # ifdef DEBUG_ON_NET_STATE_CHANGE
348 std::cout << "webdvd_window::on_net_state_change(";
350 std::cout << '"' << uri << '"';
355 gint flags_left = flags;
356 static const struct {
360 { GTK_MOZ_EMBED_FLAG_START, "STATE_START" },
361 { GTK_MOZ_EMBED_FLAG_REDIRECTING, "STATE_REDIRECTING" },
362 { GTK_MOZ_EMBED_FLAG_TRANSFERRING, "STATE_TRANSFERRING" },
363 { GTK_MOZ_EMBED_FLAG_NEGOTIATING, "STATE_NEGOTIATING" },
364 { GTK_MOZ_EMBED_FLAG_STOP, "STATE_STOP" },
365 { GTK_MOZ_EMBED_FLAG_IS_REQUEST, "STATE_IS_REQUEST" },
366 { GTK_MOZ_EMBED_FLAG_IS_DOCUMENT, "STATE_IS_DOCUMENT" },
367 { GTK_MOZ_EMBED_FLAG_IS_NETWORK, "STATE_IS_NETWORK" },
368 { GTK_MOZ_EMBED_FLAG_IS_WINDOW, "STATE_IS_WINDOW" }
370 for (int i = 0; i != sizeof(flag_names)/sizeof(flag_names[0]); ++i)
372 if (flags & flag_names[i].value)
374 std::cout << flag_names[i].name;
375 flags_left -= flag_names[i].value;
381 std::cout << "0x" << std::setbase(16) << flags_left;
383 std::cout << ", " << "0x" << std::setbase(16) << status << ")\n";
384 # endif // DEBUG_ON_NET_STATE_CHANGE
386 if (flags & GTK_MOZ_EMBED_FLAG_IS_REQUEST)
388 if (flags & GTK_MOZ_EMBED_FLAG_START)
389 ++pending_req_count_;
391 if (flags & GTK_MOZ_EMBED_FLAG_STOP)
393 assert(pending_req_count_ != 0);
394 --pending_req_count_;
398 if (flags & GTK_MOZ_EMBED_FLAG_IS_DOCUMENT
399 && flags & GTK_MOZ_EMBED_FLAG_START)
401 pending_window_update_ = true;
402 have_tweaked_page_ = false;
405 if (flags & GTK_MOZ_EMBED_FLAG_IS_WINDOW
406 && flags & GTK_MOZ_EMBED_FLAG_STOP)
408 // Check whether the load was successful, ignoring this
410 if (status != NS_IMAGELIB_ERROR_LOAD_ABORTED)
413 pending_window_update_ = false;
416 if (!browser_is_busy())
423 catch (std::exception & e)
425 std::cerr << "Fatal error";
426 if (!page_queue_.empty())
427 std::cerr << " while processing <" << page_queue_.front()
429 std::cerr << ": " << e.what() << "\n";
435 bool webdvd_window::process_page()
437 assert(!page_queue_.empty());
439 nsCOMPtr<nsIWebBrowser> browser(browser_widget_.get_browser());
440 nsCOMPtr<nsIDocShell> doc_shell(do_GetInterface(browser));
442 nsCOMPtr<nsIPresShell> pres_shell;
443 check(doc_shell->GetPresShell(getter_AddRefs(pres_shell)));
444 nsCOMPtr<nsIPresContext> pres_context;
445 check(doc_shell->GetPresContext(getter_AddRefs(pres_context)));
446 nsCOMPtr<nsIDOMWindow> dom_window;
447 check(browser->GetContentDOMWindow(getter_AddRefs(dom_window)));
449 // If we haven't done so already, apply the stylesheet and
450 // disable scrollbars.
451 if (!have_tweaked_page_)
453 apply_style_sheet(stylesheet_, pres_shell);
455 // This actually only needs to be done once.
456 nsCOMPtr<nsIDOMBarProp> dom_bar_prop;
457 check(dom_window->GetScrollbars(getter_AddRefs(dom_bar_prop)));
458 check(dom_bar_prop->SetVisible(false));
460 have_tweaked_page_ = true;
462 // Might need to wait a while for things to load or more
463 // likely for a re-layout.
464 if (browser_is_busy())
468 // All further work should only be done if we're not in preview mode.
469 if (!output_dir_.empty())
471 // If we haven't already started work on this menu, save a
472 // screenshot of its normal appearance.
473 if (!page_state_.get())
476 // Start or continue processing links.
477 process_links(pres_shell, pres_context, dom_window);
479 // If we've finished work on the links, move on to the
480 // next page, if any, or else generate the DVD filesystem.
481 if (!page_state_.get())
484 if (page_queue_.empty())
486 generate_dvd(contents_, output_dir_);
499 void webdvd_window::save_screenshot()
501 Glib::RefPtr<Gdk::Window> window(get_window());
503 window->process_updates(true);
505 background_temp_.reset(new temp_file("webdvd-back-"));
506 background_temp_->close();
507 std::cout << "saving " << background_temp_->get_name() << std::endl;
508 Gdk::Pixbuf::create(Glib::RefPtr<Gdk::Drawable>(window),
509 window->get_colormap(),
511 frame_params_.width, frame_params_.height)
512 ->save(background_temp_->get_name(), "png");
515 struct webdvd_window::page_state
517 page_state(nsIDOMDocument * doc, int width, int height)
518 : diff_pixbuf(Gdk::Pixbuf::create(
520 true, 8, // has_alpha, bits_per_sample
522 spumux_temp("webdvd-spumux-"),
523 links_temp("webdvd-links-"),
532 Glib::RefPtr<Gdk::Pixbuf> diff_pixbuf;
534 temp_file spumux_temp;
535 std::ofstream spumux_file;
537 temp_file links_temp;
540 link_iterator links_it, links_end;
544 Glib::RefPtr<Gdk::Pixbuf> norm_pixbuf;
547 void webdvd_window::process_links(nsIPresShell * pres_shell,
548 nsIPresContext * pres_context,
549 nsIDOMWindow * dom_window)
551 Glib::RefPtr<Gdk::Window> window(get_window());
554 nsCOMPtr<nsIDOMDocument> basic_doc;
555 check(dom_window->GetDocument(getter_AddRefs(basic_doc)));
556 nsCOMPtr<nsIDOMNSDocument> ns_doc(do_QueryInterface(basic_doc));
558 nsCOMPtr<nsIEventStateManager> event_state_man(
559 pres_context->EventStateManager()); // does not AddRef
560 assert(event_state_man);
561 nsCOMPtr<nsIDOMDocumentEvent> event_factory(
562 do_QueryInterface(basic_doc));
563 assert(event_factory);
564 nsCOMPtr<nsIDOMDocumentView> doc_view(do_QueryInterface(basic_doc));
566 nsCOMPtr<nsIDOMAbstractView> view;
567 check(doc_view->GetDefaultView(getter_AddRefs(view)));
569 // Set up or recover our iteration state.
570 std::auto_ptr<page_state> state(page_state_);
575 basic_doc, frame_params_.width, frame_params_.height));
577 state->spumux_file.open(state->spumux_temp.get_name().c_str());
578 state->spumux_file <<
581 " <spu force='yes' start='00:00:00.00'\n"
582 " highlight='" << state->links_temp.get_name() << "'\n"
583 " select='" << state->links_temp.get_name() << "'>\n";
586 rectangle window_rect = {
587 0, 0, frame_params_.width, frame_params_.height
590 int menu_num = resource_map_[page_queue_.front()].second;
592 for (/* no initialisation */;
593 state->links_it != state->links_end;
596 nsCOMPtr<nsIDOMNode> node(*state->links_it);
598 // Find the link URI.
599 nsCOMPtr<nsILink> link(do_QueryInterface(node));
601 nsCOMPtr<nsIURI> uri;
602 check(link->GetHrefURI(getter_AddRefs(uri)));
603 std::string uri_string;
605 nsCString uri_ns_string;
606 check(uri->GetSpec(uri_ns_string));
607 uri_string.assign(uri_ns_string.BeginReading(),
608 uri_ns_string.EndReading());
610 std::string uri_sans_fragment(uri_string, 0, uri_string.find('#'));
612 // Is this a new link?
613 if (!state->link_changing)
615 // Find a rectangle enclosing the link and clip it to the
617 nsCOMPtr<nsIDOMElement> elem(do_QueryInterface(node));
619 state->link_rect = get_elem_rect(ns_doc, elem);
620 state->link_rect &= window_rect;
622 if (state->link_rect.empty())
624 std::cerr << "Ignoring invisible link to "
625 << uri_string << "\n";
631 if (state->link_num >= dvd::menu_buttons_max)
633 if (state->link_num == dvd::menu_buttons_max)
634 std::cerr << "No more than " << dvd::menu_buttons_max
635 << " buttons can be placed on a menu\n";
636 std::cerr << "Ignoring link to " << uri_string << "\n";
640 state->spumux_file <<
641 " <button x0='" << state->link_rect.left << "'"
642 " y0='" << state->link_rect.top << "'"
643 " x1='" << state->link_rect.right - 1 << "'"
644 " y1='" << state->link_rect.bottom - 1 << "'/>\n";
646 // Check whether this is a link to a video or a page then
647 // add it to the known resources if not already seen; then
648 // add it to the menu entries.
650 check(uri->GetPath(path));
651 dvd_contents::pgc_ref dest_pgc;
652 // FIXME: This is a bit of a hack. Perhaps we could decide
653 // later based on the MIME type determined by Mozilla?
654 if ((path.Length() > 4
655 && std::strcmp(path.EndReading() - 4, ".vob") == 0)
656 || (path.Length() > 8
657 && std::strcmp(path.EndReading() - 8, ".voblist")
661 check(uri->SchemeIs("file", &is_file));
664 std::cerr << "Links to video must use the file:"
668 dest_pgc = add_title(uri_sans_fragment);
672 dest_pgc = add_menu(uri_sans_fragment);
674 contents_.menus[menu_num].entries.push_back(dest_pgc);
676 nsCOMPtr<nsIContent> content(do_QueryInterface(node));
678 nsCOMPtr<nsIDOMEventTarget> event_target(
679 do_QueryInterface(node));
680 assert(event_target);
682 state->norm_pixbuf = Gdk::Pixbuf::create(
683 Glib::RefPtr<Gdk::Drawable>(window),
684 window->get_colormap(),
685 state->link_rect.left,
686 state->link_rect.top,
689 state->link_rect.right - state->link_rect.left,
690 state->link_rect.bottom - state->link_rect.top);
692 nsCOMPtr<nsIDOMEvent> event;
693 check(event_factory->CreateEvent(
694 NS_ConvertASCIItoUTF16("MouseEvents"),
695 getter_AddRefs(event)));
696 nsCOMPtr<nsIDOMMouseEvent> mouse_event(
697 do_QueryInterface(event));
699 check(mouse_event->InitMouseEvent(
700 NS_ConvertASCIItoUTF16("mouseover"),
704 0, // detail: mouse click count
705 state->link_rect.left, // screenX
706 state->link_rect.top, // screenY
707 state->link_rect.left, // clientX
708 state->link_rect.top, // clientY
709 false, false, false, false, // qualifiers
710 0, // button: left (or primary)
711 0)); // related target
713 check(event_target->DispatchEvent(mouse_event,
715 check(event_state_man->SetContentState(content,
716 NS_EVENT_STATE_HOVER));
718 pres_shell->FlushPendingNotifications(true);
720 // We may have to exit and wait for image loading
721 // to complete, at which point we will be called
723 if (browser_is_busy())
725 state->link_changing = true;
731 window->process_updates(true);
733 Glib::RefPtr<Gdk::Pixbuf> changed_pixbuf(
735 Glib::RefPtr<Gdk::Drawable>(window),
736 window->get_colormap(),
737 state->link_rect.left,
738 state->link_rect.top,
741 state->link_rect.right - state->link_rect.left,
742 state->link_rect.bottom - state->link_rect.top));
747 state->link_rect.left,
748 state->link_rect.top,
749 state->link_rect.right - state->link_rect.left,
750 state->link_rect.bottom - state->link_rect.top);
753 quantise_rgba_pixbuf(state->diff_pixbuf, dvd::button_n_colours);
755 std::cout << "saving " << state->links_temp.get_name()
757 state->diff_pixbuf->save(state->links_temp.get_name(), "png");
759 state->spumux_file <<
764 state->spumux_file.close();
766 // TODO: if (!state->spumux_file) throw ...
769 std::ostringstream command_stream;
770 command_stream << "pngtopnm "
771 << background_temp_->get_name()
772 << " | ppmtoy4m -v0 -n1 -F"
773 << frame_params_.rate_numer
774 << ":" << frame_params_.rate_denom
775 << " -A" << frame_params_.pixel_ratio_width
776 << ":" << frame_params_.pixel_ratio_height
777 << (" -Ip -S420_mpeg2"
778 " | mpeg2enc -v0 -f8 -a2 -o/dev/stdout"
779 " | mplex -v0 -f8 -o/dev/stdout /dev/stdin"
780 " | spumux -v0 -mdvd ")
781 << state->spumux_temp.get_name()
783 << contents_.menus[menu_num].vob_temp->get_name();
784 std::string command(command_stream.str());
785 const char * argv[] = {
786 "/bin/sh", "-c", command.c_str(), 0
788 std::cout << "running " << argv[2] << std::endl;
790 Glib::spawn_sync(".",
791 Glib::ArrayHandle<std::string>(
792 argv, sizeof(argv)/sizeof(argv[0]),
793 Glib::OWNERSHIP_NONE),
794 Glib::SPAWN_STDOUT_TO_DEV_NULL,
798 if (command_result != 0)
799 throw std::runtime_error("spumux pipeline failed");
803 void generate_dvd(const dvd_contents & contents,
804 const std::string & output_dir)
806 temp_file temp("webdvd-dvdauthor-");
808 std::ofstream file(temp.get_name().c_str());
810 // We generate code that uses registers in the following way:
813 // g1: current location
814 // g12: location that last jumped to a video
816 // All locations are divided into two bitfields: the least
817 // significant 10 bits are a page/menu number and the most
818 // significant 6 bits are a link/button number, and numbering
819 // starts at 1, not 0. This is done for compatibility with
820 // the encoding of the s8 (button) register.
822 static const int button_mult = dvd::reg_s8_button_mult;
823 static const int menu_mask = button_mult - 1;
824 static const int button_mask = (1 << dvd::reg_bits) - button_mult;
831 for (std::size_t menu_num = 0;
832 menu_num != contents.menus.size();
835 const dvd_contents::menu & menu = contents.menus[menu_num];
839 // This is the first (title) menu, displayed when the
840 // disc is first played.
842 " <pgc entry='title'>\n"
844 // Initialise the current location if it is not set
845 // (all general registers are initially 0).
847 " g1 = " << 1 + button_mult << ";\n";
856 // When a title finishes or the user presses the menu
857 // button, this always jumps to the titleset's root menu.
858 // We want to return the user to the last menu they used.
859 // So we arrange for each titleset's root menu to return
860 // to the vmgm title menu and then dispatch from there to
861 // whatever the correct menu is. We determine the correct
862 // menu by looking at the menu part of g1.
864 file << " g0 = g1 & " << menu_mask << ";\n";
866 // There is a limit of 128 VM instructions in each PGC.
867 // Therefore in each menu's <pre> section we generate
868 // jumps to menus with numbers greater by 512, 256, 128,
869 // ..., 1 where (a) such a menu exists, (b) this menu
870 // number is divisible by twice that increment and (c) the
871 // correct menu is that or a later menu. Thus each menu
872 // has at most 10 such conditional jumps and is reachable
873 // by at most 10 jumps from the title menu. This chain of
874 // jumps might take too long on some players; this has yet
875 // to be investigated.
877 for (std::size_t menu_incr = (menu_mask + 1) / 2;
881 if (menu_num + menu_incr < contents.menus.size()
882 && (menu_num & (menu_incr * 2 - 1)) == 0)
885 " if (g0 ge " << 1 + menu_num + menu_incr
887 " jump menu " << 1 + menu_num + menu_incr
893 // Highlight the appropriate button.
894 " s8 = g1 & " << button_mask << ";\n"
896 " <vob file='" << menu.vob_temp->get_name() << "'/>\n";
898 for (std::size_t button_num = 0;
899 button_num != menu.entries.size();
902 file << " <button> ";
904 if (menu.entries[button_num].first == dvd_contents::menu_pgc)
906 int dest_menu_num = menu.entries[button_num].second;
908 // Look for a button on the new menu that links
909 // back to this one. If there is one, set that to
910 // be the highlighted button; otherwise, use the
912 const std::vector<dvd_contents::pgc_ref> &
914 contents.menus[dest_menu_num].entries;
915 dvd_contents::pgc_ref this_pgc(
916 dvd_contents::menu_pgc, menu_num);
917 std::size_t dest_button_num = dest_menu_entries.size();
918 while (dest_button_num != 0
919 && dest_menu_entries[--dest_button_num] != this_pgc)
923 << (1 + dest_menu_num
924 + (1 + dest_button_num) * button_mult)
925 << "; jump menu " << 1 + dest_menu_num << ";";
929 assert(menu.entries[button_num].first
930 == dvd_contents::title_pgc);
933 << 1 + menu_num + (1 + button_num) * button_mult
935 << 1 + menu.entries[button_num].second << ";";
938 file << " </button>\n";
948 // Generate a titleset for each title. This appears to make
949 // jumping to titles a whole lot simpler (but limits us to 99
951 for (std::size_t title_num = 0;
952 title_num != contents.titles.size();
957 // Generate a dummy menu so that the menu button on the
958 // remote control will work.
960 " <pgc entry='root'>\n"
961 " <pre> jump vmgm menu; </pre>\n"
966 // Record calling location.
967 " <pre> g12 = g1; </pre>\n"
968 << contents.titles[title_num].vob_list <<
969 // If the menu location has not been changed during
970 // the title, set the location to be the following
971 // button in the menu. In any case, return to some
973 " <post> if (g1 eq g12) g1 = g1 + " << button_mult
974 << "; call menu; </post>\n"
986 const char * argv[] = {
988 "-o", output_dir.c_str(),
989 "-x", temp.get_name().c_str(),
993 Glib::spawn_sync(".",
994 Glib::ArrayHandle<std::string>(
995 argv, sizeof(argv)/sizeof(argv[0]),
996 Glib::OWNERSHIP_NONE),
997 Glib::SPAWN_SEARCH_PATH
998 | Glib::SPAWN_STDOUT_TO_DEV_NULL,
1002 if (command_result != 0)
1003 throw std::runtime_error("dvdauthor failed");
1007 const video::frame_params & lookup_frame_params(const char * str)
1010 static const struct { const char * str; bool is_ntsc; }
1016 // For DVD purposes, SECAM can be treated identically to PAL.
1020 for (std::size_t i = 0;
1021 i != sizeof(known_strings)/sizeof(known_strings[0]);
1023 if (std::strcmp(str, known_strings[i].str) == 0)
1024 return known_strings[i].is_ntsc ?
1025 video::ntsc_params : video::pal_params;
1026 throw std::runtime_error(
1027 std::string("Invalid video standard: ").append(str));
1030 void print_usage(std::ostream & stream, const char * command_name)
1032 stream << "Usage: " << command_name
1033 << (" [gtk-options] [--video-std std-name]"
1034 " [--preview] menu-url [output-dir]\n");
1037 void set_browser_preferences()
1039 nsCOMPtr<nsIPrefService> pref_service;
1040 static const nsCID pref_service_cid = NS_PREFSERVICE_CID;
1041 check(CallGetService<nsIPrefService>(pref_service_cid,
1042 getter_AddRefs(pref_service)));
1043 nsCOMPtr<nsIPrefBranch> pref_branch;
1045 // Disable IE-compatibility kluge that causes backgrounds to
1046 // sometimes/usually be missing from snapshots. This is only
1047 // effective from Mozilla 1.8 onward.
1048 # if MOZ_VERSION_MAJOR > 1 \
1049 || (MOZ_VERSION_MAJOR == 1 && MOZ_VERSION_MINOR >= 8)
1050 check(pref_service->GetDefaultBranch("layout",
1051 getter_AddRefs(pref_branch)));
1052 check(pref_branch->SetBoolPref(
1053 "fire_onload_after_image_background_loads",
1057 // Set display resolution. With standard-definition video we
1058 // will be fitting ~600 pixels across a screen typically
1059 // ranging from 10 to 25 inches wide, for a resolution of
1060 // 24-60 dpi. I therefore declare the average horizontal
1061 // resolution to be 40 dpi. The vertical resolution will be
1062 // slightly higher (PAL/SECAM) or lower (NTSC), but
1063 // unfortunately Mozilla doesn't support non-square pixels
1064 // (and neither do fontconfig or Xft anyway).
1065 check(pref_service->GetDefaultBranch("browser.display",
1066 getter_AddRefs(pref_branch)));
1067 check(pref_branch->SetIntPref("screen_resolution", 40));
1072 int main(int argc, char ** argv)
1076 video::frame_params frame_params = video::pal_params;
1077 bool preview_mode = false;
1078 std::string menu_url;
1079 std::string output_dir;
1081 // Do initial option parsing. We have to do this before
1082 // letting Gtk parse the arguments since we may need to spawn
1085 while (argi != argc)
1087 if (std::strcmp(argv[argi], "--") == 0)
1091 else if (std::strcmp(argv[argi], "--help") == 0)
1093 print_usage(std::cout, argv[0]);
1094 return EXIT_SUCCESS;
1096 else if (std::strcmp(argv[argi], "--preview") == 0)
1098 preview_mode = true;
1101 else if (std::strcmp(argv[argi], "--video-std") == 0)
1103 if (argi + 1 == argc)
1105 std::cerr << "Missing argument to --video-std\n";
1106 print_usage(std::cerr, argv[0]);
1107 return EXIT_FAILURE;
1109 frame_params = lookup_frame_params(argv[argi + 1]);
1118 std::auto_ptr<x_frame_buffer> fb;
1121 // Spawn Xvfb and set env variables so that Xlib will use it
1122 // Use 8 bits each for RGB components, which should translate into
1123 // "enough" bits for YUV components.
1124 fb.reset(new x_frame_buffer(frame_params.width,
1125 frame_params.height,
1127 setenv("XAUTHORITY", fb->get_authority().c_str(), true);
1128 setenv("DISPLAY", fb->get_display().c_str(), true);
1132 Gtk::Main kit(argc, argv);
1134 // Complete option parsing with Gtk's options out of the way.
1136 while (argi != argc)
1138 if (std::strcmp(argv[argi], "--") == 0)
1143 else if (std::strcmp(argv[argi], "--preview") == 0)
1147 else if (std::strcmp(argv[argi], "--video-std") == 0)
1151 else if (argv[argi][0] == '-')
1153 std::cerr << "Invalid option: " << argv[argi] << "\n";
1154 print_usage(std::cerr, argv[0]);
1155 return EXIT_FAILURE;
1163 // Look for a starting URL or filename and (except in preview
1164 // mode) an output directory after the options.
1165 if (argc - argi != (preview_mode ? 1 : 2))
1167 print_usage(std::cerr, argv[0]);
1168 return EXIT_FAILURE;
1170 if (std::strstr(argv[argi], "://"))
1172 // It appears to be an absolute URL, so use it as-is.
1173 menu_url = argv[argi];
1177 // Assume it's a filename. Resolve it to an absolute URL.
1178 std::string path(argv[argi]);
1179 if (!Glib::path_is_absolute(path))
1180 path = Glib::build_filename(Glib::get_current_dir(), path);
1181 menu_url = Glib::filename_to_uri(path);
1184 output_dir = argv[argi + 1];
1186 // Initialise Mozilla
1187 browser_widget::initialiser browser_init;
1188 set_browser_preferences();
1190 // Run the browser/converter
1191 webdvd_window window(frame_params, menu_url, output_dir);
1192 Gtk::Main::run(window);
1194 catch (std::exception & e)
1196 std::cerr << "Fatal error: " << e.what() << "\n";
1197 return EXIT_FAILURE;
1200 return EXIT_SUCCESS;