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