File: DLLInterface.cpp

package info (click to toggle)
0ad 0.0.17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 51,248 kB
  • ctags: 46,933
  • sloc: cpp: 223,208; ansic: 31,240; python: 16,343; perl: 4,083; sh: 1,011; makefile: 915; xml: 733; java: 621; ruby: 229; erlang: 53; sql: 40
file content (367 lines) | stat: -rw-r--r-- 9,386 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* Copyright (C) 2012 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. 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.
 *
 * 0 A.D. 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 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "precompiled.h"

#include "DLLInterface.h"

#include "General/AtlasEventLoop.h"

#include "General/Datafile.h"

#include "ActorEditor/ActorEditor.h"
#include "ScenarioEditor/ScenarioEditor.h"
#include "ErrorReporter/ErrorReporter.h"

#include "GameInterface/MessagePasser.h"

#include "wx/config.h"
#include "wx/debugrpt.h"
#include "wx/file.h"

// wx and libxml both want to define ATTRIBUTE_PRINTF (with similar
// meanings), so undef it to avoid a warning
#undef ATTRIBUTE_PRINTF
#include <libxml/parser.h>

#ifndef LIBXML_THREAD_ENABLED
#error libxml2 must have threading support enabled
#endif

#ifdef __WXGTK__
#include <X11/Xlib.h>
#endif

// If enabled, we'll try to use wxDebugReport to report fatal exceptions.
// But this is broken on Linux and can cause the UI to deadlock (see comment
// in OnFatalException), and it's never especially useful, so don't use it.
#define USE_WX_FATAL_EXCEPTION_REPORT 0

// Shared memory allocation functions
ATLASDLLIMPEXP void* ShareableMalloc(size_t n)
{
	// TODO: make sure this is thread-safe everywhere. (It is in MSVC with the
	// multithreaded CRT.)
	return malloc(n);
}
ATLASDLLIMPEXP void ShareableFree(void* p)
{
	free(p);
}
// Define the function pointers that we'll use when calling those functions.
// (The game loads the addresses of the above functions, then does the same.)
namespace AtlasMessage
{
	void* (*ShareableMallocFptr) (size_t) = &ShareableMalloc;
	void (*ShareableFreeFptr) (void*) = &ShareableFree;
}


// Global variables, to remember state between DllMain and StartWindow and OnInit
wxString g_InitialWindowType;
bool g_IsLoaded = false;

#ifdef __WXMSW__
HINSTANCE g_Module;

BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD fdwReason, LPVOID WXUNUSED(lpReserved))
{
	switch (fdwReason)
	{
	case DLL_PROCESS_ATTACH:
		g_Module = hModule;
		return TRUE;

	case DLL_PROCESS_DETACH:
		if (g_IsLoaded)
		{
			wxEntryCleanup();
			g_IsLoaded = false;
		}
		break;
	}

	return TRUE;
}
#endif // __WXMSW__

using namespace AtlasMessage;

MessagePasser* AtlasMessage::g_MessagePasser = NULL;

ATLASDLLIMPEXP void Atlas_SetMessagePasser(MessagePasser* passer)
{
	g_MessagePasser = passer;
}

bool g_HasSetDataDirectory = false;
ATLASDLLIMPEXP void Atlas_SetDataDirectory(const wchar_t* path)
{
	Datafile::SetDataDirectory(path);
	g_HasSetDataDirectory = true;
}

wxString g_ConfigDir;
ATLASDLLIMPEXP void Atlas_SetConfigDirectory(const wchar_t* path)
{
	wxFileName config (path);
	g_ConfigDir = config.GetPath(wxPATH_GET_SEPARATOR);
}

ATLASDLLIMPEXP void Atlas_StartWindow(const wchar_t* type)
{
	// Initialise libxml2
	// (If we're executed from the game instead, it has the responsibility to initialise libxml2)
	LIBXML_TEST_VERSION
	
	g_InitialWindowType = type;
#ifdef __WXMSW__
	wxEntry(g_Module);
#else
#ifdef __WXGTK__
	// Because we do GL calls from a secondary thread, Xlib needs to
	// be told to support multiple threads safely
	int status = XInitThreads();
	if (status == 0)
	{
		fprintf(stderr, "Error enabling thread-safety via XInitThreads\n");
	}
#endif
	int argc = 1;
	char atlas[] = "atlas";
	char *argv[] = {atlas, NULL};
#ifndef __WXOSX__
	wxEntry(argc, argv);
#else
	// Fix for OS X init (see http://trac.wildfiregames.com/ticket/2427 )
	// If we launched from in-game, SDL started NSApplication which will
	// break some things in wxWidgets
	wxEntryStart(argc, argv);
	wxTheApp->OnInit();
	wxTheApp->OnRun();
	wxTheApp->OnExit();
	wxEntryCleanup();
#endif

#endif
}

ATLASDLLIMPEXP void Atlas_DisplayError(const wchar_t* text, size_t WXUNUSED(flags))
{
	// This is called from the game thread.
	// wxLog appears to be thread-safe, so that's okay.
	wxLogError(L"%s", text);

	// TODO: wait for user response (if possible) before returning,
	// and return their status (break/continue/debug/etc), but only in
	// cases where we're certain it won't deadlock (i.e. the UI event loop
	// is still running and won't block before showing the dialog to the user)
	// and where it matters (i.e. errors, not warnings (unless they're going to
	// turn into errors after continuing))

	// TODO: 'text' (or at least some copy of it) appears to get leaked when
	// this function is called
}


