gui: use XML-based page for splash screen instead of a static jpg

For devices with an unusual aspect ratio, stretching a bitmap to
the screen resolution is sub-optimal. So let's reuse our XML engine
that allows assembling the splash screen from individual parts.

The splash page is loaded from a separate XML file for quick loading.

Change-Id: I2d3dad26e42fcefaf563dacdf0ffa61f209dada1
diff --git a/gui/gui.cpp b/gui/gui.cpp
index d99764a..7cf21b0 100644
--- a/gui/gui.cpp
+++ b/gui/gui.cpp
@@ -62,15 +62,11 @@
 #define LOGEVENT(...) do {} while (0)
 #endif
 
-const static int CURTAIN_FADE = 32;
-
 using namespace rapidxml;
 
 // Global values
-static gr_surface gCurtain = NULL;
 static int gGuiInitialized = 0;
 static TWAtomicInt gForceRender;
-const int gNoAnimation = 1;
 blanktimer blankTimer;
 int ors_read_fd = -1;
 static FILE* orsout = NULL;
@@ -107,80 +103,6 @@
 	//abort();
 }
 
-static void curtainSet()
-{
-	gr_color(0, 0, 0, 255);
-	gr_fill(0, 0, gr_fb_width(), gr_fb_height());
-	gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain), gr_get_height(gCurtain), TW_X_OFFSET, TW_Y_OFFSET);
-	gr_flip();
-}
-
-static void curtainRaise(gr_surface surface)
-{
-	int sy = 0;
-	int h = gr_get_height(gCurtain) - 1;
-	int w = gr_get_width(gCurtain);
-	int fy = 1;
-
-	int msw = gr_get_width(surface);
-	int msh = gr_get_height(surface);
-	int CURTAIN_RATE = msh / 30;
-
-	if (gNoAnimation == 0)
-	{
-		for (; h > 0; h -= CURTAIN_RATE, sy += CURTAIN_RATE, fy += CURTAIN_RATE)
-		{
-			gr_blit(surface, 0, 0, msw, msh, 0, 0);
-			gr_blit(gCurtain, 0, sy, w, h, 0, 0);
-			gr_flip();
-		}
-	}
-	gr_blit(surface, 0, 0, msw, msh, 0, 0);
-	flip();
-}
-
-void curtainClose()
-{
-#if 0
-	int w = gr_get_width(gCurtain);
-	int h = 1;
-	int sy = gr_get_height(gCurtain) - 1;
-	int fbh = gr_fb_height();
-	int CURTAIN_RATE = fbh / 30;
-
-	if (gNoAnimation == 0)
-	{
-		for (; h < fbh; h += CURTAIN_RATE, sy -= CURTAIN_RATE)
-		{
-			gr_blit(gCurtain, 0, sy, w, h, 0, 0);
-			gr_flip();
-		}
-		gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain),
-		gr_get_height(gCurtain), 0, 0);
-		gr_flip();
-
-		if (gRecorder != -1)
-			close(gRecorder);
-
-		int fade;
-		for (fade = 16; fade < 255; fade += CURTAIN_FADE)
-		{
-			gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain),
-			gr_get_height(gCurtain), 0, 0);
-			gr_color(0, 0, 0, fade);
-			gr_fill(0, 0, gr_fb_width(), gr_fb_height());
-			gr_flip();
-		}
-		gr_color(0, 0, 0, 255);
-		gr_fill(0, 0, gr_fb_width(), gr_fb_height());
-		gr_flip();
-	}
-#else
-	gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain), gr_get_height(gCurtain), 0, 0);
-	gr_flip();
-#endif
-}
-
 class InputHandler
 {
 public:
@@ -618,17 +540,6 @@
 		gui_changePage(page_name);
 	}
 
-	// Raise the curtain
-	if (gCurtain != NULL)
-	{
-		gr_surface surface;
-
-		PageManager::Render();
-		gr_get_surface(&surface);
-		curtainRaise(surface);
-		gr_free_surface(surface);
-	}
-
 	gGuiRunning = 1;
 
 	DataManager::SetValue("tw_loaded", 1);
