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;
73 int left, top; // inclusive
74 int right, bottom; // exclusive
76 rectangle operator|=(const rectangle & other)
80 // use current extents unchanged
89 // find rectangle enclosing both extents
90 left = std::min(left, other.left);
91 top = std::min(top, other.top);
92 right = std::max(right, other.right);
93 bottom = std::max(bottom, other.bottom);
99 rectangle operator&=(const rectangle & other)
101 // find rectangle enclosed in both extents
102 left = std::max(left, other.left);
103 top = std::max(top, other.top);
104 right = std::max(left, std::min(right, other.right));
105 bottom = std::max(top, std::min(bottom, other.bottom));
111 return left == right || bottom == top;
115 rectangle get_elem_rect(nsIDOMNSDocument * ns_doc,
116 nsIDOMElement * elem)
120 // Start with this element's bounding box
121 nsCOMPtr<nsIBoxObject> box;
122 check(ns_doc->GetBoxObjectFor(elem, getter_AddRefs(box)));
124 check(box->GetScreenX(&result.left));
125 check(box->GetScreenY(&result.top));
126 check(box->GetWidth(&width));
127 check(box->GetHeight(&height));
128 result.right = result.left + width;
129 result.bottom = result.top + height;
131 // Merge bounding boxes of all child elements
132 for (child_iterator it = child_iterator(elem), end; it != end; ++it)
134 nsCOMPtr<nsIDOMNode> child_node(*it);
136 if (check(child_node->GetNodeType(&child_type)),
137 child_type == nsIDOMNode::ELEMENT_NODE)
139 nsCOMPtr<nsIDOMElement> child_elem(
140 do_QueryInterface(child_node));
141 result |= get_elem_rect(ns_doc, child_elem);
149 class webdvd_window : public Gtk::Window
153 const video::frame_params & frame_params,
154 const std::string & main_page_uri,
155 const std::string & output_dir);
157 bool is_finished() const;
160 dvd_contents::pgc_ref add_menu(const std::string & uri);
161 dvd_contents::pgc_ref add_title(const std::string & uri);
162 void load_next_page();
163 void on_net_state_change(const char * uri, gint flags, guint status);
164 bool browser_is_busy() const
166 return pending_window_update_ || pending_req_count_;
169 void save_screenshot();
170 void process_links(nsIPresShell * pres_shell,
171 nsIPresContext * pres_context,
172 nsIDOMWindow * dom_window);
174 video::frame_params frame_params_;
175 std::string output_dir_;
176 browser_widget browser_widget_;
177 nsCOMPtr<nsIStyleSheet> stylesheet_;
179 dvd_contents contents_;
180 typedef std::map<std::string, dvd_contents::pgc_ref> resource_map_type;
181 resource_map_type resource_map_;
183 std::queue<std::string> page_queue_;
184 bool pending_window_update_;
185 int pending_req_count_;
186 bool have_tweaked_page_;
187 std::auto_ptr<temp_file> background_temp_;
189 std::auto_ptr<page_state> page_state_;
194 webdvd_window::webdvd_window(
195 const video::frame_params & frame_params,
196 const std::string & main_page_uri,
197 const std::string & output_dir)
198 : frame_params_(frame_params),
199 output_dir_(output_dir),
200 stylesheet_(load_css("file://" WEBDVD_LIB_DIR "/webdvd.css")),
201 pending_window_update_(false),
202 pending_req_count_(0),
203 have_tweaked_page_(false),
206 set_size_request(frame_params_.width, frame_params_.height);
207 set_resizable(false);
209 add(browser_widget_);
210 browser_widget_.show();
211 browser_widget_.signal_net_state().connect(
212 SigC::slot(*this, &webdvd_window::on_net_state_change));
214 add_menu(main_page_uri);
218 bool webdvd_window::is_finished() const
223 dvd_contents::pgc_ref webdvd_window::add_menu(const std::string & uri)
225 dvd_contents::pgc_ref next_menu(dvd_contents::menu_pgc,
226 contents_.menus.size());
227 std::pair<resource_map_type::iterator, bool> insert_result(
228 resource_map_.insert(std::make_pair(uri, next_menu)));
230 if (!insert_result.second)
232 return insert_result.first->second;
236 page_queue_.push(uri);
237 contents_.menus.resize(contents_.menus.size() + 1);
242 dvd_contents::pgc_ref webdvd_window::add_title(const std::string & uri)
244 dvd_contents::pgc_ref next_title(dvd_contents::title_pgc,
245 contents_.titles.size());
246 std::pair<resource_map_type::iterator, bool> insert_result(
247 resource_map_.insert(std::make_pair(uri, next_title)));
249 if (!insert_result.second)
251 return insert_result.first->second;
255 Glib::ustring hostname;
256 std::string path(Glib::filename_from_uri(uri, hostname));
257 // FIXME: Should check the hostname
261 // Store a reference to a linked VOB file, or the contents
262 // of a linked VOB list file.
263 if (path.compare(path.size() - 4, 4, ".vob") == 0)
265 if (!Glib::file_test(path, Glib::FILE_TEST_IS_REGULAR))
266 throw std::runtime_error(
267 path + " is missing or not a regular file");
274 assert(path.compare(path.size() - 8, 8, ".voblist") == 0);
275 read_vob_list(path).swap(list);
278 contents_.titles.resize(contents_.titles.size() + 1);
279 contents_.titles.back().swap(list);
284 void webdvd_window::load_next_page()
286 assert(!page_queue_.empty());
287 const std::string & uri = page_queue_.front();
288 std::cout << "loading " << uri << std::endl;
290 browser_widget_.load_uri(uri);
293 void webdvd_window::on_net_state_change(const char * uri,
294 gint flags, guint status)
296 # ifdef DEBUG_ON_NET_STATE_CHANGE
297 std::cout << "webdvd_window::on_net_state_change(";
299 std::cout << '"' << uri << '"';
304 gint flags_left = flags;
305 static const struct {
309 { GTK_MOZ_EMBED_FLAG_START, "STATE_START" },
310 { GTK_MOZ_EMBED_FLAG_REDIRECTING, "STATE_REDIRECTING" },
311 { GTK_MOZ_EMBED_FLAG_TRANSFERRING, "STATE_TRANSFERRING" },
312 { GTK_MOZ_EMBED_FLAG_NEGOTIATING, "STATE_NEGOTIATING" },
313 { GTK_MOZ_EMBED_FLAG_STOP, "STATE_STOP" },
314 { GTK_MOZ_EMBED_FLAG_IS_REQUEST, "STATE_IS_REQUEST" },
315 { GTK_MOZ_EMBED_FLAG_IS_DOCUMENT, "STATE_IS_DOCUMENT" },
316 { GTK_MOZ_EMBED_FLAG_IS_NETWORK, "STATE_IS_NETWORK" },
317 { GTK_MOZ_EMBED_FLAG_IS_WINDOW, "STATE_IS_WINDOW" }
319 for (int i = 0; i != sizeof(flag_names)/sizeof(flag_names[0]); ++i)
321 if (flags & flag_names[i].value)
323 std::cout << flag_names[i].name;
324 flags_left -= flag_names[i].value;
330 std::cout << "0x" << std::setbase(16) << flags_left;
332 std::cout << ", " << "0x" << std::setbase(16) << status << ")\n";
333 # endif // DEBUG_ON_NET_STATE_CHANGE
335 if (flags & GTK_MOZ_EMBED_FLAG_IS_REQUEST)
337 if (flags & GTK_MOZ_EMBED_FLAG_START)
338 ++pending_req_count_;
340 if (flags & GTK_MOZ_EMBED_FLAG_STOP)
342 assert(pending_req_count_ != 0);
343 --pending_req_count_;
347 if (flags & GTK_MOZ_EMBED_FLAG_IS_DOCUMENT
348 && flags & GTK_MOZ_EMBED_FLAG_START)
350 pending_window_update_ = true;
351 have_tweaked_page_ = false;
354 if (flags & GTK_MOZ_EMBED_FLAG_IS_WINDOW
355 && flags & GTK_MOZ_EMBED_FLAG_STOP)
357 // Check whether the load was successful, ignoring this
359 if (status != NS_IMAGELIB_ERROR_LOAD_ABORTED)
362 pending_window_update_ = false;
365 if (!browser_is_busy())
375 catch (std::exception & e)
377 std::cerr << "Fatal error";
378 if (!page_queue_.empty())
379 std::cerr << " while processing <" << page_queue_.front()
381 std::cerr << ": " << e.what() << "\n";
384 catch (Glib::Exception & e)
386 std::cerr << "Fatal error";
387 if (!page_queue_.empty())
388 std::cerr << " while processing <" << page_queue_.front()
390 std::cerr << ": " << e.what() << "\n";
396 bool webdvd_window::process_page()
398 assert(!page_queue_.empty());
400 nsCOMPtr<nsIWebBrowser> browser(browser_widget_.get_browser());
401 nsCOMPtr<nsIDocShell> doc_shell(do_GetInterface(browser));
403 nsCOMPtr<nsIPresShell> pres_shell;
404 check(doc_shell->GetPresShell(getter_AddRefs(pres_shell)));
405 nsCOMPtr<nsIPresContext> pres_context;
406 check(doc_shell->GetPresContext(getter_AddRefs(pres_context)));
407 nsCOMPtr<nsIDOMWindow> dom_window;
408 check(browser->GetContentDOMWindow(getter_AddRefs(dom_window)));
410 // If we haven't done so already, apply the stylesheet and
411 // disable scrollbars.
412 if (!have_tweaked_page_)
414 apply_style_sheet(stylesheet_, pres_shell);
416 // This actually only needs to be done once.
417 nsCOMPtr<nsIDOMBarProp> dom_bar_prop;
418 check(dom_window->GetScrollbars(getter_AddRefs(dom_bar_prop)));
419 check(dom_bar_prop->SetVisible(false));
421 have_tweaked_page_ = true;
423 // Might need to wait a while for things to load or more
424 // likely for a re-layout.
425 if (browser_is_busy())
429 // All further work should only be done if we're not in preview mode.
430 if (!output_dir_.empty())
432 // If we haven't already started work on this menu, save a
433 // screenshot of its normal appearance.
434 if (!page_state_.get())
437 // Start or continue processing links.
438 process_links(pres_shell, pres_context, dom_window);
440 // If we've finished work on the links, move on to the
441 // next page, if any, or else generate the DVD filesystem.
442 if (!page_state_.get())
445 if (page_queue_.empty())
447 generate_dvd(contents_, output_dir_);
460 void webdvd_window::save_screenshot()
462 Glib::RefPtr<Gdk::Window> window(get_window());
464 window->process_updates(true);
466 background_temp_.reset(new temp_file("webdvd-back-"));
467 background_temp_->close();
468 std::cout << "saving " << background_temp_->get_name() << std::endl;
469 Gdk::Pixbuf::create(Glib::RefPtr<Gdk::Drawable>(window),
470 window->get_colormap(),
472 frame_params_.width, frame_params_.height)
473 ->save(background_temp_->get_name(), "png");
476 struct webdvd_window::page_state
478 page_state(nsIDOMDocument * doc, int width, int height)
479 : diff_pixbuf(Gdk::Pixbuf::create(
481 true, 8, // has_alpha, bits_per_sample
483 spumux_temp("webdvd-spumux-"),
484 links_temp("webdvd-links-"),
493 Glib::RefPtr<Gdk::Pixbuf> diff_pixbuf;
495 temp_file spumux_temp;
496 std::ofstream spumux_file;
498 temp_file links_temp;
501 link_iterator links_it, links_end;
505 Glib::RefPtr<Gdk::Pixbuf> norm_pixbuf;
508 void webdvd_window::process_links(nsIPresShell * pres_shell,
509 nsIPresContext * pres_context,
510 nsIDOMWindow * dom_window)
512 Glib::RefPtr<Gdk::Window> window(get_window());
515 nsCOMPtr<nsIDOMDocument> basic_doc;
516 check(dom_window->GetDocument(getter_AddRefs(basic_doc)));
517 nsCOMPtr<nsIDOMNSDocument> ns_doc(do_QueryInterface(basic_doc));
519 nsCOMPtr<nsIEventStateManager> event_state_man(
520 pres_context->EventStateManager()); // does not AddRef
521 assert(event_state_man);
522 nsCOMPtr<nsIDOMDocumentEvent> event_factory(
523 do_QueryInterface(basic_doc));
524 assert(event_factory);
525 nsCOMPtr<nsIDOMDocumentView> doc_view(do_QueryInterface(basic_doc));
527 nsCOMPtr<nsIDOMAbstractView> view;
528 check(doc_view->GetDefaultView(getter_AddRefs(view)));
530 // Set up or recover our iteration state.
531 std::auto_ptr<page_state> state(page_state_);
536 basic_doc, frame_params_.width, frame_params_.height));
538 state->spumux_file.open(state->spumux_temp.get_name().c_str());
539 state->spumux_file <<
542 " <spu force='yes' start='00:00:00.00'\n"
543 " highlight='" << state->links_temp.get_name() << "'\n"
544 " select='" << state->links_temp.get_name() << "'>\n";
547 rectangle window_rect = {
548 0, 0, frame_params_.width, frame_params_.height
551 unsigned menu_num = resource_map_[page_queue_.front()].index;
553 for (/* no initialisation */;
554 state->links_it != state->links_end;
557 nsCOMPtr<nsIDOMNode> node(*state->links_it);
559 // Find the link URI and separate any fragment from it.
560 nsCOMPtr<nsILink> link(do_QueryInterface(node));
562 nsCOMPtr<nsIURI> uri_iface;
563 check(link->GetHrefURI(getter_AddRefs(uri_iface)));
564 std::string uri_and_fragment, uri, fragment;
566 nsCString uri_and_fragment_ns;
567 check(uri_iface->GetSpec(uri_and_fragment_ns));
568 uri_and_fragment.assign(uri_and_fragment_ns.BeginReading(),
569 uri_and_fragment_ns.EndReading());
571 std::size_t hash_pos = uri_and_fragment.find('#');
572 uri.assign(uri_and_fragment, 0, hash_pos);
573 if (hash_pos != std::string::npos)
574 fragment.assign(uri_and_fragment,
575 hash_pos + 1, std::string::npos);
578 // Is this a new link?
579 if (!state->link_changing)
581 // Find a rectangle enclosing the link and clip it to the
583 nsCOMPtr<nsIDOMElement> elem(do_QueryInterface(node));
585 state->link_rect = get_elem_rect(ns_doc, elem);
586 state->link_rect &= window_rect;
588 if (state->link_rect.empty())
590 std::cerr << "Ignoring invisible link to "
591 << uri_and_fragment << "\n";
597 if (state->link_num >= unsigned(dvd::menu_buttons_max))
599 if (state->link_num == unsigned(dvd::menu_buttons_max))
600 std::cerr << "No more than " << dvd::menu_buttons_max
601 << " buttons can be placed on a menu\n";
602 std::cerr << "Ignoring link to " << uri_and_fragment
607 state->spumux_file <<
608 " <button x0='" << state->link_rect.left << "'"
609 " y0='" << state->link_rect.top << "'"
610 " x1='" << state->link_rect.right - 1 << "'"
611 " y1='" << state->link_rect.bottom - 1 << "'/>\n";
613 // Check whether this is a link to a video or a page then
614 // add it to the known resources if not already seen; then
615 // add it to the menu entries.
616 dvd_contents::pgc_ref target;
617 // FIXME: This is a bit of a hack. Perhaps we could decide
618 // later based on the MIME type determined by Mozilla?
620 && uri.compare(uri.size() - 4, 4, ".vob") == 0)
622 && uri.compare(uri.size() - 8, 8, ".voblist") == 0))
625 check(uri_iface->SchemeIs("file", &is_file));
628 std::cerr << "Links to video must use the file:"
632 target = add_title(uri);
634 std::strtoul(fragment.c_str(), NULL, 10);
638 target = add_menu(uri);
639 // TODO: If there's a fragment, work out which button
640 // is closest and set target.sub_index.
642 contents_.menus[menu_num].entries.push_back(target);
644 nsCOMPtr<nsIContent> content(do_QueryInterface(node));
646 nsCOMPtr<nsIDOMEventTarget> event_target(
647 do_QueryInterface(node));
648 assert(event_target);
650 state->norm_pixbuf = Gdk::Pixbuf::create(
651 Glib::RefPtr<Gdk::Drawable>(window),
652 window->get_colormap(),
653 state->link_rect.left,
654 state->link_rect.top,
657 state->link_rect.right - state->link_rect.left,
658 state->link_rect.bottom - state->link_rect.top);
660 nsCOMPtr<nsIDOMEvent> event;
661 check(event_factory->CreateEvent(
662 NS_ConvertASCIItoUTF16("MouseEvents"),
663 getter_AddRefs(event)));
664 nsCOMPtr<nsIDOMMouseEvent> mouse_event(
665 do_QueryInterface(event));
667 check(mouse_event->InitMouseEvent(
668 NS_ConvertASCIItoUTF16("mouseover"),
672 0, // detail: mouse click count
673 state->link_rect.left, // screenX
674 state->link_rect.top, // screenY
675 state->link_rect.left, // clientX
676 state->link_rect.top, // clientY
677 false, false, false, false, // qualifiers
678 0, // button: left (or primary)
679 0)); // related target
681 check(event_target->DispatchEvent(mouse_event,
683 check(event_state_man->SetContentState(content,
684 NS_EVENT_STATE_HOVER));
686 pres_shell->FlushPendingNotifications(true);
688 // We may have to exit and wait for image loading
689 // to complete, at which point we will be called
691 if (browser_is_busy())
693 state->link_changing = true;
699 window->process_updates(true);
701 Glib::RefPtr<Gdk::Pixbuf> changed_pixbuf(
703 Glib::RefPtr<Gdk::Drawable>(window),
704 window->get_colormap(),
705 state->link_rect.left,
706 state->link_rect.top,
709 state->link_rect.right - state->link_rect.left,
710 state->link_rect.bottom - state->link_rect.top));
715 state->link_rect.left,
716 state->link_rect.top,
717 state->link_rect.right - state->link_rect.left,
718 state->link_rect.bottom - state->link_rect.top);
721 quantise_rgba_pixbuf(state->diff_pixbuf, dvd::button_n_colours);
723 std::cout << "saving " << state->links_temp.get_name()
725 state->diff_pixbuf->save(state->links_temp.get_name(), "png");
727 state->spumux_file <<
732 state->spumux_file.close();
734 // TODO: if (!state->spumux_file) throw ...
737 std::ostringstream command_stream;
738 command_stream << "pngtopnm "
739 << background_temp_->get_name()
740 << " | ppmtoy4m -v0 -n1 -F"
741 << frame_params_.rate_numer
742 << ":" << frame_params_.rate_denom
743 << " -A" << frame_params_.pixel_ratio_width
744 << ":" << frame_params_.pixel_ratio_height
745 << (" -Ip -S420_mpeg2"
746 " | mpeg2enc -v0 -f8 -a2 -o/dev/stdout"
747 " | mplex -v0 -f8 -o/dev/stdout /dev/stdin"
748 " | spumux -v0 -mdvd ")
749 << state->spumux_temp.get_name()
751 << contents_.menus[menu_num].vob_temp->get_name();
752 std::string command(command_stream.str());
753 const char * argv[] = {
754 "/bin/sh", "-c", command.c_str(), 0
756 std::cout << "running " << argv[2] << std::endl;
758 Glib::spawn_sync(".",
759 Glib::ArrayHandle<std::string>(
760 argv, sizeof(argv)/sizeof(argv[0]),
761 Glib::OWNERSHIP_NONE),
762 Glib::SPAWN_STDOUT_TO_DEV_NULL,
766 if (command_result != 0)
767 throw std::runtime_error("spumux pipeline failed");
771 const video::frame_params & lookup_frame_params(const char * str)
774 static const struct { const char * str; bool is_ntsc; }
780 // For DVD purposes, SECAM can be treated identically to PAL.
784 for (std::size_t i = 0;
785 i != sizeof(known_strings)/sizeof(known_strings[0]);
787 if (std::strcmp(str, known_strings[i].str) == 0)
788 return known_strings[i].is_ntsc ?
789 video::ntsc_params : video::pal_params;
790 throw std::runtime_error(
791 std::string("Invalid video standard: ").append(str));
794 void print_usage(std::ostream & stream, const char * command_name)
796 stream << "Usage: " << command_name
797 << (" [gtk-options] [--video-std std-name]"
798 " [--preview] menu-url [output-dir]\n");
801 void set_browser_preferences()
803 nsCOMPtr<nsIPrefService> pref_service;
804 static const nsCID pref_service_cid = NS_PREFSERVICE_CID;
805 check(CallGetService<nsIPrefService>(pref_service_cid,
806 getter_AddRefs(pref_service)));
807 nsCOMPtr<nsIPrefBranch> pref_branch;
809 // Disable IE-compatibility kluge that causes backgrounds to
810 // sometimes/usually be missing from snapshots. This is only
811 // effective from Mozilla 1.8 onward.
812 # if MOZ_VERSION_MAJOR > 1 \
813 || (MOZ_VERSION_MAJOR == 1 && MOZ_VERSION_MINOR >= 8)
814 check(pref_service->GetDefaultBranch("layout",
815 getter_AddRefs(pref_branch)));
816 check(pref_branch->SetBoolPref(
817 "fire_onload_after_image_background_loads",
821 // Set display resolution. With standard-definition video we
822 // will be fitting ~600 pixels across a screen typically
823 // ranging from 10 to 25 inches wide, for a resolution of
824 // 24-60 dpi. I therefore declare the average horizontal
825 // resolution to be 40 dpi. The vertical resolution will be
826 // slightly higher (PAL/SECAM) or lower (NTSC), but
827 // unfortunately Mozilla doesn't support non-square pixels
828 // (and neither do fontconfig or Xft anyway).
829 check(pref_service->GetDefaultBranch("browser.display",
830 getter_AddRefs(pref_branch)));
831 check(pref_branch->SetIntPref("screen_resolution", 40));
836 void fatal_error(const std::string & message)
838 std::cerr << "Fatal error: " << message << "\n";
842 int main(int argc, char ** argv)
846 video::frame_params frame_params = video::pal_params;
847 bool preview_mode = false;
848 std::string menu_url;
849 std::string output_dir;
851 // Do initial option parsing. We have to do this before
852 // letting Gtk parse the arguments since we may need to spawn
857 if (std::strcmp(argv[argi], "--") == 0)
861 else if (std::strcmp(argv[argi], "--help") == 0)
863 print_usage(std::cout, argv[0]);
866 else if (std::strcmp(argv[argi], "--preview") == 0)
871 else if (std::strcmp(argv[argi], "--video-std") == 0)
873 if (argi + 1 == argc)
875 std::cerr << "Missing argument to --video-std\n";
876 print_usage(std::cerr, argv[0]);
879 frame_params = lookup_frame_params(argv[argi + 1]);
888 std::auto_ptr<x_frame_buffer> fb;
891 // Spawn Xvfb and set env variables so that Xlib will use it
892 // Use 8 bits each for RGB components, which should translate into
893 // "enough" bits for YUV components.
894 fb.reset(new x_frame_buffer(frame_params.width,
897 setenv("XAUTHORITY", fb->get_authority().c_str(), true);
898 setenv("DISPLAY", fb->get_display().c_str(), true);
902 Gtk::Main kit(argc, argv);
904 // Complete option parsing with Gtk's options out of the way.
908 if (std::strcmp(argv[argi], "--") == 0)
913 else if (std::strcmp(argv[argi], "--preview") == 0)
917 else if (std::strcmp(argv[argi], "--video-std") == 0)
921 else if (argv[argi][0] == '-')
923 std::cerr << "Invalid option: " << argv[argi] << "\n";
924 print_usage(std::cerr, argv[0]);
933 // Look for a starting URL or filename and (except in preview
934 // mode) an output directory after the options.
935 if (argc - argi != (preview_mode ? 1 : 2))
937 print_usage(std::cerr, argv[0]);
940 if (std::strstr(argv[argi], "://"))
942 // It appears to be an absolute URL, so use it as-is.
943 menu_url = argv[argi];
947 // Assume it's a filename. Resolve it to an absolute URL.
948 std::string path(argv[argi]);
949 if (!Glib::path_is_absolute(path))
950 path = Glib::build_filename(Glib::get_current_dir(), path);
951 menu_url = Glib::filename_to_uri(path);
954 output_dir = argv[argi + 1];
956 // Initialise Mozilla
957 browser_widget::initialiser browser_init;
958 set_browser_preferences();
960 null_prompt_service::install();
962 // Run the browser/converter
963 webdvd_window window(frame_params, menu_url, output_dir);
964 Gtk::Main::run(window);
966 return ((preview_mode || window.is_finished())
970 catch (std::exception & e)
972 std::cerr << "Fatal error: " << e.what() << "\n";