| 1 |
/* |
| 2 |
Copyright (C) 2009-2010 wxLauncher Team |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or |
| 5 |
modify it under the terms of the GNU General Public License |
| 6 |
as published by the Free Software Foundation; either version 2 |
| 7 |
of the License, or (at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with this program; if not, write to the Free Software |
| 16 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "generated/configure_launcher.h" |
| 20 |
#include <wx/wx.h> |
| 21 |
#include <wx/cmdline.h> |
| 22 |
#include <wx/image.h> |
| 23 |
#include <wx/filesys.h> |
| 24 |
#include <wx/fs_arc.h> |
| 25 |
#include <wx/fs_inet.h> |
| 26 |
#include <wx/splash.h> |
| 27 |
|
| 28 |
#if HAS_SDL == 1 |
| 29 |
#include "SDL.h" |
| 30 |
#endif |
| 31 |
|
| 32 |
// prevents SDL_main.h (included by SDL.h) from redefining main |
| 33 |
#ifdef main |
| 34 |
#undef main |
| 35 |
#endif |
| 36 |
|
| 37 |
#include "wxLauncherApp.h" |
| 38 |
#include "MainWindow.h" |
| 39 |
#include "apis/SkinManager.h" |
| 40 |
#include "controls/Logger.h" |
| 41 |
#include "global/version.h" |
| 42 |
#include "apis/TCManager.h" |
| 43 |
#include "apis/ProfileManager.h" |
| 44 |
#include "apis/HelpManager.h" |
| 45 |
#include "apis/FlagListManager.h" |
| 46 |
#include "apis/ProfileProxy.h" |
| 47 |
|
| 48 |
#include "global/MemoryDebugging.h" // Last include for memory debugging |
| 49 |
|
| 50 |
#if IS_LINUX |
| 51 |
// main needs to be handled by us on non-Mac *nix as SDL interferes with wxWidgets |
| 52 |
IMPLEMENT_APP_NO_MAIN(wxLauncher); |
| 53 |
#else |
| 54 |
// Windows is fine and also needs special WinMain treatment |
| 55 |
// OS X can't have SDL2 and wxWidgets 3 both initializing at startup |
| 56 |
IMPLEMENT_APP(wxLauncher); |
| 57 |
#endif |
| 58 |
|
| 59 |
void wxLauncher::OnInitCmdLine(wxCmdLineParser& parser) |
| 60 |
{ |
| 61 |
static const char addprofiledesc[] = |
| 62 |
"Add profile PROFILE from FILE. If PROFILE " |
| 63 |
"already exists it will not be overwritten. *Operator*"; |
| 64 |
static const char selectprofiledesc[] = |
| 65 |
"Make PROFILE the that wxLauncher will use " |
| 66 |
"on next run. *Operator*"; |
| 67 |
static const char profiledesc[] = |
| 68 |
"The name of a profile to operate on. Operand PROFILE."; |
| 69 |
static const char filedesc[] = |
| 70 |
"The path to a file to operate on. Operand FILE."; |
| 71 |
static const char sessiononlydesc[] = |
| 72 |
"Do not remember the profile that is selected at exit"; |
| 73 |
|
| 74 |
/* Operators */ |
| 75 |
parser.AddSwitch(wxEmptyString, wxT_2("add-profile"), |
| 76 |
wxGetTranslation(wxString::FromUTF8(addprofiledesc))); |
| 77 |
parser.AddSwitch(wxEmptyString, wxT_2("select-profile"), |
| 78 |
wxGetTranslation(wxString::FromUTF8(selectprofiledesc))); |
| 79 |
|
| 80 |
/* Operands */ |
| 81 |
parser.AddOption(wxEmptyString, wxT_2("profile"), |
| 82 |
wxGetTranslation(wxString::FromUTF8(profiledesc)), |
| 83 |
wxCMD_LINE_VAL_STRING); |
| 84 |
parser.AddOption(wxEmptyString, wxT_2("file"), |
| 85 |
wxGetTranslation(wxString::FromUTF8(filedesc)), |
| 86 |
wxCMD_LINE_VAL_STRING); |
| 87 |
|
| 88 |
/* Other */ |
| 89 |
parser.AddSwitch(wxEmptyString, wxT_2("session-only"), |
| 90 |
wxGetTranslation(wxString::FromUTF8(sessiononlydesc))); |
| 91 |
|
| 92 |
parser.SetSwitchChars(wxT_2("-")); // always use -, even on windows |
| 93 |
|
| 94 |
wxApp::OnInitCmdLine(parser); |
| 95 |
} |
| 96 |
|
| 97 |
bool wxLauncher::OnCmdLineParsed(wxCmdLineParser& parser) |
| 98 |
{ |
| 99 |
if (!wxApp::OnCmdLineParsed(parser)) |
| 100 |
return false; |
| 101 |
|
| 102 |
if (parser.Found(wxT_2("session-only"))) |
| 103 |
{ |
| 104 |
mKeepForSessionOnly = true; |
| 105 |
} |
| 106 |
|
| 107 |
if (parser.Found(wxT_2("add-profile"))) |
| 108 |
{ |
| 109 |
mProfileOperator = ProManOperator::add; |
| 110 |
if (!parser.Found(wxT_2("profile"), &mProfileOperand)) |
| 111 |
{ |
| 112 |
wxLogError(_("No profile specified to add")); |
| 113 |
return false; |
| 114 |
} |
| 115 |
if (!parser.Found(wxT_2("file"), &mFileOperand)) |
| 116 |
{ |
| 117 |
wxLogError(_("No file specified to add as profile")); |
| 118 |
return false; |
| 119 |
} |
| 120 |
} |
| 121 |
else if(parser.Found(wxT_2("select-profile"))) |
| 122 |
{ |
| 123 |
mProfileOperator = ProManOperator::select; |
| 124 |
if (!parser.Found(wxT_2("profile"), &mProfileOperand)) |
| 125 |
{ |
| 126 |
wxLogError(_("No profile specified to select")); |
| 127 |
return false; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
return true; |
| 132 |
} |
| 133 |
|
| 134 |
wxLauncher::wxLauncher() |
| 135 |
:mProfileOperator(ProManOperator::none), |
| 136 |
mKeepForSessionOnly(false), |
| 137 |
mShowGUI(false) |
| 138 |
// The strings init themselves sanely |
| 139 |
{ |
| 140 |
} |
| 141 |
|
| 142 |
static const char* DISPLAY_SPLASH_FAIL_TEXT = |
| 143 |
"Unable to load splash image." |
| 144 |
" This normally means that you are running the Launcher from a folder" |
| 145 |
" that the launcher does not know how to find the resource folder from." |
| 146 |
"\n\nThe launcher is expecting (%s) to contain the resource images."; |
| 147 |
|
| 148 |
/** Display the splash screen. |
| 149 |
|
| 150 |
\param splashWindow Out. Point to splash window if created. NULL otherwise. |
| 151 |
\param returns false if an error occurented while loading spalsh; a |
| 152 |
false return value indicates that this function has already informed the user |
| 153 |
of the failue. Returns true otherwise. |
| 154 |
*/ |
| 155 |
bool displaySplash(wxSplashScreen **splashWindow) |
| 156 |
{ |
| 157 |
wxBitmap splash; |
| 158 |
// splash image location is fixed so that it's known at compile time |
| 159 |
wxFileName splashFile(wxT_2(RESOURCES_PATH), wxT_2("wxL_Splash.png")); |
| 160 |
if (splash.LoadFile(splashFile.GetFullPath(), wxBITMAP_TYPE_ANY)) { |
| 161 |
#if NDEBUG |
| 162 |
(*splashWindow) = new wxSplashScreen(splash, wxSPLASH_CENTRE_ON_SCREEN, 0, NULL, wxID_ANY); |
| 163 |
#else |
| 164 |
(*splashWindow) = NULL;//new wxSplashScreen(splash, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_TIMEOUT, 1000, NULL, wxID_ANY); |
| 165 |
#endif |
| 166 |
wxYield(); |
| 167 |
} else { |
| 168 |
wxFileName expectedDir; |
| 169 |
if (wxFileName(wxT_2(RESOURCES_PATH)).IsAbsolute()) { |
| 170 |
expectedDir = wxFileName(wxT_2(RESOURCES_PATH)); |
| 171 |
} else { |
| 172 |
expectedDir = wxFileName(::wxGetCwd(), wxT_2(RESOURCES_PATH)); |
| 173 |
} |
| 174 |
wxString errmsg(DISPLAY_SPLASH_FAIL_TEXT, wxMBConvUTF8()); |
| 175 |
wxLogFatalError(wxString::Format(wxGetTranslation(errmsg), |
| 176 |
expectedDir.GetFullPath().c_str()).c_str()); |
| 177 |
return false; |
| 178 |
} |
| 179 |
return true; |
| 180 |
} |
| 181 |
|
| 182 |
int wxLauncher::OnRun() { |
| 183 |
if (mProfileOperator == ProManOperator::none) |
| 184 |
{ |
| 185 |
return wxApp::OnRun(); |
| 186 |
} |
| 187 |
else |
| 188 |
{ |
| 189 |
return ProManOperator::RunProfileOperator(mProfileOperator); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
bool wxLauncher::OnInit() { |
| 194 |
wxInitAllImageHandlers(); |
| 195 |
|
| 196 |
// call base class OnInit so that cmdline stuff works. |
| 197 |
if (!wxApp::OnInit()) |
| 198 |
return false; // base said abort so abort |
| 199 |
|
| 200 |
wxLog::SetActiveTarget(new Logger()); |
| 201 |
wxLogInfo(wxT_2("wxLauncher Version %d.%d.%d"), MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION); |
| 202 |
wxLogInfo(wxT_2("Build \"%s\" committed on (%s)"), GITVersion, GITDate); |
| 203 |
wxLogInfo(wxDateTime(time(NULL)).Format(wxT_2("%c"))); |
| 204 |
|
| 205 |
#if MSCRTMEMORY |
| 206 |
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); |
| 207 |
#endif |
| 208 |
// Little hack to deal with people starting the launcher from the bin folder |
| 209 |
if ( !wxFileName::DirExists(wxT_2(RESOURCES_PATH)) ) { |
| 210 |
wxFileName resourceDir; |
| 211 |
resourceDir.AssignDir(::wxGetCwd()); |
| 212 |
resourceDir.AppendDir(wxT_2("..")); |
| 213 |
resourceDir.AppendDir(wxT_2(RESOURCES_PATH)); |
| 214 |
if ( resourceDir.DirExists() ) { |
| 215 |
wxFileName newWorkingDir(::wxGetCwd(), wxT_2("..")); |
| 216 |
::wxSetWorkingDirectory(newWorkingDir.GetFullPath()); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
wxSplashScreen* splashWindow = NULL; |
| 221 |
if (!displaySplash(&splashWindow)) |
| 222 |
return false; |
| 223 |
|
| 224 |
wxLogInfo(wxT_2("Initializing profiles...")); |
| 225 |
ProMan::Flags promanFlags = ProMan::None; |
| 226 |
if (mKeepForSessionOnly) |
| 227 |
promanFlags = promanFlags | ProMan::NoUpdateLastProfile; |
| 228 |
if ( !ProMan::Initialize(promanFlags) ) { |
| 229 |
wxLogFatalError(wxT_2("ProfileManager failed to initialize. Aborting! See log file for more details.")); |
| 230 |
return false; |
| 231 |
} |
| 232 |
if (mProfileOperator != ProManOperator::none) |
| 233 |
{ |
| 234 |
// We are not to create a GUI so we are done init now |
| 235 |
return true; |
| 236 |
} |
| 237 |
|
| 238 |
wxFileSystem::AddHandler(new wxArchiveFSHandler); |
| 239 |
wxFileSystem::AddHandler(new wxInternetFSHandler); |
| 240 |
|
| 241 |
wxLogInfo(wxT_2("Initializing SkinSystem...")); |
| 242 |
SkinSystem::Initialize(); |
| 243 |
|
| 244 |
wxLogInfo(wxT_2("Initializing HelpManager...")); |
| 245 |
HelpManager::Initialize(); |
| 246 |
|
| 247 |
wxLogInfo(wxT_2("Initializing FlagListManager...")); |
| 248 |
FlagListManager::Initialize(); |
| 249 |
|
| 250 |
wxLogInfo(wxT_2("Initializing ProfileProxy...")); |
| 251 |
ProfileProxy::Initialize(); |
| 252 |
|
| 253 |
wxLogInfo(wxT_2("wxLauncher starting up.")); |
| 254 |
|
| 255 |
|
| 256 |
MainWindow* window = new MainWindow(); |
| 257 |
wxLogStatus(_("MainWindow is complete")); |
| 258 |
window->Show(true); |
| 259 |
#if NDEBUG // will autodelete when timout runs out in debug |
| 260 |
splashWindow->Show(false); |
| 261 |
splashWindow->Destroy(); |
| 262 |
#endif |
| 263 |
|
| 264 |
// must call TCManager::CurrentProfileChanged() manually on startup, |
| 265 |
// since initial profile switch takes place before TCManager has been initialized |
| 266 |
// calling it here to ensure that AdvSettingsPage is set up by the time events are triggered |
| 267 |
wxCommandEvent tcMgrInitEvent; |
| 268 |
TCManager::Get()->CurrentProfileChanged(tcMgrInitEvent); |
| 269 |
|
| 270 |
wxLogStatus(_("Ready.")); |
| 271 |
return true; |
| 272 |
} |
| 273 |
|
| 274 |
int wxLauncher::OnExit() { |
| 275 |
|
| 276 |
ProMan::DeInitialize(); |
| 277 |
|
| 278 |
if (mProfileOperator == ProManOperator::none) |
| 279 |
{ |
| 280 |
|
| 281 |
// deinitialize subsystems in the opposite order of initialization |
| 282 |
ProfileProxy::DeInitialize(); |
| 283 |
FlagListManager::DeInitialize(); |
| 284 |
HelpManager::DeInitialize(); |
| 285 |
SkinSystem::DeInitialize(); |
| 286 |
|
| 287 |
#if HAS_SDL |
| 288 |
SDL_QuitSubSystem(SDL_INIT_VIDEO); |
| 289 |
#endif |
| 290 |
|
| 291 |
} |
| 292 |
|
| 293 |
wxLogInfo(_("wxLogger shutdown complete.")); |
| 294 |
|
| 295 |
return wxApp::OnExit(); |
| 296 |
} |
| 297 |
|
| 298 |
#if IS_LINUX |
| 299 |
int main(int argc, char** argv) |
| 300 |
{ |
| 301 |
#if HAS_SDL == 1 |
| 302 |
if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) |
| 303 |
{ |
| 304 |
wxLogFatalError(wxT_2("SDL_Init failed")); |
| 305 |
return 1; |
| 306 |
} |
| 307 |
#endif |
| 308 |
|
| 309 |
return wxEntry(argc, argv); |
| 310 |
} |
| 311 |
#endif |