@@ -822,30 +733,18 @@
 extern "C" int gui_init(void)
 {
 	gr_init();
-	std::string curtain_path = TWRES "images/curtain.jpg";
-	gr_surface source_Surface = NULL;
 
-	if (res_create_surface(curtain_path.c_str(), &source_Surface))
-	{
-		printf("Unable to locate '%s'\nDid you set a TW_THEME in your config files?\n", curtain_path.c_str());
-		return -1;
+	// load and show splash screen
+	if (PageManager::LoadPackage("splash", TWRES "splash.xml", "splash")) {
+		LOGERR("Failed to load splash screen XML.\n");
 	}
-	if (gr_get_width(source_Surface) != (unsigned)gr_fb_width() || gr_get_height(source_Surface) != (unsigned)gr_fb_height()) {
-		// We need to scale the curtain to fit the screen
-		float scale_w = (float)gr_fb_width() / (float)gr_get_width(source_Surface);
-		float scale_h = (float)gr_fb_height() / (float)gr_get_height(source_Surface);
-		if (res_scale_surface(source_Surface, &gCurtain, scale_w, scale_h)) {
-			LOGINFO("Failed to scale curtain\n");
-			gCurtain = source_Surface;
-		} else {
-			LOGINFO("Scaling the curtain width %fx and height %fx\n", scale_w, scale_h);
-		}
-	} else {
-		gCurtain = source_Surface;
+	else {
+		PageManager::SelectPackage("splash");
+		PageManager::Render();
+		flip();
+		PageManager::ReleasePackage("splash");
 	}
 
-	curtainSet();
-
 	ev_init();
 	return 0;
 }
diff --git a/gui/pages.cpp b/gui/pages.cpp
index def2629..c455662 100644
--- a/gui/pages.cpp
+++ b/gui/pages.cpp
@@ -64,7 +64,6 @@
 
 std::map<std::string, PageSet*> PageManager::mPageSets;
 PageSet* PageManager::mCurrentSet;
-PageSet* PageManager::mBaseSet = NULL;
 MouseCursor *PageManager::mMouseCursor = NULL;
 HardwareKeyboard *PageManager::mHardwareKeyboard = NULL;
 bool PageManager::mReloadTheme = false;
@@ -1421,10 +1420,6 @@
 			LOGERR("Package %s failed to load.\n", name.c_str());
 	}
 
-	// The first successful package we loaded is the base
-	if (mBaseSet == NULL)
-		mBaseSet = mCurrentSet;
-
 	mCurrentSet = pageSet;
 
 	if (pZip) {
@@ -1500,8 +1495,6 @@
 	}
 	if (mCurrentSet == set)
 		SelectPackage(name);
-	if (mBaseSet == set)
-		mBaseSet = mCurrentSet;
 	delete set;
 	GUIConsole::Translate_Now();
 	return 0;
@@ -1518,6 +1511,8 @@
 	PageSet* set = (*iter).second;
 	mPageSets.erase(iter);
 	delete set;
+	if (set == mCurrentSet)
+		mCurrentSet = NULL;
 	return;
 }
 
diff --git a/gui/pages.hpp b/gui/pages.hpp
index bb521c1..cf1afa1 100644
--- a/gui/pages.hpp
+++ b/gui/pages.hpp
@@ -178,7 +178,6 @@
 protected:
 	static std::map<std::string, PageSet*> mPageSets;
 	static PageSet* mCurrentSet;
-	static PageSet* mBaseSet;
 	static MouseCursor *mMouseCursor;
 	static HardwareKeyboard *mHardwareKeyboard;
 	static bool mReloadTheme;
diff --git a/gui/theme/landscape_hdpi/images/curtain.jpg b/gui/theme/landscape_hdpi/images/curtain.jpg
deleted file mode 100644
index 51ea6a5..0000000
--- a/gui/theme/landscape_hdpi/images/curtain.jpg
+++ /dev/null
Binary files differ
diff --git a/gui/theme/landscape_hdpi/images/splashlogo.png b/gui/theme/landscape_hdpi/images/splashlogo.png
new file mode 100755
index 0000000..337a7b6
--- /dev/null
+++ b/gui/theme/landscape_hdpi/images/splashlogo.png
Binary files differ
diff --git a/gui/theme/landscape_hdpi/images/splashteamwin.png b/gui/theme/landscape_hdpi/images/splashteamwin.png
new file mode 100755
index 0000000..f44a8fe
--- /dev/null
+++ b/gui/theme/landscape_hdpi/images/splashteamwin.png
Binary files differ
diff --git a/gui/theme/landscape_hdpi/splash.xml b/gui/theme/landscape_hdpi/splash.xml
new file mode 100755
index 0000000..1a9fd53
--- /dev/null
+++ b/gui/theme/landscape_hdpi/splash.xml
@@ -0,0 +1,52 @@
+<?xml version="1.0"?>

