Merge branch '1.0' of /home/carl/git/dvdomatic into 1.0
[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 #endif
38 #ifdef DCPOMATIC_POSIX
39 #include <sys/types.h>
40 #include <ifaddrs.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
43 #endif
44 #include "exceptions.h"
45
46 using std::pair;
47 using std::list;
48 using std::ifstream;
49 using std::string;
50 using std::wstring;
51 using std::make_pair;
52 using boost::shared_ptr;
53
54 /** @param s Number of seconds to sleep for */
55 void
56 dcpomatic_sleep (int s)
57 {
58 #ifdef DCPOMATIC_POSIX
59         sleep (s);
60 #endif
61 #ifdef DCPOMATIC_WINDOWS
62         Sleep (s * 1000);
63 #endif
64 }
65
66 /** @return A string of CPU information (model name etc.) */
67 string
68 cpu_info ()
69 {
70         string info;
71         
72 #ifdef DCPOMATIC_LINUX
73         ifstream f ("/proc/cpuinfo");
74         while (f.good ()) {
75                 string l;
76                 getline (f, l);
77                 if (boost::algorithm::starts_with (l, "model name")) {
78                         string::size_type const c = l.find (':');
79                         if (c != string::npos) {
80                                 info = l.substr (c + 2);
81                         }
82                 }
83         }
84 #endif
85
86 #ifdef DCPOMATIC_OSX
87         char buffer[64];
88         size_t N = sizeof (buffer);
89         if (sysctlbyname ("machdep.cpu.brand_string", buffer, &N, 0, 0) == 0) {
90                 info = buffer;
91         }
92 #endif          
93
94 #ifdef DCPOMATIC_WINDOWS
95         HKEY key;
96         if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &key) != ERROR_SUCCESS) {
97                 return info;
98         }
99
100         DWORD type;
101         DWORD data;
102         if (RegQueryValueEx (key, L"ProcessorNameString", 0, &type, 0, &data) != ERROR_SUCCESS) {
103                 return info;
104         }
105
106         if (type != REG_SZ) {
107                 return info;
108         }
109
110         wstring value (data / sizeof (wchar_t), L'\0');
111         if (RegQueryValueEx (key, L"ProcessorNameString", 0, 0, reinterpret_cast<LPBYTE> (&value[0]), &data) != ERROR_SUCCESS) {
112                 RegCloseKey (key);
113                 return info;
114         }
115
116         info = string (value.begin(), value.end());
117         
118         RegCloseKey (key);
119
120 #endif  
121         
122         return info;
123 }
124
125 #ifdef DCPOMATIC_OSX
126 /** @return Path of the Contents directory in the .app */
127 boost::filesystem::path
128 app_contents ()
129 {
130         uint32_t size = 1024;
131         char buffer[size];
132         if (_NSGetExecutablePath (buffer, &size)) {
133                 throw StringError ("_NSGetExecutablePath failed");
134         }
135         
136         boost::filesystem::path path (buffer);
137         path = boost::filesystem::canonical (path);
138         path = path.parent_path ();
139         path = path.parent_path ();
140         return path;
141 }
142 #endif
143
144 void
145 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out, shared_ptr<Log> log)
146 {
147 #ifdef DCPOMATIC_WINDOWS
148         SECURITY_ATTRIBUTES security;
149         security.nLength = sizeof (security);
150         security.bInheritHandle = TRUE;
151         security.lpSecurityDescriptor = 0;
152
153         HANDLE child_stderr_read;
154         HANDLE child_stderr_write;
155         if (!CreatePipe (&child_stderr_read, &child_stderr_write, &security, 0)) {
156                 log->log ("ffprobe call failed (could not CreatePipe)");
157                 return;
158         }
159
160         wchar_t dir[512];
161         GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
162         PathRemoveFileSpec (dir);
163         SetCurrentDirectory (dir);
164
165         STARTUPINFO startup_info;
166         ZeroMemory (&startup_info, sizeof (startup_info));
167         startup_info.cb = sizeof (startup_info);
168         startup_info.hStdError = child_stderr_write;
169         startup_info.dwFlags |= STARTF_USESTDHANDLES;
170
171         wchar_t command[512];
172         wcscpy (command, L"ffprobe.exe \"");
173
174         wchar_t file[512];
175         MultiByteToWideChar (CP_UTF8, 0, content.string().c_str(), -1, file, sizeof(file));
176         wcscat (command, file);
177
178         wcscat (command, L"\"");
179
180         PROCESS_INFORMATION process_info;
181         ZeroMemory (&process_info, sizeof (process_info));
182         if (!CreateProcess (0, command, 0, 0, TRUE, CREATE_NO_WINDOW, 0, 0, &startup_info, &process_info)) {
183                 log->log ("ffprobe call failed (could not CreateProcess)");
184                 return;
185         }
186
187         FILE* o = fopen (out.string().c_str(), "w");
188         if (!o) {
189                 log->log ("ffprobe call failed (could not create output file)");
190                 return;
191         }
192
193         CloseHandle (child_stderr_write);
194
195         while (1) {
196                 char buffer[512];
197                 DWORD read;
198                 if (!ReadFile(child_stderr_read, buffer, sizeof(buffer), &read, 0) || read == 0) {
199                         break;
200                 }
201                 fwrite (buffer, read, 1, o);
202         }
203
204         fclose (o);
205
206         WaitForSingleObject (process_info.hProcess, INFINITE);
207         CloseHandle (process_info.hProcess);
208         CloseHandle (process_info.hThread);
209         CloseHandle (child_stderr_read);
210 #endif
211
212 #ifdef DCPOMATIC_LINUX 
213         string ffprobe = "ffprobe \"" + content.string() + "\" 2> \"" + out.string() + "\"";
214         log->log (String::compose ("Probing with %1", ffprobe));
215         system (ffprobe.c_str ());
216 #endif
217
218 #ifdef DCPOMATIC_OSX
219         boost::filesystem::path path = app_contents();
220         path /= "MacOS";
221         path /= "ffprobe";
222         
223         string ffprobe = path.string() + " \"" + content.string() + "\" 2> \"" + out.string() + "\"";
224         log->log (String::compose ("Probing with %1", ffprobe));
225         system (ffprobe.c_str ());
226 #endif
227 }
228
229 list<pair<string, string> >
230 mount_info ()
231 {
232         list<pair<string, string> > m;
233         
234 #ifdef DCPOMATIC_LINUX
235         FILE* f = setmntent ("/etc/mtab", "r");
236         if (!f) {
237                 return m;
238         }
239         
240         while (1) {
241                 struct mntent* mnt = getmntent (f);
242                 if (!mnt) {
243                         break;
244                 }
245
246                 m.push_back (make_pair (mnt->mnt_dir, mnt->mnt_type));
247         }
248
249         endmntent (f);
250 #endif
251
252         return m;
253 }
254
255 boost::filesystem::path
256 openssl_path ()
257 {
258 #ifdef DCPOMATIC_WINDOWS
259         wchar_t dir[512];
260         GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
261         PathRemoveFileSpec (dir);
262         
263         boost::filesystem::path path = dir;
264         path /= "openssl.exe";
265         return path;
266 #else   
267         /* We assume that it's on the path for Linux and OS X */
268         return "openssl";
269 #endif
270
271 }