ATLASDLLIMPEXP void Atlas_ReportError()
{
	///ReportError();	// janwas: disabled until ErrorReporter.cpp compiles
}

class AtlasDLLApp : public wxApp
{
public:

#ifdef __WXOSX__
	virtual bool OSXIsGUIApplication()
	{
		return false;
	}
#endif

	virtual bool OnInit()
	{
// 		_CrtSetBreakAlloc(5632);

#if wxUSE_ON_FATAL_EXCEPTION && USE_WX_FATAL_EXCEPTION_REPORT
		if (! wxIsDebuggerRunning())
			wxHandleFatalExceptions();
#endif

#ifndef __WXMSW__ // On Windows we use the registry so don't attempt to set the path.
		// When launching a standalone executable g_ConfigDir may not be
		// set. In this case we default to the XDG base dir spec and use
		// 0ad/config/ as the config directory.
		wxString configPath;
		if (!g_ConfigDir.IsEmpty())
		{
			configPath = g_ConfigDir;
		}
		else
		{
			wxString xdgConfigHome;
			if (wxGetEnv(_T("XDG_CONFIG_HOME"), &xdgConfigHome) && !xdgConfigHome.IsEmpty())
				configPath = xdgConfigHome + _T("/0ad/config/");
			else
				configPath = wxFileName::GetHomeDir() + _T("/.config/0ad/config/");
		}
#endif

		// Initialise the global config file
		wxConfigBase::Set(new wxConfig(_T("Atlas Editor"), _T("Wildfire Games")
#ifndef __WXMSW__ // On Windows we use wxRegConfig and setting this changes the Registry key
			, configPath + _T("atlas.ini")
#endif
			));

		if (! g_HasSetDataDirectory)
		{
			// Assume that the .exe is located in .../binaries/system. (We can't
			// just use the cwd, since that isn't correct when being executed by
			// dragging-and-dropping onto the program in Explorer.)
			Datafile::SetSystemDirectory(argv[0]);
		}

		// Display the appropriate window
		wxFrame* frame;
		if (g_InitialWindowType == _T("ActorEditor"))
		{
			frame = new ActorEditor(NULL);
		}
		else if (g_InitialWindowType == _T("ScenarioEditor"))
		{
			frame = new ScenarioEditor(NULL);
		}
		else
		{
			wxFAIL_MSG(_("Internal error: invalid window type"));
			return false;
		}

		frame->Show();
		SetTopWindow(frame);

		AtlasWindow* win = wxDynamicCast(frame, AtlasWindow);
		if (win)
		{
			// One argument => argv[1] is probably a filename to open
			if (argc > 1)
			{
				wxString filename = argv[1];

				if (filename[0] != _T('-')) // ignore -options
				{
					if (wxFile::Exists(filename))
					{
						win->OpenFile(filename);
					}
					else
						wxLogError(_("Cannot find file '%s'"), filename.c_str());
				}
			}
		}

		return true;
	}

#if wxUSE_DEBUGREPORT && USE_WX_FATAL_EXCEPTION_REPORT
	virtual void OnFatalException()
	{
		// NOTE: At least on Linux, this might be called from a thread other
		// than the UI thread, so it's not safe to use any wx objects here

		wxDebugReport report;
		wxDebugReportPreviewStd preview;

		report.AddAll();

		if (preview.Show(report))
		{
			wxString dir = report.GetDirectory(); // save the string, since it gets cleared by Process
			report.Process();
			OpenDirectory(dir);
		}
	}
#endif // wxUSE_DEBUGREPORT

/* Disabled (and should be removed if it turns out to be unnecessary)
- see MessagePasserImpl.cpp for information
	virtual int MainLoop()
	{
		// Override the default MainLoop so that we can provide our own event loop

		wxEventLoop* old = m_mainLoop;
		m_mainLoop = new AtlasEventLoop;

		int ret = m_mainLoop->Run();

		delete m_mainLoop;
		m_mainLoop = old;
		return ret;
	}
*/

private:

	bool OpenDirectory(const wxString& dir)
	{
		// Open a directory on the filesystem - used so people can find the
		// debug report files generated in OnFatalException easily

#ifdef __WXMSW__
		// Code largely copied from wxLaunchDefaultBrowser:

		typedef HINSTANCE (WINAPI *LPShellExecute)(HWND hwnd, const wxChar* lpOperation,
			const wxChar* lpFile,
			const wxChar* lpParameters,
			const wxChar* lpDirectory,
			INT nShowCmd);

		HINSTANCE hShellDll = ::LoadLibrary(_T("shell32.dll"));
		if (hShellDll == NULL)
			return false;

		LPShellExecute lpShellExecute =
			(LPShellExecute) ::GetProcAddress(hShellDll,
			wxString(_T("ShellExecute")
# ifdef _UNICODE
			_T("W")
# else
			_T("A")
# endif
			).mb_str(wxConvLocal));

		if (lpShellExecute == NULL)
			return false;

		/*HINSTANCE nResult =*/ (*lpShellExecute)(NULL, _T("explore"), dir.c_str(), NULL, NULL, SW_SHOWNORMAL);
		// ignore return value, since we're not going to do anything if this fails

		::FreeLibrary(hShellDll);

		return true;
#else
		// Figure out what goes for "default browser" on unix/linux/whatever
		// open an xterm perhaps? :)
		(void)dir;
		return false;
#endif
	}
};

IMPLEMENT_APP_NO_MAIN(AtlasDLLApp);