]> git.decadent.org.uk Git - videolink.git/blobdiff - webdvd.cpp
Added code to set the display resolution assumed by Mozilla.
[videolink.git] / webdvd.cpp
index e3c02e90f271e757283ddf4f667d5ab8e5296193..31ad122776a154586a14ea9baf4e7642331dcd5a 100644 (file)
@@ -142,6 +142,36 @@ namespace
        return result;
     }
 
+
+    std::string xml_escape(const std::string & str)
+    {
+       std::string result;
+       std::size_t begin = 0;
+
+       for (;;)
+       {
+           std::size_t end = str.find_first_of("\"&'<>", begin);
+           result.append(str, begin, end - begin);
+           if (end == std::string::npos)
+               return result;
+
+           const char * entity = NULL;
+           switch (str[end])
+           {
+           case '"':  entity = "&quot;"; break;
+           case '&':  entity = "&amp;";  break;
+           case '\'': entity = "&apos;"; break;
+           case '<':  entity = "&lt;";   break;
+           case '>':  entity = "&gt;";   break;
+           }
+           assert(entity);
+           result.append(entity);
+
+           begin = end + 1;
+       }
+    }
+
+    
     struct dvd_contents
     {
        enum pgc_type { menu_pgc, title_pgc };
@@ -280,8 +310,7 @@ namespace
                        filename + " is missing or not a regular file");
                vob_list
                    .append("<vob file='")
-                   // FIXME: Should XML-escape the path
-                   .append(filename)
+                   .append(xml_escape(filename))
                    .append("'/>\n");
            }
            else
@@ -765,9 +794,6 @@ namespace
        }
     }
 
-    void generate_menu_dispatch(std::ostream &, int indent,
-                               int first_menu, int last_menu);
-
     void webdvd_window::generate_dvd()
     {
        temp_file temp("webdvd-dvdauthor-");
@@ -776,10 +802,9 @@ namespace
 
        // We generate code that uses registers in the following way:
        //
-       // g0:     button destination (when jumping to menu 1), then scratch
+       // g0:     scratch
        // g1:     current location
-       // g2-g11: location history (g2 = most recent)
-       // g12:    location that last linked to a video
+       // g12:    location that last jumped to a video
        //
        // All locations are divided into two bitfields: the least
        // significant 10 bits are a page/menu number and the most
@@ -790,7 +815,6 @@ namespace
        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"
@@ -805,93 +829,102 @@ namespace
 
            if (menu_num == 0)
            {
-               // This is the first (title) menu, which needs to include
-               // initialisation and dispatch code.
-       
+               // This is the first (title) menu, displayed when the
+               // disc is first played.
                file <<
                    "      <pgc entry='title'>\n"
                    "        <pre>\n"
-                   // Has the location been set yet?
+                   // Initialise the current location if it is not set
+                   // (all general registers are initially 0).
                    "          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) &amp; " << 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 &amp; " << 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";
+                   "            g1 = " << 1 + button_mult << ";\n";
            }
-           else // menu_num != 0
+           else
            {
                file <<
                    "      <pgc>\n"
                    "        <pre>\n";
            }
 
+           // When a title finishes or the user presses the menu
+           // button, this always jumps to the titleset's root menu.
+           // We want to return the user to the last menu they used.
+           // So we arrange for each titleset's root menu to return
+           // to the vmgm title menu and then dispatch from there to
+           // whatever the correct menu is.  We determine the correct
+           // menu by looking at the menu part of g1.
+
+           file << "          g0 = g1 &amp; " << menu_mask << ";\n";
+
+           // There is a limit of 128 VM instructions in each PGC.
+           // Therefore in each menu's <pre> section we generate
+           // jumps to menus with numbers greater by 512, 256, 128,
+           // ..., 1 where (a) such a menu exists, (b) this menu
+           // number is divisible by twice that increment and (c) the
+           // correct menu is that or a later menu.  Thus each menu
+           // has at most 10 such conditional jumps and is reachable
+           // by at most 10 jumps from the title menu.  This chain of
+           // jumps might take too long on some players; this has yet
+           // to be investigated.
+           
+           for (std::size_t menu_incr = (menu_mask + 1) / 2;
+                menu_incr != 0;
+                menu_incr /= 2)
+           {
+               if (menu_num + menu_incr < contents_.menus.size()
+                   && (menu_num & (menu_incr * 2 - 1)) == 0)
+               {
+                   file <<
+                       "          if (g0 ge " << 1 + menu_num + menu_incr
+                                              << ")\n"
+                       "            jump menu " << 1 + menu_num + menu_incr
+                                              << ";\n";
+               }
+           }
+
            file <<
-               // Clear link indicator and highlight the
-               // appropriate link/button.
-               "          g0 = 0; s8 = g1 &amp; " << button_mask << ";\n"
+               // Highlight the appropriate button.
+               "          s8 = g1 &amp; " << button_mask << ";\n"
                "        </pre>\n"
-               "        <vob file='"
-                << menu.vob_temp->get_name() << "'/>\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
-                    << ";";
+               file << "        <button> ";
 
-               // 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;";
+                   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)
+                       ;
+                        
+                   file << "g1 = "
+                        << (1 + dest_menu_num
+                            + (1 + dest_button_num) * button_mult)
+                        << "; jump menu " << 1 + dest_menu_num << ";";
                }
                else
                {
                    assert(menu.entries[button_num].first
                           == dvd_contents::title_pgc);
-                   file << " jump title "
+
+                   file << "g1 = "
+                        << 1 + menu_num + (1 + button_num) * button_mult
+                        << "; jump title "
                         << 1 + menu.entries[button_num].second << ";";
                }
 
@@ -923,13 +956,13 @@ namespace
                "    </menus>\n"
                "    <titles>\n"
                "      <pgc>\n"
-               // Record calling page/menu.
+               // Record calling location.
                "        <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.
+               // 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
+               // menu.
                "        <post> if (g1 eq g12) g1 = g1 + " << button_mult
                 << "; call menu; </post>\n"
                "      </pgc>\n"
@@ -964,50 +997,6 @@ namespace
        }
     }
 
-    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);
-           }
-       }
-    }
-
     const video::frame_params & lookup_frame_params(const char * str)
     {
        assert(str);
@@ -1040,16 +1029,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(
@@ -1057,9 +1047,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