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