Improve ratings dialog to allow only valid values (#2199).
[dcpomatic.git] / src / lib / cross_linux.cc
1 /*
2     Copyright (C) 2012-2021 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
22 #include "compose.hpp"
23 #include "config.h"
24 #include "cross.h"
25 #include "dcpomatic_log.h"
26 #include "dcpomatic_log.h"
27 #include "exceptions.h"
28 #include "log.h"
29 #include "warnings.h"
30 #include <dcp/raw_convert.h>
31 #include <glib.h>
32 DCPOMATIC_DISABLE_WARNINGS
33 extern "C" {
34 #include <libavformat/avio.h>
35 }
36 DCPOMATIC_ENABLE_WARNINGS
37 #include <boost/algorithm/string.hpp>
38 #if BOOST_VERSION >= 106100
39 #include <boost/dll/runtime_symbol_info.hpp>
40 #endif
41 #include <unistd.h>
42 #include <mntent.h>
43 #include <sys/types.h>
44 #include <sys/mount.h>
45 #include <ifaddrs.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48 #include <fstream>
49
50 #include "i18n.h"
51
52
53 using std::cerr;
54 using std::cout;
55 using std::ifstream;
56 using std::list;
57 using std::make_pair;
58 using std::pair;
59 using std::string;
60 using std::vector;
61 using boost::optional;
62
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
72 void
73 dcpomatic_sleep_milliseconds (int ms)
74 {
75         usleep (ms * 1000);
76 }
77
78
79 /** @return A string of CPU information (model name etc.) */
80 string
81 cpu_info ()
82 {
83         string info;
84
85         /* This use of ifstream is ok; the filename can never
86            be non-Latin
87         */
88         ifstream f ("/proc/cpuinfo");
89         while (f.good ()) {
90                 string l;
91                 getline (f, l);
92                 if (boost::algorithm::starts_with (l, "model name")) {
93                         string::size_type const c = l.find (':');
94                         if (c != string::npos) {
95                                 info = l.substr (c + 2);
96                         }
97                 }
98         }
99
100         return info;
101 }
102
103
104 boost::filesystem::path
105 resources_path ()
106 {
107         return directory_containing_executable().parent_path() / "share" / "dcpomatic2";
108 }
109
110
111 boost::filesystem::path
112 libdcp_resources_path ()
113 {
114         if (auto appdir = getenv("APPDIR")) {
115                 return boost::filesystem::path(appdir) / "usr" / "share" / "libdcp";
116         }
117         return boost::filesystem::canonical(LINUX_SHARE_PREFIX) / "libdcp";
118 }
119
120
121 void
122 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out)
123 {
124         string ffprobe = "ffprobe \"" + content.string() + "\" 2> \"" + out.string() + "\"";
125         LOG_GENERAL (N_("Probing with %1"), ffprobe);
126         int const r = system (ffprobe.c_str());
127         if (r == -1 || (WIFEXITED(r) && WEXITSTATUS(r) != 0)) {
128                 LOG_GENERAL (N_("Could not run ffprobe (system returned %1"), r);
129         }
130 }
131
132
133 list<pair<string, string>>
134 mount_info ()
135 {
136         list<pair<string, string>> m;
137
138         auto f = setmntent ("/etc/mtab", "r");
139         if (!f) {
140                 return m;
141         }
142
143         while (true) {
144                 struct mntent* mnt = getmntent (f);
145                 if (!mnt) {
146                         break;
147                 }
148
149                 m.push_back (make_pair (mnt->mnt_dir, mnt->mnt_type));
150         }
151
152         endmntent (f);
153
154         return m;
155 }
156
157
158 boost::filesystem::path
159 directory_containing_executable ()
160 {
161 #if BOOST_VERSION >= 106100
162         return boost::dll::program_location().parent_path();
163 #else
164         char buffer[PATH_MAX];
165         ssize_t N = readlink ("/proc/self/exe", buffer, PATH_MAX);
166         return boost::filesystem::path(string(buffer, N)).parent_path();
167 #endif
168 }
169
170
171 boost::filesystem::path
172 openssl_path ()
173 {
174         auto p = directory_containing_executable() / "dcpomatic2_openssl";
175         if (boost::filesystem::is_regular_file(p)) {
176                 return p;
177         }
178
179         return "dcpomatic2_openssl";
180 }
181
182
183 #ifdef DCPOMATIC_DISK
184 boost::filesystem::path
185 disk_writer_path ()
186 {
187         return directory_containing_executable() / "dcpomatic2_disk_writer";
188 }
189 #endif
190
191
192 /* Apparently there is no way to create an ofstream using a UTF-8
193    filename under Windows.  We are hence reduced to using fopen
194    with this wrapper.
195 */
196 FILE *
197 fopen_boost (boost::filesystem::path p, string t)
198 {
199         return fopen(p.c_str(), t.c_str());
200 }
201
202
203 int
204 dcpomatic_fseek (FILE* stream, int64_t offset, int whence)
205 {
206         return fseek (stream, offset, whence);
207 }
208
209
210 void
211 Waker::nudge ()
212 {
213
214 }
215
216
217 Waker::Waker ()
218 {
219
220 }
221
222
223 Waker::~Waker ()
224 {
225
226 }
227
228
229 void
230 start_tool (string executable)
231 {
232         auto batch = directory_containing_executable() / executable;
233
234         pid_t pid = fork ();
235         if (pid == 0) {
236                 int const r = system (batch.string().c_str());
237                 exit (WEXITSTATUS (r));
238         }
239 }
240
241
242 void
243 start_batch_converter ()
244 {
245         start_tool ("dcpomatic2_batch");
246 }
247
248
249 void
250 start_player ()
251 {
252         start_tool ("dcpomatic2_player");
253 }
254
255
256 uint64_t
257 thread_id ()
258 {
259         return (uint64_t) pthread_self ();
260 }
261
262
263 int
264 avio_open_boost (AVIOContext** s, boost::filesystem::path file, int flags)
265 {
266         return avio_open (s, file.c_str(), flags);
267 }
268
269
270 boost::filesystem::path
271 home_directory ()
272 {
273         return getenv("HOME");
274 }
275
276
277 /** @return true if this process is a 32-bit one running on a 64-bit-capable OS */
278 bool
279 running_32_on_64 ()
280 {
281         /* I'm assuming nobody does this on Linux */
282         return false;
283 }
284
285
286 static
287 vector<pair<string, string>>
288 get_mounts (string prefix)
289 {
290         vector<pair<string, string>> mounts;
291
292         std::ifstream f("/proc/mounts");
293         string line;
294         while (f.good()) {
295                 getline(f, line);
296                 vector<string> bits;
297                 boost::algorithm::split (bits, line, boost::is_any_of(" "));
298                 if (bits.size() > 1 && boost::algorithm::starts_with(bits[0], prefix)) {
299                         boost::algorithm::replace_all (bits[1], "\\040", " ");
300                         mounts.push_back(make_pair(bits[0], bits[1]));
301                         LOG_DISK("Found mounted device %1 from prefix %2", bits[0], prefix);
302                 }
303         }
304
305         return mounts;
306 }
307
308
309 vector<Drive>
310 Drive::get ()
311 {
312         vector<Drive> drives;
313
314         using namespace boost::filesystem;
315         auto mounted_devices = get_mounts("/dev/");
316
317         for (auto i: directory_iterator("/sys/block")) {
318                 string const name = i.path().filename().string();
319                 path device_type_file("/sys/block/" + name + "/device/type");
320                 optional<string> device_type;
321                 if (exists(device_type_file)) {
322                         device_type = dcp::file_to_string (device_type_file);
323                         boost::trim(*device_type);
324                 }
325                 /* Device type 5 is "SCSI_TYPE_ROM" in blkdev.h; seems usually to be a CD/DVD drive */
326                 if (!boost::algorithm::starts_with(name, "loop") && (!device_type || *device_type != "5")) {
327                         uint64_t const size = dcp::raw_convert<uint64_t>(dcp::file_to_string(i / "size")) * 512;
328                         if (size == 0) {
329                                 continue;
330                         }
331                         optional<string> vendor;
332                         try {
333                                 vendor = dcp::file_to_string("/sys/block/" + name + "/device/vendor");
334                                 boost::trim(*vendor);
335                         } catch (...) {}
336                         optional<string> model;
337                         try {
338                                 model = dcp::file_to_string("/sys/block/" + name + "/device/model");
339                                 boost::trim(*model);
340                         } catch (...) {}
341                         vector<boost::filesystem::path> mount_points;
342                         for (auto const& j: mounted_devices) {
343                                 if (boost::algorithm::starts_with(j.first, "/dev/" + name)) {
344                                         mount_points.push_back (j.second);
345                                 }
346                         }
347                         drives.push_back(Drive("/dev/" + name, mount_points, size, vendor, model));
348                         LOG_DISK_NC(drives.back().log_summary());
349                 }
350         }
351
352         return drives;
353 }
354
355
356 bool
357 Drive::unmount ()
358 {
359         for (auto i: _mount_points) {
360                 int const r = umount(i.string().c_str());
361                 LOG_DISK("Tried to unmount %1 and got %2 and %3", i.string(), r, errno);
362                 if (r == -1) {
363                         return false;
364                 }
365         }
366         return true;
367 }
368
369
370 boost::filesystem::path
371 config_path (optional<string> version)
372 {
373         boost::filesystem::path p;
374         p /= g_get_user_config_dir ();
375         p /= "dcpomatic2";
376         if (version) {
377                 p /= *version;
378         }
379         return p;
380 }
381
382
383 void
384 disk_write_finished ()
385 {
386
387 }
388
389
390 string
391 dcpomatic::get_process_id ()
392 {
393         return dcp::raw_convert<string>(getpid());
394 }
395
396
397 boost::filesystem::path
398 fix_long_path (boost::filesystem::path path)
399 {
400         return path;
401 }
402
403
404 bool
405 show_in_file_manager (boost::filesystem::path dir, boost::filesystem::path)
406 {
407         int r = system ("which nautilus");
408         if (WEXITSTATUS(r) == 0) {
409                 r = system (String::compose("nautilus \"%1\"", dir.string()).c_str());
410                 return static_cast<bool>(WEXITSTATUS(r));
411         } else {
412                 int r = system ("which konqueror");
413                 if (WEXITSTATUS(r) == 0) {
414                         r = system (String::compose("konqueror \"%1\"", dir.string()).c_str());
415                         return static_cast<bool>(WEXITSTATUS(r));
416                 }
417         }
418
419         return true;
420 }
421