-
- page_temp_files_.push_back(vob_temp);
- }
- }
-
- void generate_page_dispatch(std::ostream &, int indent,
- int first_page, int last_page);
-
- void WebDvdWindow::generate_dvd()
- {
- temp_file temp("webdvd-dvdauthor-");
- temp.close();
- std::ofstream file(temp.get_name().c_str());
-
- // We generate code that uses registers in the following way:
- //
- // g0: link destination (when jumping to menu 1), then scratch
- // g1: current location
- // g2-g11: location history (g2 = most recent)
- // g12: location that last linked to a video
- //
- // All locations are divided into two bitfields: the least
- // significant 10 bits are a page/menu number and the most
- // significant 6 bits are a link/button number. This is
- // chosen for compatibility with the encoding of the s8
- // (button) register.
- //
- static const int link_mult = dvd::reg_s8_button_mult;
- static const int page_mask = link_mult - 1;
- static const int link_mask = (1 << dvd::reg_bits) - link_mult;
-
- file <<
- "<dvdauthor>\n"
- " <vmgm>\n"
- " <menus>\n";
-
- for (std::size_t page_num = 1;
- page_num <= page_links_.size();
- ++page_num)
- {
- std::vector<std::string> & page_links =
- page_links_[page_num - 1];
-
- if (page_num == 1)
- {
- // This is the first page (root menu) which needs to
- // include initialisation and dispatch code.
-
- file <<
- " <pgc entry='title'>\n"
- " <pre>\n"
- // Has the location been set yet?
- " if (g1 eq 0)\n"
- " {\n"
- // Initialise the current location to first link on
- // this page.
- " g1 = " << 1 * link_mult + 1 << ";\n"
- " }\n"
- " else\n"
- " {\n"
- // Has the user selected a link?
- " if (g0 ne 0)\n"
- " {\n"
- // First update the history.
- // Does link go to the last page in the history?
- " if (((g0 ^ g2) & " << page_mask
- << ") == 0)\n"
- // It does; we treat this as going back and pop the old
- // location off the history stack into the current
- // location. Clear the free stack slot.
- " {\n"
- " g1 = g2; g2 = g3; g3 = g4; g4 = g5;\n"
- " g5 = g6; g6 = g7; g7 = g8; g8 = g9;\n"
- " g9 = g10; g10 = g11; g11 = 0;\n"
- " }\n"
- " else\n"
- // Link goes to some other page, so push current
- // location onto the history stack and set the current
- // location to be exactly the target location.
- " {\n"
- " g11 = g10; g10 = g9; g9 = g8; g8 = g7;\n"
- " g7 = g6; g6 = g5; g5 = g4; g4 = g3;\n"
- " g3 = g2; g2 = g1; g1 = g0;\n"
- " }\n"
- " }\n"
- // Find the target page number.
- " g0 = g1 & " << page_mask << ";\n";
- // There seems to be no way to perform a computed jump,
- // so we generate all possible jumps and a binary search
- // to select the correct one.
- generate_page_dispatch(file, 12, 1, page_links_.size());
- file <<
- " }\n";
- }
- else // page_num != 1
- {
- file <<
- " <pgc>\n"
- " <pre>\n";
- }
-
- file <<
- // Clear link indicator and highlight the
- // appropriate link/button.
- " g0 = 0; s8 = g1 & " << link_mask << ";\n"
- " </pre>\n"
- " <vob file='"
- << page_temp_files_[page_num - 1]->get_name() << "'/>\n";
-
- for (std::size_t link_num = 1;
- link_num <= page_links.size();
- ++link_num)
- {
- file <<
- " <button>"
- // Update current location.
- " g1 = " << link_num * link_mult + page_num << ";";
-
- // Jump to appropriate resource.
- const ResourceEntry & resource_loc =
- resource_map_[page_links[link_num - 1]];
- if (resource_loc.first == page_resource)
- file <<
- " g0 = " << 1 * link_mult + resource_loc.second << ";"
- " jump menu 1;";
- else if (resource_loc.first == video_resource)
- file << " jump title " << resource_loc.second << ";";
-
- file << " </button>\n";
- }
-
- file << " </pgc>\n";
- }
-
- file <<
- " </menus>\n"
- " </vmgm>\n";
-
- // Generate a titleset for each video. This appears to make
- // jumping to titles a whole lot simpler.
- for (std::size_t video_num = 1;
- video_num <= video_paths_.size();
- ++video_num)
- {
- file <<
- " <titleset>\n"
- // Generate a dummy menu so that the menu button on the
- // remote control will work.
- " <menus>\n"
- " <pgc entry='root'>\n"
- " <pre> jump vmgm menu; </pre>\n"
- " </pgc>\n"
- " </menus>\n"
- " <titles>\n"
- " <pgc>\n"
- // Record calling page/menu.
- " <pre> g12 = g1; </pre>\n"
- // FIXME: Should XML-escape the path
- " <vob file='" << video_paths_[video_num - 1]
- << "'/>\n"
- // If page/menu location has not been changed during the
- // video, change the location to be the following
- // link/button when returning to it. In any case,
- // return to a page/menu.
- " <post> if (g1 eq g12) g1 = g1 + " << link_mult
- << "; call menu; </post>\n"
- " </pgc>\n"
- " </titles>\n"
- " </titleset>\n";
- }
-
- file <<
- "</dvdauthor>\n";
-
- file.close();
-
- {
- const char * argv[] = {
- "dvdauthor",
- "-o", output_dir_.c_str(),
- "-x", temp.get_name().c_str(),
- 0
- };
- int command_result;
- Glib::spawn_sync(".",
- Glib::ArrayHandle<std::string>(
- argv, sizeof(argv)/sizeof(argv[0]),
- Glib::OWNERSHIP_NONE),
- Glib::SPAWN_SEARCH_PATH
- | Glib::SPAWN_STDOUT_TO_DEV_NULL,
- SigC::Slot0<void>(),
- 0, 0,
- &command_result);
- if (command_result != 0)
- throw std::runtime_error("dvdauthor failed");
- }
- }
-
- void generate_page_dispatch(std::ostream & file, int indent,
- int first_page, int last_page)
- {
- if (first_page == 1 && last_page == 1)
- {
- // The dispatch code is *on* page 1 so we must not dispatch to
- // page 1 since that would cause an infinite loop. This case
- // should be unreachable if there is more than one page due
- // to the following case.
- }
- else if (first_page == 1 && last_page == 2)
- {
- // dvdauthor doesn't allow empty blocks or null statements so
- // when selecting between pages 1 and 2 we don't use an "else"
- // part. We must use braces so that a following "else" will
- // match the right "if".
- file << std::setw(indent) << "" << "{\n"
- << std::setw(indent) << "" << "if (g0 eq 2)\n"
- << std::setw(indent + 2) << "" << "jump menu 2;\n"
- << std::setw(indent) << "" << "}\n";
- }
- else if (first_page == last_page)
- {
- file << std::setw(indent) << ""
- << "jump menu " << first_page << ";\n";
- }
- else
- {
- int middle = (first_page + last_page) / 2;
- file << std::setw(indent) << "" << "if (g0 le " << middle << ")\n";
- generate_page_dispatch(file, indent + 2, first_page, middle);
- file << std::setw(indent) << "" << "else\n";
- generate_page_dispatch(file, indent + 2, middle + 1, last_page);