Trim spaces from vendor/model.
[dcpomatic.git] / src / lib / cross_osx.cc
1 /*
2     Copyright (C) 2012-2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "cross.h"
22 #include "compose.hpp"
23 #include "log.h"
24 #include "dcpomatic_log.h"
25 #include "config.h"
26 #include "exceptions.h"
27 #include <dcp/raw_convert.h>
28 #include <glib.h>
29 extern "C" {
30 #include <libavformat/avio.h>
31 }
32 #include <boost/algorithm/string.hpp>
33 #include <boost/foreach.hpp>
34 #include <boost/dll/runtime_symbol_info.hpp>
35 #include <boost/regex.hpp>
36 #include <sys/sysctl.h>
37 #include <mach-o/dyld.h>
38 #include <IOKit/pwr_mgt/IOPMLib.h>
39 #include <IOKit/storage/IOMedia.h>
40 #include <DiskArbitration/DADisk.h>
41 #include <DiskArbitration/DiskArbitration.h>
42 #include <sys/types.h>
43 #include <ifaddrs.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
46 #include <fstream>
47 #include <cstring>
48
49 #include "i18n.h"
50
51 using std::pair;
52 using std::list;
53 using std::ifstream;
54 using std::string;
55 using std::wstring;
56 using std::make_pair;
57 using std::vector;
58 using std::cerr;
59 using std::cout;
60 using std::runtime_error;
61 using boost::shared_ptr;
62 using boost::optional;
63
64 /** @param s Number of seconds to sleep for */
65 void
66 dcpomatic_sleep_seconds (int s)
67 {
68         sleep (s);
69 }
70
71 void
72 dcpomatic_sleep_milliseconds (int ms)
73 {
74         usleep (ms * 1000);
75 }
76
77 /** @return A string of CPU information (model name etc.) */
78 string
79 cpu_info ()
80 {
81         string info;
82
83         char buffer[64];
84         size_t N = sizeof (buffer);
85         if (sysctlbyname ("machdep.cpu.brand_string", buffer, &N, 0, 0) == 0) {
86                 info = buffer;
87         }
88
89         return info;
90 }
91
92 /** @return Path of the Contents directory in the .app */
93 boost::filesystem::path
94 app_contents ()
95 {
96         return boost::dll::program_location().parent_path().parent_path();
97 }
98
99 boost::filesystem::path
100 shared_path ()
101 {
102         return app_contents() / "Resources";
103 }
104
105 void
106 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out)
107 {
108         boost::filesystem::path path = app_contents();
109         path /= "MacOS";
110         path /= "ffprobe";
111
112         string ffprobe = "\"" + path.string() + "\" \"" + content.string() + "\" 2> \"" + out.string() + "\"";
113         LOG_GENERAL (N_("Probing with %1"), ffprobe);
114         system (ffprobe.c_str ());
115 }
116
117 list<pair<string, string> >
118 mount_info ()
119 {
120         list<pair<string, string> > m;
121         return m;
122 }
123
124 boost::filesystem::path
125 openssl_path ()
126 {
127         boost::filesystem::path path = app_contents();
128         path /= "MacOS";
129         path /= "openssl";
130         return path;
131 }
132
133 boost::filesystem::path
134 disk_writer_path ()
135 {
136         return "/Users/carl/dcpomatic/src/dcpomatic/build/src/tools/dcpomatic2_disk_writer";
137         boost::filesystem::path path = app_contents();
138         path /= "MacOS";
139         path /= "dcpomatic2_disk_writer";
140         return path;
141 }
142
143 /* Apparently there is no way to create an ofstream using a UTF-8
144    filename under Windows.  We are hence reduced to using fopen
145    with this wrapper.
146 */
147 FILE *
148 fopen_boost (boost::filesystem::path p, string t)
149 {
150         return fopen (p.c_str(), t.c_str ());
151 }
152
153 int
154 dcpomatic_fseek (FILE* stream, int64_t offset, int whence)
155 {
156         return fseek (stream, offset, whence);
157 }
158
159 void
160 Waker::nudge ()
161 {
162
163 }
164
165 Waker::Waker ()
166 {
167         boost::mutex::scoped_lock lm (_mutex);
168         /* We should use this */
169         // IOPMAssertionCreateWithName (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, CFSTR ("Encoding DCP"), &_assertion_id);
170         /* but it's not available on 10.5, so we use this */
171         IOPMAssertionCreate (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, &_assertion_id);
172 }
173
174 Waker::~Waker ()
175 {
176         boost::mutex::scoped_lock lm (_mutex);
177         IOPMAssertionRelease (_assertion_id);
178 }
179
180 void
181 start_tool (boost::filesystem::path dcpomatic, string executable, string app)
182 {
183         boost::filesystem::path batch = dcpomatic.parent_path ();
184         batch = batch.parent_path (); // MacOS
185         batch = batch.parent_path (); // Contents
186         batch = batch.parent_path (); // DCP-o-matic.app
187         batch = batch.parent_path (); // Applications
188         batch /= app;
189         batch /= "Contents";
190         batch /= "MacOS";
191         batch /= executable;
192
193         pid_t pid = fork ();
194         if (pid == 0) {
195                 int const r = system (batch.string().c_str());
196                 exit (WEXITSTATUS (r));
197         }
198 }
199
200 void
201 start_batch_converter (boost::filesystem::path dcpomatic)
202 {
203         start_tool (dcpomatic, "dcpomatic2_batch", "DCP-o-matic\\ 2\\ Batch\\ Converter.app");
204 }
205
206 void
207 start_player (boost::filesystem::path dcpomatic)
208 {
209         start_tool (dcpomatic, "dcpomatic2_player", "DCP-o-matic\\ 2\\ Player.app");
210 }
211
212 uint64_t
213 thread_id ()
214 {
215         return (uint64_t) pthread_self ();
216 }
217
218 int
219 avio_open_boost (AVIOContext** s, boost::filesystem::path file, int flags)
220 {
221         return avio_open (s, file.c_str(), flags);
222 }
223
224 boost::filesystem::path
225 home_directory ()
226 {
227                 return getenv("HOME");
228 }
229
230 string
231 command_and_read (string cmd)
232 {
233         return "";
234 }
235
236 /** @return true if this process is a 32-bit one running on a 64-bit-capable OS */
237 bool
238 running_32_on_64 ()
239 {
240         /* I'm assuming nobody does this on OS X */
241         return false;
242 }
243
244 static void
245 disk_appeared (DADiskRef disk, void* context)
246 {
247         const char* name = DADiskGetBSDName (disk);
248         if (!name) {
249                 return;
250         }
251
252         vector<Drive>* drives = reinterpret_cast<vector<Drive>*> (context);
253
254         CFDictionaryRef description = DADiskCopyDescription (disk);
255
256         optional<string> vendor;
257         void const* str = CFDictionaryGetValue (description, kDADiskDescriptionDeviceVendorKey);
258         if (str) {
259                 vendor = CFStringGetCStringPtr ((CFStringRef) str, kCFStringEncodingUTF8);
260                 vendor = boost::algorithm::trim (*vendor);
261         }
262
263         optional<string> model;
264         str = CFDictionaryGetValue (description, kDADiskDescriptionDeviceModelKey);
265         if (str) {
266                 model = CFStringGetCStringPtr ((CFStringRef) str, kCFStringEncodingUTF8);
267                 model = boost::algorithm::trim (*model);
268         }
269
270         str = CFDictionaryGetValue (description, kDADiskDescriptionMediaPathKey);
271         if (str) {
272                 char const* path = CFStringGetCStringPtr((CFStringRef) str, kCFStringEncodingUTF8);
273                 if (strncmp(path, "IODeviceTree:", 13) != 0) {
274                         return;
275                 }
276         }
277
278         io_service_t service = DADiskCopyIOMedia (disk);
279         CFTypeRef whole_media_ref = IORegistryEntryCreateCFProperty (service, CFSTR(kIOMediaWholeKey), kCFAllocatorDefault, 0);
280         bool whole_media = false;
281         if (whole_media_ref) {
282                 whole_media = CFBooleanGetValue((CFBooleanRef) whole_media_ref);
283                 CFRelease (whole_media_ref);
284         }
285         IOObjectRelease (service);
286         if (!whole_media) {
287                 return;
288         }
289
290         unsigned long size;
291         CFNumberGetValue ((CFNumberRef) CFDictionaryGetValue (description, kDADiskDescriptionMediaSizeKey), kCFNumberLongType, &size);
292
293         CFRelease (description);
294
295         drives->push_back (Drive(name, size, false, vendor, model));
296 }
297
298 vector<Drive>
299 get_drives ()
300 {
301         vector<Drive> drives;
302
303         DASessionRef session = DASessionCreate(kCFAllocatorDefault);
304         if (!session) {
305                 return drives;
306         }
307
308         DARegisterDiskAppearedCallback (session, NULL, disk_appeared, &drives);
309         CFRunLoopRef run_loop = CFRunLoopGetCurrent ();
310         DASessionScheduleWithRunLoop (session, run_loop, kCFRunLoopDefaultMode);
311         CFRunLoopStop (run_loop);
312         CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.05, 0);
313         DAUnregisterCallback(session, (void *) disk_appeared, &drives);
314         CFRelease(session);
315
316         return drives;
317 }
318
319 boost::filesystem::path
320 config_path ()
321 {
322         boost::filesystem::path p;
323         p /= g_get_home_dir ();
324         p /= "Library";
325         p /= "Preferences";
326         p /= "com.dcpomatic";
327         p /= "2";
328         return p;
329 }