Invert Pan-Azimuth (up means left)
[ardour.git] / libs / pbd / pbd.cc
1 /*
2  * Copyright (C) 2013-2015 Tim Mayberry <mojofunk@gmail.com>
3  * Copyright (C) 2014-2016 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2014-2019 Robin Gareus <robin@gareus.org>
5  *
6  * This program 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  * This program 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 along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include <iostream>
22 #include <cstdlib>
23 #include <string>
24
25 #ifdef PLATFORM_WINDOWS
26 #include <fcntl.h>
27 #endif
28
29 #include <giomm.h>
30
31 #include <glibmm/thread.h>
32
33 #include "pbd/pbd.h"
34 #include "pbd/debug.h"
35 #include "pbd/error.h"
36 #include "pbd/id.h"
37 #include "pbd/enumwriter.h"
38 #include "pbd/fpu.h"
39
40 #ifdef PLATFORM_WINDOWS
41 #include <winsock2.h>
42 #include "pbd/windows_timer_utils.h"
43 #include "pbd/windows_mmcss.h"
44 #endif
45
46 #include "pbd/i18n.h"
47
48 extern void setup_libpbd_enums ();
49
50 namespace {
51
52 static bool libpbd_initialized = false;
53
54 static
55 void
56 set_debug_options_from_env ()
57 {
58         bool set;
59         std::string options;
60
61         options = Glib::getenv ("PBD_DEBUG", set);
62         if (set) {
63                 std::cerr << X_("PBD_DEBUG=") << options << std::endl;
64                 PBD::parse_debug_options (options.c_str());
65         }
66 }
67
68 #ifdef PLATFORM_WINDOWS
69 static
70 void
71 test_timers_from_env ()
72 {
73         bool set;
74         std::string options;
75
76         options = Glib::getenv ("PBD_TEST_TIMERS", set);
77         if (set) {
78                 if (!PBD::QPC::check_timer_valid ()) {
79                         PBD::error << X_("Windows QPC Timer source not usable") << endmsg;
80                 } else {
81                         PBD::info << X_("Windows QPC Timer source usable") << endmsg;
82                 }
83         }
84 }
85 #endif
86
87 } // namespace
88
89 bool
90 PBD::init ()
91 {
92         if (libpbd_initialized) {
93                 return true;
94         }
95
96 #ifdef PLATFORM_WINDOWS
97         // Essential!!  Make sure that any files used by Ardour
98         //              will be created or opened in BINARY mode!
99         _fmode = O_BINARY;
100
101         WSADATA wsaData;
102
103         /* Initialize windows socket DLL for PBD::CrossThreadChannel
104          */
105
106         if (WSAStartup(MAKEWORD(1,1),&wsaData) != 0) {
107                 error << X_("Windows socket initialization failed with error: ") << WSAGetLastError() << endmsg;
108                 return false;
109         }
110
111         QPC::initialize();
112         test_timers_from_env ();
113
114         if (!PBD::MMCSS::initialize()) {
115                 PBD::info << X_("Unable to initialize MMCSS") << endmsg;
116         } else {
117                 PBD::info << X_("MMCSS Initialized") << endmsg;
118         }
119 #endif
120
121         if (!Glib::thread_supported()) {
122                 Glib::thread_init();
123         }
124
125         Gio::init ();
126
127         PBD::ID::init ();
128
129         setup_libpbd_enums ();
130
131         set_debug_options_from_env ();
132
133         libpbd_initialized = true;
134         return true;
135 }
136
137 void
138 PBD::cleanup ()
139 {
140 #ifdef PLATFORM_WINDOWS
141         PBD::MMCSS::deinitialize ();
142         WSACleanup();
143 #endif
144
145         EnumWriter::destroy ();
146         FPU::destroy ();
147 }