- void generate_menu_dispatch(std::ostream &, int indent,
- int first_menu, int last_menu);
-
- void webdvd_window::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: button 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, and numbering
- // starts at 1, not 0. This is done for compatibility with
- // the encoding of the s8 (button) register.
- //
- static const int button_mult = dvd::reg_s8_button_mult;
- static const int menu_mask = button_mult - 1;
- static const int button_mask = (1 << dvd::reg_bits) - button_mult;
- static const int location_bias = button_mult + 1;
-
- file <<
- "<dvdauthor>\n"
- " <vmgm>\n"
- " <menus>\n";
-
- for (std::size_t menu_num = 0;
- menu_num != contents_.menus.size();
- ++menu_num)
- {
- dvd_contents::menu & menu = contents_.menus[menu_num];
-
- if (menu_num == 0)
- {
- // This is the first (title) 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 button on
- // this menu.
- " g1 = " << location_bias << ";\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) & " << menu_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 & " << menu_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_menu_dispatch(file, 12,
- 0, contents_.menus.size() - 1);
- file <<
- " }\n";
- }
- else // menu_num != 0
- {
- file <<
- " <pgc>\n"
- " <pre>\n";
- }
-
- file <<
- // Clear link indicator and highlight the
- // appropriate link/button.
- " g0 = 0; s8 = g1 & " << button_mask << ";\n"
- " </pre>\n"
- " <vob file='"
- << menu.vob_temp->get_name() << "'/>\n";
-
- for (std::size_t button_num = 0;
- button_num != menu.entries.size();
- ++button_num)
- {
- file << " <button> "
- // Update current location.
- " g1 = "
- << location_bias + button_num * button_mult + menu_num
- << ";";
-
- // Jump to appropriate resource.
- if (menu.entries[button_num].first == dvd_contents::menu_pgc)
- {
- file << " g0 = "
- << location_bias + menu.entries[button_num].second
- << "; jump menu 1;";
- }
- else
- {
- assert(menu.entries[button_num].first
- == dvd_contents::title_pgc);
- file << " jump title "
- << 1 + menu.entries[button_num].second << ";";
- }
-
- file << " </button>\n";
- }
-
- file << " </pgc>\n";
- }
-
- file <<
- " </menus>\n"
- " </vmgm>\n";
-
- // 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();
- ++title_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"
- << contents_.titles[title_num].vob_list <<
- // 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 + " << button_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_menu_dispatch(std::ostream & file, int indent,
- int first_menu, int last_menu)
- {
- if (first_menu == last_menu)
- {
- if (first_menu == 0)
- {
- // This dispatch code is generated *on* the first menu
- // so don't create an infinite loop.
- }
- else
- {
- file << std::setw(indent) << ""
- << "jump menu " << 1 + first_menu << ";\n";
- }
- }
- else // first_menu != last_menu
- {
- if (first_menu == 0 && last_menu == 1)
- {
- // dvdauthor doesn't allow empty blocks or null
- // statements so when selecting between the first 2
- // menus 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
- {
- int middle = (first_menu + last_menu) / 2;
- file << std::setw(indent) << "" << "if (g0 le " << 1 + middle
- << ")\n";
- generate_menu_dispatch(file, indent + 2,
- first_menu, middle);
- file << std::setw(indent) << "" << "else\n";
- generate_menu_dispatch(file, indent + 2,
- middle + 1, last_menu);
- }
- }
- }
-