2 Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
22 #include "compose.hpp"
24 #include "dcpomatic_log.h"
26 #include "exceptions.h"
28 #include <libavformat/avio.h>
30 #include <boost/algorithm/string.hpp>
31 #ifdef DCPOMATIC_LINUX
35 #ifdef DCPOMATIC_WINDOWS
43 #include <sys/sysctl.h>
44 #include <mach-o/dyld.h>
45 #include <IOKit/pwr_mgt/IOPMLib.h>
47 #ifdef DCPOMATIC_POSIX
48 #include <sys/types.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
63 using std::runtime_error;
64 using boost::shared_ptr;
66 /** @param s Number of seconds to sleep for */
68 dcpomatic_sleep (int s)
70 #ifdef DCPOMATIC_POSIX
73 #ifdef DCPOMATIC_WINDOWS
78 /** @return A string of CPU information (model name etc.) */
84 #ifdef DCPOMATIC_LINUX
85 /* This use of ifstream is ok; the filename can never
88 ifstream f ("/proc/cpuinfo");
92 if (boost::algorithm::starts_with (l, "model name")) {
93 string::size_type const c = l.find (':');
94 if (c != string::npos) {
95 info = l.substr (c + 2);
103 size_t N = sizeof (buffer);
104 if (sysctlbyname ("machdep.cpu.brand_string", buffer, &N, 0, 0) == 0) {
109 #ifdef DCPOMATIC_WINDOWS
111 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &key) != ERROR_SUCCESS) {
117 if (RegQueryValueEx (key, L"ProcessorNameString", 0, &type, 0, &data) != ERROR_SUCCESS) {
121 if (type != REG_SZ) {
125 wstring value (data / sizeof (wchar_t), L'\0');
126 if (RegQueryValueEx (key, L"ProcessorNameString", 0, 0, reinterpret_cast<LPBYTE> (&value[0]), &data) != ERROR_SUCCESS) {
131 info = string (value.begin(), value.end());
141 /** @return Path of the Contents directory in the .app */
142 boost::filesystem::path
145 uint32_t size = 1024;
147 if (_NSGetExecutablePath (buffer, &size)) {
148 throw runtime_error ("_NSGetExecutablePath failed");
151 boost::filesystem::path path (buffer);
152 path = boost::filesystem::canonical (path);
153 path = path.parent_path ();
154 path = path.parent_path ();
159 boost::filesystem::path
162 #ifdef DCPOMATIC_LINUX
163 char const * p = getenv ("DCPOMATIC_LINUX_SHARE_PREFIX");
167 return boost::filesystem::canonical (LINUX_SHARE_PREFIX);
169 #ifdef DCPOMATIC_WINDOWS
171 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
172 PathRemoveFileSpec (dir);
173 boost::filesystem::path path = dir;
174 return path.parent_path();
177 return app_contents() / "Resources";
182 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out)
184 #ifdef DCPOMATIC_WINDOWS
185 SECURITY_ATTRIBUTES security;
186 security.nLength = sizeof (security);
187 security.bInheritHandle = TRUE;
188 security.lpSecurityDescriptor = 0;
190 HANDLE child_stderr_read;
191 HANDLE child_stderr_write;
192 if (!CreatePipe (&child_stderr_read, &child_stderr_write, &security, 0)) {
193 LOG_ERROR_NC ("ffprobe call failed (could not CreatePipe)");
198 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
199 PathRemoveFileSpec (dir);
200 SetCurrentDirectory (dir);
202 STARTUPINFO startup_info;
203 ZeroMemory (&startup_info, sizeof (startup_info));
204 startup_info.cb = sizeof (startup_info);
205 startup_info.hStdError = child_stderr_write;
206 startup_info.dwFlags |= STARTF_USESTDHANDLES;
208 wchar_t command[512];
209 wcscpy (command, L"ffprobe.exe \"");
212 MultiByteToWideChar (CP_UTF8, 0, content.string().c_str(), -1, file, sizeof(file));
213 wcscat (command, file);
215 wcscat (command, L"\"");
217 PROCESS_INFORMATION process_info;
218 ZeroMemory (&process_info, sizeof (process_info));
219 if (!CreateProcess (0, command, 0, 0, TRUE, CREATE_NO_WINDOW, 0, 0, &startup_info, &process_info)) {
220 LOG_ERROR_NC (N_("ffprobe call failed (could not CreateProcess)"));
224 FILE* o = fopen_boost (out, "w");
226 LOG_ERROR_NC (N_("ffprobe call failed (could not create output file)"));
230 CloseHandle (child_stderr_write);
235 if (!ReadFile(child_stderr_read, buffer, sizeof(buffer), &read, 0) || read == 0) {
238 fwrite (buffer, read, 1, o);
243 WaitForSingleObject (process_info.hProcess, INFINITE);
244 CloseHandle (process_info.hProcess);
245 CloseHandle (process_info.hThread);
246 CloseHandle (child_stderr_read);
249 #ifdef DCPOMATIC_LINUX
250 string ffprobe = "ffprobe \"" + content.string() + "\" 2> \"" + out.string() + "\"";
251 LOG_GENERAL (N_("Probing with %1"), ffprobe);
252 system (ffprobe.c_str ());
256 boost::filesystem::path path = app_contents();
260 string ffprobe = "\"" + path.string() + "\" \"" + content.string() + "\" 2> \"" + out.string() + "\"";
261 LOG_GENERAL (N_("Probing with %1"), ffprobe);
262 system (ffprobe.c_str ());
266 list<pair<string, string> >
269 list<pair<string, string> > m;
271 #ifdef DCPOMATIC_LINUX
272 FILE* f = setmntent ("/etc/mtab", "r");
278 struct mntent* mnt = getmntent (f);
283 m.push_back (make_pair (mnt->mnt_dir, mnt->mnt_type));
292 boost::filesystem::path
295 #ifdef DCPOMATIC_WINDOWS
297 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
298 PathRemoveFileSpec (dir);
300 boost::filesystem::path path = dir;
301 path /= "openssl.exe";
304 /* We assume that it's on the path for Linux and OS X */
310 /* Apparently there is no way to create an ofstream using a UTF-8
311 filename under Windows. We are hence reduced to using fopen
315 fopen_boost (boost::filesystem::path p, string t)
317 #ifdef DCPOMATIC_WINDOWS
318 wstring w (t.begin(), t.end());
319 /* c_str() here should give a UTF-16 string */
320 return _wfopen (p.c_str(), w.c_str ());
322 return fopen (p.c_str(), t.c_str ());
327 dcpomatic_fseek (FILE* stream, int64_t offset, int whence)
329 #ifdef DCPOMATIC_WINDOWS
330 return _fseeki64 (stream, offset, whence);
332 return fseek (stream, offset, whence);
339 #ifdef DCPOMATIC_WINDOWS
340 SetThreadExecutionState (ES_SYSTEM_REQUIRED);
347 /* We should use this */
348 // IOPMAssertionCreateWithName (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, CFSTR ("Encoding DCP"), &_assertion_id);
349 /* but it's not available on 10.5, so we use this */
350 IOPMAssertionCreate (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, &_assertion_id);
357 IOPMAssertionRelease (_assertion_id);
362 start_tool (boost::filesystem::path dcpomatic, string executable,
370 #if defined(DCPOMATIC_LINUX) || defined(DCPOMATIC_WINDOWS)
371 boost::filesystem::path batch = dcpomatic.parent_path() / executable;
375 boost::filesystem::path batch = dcpomatic.parent_path ();
376 batch = batch.parent_path (); // MacOS
377 batch = batch.parent_path (); // Contents
378 batch = batch.parent_path (); // DCP-o-matic.app
379 batch = batch.parent_path (); // Applications
386 #if defined(DCPOMATIC_LINUX) || defined(DCPOMATIC_OSX)
389 int const r = system (batch.string().c_str());
390 exit (WEXITSTATUS (r));
394 #ifdef DCPOMATIC_WINDOWS
395 STARTUPINFO startup_info;
396 ZeroMemory (&startup_info, sizeof (startup_info));
397 startup_info.cb = sizeof (startup_info);
399 PROCESS_INFORMATION process_info;
400 ZeroMemory (&process_info, sizeof (process_info));
403 MultiByteToWideChar (CP_UTF8, 0, batch.string().c_str(), -1, cmd, sizeof(cmd));
404 CreateProcess (0, cmd, 0, 0, FALSE, 0, 0, 0, &startup_info, &process_info);
409 start_batch_converter (boost::filesystem::path dcpomatic)
411 start_tool (dcpomatic, "dcpomatic2_batch", "DCP-o-matic\\ 2\\ Batch\\ Converter.app");
415 start_player (boost::filesystem::path dcpomatic)
417 start_tool (dcpomatic, "dcpomatic2_player", "DCP-o-matic\\ 2\\ Player.app");
423 #ifdef DCPOMATIC_WINDOWS
424 return (uint64_t) GetCurrentThreadId ();
426 return (uint64_t) pthread_self ();
431 avio_open_boost (AVIOContext** s, boost::filesystem::path file, int flags)
433 #ifdef DCPOMATIC_WINDOWS
434 int const length = (file.string().length() + 1) * 2;
435 char* utf8 = new char[length];
436 WideCharToMultiByte (CP_UTF8, 0, file.c_str(), -1, utf8, length, 0, 0);
437 int const r = avio_open (s, utf8, flags);
441 return avio_open (s, file.c_str(), flags);
445 #ifdef DCPOMATIC_WINDOWS
447 maybe_open_console ()
449 if (Config::instance()->win32_console ()) {
452 HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
453 int hCrt = _open_osfhandle((intptr_t) handle_out, _O_TEXT);
454 FILE* hf_out = _fdopen(hCrt, "w");
455 setvbuf(hf_out, NULL, _IONBF, 1);
458 HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
459 hCrt = _open_osfhandle((intptr_t) handle_in, _O_TEXT);
460 FILE* hf_in = _fdopen(hCrt, "r");
461 setvbuf(hf_in, NULL, _IONBF, 128);
467 boost::filesystem::path
470 #if defined(DCPOMATIC_LINUX) || defined(DCPOMATIC_OSX)
471 return getenv("HOME");
473 #ifdef DCPOMATIC_WINDOWS
474 return boost::filesystem::path(getenv("HOMEDRIVE")) / boost::filesystem::path(getenv("HOMEPATH"));
479 command_and_read (string cmd)
481 #ifdef DCPOMATIC_LINUX
482 FILE* pipe = popen (cmd.c_str(), "r");
484 throw runtime_error ("popen failed");
490 while (fgets(buffer, sizeof(buffer), pipe)) {