tabs -> spaces.
[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::make_pair;
43 using boost::shared_ptr;
44
45 void
46 dcpomatic_sleep (int s)
47 {
48 #ifdef DCPOMATIC_POSIX
49         sleep (s);
50 #endif
51 #ifdef DCPOMATIC_WINDOWS
52         Sleep (s * 1000);
53 #endif
54 }
55
56 /** @return A pair containing CPU model name and the number of processors */
57 pair<string, int>
58 cpu_info ()
59 {
60         pair<string, int> info;
61         info.second = 0;
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.first = l.substr (c + 2);
72                         }
73                 } else if (boost::algorithm::starts_with (l, "processor")) {
74                         ++info.second;
75                 }
76         }
77 #endif
78
79 #ifdef DCPOMATIC_OSX
80         size_t N = sizeof (info.second);
81         sysctlbyname ("hw.ncpu", &info.second, &N, 0, 0);
82         char buffer[64];
83         N = sizeof (buffer);
84         if (sysctlbyname ("machdep.cpu.brand_string", buffer, &N, 0, 0) == 0) {
85                 info.first = buffer;
86         }
87 #endif          
88
89         return info;
90 }
91
92 void
93 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out, shared_ptr<Log> log)
94 {
95 #ifdef DCPOMATIC_WINDOWS
96         SECURITY_ATTRIBUTES security;
97         security.nLength = sizeof (security);
98         security.bInheritHandle = TRUE;
99         security.lpSecurityDescriptor = 0;
100
101         HANDLE child_stderr_read;
102         HANDLE child_stderr_write;
103         if (!CreatePipe (&child_stderr_read, &child_stderr_write, &security, 0)) {
104                 log->log ("ffprobe call failed (could not CreatePipe)");
105                 return;
106         }
107
108         wchar_t dir[512];
109         GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
110         PathRemoveFileSpec (dir);
111         SetCurrentDirectory (dir);
112
113         STARTUPINFO startup_info;
114         ZeroMemory (&startup_info, sizeof (startup_info));
115         startup_info.cb = sizeof (startup_info);
116         startup_info.hStdError = child_stderr_write;
117         startup_info.dwFlags |= STARTF_USESTDHANDLES;
118
119         wchar_t command[512];
120         wcscpy (command, L"ffprobe.exe \"");
121
122         wchar_t file[512];
123         MultiByteToWideChar (CP_UTF8, 0, content.string().c_str(), -1, file, sizeof(file));
124         wcscat (command, file);
125
126         wcscat (command, L"\"");
127
128         PROCESS_INFORMATION process_info;
129         ZeroMemory (&process_info, sizeof (process_info));
130         if (!CreateProcess (0, command, 0, 0, TRUE, CREATE_NO_WINDOW, 0, 0, &startup_info, &process_info)) {
131                 log->log ("ffprobe call failed (could not CreateProcess)");
132                 return;
133         }
134
135         FILE* o = fopen (out.string().c_str(), "w");
136         if (!o) {
137                 log->log ("ffprobe call failed (could not create output file)");
138                 return;
139         }
140
141         CloseHandle (child_stderr_write);
142
143         while (1) {
144                 char buffer[512];
145                 DWORD read;
146                 if (!ReadFile(child_stderr_read, buffer, sizeof(buffer), &read, 0) || read == 0) {
147                         break;
148                 }
149                 fwrite (buffer, read, 1, o);
150         }
151
152         fclose (o);
153
154         WaitForSingleObject (process_info.hProcess, INFINITE);
155         CloseHandle (process_info.hProcess);
156         CloseHandle (process_info.hThread);
157         CloseHandle (child_stderr_read);
158 #else
159         string ffprobe = "ffprobe \"" + content.string() + "\" 2> \"" + out.string() + "\"";
160         log->log (String::compose ("Probing with %1", ffprobe));
161         system (ffprobe.c_str ());
162 #endif  
163 }
164
165 list<pair<string, string> >
166 mount_info ()
167 {
168         list<pair<string, string> > m;
169         
170 #ifdef DCPOMATIC_LINUX
171         FILE* f = setmntent ("/etc/mtab", "r");
172         if (!f) {
173                 return m;
174         }
175         
176         while (1) {
177                 struct mntent* mnt = getmntent (f);
178                 if (!mnt) {
179                         break;
180                 }
181
182                 m.push_back (make_pair (mnt->mnt_dir, mnt->mnt_type));
183         }
184
185         endmntent (f);
186 #endif
187
188         return m;
189 }