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 "dcpomatic_assert.h"
28 #include <dcp/raw_convert.h>
31 #include <libavformat/avio.h>
33 #include <boost/algorithm/string.hpp>
34 #include <boost/foreach.hpp>
35 #include <boost/dll/runtime_symbol_info.hpp>
58 using std::runtime_error;
59 using boost::shared_ptr;
60 using boost::optional;
62 /** @param s Number of seconds to sleep for */
64 dcpomatic_sleep_seconds (int s)
70 dcpomatic_sleep_milliseconds (int ms)
75 /** @return A string of CPU information (model name etc.) */
82 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &key) != ERROR_SUCCESS) {
88 if (RegQueryValueEx (key, L"ProcessorNameString", 0, &type, 0, &data) != ERROR_SUCCESS) {
96 wstring value (data / sizeof (wchar_t), L'\0');
97 if (RegQueryValueEx (key, L"ProcessorNameString", 0, 0, reinterpret_cast<LPBYTE> (&value[0]), &data) != ERROR_SUCCESS) {
102 info = string (value.begin(), value.end());
110 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out)
112 SECURITY_ATTRIBUTES security;
113 security.nLength = sizeof (security);
114 security.bInheritHandle = TRUE;
115 security.lpSecurityDescriptor = 0;
117 HANDLE child_stderr_read;
118 HANDLE child_stderr_write;
119 if (!CreatePipe (&child_stderr_read, &child_stderr_write, &security, 0)) {
120 LOG_ERROR_NC ("ffprobe call failed (could not CreatePipe)");
125 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
126 PathRemoveFileSpec (dir);
127 SetCurrentDirectory (dir);
129 STARTUPINFO startup_info;
130 ZeroMemory (&startup_info, sizeof (startup_info));
131 startup_info.cb = sizeof (startup_info);
132 startup_info.hStdError = child_stderr_write;
133 startup_info.dwFlags |= STARTF_USESTDHANDLES;
135 wchar_t command[512];
136 wcscpy (command, L"ffprobe.exe \"");
139 MultiByteToWideChar (CP_UTF8, 0, content.string().c_str(), -1, file, sizeof(file));
140 wcscat (command, file);
142 wcscat (command, L"\"");
144 PROCESS_INFORMATION process_info;
145 ZeroMemory (&process_info, sizeof (process_info));
146 if (!CreateProcess (0, command, 0, 0, TRUE, CREATE_NO_WINDOW, 0, 0, &startup_info, &process_info)) {
147 LOG_ERROR_NC (N_("ffprobe call failed (could not CreateProcess)"));
151 FILE* o = fopen_boost (out, "w");
153 LOG_ERROR_NC (N_("ffprobe call failed (could not create output file)"));
157 CloseHandle (child_stderr_write);
162 if (!ReadFile(child_stderr_read, buffer, sizeof(buffer), &read, 0) || read == 0) {
165 fwrite (buffer, read, 1, o);
170 WaitForSingleObject (process_info.hProcess, INFINITE);
171 CloseHandle (process_info.hProcess);
172 CloseHandle (process_info.hThread);
173 CloseHandle (child_stderr_read);
176 list<pair<string, string> >
179 list<pair<string, string> > m;
183 static boost::filesystem::path
186 return boost::dll::program_location().parent_path();
189 boost::filesystem::path
192 return executable_path().parent_path();
195 boost::filesystem::path
198 return executable_path() / "openssl.exe";
201 boost::filesystem::path
204 return executable_path() / "dcpomatic2_disk_writer.exe";
207 /* Apparently there is no way to create an ofstream using a UTF-8
208 filename under Windows. We are hence reduced to using fopen
212 fopen_boost (boost::filesystem::path p, string t)
214 wstring w (t.begin(), t.end());
215 /* c_str() here should give a UTF-16 string */
216 return _wfopen (p.c_str(), w.c_str ());
220 dcpomatic_fseek (FILE* stream, int64_t offset, int whence)
222 return _fseeki64 (stream, offset, whence);
228 boost::mutex::scoped_lock lm (_mutex);
229 SetThreadExecutionState (ES_SYSTEM_REQUIRED);
243 start_tool (boost::filesystem::path dcpomatic, string executable, string)
245 boost::filesystem::path batch = dcpomatic.parent_path() / executable;
247 STARTUPINFO startup_info;
248 ZeroMemory (&startup_info, sizeof (startup_info));
249 startup_info.cb = sizeof (startup_info);
251 PROCESS_INFORMATION process_info;
252 ZeroMemory (&process_info, sizeof (process_info));
255 MultiByteToWideChar (CP_UTF8, 0, batch.string().c_str(), -1, cmd, sizeof(cmd));
256 CreateProcess (0, cmd, 0, 0, FALSE, 0, 0, 0, &startup_info, &process_info);
260 start_batch_converter (boost::filesystem::path dcpomatic)
262 start_tool (dcpomatic, "dcpomatic2_batch", "DCP-o-matic\\ 2\\ Batch\\ Converter.app");
266 start_player (boost::filesystem::path dcpomatic)
268 start_tool (dcpomatic, "dcpomatic2_player", "DCP-o-matic\\ 2\\ Player.app");
274 return (uint64_t) GetCurrentThreadId ();
278 wchar_to_utf8 (wchar_t const * s)
280 int const length = (wcslen(s) + 1) * 2;
281 char* utf8 = new char[length];
282 WideCharToMultiByte (CP_UTF8, 0, s, -1, utf8, length, 0, 0);
289 avio_open_boost (AVIOContext** s, boost::filesystem::path file, int flags)
291 return avio_open (s, wchar_to_utf8(file.c_str()).c_str(), flags);
295 maybe_open_console ()
297 if (Config::instance()->win32_console ()) {
300 HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
301 int hCrt = _open_osfhandle((intptr_t) handle_out, _O_TEXT);
302 FILE* hf_out = _fdopen(hCrt, "w");
303 setvbuf(hf_out, NULL, _IONBF, 1);
306 HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
307 hCrt = _open_osfhandle((intptr_t) handle_in, _O_TEXT);
308 FILE* hf_in = _fdopen(hCrt, "r");
309 setvbuf(hf_in, NULL, _IONBF, 128);
314 boost::filesystem::path
317 return boost::filesystem::path(getenv("HOMEDRIVE")) / boost::filesystem::path(getenv("HOMEPATH"));
321 command_and_read (string)
326 /** @return true if this process is a 32-bit one running on a 64-bit-capable OS */
331 IsWow64Process (GetCurrentProcess(), &p);
335 static optional<string>
336 get_friendly_name (HDEVINFO device_info, SP_DEVINFO_DATA* device_info_data)
338 wchar_t buffer[MAX_PATH];
339 ZeroMemory (&buffer, sizeof(buffer));
340 bool r = SetupDiGetDeviceRegistryPropertyW (
341 device_info, device_info_data, SPDRP_FRIENDLYNAME, 0, reinterpret_cast<PBYTE>(buffer), sizeof(buffer), 0
344 return optional<string>();
346 return wchar_to_utf8 (buffer);
349 static const GUID GUID_DEVICE_INTERFACE_DISK = {
350 0x53F56307L, 0xB6BF, 0x11D0, { 0x94, 0xF2, 0x00, 0xA0, 0xC9, 0x1E, 0xFB, 0x8B }
354 get_device_number (HDEVINFO device_info, SP_DEVINFO_DATA* device_info_data)
356 /* Find the Windows path to the device */
358 SP_DEVICE_INTERFACE_DATA device_interface_data;
359 device_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
361 BOOL r = SetupDiEnumDeviceInterfaces (device_info, device_info_data, &GUID_DEVICE_INTERFACE_DISK, 0, &device_interface_data);
363 LOG_DISK("SetupDiEnumDeviceInterfaces failed (%1)", GetLastError());
364 return optional<int>();
367 /* Find out how much space we need for our SP_DEVICE_INTERFACE_DETAIL_DATA_W */
369 r = SetupDiGetDeviceInterfaceDetailW(device_info, &device_interface_data, 0, 0, &size, 0);
370 PSP_DEVICE_INTERFACE_DETAIL_DATA_W device_detail_data = static_cast<PSP_DEVICE_INTERFACE_DETAIL_DATA_W> (malloc(size));
371 if (!device_detail_data) {
372 LOG_DISK_NC("malloc failed");
373 return optional<int>();
376 device_detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_W);
378 /* And get the path */
379 r = SetupDiGetDeviceInterfaceDetailW (device_info, &device_interface_data, device_detail_data, size, &size, 0);
381 LOG_DISK_NC("SetupDiGetDeviceInterfaceDetailW failed");
382 free (device_detail_data);
383 return optional<int>();
386 /* Open it. We would not be allowed GENERIC_READ access here but specifying 0 for
387 dwDesiredAccess allows us to query some metadata.
389 HANDLE device = CreateFileW (
390 device_detail_data->DevicePath, 0,
391 FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
395 free (device_detail_data);
397 if (device == INVALID_HANDLE_VALUE) {
398 LOG_DISK("CreateFileW failed with %1", GetLastError());
399 return optional<int>();
402 /* Get the device number */
403 STORAGE_DEVICE_NUMBER device_number;
404 r = DeviceIoControl (
405 device, IOCTL_STORAGE_GET_DEVICE_NUMBER, 0, 0,
406 &device_number, sizeof(device_number), &size, 0
409 CloseHandle (device);
412 return optional<int>();
415 return device_number.DeviceNumber;
418 /** Take a volume path (with a trailing \) and add any disk numbers related to that volume
422 add_volume_disk_number (wchar_t* volume, vector<int>& disks)
424 /* Strip trailing \ */
425 size_t const len = wcslen (volume);
426 DCPOMATIC_ASSERT (len > 0);
427 volume[len - 1] = L'\0';
429 HANDLE handle = CreateFileW (
431 FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
435 DCPOMATIC_ASSERT (handle != INVALID_HANDLE_VALUE);
437 VOLUME_DISK_EXTENTS extents;
439 BOOL r = DeviceIoControl (handle, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, 0, 0, &extents, sizeof(extents), &size, 0);
440 CloseHandle (handle);
444 DCPOMATIC_ASSERT (extents.NumberOfDiskExtents == 1);
445 return disks.push_back (extents.Extents[0].DiskNumber);
448 /* Return a list of disk numbers that contain volumes; i.e. a list of disk numbers that should
449 * not be offered as targets to write to as they are "mounted" (whatever that means on Windows).
452 disk_numbers_with_volumes ()
456 wchar_t volume_name[512];
457 HANDLE volume = FindFirstVolumeW (volume_name, sizeof(volume_name) / sizeof(wchar_t));
458 if (volume == INVALID_HANDLE_VALUE) {
462 add_volume_disk_number (volume_name, disks);
464 if (!FindNextVolumeW(volume, volume_name, sizeof(volume_name) / sizeof(wchar_t))) {
467 add_volume_disk_number (volume_name, disks);
469 FindVolumeClose (volume);
477 vector<Drive> drives;
479 vector<int> disks_to_ignore = disk_numbers_with_volumes ();
481 /* Get a `device information set' containing information about all disks */
482 HDEVINFO device_info = SetupDiGetClassDevsA (&GUID_DEVICE_INTERFACE_DISK, 0, 0, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
483 if (device_info == INVALID_HANDLE_VALUE) {
484 LOG_DISK_NC ("SetupDiClassDevsA failed");
490 /* Find out about the next disk */
491 SP_DEVINFO_DATA device_info_data;
492 device_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
493 if (!SetupDiEnumDeviceInfo(device_info, i, &device_info_data)) {
494 DWORD e = GetLastError();
495 if (e != ERROR_NO_MORE_ITEMS) {
496 LOG_DISK ("SetupDiEnumDeviceInfo failed (%1)", GetLastError());
502 optional<string> const friendly_name = get_friendly_name (device_info, &device_info_data);
503 optional<int> device_number = get_device_number (device_info, &device_info_data);
504 if (!device_number) {
508 string const physical_drive = String::compose("\\\\.\\PHYSICALDRIVE%1", *device_number);
510 HANDLE device = CreateFileA (
511 physical_drive.c_str(), 0,
512 FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
516 if (device == INVALID_HANDLE_VALUE) {
517 LOG_DISK_NC("Could not open PHYSICALDRIVE");
523 BOOL r = DeviceIoControl (
524 device, IOCTL_DISK_GET_DRIVE_GEOMETRY, 0, 0,
525 &geom, sizeof(geom), &returned, 0
528 if (r && find(disks_to_ignore.begin(), disks_to_ignore.end(), *device_number) == disks_to_ignore.end()) {
529 uint64_t const disk_size = geom.Cylinders.QuadPart * geom.TracksPerCylinder * geom.SectorsPerTrack * geom.BytesPerSector;
530 drives.push_back (Drive(physical_drive, disk_size, false, friendly_name, optional<string>()));
533 CloseHandle (device);
540 Drive::description () const
543 snprintf(gb, 64, "%.1f", _size / 1000000000.0);
550 if (name.size() > 0) {
551 name += " " + *_model;
554 if (name.size() == 0) {
558 return String::compose("%1 (%2 GB) [%3]", name, gb, _internal_name);
561 boost::filesystem::path
564 boost::filesystem::path p;
565 p /= g_get_user_config_dir ();