Fix MSVC builds, use C89 strtol() instead of C99 strtoll()
[ardour.git] / libs / pbd / base_ui.cc
1 /*
2  * Copyright (C) 2000-2016 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2008-2009 David Robillard <d@drobilla.net>
4  * Copyright (C) 2011-2012 Carl Hetherington <carl@carlh.net>
5  * Copyright (C) 2013 John Emmas <john@creativepost.co.uk>
6  * Copyright (C) 2015-2017 Robin Gareus <robin@gareus.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include <cstring>
24 #include <stdint.h>
25 #ifdef COMPILER_MSVC
26 #include <io.h>      // Microsoft's nearest equivalent to <unistd.h>
27 #else
28 #include <unistd.h>
29 #endif
30 #include <fcntl.h>
31 #include <cerrno>
32 #include <cstring>
33
34 #include <pthread.h>
35 #include <sched.h>
36
37 #include "pbd/base_ui.h"
38 #include "pbd/debug.h"
39 #include "pbd/pthread_utils.h"
40 #include "pbd/error.h"
41 #include "pbd/compose.h"
42 #include "pbd/failed_constructor.h"
43
44 #include "pbd/i18n.h"
45
46 #include "pbd/debug.h"
47
48 using namespace std;
49 using namespace PBD;
50 using namespace Glib;
51
52 uint64_t BaseUI::rt_bit = 1;
53 BaseUI::RequestType BaseUI::CallSlot = BaseUI::new_request_type();
54 BaseUI::RequestType BaseUI::Quit = BaseUI::new_request_type();
55
56 BaseUI::BaseUI (const string& loop_name)
57         : EventLoop (loop_name)
58         , m_context(MainContext::get_default())
59         , run_loop_thread (0)
60         , request_channel (true)
61 {
62         base_ui_instance = this;
63         request_channel.set_receive_handler (sigc::mem_fun (*this, &BaseUI::request_handler));
64
65         /* derived class must set _ok */
66 }
67
68 BaseUI::~BaseUI()
69 {
70 }
71
72 BaseUI::RequestType
73 BaseUI::new_request_type ()
74 {
75         RequestType rt;
76
77         /* XXX catch out-of-range */
78
79         rt = RequestType (rt_bit);
80         rt_bit <<= 1;
81
82         return rt;
83 }
84
85 int
86 BaseUI::set_thread_priority (const int policy, int priority) const
87 {
88         return pbd_set_thread_priority (pthread_self(), policy, priority);
89 }
90
91 void
92 BaseUI::main_thread ()
93 {
94         DEBUG_TRACE (DEBUG::EventLoop, string_compose ("%1: event loop running in thread %2\n", event_loop_name(), pthread_name()));
95         set_event_loop_for_thread (this);
96         thread_init ();
97         _main_loop->get_context()->signal_idle().connect (sigc::mem_fun (*this, &BaseUI::signal_running));
98         _main_loop->run ();
99 }
100
101 bool
102 BaseUI::signal_running ()
103 {
104         Glib::Threads::Mutex::Lock lm (_run_lock);
105         _running.signal ();
106
107         return false; // don't call it again
108 }
109
110 void
111 BaseUI::run ()
112 {
113         /* to be called by UI's that need/want their own distinct, self-created event loop thread.
114         */
115
116         m_context = MainContext::create();
117         _main_loop = MainLoop::create (m_context);
118         attach_request_source ();
119
120         Glib::Threads::Mutex::Lock lm (_run_lock);
121         run_loop_thread = Glib::Threads::Thread::create (mem_fun (*this, &BaseUI::main_thread));
122         _running.wait (_run_lock);
123 }
124
125 void
126 BaseUI::quit ()
127 {
128         if (_main_loop && _main_loop->is_running()) {
129                 _main_loop->quit ();
130                 run_loop_thread->join ();
131         }
132 }
133
134 bool
135 BaseUI::request_handler (Glib::IOCondition ioc)
136 {
137         /* check the request pipe */
138
139         if (ioc & ~IO_IN) {
140                 _main_loop->quit ();
141         }
142
143         if (ioc & IO_IN) {
144                 request_channel.drain ();
145
146                 /* there may been an error. we'd rather handle requests first,
147                    and then get IO_HUP or IO_ERR on the next loop.
148                 */
149
150                 /* handle requests */
151
152                 DEBUG_TRACE (DEBUG::EventLoop, string_compose ("%1: request handler\n", event_loop_name()));
153                 handle_ui_requests ();
154         }
155
156         return true;
157 }
158
159 void
160 BaseUI::signal_new_request ()
161 {
162         DEBUG_TRACE (DEBUG::EventLoop, string_compose ("%1: signal_new_request\n", event_loop_name()));
163         request_channel.wakeup ();
164 }
165
166 /**
167  * This method relies on the caller having already set m_context
168  */
169 void
170 BaseUI::attach_request_source ()
171 {
172         DEBUG_TRACE (DEBUG::EventLoop, string_compose ("%1: attach request source\n", event_loop_name()));
173         request_channel.attach (m_context);
174 }