summaryrefslogtreecommitdiff
path: root/src/lib/cross_osx.cc
blob: 2dc9b2702b006044f8d384e450f6877753ccc4c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/*
    Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    DCP-o-matic is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/


#include "cross.h"
#include "compose.hpp"
#include "log.h"
#include "dcpomatic_log.h"
#include "config.h"
#include "exceptions.h"
#include "warnings.h"
#include <dcp/raw_convert.h>
#include <glib.h>
extern "C" {
#include <libavformat/avio.h>
}
#include <boost/algorithm/string.hpp>
#include <boost/regex.hpp>
#if BOOST_VERSION >= 106100
#include <boost/dll/runtime_symbol_info.hpp>
#endif
#include <ApplicationServices/ApplicationServices.h>
#include <sys/sysctl.h>
#include <mach-o/dyld.h>
#include <IOKit/pwr_mgt/IOPMLib.h>
#include <IOKit/storage/IOMedia.h>
#include <DiskArbitration/DADisk.h>
#include <DiskArbitration/DiskArbitration.h>
#include <CoreFoundation/CFURL.h>
#include <sys/types.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fstream>
#include <cstring>

#include "i18n.h"


using std::pair;
using std::list;
using std::ifstream;
using std::string;
using std::wstring;
using std::make_pair;
using std::vector;
using std::cerr;
using std::cout;
using std::runtime_error;
using std::map;
using std::shared_ptr;
using boost::optional;
using std::function;


/** @param s Number of seconds to sleep for */
void
dcpomatic_sleep_seconds (int s)
{
	sleep (s);
}


void
dcpomatic_sleep_milliseconds (int ms)
{
	usleep (ms * 1000);
}


/** @return A string of CPU information (model name etc.) */
string
cpu_info ()
{
	string info;

	char buffer[64];
	size_t N = sizeof (buffer);
	if (sysctlbyname("machdep.cpu.brand_string", buffer, &N, 0, 0) == 0) {
		info = buffer;
	}

	return info;
}


boost::filesystem::path
directory_containing_executable ()
{
	return boost::filesystem::canonical(boost::dll::program_location()).parent_path();
}


boost::filesystem::path
resources_path ()
{
	return directory_containing_executable().parent_path() / "Resources";
}


boost::filesystem::path
xsd_path ()
{
	return resources_path() / "xsd";
}


boost::filesystem::path
tags_path ()
{
	return resources_path() / "tags";
}


void
run_ffprobe (boost::filesystem::path content, boost::filesystem::path out)
{
	auto path = directory_containing_executable () / "ffprobe";

	string ffprobe = "\"" + path.string() + "\" \"" + content.string() + "\" 2> \"" + out.string() + "\"";
	LOG_GENERAL (N_("Probing with %1"), ffprobe);
	system (ffprobe.c_str ());
}



list<pair<string, string>>
mount_info ()
{
	return {};
}


boost::filesystem::path
openssl_path ()
{
	return directory_containing_executable() / "openssl";
}


#ifdef DCPOMATIC_DISK
/* Note: this isn't actually used at the moment as the disk writer is started as a service */
boost::filesystem::path
disk_writer_path ()
{
	return directory_containing_executable() / "dcpomatic2_disk_writer";
}
#endif


/* Apparently there is no way to create an ofstream using a UTF-8
   filename under Windows.  We are hence reduced to using fopen
   with this wrapper.
*/
FILE *
fopen_boost (boost::filesystem::path p, string t)
{
        return fopen (p.c_str(), t.c_str());
}


int
dcpomatic_fseek (FILE* stream, int64_t offset, int whence)
{
	return fseek (stream, offset, whence);
}


void
Waker::nudge ()
{

}


Waker::Waker ()
{
	boost::mutex::scoped_lock lm (_mutex);
	IOPMAssertionCreateWithName (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, CFSTR ("Encoding DCP"), &_assertion_id);
}


Waker::~Waker ()
{
	boost::mutex::scoped_lock lm (_mutex);
	IOPMAssertionRelease (_assertion_id);
}


void
start_tool (string executable, string app)
{
	auto exe_path = directory_containing_executable();
	exe_path = exe_path.parent_path(); // Contents
	exe_path = exe_path.parent_path(); // DCP-o-matic 2.app
	exe_path = exe_path.parent_path(); // Applications
	exe_path /= app;
	exe_path /= "Contents";
	exe_path /= "MacOS";
	exe_path /= executable;

	pid_t pid = fork ();
	if (pid == 0) {
		LOG_GENERAL ("start_tool %1 %2 with path %3", executable, app, exe_path.string());
		int const r = system (exe_path.string().c_str());
		exit (WEXITSTATUS (r));
	} else if (pid == -1) {
		LOG_ERROR_NC("Fork failed in start_tool");
	}
}


void
start_batch_converter ()
{
	start_tool ("dcpomatic2_batch", "DCP-o-matic\\ 2\\ Batch\\ Converter.app");
}


