File: wxLauncherApp.cpp

package info (click to toggle)
freespace2-launcher-wxlauncher 0.11.0%2Bdfsg-3
  • links: PTS, VCS
  • area: contrib
  • in suites: bullseye
  • size: 2,368 kB
  • sloc: cpp: 13,446; python: 797; makefile: 12; sh: 12
file content (310 lines) | stat: -rw-r--r-- 8,947 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
Copyright (C) 2009-2010 wxLauncher Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "generated/configure_launcher.h"
#include <wx/wx.h>
#include <wx/cmdline.h>
#include <wx/image.h>
#include <wx/filesys.h>
#include <wx/fs_arc.h>
#include <wx/fs_inet.h>
#include <wx/splash.h>

#if HAS_SDL == 1
#include "SDL.h"
#endif

// prevents SDL_main.h (included by SDL.h) from redefining main
#ifdef main
#undef main
#endif

#include "wxLauncherApp.h"
#include "MainWindow.h"
#include "apis/SkinManager.h"
#include "controls/Logger.h"
#include "global/version.h"
#include "apis/TCManager.h"
#include "apis/ProfileManager.h"
#include "apis/HelpManager.h"
#include "apis/FlagListManager.h"
#include "apis/ProfileProxy.h"

#include "global/MemoryDebugging.h" // Last include for memory debugging

#ifndef WIN32
// main needs to be handled by us as SDL interferes with wxWidgets
IMPLEMENT_APP_NO_MAIN(wxLauncher);
#else
// Windows is fine and also needs special WinMain treatment
IMPLEMENT_APP(wxLauncher);
#endif

void wxLauncher::OnInitCmdLine(wxCmdLineParser& parser)
{
	static const char addprofiledesc[] =
		"Add profile PROFILE from FILE. If PROFILE "
		"already exists it will not be overwritten. *Operator*";
	static const char selectprofiledesc[] =
		"Make PROFILE the that wxLauncher will use "
		"on next run. *Operator*";
	static const char profiledesc[] =
		"The name of a profile to operate on. Operand PROFILE.";
	static const char filedesc[] =
		"The path to a file to operate on. Operand FILE.";
	static const char sessiononlydesc[] =
		"Do not remember the profile that is selected at exit";

	/* Operators */
	parser.AddSwitch(wxEmptyString, wxT_2("add-profile"),
		wxGetTranslation(wxString::FromUTF8(addprofiledesc)));
	parser.AddSwitch(wxEmptyString, wxT_2("select-profile"),
		wxGetTranslation(wxString::FromUTF8(selectprofiledesc)));

	/* Operands */
	parser.AddOption(wxEmptyString, wxT_2("profile"),
		wxGetTranslation(wxString::FromUTF8(profiledesc)),
		wxCMD_LINE_VAL_STRING);
	parser.AddOption(wxEmptyString, wxT_2("file"),
		wxGetTranslation(wxString::FromUTF8(filedesc)),
		wxCMD_LINE_VAL_STRING);

	/* Other */
	parser.AddSwitch(wxEmptyString, wxT_2("session-only"),
		wxGetTranslation(wxString::FromUTF8(sessiononlydesc)));

	parser.SetSwitchChars(wxT_2("-")); // always use -, even on windows

	wxApp::OnInitCmdLine(parser);
}

bool wxLauncher::OnCmdLineParsed(wxCmdLineParser& parser)
{
	if (!wxApp::OnCmdLineParsed(parser))
		return false;

	if (parser.Found(wxT_2("session-only")))
	{
		mKeepForSessionOnly = true;
	}
	
	if (parser.Found(wxT_2("add-profile")))
	{
		mProfileOperator = ProManOperator::add;
		if (!parser.Found(wxT_2("profile"), &mProfileOperand))
		{
			wxLogError(_("No profile specified to add"));
			return false;
		}
		if (!parser.Found(wxT_2("file"), &mFileOperand))
		{
			wxLogError(_("No file specified to add as profile"));
			return false;
		}
	}
	else if(parser.Found(wxT_2("select-profile")))
	{
		mProfileOperator = ProManOperator::select;
		if (!parser.Found(wxT_2("profile"), &mProfileOperand))
		{
			wxLogError(_("No profile specified to select"));
			return false;
		}
	}

	return true;
}

wxLauncher::wxLauncher()
	:mProfileOperator(ProManOperator::none),
	mKeepForSessionOnly(false),
	mShowGUI(false)
	// The strings init themselves sanely
{
}

static char* DISPLAY_SPLASH_FAIL_TEXT = 
	"Unable to load splash image."
	" This normally means that you are running the Launcher from a folder"
	" that the launcher does not know how to find the resource folder from."
	"\n\nThe launcher is expecting (%s) to contain the resource images.";

