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;
55 dcpomatic_sleep (int s)
57 #ifdef DCPOMATIC_POSIX
60 #ifdef DCPOMATIC_WINDOWS
65 /** @return A string of CPU information (model name etc.) */
71 #ifdef DCPOMATIC_LINUX
72 ifstream f ("/proc/cpuinfo");
76 if (boost::algorithm::starts_with (l, "model name")) {
77 string::size_type const c = l.find (':');
78 if (c != string::npos) {
79 info = l.substr (c + 2);
87 size_t N = sizeof (buffer);
88 if (sysctlbyname ("machdep.cpu.brand_string", buffer, &N, 0, 0) == 0) {
93 #ifdef DCPOMATIC_WINDOWS
95 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &key) != ERROR_SUCCESS) {
101 if (RegQueryValueEx (key, L"ProcessorNameString", 0, &type, 0, &data) != ERROR_SUCCESS) {
105 if (type != REG_SZ) {
109 wstring value (data / sizeof (wchar_t), L'\0');
110 if (RegQueryValueEx (key, L"ProcessorNameString", 0, 0, reinterpret_cast<LPBYTE> (&value[0]), &data) != ERROR_SUCCESS) {
115 info = string (value.begin(), value.end());
125 /** @return Path of the Contents directory in the .app */
126 boost::filesystem::path
129 uint32_t size = 1024;
131 if (_NSGetExecutablePath (buffer, &size)) {
132 throw StringError ("_NSGetExecutablePath failed");
135 boost::filesystem::path path (buffer);
136 path = boost::filesystem::canonical (path);
137 path = path.parent_path ();
138 path = path.parent_path ();
144 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out, shared_ptr<Log> log)
146 #ifdef DCPOMATIC_WINDOWS
147 SECURITY_ATTRIBUTES security;
148 security.nLength = sizeof (security);
149 security.bInheritHandle = TRUE;
150 security.lpSecurityDescriptor = 0;
152 HANDLE child_stderr_read;
153 HANDLE child_stderr_write;
154 if (!CreatePipe (&child_stderr_read, &child_stderr_write, &security, 0)) {
155 log->log ("ffprobe call failed (could not CreatePipe)");
160 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
161 PathRemoveFileSpec (dir);
162 SetCurrentDirectory (dir);
164 STARTUPINFO startup_info;
165 ZeroMemory (&startup_info, sizeof (startup_info));
166 startup_info.cb = sizeof (startup_info);
167 startup_info.hStdError = child_stderr_write;
168 startup_info.dwFlags |= STARTF_USESTDHANDLES;
170 wchar_t command[512];
171 wcscpy (command, L"ffprobe.exe \"");
174 MultiByteToWideChar (CP_UTF8, 0, content.string().c_str(), -1, file, sizeof(file));
175 wcscat (command, file);
177 wcscat (command, L"\"");
179 PROCESS_INFORMATION process_info;
180 ZeroMemory (&process_info, sizeof (process_info));
181 if (!CreateProcess (0, command, 0, 0, TRUE, CREATE_NO_WINDOW, 0, 0, &startup_info, &process_info)) {
182 log->log ("ffprobe call failed (could not CreateProcess)");
186 FILE* o = fopen (out.string().c_str(), "w");
188 log->log ("ffprobe call failed (could not create output file)");
192 CloseHandle (child_stderr_write);
197 if (!ReadFile(child_stderr_read, buffer, sizeof(buffer), &read, 0) || read == 0) {
200 fwrite (buffer, read, 1, o);
205 WaitForSingleObject (process_info.hProcess, INFINITE);
206 CloseHandle (process_info.hProcess);
207 CloseHandle (process_info.hThread);
208 CloseHandle (child_stderr_read);
211 #ifdef DCPOMATIC_LINUX
212 string ffprobe = "ffprobe \"" + content.string() + "\" 2> \"" + out.string() + "\"";
213 log->log (String::compose ("Probing with %1", ffprobe));
214 system (ffprobe.c_str ());
218 boost::filesystem::path path = app_contents();
222 string ffprobe = path.string() + " \"" + content.string() + "\" 2> \"" + out.string() + "\"";
223 log->log (String::compose ("Probing with %1", ffprobe));
224 system (ffprobe.c_str ());
228 list<pair<string, string> >
231 list<pair<string, string> > m;
233 #ifdef DCPOMATIC_LINUX
234 FILE* f = setmntent ("/etc/mtab", "r");
240 struct mntent* mnt = getmntent (f);
245 m.push_back (make_pair (mnt->mnt_dir, mnt->mnt_type));
254 boost::filesystem::path
257 #ifdef DCPOMATIC_WINDOWS
259 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
260 PathRemoveFileSpec (dir);
262 boost::filesystem::path path = dir;
263 path /= "openssl.exe";
266 /* We assume that it's on the path for Linux and OS X */
273 network_interfaces ()
275 list<string> interfaces;
277 #ifdef DCPOMATIC_POSIX
278 struct ifaddrs* addresses = 0;
280 getifaddrs (&addresses);
282 for (struct ifaddrs* i = addresses; i; i = i->ifa_next) {
283 if (i->ifa_addr->sa_family == AF_INET) {
284 void* p = &((struct sockaddr_in *) i->ifa_addr)->sin_addr;
285 char b[INET_ADDRSTRLEN];
286 inet_ntop (AF_INET, p, b, INET_ADDRSTRLEN);
287 interfaces.push_back (b);
291 freeifaddrs (addresses);