+<recovery>

+	<details>

+		<resolution width="1920" height="1200"/>

+		<author>TeamWin</author>

+		<title>TWRP</title>

+		<description>Default basic theme</description>

+		<preview>preview.jpg</preview>

+		<themeversion>1</themeversion>

+	</details>

+

+	<resources>

+		<font name="font_l" filename="RobotoCondensed-Regular.ttf" size="52"/>

+		<image name="splashlogo" filename="splashlogo" retainaspect="1"/>

+		<image name="splashteamwin" filename="splashteamwin" retainaspect="1"/>

+	</resources>

+

+	<variables>

+		<variable name="screen_width" value="1920"/>

+		<variable name="screen_height" value="1200"/>

+		<variable name="background_color" value="#222222"/>

+		<variable name="header_color" value="#555555"/>

+		<variable name="accent_color" value="#0090CA"/>

+	</variables>

+

+	<pages>

+		<page name="splash">

+			<background color="%background_color%"/>

+

+			<fill color="%header_color%">

+				<placement x="0" y="0" w="%screen_width%" h="320"/>

+			</fill>

+

+			<image>

+				<image resource="splashlogo"/>

+				<placement x="960" y="320" placement="4"/>

+			</image>

+

+			<image>

+				<image resource="splashteamwin"/>

+				<placement x="960" y="920" placement="4"/>

+			</image>

+

+			<text color="%header_color%">

+				<font resource="font_l"/>

+				<placement x="960" y="970" placement="5"/>

+				<text>Recovery Project %tw_version%</text>

+			</text>

+		</page>

+	</pages>

+</recovery>

+

diff --git a/gui/theme/landscape_mdpi/images/curtain.jpg b/gui/theme/landscape_mdpi/images/curtain.jpg
deleted file mode 100644
index f79ab92..0000000
--- a/gui/theme/landscape_mdpi/images/curtain.jpg
+++ /dev/null
Binary files differ
diff --git a/gui/theme/landscape_mdpi/images/splashlogo.png b/gui/theme/landscape_mdpi/images/splashlogo.png
new file mode 100755
index 0000000..4b29425
--- /dev/null
+++ b/gui/theme/landscape_mdpi/images/splashlogo.png
Binary files differ
diff --git a/gui/theme/landscape_mdpi/images/splashteamwin.png b/gui/theme/landscape_mdpi/images/splashteamwin.png
new file mode 100755
index 0000000..125501f
--- /dev/null
+++ b/gui/theme/landscape_mdpi/images/splashteamwin.png
Binary files differ
diff --git a/gui/theme/landscape_mdpi/splash.xml b/gui/theme/landscape_mdpi/splash.xml
new file mode 100755
index 0000000..34f8325
--- /dev/null
+++ b/gui/theme/landscape_mdpi/splash.xml
@@ -0,0 +1,52 @@
+<?xml version="1.0"?>

+<recovery>

+	<details>

+		<resolution width="800" height="480"/>

+		<author>TeamWin</author>

+		<title>TWRP</title>

+		<description>Default basic theme</description>

+		<preview>preview.jpg</preview>

+		<themeversion>1</themeversion>

+	</details>

+

+	<resources>

+		<font name="font_l" filename="RobotoCondensed-Regular.ttf" size="24"/>

+		<image name="splashlogo" filename="splashlogo" retainaspect="1"/>

+		<image name="splashteamwin" filename="splashteamwin" retainaspect="1"/>

+	</resources>

+

+	<variables>

+		<variable name="screen_width" value="800"/>

+		<variable name="screen_height" value="480"/>

+		<variable name="background_color" value="#222222"/>

+		<variable name="header_color" value="#555555"/>

+		<variable name="accent_color" value="#0090CA"/>

+	</variables>

+

+	<pages>

+		<page name="splash">

+			<background color="%background_color%"/>

+

+			<fill color="%header_color%">

+				<placement x="0" y="0" w="%screen_width%" h="140"/>

+			</fill>

+

+			<image>

+				<image resource="splashlogo"/>

+				<placement x="400" y="140" placement="4"/>

+			</image>

+

+			<image>

+				<image resource="splashteamwin"/>

+				<placement x="400" y="366" placement="4"/>

+			</image>

+