/** Display the splash screen.

\param splashWindow Out. Point to splash window if created. NULL otherwise.
\param returns false if an error occurented while loading spalsh; a
false return value indicates that this function has already informed the user
of the failue.  Returns true otherwise.
*/
bool displaySplash(wxSplashScreen **splashWindow)
{
	wxBitmap splash;
	// splash image location is fixed so that it's known at compile time
	wxFileName splashFile(wxT_2(RESOURCES_PATH), wxT_2("wxL_Splash.png"));
	if (splash.LoadFile(splashFile.GetFullPath(), wxBITMAP_TYPE_ANY)) {
#if NDEBUG
		(*splashWindow) = new wxSplashScreen(splash, wxSPLASH_CENTRE_ON_SCREEN, 0, NULL, wxID_ANY);
#else
		(*splashWindow) = NULL;//new wxSplashScreen(splash, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_TIMEOUT, 1000, NULL, wxID_ANY);
#endif
		wxYield();
	} else {
		wxFileName expectedDir;
		if (wxFileName(wxT_2(RESOURCES_PATH)).IsAbsolute()) {
			expectedDir = wxFileName(wxT_2(RESOURCES_PATH));
		} else {
			expectedDir = wxFileName(::wxGetCwd(), wxT_2(RESOURCES_PATH));	
		}
		wxString errmsg(DISPLAY_SPLASH_FAIL_TEXT, wxMBConvUTF8());
		wxLogFatalError(wxString::Format(wxGetTranslation(errmsg),
			expectedDir.GetFullPath().c_str()).c_str());
		return false;
	}
	return true;
}

int wxLauncher::OnRun() {
	if (mProfileOperator == ProManOperator::none)
	{
		return wxApp::OnRun();
	}
	else
	{
		return ProManOperator::RunProfileOperator(mProfileOperator);
	}
}

bool wxLauncher::OnInit() {
	wxInitAllImageHandlers();

	// call base class OnInit so that cmdline stuff works.
	if (!wxApp::OnInit())
		return false; // base said abort so abort

	wxLog::SetActiveTarget(new Logger());
	wxLogInfo(wxT_2("wxLauncher Version %d.%d.%d"), MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION);
	wxLogInfo(wxT_2("Build \"%s\" committed on (%s)"), GITVersion, GITDate);
	wxLogInfo(wxDateTime(time(NULL)).Format(wxT_2("%c")));

#if MSCRTMEMORY
	_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
	// Little hack to deal with people starting the launcher from the bin folder
	if ( !wxFileName::DirExists(wxT_2(RESOURCES_PATH)) ) {
		wxFileName resourceDir;
		resourceDir.AssignDir(::wxGetCwd());
		resourceDir.AppendDir(wxT_2(".."));
		resourceDir.AppendDir(wxT_2(RESOURCES_PATH));
		if ( resourceDir.DirExists() ) {
			wxFileName newWorkingDir(::wxGetCwd(), wxT_2(".."));
			::wxSetWorkingDirectory(newWorkingDir.GetFullPath());
		}
	}

	wxSplashScreen* splashWindow = NULL;
	if (!displaySplash(&splashWindow))
		return false;

	wxLogInfo(wxT_2("Initializing profiles..."));
	ProMan::Flags promanFlags = ProMan::None;
	if (mKeepForSessionOnly)
		promanFlags = promanFlags | ProMan::NoUpdateLastProfile;
	if ( !ProMan::Initialize(promanFlags) ) {
		wxLogFatalError(wxT_2("ProfileManager failed to initialize. Aborting! See log file for more details."));
		return false;
	}
	if (mProfileOperator != ProManOperator::none)
	{
		// We are not to create a GUI so we are done init now
		return true;
	}

	wxFileSystem::AddHandler(new wxArchiveFSHandler);
	wxFileSystem::AddHandler(new wxInternetFSHandler);

	wxLogInfo(wxT_2("Initializing SkinSystem..."));
	SkinSystem::Initialize();

	wxLogInfo(wxT_2("Initializing HelpManager..."));
	HelpManager::Initialize();
	
	wxLogInfo(wxT_2("Initializing FlagListManager..."));
	FlagListManager::Initialize();
	
	wxLogInfo(wxT_2("Initializing ProfileProxy..."));
	ProfileProxy::Initialize();

	wxLogInfo(wxT_2("wxLauncher starting up."));


	MainWindow* window = new MainWindow();
	wxLogStatus(_("MainWindow is complete"));
	window->Show(true);
#if NDEBUG // will autodelete when timout runs out in debug
	splashWindow->Show(false);
	splashWindow->Destroy();
#endif

	// must call TCManager::CurrentProfileChanged() manually on startup,
	// since initial profile switch takes place before TCManager has been initialized
	// calling it here to ensure that AdvSettingsPage is set up by the time events are triggered
	wxCommandEvent tcMgrInitEvent;
	TCManager::Get()->CurrentProfileChanged(tcMgrInitEvent);

	wxLogStatus(_("Ready."));
	return true;
}

int wxLauncher::OnExit() {

	ProMan::DeInitialize();

	if (mProfileOperator == ProManOperator::none)
	{

		// deinitialize subsystems in the opposite order of initialization
		ProfileProxy::DeInitialize();
		FlagListManager::DeInitialize();
		HelpManager::DeInitialize();
		SkinSystem::DeInitialize();

#if HAS_SDL == 1
		SDL_Quit();
#endif

	}

	wxLogInfo(_("wxLogger shutdown complete."));

	return wxApp::OnExit();
}

#ifndef WIN32
int main(int argc, char** argv)
{
#if HAS_SDL == 1
	if (SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		wxLogFatalError(wxT_2("SDL_Init failed"));
		return 1;
	}
#endif

	return wxEntry(argc, argv);
}
#endif