summaryrefslogtreecommitdiff
path: root/src/tools/dcpomatic_player.cc
blob: fb09435047d7e753652072b67c3b3dfb4248f4bf (plain)
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
/*
    Copyright (C) 2017-2021 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic 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.

    DCP-o-matic 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 DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/


#include "wx/i18n_setup.h"
#include "wx/id.h"
#include "wx/player_frame.h"
#include "wx/wx_signal_manager.h"
#include "wx/wx_util.h"
#include "wx/wx_variant.h"
#include "lib/constants.h"
#include "lib/cross.h"
#include "lib/film.h"
#include "lib/update_checker.h"
#include <dcp/dcp.h>
#include <dcp/exceptions.h>
#include <dcp/filesystem.h>
#include <dcp/warnings.h>
LIBDCP_DISABLE_WARNINGS
#include <wx/cmdline.h>
#include <wx/progdlg.h>
#include <wx/splash.h>
#include <wx/wx.h>
LIBDCP_ENABLE_WARNINGS
#ifdef __WXGTK__
#include <X11/Xlib.h>
#endif
#include <boost/bind/bind.hpp>
#include <iostream>

#ifdef check
#undef check
#endif


using std::cout;
using std::exception;
using std::list;
using std::string;
using boost::scoped_array;
#if BOOST_VERSION >= 106100
using namespace boost::placeholders;
#endif
using namespace dcpomatic;


static const wxCmdLineEntryDesc command_line_description[] = {
	{ wxCMD_LINE_PARAM, 0, 0, "DCP to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
	{ wxCMD_LINE_OPTION, "c", "config", "Directory containing config.xml", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
	{ wxCMD_LINE_OPTION, "s", "stress", "File containing description of stress test", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
	{ wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType(0), 0 }
};


/** @class App
 *  @brief The magic App class for wxWidgets.
 */
class App : public wxApp
{
public:
	App()
		: wxApp()
	{
#ifdef DCPOMATIC_LINUX
		XInitThreads();
#endif
	}

private:

	bool OnInit() override
	{
		wxSplashScreen* splash;
		try {
			wxInitAllImageHandlers();

			Config::FailedToLoad.connect(boost::bind(&App::config_failed_to_load, this));
			Config::Warning.connect(boost::bind(&App::config_warning, this, _1));

			splash = maybe_show_splash();

			SetAppName(variant::wx::dcpomatic_player());

			if (!wxApp::OnInit()) {
				return false;
			}

#ifdef DCPOMATIC_LINUX
			unsetenv("UBUNTU_MENUPROXY");
#endif

#ifdef DCPOMATIC_OSX
			make_foreground_application();
#endif

			dcpomatic_setup_path_encoding();

			/* Enable i18n; this will create a Config object
			   to look for a force-configured language.  This Config
			   object will be wrong, however, because dcpomatic_setup
			   hasn't yet been called and there aren't any filters etc.
			   set up yet.
			*/
			dcpomatic::wx::setup_i18n();

			/* Set things up, including filters etc.
			   which will now be internationalised correctly.
			*/
			dcpomatic_setup();

			/* Force the configuration to be re-loaded correctly next
			   time it is needed.
			*/
			Config::drop();

			signal_manager = new wxSignalManager(this);

			_frame = new dcpomatic::ui::PlayerFrame();
			SetTopWindow(_frame);
			_frame->Maximize();
			if (splash) {
				splash->Destroy();
				splash = nullptr;
			}
			_frame->Show();

			if (_dcp_to_load && dcp::filesystem::is_directory(*_dcp_to_load)) {
				try {
					_frame->load_dcp(*_dcp_to_load);
				} catch (exception& e) {
					error_dialog(nullptr, wxString::Format(_("Could not load DCP %s"), std_to_wx(_dcp_to_load->string())), std_to_wx(e.what()));
				}
			}

			if (_stress) {
				try {
					_frame->load_stress_script(*_stress);
				} catch (exception& e) {
					error_dialog(nullptr, wxString::Format(_("Could not load stress test file %s"), std_to_wx(*_stress)));
				}
			}

			Bind(wxEVT_IDLE, boost::bind(&App::idle, this));

			if (Config::instance()->check_for_updates()) {
				UpdateChecker::instance()->run();
			}
		}
		catch (exception& e)
		{
			if (splash) {
				splash->Destroy();
			}
			error_dialog(nullptr, variant::wx::insert_dcpomatic_player(_("%s could not start")), std_to_wx(e.what()));
		}

		return true;
	}

	void OnInitCmdLine(wxCmdLineParser& parser) override
	{
		parser.SetDesc(command_line_description);
		parser.SetSwitchChars(char_to_wx("-"));
	}

	bool OnCmdLineParsed(wxCmdLineParser& parser) override
	{
		if (parser.GetParamCount() > 0) {
			auto path = boost::filesystem::path(wx_to_std(parser.GetParam(0)));
			/* Go at most two directories higher looking for a DCP that contains the file
			 * that was passed in.
			 */
			for (int i = 0; i < 2; ++i) {
				if (dcp::filesystem::is_directory(path) && contains_assetmap(path)) {
					_dcp_to_load = path;
					break;
				}
				path = path.parent_path();
			}
		}

		wxString config;
		if (parser.Found(char_to_wx("c"), &config)) {
			Config::override_path = wx_to_std(config);
		}
		wxString stress;
		if (parser.Found(char_to_wx("s"), &stress)) {
			_stress = wx_to_std(stress);
		}

		return true;
	}

	void report_exception()
	{
		try {
			throw;
		} catch (FileError& e) {
			error_dialog(
				0,
				wxString::Format(
					_("An exception occurred: %s (%s)\n\n%s"),
					std_to_wx(e.what()),
					std_to_wx(e.file().string().c_str()),
					dcpomatic::wx::report_problem()
					)
				);
		} catch (exception& e) {
			error_dialog(
				0,
				wxString::Format(
					_("An exception occurred: %s\n\n%s"),
					std_to_wx(e.what()),
					dcpomatic::wx::report_problem()
					)
				);
		} catch (...) {
			error_dialog(nullptr, wxString::Format(_("An unknown exception occurred. %s"), dcpomatic::wx::report_problem()));
		}
	}

	/* An unhandled exception has occurred inside the main event loop */
	bool OnExceptionInMainLoop() override
	{
		report_exception();
		/* This will terminate the program */
		return false;
	}

	void OnUnhandledException() override
	{
		report_exception();
	}

	void idle()
	{
		signal_manager->ui_idle();
		if (_frame) {
			_frame->idle();
		}
	}

	void config_failed_to_load()
	{
		message_dialog(_frame, _("The existing configuration failed to load.  Default values will be used instead.  These may take a short time to create."));
	}

	void config_warning(string m)
	{
		message_dialog(_frame, std_to_wx(m));
	}

	dcpomatic::ui::PlayerFrame* _frame = nullptr;
	boost::optional<boost::filesystem::path> _dcp_to_load;
	boost::optional<string> _stress;
};

IMPLEMENT_APP(App)