2008-06-02 17:41:35 -04:00
|
|
|
/*
|
2015-10-04 14:51:05 -04:00
|
|
|
Copyright (C) 2000-2007 Paul Davis
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <stdint.h>
|
2013-07-16 13:00:49 -04:00
|
|
|
#ifdef COMPILER_MSVC
|
|
|
|
#include <io.h> // Microsoft's nearest equivalent to <unistd.h>
|
|
|
|
#else
|
2008-06-02 17:41:35 -04:00
|
|
|
#include <unistd.h>
|
2013-07-16 13:00:49 -04:00
|
|
|
#endif
|
2008-06-02 17:41:35 -04:00
|
|
|
#include <fcntl.h>
|
2008-09-10 11:03:30 -04:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstring>
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2009-02-25 13:26:51 -05:00
|
|
|
#include "pbd/base_ui.h"
|
2012-04-24 12:45:38 -04:00
|
|
|
#include "pbd/debug.h"
|
2009-12-28 11:49:44 -05:00
|
|
|
#include "pbd/pthread_utils.h"
|
2009-02-25 13:26:51 -05:00
|
|
|
#include "pbd/error.h"
|
|
|
|
#include "pbd/compose.h"
|
|
|
|
#include "pbd/failed_constructor.h"
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
#include "i18n.h"
|
|
|
|
|
2013-07-11 12:56:35 -04:00
|
|
|
#include "pbd/debug.h"
|
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
using namespace std;
|
|
|
|
using namespace PBD;
|
2009-12-08 22:05:14 -05:00
|
|
|
using namespace Glib;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-08 22:05:14 -05:00
|
|
|
uint64_t BaseUI::rt_bit = 1;
|
2008-06-02 17:41:35 -04:00
|
|
|
BaseUI::RequestType BaseUI::CallSlot = BaseUI::new_request_type();
|
2009-12-09 13:37:06 -05:00
|
|
|
BaseUI::RequestType BaseUI::Quit = BaseUI::new_request_type();
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2015-12-12 10:55:40 -05:00
|
|
|
BaseUI::BaseUI (const string& loop_name)
|
|
|
|
: EventLoop (loop_name)
|
|
|
|
, m_context(MainContext::get_default())
|
2013-07-11 12:56:35 -04:00
|
|
|
, run_loop_thread (0)
|
2013-07-11 12:52:46 -04:00
|
|
|
, request_channel (true)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
base_ui_instance = this;
|
2014-12-03 21:11:02 -05:00
|
|
|
request_channel.set_receive_handler (sigc::mem_fun (*this, &BaseUI::request_handler));
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2009-12-08 22:05:14 -05:00
|
|
|
/* derived class must set _ok */
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
BaseUI::~BaseUI()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
BaseUI::RequestType
|
|
|
|
BaseUI::new_request_type ()
|
|
|
|
{
|
|
|
|
RequestType rt;
|
|
|
|
|
|
|
|
/* XXX catch out-of-range */
|
|
|
|
|
|
|
|
rt = RequestType (rt_bit);
|
|
|
|
rt_bit <<= 1;
|
|
|
|
|
|
|
|
return rt;
|
|
|
|
}
|
|
|
|
|
2009-12-08 22:05:14 -05:00
|
|
|
void
|
|
|
|
BaseUI::main_thread ()
|
|
|
|
{
|
2015-12-12 10:55:40 -05:00
|
|
|
DEBUG_TRACE (DEBUG::EventLoop, string_compose ("%1: event loop running in thread %2\n", event_loop_name(), pthread_name()));
|
2009-12-21 13:23:07 -05:00
|
|
|
set_event_loop_for_thread (this);
|
2009-12-08 22:05:14 -05:00
|
|
|
thread_init ();
|
2012-05-14 13:07:53 -04:00
|
|
|
_main_loop->get_context()->signal_idle().connect (sigc::mem_fun (*this, &BaseUI::signal_running));
|
2009-12-08 22:05:14 -05:00
|
|
|
_main_loop->run ();
|
|
|
|
}
|
|
|
|
|
2012-05-14 13:07:53 -04:00
|
|
|
bool
|
|
|
|
BaseUI::signal_running ()
|
|
|
|
{
|
2012-07-25 13:48:55 -04:00
|
|
|
Glib::Threads::Mutex::Lock lm (_run_lock);
|
2012-05-14 13:07:53 -04:00
|
|
|
_running.signal ();
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2012-05-14 13:07:53 -04:00
|
|
|
return false; // don't call it again
|
|
|
|
}
|
|
|
|
|
2009-12-08 22:05:14 -05:00
|
|
|
void
|
|
|
|
BaseUI::run ()
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
2009-12-08 22:05:14 -05:00
|
|
|
/* to be called by UI's that need/want their own distinct, self-created event loop thread.
|
2008-06-02 17:41:35 -04:00
|
|
|
*/
|
|
|
|
|
2013-07-11 12:56:35 -04:00
|
|
|
m_context = MainContext::create();
|
|
|
|
_main_loop = MainLoop::create (m_context);
|
|
|
|
attach_request_source ();
|
2009-12-09 13:37:06 -05:00
|
|
|
|
2012-07-25 13:48:55 -04:00
|
|
|
Glib::Threads::Mutex::Lock lm (_run_lock);
|
|
|
|
run_loop_thread = Glib::Threads::Thread::create (mem_fun (*this, &BaseUI::main_thread));
|
2012-05-14 13:07:53 -04:00
|
|
|
_running.wait (_run_lock);
|
2009-12-08 22:05:14 -05:00
|
|
|
}
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2009-12-08 22:05:14 -05:00
|
|
|
void
|
|
|
|
BaseUI::quit ()
|
|
|
|
{
|
2012-05-09 06:34:00 -04:00
|
|
|
if (_main_loop && _main_loop->is_running()) {
|
2009-12-09 13:37:06 -05:00
|
|
|
_main_loop->quit ();
|
|
|
|
run_loop_thread->join ();
|
|
|
|
}
|
2009-12-08 22:05:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
BaseUI::request_handler (Glib::IOCondition ioc)
|
|
|
|
{
|
2012-04-23 22:28:51 -04:00
|
|
|
/* check the request pipe */
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2009-12-08 22:05:14 -05:00
|
|
|
if (ioc & ~IO_IN) {
|
|
|
|
_main_loop->quit ();
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
2009-12-08 22:05:14 -05:00
|
|
|
if (ioc & IO_IN) {
|
|
|
|
request_channel.drain ();
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-08 22:05:14 -05:00
|
|
|
/* there may been an error. we'd rather handle requests first,
|
|
|
|
and then get IO_HUP or IO_ERR on the next loop.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* handle requests */
|
|
|
|
|
2016-01-12 22:10:23 -05:00
|
|
|
DEBUG_TRACE (DEBUG::EventLoop, string_compose ("%1: request handler\n", event_loop_name()));
|
2009-12-08 22:05:14 -05:00
|
|
|
handle_ui_requests ();
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
2009-12-08 22:05:14 -05:00
|
|
|
return true;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
2013-07-11 12:52:46 -04:00
|
|
|
|
|
|
|
void
|
|
|
|
BaseUI::signal_new_request ()
|
|
|
|
{
|
2016-01-12 22:10:23 -05:00
|
|
|
DEBUG_TRACE (DEBUG::EventLoop, string_compose ("%1: signal_new_request\n", event_loop_name()));
|
2013-07-11 12:52:46 -04:00
|
|
|
request_channel.wakeup ();
|
|
|
|
}
|
|
|
|
|
2013-07-11 12:56:35 -04:00
|
|
|
/**
|
|
|
|
* This method relies on the caller having already set m_context
|
|
|
|
*/
|
2013-07-11 12:52:46 -04:00
|
|
|
void
|
2013-07-11 12:56:35 -04:00
|
|
|
BaseUI::attach_request_source ()
|
2013-07-11 12:52:46 -04:00
|
|
|
{
|
2016-01-12 22:10:23 -05:00
|
|
|
DEBUG_TRACE (DEBUG::EventLoop, string_compose ("%1: attach request source\n", event_loop_name()));
|
2014-12-03 21:11:02 -05:00
|
|
|
request_channel.attach (m_context);
|
2013-07-11 12:52:46 -04:00
|
|
|
}
|