X-Git-Url: https://git.decadent.org.uk/gitweb/?p=videolink.git;a=blobdiff_plain;f=webdvd.cpp;h=e2d8cb87425867a969e293e08c95e53a306ad598;hp=cc47bd140eb026f2c5fa33452057ca8b65c77dad;hb=1d013cc3943da6dcfd32a4ab639464d00e5c7d16;hpb=bd93155f1cd53bfa0eb15eaf2caf28a3b1305438 diff --git a/webdvd.cpp b/webdvd.cpp index cc47bd1..e2d8cb8 100644 --- a/webdvd.cpp +++ b/webdvd.cpp @@ -174,8 +174,29 @@ namespace struct dvd_contents { - enum pgc_type { menu_pgc, title_pgc }; - typedef std::pair pgc_ref; + enum pgc_type { unknown_pgc, menu_pgc, title_pgc }; + + struct pgc_ref + { + explicit pgc_ref(pgc_type type = unknown_pgc, + int index = -1, + int sub_index = 0) + : type(type), index(index), sub_index(sub_index) + {} + bool operator==(const pgc_ref & other) const + { + return type == other.type && index == other.index; + } + bool operator!=(const pgc_ref & other) const + { + return !(*this == other); + } + + pgc_type type; // Menu or title reference? + unsigned index; // Menu or title index (within resp. vector) + unsigned sub_index; // Button or chapter number (1-based; 0 if + // unspecified; not compared!) + }; struct menu { @@ -202,6 +223,9 @@ namespace std::vector titles; }; + void generate_dvd(const dvd_contents & contents, + const std::string & output_dir); + class webdvd_window : public Gtk::Window { public: @@ -215,12 +239,15 @@ namespace dvd_contents::pgc_ref add_title(const std::string & uri); void load_next_page(); void on_net_state_change(const char * uri, gint flags, guint status); + bool browser_is_busy() const + { + 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); - void generate_dvd(); video::frame_params frame_params_; std::string output_dir_; @@ -407,7 +434,7 @@ namespace pending_window_update_ = false; } - if (pending_req_count_ == 0 && !pending_window_update_) + if (!browser_is_busy()) { try { @@ -423,6 +450,15 @@ namespace 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"; + Gtk::Main::quit(); + } } } @@ -455,7 +491,7 @@ namespace // Might need to wait a while for things to load or more // likely for a re-layout. - if (pending_req_count_ > 0) + if (browser_is_busy()) return true; } @@ -477,7 +513,7 @@ namespace page_queue_.pop(); if (page_queue_.empty()) { - generate_dvd(); + generate_dvd(contents_, output_dir_); return false; } else @@ -530,7 +566,7 @@ namespace temp_file links_temp; - int link_num; + unsigned link_num; link_iterator links_it, links_end; rectangle link_rect; @@ -581,7 +617,7 @@ namespace 0, 0, frame_params_.width, frame_params_.height }; - int menu_num = resource_map_[page_queue_.front()].second; + unsigned menu_num = resource_map_[page_queue_.front()].index; for (/* no initialisation */; state->links_it != state->links_end; @@ -589,19 +625,24 @@ namespace { nsCOMPtr<nsIDOMNode> node(*state->links_it); - // Find the link URI. + // Find the link URI and separate any fragment from it. nsCOMPtr<nsILink> link(do_QueryInterface(node)); assert(link); - nsCOMPtr<nsIURI> uri; - check(link->GetHrefURI(getter_AddRefs(uri))); - std::string uri_string; + nsCOMPtr<nsIURI> uri_iface; + check(link->GetHrefURI(getter_AddRefs(uri_iface))); + std::string uri_and_fragment, uri, fragment; { - nsCString uri_ns_string; - check(uri->GetSpec(uri_ns_string)); - uri_string.assign(uri_ns_string.BeginReading(), - uri_ns_string.EndReading()); + 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); + if (hash_pos != std::string::npos) + fragment.assign(uri_and_fragment, + hash_pos + 1, std::string::npos); } - std::string uri_sans_fragment(uri_string, 0, uri_string.find('#')); // Is this a new link? if (!state->link_changing) @@ -616,18 +657,19 @@ namespace if (state->link_rect.empty()) { std::cerr << "Ignoring invisible link to " - << uri_string << "\n"; + << uri_and_fragment << "\n"; continue; } ++state->link_num; - if (state->link_num >= dvd::menu_buttons_max) + if (state->link_num >= unsigned(dvd::menu_buttons_max)) { - if (state->link_num == 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_string << "\n"; + std::cerr << "Ignoring link to " << uri_and_fragment + << "\n"; continue; } @@ -640,32 +682,33 @@ namespace // Check whether this is a link to a video or a page then // add it to the known resources if not already seen; then // add it to the menu entries. - nsCString path; - check(uri->GetPath(path)); - dvd_contents::pgc_ref dest_pgc; + dvd_contents::pgc_ref target; // FIXME: This is a bit of a hack. Perhaps we could decide // later based on the MIME type determined by Mozilla? - if ((path.Length() > 4 - && std::strcmp(path.EndReading() - 4, ".vob") == 0) - || (path.Length() > 8 - && std::strcmp(path.EndReading() - 8, ".voblist") - == 0)) + if ((uri.size() > 4 + && uri.compare(uri.size() - 4, 4, ".vob") == 0) + || (uri.size() > 8 + && uri.compare(uri.size() - 8, 8, ".voblist") == 0)) { PRBool is_file; - check(uri->SchemeIs("file", &is_file)); + check(uri_iface->SchemeIs("file", &is_file)); if (!is_file) { std::cerr << "Links to video must use the file:" << " scheme\n"; continue; } - dest_pgc = add_title(uri_sans_fragment); + target = add_title(uri); + target.sub_index = + std::strtoul(fragment.c_str(), NULL, 10); } else { - dest_pgc = add_menu(uri_sans_fragment); + target = add_menu(uri); + // TODO: If there's a fragment, work out which button + // is closest and set target.sub_index. } - contents_.menus[menu_num].entries.push_back(dest_pgc); + contents_.menus[menu_num].entries.push_back(target); nsCOMPtr<nsIContent> content(do_QueryInterface(node)); assert(content); @@ -714,7 +757,7 @@ namespace // We may have to exit and wait for image loading // to complete, at which point we will be called // again. - if (pending_req_count_ > 0) + if (browser_is_busy()) { state->link_changing = true; page_state_ = state; @@ -794,7 +837,8 @@ namespace } } - void webdvd_window::generate_dvd() + void generate_dvd(const dvd_contents & contents, + const std::string & output_dir) { temp_file temp("webdvd-dvdauthor-"); temp.close(); @@ -821,11 +865,11 @@ namespace " <vmgm>\n" " <menus>\n"; - for (std::size_t menu_num = 0; - menu_num != contents_.menus.size(); + for (unsigned menu_num = 0; + menu_num != contents.menus.size(); ++menu_num) { - dvd_contents::menu & menu = contents_.menus[menu_num]; + const dvd_contents::menu & menu = contents.menus[menu_num]; if (menu_num == 0) { @@ -871,7 +915,7 @@ namespace menu_incr != 0; menu_incr /= 2) { - if (menu_num + menu_incr < contents_.menus.size() + if (menu_num + menu_incr < contents.menus.size() && (menu_num & (menu_incr * 2 - 1)) == 0) { file << @@ -888,44 +932,57 @@ namespace " </pre>\n" " <vob file='" << menu.vob_temp->get_name() << "'/>\n"; - for (std::size_t button_num = 0; + for (unsigned button_num = 0; button_num != menu.entries.size(); ++button_num) { + const dvd_contents::pgc_ref & target = + menu.entries[button_num]; + file << " <button> "; - if (menu.entries[button_num].first == dvd_contents::menu_pgc) + if (target.type == dvd_contents::menu_pgc) { - int dest_menu_num = menu.entries[button_num].second; - - // Look for a button on the new menu that links - // back to this one. If there is one, set that to - // be the highlighted button; otherwise, use the - // first button. - const std::vector<dvd_contents::pgc_ref> & - dest_menu_entries = - contents_.menus[dest_menu_num].entries; - dvd_contents::pgc_ref this_pgc( - dvd_contents::menu_pgc, menu_num); - std::size_t dest_button_num = dest_menu_entries.size(); - while (dest_button_num != 0 - && dest_menu_entries[--dest_button_num] != this_pgc) - ; + unsigned target_button_num; + + if (target.sub_index) + { + target_button_num = target.sub_index; + } + else + { + // Look for a button on the new menu that links + // back to this one. If there is one, set that to + // be the highlighted button; otherwise, use the + // first button. + const std::vector<dvd_contents::pgc_ref> & + target_menu_entries = + contents.menus[target.index].entries; + dvd_contents::pgc_ref this_pgc(dvd_contents::menu_pgc, + menu_num); + target_button_num = target_menu_entries.size(); + while (target_button_num != 0 + && (target_menu_entries[--target_button_num] + != this_pgc)) + ; + } file << "g1 = " - << (1 + dest_menu_num - + (1 + dest_button_num) * button_mult) - << "; jump menu " << 1 + dest_menu_num << ";"; + << (1 + target.index + + (1 + target_button_num) * button_mult) + << "; jump menu " << 1 + target.index << ";"; } else { - assert(menu.entries[button_num].first - == dvd_contents::title_pgc); + assert(target.type == dvd_contents::title_pgc); file << "g1 = " << 1 + menu_num + (1 + button_num) * button_mult << "; jump title " - << 1 + menu.entries[button_num].second << ";"; + << 1 + target.index; + if (target.sub_index) + file << " chapter " << target.sub_index; + file << ";"; } file << " </button>\n"; @@ -941,8 +998,8 @@ namespace // Generate a titleset for each title. This appears to make // jumping to titles a whole lot simpler (but limits us to 99 // titles). - for (std::size_t title_num = 0; - title_num != contents_.titles.size(); + for (unsigned title_num = 0; + title_num != contents.titles.size(); ++title_num) { file << @@ -958,7 +1015,7 @@ namespace " <pgc>\n" // Record calling location. " <pre> g12 = g1; </pre>\n" - << contents_.titles[title_num].vob_list << + << contents.titles[title_num].vob_list << // If the menu location has not been changed during // the title, set the location to be the following // button in the menu. In any case, return to some @@ -978,7 +1035,7 @@ namespace { const char * argv[] = { "dvdauthor", - "-o", output_dir_.c_str(), + "-o", output_dir.c_str(), "-x", temp.get_name().c_str(), 0 }; @@ -1029,16 +1086,17 @@ namespace void set_browser_preferences() { - // Disable IE-compatibility kluge that causes backgrounds to - // sometimes/usually be missing from snapshots. This is only - // effective from Mozilla 1.8 onward. -# if MOZ_VERSION_MAJOR > 1 \ - || (MOZ_VERSION_MAJOR == 1 && MOZ_VERSION_MINOR >= 8) nsCOMPtr<nsIPrefService> pref_service; static const nsCID pref_service_cid = NS_PREFSERVICE_CID; check(CallGetService<nsIPrefService>(pref_service_cid, getter_AddRefs(pref_service))); nsCOMPtr<nsIPrefBranch> pref_branch; + + // Disable IE-compatibility kluge that causes backgrounds to + // sometimes/usually be missing from snapshots. This is only + // effective from Mozilla 1.8 onward. +# if MOZ_VERSION_MAJOR > 1 \ + || (MOZ_VERSION_MAJOR == 1 && MOZ_VERSION_MINOR >= 8) check(pref_service->GetDefaultBranch("layout", getter_AddRefs(pref_branch))); check(pref_branch->SetBoolPref( @@ -1046,9 +1104,17 @@ namespace true)); # endif - // TODO: Set display resolution? Unfortunately Mozilla doesn't - // support non-square pixels (and neither do fontconfig or Xft - // anyway). + // Set display resolution. With standard-definition video we + // will be fitting ~600 pixels across a screen typically + // ranging from 10 to 25 inches wide, for a resolution of + // 24-60 dpi. I therefore declare the average horizontal + // resolution to be 40 dpi. The vertical resolution will be + // slightly higher (PAL/SECAM) or lower (NTSC), but + // unfortunately Mozilla doesn't support non-square pixels + // (and neither do fontconfig or Xft anyway). + check(pref_service->GetDefaultBranch("browser.display", + getter_AddRefs(pref_branch))); + check(pref_branch->SetIntPref("screen_resolution", 40)); } } // namespace