+			<text color="%header_color%">

+				<font resource="font_l"/>

+				<placement x="400" y="386" placement="5"/>

+				<text>Recovery Project %tw_version%</text>

+			</text>

+		</page>

+	</pages>

+</recovery>

+

diff --git a/gui/theme/portrait_hdpi/images/curtain.jpg b/gui/theme/portrait_hdpi/images/curtain.jpg
deleted file mode 100644
index fd0ea31..0000000
--- a/gui/theme/portrait_hdpi/images/curtain.jpg
+++ /dev/null
Binary files differ
diff --git a/gui/theme/portrait_hdpi/images/splashlogo.png b/gui/theme/portrait_hdpi/images/splashlogo.png
new file mode 100755
index 0000000..337a7b6
--- /dev/null
+++ b/gui/theme/portrait_hdpi/images/splashlogo.png
Binary files differ
diff --git a/gui/theme/portrait_hdpi/images/splashteamwin.png b/gui/theme/portrait_hdpi/images/splashteamwin.png
new file mode 100755
index 0000000..f44a8fe
--- /dev/null
+++ b/gui/theme/portrait_hdpi/images/splashteamwin.png
Binary files differ
diff --git a/gui/theme/portrait_hdpi/splash.xml b/gui/theme/portrait_hdpi/splash.xml
new file mode 100755
index 0000000..964b2af
--- /dev/null
+++ b/gui/theme/portrait_hdpi/splash.xml
@@ -0,0 +1,52 @@
+<?xml version="1.0"?>
+<recovery>
+	<details>
+		<resolution width="1080" height="1920"/>
+		<author>TeamWin</author>
+		<title>TWRP</title>
+		<description>Default basic theme</description>
+		<preview>preview.jpg</preview>
+		<themeversion>1</themeversion>
+	</details>
+
+	<resources>
+		<font name="font_l" filename="RobotoCondensed-Regular.ttf" size="52"/>
+		<image name="splashlogo" filename="splashlogo" retainaspect="1"/>
+		<image name="splashteamwin" filename="splashteamwin" retainaspect="1"/>
+	</resources>
+
+	<variables>
+		<variable name="screen_width" value="1080"/>
+		<variable name="screen_height" value="1920"/>
+		<variable name="background_color" value="#222222"/>
+		<variable name="header_color" value="#555555"/>
+		<variable name="accent_color" value="#0090CA"/>
+	</variables>
+
+	<pages>
+		<page name="splash">
+			<background color="%background_color%"/>
+
+			<fill color="%header_color%">
+				<placement x="0" y="0" w="%screen_width%" h="456"/>
+			</fill>
+
+			<image>
+				<image resource="splashlogo"/>
+				<placement x="540" y="456" placement="4"/>
+			</image>
+
+			<image>
+				<image resource="splashteamwin"/>
+				<placement x="540" y="1540" placement="4"/>
+			</image>
+
+			<text color="%header_color%">
+				<font resource="font_l"/>
+				<placement x="540" y="1590" placement="5"/>
+				<text>Recovery Project %tw_version%</text>
+			</text>
+		</page>
+	</pages>
+</recovery>
+
diff --git a/gui/theme/portrait_mdpi/images/curtain.jpg b/gui/theme/portrait_mdpi/images/curtain.jpg
deleted file mode 100644
index 680b023..0000000
--- a/gui/theme/portrait_mdpi/images/curtain.jpg
+++ /dev/null
Binary files differ
diff --git a/gui/theme/portrait_mdpi/images/splashlogo.png b/gui/theme/portrait_mdpi/images/splashlogo.png
new file mode 100755
index 0000000..4b29425
--- /dev/null
+++ b/gui/theme/portrait_mdpi/images/splashlogo.png
Binary files differ
diff --git a/gui/theme/portrait_mdpi/images/splashteamwin.png b/gui/theme/portrait_mdpi/images/splashteamwin.png
new file mode 100755
index 0000000..125501f
--- /dev/null
+++ b/gui/theme/portrait_mdpi/images/splashteamwin.png
Binary files differ
diff --git a/gui/theme/portrait_mdpi/splash.xml b/gui/theme/portrait_mdpi/splash.xml
new file mode 100755
index 0000000..31f7933
--- /dev/null
+++ b/gui/theme/portrait_mdpi/splash.xml
@@ -0,0 +1,52 @@
+<?xml version="1.0"?>

+<recovery>