void
start_player ()
{
	start_tool ("dcpomatic2_player", "DCP-o-matic\\ 2\\ Player.app");
}


uint64_t
thread_id ()
{
	return (uint64_t) pthread_self ();
}


int
avio_open_boost (AVIOContext** s, boost::filesystem::path file, int flags)
{
	return avio_open (s, file.c_str(), flags);
}


boost::filesystem::path
home_directory ()
{
	return getenv("HOME");
}


/** @return true if this process is a 32-bit one running on a 64-bit-capable OS */
bool
running_32_on_64 ()
{
	/* I'm assuming nobody does this on OS X */
	return false;
}


static optional<string>
get_vendor (CFDictionaryRef& description)
{
	void const* str = CFDictionaryGetValue (description, kDADiskDescriptionDeviceVendorKey);
	if (!str) {
		return {};
	}

	auto c_str = CFStringGetCStringPtr ((CFStringRef) str, kCFStringEncodingUTF8);
	if (!c_str) {
		return {};
	}

	string s (c_str);
	boost::algorithm::trim (s);
	return s;
}


static optional<string>
get_model (CFDictionaryRef& description)
{
	void const* str = CFDictionaryGetValue (description, kDADiskDescriptionDeviceModelKey);
	if (!str) {
		return {};
	}

	auto c_str = CFStringGetCStringPtr ((CFStringRef) str, kCFStringEncodingUTF8);
	if (!c_str) {
		return {};
	}

	string s (c_str);
	boost::algorithm::trim (s);
	return s;
}


static optional<OSXMediaPath>
analyse_media_path (CFDictionaryRef& description)
{
	using namespace boost::algorithm;

	void const* str = CFDictionaryGetValue (description, kDADiskDescriptionMediaPathKey);
	if (!str) {
		LOG_DISK_NC("There is no MediaPathKey (no dictionary value)");
		return {};
	}

	auto path_key_cstr = CFStringGetCStringPtr((CFStringRef) str, kCFStringEncodingUTF8);
	if (!path_key_cstr) {
		LOG_DISK_NC("There is no MediaPathKey (no cstring)");
		return {};
	}

	string path(path_key_cstr);
	LOG_DISK("MediaPathKey is %1", path);
	return analyse_osx_media_path (path);
}


static bool
is_whole_drive (DADiskRef& disk)
{
	io_service_t service = DADiskCopyIOMedia (disk);
        CFTypeRef whole_media_ref = IORegistryEntryCreateCFProperty (service, CFSTR(kIOMediaWholeKey), kCFAllocatorDefault, 0);
	bool whole_media = false;
        if (whole_media_ref) {
		whole_media = CFBooleanGetValue((CFBooleanRef) whole_media_ref);
                CFRelease (whole_media_ref);
        }
	IOObjectRelease (service);
	return whole_media;
}


static optional<boost::filesystem::path>
mount_point (CFDictionaryRef& description)
{
	auto volume_path_key = (CFURLRef) CFDictionaryGetValue (description, kDADiskDescriptionVolumePathKey);
	if (!volume_path_key) {
		return {};
	}

	char mount_path_buffer[1024];
	if (!CFURLGetFileSystemRepresentation(volume_path_key, false, (UInt8 *) mount_path_buffer, sizeof(mount_path_buffer))) {
		return {};
	}
	return boost::filesystem::path(mount_path_buffer);
}


/* Here follows some rather intricate and (probably) fragile code to find the list of available
 * "real" drives on macOS that we might want to write a DCP to.
 *
 * We use the Disk Arbitration framework to give us a series of mount_points (/dev/disk0, /dev/disk1,
 * /dev/disk1s1 and so on) and we use the API to gather useful information about these mount_points into
 * a vector of Disk structs.
 *
 * Then we read the Disks that we found and try to derive a list of drives that we should offer to the
 * user, with details of whether those drives are currently mounted or not.
 *
 * At the basic level we find the "disk"-level mount_points, looking at whether any of their partitions are mounted.
 *
 * This is complicated enormously by recent-ish macOS versions' habit of making `synthesized' volumes which
 * reflect data in `real' partitions.  So, for example, we might have a real (physical) drive /dev/disk2 with
 * a partition /dev/disk2s2 whose content is made into a synthesized /dev/disk3, itself containing some partitions
 * which are mounted.  /dev/disk2s2 is not considered to be mounted, in this case.  So we need to know that
 * disk2s2 is related to disk3 so we can consider disk2s2 as mounted if any parts of disk3 are.  In order to do
 * this I am taking the first two parts of the IODeviceTree and seeing if they exist anywhere in a
 * IOService identifier.  If they do, I am assuming the IOService device is on the matching IODeviceTree device.
 *
 * Lots of this is guesswork and may be broken.  In my defence the documentation that I have been able to
 * unearth is, to put it impolitely, crap.
 */

