2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <boost/algorithm/string.hpp>
23 #include "compose.hpp"
25 #ifdef DCPOMATIC_LINUX
29 #ifdef DCPOMATIC_WINDOWS
35 #include <sys/sysctl.h>
36 #include <mach-o/dyld.h>
38 #include "exceptions.h"
46 using boost::shared_ptr;
49 dcpomatic_sleep (int s)
51 #ifdef DCPOMATIC_POSIX
54 #ifdef DCPOMATIC_WINDOWS
59 /** @return A string of CPU information (model name etc.) */
65 #ifdef DCPOMATIC_LINUX
66 ifstream f ("/proc/cpuinfo");
70 if (boost::algorithm::starts_with (l, "model name")) {
71 string::size_type const c = l.find (':');
72 if (c != string::npos) {
73 info = l.substr (c + 2);
81 size_t N = sizeof (buffer);
82 if (sysctlbyname ("machdep.cpu.brand_string", buffer, &N, 0, 0) == 0) {
87 #ifdef DCPOMATIC_WINDOWS
89 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &key) != ERROR_SUCCESS) {
95 if (RegQueryValueEx (key, L"ProcessorNameString", 0, &type, 0, &data) != ERROR_SUCCESS) {
103 wstring value (data / sizeof (wchar_t), L'\0');
104 if (RegQueryValueEx (key, L"ProcessorNameString", 0, 0, reinterpret_cast<LPBYTE> (&value[0]), &data) != ERROR_SUCCESS) {
109 info = string (value.begin(), value.end());
119 /** @return Path of the Contents directory in the .app */
120 boost::filesystem::path
123 uint32_t size = 1024;
125 if (_NSGetExecutablePath (buffer, &size)) {
126 throw StringError ("_NSGetExecutablePath failed");
129 boost::filesystem::path path (buffer);
130 path = boost::filesystem::canonical (path);
131 std::cout << "start " << path << "\n";
132 path = path.parent_path ();
133 std::cout << "then " << path << "\n";
134 path = path.parent_path ();
135 std::cout << "and then " << path << "\n";
141 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out, shared_ptr<Log> log)
143 #ifdef DCPOMATIC_WINDOWS
144 SECURITY_ATTRIBUTES security;
145 security.nLength = sizeof (security);
146 security.bInheritHandle = TRUE;
147 security.lpSecurityDescriptor = 0;
149 HANDLE child_stderr_read;
150 HANDLE child_stderr_write;
151 if (!CreatePipe (&child_stderr_read, &child_stderr_write, &security, 0)) {
152 log->log ("ffprobe call failed (could not CreatePipe)");
157 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
158 PathRemoveFileSpec (dir);
159 SetCurrentDirectory (dir);
161 STARTUPINFO startup_info;
162 ZeroMemory (&startup_info, sizeof (startup_info));
163 startup_info.cb = sizeof (startup_info);
164 startup_info.hStdError = child_stderr_write;
165 startup_info.dwFlags |= STARTF_USESTDHANDLES;
167 wchar_t command[512];
168 wcscpy (command, L"ffprobe.exe \"");
171 MultiByteToWideChar (CP_UTF8, 0, content.string().c_str(), -1, file, sizeof(file));
172 wcscat (command, file);
174 wcscat (command, L"\"");
176 PROCESS_INFORMATION process_info;
177 ZeroMemory (&process_info, sizeof (process_info));
178 if (!CreateProcess (0, command, 0, 0, TRUE, CREATE_NO_WINDOW, 0, 0, &startup_info, &process_info)) {
179 log->log ("ffprobe call failed (could not CreateProcess)");
183 FILE* o = fopen (out.string().c_str(), "w");
185 log->log ("ffprobe call failed (could not create output file)");
189 CloseHandle (child_stderr_write);
194 if (!ReadFile(child_stderr_read, buffer, sizeof(buffer), &read, 0) || read == 0) {
197 fwrite (buffer, read, 1, o);
202 WaitForSingleObject (process_info.hProcess, INFINITE);
203 CloseHandle (process_info.hProcess);
204 CloseHandle (process_info.hThread);
205 CloseHandle (child_stderr_read);
208 #ifdef DCPOMATIC_LINUX
209 string ffprobe = "ffprobe \"" + content.string() + "\" 2> \"" + out.string() + "\"";
210 log->log (String::compose ("Probing with %1", ffprobe));
211 system (ffprobe.c_str ());
215 boost::filesystem::path path = app_contents();
219 string ffprobe = path.string() + " \"" + content.string() + "\" 2> \"" + out.string() + "\"";
220 log->log (String::compose ("Probing with %1", ffprobe));
221 system (ffprobe.c_str ());
225 list<pair<string, string> >
228 list<pair<string, string> > m;
230 #ifdef DCPOMATIC_LINUX
231 FILE* f = setmntent ("/etc/mtab", "r");
237 struct mntent* mnt = getmntent (f);
242 m.push_back (make_pair (mnt->mnt_dir, mnt->mnt_type));
251 boost::filesystem::path
254 #ifdef DCPOMATIC_WINDOWS
256 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
257 PathRemoveFileSpec (dir);
259 boost::filesystem::path path = dir;
260 path /= "openssl.exe";
263 /* We assume that it's on the path for Linux and OS X */