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