From 545b0c4526c53fe02b9714409abb904946bcf9f3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <pali.rohar@gmail.com>
Date: Sun, 11 Jul 2010 12:36:30 +0200
Subject: [PATCH] Added function SetFullGameName to LUA scripts - use name for
 WM icon and title  - On Unix load icon from /usr/share/pixmaps/  - On Windows
 load icon from exe file from previous image argv[0]

---
 doc/ChangeLog.html       |  2 ++
 src/include/stratagus.h  |  1 +
 src/stratagus/script.cpp | 17 +++++++++++
 src/video/sdl.cpp        | 61 +++++++++++++++++++++++++++++++++++++++-
 4 files changed, 80 insertions(+), 1 deletion(-)

diff --git a/doc/ChangeLog.html b/doc/ChangeLog.html
index b8aa65e76..6ebe9bd71 100644
--- a/doc/ChangeLog.html
+++ b/doc/ChangeLog.html
@@ -41,6 +41,8 @@
 	<li>Fixed saving campaign game - multiline lua strings and lua triggers (from Pali Rohar)
 	<li>Added option print debug output to console on Windows (from Pali Rohar)
 	<li>Added function PlayMovie to LUA scripts - play movies from game (from Pali Rohar)
+	<li>Added command line params -O and -o for force using OpenGL (from Pali Rohar)
+	<li>Added function SetFullGameName to LUA scripts - use name for WM icon and title (from Pali Rohar)
 	<li>Added support for 64bit Windows version (from Pali Rohar)
 	<li>Added new NSIS Installer for Windows (from Pali Rohar)
 	<li>Added support for more graphics resolutions (from Pali Rohar)
diff --git a/src/include/stratagus.h b/src/include/stratagus.h
index 62382b98c..253dc5ce7 100644
--- a/src/include/stratagus.h
+++ b/src/include/stratagus.h
@@ -242,6 +242,7 @@ extern const char NameLine[];
 extern std::string UserDirectory;           /// Directory containing user settings and data
 extern std::string StratagusLibPath;        /// Location of stratagus data
 extern std::string GameName;                /// Name of the game
+extern std::string FullGameName;            /// Full Name of the game
 extern std::string ClickMissile;            /// Missile to show when you click
 extern std::string DamageMissile;           /// Missile to show damage caused
 
diff --git a/src/stratagus/script.cpp b/src/stratagus/script.cpp
index 116f94a62..8e8cb6e5d 100644
--- a/src/stratagus/script.cpp
+++ b/src/stratagus/script.cpp
@@ -83,6 +83,7 @@ lua_State *Lua;                       /// Structure to work with lua files.
 std::string CclStartFile;             /// CCL start file
 std::string UserDirectory;
 std::string GameName;
+std::string FullGameName;
 int CclInConfigFile;                  /// True while config file parsing
 bool SaveGameLoading;                 /// If a Saved Game is Loading
 std::string CurrentLuaFile;           /// Lua file currently being interpreted
@@ -1928,6 +1929,21 @@ static int CclSetGameName(lua_State *l)
 	return 0;
 }
 
+static int CclSetFullGameName(lua_State *l)
+{
+	int args;
+
+	args = lua_gettop(l);
+	if (args > 1 || (args == 1 && (!lua_isnil(l, 1) && !lua_isstring(l, 1)))) {
+		LuaError(l, "incorrect argument");
+	}
+	if (args == 1 && !lua_isnil(l, 1)) {
+		FullGameName = lua_tostring(l, 1);
+	}
+
+	return 0;
+}
+
 /**
 **  Set the video sync speed
 **
@@ -2381,6 +2397,7 @@ void InitCcl(void)
 	lua_register(Lua, "ListFilesInDirectory", CclListFilesInDirectory);
 	lua_register(Lua, "ListDirsInDirectory", CclListDirsInDirectory);
 	lua_register(Lua, "SetGameName", CclSetGameName);
+	lua_register(Lua, "SetFullGameName", CclSetFullGameName);
 	lua_register(Lua, "SetVideoSyncSpeed", CclSetVideoSyncSpeed);
 	lua_register(Lua, "SetLocalPlayerName", CclSetLocalPlayerName);
 	lua_register(Lua, "GetLocalPlayerName", CclGetLocalPlayerName);
diff --git a/src/video/sdl.cpp b/src/video/sdl.cpp
index 3f027064f..25bc723a2 100644
--- a/src/video/sdl.cpp
+++ b/src/video/sdl.cpp
@@ -51,6 +51,7 @@
 #ifndef _MSC_VER
 #include <sys/time.h>
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <unistd.h>
 #endif
 #include "SDL.h"
@@ -67,6 +68,7 @@
 
 #ifdef USE_WIN32
 #include "net_lowlevel.h"
+#include "SDL_syswm.h"
 #endif
 
 #if defined(USE_WIN32) && defined(NO_STDIO_REDIRECT)
@@ -459,8 +461,65 @@ void InitVideoSdl(void)
 		signal(SIGSEGV, CleanExit);
 		signal(SIGABRT, CleanExit);
 #endif
+		if (FullGameName.empty())
+			FullGameName = "Stratagus";
+
 		// Set WindowManager Title
-		SDL_WM_SetCaption("Stratagus", "Stratagus");
+		SDL_WM_SetCaption(FullGameName.c_str(), FullGameName.c_str());
+
+#ifndef USE_WIN32
+		SDL_Surface * icon = NULL;
+		CGraphic * g = NULL;
+		struct stat st;
+		char buf[1024];
+		sprintf(buf, "/usr/share/pixmaps/%s.png", FullGameName.c_str());
+
+		if (stat(buf, &st) == 0) {
+			g = CGraphic::New(buf);
+			g->Load();
+			icon = g->Surface;
+		}
+
+		if (!icon) {
+			FullGameName[0] = tolower(FullGameName[0]);
+			sprintf(buf, "/usr/share/pixmaps/%s.png", FullGameName.c_str());
+
+			if (stat(buf, &st) == 0) {
+				if (g)
+					CGraphic::Free(g);
+				g = CGraphic::New(buf);
+				g->Load();
+				icon = g->Surface;
+			}
+		}
+
+		if (icon)
+			SDL_WM_SetIcon(icon, 0);
+
+		if (g)
+			CGraphic::Free(g);
+#else
+		int argc = 0;
+		LPWSTR * argv = NULL;
+		HWND hwnd = NULL;
+		HICON hicon = NULL;
+		SDL_SysWMinfo info;
+		SDL_VERSION(&info.version);
+
+		argv = CommandLineToArgvW(GetCommandLineW(), &argc);
+
+		if (SDL_GetWMInfo(&info))
+			hwnd = info.window;
+
+		if (hwnd)
+			hicon = ExtractIcon(GetModuleHandle(NULL), argv[0], 0);
+
+		if (hicon) {
+			SendMessage(hwnd, (UINT)WM_SETICON, ICON_BIG, (LPARAM)hicon);
+			SetClassLong(hwnd, GCL_HICON, (LONG)hicon);
+			DestroyIcon(hicon);
+		}
+#endif
 	}
 
 	// Initialize the display