static void
disk_appeared (DADiskRef disk, void* context)
{
	auto bsd_name = DADiskGetBSDName (disk);
	if (!bsd_name) {
		LOG_DISK_NC("Disk with no BSDName appeared");
		return;
	}
	LOG_DISK("%1 appeared", bsd_name);

	OSXDisk this_disk;

	this_disk.device = string("/dev/") + bsd_name;
	LOG_DISK("Device is %1", this_disk.device);

	CFDictionaryRef description = DADiskCopyDescription (disk);

	this_disk.vendor = get_vendor (description);
	this_disk.model = get_model (description);
	LOG_DISK("Vendor/model: %1 %2", this_disk.vendor.get_value_or("[none]"), this_disk.model.get_value_or("[none]"));

	auto media_path = analyse_media_path (description);
	if (!media_path) {
		LOG_DISK("Finding media path for %1 failed", bsd_name);
		return;
	}

	this_disk.media_path = *media_path;
	this_disk.whole = is_whole_drive (disk);
	auto mp = mount_point (description);
	if (mp) {
		this_disk.mount_points.push_back (*mp);
	}

	LOG_DISK(
		"%1 %2 mounted at %3",
		 this_disk.media_path.real ? "Real" : "Synth",
		 this_disk.whole ? "whole" : "part",
		 mp ? mp->string() : "[nowhere]"
		);

	auto media_size_cstr = CFDictionaryGetValue (description, kDADiskDescriptionMediaSizeKey);
	if (!media_size_cstr) {
		LOG_DISK_NC("Could not read media size");
		return;
	}

	CFNumberGetValue ((CFNumberRef) media_size_cstr, kCFNumberLongType, &this_disk.size);
	CFRelease (description);

	reinterpret_cast<vector<OSXDisk>*>(context)->push_back(this_disk);
}


vector<Drive>
Drive::get ()
{
	using namespace boost::algorithm;
	vector<OSXDisk> disks;

	auto session = DASessionCreate(kCFAllocatorDefault);
	if (!session) {
		return {};
	}

	DARegisterDiskAppearedCallback (session, NULL, disk_appeared, &disks);
	auto run_loop = CFRunLoopGetCurrent ();
	DASessionScheduleWithRunLoop (session, run_loop, kCFRunLoopDefaultMode);
	CFRunLoopStop (run_loop);
	CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.05, 0);
	DAUnregisterCallback(session, (void *) disk_appeared, &disks);
	CFRelease(session);

	return osx_disks_to_drives (disks);
}


boost::filesystem::path
config_path (optional<string> version)
{
	boost::filesystem::path p;
	p /= g_get_home_dir ();
	p /= "Library";
	p /= "Preferences";
	p /= "com.dcpomatic";
	p /= "2";
	if (version) {
		p /= *version;
	}
	return p;
}


void done_callback(DADiskRef, DADissenterRef dissenter, void* context)
{
	LOG_DISK_NC("Unmount finished");
	bool* success = reinterpret_cast<bool*> (context);
	if (dissenter) {
		LOG_DISK("Error: %1", DADissenterGetStatus(dissenter));
		*success = false;
	} else {
		LOG_DISK_NC("Successful");
		*success = true;
	}
}


bool
Drive::unmount ()
{
	LOG_DISK_NC("Unmount operation started");

	auto session = DASessionCreate(kCFAllocatorDefault);
	if (!session) {
		return false;
	}

	auto disk = DADiskCreateFromBSDName(kCFAllocatorDefault, session, _device.c_str());
	if (!disk) {
		return false;
	}
	LOG_DISK("Requesting unmount of %1 from %2", _device, thread_id());
	bool success = false;
	DADiskUnmount(disk, kDADiskUnmountOptionWhole, &done_callback, &success);
	CFRelease (disk);

	CFRunLoopRef run_loop = CFRunLoopGetCurrent ();
	DASessionScheduleWithRunLoop (session, run_loop, kCFRunLoopDefaultMode);
	CFRunLoopStop (run_loop);
	CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.5, 0);
	CFRelease(session);

	LOG_DISK_NC("End of unmount");
	return success;
}


void
disk_write_finished ()
{

}


void
make_foreground_application ()
{
	ProcessSerialNumber serial;
DCPOMATIC_DISABLE_WARNINGS
	GetCurrentProcess (&serial);
DCPOMATIC_ENABLE_WARNINGS
	TransformProcessType (&serial, kProcessTransformToForegroundApplication);
}


string
dcpomatic::get_process_id ()
{
	return dcp::raw_convert<string>(getpid());
}


boost::filesystem::path
fix_long_path (boost::filesystem::path path)
{
	return path;
}


bool
show_in_file_manager (boost::filesystem::path, boost::filesystem::path select)
{
	int r = system (String::compose("open -R \"%1\"", select.string()).c_str());
	return static_cast<bool>(WEXITSTATUS(r));
}