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