X-Git-Url: https://git.decadent.org.uk/gitweb/?p=videolink.git;a=blobdiff_plain;f=videolink.cpp;h=a15bbea1bfe125a2cf9c67e152935203a1e3e2a3;hp=3d2daebd0176644a008444e729c76e86ebd85be3;hb=HEAD;hpb=4b8bff9ce93df43b120c5cf4d1476d3a435f99d3 diff --git a/videolink.cpp b/videolink.cpp index 3d2daeb..a15bbea 100644 --- a/videolink.cpp +++ b/videolink.cpp @@ -1,4 +1,4 @@ -// Copyright 2005-6 Ben Hutchings . +// Copyright 2005-8 Ben Hutchings . // See the file "COPYING" for licence details. #include @@ -15,15 +15,21 @@ #include -#include - +#include #include #include #include #include #include -#include +#include "videolink.hpp" +#include "wchar_t_short.h" +#include +#if MOZ_VERSION_GE(1,9,0) +#include +/* For some reason no longer defines this */ +typedef nsCOMPtr nsWeakPtr; +#endif #include #include #include @@ -44,22 +50,29 @@ #include #include #include -#include #include -#include +#include #include +#ifdef MOZILLA_INTERNAL_API #include +#else +#include +#endif +#include "wchar_t_default.h" #include "browser_widget.hpp" #include "child_iterator.hpp" #include "dvd.hpp" +#include "event_state_manager.hpp" #include "generate_dvd.hpp" +#include "geometry.hpp" #include "link_iterator.hpp" #include "null_prompt_service.hpp" #include "pixbufs.hpp" #include "style_sheets.hpp" #include "temp_file.hpp" #include "video.hpp" +#include "warp_pointer.hpp" #include "x_frame_buffer.hpp" #include "xml_utils.hpp" #include "xpcom_support.hpp" @@ -68,66 +81,17 @@ using xpcom_support::check; namespace { - // We can try using any of these encoders to convert PNG to MPEG. - enum mpeg_encoder - { - mpeg_encoder_ffmpeg, // ffmpeg - mpeg_encoder_mjpegtools_old, // mjpegtools before version 1.8 - mpeg_encoder_mjpegtools_new // mjpegtools from version 1.8 - }; - - struct rectangle - { - int left, top; // inclusive - int right, bottom; // exclusive - - rectangle operator|=(const rectangle & other) - { - if (other.empty()) - { - // use current extents unchanged - } - else if (empty()) - { - // use other extents - *this = other; - } - else - { - // find rectangle enclosing both extents - left = std::min(left, other.left); - top = std::min(top, other.top); - right = std::max(right, other.right); - bottom = std::max(bottom, other.bottom); - } - - return *this; - } - - rectangle operator&=(const rectangle & other) - { - // find rectangle enclosed in both extents - left = std::max(left, other.left); - top = std::max(top, other.top); - right = std::max(left, std::min(right, other.right)); - bottom = std::max(top, std::min(bottom, other.bottom)); - return *this; - } - - bool empty() const - { - return left == right || bottom == top; - } - }; - - rectangle get_elem_rect(nsIDOMNSDocument * ns_doc, - nsIDOMElement * elem) +#if MOZ_VERSION_GE(2,0,-1) + rectangle get_elem_rect(nsIDocument * doc, nsIDOMElement * elem) +#else + rectangle get_elem_rect(nsIDOMNSDocument * doc, nsIDOMElement * elem) +#endif { rectangle result; // Start with this element's bounding box nsCOMPtr box; - check(ns_doc->GetBoxObjectFor(elem, getter_AddRefs(box))); + check(doc->GetBoxObjectFor(elem, getter_AddRefs(box))); int width, height; check(box->GetScreenX(&result.left)); check(box->GetScreenY(&result.top)); @@ -146,7 +110,7 @@ namespace { nsCOMPtr child_elem( do_QueryInterface(child_node)); - result |= get_elem_rect(ns_doc, child_elem); + result |= get_elem_rect(doc, child_elem); } } @@ -154,20 +118,122 @@ namespace } - class videolink_window : public Gtk::Window + enum video_format + { + video_format_none, + video_format_mpeg2_ps, + video_format_vob_list + }; + + video_format video_format_from_uri(const std::string & uri) + { + // FIXME: This is a bit of a hack. Perhaps we could decide + // later based on the MIME type determined by Mozilla? + static struct { + const char * extension; + video_format format; + } const mapping[] = { + {".vob", video_format_mpeg2_ps}, + {".mpeg", video_format_mpeg2_ps}, + {".mpeg2", video_format_mpeg2_ps}, + {".mpg", video_format_mpeg2_ps}, + {".voblist", video_format_vob_list} + }; + for (std::size_t i = 0; + i != sizeof(mapping) / sizeof(mapping[0]); + ++i) + { + std::size_t ext_len = std::strlen(mapping[i].extension); + if (uri.size() > ext_len + && uri.compare(uri.size() - ext_len, ext_len, + mapping[i].extension) == 0) + return mapping[i].format; + } + return video_format_none; + } + + + class base_window : public Gtk::Window + { + public: + base_window(const video::frame_params & frame_params); + + protected: + video::frame_params frame_params_; + browser_widget browser_widget_; + }; + + base_window::base_window(const video::frame_params & frame_params) + : frame_params_(frame_params) + { + set_size_request(frame_params_.width, frame_params_.height); + set_resizable(false); + + add(browser_widget_); + browser_widget_.show(); + } + + class preview_window : public base_window + { + public: + preview_window(const video::frame_params & frame_params, + const std::string & main_page_uri); + + private: + bool on_idle(); + bool on_key_press(GdkEventKey *); + + std::string main_page_uri_; + }; + + preview_window::preview_window(const video::frame_params & frame_params, + const std::string & main_page_uri) + : base_window(frame_params), + main_page_uri_(main_page_uri) + { + Glib::signal_idle().connect( + sigc::mem_fun(this, &preview_window::on_idle)); + signal_key_press_event().connect( + sigc::mem_fun(this, &preview_window::on_key_press)); + } + + bool preview_window::on_idle() + { + browser_widget_.load_uri(main_page_uri_); + return false; // don't call again + } + + bool preview_window::on_key_press(GdkEventKey * event) + { + switch (event->keyval) + { + case GDK_t: // = top menu + browser_widget_.load_uri(main_page_uri_); + return true; + case GDK_q: // = quit + Gtk::Main::quit(); + return true; + default: + return false; + } + } + + class conversion_window : public base_window { public: - videolink_window( - const video::frame_params & frame_params, - const std::string & main_page_uri, - const std::string & output_dir, - mpeg_encoder encoder); + conversion_window(const video::frame_params & frame_params, + const std::string & main_page_uri, + const std::string & output_dir, + dvd_generator::mpeg_encoder encoder); bool is_finished() const; private: - dvd_contents::pgc_ref add_menu(const std::string & uri); - dvd_contents::pgc_ref add_title(const std::string & uri); + struct page_state; + + dvd_generator::pgc_ref add_menu(const std::string & uri); + dvd_generator::pgc_ref add_title(const std::string & uri, + video_format format); void load_next_page(); bool on_idle(); void on_net_state_change(const char * uri, gint flags, guint status); @@ -175,96 +241,81 @@ namespace { return pending_window_update_ || pending_req_count_; } - bool process_page(); - void save_screenshot(); - void process_links(nsIPresShell * pres_shell, - nsIPresContext * pres_context, - nsIDOMWindow * dom_window); + // Do as much processing as possible. Return a flag indicating + // whether to call again once the browser is idle. + bool process(); + // Return a Pixbuf containing a copy of the window contents. + Glib::RefPtr get_screenshot(); + // Do as much processing as possible on the page links. Return + // a flag indicating whether to call again once the browser is + // idle. + bool process_links( + page_state * state, + nsIDOMDocument * basic_doc, + nsIDocShell * doc_shell, + nsIDOMWindow * dom_window); - video::frame_params frame_params_; std::string output_dir_; - mpeg_encoder encoder_; - browser_widget browser_widget_; - nsCOMPtr stylesheet_; - dvd_contents contents_; - typedef std::map resource_map_type; + enum { + state_initial, + state_processing, + state_finished + } state_; + + dvd_generator generator_; + typedef std::map + resource_map_type; resource_map_type resource_map_; std::queue page_queue_; bool pending_window_update_; int pending_req_count_; - bool have_tweaked_page_; - std::auto_ptr background_temp_; - struct page_state; std::auto_ptr page_state_; - - bool finished_; }; - videolink_window::videolink_window( + conversion_window::conversion_window( const video::frame_params & frame_params, const std::string & main_page_uri, const std::string & output_dir, - mpeg_encoder encoder) - : frame_params_(frame_params), - output_dir_(output_dir), - encoder_(encoder), - stylesheet_(load_css("file://" VIDEOLINK_LIB_DIR "/videolink.css")), - pending_window_update_(false), - pending_req_count_(0), - have_tweaked_page_(false), - finished_(false) + dvd_generator::mpeg_encoder encoder) + : base_window(frame_params), + output_dir_(output_dir), + state_(state_initial), + generator_(frame_params, encoder), + pending_window_update_(false), + pending_req_count_(0) { - set_size_request(frame_params_.width, frame_params_.height); - set_resizable(false); - - add(browser_widget_); - browser_widget_.show(); Glib::signal_idle().connect( - SigC::slot(*this, &videolink_window::on_idle)); + sigc::mem_fun(this, &conversion_window::on_idle)); browser_widget_.signal_net_state().connect( - SigC::slot(*this, &videolink_window::on_net_state_change)); + sigc::mem_fun(this, &conversion_window::on_net_state_change)); add_menu(main_page_uri); } - bool videolink_window::is_finished() const + bool conversion_window::is_finished() const { - return finished_; + return state_ == state_finished; } - dvd_contents::pgc_ref videolink_window::add_menu(const std::string & uri) + dvd_generator::pgc_ref conversion_window::add_menu(const std::string & uri) { - dvd_contents::pgc_ref next_menu(dvd_contents::menu_pgc, - contents_.menus.size()); - std::pair insert_result( - resource_map_.insert(std::make_pair(uri, next_menu))); - - if (!insert_result.second) - { - return insert_result.first->second; - } - else + dvd_generator::pgc_ref & pgc_ref = resource_map_[uri]; + if (pgc_ref.type == dvd_generator::unknown_pgc) { + pgc_ref = generator_.add_menu(); page_queue_.push(uri); - contents_.menus.resize(contents_.menus.size() + 1); - return next_menu; } + return pgc_ref; } - dvd_contents::pgc_ref videolink_window::add_title(const std::string & uri) + dvd_generator::pgc_ref conversion_window::add_title(const std::string & uri, + video_format format) { - dvd_contents::pgc_ref next_title(dvd_contents::title_pgc, - contents_.titles.size()); - std::pair insert_result( - resource_map_.insert(std::make_pair(uri, next_title))); + dvd_generator::pgc_ref & pgc_ref = resource_map_[uri]; - if (!insert_result.second) - { - return insert_result.first->second; - } - else + if (pgc_ref.type == dvd_generator::unknown_pgc) { Glib::ustring hostname; std::string path(Glib::filename_from_uri(uri, hostname)); @@ -274,7 +325,7 @@ namespace // Store a reference to a linked VOB file, or the contents // of a linked VOB list file. - if (path.compare(path.size() - 4, 4, ".vob") == 0) + if (format == video_format_mpeg2_ps) { if (!Glib::file_test(path, Glib::FILE_TEST_IS_REGULAR)) throw std::runtime_error( @@ -283,38 +334,35 @@ namespace ref.file = path; list.push_back(ref); } - else + else if (format == video_format_vob_list) { - assert(path.compare(path.size() - 8, 8, ".voblist") == 0); read_vob_list(path).swap(list); } + else + { + assert(!"unrecognised format in add_title"); + } - contents_.titles.resize(contents_.titles.size() + 1); - contents_.titles.back().swap(list); - return next_title; + pgc_ref = generator_.add_title(list); } + + return pgc_ref; } - void videolink_window::load_next_page() + void conversion_window::load_next_page() { assert(!page_queue_.empty()); const std::string & uri = page_queue_.front(); - std::cout << "loading " << uri << std::endl; + std::cout << "INFO: Loading <" << uri << ">" << std::endl; browser_widget_.load_uri(uri); } - bool videolink_window::on_idle() - { - load_next_page(); - return false; // don't call again thankyou - } - - void videolink_window::on_net_state_change(const char * uri, - gint flags, guint status) + void conversion_window::on_net_state_change(const char * uri, + gint flags, guint status) { # ifdef DEBUG_ON_NET_STATE_CHANGE - std::cout << "videolink_window::on_net_state_change("; + std::cout << "conversion_window::on_net_state_change("; if (uri) std::cout << '"' << uri << '"'; else @@ -368,7 +416,6 @@ namespace && flags & GTK_MOZ_EMBED_FLAG_START) { pending_window_update_ = true; - have_tweaked_page_ = false; } if (flags & GTK_MOZ_EMBED_FLAG_IS_WINDOW @@ -376,168 +423,178 @@ namespace { // Check whether the load was successful, ignoring this // pseudo-error. +#ifdef NS_IMAGELIB_ERROR_LOAD_ABORTED if (status != NS_IMAGELIB_ERROR_LOAD_ABORTED) +#endif check(status); pending_window_update_ = false; } + } - if (!browser_is_busy()) + struct conversion_window::page_state + { + page_state(Glib::RefPtr norm_pixbuf, + nsIDOMDocument * doc, int width, int height) + : norm_pixbuf(norm_pixbuf), + diff_pixbuf(Gdk::Pixbuf::create( + Gdk::COLORSPACE_RGB, + true, 8, // has_alpha, bits_per_sample + width, height)), + links_it(doc), + link_changing(false) + { + } + + Glib::RefPtr norm_pixbuf; + Glib::RefPtr diff_pixbuf; + + link_iterator links_it, links_end; + + rectangle link_rect; + std::string link_target; + bool link_changing; + }; + + bool conversion_window::on_idle() + { + if (state_ == state_initial) + { + // Put pointer in the top-left so that no links appear in + // the hover state when we take a screenshot. + warp_pointer(get_window(), + -frame_params_.width, -frame_params_.height); + + load_next_page(); + + state_ = state_processing; + } + else if (state_ == state_processing && !browser_is_busy()) { try { - if (!process_page()) + if (!process()) { - finished_ = true; + state_ = state_finished; Gtk::Main::quit(); } } - catch (std::exception & e) + catch (...) { - std::cerr << "Fatal error"; + // Print context of exception. if (!page_queue_.empty()) - std::cerr << " while processing <" << page_queue_.front() - << ">"; - std::cerr << ": " << e.what() << "\n"; - Gtk::Main::quit(); - } - catch (Glib::Exception & e) - { - std::cerr << "Fatal error"; - if (!page_queue_.empty()) - std::cerr << " while processing <" << page_queue_.front() - << ">"; - std::cerr << ": " << e.what() << "\n"; + { + std::cerr << "ERROR: While processing page <" + << page_queue_.front() << ">:\n"; + if (page_state_.get() && !page_state_->link_target.empty()) + std::cerr << "ERROR: While processing link to <" + << page_state_->link_target << ">:\n"; + } + + // Print exception message. + try + { + throw; + } + catch (std::exception & e) + { + std::cerr << "ERROR: " << e.what() << "\n"; + } + catch (Glib::Exception & e) + { + std::cerr << "ERROR: " << e.what() << "\n"; + } + catch (...) + { + std::cerr << "ERROR: Unknown exception\n"; + } + Gtk::Main::quit(); } } + + // Call again if we're not done. + return state_ != state_finished; } - bool videolink_window::process_page() + bool conversion_window::process() { assert(!page_queue_.empty()); nsCOMPtr browser(browser_widget_.get_browser()); nsCOMPtr doc_shell(do_GetInterface(browser)); assert(doc_shell); - nsCOMPtr pres_shell; - check(doc_shell->GetPresShell(getter_AddRefs(pres_shell))); - nsCOMPtr pres_context; - check(doc_shell->GetPresContext(getter_AddRefs(pres_context))); nsCOMPtr dom_window; check(browser->GetContentDOMWindow(getter_AddRefs(dom_window))); - // If we haven't done so already, apply the stylesheet and - // disable scrollbars. - if (!have_tweaked_page_) - { - apply_style_sheet(stylesheet_, pres_shell); - - // This actually only needs to be done once. - nsCOMPtr dom_bar_prop; - check(dom_window->GetScrollbars(getter_AddRefs(dom_bar_prop))); - check(dom_bar_prop->SetVisible(false)); - - have_tweaked_page_ = true; - - // Might need to wait a while for things to load or more - // likely for a re-layout. - if (browser_is_busy()) - return true; - } + nsCOMPtr basic_doc; + check(dom_window->GetDocument(getter_AddRefs(basic_doc))); - // All further work should only be done if we're not in preview mode. - if (!output_dir_.empty()) + // Start or continue processing links. + if (!page_state_.get()) + page_state_.reset( + new page_state( + get_screenshot(), + basic_doc, frame_params_.width, frame_params_.height)); + if (!process_links(page_state_.get(), basic_doc, doc_shell, dom_window)) { - // If we haven't already started work on this menu, save a - // screenshot of its normal appearance. - if (!page_state_.get()) - save_screenshot(); - - // Start or continue processing links. - process_links(pres_shell, pres_context, dom_window); - - // If we've finished work on the links, move on to the - // next page, if any, or else generate the DVD filesystem. - if (!page_state_.get()) + // We've finished work on the links so generate the + // menu VOB. + quantise_rgba_pixbuf(page_state_->diff_pixbuf, + dvd::button_n_colours); + generator_.generate_menu_vob( + resource_map_[page_queue_.front()].index, + page_state_->norm_pixbuf, page_state_->diff_pixbuf); + + // Move on to the next page, if any, or else generate + // the DVD filesystem. + page_state_.reset(); + page_queue_.pop(); + if (!page_queue_.empty()) { - page_queue_.pop(); - if (page_queue_.empty()) - { - generate_dvd(contents_, output_dir_); - return false; - } - else - { - load_next_page(); - } + load_next_page(); + } + else + { + generator_.generate(output_dir_); + return false; } } return true; } - void videolink_window::save_screenshot() + Glib::RefPtr conversion_window::get_screenshot() { Glib::RefPtr window(get_window()); assert(window); window->process_updates(true); - background_temp_.reset(new temp_file("videolink-back-")); - background_temp_->close(); - std::cout << "saving " << background_temp_->get_name() << std::endl; - Gdk::Pixbuf::create(Glib::RefPtr(window), - window->get_colormap(), - 0, 0, 0, 0, - frame_params_.width, frame_params_.height) - ->save(background_temp_->get_name(), "png"); + return Gdk::Pixbuf::create(Glib::RefPtr(window), + window->get_colormap(), + 0, 0, 0, 0, + frame_params_.width, frame_params_.height); } - struct videolink_window::page_state - { - page_state(nsIDOMDocument * doc, int width, int height) - : diff_pixbuf(Gdk::Pixbuf::create( - Gdk::COLORSPACE_RGB, - true, 8, // has_alpha, bits_per_sample - width, height)), - spumux_temp("videolink-spumux-"), - links_temp("videolink-links-"), - link_num(0), - links_it(doc), - link_changing(false) - { - spumux_temp.close(); - links_temp.close(); - } - - Glib::RefPtr diff_pixbuf; - - temp_file spumux_temp; - std::ofstream spumux_file; - - temp_file links_temp; - - unsigned link_num; - link_iterator links_it, links_end; - - rectangle link_rect; - bool link_changing; - Glib::RefPtr norm_pixbuf; - }; - - void videolink_window::process_links(nsIPresShell * pres_shell, - nsIPresContext * pres_context, - nsIDOMWindow * dom_window) + bool conversion_window::process_links( + page_state * state, + nsIDOMDocument * basic_doc, + nsIDocShell * doc_shell, + nsIDOMWindow * dom_window) { Glib::RefPtr window(get_window()); assert(window); - nsCOMPtr basic_doc; - check(dom_window->GetDocument(getter_AddRefs(basic_doc))); nsCOMPtr ns_doc(do_QueryInterface(basic_doc)); assert(ns_doc); +#if MOZ_VERSION_GE(2,0,-1) + nsCOMPtr doc(do_QueryInterface(basic_doc)); + assert(doc); +#endif + nsCOMPtr pres_shell; + check(doc_shell->GetPresShell(getter_AddRefs(pres_shell))); nsCOMPtr event_state_man( - pres_context->EventStateManager()); // does not AddRef + get_event_state_manager(doc_shell)); assert(event_state_man); nsCOMPtr event_factory( do_QueryInterface(basic_doc)); @@ -547,28 +604,11 @@ namespace nsCOMPtr view; check(doc_view->GetDefaultView(getter_AddRefs(view))); - // Set up or recover our iteration state. - std::auto_ptr state(page_state_); - if (!state.get()) - { - state.reset( - new page_state( - basic_doc, frame_params_.width, frame_params_.height)); - - state->spumux_file.open(state->spumux_temp.get_name().c_str()); - state->spumux_file << - "\n" - " \n" - " \n"; - } - rectangle window_rect = { 0, 0, frame_params_.width, frame_params_.height }; - unsigned menu_num = resource_map_[page_queue_.front()].index; + unsigned menu_index = resource_map_[page_queue_.front()].index; for (/* no initialisation */; state->links_it != state->links_end; @@ -577,21 +617,29 @@ namespace nsCOMPtr node(*state->links_it); // Find the link URI and separate any fragment from it. + nsCOMPtr uri_iface; +#if MOZ_VERSION_GE(2,0,-1) + nsCOMPtr content(do_QueryInterface(node)); + assert(content); + uri_iface = content->GetHrefURI(); + assert(uri_iface); +#else nsCOMPtr link(do_QueryInterface(node)); assert(link); - nsCOMPtr uri_iface; check(link->GetHrefURI(getter_AddRefs(uri_iface))); - std::string uri_and_fragment, uri, fragment; +#endif + std::string uri, fragment; { - nsCString uri_and_fragment_ns; - check(uri_iface->GetSpec(uri_and_fragment_ns)); - uri_and_fragment.assign(uri_and_fragment_ns.BeginReading(), - uri_and_fragment_ns.EndReading()); - - std::size_t hash_pos = uri_and_fragment.find('#'); - uri.assign(uri_and_fragment, 0, hash_pos); + nsCString link_target_ns; + check(uri_iface->GetSpec(link_target_ns)); + const char * str; + PRUint32 len = NS_CStringGetData(link_target_ns, &str); + state->link_target.assign(str, len); + + std::size_t hash_pos = state->link_target.find('#'); + uri.assign(state->link_target, 0, hash_pos); if (hash_pos != std::string::npos) - fragment.assign(uri_and_fragment, + fragment.assign(state->link_target, hash_pos + 1, std::string::npos); } @@ -602,64 +650,45 @@ namespace // window. nsCOMPtr elem(do_QueryInterface(node)); assert(elem); +#if MOZ_VERSION_GE(2,0,-1) + state->link_rect = get_elem_rect(doc, elem); +#else state->link_rect = get_elem_rect(ns_doc, elem); +#endif state->link_rect &= window_rect; if (state->link_rect.empty()) { - std::cerr << "Ignoring invisible link to " - << uri_and_fragment << "\n"; - continue; - } - - ++state->link_num; - - if (state->link_num >= unsigned(dvd::menu_buttons_max)) - { - if (state->link_num == unsigned(dvd::menu_buttons_max)) - std::cerr << "No more than " << dvd::menu_buttons_max - << " buttons can be placed on a menu\n"; - std::cerr << "Ignoring link to " << uri_and_fragment - << "\n"; + std::cerr << "WARN: Ignoring invisible link to <" + << state->link_target << ">\n"; continue; } - state->spumux_file << - "