+	<details>

+		<resolution width="480" height="800"/>

+		<author>TeamWin</author>

+		<title>TWRP</title>

+		<description>Default basic theme</description>

+		<preview>preview.jpg</preview>

+		<themeversion>1</themeversion>

+	</details>

+

+	<resources>

+		<font name="font_l" filename="RobotoCondensed-Regular.ttf" size="24"/>

+		<image name="splashlogo" filename="splashlogo" retainaspect="1"/>

+		<image name="splashteamwin" filename="splashteamwin" retainaspect="1"/>

+	</resources>

+

+	<variables>

+		<variable name="screen_width" value="480"/>

+		<variable name="screen_height" value="800"/>

+		<variable name="background_color" value="#222222"/>

+		<variable name="header_color" value="#555555"/>

+		<variable name="accent_color" value="#0090CA"/>

+	</variables>

+

+	<pages>

+		<page name="splash">

+			<background color="%background_color%"/>

+

+			<fill color="%header_color%">

+				<placement x="0" y="0" w="%screen_width%" h="200"/>

+			</fill>

+

+			<image>

+				<image resource="splashlogo"/>

+				<placement x="240" y="200" placement="4"/>

+			</image>

+

+			<image>

+				<image resource="splashteamwin"/>

+				<placement x="240" y="660" placement="4"/>

+			</image>

+

+			<text color="%header_color%">

+				<font resource="font_l"/>

+				<placement x="240" y="680" placement="5"/>

+				<text>Recovery Project %tw_version%</text>

+			</text>

+		</page>

+	</pages>

+</recovery>

+

diff --git a/gui/theme/watch_mdpi/images/curtain.jpg b/gui/theme/watch_mdpi/images/curtain.jpg
deleted file mode 100644
index 1d1d49e..0000000
--- a/gui/theme/watch_mdpi/images/curtain.jpg
+++ /dev/null
Binary files differ
diff --git a/gui/theme/watch_mdpi/images/splashlogo.png b/gui/theme/watch_mdpi/images/splashlogo.png
new file mode 100755
index 0000000..757c740
--- /dev/null
+++ b/gui/theme/watch_mdpi/images/splashlogo.png
Binary files differ
diff --git a/gui/theme/watch_mdpi/images/splashteamwin.png b/gui/theme/watch_mdpi/images/splashteamwin.png
new file mode 100755
index 0000000..94546f8
--- /dev/null
+++ b/gui/theme/watch_mdpi/images/splashteamwin.png
Binary files differ
diff --git a/gui/theme/watch_mdpi/splash.xml b/gui/theme/watch_mdpi/splash.xml
new file mode 100755
index 0000000..81bef45
--- /dev/null
+++ b/gui/theme/watch_mdpi/splash.xml
@@ -0,0 +1,52 @@
+<?xml version="1.0"?>

+<recovery>

+	<details>

+		<resolution width="320" height="320"/>

+		<author>TeamWin</author>

+		<title>TWRP</title>

+		<description>Default basic theme</description>

+		<preview>preview.jpg</preview>

+		<themeversion>1</themeversion>

+	</details>

+

+	<resources>

+		<font name="font_l" filename="RobotoCondensed-Regular.ttf" size="24"/>

+		<image name="splashlogo" filename="splashlogo" retainaspect="1"/>

+		<image name="splashteamwin" filename="splashteamwin" retainaspect="1"/>

+	</resources>

+

+	<variables>

+		<variable name="screen_width" value="320"/>

+		<variable name="screen_height" value="320"/>

+		<variable name="background_color" value="#222222"/>

+		<variable name="header_color" value="#555555"/>

+		<variable name="accent_color" value="#0090CA"/>

+	</variables>

+

+	<pages>

+		<page name="splash">

+			<background color="%background_color%"/>

+

+			<fill color="%header_color%">

+				<placement x="0" y="0" w="%screen_width%" h="120"/>

+			</fill>

+

+			<image>

+				<image resource="splashlogo"/>

+				<placement x="160" y="120" placement="4"/>

+			</image>

+

+			<image>

+				<image resource="splashteamwin"/>

+				<placement x="160" y="270" placement="4"/>

+			</image>

+

+			<text color="%header_color%">

+				<font resource="font_l"/>

+				<placement x="160" y="290" placement="5"/>

+				<text>Recovery Project %tw_version%</text>

+			</text>

+		</page>

+	</pages>

+</recovery>

+