2 Copyright (C) 2012-2020 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"
27 #include <dcp/raw_convert.h>
30 #include <libavformat/avio.h>
32 #include <boost/algorithm/string.hpp>
33 #include <boost/foreach.hpp>
34 #ifdef DCPOMATIC_LINUX
38 #ifdef DCPOMATIC_WINDOWS
46 #include <sys/sysctl.h>
47 #include <mach-o/dyld.h>
48 #include <IOKit/pwr_mgt/IOPMLib.h>
50 #ifdef DCPOMATIC_POSIX
51 #include <sys/types.h>
53 #include <netinet/in.h>
54 #include <arpa/inet.h>
69 using std::runtime_error;
70 using boost::shared_ptr;
71 using boost::optional;
73 /** @param s Number of seconds to sleep for */
75 dcpomatic_sleep_seconds (int s)
77 #ifdef DCPOMATIC_POSIX
80 #ifdef DCPOMATIC_WINDOWS
86 dcpomatic_sleep_milliseconds (int ms)
88 #ifdef DCPOMATIC_POSIX
91 #ifdef DCPOMATIC_WINDOWS
96 /** @return A string of CPU information (model name etc.) */
102 #ifdef DCPOMATIC_LINUX
103 /* This use of ifstream is ok; the filename can never
106 ifstream f ("/proc/cpuinfo");
110 if (boost::algorithm::starts_with (l, "model name")) {
111 string::size_type const c = l.find (':');
112 if (c != string::npos) {
113 info = l.substr (c + 2);
121 size_t N = sizeof (buffer);
122 if (sysctlbyname ("machdep.cpu.brand_string", buffer, &N, 0, 0) == 0) {
127 #ifdef DCPOMATIC_WINDOWS
129 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &key) != ERROR_SUCCESS) {
135 if (RegQueryValueEx (key, L"ProcessorNameString", 0, &type, 0, &data) != ERROR_SUCCESS) {
139 if (type != REG_SZ) {
143 wstring value (data / sizeof (wchar_t), L'\0');
144 if (RegQueryValueEx (key, L"ProcessorNameString", 0, 0, reinterpret_cast<LPBYTE> (&value[0]), &data) != ERROR_SUCCESS) {
149 info = string (value.begin(), value.end());
159 /** @return Path of the Contents directory in the .app */
160 boost::filesystem::path
163 uint32_t size = 1024;
165 if (_NSGetExecutablePath (buffer, &size)) {
166 throw runtime_error ("_NSGetExecutablePath failed");
169 boost::filesystem::path path (buffer);
170 path = boost::filesystem::canonical (path);
171 path = path.parent_path ();
172 path = path.parent_path ();
177 boost::filesystem::path
180 #ifdef DCPOMATIC_LINUX
181 char const * p = getenv ("DCPOMATIC_LINUX_SHARE_PREFIX");
185 return boost::filesystem::canonical (LINUX_SHARE_PREFIX);
187 #ifdef DCPOMATIC_WINDOWS
189 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
190 PathRemoveFileSpec (dir);
191 boost::filesystem::path path = dir;
192 return path.parent_path();
195 return app_contents() / "Resources";
200 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out)
202 #ifdef DCPOMATIC_WINDOWS
203 SECURITY_ATTRIBUTES security;
204 security.nLength = sizeof (security);
205 security.bInheritHandle = TRUE;
206 security.lpSecurityDescriptor = 0;
208 HANDLE child_stderr_read;
209 HANDLE child_stderr_write;
210 if (!CreatePipe (&child_stderr_read, &child_stderr_write, &security, 0)) {
211 LOG_ERROR_NC ("ffprobe call failed (could not CreatePipe)");
216 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
217 PathRemoveFileSpec (dir);
218 SetCurrentDirectory (dir);
220 STARTUPINFO startup_info;
221 ZeroMemory (&startup_info, sizeof (startup_info));
222 startup_info.cb = sizeof (startup_info);
223 startup_info.hStdError = child_stderr_write;
224 startup_info.dwFlags |= STARTF_USESTDHANDLES;
226 wchar_t command[512];
227 wcscpy (command, L"ffprobe.exe \"");
230 MultiByteToWideChar (CP_UTF8, 0, content.string().c_str(), -1, file, sizeof(file));
231 wcscat (command, file);
233 wcscat (command, L"\"");
235 PROCESS_INFORMATION process_info;
236 ZeroMemory (&process_info, sizeof (process_info));
237 if (!CreateProcess (0, command, 0, 0, TRUE, CREATE_NO_WINDOW, 0, 0, &startup_info, &process_info)) {
238 LOG_ERROR_NC (N_("ffprobe call failed (could not CreateProcess)"));
242 FILE* o = fopen_boost (out, "w");
244 LOG_ERROR_NC (N_("ffprobe call failed (could not create output file)"));
248 CloseHandle (child_stderr_write);
253 if (!ReadFile(child_stderr_read, buffer, sizeof(buffer), &read, 0) || read == 0) {
256 fwrite (buffer, read, 1, o);
261 WaitForSingleObject (process_info.hProcess, INFINITE);
262 CloseHandle (process_info.hProcess);
263 CloseHandle (process_info.hThread);
264 CloseHandle (child_stderr_read);
267 #ifdef DCPOMATIC_LINUX
268 string ffprobe = "ffprobe \"" + content.string() + "\" 2> \"" + out.string() + "\"";
269 LOG_GENERAL (N_("Probing with %1"), ffprobe);
270 system (ffprobe.c_str ());
274 boost::filesystem::path path = app_contents();
278 string ffprobe = "\"" + path.string() + "\" \"" + content.string() + "\" 2> \"" + out.string() + "\"";
279 LOG_GENERAL (N_("Probing with %1"), ffprobe);
280 system (ffprobe.c_str ());
284 list<pair<string, string> >
287 list<pair<string, string> > m;
289 #ifdef DCPOMATIC_LINUX
290 FILE* f = setmntent ("/etc/mtab", "r");
296 struct mntent* mnt = getmntent (f);
301 m.push_back (make_pair (mnt->mnt_dir, mnt->mnt_type));
310 boost::filesystem::path
313 #ifdef DCPOMATIC_WINDOWS
315 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
316 PathRemoveFileSpec (dir);
318 boost::filesystem::path path = dir;
319 path /= "openssl.exe";
324 boost::filesystem::path path = app_contents();
330 #ifdef DCPOMATIC_LINUX
331 return "dcpomatic2_openssl";
336 /* Apparently there is no way to create an ofstream using a UTF-8
337 filename under Windows. We are hence reduced to using fopen
341 fopen_boost (boost::filesystem::path p, string t)
343 #ifdef DCPOMATIC_WINDOWS
344 wstring w (t.begin(), t.end());
345 /* c_str() here should give a UTF-16 string */
346 return _wfopen (p.c_str(), w.c_str ());
348 return fopen (p.c_str(), t.c_str ());
353 dcpomatic_fseek (FILE* stream, int64_t offset, int whence)
355 #ifdef DCPOMATIC_WINDOWS
356 return _fseeki64 (stream, offset, whence);
358 return fseek (stream, offset, whence);
365 #ifdef DCPOMATIC_WINDOWS
366 boost::mutex::scoped_lock lm (_mutex);
367 SetThreadExecutionState (ES_SYSTEM_REQUIRED);
374 boost::mutex::scoped_lock lm (_mutex);
375 /* We should use this */
376 // IOPMAssertionCreateWithName (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, CFSTR ("Encoding DCP"), &_assertion_id);
377 /* but it's not available on 10.5, so we use this */
378 IOPMAssertionCreate (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, &_assertion_id);
385 boost::mutex::scoped_lock lm (_mutex);
386 IOPMAssertionRelease (_assertion_id);
391 start_tool (boost::filesystem::path dcpomatic, string executable,
399 #if defined(DCPOMATIC_LINUX) || defined(DCPOMATIC_WINDOWS)
400 boost::filesystem::path batch = dcpomatic.parent_path() / executable;
404 boost::filesystem::path batch = dcpomatic.parent_path ();
405 batch = batch.parent_path (); // MacOS
406 batch = batch.parent_path (); // Contents
407 batch = batch.parent_path (); // DCP-o-matic.app
408 batch = batch.parent_path (); // Applications
415 #if defined(DCPOMATIC_LINUX) || defined(DCPOMATIC_OSX)
418 int const r = system (batch.string().c_str());
419 exit (WEXITSTATUS (r));
423 #ifdef DCPOMATIC_WINDOWS
424 STARTUPINFO startup_info;
425 ZeroMemory (&startup_info, sizeof (startup_info));
426 startup_info.cb = sizeof (startup_info);
428 PROCESS_INFORMATION process_info;
429 ZeroMemory (&process_info, sizeof (process_info));
432 MultiByteToWideChar (CP_UTF8, 0, batch.string().c_str(), -1, cmd, sizeof(cmd));
433 CreateProcess (0, cmd, 0, 0, FALSE, 0, 0, 0, &startup_info, &process_info);
438 start_batch_converter (boost::filesystem::path dcpomatic)
440 start_tool (dcpomatic, "dcpomatic2_batch", "DCP-o-matic\\ 2\\ Batch\\ Converter.app");
444 start_player (boost::filesystem::path dcpomatic)
446 start_tool (dcpomatic, "dcpomatic2_player", "DCP-o-matic\\ 2\\ Player.app");
452 #ifdef DCPOMATIC_WINDOWS
453 return (uint64_t) GetCurrentThreadId ();
455 return (uint64_t) pthread_self ();
460 avio_open_boost (AVIOContext** s, boost::filesystem::path file, int flags)
462 #ifdef DCPOMATIC_WINDOWS
463 int const length = (file.string().length() + 1) * 2;
464 char* utf8 = new char[length];
465 WideCharToMultiByte (CP_UTF8, 0, file.c_str(), -1, utf8, length, 0, 0);
466 int const r = avio_open (s, utf8, flags);
470 return avio_open (s, file.c_str(), flags);
474 #ifdef DCPOMATIC_WINDOWS
476 maybe_open_console ()
478 if (Config::instance()->win32_console ()) {
481 HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
482 int hCrt = _open_osfhandle((intptr_t) handle_out, _O_TEXT);
483 FILE* hf_out = _fdopen(hCrt, "w");
484 setvbuf(hf_out, NULL, _IONBF, 1);
487 HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
488 hCrt = _open_osfhandle((intptr_t) handle_in, _O_TEXT);
489 FILE* hf_in = _fdopen(hCrt, "r");
490 setvbuf(hf_in, NULL, _IONBF, 128);
496 boost::filesystem::path
499 #if defined(DCPOMATIC_LINUX) || defined(DCPOMATIC_OSX)
500 return getenv("HOME");
502 #ifdef DCPOMATIC_WINDOWS
503 return boost::filesystem::path(getenv("HOMEDRIVE")) / boost::filesystem::path(getenv("HOMEPATH"));
508 command_and_read (string cmd)
510 #ifdef DCPOMATIC_LINUX
511 FILE* pipe = popen (cmd.c_str(), "r");
513 throw runtime_error ("popen failed");
519 while (fgets(buffer, sizeof(buffer), pipe)) {
534 /** @return true if this process is a 32-bit one running on a 64-bit-capable OS */
538 #ifdef DCPOMATIC_WINDOWS
540 IsWow64Process (GetCurrentProcess(), &p);
543 /* XXX: assuming nobody does this on Linux / OS X */
550 vector<Drive> drives;
551 #ifdef DCPOMATIC_LINUX
553 using namespace boost::filesystem;
554 list<string> mounted_devices;
555 std::ifstream f("/proc/mounts");
560 boost::algorithm::split (bits, line, boost::is_any_of(" "));
561 if (bits.size() > 0 && boost::algorithm::starts_with(bits[0], "/dev/")) {
562 mounted_devices.push_back(bits[0]);
563 LOG_DIST("Mounted device %1", bits[0]);
567 for (directory_iterator i = directory_iterator("/sys/block"); i != directory_iterator(); ++i) {
568 string const name = i->path().filename().string();
569 path device_type_file("/sys/block/" + name + "/device/type");
570 optional<string> device_type;
571 if (exists(device_type_file)) {
572 device_type = dcp::file_to_string (device_type_file);
573 boost::trim(*device_type);
575 /* Device type 5 is "SCSI_TYPE_ROM" in blkdev.h; seems usually to be a CD/DVD drive */
576 if (!boost::algorithm::starts_with(name, "loop") && (!device_type || *device_type != "5")) {
577 uint64_t const size = dcp::raw_convert<uint64_t>(dcp::file_to_string(*i / "size")) * 512;
581 bool mounted = false;
582 optional<string> vendor;
584 vendor = dcp::file_to_string("/sys/block/" + name + "/device/vendor");
585 boost::trim(*vendor);
587 optional<string> model;
589 model = dcp::file_to_string("/sys/block/" + name + "/device/model");
592 BOOST_FOREACH (string j, mounted_devices) {
593 if (boost::algorithm::starts_with(j, "/dev/" + name)) {
597 drives.push_back(Drive(i->path().filename().string(), size, mounted, vendor, model));
598 LOG_DIST("Block device %1 size %2 %3 vendor %4 model %5", name, size, mounted ? "mounted" : "not mounted", vendor.get_value_or("[none]"), model.get_value_or("[none]"));
606 Drive::description () const
609 snprintf(gb, 64, "%.1f", _size / 1000000000.0);
616 if (name.size() > 0) {
617 name += " " + *_model;
620 if (name.size() == 0) {
624 return String::compose("%1 (%2 GB) [%3]", name, gb, _internal_name);
627 #ifdef DCPOMATIC_LINUX
631 uid_t ruid, euid, suid;
632 if (getresuid(&ruid, &euid, &suid) == -1) {
633 cerr << "getresuid() failed.\n";
639 PrivilegeEscalator::~PrivilegeEscalator ()
644 PrivilegeEscalator::PrivilegeEscalator ()
650 boost::filesystem::path
653 boost::filesystem::path p;
655 p /= g_get_home_dir ();
658 p /= "com.dcpomatic";
661 p /= g_get_user_config_dir ();