Fix test reference after subtitle fixes.
[dcpomatic.git] / src / lib / cross.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include "cross.h"
21 #include "compose.hpp"
22 #include "log.h"
23 #include "exceptions.h"
24 #include <boost/algorithm/string.hpp>
25 #ifdef DCPOMATIC_LINUX
26 #include <unistd.h>
27 #include <mntent.h>
28 #endif
29 #ifdef DCPOMATIC_WINDOWS
30 #include <windows.h>
31 #undef DATADIR
32 #include <shlwapi.h>
33 #endif
34 #ifdef DCPOMATIC_OSX
35 #include <sys/sysctl.h>
36 #include <mach-o/dyld.h>
37 #include <IOKit/pwr_mgt/IOPMLib.h>
38 #endif
39 #ifdef DCPOMATIC_POSIX
40 #include <sys/types.h>
41 #include <ifaddrs.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44 #endif
45 #include <fstream>
46
47 #include "i18n.h"
48
49 #define LOG_GENERAL(...) log->log (String::compose (__VA_ARGS__), Log::TYPE_GENERAL);
50 #define LOG_ERROR(...) log->log (String::compose (__VA_ARGS__), Log::TYPE_ERROR);
51 #define LOG_ERROR_NC(...) log->log (__VA_ARGS__, Log::TYPE_ERROR);
52
53 using std::pair;
54 using std::list;
55 using std::ifstream;
56 using std::string;
57 using std::wstring;
58 using std::make_pair;
59 using boost::shared_ptr;
60
61 /** @param s Number of seconds to sleep for */
62 void
63 dcpomatic_sleep (int s)
64 {
65 #ifdef DCPOMATIC_POSIX
66         sleep (s);
67 #endif
68 #ifdef DCPOMATIC_WINDOWS
69         Sleep (s * 1000);
70 #endif
71 }
72
73 /** @return A string of CPU information (model name etc.) */
74 string
75 cpu_info ()
76 {
77         string info;
78         
79 #ifdef DCPOMATIC_LINUX
80         /* This use of ifstream is ok; the filename can never
81            be non-Latin
82         */
83         ifstream f ("/proc/cpuinfo");
84         while (f.good ()) {
85                 string l;
86                 getline (f, l);
87                 if (boost::algorithm::starts_with (l, "model name")) {
88                         string::size_type const c = l.find (':');
89                         if (c != string::npos) {
90                                 info = l.substr (c + 2);
91                         }
92                 }
93         }
94 #endif
95
96 #ifdef DCPOMATIC_OSX
97         char buffer[64];
98         size_t N = sizeof (buffer);
99         if (sysctlbyname ("machdep.cpu.brand_string", buffer, &N, 0, 0) == 0) {
100                 info = buffer;
101         }
102 #endif          
103
104 #ifdef DCPOMATIC_WINDOWS
105         HKEY key;
106         if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &key) != ERROR_SUCCESS) {
107                 return info;
108         }
109
110         DWORD type;
111         DWORD data;
112         if (RegQueryValueEx (key, L"ProcessorNameString", 0, &type, 0, &data) != ERROR_SUCCESS) {
113                 return info;
114         }
115
116         if (type != REG_SZ) {
117                 return info;
118         }
119
120         wstring value (data / sizeof (wchar_t), L'\0');
121         if (RegQueryValueEx (key, L"ProcessorNameString", 0, 0, reinterpret_cast<LPBYTE> (&value[0]), &data) != ERROR_SUCCESS) {
122                 RegCloseKey (key);
123                 return info;
124         }
125
126         info = string (value.begin(), value.end());
127         
128         RegCloseKey (key);
129
130 #endif  
131         
132         return info;
133 }
134
135 #ifdef DCPOMATIC_OSX
136 /** @return Path of the Contents directory in the .app */
137 boost::filesystem::path
138 app_contents ()
139 {
140         uint32_t size = 1024;
141         char buffer[size];
142         if (_NSGetExecutablePath (buffer, &size)) {
143                 throw StringError ("_NSGetExecutablePath failed");
144         }
145         
146         boost::filesystem::path path (buffer);
147         path = boost::filesystem::canonical (path);
148         path = path.parent_path ();
149         path = path.parent_path ();
150         return path;
151 }
152 #endif
153
154 boost::filesystem::path
155 shared_path ()
156 {
157 #ifdef DCPOMATIC_LINUX
158 #ifdef DCPOMATIC_DEBUG
159         char const * p = getenv ("DCPOMATIC_LINUX_SHARE_PREFIX");
160         if (p) {
161                 return p;
162         }
163 #endif
164         return boost::filesystem::canonical (LINUX_SHARE_PREFIX);
165 #endif
166 #ifdef DCPOMATIC_WINDOWS
167         wchar_t dir[512];
168         GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
169         PathRemoveFileSpec (dir);
170         boost::filesystem::path path = dir;
171         return path.parent_path();
172 #endif
173 #ifdef DCPOMATIC_OSX
174         return app_contents();
175 #endif  
176 }
177
178 void
179 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out, shared_ptr<Log> log)
180 {
181 #ifdef DCPOMATIC_WINDOWS
182         SECURITY_ATTRIBUTES security;
183         security.nLength = sizeof (security);
184         security.bInheritHandle = TRUE;
185         security.lpSecurityDescriptor = 0;
186
187         HANDLE child_stderr_read;
188         HANDLE child_stderr_write;
189         if (!CreatePipe (&child_stderr_read, &child_stderr_write, &security, 0)) {
190                 LOG_ERROR_NC ("ffprobe call failed (could not CreatePipe)");
191                 return;
192         }
193
194         wchar_t dir[512];
195         GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
196         PathRemoveFileSpec (dir);
197         SetCurrentDirectory (dir);
198
199         STARTUPINFO startup_info;
200         ZeroMemory (&startup_info, sizeof (startup_info));
201         startup_info.cb = sizeof (startup_info);
202         startup_info.hStdError = child_stderr_write;
203         startup_info.dwFlags |= STARTF_USESTDHANDLES;
204
205         wchar_t command[512];
206         wcscpy (command, L"ffprobe.exe \"");
207
208         wchar_t file[512];
209         MultiByteToWideChar (CP_UTF8, 0, content.string().c_str(), -1, file, sizeof(file));
210         wcscat (command, file);
211
212         wcscat (command, L"\"");
213
214         PROCESS_INFORMATION process_info;
215         ZeroMemory (&process_info, sizeof (process_info));
216         if (!CreateProcess (0, command, 0, 0, TRUE, CREATE_NO_WINDOW, 0, 0, &startup_info, &process_info)) {
217                 LOG_ERROR_NC (N_("ffprobe call failed (could not CreateProcess)"));
218                 return;
219         }
220
221         FILE* o = fopen_boost (out, "w");
222         if (!o) {
223                 LOG_ERROR_NC (N_("ffprobe call failed (could not create output file)"));
224                 return;
225         }
226
227         CloseHandle (child_stderr_write);
228
229         while (true) {
230                 char buffer[512];
231                 DWORD read;
232                 if (!ReadFile(child_stderr_read, buffer, sizeof(buffer), &read, 0) || read == 0) {
233                         break;
234                 }
235                 fwrite (buffer, read, 1, o);
236         }
237
238         fclose (o);
239
240         WaitForSingleObject (process_info.hProcess, INFINITE);
241         CloseHandle (process_info.hProcess);
242         CloseHandle (process_info.hThread);
243         CloseHandle (child_stderr_read);
244 #endif
245
246 #ifdef DCPOMATIC_LINUX 
247         string ffprobe = "ffprobe \"" + content.string() + "\" 2> \"" + out.string() + "\"";
248         LOG_GENERAL (N_("Probing with %1"), ffprobe);
249         system (ffprobe.c_str ());
250 #endif
251
252 #ifdef DCPOMATIC_OSX
253         boost::filesystem::path path = app_contents();
254         path /= "MacOS";
255         path /= "ffprobe";
256         
257         string ffprobe = path.string() + " \"" + content.string() + "\" 2> \"" + out.string() + "\"";
258         LOG_GENERAL (N_("Probing with %1"), ffprobe);
259         system (ffprobe.c_str ());
260 #endif
261 }
262
263 list<pair<string, string> >
264 mount_info ()
265 {
266         list<pair<string, string> > m;
267         
268 #ifdef DCPOMATIC_LINUX
269         FILE* f = setmntent ("/etc/mtab", "r");
270         if (!f) {
271                 return m;
272         }
273         
274         while (true) {
275                 struct mntent* mnt = getmntent (f);
276                 if (!mnt) {
277                         break;
278                 }
279
280                 m.push_back (make_pair (mnt->mnt_dir, mnt->mnt_type));
281         }
282
283         endmntent (f);
284 #endif
285
286         return m;
287 }
288
289 boost::filesystem::path
290 openssl_path ()
291 {
292 #ifdef DCPOMATIC_WINDOWS
293         wchar_t dir[512];
294         GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
295         PathRemoveFileSpec (dir);
296         
297         boost::filesystem::path path = dir;
298         path /= "openssl.exe";
299         return path;
300 #else   
301         /* We assume that it's on the path for Linux and OS X */
302         return "openssl";
303 #endif
304
305 }
306
307 /* Apparently there is no way to create an ofstream using a UTF-8
308    filename under Windows.  We are hence reduced to using fopen
309    with this wrapper.
310 */
311 FILE *
312 fopen_boost (boost::filesystem::path p, string t)
313 {
314 #ifdef DCPOMATIC_WINDOWS
315         wstring w (t.begin(), t.end());
316         /* c_str() here should give a UTF-16 string */
317         return _wfopen (p.c_str(), w.c_str ());
318 #else
319         return fopen (p.c_str(), t.c_str ());
320 #endif
321 }
322
323 int
324 dcpomatic_fseek (FILE* stream, int64_t offset, int whence)
325 {
326 #ifdef DCPOMATIC_WINDOWS
327         return _fseeki64 (stream, offset, whence);
328 #else   
329         return fseek (stream, offset, whence);
330 #endif  
331 }
332
333 void
334 Waker::nudge ()
335 {
336 #ifdef DCPOMATIC_WINDOWS
337         SetThreadExecutionState (ES_SYSTEM_REQUIRED);
338 #endif  
339 }
340
341 Waker::Waker ()
342 {
343 #ifdef DCPOMATIC_OSX
344         /* We should use this */
345         // IOPMAssertionCreateWithName (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, CFSTR ("Encoding DCP"), &_assertion_id);
346         /* but it's not available on 10.5, so we use this */
347         IOPMAssertionCreate (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, &_assertion_id);
348 #endif  
349 }
350
351 Waker::~Waker ()
352 {
353 #ifdef DCPOMATIC_OSX    
354         IOPMAssertionRelease (_assertion_id);
355 #endif  
356 }