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