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