2008-06-02 17:41:35 -04:00
|
|
|
/*
|
|
|
|
Copyright (C) 2000-2007 Paul Davis
|
|
|
|
|
|
|
|
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>
|
|
|
|
#include <unistd.h>
|
|
|
|
#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"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace PBD;
|
2009-12-08 22:05:14 -05:00
|
|
|
using namespace Glib;
|
2008-06-02 17:41:35 -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
|
|
|
|
2009-12-08 22:05:14 -05:00
|
|
|
BaseUI::BaseUI (const string& str)
|
2011-04-04 08:21:26 -04:00
|
|
|
: request_channel (true)
|
|
|
|
, run_loop_thread (0)
|
2009-12-08 22:05:14 -05:00
|
|
|
, _name (str)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
base_ui_instance = this;
|
|
|
|
|
2009-12-08 22:05:14 -05:00
|
|
|
request_channel.ios()->connect (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 ()
|
|
|
|
{
|
2012-04-24 12:45:38 -04:00
|
|
|
DEBUG_TRACE (DEBUG::EventLoop, string_compose ("%1: event loop running in thread %2\n", name(), pthread_self()));
|
2009-12-21 13:23:07 -05:00
|
|
|
set_event_loop_for_thread (this);
|
2009-12-08 22:05:14 -05:00
|
|
|
thread_init ();
|
|
|
|
_main_loop->run ();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
2009-12-08 22:05:14 -05:00
|
|
|
_main_loop = MainLoop::create (MainContext::create());
|
|
|
|
request_channel.ios()->attach (_main_loop->get_context());
|
2009-12-09 13:37:06 -05:00
|
|
|
|
|
|
|
/* glibmm hack - drop the refptr to the IOSource now before it can hurt */
|
|
|
|
request_channel.drop_ios ();
|
|
|
|
|
2009-12-08 22:05:14 -05:00
|
|
|
run_loop_thread = Thread::create (mem_fun (*this, &BaseUI::main_thread), true);
|
|
|
|
}
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2009-12-08 22:05:14 -05:00
|
|
|
void
|
|
|
|
BaseUI::quit ()
|
|
|
|
{
|
2009-12-09 13:37:06 -05:00
|
|
|
if (_main_loop->is_running()) {
|
|
|
|
_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 ();
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2009-12-08 22:05:14 -05:00
|
|
|
|