Allow + in DCP names for ISDCF RU rating strings.
[dcpomatic.git] / src / lib / util.cc
1 /*
2     Copyright (C) 2012-2016 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 /** @file src/lib/util.cc
22  *  @brief Some utility functions and classes.
23  */
24
25 #include "util.h"
26 #include "exceptions.h"
27 #include "dcp_content_type.h"
28 #include "filter.h"
29 #include "cinema_sound_processor.h"
30 #include "config.h"
31 #include "ratio.h"
32 #include "job.h"
33 #include "cross.h"
34 #include "video_content.h"
35 #include "rect.h"
36 #include "digester.h"
37 #include "audio_processor.h"
38 #include "compose.hpp"
39 #include "audio_buffers.h"
40 #include <dcp/locale_convert.h>
41 #include <dcp/util.h>
42 #include <dcp/raw_convert.h>
43 #include <dcp/picture_asset.h>
44 #include <dcp/sound_asset.h>
45 #include <dcp/subtitle_asset.h>
46 extern "C" {
47 #include <libavfilter/avfilter.h>
48 #include <libavformat/avformat.h>
49 #include <libavcodec/avcodec.h>
50 }
51 #include <curl/curl.h>
52 #ifdef DCPOMATIC_GRAPHICS_MAGICK
53 #include <Magick++.h>
54 #endif
55 #include <glib.h>
56 #include <pangomm/init.h>
57 #include <boost/algorithm/string.hpp>
58 #include <boost/range/algorithm/replace_if.hpp>
59 #include <boost/thread.hpp>
60 #include <boost/filesystem.hpp>
61 #ifdef DCPOMATIC_WINDOWS
62 #include <boost/locale.hpp>
63 #include <dbghelp.h>
64 #endif
65 #include <signal.h>
66 #include <iomanip>
67 #include <iostream>
68 #include <fstream>
69 #include <climits>
70 #include <stdexcept>
71 #ifdef DCPOMATIC_POSIX
72 #include <execinfo.h>
73 #include <cxxabi.h>
74 #endif
75
76 #include "i18n.h"
77
78 using std::string;
79 using std::setfill;
80 using std::ostream;
81 using std::endl;
82 using std::vector;
83 using std::min;
84 using std::max;
85 using std::map;
86 using std::list;
87 using std::multimap;
88 using std::istream;
89 using std::pair;
90 using std::cout;
91 using std::bad_alloc;
92 using std::set_terminate;
93 using std::make_pair;
94 using boost::shared_ptr;
95 using boost::thread;
96 using boost::optional;
97 using boost::lexical_cast;
98 using boost::bad_lexical_cast;
99 using dcp::Size;
100 using dcp::raw_convert;
101 using dcp::locale_convert;
102
103 /** Path to our executable, required by the stacktrace stuff and filled
104  *  in during App::onInit().
105  */
106 string program_name;
107 bool is_batch_converter = false;
108 static boost::thread::id ui_thread;
109 static boost::filesystem::path backtrace_file;
110
111 /** Convert some number of seconds to a string representation
112  *  in hours, minutes and seconds.
113  *
114  *  @param s Seconds.
115  *  @return String of the form H:M:S (where H is hours, M
116  *  is minutes and S is seconds).
117  */
118 string
119 seconds_to_hms (int s)
120 {
121         int m = s / 60;
122         s -= (m * 60);
123         int h = m / 60;
124         m -= (h * 60);
125
126         char buffer[64];
127         snprintf (buffer, sizeof(buffer), "%d:%02d:%02d", h, m, s);
128         return buffer;
129 }
130
131 string
132 time_to_hmsf (DCPTime time, Frame rate)
133 {
134         Frame f = time.frames_round (rate);
135         int s = f / rate;
136         f -= (s * rate);
137         int m = s / 60;
138         s -= m * 60;
139         int h = m / 60;
140         m -= h * 60;
141
142         char buffer[64];
143         snprintf (buffer, sizeof(buffer), "%d:%02d:%02d.%d", h, m, s, static_cast<int>(f));
144         return buffer;
145 }
146
147 /** @param s Number of seconds.
148  *  @return String containing an approximate description of s (e.g. "about 2 hours")
149  */
150 string
151 seconds_to_approximate_hms (int s)
152 {
153         int m = s / 60;
154         s -= (m * 60);
155         int h = m / 60;
156         m -= (h * 60);
157
158         string ap;
159
160         bool const hours = h > 0;
161         bool const minutes = h < 6 && m > 0;
162         bool const seconds = h == 0 && m < 10 && s > 0;
163
164         if (hours) {
165                 if (m > 30 && !minutes) {
166                         /// TRANSLATORS: h here is an abbreviation for hours
167                         ap += locale_convert<string>(h + 1) + _("h");
168                 } else {
169                         /// TRANSLATORS: h here is an abbreviation for hours
170                         ap += locale_convert<string>(h) + _("h");
171                 }
172
173                 if (minutes || seconds) {
174                         ap += N_(" ");
175                 }
176         }
177
178         if (minutes) {
179                 /* Minutes */
180                 if (s > 30 && !seconds) {
181                         /// TRANSLATORS: m here is an abbreviation for minutes
182                         ap += locale_convert<string>(m + 1) + _("m");
183                 } else {
184                         /// TRANSLATORS: m here is an abbreviation for minutes
185                         ap += locale_convert<string>(m) + _("m");
186                 }
187
188                 if (seconds) {
189                         ap += N_(" ");
190                 }
191         }
192
193         if (seconds) {
194                 /* Seconds */
195                 /// TRANSLATORS: s here is an abbreviation for seconds
196                 ap += locale_convert<string>(s) + _("s");
197         }
198
199         return ap;
200 }
201
202 double
203 seconds (struct timeval t)
204 {
205         return t.tv_sec + (double (t.tv_usec) / 1e6);
206 }
207
208 #ifdef DCPOMATIC_WINDOWS
209
210 /** Resolve symbol name and source location given the path to the executable */
211 int
212 addr2line (void const * const addr)
213 {
214         char addr2line_cmd[512] = { 0 };
215         sprintf (addr2line_cmd, "addr2line -f -p -e %.256s %p > %s", program_name.c_str(), addr, backtrace_file.string().c_str());
216         return system(addr2line_cmd);
217 }
218
219 /** This is called when C signals occur on Windows (e.g. SIGSEGV)
220  *  (NOT C++ exceptions!).  We write a backtrace to backtrace_file by dark means.
221  *  Adapted from code here: http://spin.atomicobject.com/2013/01/13/exceptions-stack-traces-c/
222  */
223 LONG WINAPI
224 exception_handler(struct _EXCEPTION_POINTERS * info)
225 {
226         FILE* f = fopen_boost (backtrace_file, "w");
227         fprintf (f, "C-style exception %d\n", info->ExceptionRecord->ExceptionCode);
228         fclose(f);
229
230         if (info->ExceptionRecord->ExceptionCode != EXCEPTION_STACK_OVERFLOW) {
231                 CONTEXT* context = info->ContextRecord;
232                 SymInitialize (GetCurrentProcess (), 0, true);
233
234                 STACKFRAME frame = { 0 };
235
236                 /* setup initial stack frame */
237 #if _WIN64
238                 frame.AddrPC.Offset    = context->Rip;
239                 frame.AddrStack.Offset = context->Rsp;
240                 frame.AddrFrame.Offset = context->Rbp;
241 #else
242                 frame.AddrPC.Offset    = context->Eip;
243                 frame.AddrStack.Offset = context->Esp;
244                 frame.AddrFrame.Offset = context->Ebp;
245 #endif
246                 frame.AddrPC.Mode      = AddrModeFlat;
247                 frame.AddrStack.Mode   = AddrModeFlat;
248                 frame.AddrFrame.Mode   = AddrModeFlat;
249
250                 while (
251                         StackWalk (
252                                 IMAGE_FILE_MACHINE_I386,
253                                 GetCurrentProcess (),
254                                 GetCurrentThread (),
255                                 &frame,
256                                 context,
257                                 0,
258                                 SymFunctionTableAccess,
259                                 SymGetModuleBase,
260                                 0
261                                 )
262                         ) {
263                         addr2line((void *) frame.AddrPC.Offset);
264                 }
265         } else {
266 #ifdef _WIN64
267                 addr2line ((void *) info->ContextRecord->Rip);
268 #else
269                 addr2line ((void *) info->ContextRecord->Eip);
270 #endif
271         }
272
273         return EXCEPTION_CONTINUE_SEARCH;
274 }
275 #endif
276
277 void
278 set_backtrace_file (boost::filesystem::path p)
279 {
280         backtrace_file = p;
281 }
282
283 /** This is called when there is an unhandled exception.  Any
284  *  backtrace in this function is useless on Windows as the stack has
285  *  already been unwound from the throw; we have the gdb wrap hack to
286  *  cope with that.
287  */
288 void
289 terminate ()
290 {
291         try {
292                 static bool tried_throw = false;
293                 // try once to re-throw currently active exception
294                 if (!tried_throw) {
295                         tried_throw = true;
296                         throw;
297                 }
298         }
299         catch (const std::exception &e) {
300                 std::cerr << __FUNCTION__ << " caught unhandled exception. what(): "
301                           << e.what() << std::endl;
302         }
303         catch (...) {
304                 std::cerr << __FUNCTION__ << " caught unknown/unhandled exception."
305                           << std::endl;
306         }
307
308         abort();
309 }
310
311 void
312 dcpomatic_setup_path_encoding ()
313 {
314 #ifdef DCPOMATIC_WINDOWS
315         /* Dark voodoo which, I think, gets boost::filesystem::path to
316            correctly convert UTF-8 strings to paths, and also paths
317            back to UTF-8 strings (on path::string()).
318
319            After this, constructing boost::filesystem::paths from strings
320            converts from UTF-8 to UTF-16 inside the path.  Then
321            path::string().c_str() gives UTF-8 and
322            path::c_str()          gives UTF-16.
323
324            This is all Windows-only.  AFAICT Linux/OS X use UTF-8 everywhere,
325            so things are much simpler.
326         */
327         std::locale::global (boost::locale::generator().generate (""));
328         boost::filesystem::path::imbue (std::locale ());
329 #endif
330 }
331
332 /** Call the required functions to set up DCP-o-matic's static arrays, etc.
333  *  Must be called from the UI thread, if there is one.
334  */
335 void
336 dcpomatic_setup ()
337 {
338 #ifdef DCPOMATIC_WINDOWS
339         boost::filesystem::path p = g_get_user_config_dir ();
340         p /= "backtrace.txt";
341         set_backtrace_file (p);
342         SetUnhandledExceptionFilter(exception_handler);
343 #endif
344
345         av_register_all ();
346         avfilter_register_all ();
347
348 #ifdef DCPOMATIC_OSX
349         /* Add our library directory to the libltdl search path so that
350            xmlsec can find xmlsec1-openssl.
351         */
352         boost::filesystem::path lib = app_contents ();
353         lib /= "Frameworks";
354         setenv ("LTDL_LIBRARY_PATH", lib.c_str (), 1);
355 #endif
356
357         set_terminate (terminate);
358
359         Pango::init ();
360         dcp::init ();
361
362         Ratio::setup_ratios ();
363         PresetColourConversion::setup_colour_conversion_presets ();
364         VideoContentScale::setup_scales ();
365         DCPContentType::setup_dcp_content_types ();
366         Filter::setup_filters ();
367         CinemaSoundProcessor::setup_cinema_sound_processors ();
368         AudioProcessor::setup_audio_processors ();
369
370         curl_global_init (CURL_GLOBAL_ALL);
371
372 #ifdef DCPOMATIC_GRAPHICS_MAGICK
373         Magick::InitializeMagick (0);
374 #endif
375
376         ui_thread = boost::this_thread::get_id ();
377 }
378
379 #ifdef DCPOMATIC_WINDOWS
380 boost::filesystem::path
381 mo_path ()
382 {
383         wchar_t buffer[512];
384         GetModuleFileName (0, buffer, 512 * sizeof(wchar_t));
385         boost::filesystem::path p (buffer);
386         p = p.parent_path ();
387         p = p.parent_path ();
388         p /= "locale";
389         return p;
390 }
391 #endif
392
393 #ifdef DCPOMATIC_OSX
394 boost::filesystem::path
395 mo_path ()
396 {
397         return "DCP-o-matic 2.app/Contents/Resources";
398 }
399 #endif
400
401 void
402 dcpomatic_setup_gettext_i18n (string lang)
403 {
404 #ifdef DCPOMATIC_LINUX
405         lang += ".UTF8";
406 #endif
407
408         if (!lang.empty ()) {
409                 /* Override our environment language.  Note that the caller must not
410                    free the string passed into putenv().
411                 */
412                 string s = String::compose ("LANGUAGE=%1", lang);
413                 putenv (strdup (s.c_str ()));
414                 s = String::compose ("LANG=%1", lang);
415                 putenv (strdup (s.c_str ()));
416                 s = String::compose ("LC_ALL=%1", lang);
417                 putenv (strdup (s.c_str ()));
418         }
419
420         setlocale (LC_ALL, "");
421         textdomain ("libdcpomatic2");
422
423 #if defined(DCPOMATIC_WINDOWS) || defined(DCPOMATIC_OSX)
424         bindtextdomain ("libdcpomatic2", mo_path().string().c_str());
425         bind_textdomain_codeset ("libdcpomatic2", "UTF8");
426 #endif
427
428 #ifdef DCPOMATIC_LINUX
429         bindtextdomain ("libdcpomatic2", LINUX_LOCALE_PREFIX);
430 #endif
431 }
432
433 /** Compute a digest of the first and last `size' bytes of a set of files. */
434 string
435 digest_head_tail (vector<boost::filesystem::path> files, boost::uintmax_t size)
436 {
437         boost::scoped_array<char> buffer (new char[size]);
438         Digester digester;
439
440         /* Head */
441         boost::uintmax_t to_do = size;
442         char* p = buffer.get ();
443         int i = 0;
444         while (i < int64_t (files.size()) && to_do > 0) {
445                 FILE* f = fopen_boost (files[i], "rb");
446                 if (!f) {
447                         throw OpenFileError (files[i].string(), errno, true);
448                 }
449
450                 boost::uintmax_t this_time = min (to_do, boost::filesystem::file_size (files[i]));
451                 fread (p, 1, this_time, f);
452                 p += this_time;
453                 to_do -= this_time;
454                 fclose (f);
455
456                 ++i;
457         }
458         digester.add (buffer.get(), size - to_do);
459
460         /* Tail */
461         to_do = size;
462         p = buffer.get ();
463         i = files.size() - 1;
464         while (i >= 0 && to_do > 0) {
465                 FILE* f = fopen_boost (files[i], "rb");
466                 if (!f) {
467                         throw OpenFileError (files[i].string(), errno, true);
468                 }
469
470                 boost::uintmax_t this_time = min (to_do, boost::filesystem::file_size (files[i]));
471                 dcpomatic_fseek (f, -this_time, SEEK_END);
472                 fread (p, 1, this_time, f);
473                 p += this_time;
474                 to_do -= this_time;
475                 fclose (f);
476
477                 --i;
478         }
479         digester.add (buffer.get(), size - to_do);
480
481         return digester.get ();
482 }
483
484 /** Round a number up to the nearest multiple of another number.
485  *  @param c Index.
486  *  @param stride Array of numbers to round, indexed by c.
487  *  @param t Multiple to round to.
488  *  @return Rounded number.
489  */
490 int
491 stride_round_up (int c, int const * stride, int t)
492 {
493         int const a = stride[c] + (t - 1);
494         return a - (a % t);
495 }
496
497 /** Trip an assert if the caller is not in the UI thread */
498 void
499 ensure_ui_thread ()
500 {
501         DCPOMATIC_ASSERT (boost::this_thread::get_id() == ui_thread);
502 }
503
504 string
505 audio_channel_name (int c)
506 {
507         DCPOMATIC_ASSERT (MAX_DCP_AUDIO_CHANNELS == 16);
508
509         /// TRANSLATORS: these are the names of audio channels; Lfe (sub) is the low-frequency
510         /// enhancement channel (sub-woofer).
511         string const channels[] = {
512                 _("Left"),
513                 _("Right"),
514                 _("Centre"),
515                 _("Lfe (sub)"),
516                 _("Left surround"),
517                 _("Right surround"),
518                 _("Hearing impaired"),
519                 _("Visually impaired"),
520                 _("Left centre"),
521                 _("Right centre"),
522                 _("Left rear surround"),
523                 _("Right rear surround"),
524                 _("D-BOX primary"),
525                 _("D-BOX secondary"),
526                 _("Unused"),
527                 _("Unused")
528         };
529
530         return channels[c];
531 }
532
533 string
534 short_audio_channel_name (int c)
535 {
536         DCPOMATIC_ASSERT (MAX_DCP_AUDIO_CHANNELS == 16);
537
538         /// TRANSLATORS: these are short names of audio channels; Lfe is the low-frequency
539         /// enhancement channel (sub-woofer).  HI is the hearing-impaired audio track and
540         /// VI is the visually-impaired audio track (audio describe).  DBP is the D-BOX
541         /// primary channel and DBS is the D-BOX secondary channel.
542         string const channels[] = {
543                 _("L"),
544                 _("R"),
545                 _("C"),
546                 _("Lfe"),
547                 _("Ls"),
548                 _("Rs"),
549                 _("HI"),
550                 _("VI"),
551                 _("Lc"),
552                 _("Rc"),
553                 _("BsL"),
554                 _("BsR"),
555                 _("DBP"),
556                 _("DBS"),
557                 "",
558                 ""
559         };
560
561         return channels[c];
562 }
563
564
565 bool
566 valid_image_file (boost::filesystem::path f)
567 {
568         if (boost::starts_with (f.leaf().string(), "._")) {
569                 return false;
570         }
571
572         string ext = f.extension().string();
573         transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
574         return (
575                 ext == ".tif" || ext == ".tiff" || ext == ".jpg" || ext == ".jpeg" ||
576                 ext == ".png" || ext == ".bmp" || ext == ".tga" || ext == ".dpx" ||
577                 ext == ".j2c" || ext == ".j2k" || ext == ".jp2" || ext == ".exr"
578                 );
579 }
580
581 bool
582 valid_sound_file (boost::filesystem::path f)
583 {
584         if (boost::starts_with (f.leaf().string(), "._")) {
585                 return false;
586         }
587
588         string ext = f.extension().string();
589         transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
590         return (ext == ".wav" || ext == ".mp3" || ext == ".aif" || ext == ".aiff");
591 }
592
593 bool
594 valid_j2k_file (boost::filesystem::path f)
595 {
596         string ext = f.extension().string();
597         transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
598         return (ext == ".j2k" || ext == ".j2c" || ext == ".jp2");
599 }
600
601 string
602 tidy_for_filename (string f)
603 {
604         boost::replace_if (f, boost::is_any_of ("\\/:"), '_');
605         return f;
606 }
607
608 dcp::Size
609 fit_ratio_within (float ratio, dcp::Size full_frame)
610 {
611         if (ratio < full_frame.ratio ()) {
612                 return dcp::Size (lrintf (full_frame.height * ratio), full_frame.height);
613         }
614
615         return dcp::Size (full_frame.width, lrintf (full_frame.width / ratio));
616 }
617
618 void *
619 wrapped_av_malloc (size_t s)
620 {
621         void* p = av_malloc (s);
622         if (!p) {
623                 throw bad_alloc ();
624         }
625         return p;
626 }
627
628 map<string, string>
629 split_get_request (string url)
630 {
631         enum {
632                 AWAITING_QUESTION_MARK,
633                 KEY,
634                 VALUE
635         } state = AWAITING_QUESTION_MARK;
636
637         map<string, string> r;
638         string k;
639         string v;
640         for (size_t i = 0; i < url.length(); ++i) {
641                 switch (state) {
642                 case AWAITING_QUESTION_MARK:
643                         if (url[i] == '?') {
644                                 state = KEY;
645                         }
646                         break;
647                 case KEY:
648                         if (url[i] == '=') {
649                                 v.clear ();
650                                 state = VALUE;
651                         } else {
652                                 k += url[i];
653                         }
654                         break;
655                 case VALUE:
656                         if (url[i] == '&') {
657                                 r.insert (make_pair (k, v));
658                                 k.clear ();
659                                 state = KEY;
660                         } else {
661                                 v += url[i];
662                         }
663                         break;
664                 }
665         }
666
667         if (state == VALUE) {
668                 r.insert (make_pair (k, v));
669         }
670
671         return r;
672 }
673
674 string
675 video_asset_filename (shared_ptr<dcp::PictureAsset> asset, int reel_index, int reel_count, optional<string> summary)
676 {
677         dcp::NameFormat::Map values;
678         values['t'] = "j2c";
679         values['r'] = raw_convert<string> (reel_index + 1);
680         values['n'] = raw_convert<string> (reel_count);
681         if (summary) {
682                 values['c'] = careful_string_filter (summary.get());
683         }
684         return Config::instance()->dcp_asset_filename_format().get(values, "_" + asset->id() + ".mxf");
685 }
686
687 string
688 audio_asset_filename (shared_ptr<dcp::SoundAsset> asset, int reel_index, int reel_count, optional<string> summary)
689 {
690         dcp::NameFormat::Map values;
691         values['t'] = "pcm";
692         values['r'] = raw_convert<string> (reel_index + 1);
693         values['n'] = raw_convert<string> (reel_count);
694         if (summary) {
695                 values['c'] = careful_string_filter (summary.get());
696         }
697         return Config::instance()->dcp_asset_filename_format().get(values, "_" + asset->id() + ".mxf");
698 }
699
700 float
701 relaxed_string_to_float (string s)
702 {
703         try {
704                 boost::algorithm::replace_all (s, ",", ".");
705                 return lexical_cast<float> (s);
706         } catch (bad_lexical_cast) {
707                 boost::algorithm::replace_all (s, ".", ",");
708                 return lexical_cast<float> (s);
709         }
710 }
711
712 string
713 careful_string_filter (string s)
714 {
715         /* Filter out `bad' characters which `may' cause problems with some systems (either for DCP name or filename).
716            There's no apparent list of what really is allowed, so this is a guess.
717            Safety first and all that.
718         */
719
720         string out;
721         string const allowed = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_%.+";
722         for (size_t i = 0; i < s.size(); ++i) {
723                 if (allowed.find (s[i]) != string::npos) {
724                         out += s[i];
725                 }
726         }
727
728         return out;
729 }
730
731 /** @param mapped List of mapped audio channels from a Film.
732  *  @param channels Total number of channels in the Film.
733  *  @return First: number of non-LFE channels, second: number of LFE channels.
734  */
735 pair<int, int>
736 audio_channel_types (list<int> mapped, int channels)
737 {
738         int non_lfe = 0;
739         int lfe = 0;
740
741         BOOST_FOREACH (int i, mapped) {
742                 if (i >= channels) {
743                         /* This channel is mapped but is not included in the DCP */
744                         continue;
745                 }
746
747                 if (static_cast<dcp::Channel> (i) == dcp::LFE) {
748                         ++lfe;
749                 } else {
750                         ++non_lfe;
751                 }
752         }
753
754         return make_pair (non_lfe, lfe);
755 }
756
757 shared_ptr<AudioBuffers>
758 remap (shared_ptr<const AudioBuffers> input, int output_channels, AudioMapping map)
759 {
760         shared_ptr<AudioBuffers> mapped (new AudioBuffers (output_channels, input->frames()));
761         mapped->make_silent ();
762
763         for (int i = 0; i < map.input_channels(); ++i) {
764                 for (int j = 0; j < mapped->channels(); ++j) {
765                         if (map.get (i, static_cast<dcp::Channel> (j)) > 0) {
766                                 mapped->accumulate_channel (
767                                         input.get(),
768                                         i,
769                                         static_cast<dcp::Channel> (j),
770                                         map.get (i, static_cast<dcp::Channel> (j))
771                                         );
772                         }
773                 }
774         }
775
776         return mapped;
777 }
778
779 Eyes
780 increment_eyes (Eyes e)
781 {
782         if (e == EYES_LEFT) {
783                 return EYES_RIGHT;
784         }
785
786         return EYES_LEFT;
787 }