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 #ifdef DCPOMATIC_POSIX
39 #include <sys/types.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
44 #include "exceptions.h"
52 using boost::shared_ptr;
54 /** @param s Number of seconds to sleep for */
56 dcpomatic_sleep (int s)
58 #ifdef DCPOMATIC_POSIX
61 #ifdef DCPOMATIC_WINDOWS
66 /** @return A string of CPU information (model name etc.) */
72 #ifdef DCPOMATIC_LINUX
73 /* This use of ifstream is ok; the filename can never
76 ifstream f ("/proc/cpuinfo");
80 if (boost::algorithm::starts_with (l, "model name")) {
81 string::size_type const c = l.find (':');
82 if (c != string::npos) {
83 info = l.substr (c + 2);
91 size_t N = sizeof (buffer);
92 if (sysctlbyname ("machdep.cpu.brand_string", buffer, &N, 0, 0) == 0) {
97 #ifdef DCPOMATIC_WINDOWS
99 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &key) != ERROR_SUCCESS) {
105 if (RegQueryValueEx (key, L"ProcessorNameString", 0, &type, 0, &data) != ERROR_SUCCESS) {
109 if (type != REG_SZ) {
113 wstring value (data / sizeof (wchar_t), L'\0');
114 if (RegQueryValueEx (key, L"ProcessorNameString", 0, 0, reinterpret_cast<LPBYTE> (&value[0]), &data) != ERROR_SUCCESS) {
119 info = string (value.begin(), value.end());
129 /** @return Path of the Contents directory in the .app */
130 boost::filesystem::path
133 uint32_t size = 1024;
135 if (_NSGetExecutablePath (buffer, &size)) {
136 throw StringError ("_NSGetExecutablePath failed");
139 boost::filesystem::path path (buffer);
140 path = boost::filesystem::canonical (path);
141 path = path.parent_path ();
142 path = path.parent_path ();
148 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out, shared_ptr<Log> log)
150 #ifdef DCPOMATIC_WINDOWS
151 SECURITY_ATTRIBUTES security;
152 security.nLength = sizeof (security);
153 security.bInheritHandle = TRUE;
154 security.lpSecurityDescriptor = 0;
156 HANDLE child_stderr_read;
157 HANDLE child_stderr_write;
158 if (!CreatePipe (&child_stderr_read, &child_stderr_write, &security, 0)) {
159 log->log ("ffprobe call failed (could not CreatePipe)");
164 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
165 PathRemoveFileSpec (dir);
166 SetCurrentDirectory (dir);
168 STARTUPINFO startup_info;
169 ZeroMemory (&startup_info, sizeof (startup_info));
170 startup_info.cb = sizeof (startup_info);
171 startup_info.hStdError = child_stderr_write;
172 startup_info.dwFlags |= STARTF_USESTDHANDLES;
174 wchar_t command[512];
175 wcscpy (command, L"ffprobe.exe \"");
178 MultiByteToWideChar (CP_UTF8, 0, content.string().c_str(), -1, file, sizeof(file));
179 wcscat (command, file);
181 wcscat (command, L"\"");
183 PROCESS_INFORMATION process_info;
184 ZeroMemory (&process_info, sizeof (process_info));
185 if (!CreateProcess (0, command, 0, 0, TRUE, CREATE_NO_WINDOW, 0, 0, &startup_info, &process_info)) {
186 log->log ("ffprobe call failed (could not CreateProcess)");
190 FILE* o = fopen (out.string().c_str(), "w");
192 log->log ("ffprobe call failed (could not create output file)");
196 CloseHandle (child_stderr_write);
201 if (!ReadFile(child_stderr_read, buffer, sizeof(buffer), &read, 0) || read == 0) {
204 fwrite (buffer, read, 1, o);
209 WaitForSingleObject (process_info.hProcess, INFINITE);
210 CloseHandle (process_info.hProcess);
211 CloseHandle (process_info.hThread);
212 CloseHandle (child_stderr_read);
215 #ifdef DCPOMATIC_LINUX
216 string ffprobe = "ffprobe \"" + content.string() + "\" 2> \"" + out.string() + "\"";
217 log->log (String::compose ("Probing with %1", ffprobe));
218 system (ffprobe.c_str ());
222 boost::filesystem::path path = app_contents();
226 string ffprobe = path.string() + " \"" + content.string() + "\" 2> \"" + out.string() + "\"";
227 log->log (String::compose ("Probing with %1", ffprobe));
228 system (ffprobe.c_str ());
232 list<pair<string, string> >
235 list<pair<string, string> > m;
237 #ifdef DCPOMATIC_LINUX
238 FILE* f = setmntent ("/etc/mtab", "r");
244 struct mntent* mnt = getmntent (f);
249 m.push_back (make_pair (mnt->mnt_dir, mnt->mnt_type));
258 boost::filesystem::path
261 #ifdef DCPOMATIC_WINDOWS
263 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
264 PathRemoveFileSpec (dir);
266 boost::filesystem::path path = dir;
267 path /= "openssl.exe";
270 /* We assume that it's on the path for Linux and OS X */
276 /* Apparently there is no way to create an ofstream using a UTF-8
277 filename under Windows. We are hence reduced to using fopen
281 fopen_boost (boost::filesystem::path p, string t)
283 #ifdef DCPOMATIC_WINDOWS
284 wstring w (t.begin(), t.end());
285 /* c_str() here should give a UTF-16 string */
286 return _wfopen (p.c_str(), w.c_str ());
288 return fopen (p.c_str(), t.c_str ());