2 Copyright (C) 2012-2020 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
22 #include "compose.hpp"
24 #include "dcpomatic_log.h"
26 #include "exceptions.h"
27 #include <dcp/raw_convert.h>
30 #include <libavformat/avio.h>
32 #include <boost/algorithm/string.hpp>
33 #include <boost/foreach.hpp>
56 using std::runtime_error;
57 using boost::shared_ptr;
58 using boost::optional;
60 /** @param s Number of seconds to sleep for */
62 dcpomatic_sleep_seconds (int s)
68 dcpomatic_sleep_milliseconds (int ms)
73 /** @return A string of CPU information (model name etc.) */
80 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &key) != ERROR_SUCCESS) {
86 if (RegQueryValueEx (key, L"ProcessorNameString", 0, &type, 0, &data) != ERROR_SUCCESS) {
94 wstring value (data / sizeof (wchar_t), L'\0');
95 if (RegQueryValueEx (key, L"ProcessorNameString", 0, 0, reinterpret_cast<LPBYTE> (&value[0]), &data) != ERROR_SUCCESS) {
100 info = string (value.begin(), value.end());
107 boost::filesystem::path
111 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
112 PathRemoveFileSpec (dir);
113 boost::filesystem::path path = dir;
114 return path.parent_path();
118 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out)
120 SECURITY_ATTRIBUTES security;
121 security.nLength = sizeof (security);
122 security.bInheritHandle = TRUE;
123 security.lpSecurityDescriptor = 0;
125 HANDLE child_stderr_read;
126 HANDLE child_stderr_write;
127 if (!CreatePipe (&child_stderr_read, &child_stderr_write, &security, 0)) {
128 LOG_ERROR_NC ("ffprobe call failed (could not CreatePipe)");
133 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
134 PathRemoveFileSpec (dir);
135 SetCurrentDirectory (dir);
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;
143 wchar_t command[512];
144 wcscpy (command, L"ffprobe.exe \"");
147 MultiByteToWideChar (CP_UTF8, 0, content.string().c_str(), -1, file, sizeof(file));
148 wcscat (command, file);
150 wcscat (command, L"\"");
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_ERROR_NC (N_("ffprobe call failed (could not CreateProcess)"));
159 FILE* o = fopen_boost (out, "w");
161 LOG_ERROR_NC (N_("ffprobe call failed (could not create output file)"));
165 CloseHandle (child_stderr_write);
170 if (!ReadFile(child_stderr_read, buffer, sizeof(buffer), &read, 0) || read == 0) {
173 fwrite (buffer, read, 1, o);
178 WaitForSingleObject (process_info.hProcess, INFINITE);
179 CloseHandle (process_info.hProcess);
180 CloseHandle (process_info.hThread);
181 CloseHandle (child_stderr_read);
184 list<pair<string, string> >
187 list<pair<string, string> > m;
191 boost::filesystem::path
195 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
196 PathRemoveFileSpec (dir);
198 boost::filesystem::path path = dir;
199 path /= "openssl.exe";
204 /* Apparently there is no way to create an ofstream using a UTF-8
205 filename under Windows. We are hence reduced to using fopen
209 fopen_boost (boost::filesystem::path p, string t)
211 wstring w (t.begin(), t.end());
212 /* c_str() here should give a UTF-16 string */
213 return _wfopen (p.c_str(), w.c_str ());
217 dcpomatic_fseek (FILE* stream, int64_t offset, int whence)
219 return _fseeki64 (stream, offset, whence);
225 boost::mutex::scoped_lock lm (_mutex);
226 SetThreadExecutionState (ES_SYSTEM_REQUIRED);
240 start_tool (boost::filesystem::path dcpomatic, string executable, string)
242 boost::filesystem::path batch = dcpomatic.parent_path() / executable;
244 STARTUPINFO startup_info;
245 ZeroMemory (&startup_info, sizeof (startup_info));
246 startup_info.cb = sizeof (startup_info);
248 PROCESS_INFORMATION process_info;
249 ZeroMemory (&process_info, sizeof (process_info));
252 MultiByteToWideChar (CP_UTF8, 0, batch.string().c_str(), -1, cmd, sizeof(cmd));
253 CreateProcess (0, cmd, 0, 0, FALSE, 0, 0, 0, &startup_info, &process_info);
257 start_batch_converter (boost::filesystem::path dcpomatic)
259 start_tool (dcpomatic, "dcpomatic2_batch", "DCP-o-matic\\ 2\\ Batch\\ Converter.app");
263 start_player (boost::filesystem::path dcpomatic)
265 start_tool (dcpomatic, "dcpomatic2_player", "DCP-o-matic\\ 2\\ Player.app");
271 return (uint64_t) GetCurrentThreadId ();
275 avio_open_boost (AVIOContext** s, boost::filesystem::path file, int flags)
277 int const length = (file.string().length() + 1) * 2;
278 char* utf8 = new char[length];
279 WideCharToMultiByte (CP_UTF8, 0, file.c_str(), -1, utf8, length, 0, 0);
280 int const r = avio_open (s, utf8, flags);
286 maybe_open_console ()
288 if (Config::instance()->win32_console ()) {
291 HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
292 int hCrt = _open_osfhandle((intptr_t) handle_out, _O_TEXT);
293 FILE* hf_out = _fdopen(hCrt, "w");
294 setvbuf(hf_out, NULL, _IONBF, 1);
297 HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
298 hCrt = _open_osfhandle((intptr_t) handle_in, _O_TEXT);
299 FILE* hf_in = _fdopen(hCrt, "r");
300 setvbuf(hf_in, NULL, _IONBF, 128);
305 boost::filesystem::path
308 return boost::filesystem::path(getenv("HOMEDRIVE")) / boost::filesystem::path(getenv("HOMEPATH"));
312 command_and_read (string)
317 /** @return true if this process is a 32-bit one running on a 64-bit-capable OS */
322 IsWow64Process (GetCurrentProcess(), &p);
329 vector<Drive> drives;
331 const GUID GUID_DEVICE_INTERFACE_DISK = {
332 0x53F56307L, 0xB6BF, 0x11D0, { 0x94, 0xF2, 0x00, 0xA0, 0xC9, 0x1E, 0xFB, 0x8B }
335 HDEVINFO device_info = SetupDiGetClassDevsA (&GUID_DEVICE_INTERFACE_DISK, 0, 0, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
336 if (device_info == INVALID_HANDLE_VALUE) {
337 LOG_DIST_NC("SetupDiClassDevsA failed");
343 SP_DEVINFO_DATA device_info_data;
344 device_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
345 if (!SetupDiEnumDeviceInfo(device_info, i, &device_info_data)) {
346 LOG_DIST ("SetupDiEnumDeviceInfo failed (%1)", GetLastError());
351 wchar_t friendly_name_buffer[MAX_PATH];
352 ZeroMemory (&friendly_name_buffer, sizeof(friendly_name_buffer));
354 bool r = SetupDiGetDeviceRegistryPropertyW (
355 device_info, &device_info_data, SPDRP_FRIENDLYNAME, 0, reinterpret_cast<PBYTE>(friendly_name_buffer), sizeof(friendly_name_buffer), 0
359 int const length = (wcslen(friendly_name_buffer) + 1) * 2;
360 char* utf8 = new char[length];
361 /* XXX: this is used in a few places in this file; should be abstracted out */
362 WideCharToMultiByte (CP_UTF8, 0, friendly_name_buffer, -1, utf8, length, 0, 0);
363 /* XXX: utf8 contains a user-readable name */
369 SP_DEVICE_INTERFACE_DATA device_interface_data;
370 device_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
372 bool r = SetupDiEnumDeviceInterfaces (device_info, &device_info_data, &GUID_DEVICE_INTERFACE_DISK, j, &device_interface_data);
374 DWORD e = GetLastError();
375 if (e == ERROR_NO_MORE_ITEMS) {
378 LOG_DIST("SetupDiEnumDeviceInterfaces failed (%1)", e);
384 r = SetupDiGetDeviceInterfaceDetailW(device_info, &device_interface_data, 0, 0, &size, 0);
385 PSP_DEVICE_INTERFACE_DETAIL_DATA_W device_detail_data = static_cast<PSP_DEVICE_INTERFACE_DETAIL_DATA_W> (malloc(size));
386 if (!device_detail_data) {
387 LOG_DIST_NC("malloc failed");
391 device_detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_W);
393 r = SetupDiGetDeviceInterfaceDetailW (device_info, &device_interface_data, device_detail_data, size, &size, 0);
395 LOG_DIST_NC("SetupDiGetDeviceInterfaceDetailW failed");
396 return vector<Drive>();
399 HANDLE device = CreateFileW (
400 device_detail_data->DevicePath, 0,
401 FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
402 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0
405 if (device == INVALID_HANDLE_VALUE) {
406 LOG_DIST_NC("CreateFileW failed");
410 VOLUME_DISK_EXTENTS disk_extents;
411 r = DeviceIoControl (
412 device, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, 0, 0,
413 &disk_extents, sizeof(VOLUME_DISK_EXTENTS), &size, 0
416 if (r && disk_extents.NumberOfDiskExtents > 0) {
417 LOG_DIST("GET_VOLUME_DISK_EXTENTS gives %1", disk_extents.Extents[0].DiskNumber);
418 /* Disk number for \\.\PHYSICALDRIVEx is disk disk_extents.Extents[0].DiskNumber */
421 STORAGE_DEVICE_NUMBER device_number;
422 r = DeviceIoControl (
423 device, IOCTL_STORAGE_GET_DEVICE_NUMBER, 0, 0,
424 &device_number, sizeof(device_number), &size, 0
428 LOG_DIST("GET_DEVICE_NUMBER gives %1", device_number.DeviceNumber);
429 /* Disk number for \\.\PHYSICALDRIVEx is device_number.DeviceNumber */
440 Drive::description () const
443 snprintf(gb, 64, "%.1f", _size / 1000000000.0);
450 if (name.size() > 0) {
451 name += " " + *_model;
454 if (name.size() == 0) {
458 return String::compose("%1 (%2 GB) [%3]", name, gb, _internal_name);
461 boost::filesystem::path
464 boost::filesystem::path p;
465 p /= g_get_user_config_dir ();