1 // Copyright 2005 Ben Hutchings <ben@decadentplace.org.uk>.
2 // See the file "COPYING" for licence details.
17 #include <gdkmm/pixbuf.h>
18 #include <gtkmm/main.h>
19 #include <gtkmm/window.h>
21 #include <imglib2/ImageErrors.h>
22 #include <nsGUIEvent.h>
23 #include <nsIBoxObject.h>
24 #include <nsIContent.h>
25 #include <nsIDocShell.h>
26 #include <nsIDOMAbstractView.h>
27 #include <nsIDOMDocumentEvent.h>
28 #include <nsIDOMDocumentView.h>
29 #include <nsIDOMElement.h>
30 #include <nsIDOMEventTarget.h>
31 #include <nsIDOMHTMLDocument.h>
32 #include <nsIDOMMouseEvent.h>
33 #include <nsIDOMNSDocument.h>
34 #include <nsIDOMWindow.h>
35 #include <nsIEventStateManager.h>
36 #include <nsIInterfaceRequestorUtils.h>
37 #include <nsIURI.h> // required before nsILink.h
39 #include <nsIPresContext.h>
40 #include <nsIPresShell.h>
41 #include <nsIWebBrowser.h>
44 #include "browserwidget.hpp"
45 #include "childiterator.hpp"
47 #include "framebuffer.hpp"
48 #include "linkiterator.hpp"
49 #include "pixbufs.hpp"
50 #include "stylesheets.hpp"
52 #include "xpcom_support.hpp"
54 using xpcom_support::check;
60 int left, top; // inclusive
61 int right, bottom; // exclusive
63 rectangle operator|=(const rectangle & other)
67 // use current extents unchanged
76 // find rectangle enclosing both extents
77 left = std::min(left, other.left);
78 top = std::min(top, other.top);
79 right = std::max(right, other.right);
80 bottom = std::max(bottom, other.bottom);
86 rectangle operator&=(const rectangle & other)
88 // find rectangle enclosed in both extents
89 left = std::max(left, other.left);
90 top = std::max(top, other.top);
91 right = std::max(left, std::min(right, other.right));
92 bottom = std::max(top, std::min(bottom, other.bottom));
98 return left == right || bottom == top;
102 rectangle get_elem_rect(nsIDOMNSDocument * ns_doc,
103 nsIDOMElement * elem)
107 nsCOMPtr<nsIBoxObject> box;
108 check(ns_doc->GetBoxObjectFor(elem, getter_AddRefs(box)));
110 check(box->GetScreenX(&result.left));
111 check(box->GetScreenY(&result.top));
112 check(box->GetWidth(&width));
113 check(box->GetHeight(&height));
114 result.right = result.left + width;
115 result.bottom = result.top + height;
117 for (ChildIterator it = ChildIterator(elem), end; it != end; ++it)
119 nsCOMPtr<nsIDOMNode> child_node(*it);
121 if (check(child_node->GetNodeType(&child_type)),
122 child_type == nsIDOMNode::ELEMENT_NODE)
124 nsCOMPtr<nsIDOMElement> child_elem(
125 do_QueryInterface(child_node));
126 result |= get_elem_rect(ns_doc, child_elem);
133 class WebDvdWindow : public Gtk::Window
137 const video::frame_params & frame_params,
138 const std::string & main_page_uri);
141 void add_page(const std::string & uri);
142 void add_video(const std::string & uri);
143 void load_next_page();
144 void on_net_state_change(const char * uri, gint flags, guint status);
145 void save_screenshot();
146 void process_links(nsIPresShell * pres_shell,
147 nsIPresContext * pres_context,
148 nsIDOMWindow * dom_window);
149 void generate_dvdauthor_file();
151 enum ResourceType { page_resource, video_resource };
152 typedef std::pair<ResourceType, int> ResourceEntry;
153 video::frame_params frame_params_;
154 BrowserWidget browser_widget_;
155 nsCOMPtr<nsIStyleSheet> stylesheet_;
156 std::queue<std::string> page_queue_;
157 std::map<std::string, ResourceEntry> resource_map_;
158 std::vector<std::vector<std::string> > page_links_;
159 std::vector<std::string> video_paths_;
161 int pending_req_count_;
163 std::auto_ptr<link_state> link_state_;
166 WebDvdWindow::WebDvdWindow(
167 const video::frame_params & frame_params,
168 const std::string & main_page_uri)
169 : frame_params_(frame_params),
170 stylesheet_(load_css("file://" WEBDVD_LIB_DIR "/webdvd.css")),
172 pending_req_count_(0)
174 set_default_size(frame_params_.width, frame_params_.height);
175 add(browser_widget_);
176 browser_widget_.show();
177 browser_widget_.signal_net_state().connect(
178 SigC::slot(*this, &WebDvdWindow::on_net_state_change));
180 add_page(main_page_uri);
184 void WebDvdWindow::add_page(const std::string & uri)
186 if (resource_map_.insert(
187 std::make_pair(uri, ResourceEntry(page_resource, 0)))
190 page_queue_.push(uri);
194 void WebDvdWindow::add_video(const std::string & uri)
196 if (resource_map_.insert(
197 std::make_pair(uri, ResourceEntry(video_resource,
198 video_paths_.size() + 1)))
201 // FIXME: Should accept some slightly different URI prefixes
202 // (e.g. file://localhost/) and decode any URI-escaped
203 // characters in the path.
204 assert(uri.compare(0, 8, "file:///") == 0);
205 video_paths_.push_back(uri.substr(7));
209 void WebDvdWindow::load_next_page()
213 assert(!page_queue_.empty());
214 const std::string & uri = page_queue_.front();
215 std::cout << "loading " << uri << std::endl;
217 std::size_t page_count = page_links_.size();
218 resource_map_[uri].second = ++page_count;
219 page_links_.resize(page_count);
220 browser_widget_.load_uri(uri);
223 void WebDvdWindow::on_net_state_change(const char * uri,
224 gint flags, guint status)
230 } action = process_nothing;
232 if (flags & GTK_MOZ_EMBED_FLAG_IS_REQUEST)
234 if (flags & GTK_MOZ_EMBED_FLAG_START)
235 ++pending_req_count_;
236 if (flags & GTK_MOZ_EMBED_FLAG_STOP)
238 assert(pending_req_count_ != 0);
239 --pending_req_count_;
241 if (pending_req_count_ == 0 && link_state_.get())
242 action = process_current_link;
245 if (flags & GTK_MOZ_EMBED_FLAG_STOP
246 && flags & GTK_MOZ_EMBED_FLAG_IS_WINDOW)
247 action = process_new_page;
249 if (action != process_nothing)
251 assert(loading_ && !page_queue_.empty());
252 assert(pending_req_count_ == 0);
256 // Check whether the load was successful, ignoring this
258 if (status != NS_IMAGELIB_ERROR_LOAD_ABORTED)
261 nsCOMPtr<nsIWebBrowser> browser(
262 browser_widget_.get_browser());
263 nsCOMPtr<nsIDocShell> doc_shell(do_GetInterface(browser));
265 nsCOMPtr<nsIPresShell> pres_shell;
266 check(doc_shell->GetPresShell(getter_AddRefs(pres_shell)));
267 nsCOMPtr<nsIPresContext> pres_context;
268 check(doc_shell->GetPresContext(
269 getter_AddRefs(pres_context)));
270 nsCOMPtr<nsIDOMWindow> dom_window;
271 check(browser->GetContentDOMWindow(
272 getter_AddRefs(dom_window)));
274 if (action == process_new_page)
276 apply_style_sheet(stylesheet_, pres_shell);
279 process_links(pres_shell, pres_context, dom_window);
280 if (!link_state_.get())
283 if (page_queue_.empty())
285 generate_dvdauthor_file();
292 catch (std::exception & e)
294 std::cerr << "Fatal error";
295 if (!page_queue_.empty())
296 std::cerr << " while processing <" << page_queue_.front()
298 std::cerr << ": " << e.what() << "\n";
304 void WebDvdWindow::save_screenshot()
307 std::sprintf(filename, "page_%06d_back.png", page_links_.size());
308 Glib::RefPtr<Gdk::Window> window(get_window());
310 window->process_updates(true);
311 std::cout << "saving " << filename << std::endl;
312 Gdk::Pixbuf::create(Glib::RefPtr<Gdk::Drawable>(window),
313 window->get_colormap(),
315 frame_params_.width, frame_params_.height)
316 ->save(filename, "png");
319 struct WebDvdWindow::link_state
321 Glib::RefPtr<Gdk::Pixbuf> diff_pixbuf;
323 std::ofstream spumux_file;
326 LinkIterator links_it, links_end;
330 Glib::RefPtr<Gdk::Pixbuf> norm_pixbuf;
333 void WebDvdWindow::process_links(nsIPresShell * pres_shell,
334 nsIPresContext * pres_context,
335 nsIDOMWindow * dom_window)
337 Glib::RefPtr<Gdk::Window> window(get_window());
340 nsCOMPtr<nsIDOMDocument> basic_doc;
341 check(dom_window->GetDocument(getter_AddRefs(basic_doc)));
342 nsCOMPtr<nsIDOMNSDocument> ns_doc(do_QueryInterface(basic_doc));
344 nsCOMPtr<nsIEventStateManager> event_state_man(
345 pres_context->EventStateManager()); // does not AddRef
346 assert(event_state_man);
347 nsCOMPtr<nsIDOMDocumentEvent> event_factory(
348 do_QueryInterface(basic_doc));
349 assert(event_factory);
350 nsCOMPtr<nsIDOMDocumentView> doc_view(do_QueryInterface(basic_doc));
352 nsCOMPtr<nsIDOMAbstractView> view;
353 check(doc_view->GetDefaultView(getter_AddRefs(view)));
355 // Set up or recover our iteration state.
356 std::auto_ptr<link_state> state(link_state_);
359 state.reset(new link_state);
361 state->diff_pixbuf = Gdk::Pixbuf::create(
363 true, 8, // has_alpha, bits_per_sample
364 frame_params_.width, frame_params_.height);
366 char spumux_filename[20];
367 std::sprintf(spumux_filename,
368 "page_%06d.spumux", page_links_.size());
369 state->spumux_file.open(spumux_filename);
370 state->spumux_file <<
373 " <spu force='yes' start='00:00:00.00'\n"
374 " highlight='page_" << std::setfill('0')
375 << std::setw(6) << page_links_.size()
377 " select='page_" << std::setfill('0')
378 << std::setw(6) << page_links_.size()
382 state->links_it = LinkIterator(basic_doc);
383 state->link_changing = false;
386 rectangle window_rect = {
387 0, 0, frame_params_.width, frame_params_.height
390 for (/* no initialisation */;
391 state->links_it != state->links_end;
394 nsCOMPtr<nsIDOMNode> node(*state->links_it);
396 // Find the link URI.
397 nsCOMPtr<nsILink> link(do_QueryInterface(node));
399 nsCOMPtr<nsIURI> uri;
400 check(link->GetHrefURI(getter_AddRefs(uri)));
401 std::string uri_string;
403 nsCString uri_ns_string;
404 check(uri->GetSpec(uri_ns_string));
405 uri_string.assign(uri_ns_string.BeginReading(),
406 uri_ns_string.EndReading());
408 std::string uri_sans_fragment(uri_string, 0, uri_string.find('#'));
410 // Is this a new link?
411 if (!state->link_changing)
413 // Find a rectangle enclosing the link and clip it to the
415 nsCOMPtr<nsIDOMElement> elem(do_QueryInterface(node));
417 state->link_rect = get_elem_rect(ns_doc, elem);
418 state->link_rect &= window_rect;
420 if (state->link_rect.empty())
422 std::cerr << "Ignoring invisible link to "
423 << uri_string << "\n";
429 if (state->link_num >= dvd::menu_buttons_max)
431 if (state->link_num == dvd::menu_buttons_max)
432 std::cerr << "No more than " << dvd::menu_buttons_max
433 << " buttons can be placed on a page\n";
434 std::cerr << "Ignoring link to " << uri_string << "\n";
438 // Check whether this is a link to a video or a page then
439 // add it to the known resources if not already seen.
441 check(uri->GetPath(path));
442 // FIXME: This is a bit of a hack. Perhaps we could decide
443 // later based on the MIME type determined by Mozilla?
444 if (path.Length() > 4
445 && std::strcmp(path.EndReading() - 4, ".vob") == 0)
448 check(uri->SchemeIs("file", &is_file));
451 std::cerr << "Links to video must use the file:"
455 add_video(uri_sans_fragment);
459 add_page(uri_sans_fragment);
462 nsCOMPtr<nsIContent> content(do_QueryInterface(node));
464 nsCOMPtr<nsIDOMEventTarget> event_target(
465 do_QueryInterface(node));
466 assert(event_target);
468 state->norm_pixbuf = Gdk::Pixbuf::create(
469 Glib::RefPtr<Gdk::Drawable>(window),
470 window->get_colormap(),
471 state->link_rect.left,
472 state->link_rect.top,
475 state->link_rect.right - state->link_rect.left,
476 state->link_rect.bottom - state->link_rect.top);
478 nsCOMPtr<nsIDOMEvent> event;
479 check(event_factory->CreateEvent(
480 NS_ConvertASCIItoUTF16("MouseEvents"),
481 getter_AddRefs(event)));
482 nsCOMPtr<nsIDOMMouseEvent> mouse_event(
483 do_QueryInterface(event));
485 check(mouse_event->InitMouseEvent(
486 NS_ConvertASCIItoUTF16("mouseover"),
490 0, // detail: mouse click count
491 state->link_rect.left, // screenX
492 state->link_rect.top, // screenY
493 state->link_rect.left, // clientX
494 state->link_rect.top, // clientY
495 false, false, false, false, // qualifiers
496 0, // button: left (or primary)
497 0)); // related target
499 check(event_target->DispatchEvent(mouse_event,
501 check(event_state_man->SetContentState(content,
502 NS_EVENT_STATE_HOVER));
504 pres_shell->FlushPendingNotifications(true);
506 // We may have to exit and wait for image loading
507 // to complete, at which point we will be called
509 if (pending_req_count_ > 0)
511 state->link_changing = true;
517 window->process_updates(true);
519 Glib::RefPtr<Gdk::Pixbuf> changed_pixbuf(
521 Glib::RefPtr<Gdk::Drawable>(window),
522 window->get_colormap(),
523 state->link_rect.left,
524 state->link_rect.top,
527 state->link_rect.right - state->link_rect.left,
528 state->link_rect.bottom - state->link_rect.top));
533 state->link_rect.left,
534 state->link_rect.top,
535 state->link_rect.right - state->link_rect.left,
536 state->link_rect.bottom - state->link_rect.top);
538 state->spumux_file <<
539 " <button x0='" << state->link_rect.left << "'"
540 " y0='" << state->link_rect.top << "'"
541 " x1='" << state->link_rect.right - 1 << "'"
542 " y1='" << state->link_rect.bottom - 1 << "'/>\n";
544 // Add to the page's links, ignoring any fragment (for now).
545 page_links_.back().push_back(uri_sans_fragment);
548 quantise_rgba_pixbuf(state->diff_pixbuf, dvd::button_n_colours);
551 std::sprintf(filename, "page_%06d_links.png", page_links_.size());
552 std::cout << "saving " << filename << std::endl;
553 state->diff_pixbuf->save(filename, "png");
555 state->spumux_file <<
561 void generate_page_dispatch(std::ostream &, int indent,
562 int first_page, int last_page);
564 void WebDvdWindow::generate_dvdauthor_file()
566 std::ofstream file("webdvd.dvdauthor");
568 // We generate code that uses registers in the following way:
570 // g0: link destination (when jumping to menu 1), then scratch
571 // g1: current location
572 // g2-g11: location history (g2 = most recent)
573 // g12: location that last linked to a video
575 // All locations are divided into two bitfields: the least
576 // significant 10 bits are a page/menu number and the most
577 // significant 6 bits are a link/button number. This is
578 // chosen for compatibility with the encoding of the s8
579 // (button) register.
581 static const int link_mult = dvd::reg_s8_button_mult;
582 static const int page_mask = link_mult - 1;
583 static const int link_mask = (1 << dvd::reg_bits) - link_mult;
590 for (std::size_t page_num = 1;
591 page_num <= page_links_.size();
594 std::vector<std::string> & page_links =
595 page_links_[page_num - 1];
599 // This is the first page (root menu) which needs to
600 // include initialisation and dispatch code.
603 " <pgc entry='title'>\n"
605 // Has the location been set yet?
608 // Initialise the current location to first link on
610 " g1 = " << 1 * link_mult + 1 << ";\n"
614 // Has the user selected a link?
617 // First update the history.
618 // Does link go to the last page in the history?
619 " if (((g0 ^ g2) & " << page_mask
621 // It does; we treat this as going back and pop the old
622 // location off the history stack into the current
623 // location. Clear the free stack slot.
625 " g1 = g2; g2 = g3; g3 = g4; g4 = g5;\n"
626 " g5 = g6; g6 = g7; g7 = g8; g8 = g9;\n"
627 " g9 = g10; g10 = g11; g11 = 0;\n"
630 // Link goes to some other page, so push current
631 // location onto the history stack and set the current
632 // location to be exactly the target location.
634 " g11 = g10; g10 = g9; g9 = g8; g8 = g7;\n"
635 " g7 = g6; g6 = g5; g5 = g4; g4 = g3;\n"
636 " g3 = g2; g2 = g1; g1 = g0;\n"
639 // Find the target page number.
640 " g0 = g1 & " << page_mask << ";\n";
641 // There seems to be no way to perform a computed jump,
642 // so we generate all possible jumps and a binary search
643 // to select the correct one.
644 generate_page_dispatch(file, 12, 1, page_links_.size());
648 else // page_num != 1
656 // Clear link indicator and highlight the
657 // appropriate link/button.
658 " g0 = 0; s8 = g1 & " << link_mask << ";\n"
661 << std::setfill('0') << std::setw(6) << page_num
664 for (std::size_t link_num = 1;
665 link_num <= page_links.size();
670 // Update current location.
671 " g1 = " << link_num * link_mult + page_num << ";";
673 // Jump to appropriate resource.
674 const ResourceEntry & resource_loc =
675 resource_map_[page_links[link_num - 1]];
676 if (resource_loc.first == page_resource)
678 " g0 = " << 1 * link_mult + resource_loc.second << ";"
680 else if (resource_loc.first == video_resource)
681 file << " jump title " << resource_loc.second << ";";
683 file << " </button>\n";
693 // Generate a titleset for each video. This appears to make
694 // jumping to titles a whole lot simpler.
695 for (std::size_t video_num = 1;
696 video_num <= video_paths_.size();
701 // Generate a dummy menu so that the menu button on the
702 // remote control will work.
704 " <pgc entry='root'>\n"
705 " <pre> jump vmgm menu; </pre>\n"
710 // Record calling page/menu.
711 " <pre> g12 = g1; </pre>\n"
712 // FIXME: Should XML-escape the path
713 " <vob file='" << video_paths_[video_num - 1]
715 // If page/menu location has not been changed during the
716 // video, change the location to be the following
717 // link/button when returning to it. In any case,
718 // return to a page/menu.
719 " <post> if (g1 eq g12) g1 = g1 + " << link_mult
720 << "; call menu; </post>\n"
730 void generate_page_dispatch(std::ostream & file, int indent,
731 int first_page, int last_page)
733 if (first_page == 1 && last_page == 1)
735 // The dispatch code is *on* page 1 so we must not dispatch to
736 // page 1 since that would cause an infinite loop. This case
737 // should be unreachable if there is more than one page due
738 // to the following case.
740 else if (first_page == 1 && last_page == 2)
742 // dvdauthor doesn't allow empty blocks or null statements so
743 // when selecting between pages 1 and 2 we don't use an "else"
744 // part. We must use braces so that a following "else" will
745 // match the right "if".
746 file << std::setw(indent) << "" << "{\n"
747 << std::setw(indent) << "" << "if (g0 eq 2)\n"
748 << std::setw(indent + 2) << "" << "jump menu 2;\n"
749 << std::setw(indent) << "" << "}\n";
751 else if (first_page == last_page)
753 file << std::setw(indent) << ""
754 << "jump menu " << first_page << ";\n";
758 int middle = (first_page + last_page) / 2;
759 file << std::setw(indent) << "" << "if (g0 le " << middle << ")\n";
760 generate_page_dispatch(file, indent + 2, first_page, middle);
761 file << std::setw(indent) << "" << "else\n";
762 generate_page_dispatch(file, indent + 2, middle + 1, last_page);
766 const video::frame_params & lookup_frame_params(const char * str)
769 static const struct { const char * str; bool is_ntsc; }
775 // For DVD purposes, SECAM can be treated identically to PAL.
779 for (std::size_t i = 0;
780 i != sizeof(known_strings)/sizeof(known_strings[0]);
782 if (std::strcmp(str, known_strings[i].str) == 0)
783 return known_strings[i].is_ntsc ?
784 video::ntsc_params : video::pal_params;
785 throw std::runtime_error(
786 std::string("Invalid video standard: ").append(str));
791 int main(int argc, char ** argv)
795 // Determine video frame parameters.
796 video::frame_params frame_params = video::pal_params;
797 for (int argi = 1; argi != argc; ++argi)
799 if (std::strcmp(argv[argi], "--") == 0)
801 if (std::strcmp(argv[argi], "--video-std") == 0)
803 if (argi + 1 == argc)
805 std::cerr << "Missing argument to --video-std\n";
808 frame_params = lookup_frame_params(argv[argi + 1]);
813 // Spawn Xvfb and set env variables so that Xlib will use it
814 // Use 8 bits each for RGB components, which should translate into
815 // "enough" bits for YUV components.
816 FrameBuffer fb(frame_params.width, frame_params.height, 3 * 8);
817 setenv("XAUTHORITY", fb.get_x_authority().c_str(), true);
818 setenv("DISPLAY", fb.get_x_display().c_str(), true);
821 Gtk::Main kit(argc, argv);
823 // Check we have the right number of arguments. We can't
824 // do this earlier because we need to let Gtk read and remove
825 // any of the many options it understands.
829 if (std::strcmp(argv[argi], "--video-std") == 0)
833 else if (std::strcmp(argv[argi], "--") == 0)
838 else if (argv[argi][0] == '-')
840 std::cerr << "Invalid option: " << argv[argi] << "\n";
846 if (argi != argc - 1)
848 std::cerr << "Usage: " << argv[0]
849 << (" [gtk-options] [--video-std std-name]"
854 // Initialise Mozilla
855 BrowserWidget::init();
857 WebDvdWindow window(frame_params, argv[argi]);
858 Gtk::Main::run(window);
860 catch (std::exception & e)
862 std::cerr << "Fatal error: " << e.what() << "\n";