gui: refactor a2j_proxy

This commit is contained in:
Nedko Arnaudov 2009-12-06 23:35:01 +02:00
parent 9383960e44
commit ad421b846a
4 changed files with 329 additions and 396 deletions

314
gui/a2j_proxy.c Normal file
View File

@ -0,0 +1,314 @@
/* -*- Mode: C ; c-basic-offset: 2 -*- */
/*
* LADI Session Handler (ladish)
*
* Copyright (C) 2008,2009 Nedko Arnaudov <nedko@arnaudov.name>
*
**************************************************************************
* This file contains code that interface with a2jmidid through D-Bus
**************************************************************************
*
* LADI Session Handler 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.
*
* LADI Session Handler 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 LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
* or write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "common.h"
#include "a2j_proxy.h"
#include "../dbus/helpers.h"
#define A2J_SERVICE "org.gna.home.a2jmidid"
#define A2J_OBJECT "/"
#define A2J_IFACE_CONTROL "org.gna.home.a2jmidid.control"
static const char * g_signals[] =
{
"bridge_started",
"bridge_stopped",
NULL
};
static bool g_a2j_started = false;
static char * g_a2j_jack_client_name = NULL;
static
DBusHandlerResult
message_hook(
DBusConnection * connection,
DBusMessage * message,
void * proxy)
{
const char * object_name;
const char * old_owner;
const char * new_owner;
if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged"))
{
if (!dbus_message_get_args(
message, &g_dbus_error,
DBUS_TYPE_STRING, &object_name,
DBUS_TYPE_STRING, &old_owner,
DBUS_TYPE_STRING, &new_owner,
DBUS_TYPE_INVALID))
{
log_error("dbus_message_get_args() failed to extract NameOwnerChanged signal arguments (%s)", g_dbus_error.message);
dbus_error_free(&g_dbus_error);
return DBUS_HANDLER_RESULT_HANDLED;
}
if (strcmp(object_name, A2J_SERVICE) != 0)
{
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
if (old_owner[0] == '\0')
{
log_info("a2j activatation detected.");
}
else if (new_owner[0] == '\0')
{
log_info("a2j deactivatation detected.");
}
return DBUS_HANDLER_RESULT_HANDLED;
}
if (dbus_message_is_signal(message, A2J_IFACE_CONTROL, "bridge_started"))
{
log_info("a2j bridge started.");
if (g_a2j_jack_client_name != NULL)
{
free(g_a2j_jack_client_name);
g_a2j_jack_client_name = NULL;
}
g_a2j_started = true;
return DBUS_HANDLER_RESULT_HANDLED;
}
if (dbus_message_is_signal(message, A2J_IFACE_CONTROL, "bridge_stopped"))
{
if (g_a2j_jack_client_name != NULL)
{
free(g_a2j_jack_client_name);
g_a2j_jack_client_name = NULL;
}
g_a2j_started = false;
log_info("a2j bridge stopped.");
return DBUS_HANDLER_RESULT_HANDLED;
}
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
bool a2j_proxy_init(void)
{
char rule[1024];
const char ** signal;
dbus_bus_add_match(
g_dbus_connection,
"type='signal',interface='"DBUS_INTERFACE_DBUS"',member=NameOwnerChanged,arg0='"A2J_SERVICE"'",
&g_dbus_error);
if (dbus_error_is_set(&g_dbus_error))
{
log_error("Failed to add D-Bus match rule: %s", g_dbus_error.message);
dbus_error_free(&g_dbus_error);
return false;
}
g_a2j_started = a2j_proxy_is_started();
if (g_a2j_started)
{
a2j_proxy_get_jack_client_name_noncached(&g_a2j_jack_client_name);
}
for (signal = g_signals; *signal != NULL; signal++)
{
snprintf(
rule,
sizeof(rule),
"type='signal',sender='"A2J_SERVICE"',path='"A2J_OBJECT"',interface='"A2J_IFACE_CONTROL"',member='%s'",
*signal);
dbus_bus_add_match(g_dbus_connection, rule, &g_dbus_error);
if (dbus_error_is_set(&g_dbus_error))
{
log_error("Failed to add D-Bus match rule: %s", g_dbus_error.message);
dbus_error_free(&g_dbus_error);
return false;
}
}
if (!dbus_connection_add_filter(g_dbus_connection, message_hook, NULL, NULL))
{
log_error("Failed to add D-Bus filter");
return false;
}
return true;
}
void a2j_proxy_uninit(void)
{
dbus_connection_remove_filter(g_dbus_connection, message_hook, NULL);
}
const char * a2j_proxy_get_jack_client_name_cached(void)
{
if (!g_a2j_started)
{
return NULL;
}
if (g_a2j_jack_client_name == NULL)
{
a2j_proxy_get_jack_client_name_noncached(&g_a2j_jack_client_name);
}
return g_a2j_jack_client_name;
}
bool a2j_proxy_get_jack_client_name_noncached(char ** client_name_ptr_ptr)
{
DBusMessage * reply_ptr;
const char * name;
if (!dbus_call(A2J_SERVICE, A2J_OBJECT, A2J_IFACE_CONTROL, "get_jack_client_name", "", NULL, &reply_ptr))
{
//log_error("a2j::get_jack_client_name() failed.");
return false;
}
if (!dbus_message_get_args(reply_ptr, &g_dbus_error, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID))
{
dbus_message_unref(reply_ptr);
dbus_error_free(&g_dbus_error);
log_error("decoding reply of get_jack_client_name failed.");
return false;
}
*client_name_ptr_ptr = strdup(name);
dbus_message_unref(reply_ptr);
if (*client_name_ptr_ptr == NULL)
{
log_error("strdup() failed for a2j jack client name string");
return false;
}
return true;
}
bool
a2j_proxy_map_jack_port(
const char * jack_port_name,
char ** alsa_client_name_ptr_ptr,
char ** alsa_port_name_ptr_ptr,
uint32_t * alsa_client_id_ptr)
{
DBusMessage * reply_ptr;
dbus_uint32_t alsa_client_id;
dbus_uint32_t alsa_port_id;
const char * alsa_client_name;
const char * alsa_port_name;
if (!dbus_call(A2J_SERVICE, A2J_OBJECT, A2J_IFACE_CONTROL, "map_jack_port_to_alsa", "s", &jack_port_name, NULL, &reply_ptr))
{
log_error("a2j::map_jack_port_to_alsa() failed.");
return false;
}
if (!dbus_message_get_args(
reply_ptr,
&g_dbus_error,
DBUS_TYPE_UINT32,
&alsa_client_id,
DBUS_TYPE_UINT32,
&alsa_port_id,
DBUS_TYPE_STRING,
&alsa_client_name,
DBUS_TYPE_STRING,
&alsa_port_name,
DBUS_TYPE_INVALID))
{
dbus_message_unref(reply_ptr);
dbus_error_free(&g_dbus_error);
log_error("decoding reply of map_jack_port_to_alsa failed.");
return false;
}
*alsa_client_name_ptr_ptr = strdup(alsa_client_name);
if (*alsa_client_name_ptr_ptr == NULL)
{
dbus_message_unref(reply_ptr);
log_error("strdup() failed for a2j alsa client name string");
return false;
}
*alsa_port_name_ptr_ptr = strdup(alsa_port_name);
if (*alsa_port_name_ptr_ptr == NULL)
{
dbus_message_unref(reply_ptr);
log_error("strdup() failed for a2j alsa port name string");
free(*alsa_client_name_ptr_ptr);
return false;
}
*alsa_client_id_ptr = alsa_client_id;
dbus_message_unref(reply_ptr);
return true;
}
bool a2j_proxy_is_started(void)
{
dbus_bool_t started;
if (!dbus_call(A2J_SERVICE, A2J_OBJECT, A2J_IFACE_CONTROL, "is_started", "", "b", &started))
{
log_error("a2j::is_started() failed.");
return false;
}
return started;
}
bool a2j_proxy_start_bridge(void)
{
if (!dbus_call(A2J_SERVICE, A2J_OBJECT, A2J_IFACE_CONTROL, "start", "", ""))
{
log_error("a2j::start() failed.");
return false;
}
return true;
}
bool a2j_proxy_stop_bridge(void)
{
if (!dbus_call(A2J_SERVICE, A2J_OBJECT, A2J_IFACE_CONTROL, "stop", "", ""))
{
log_error("a2j::stop() failed.");
return false;
}
return true;
}

View File

@ -1,367 +0,0 @@
/* -*- Mode: C ; c-basic-offset: 2 -*- */
/*
* LADI Session Handler (ladish)
*
* Copyright (C) 2008 Nedko Arnaudov <nedko@arnaudov.name>
*
**************************************************************************
* This file contains code that interface with a2jmidid through D-Bus
**************************************************************************
*
* LADI Session Handler 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.
*
* LADI Session Handler 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 LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
* or write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "common.h"
#include <dbus/dbus.h>
#include "a2j_proxy.hpp"
#include "Patchage.hpp"
#include "globals.hpp"
#include "dbus_helpers.h"
#define A2J_SERVICE "org.gna.home.a2jmidid"
#define A2J_OBJECT "/"
#define A2J_IFACE_CONTROL "org.gna.home.a2jmidid.control"
static
void
error_msg(
const std::string& msg)
{
g_app->error_msg((std::string)"[A2J] " + msg);
}
static
void
info_msg(
const std::string& msg)
{
g_app->info_msg((std::string)"[A2J] " + msg);
}
struct a2j_proxy_impl
{
void
init();
static
DBusHandlerResult
dbus_message_hook(
DBusConnection * connection,
DBusMessage * message,
void * proxy);
bool
call(
bool response_expected,
const char* iface,
const char* method,
DBusMessage ** reply_ptr_ptr,
int in_type,
...);
bool
get_jack_client_name(
std::string& jack_client_name_ref);
bool
is_started();
bool _server_responding;
std::string _jack_client_name;
};
a2j_proxy::a2j_proxy()
{
_impl_ptr = new a2j_proxy_impl;
_impl_ptr->init();
}
a2j_proxy::~a2j_proxy()
{
delete _impl_ptr;
}
const char *
a2j_proxy::get_jack_client_name()
{
return _impl_ptr->_jack_client_name.c_str();
}
void
a2j_proxy_impl::init()
{
unsigned int status;
_server_responding = false;
patchage_dbus_add_match("type='signal',interface='" DBUS_INTERFACE_DBUS "',member=NameOwnerChanged,arg0='" A2J_SERVICE "'");
patchage_dbus_add_match("type='signal',interface='" A2J_IFACE_CONTROL "',member=bridge_started");
patchage_dbus_add_match("type='signal',interface='" A2J_IFACE_CONTROL "',member=bridge_stopped");
patchage_dbus_add_filter(dbus_message_hook, this);
// get jack client name
// calling any method to updates server responding status
// this also actiavtes a2j object if it not activated already
get_jack_client_name(_jack_client_name);
if (is_started())
{
status = A2J_STATUS_BRIDGE_STARTED;
}
else
{
if (!_server_responding)
{
status = A2J_STATUS_NO_RESPONSE;
}
else
{
status = A2J_STATUS_BRIDGE_STOPPED;
}
}
g_app->set_a2j_status(status);
}
DBusHandlerResult
a2j_proxy_impl::dbus_message_hook(
DBusConnection * connection,
DBusMessage * message,
void * proxy)
{
const char * object_name;
const char * old_owner;
const char * new_owner;
ASSERT(proxy);
//a2j_proxy_impl * me = reinterpret_cast<a2j_proxy_impl *>(proxy);
//info_msg("dbus_message_hook() called.");
// Handle signals we have subscribed for in attach()
if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged"))
{
if (!dbus_message_get_args(
message, &g_dbus_error,
DBUS_TYPE_STRING, &object_name,
DBUS_TYPE_STRING, &old_owner,
DBUS_TYPE_STRING, &new_owner,
DBUS_TYPE_INVALID))
{
error_msg(str(boost::format("dbus_message_get_args() failed to extract NameOwnerChanged signal arguments (%s)") % g_dbus_error.message));
dbus_error_free(&g_dbus_error);
return DBUS_HANDLER_RESULT_HANDLED;
}
if ((std::string)object_name != A2J_SERVICE)
{
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
if (old_owner[0] == '\0')
{
info_msg((std::string)"A2J activated.");
g_app->set_a2j_status(A2J_STATUS_BRIDGE_STOPPED);
}
else if (new_owner[0] == '\0')
{
info_msg((std::string)"A2J deactivated.");
g_app->set_a2j_status(A2J_STATUS_NO_RESPONSE);
}
return DBUS_HANDLER_RESULT_HANDLED;
}
if (dbus_message_is_signal(message, A2J_IFACE_CONTROL, "bridge_started"))
{
info_msg("bridge started.");
g_app->set_a2j_status(A2J_STATUS_BRIDGE_STARTED);
return DBUS_HANDLER_RESULT_HANDLED;
}
if (dbus_message_is_signal(message, A2J_IFACE_CONTROL, "bridge_stopped"))
{
info_msg("bridge stopped.");
g_app->set_a2j_status(A2J_STATUS_BRIDGE_STOPPED);
return DBUS_HANDLER_RESULT_HANDLED;
}
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
bool
a2j_proxy_impl::call(
bool response_expected,
const char* iface,
const char* method,
DBusMessage ** reply_ptr_ptr,
int in_type,
...)
{
va_list ap;
va_start(ap, in_type);
_server_responding = patchage_dbus_call_valist(
response_expected,
A2J_SERVICE,
A2J_OBJECT,
iface,
method,
reply_ptr_ptr,
in_type,
ap);
va_end(ap);
return _server_responding;
}
bool
a2j_proxy_impl::get_jack_client_name(
std::string& jack_client_name_ref)
{
DBusMessage * reply_ptr;
const char * jack_client_name;
if (!call(true, A2J_IFACE_CONTROL, "get_jack_client_name", &reply_ptr, DBUS_TYPE_INVALID))
{
return false;
}
if (!dbus_message_get_args(reply_ptr, &g_dbus_error, DBUS_TYPE_STRING, &jack_client_name, DBUS_TYPE_INVALID))
{
dbus_message_unref(reply_ptr);
dbus_error_free(&g_dbus_error);
error_msg("decoding reply of get_jack_client_name failed.");
return false;
}
jack_client_name_ref = jack_client_name;
dbus_message_unref(reply_ptr);
return true;
}
bool
a2j_proxy::map_jack_port(
const char * jack_port_name,
std::string& alsa_client_name_ref,
std::string& alsa_port_name_ref,
uint32_t& alsa_client_id_ref)
{
DBusMessage * reply_ptr;
dbus_uint32_t alsa_client_id;
dbus_uint32_t alsa_port_id;
const char * alsa_client_name;
const char * alsa_port_name;
if (!_impl_ptr->call(
true,
A2J_IFACE_CONTROL,
"map_jack_port_to_alsa",
&reply_ptr,
DBUS_TYPE_STRING,
&jack_port_name,
DBUS_TYPE_INVALID))
{
return false;
}
if (!dbus_message_get_args(
reply_ptr,
&g_dbus_error,
DBUS_TYPE_UINT32,
&alsa_client_id,
DBUS_TYPE_UINT32,
&alsa_port_id,
DBUS_TYPE_STRING,
&alsa_client_name,
DBUS_TYPE_STRING,
&alsa_port_name,
DBUS_TYPE_INVALID))
{
dbus_message_unref(reply_ptr);
dbus_error_free(&g_dbus_error);
error_msg("decoding reply of map_jack_port_to_alsa failed.");
return false;
}
alsa_client_name_ref = alsa_client_name;
alsa_port_name_ref = alsa_port_name;
alsa_client_id_ref = alsa_client_id;
dbus_message_unref(reply_ptr);
return true;
}
bool
a2j_proxy_impl::is_started()
{
DBusMessage * reply_ptr;
dbus_bool_t started;
if (!call(true, A2J_IFACE_CONTROL, "is_started", &reply_ptr, DBUS_TYPE_INVALID))
{
return false;
}
if (!dbus_message_get_args(reply_ptr, &g_dbus_error, DBUS_TYPE_BOOLEAN, &started, DBUS_TYPE_INVALID))
{
dbus_message_unref(reply_ptr);
dbus_error_free(&g_dbus_error);
error_msg("decoding reply of is_started failed.");
return false;
}
dbus_message_unref(reply_ptr);
return started;
}
void
a2j_proxy::start_bridge()
{
DBusMessage * reply_ptr;
if (!_impl_ptr->call(true, A2J_IFACE_CONTROL, "start", &reply_ptr, DBUS_TYPE_INVALID))
{
return;
}
dbus_message_unref(reply_ptr);
}
void
a2j_proxy::stop_bridge()
{
DBusMessage * reply_ptr;
if (!_impl_ptr->call(true, A2J_IFACE_CONTROL, "stop", &reply_ptr, DBUS_TYPE_INVALID))
{
return;
}
dbus_message_unref(reply_ptr);
}

View File

@ -2,7 +2,7 @@
/*
* LADI Session Handler (ladish)
*
* Copyright (C) 2008 Nedko Arnaudov <nedko@arnaudov.name>
* Copyright (C) 2008,2009 Nedko Arnaudov <nedko@arnaudov.name>
*
**************************************************************************
* This file contains interface to code that interfaces a2jmidid through D-Bus
@ -27,36 +27,22 @@
#ifndef A2J_PROXY_HPP__24525CB1_8AED_4697_8C56_5C57473839CC__INCLUDED
#define A2J_PROXY_HPP__24525CB1_8AED_4697_8C56_5C57473839CC__INCLUDED
struct a2j_proxy_impl;
typedef struct a2j_proxy_tag { int unused; } * a2j_proxy_handle;
#define A2J_STATUS_NO_RESPONSE 0
#define A2J_STATUS_BRIDGE_STOPPED 1
#define A2J_STATUS_BRIDGE_STARTED 2
bool a2j_proxy_init(void);
void a2j_proxy_uninit(void);
const char * a2j_proxy_get_jack_client_name_cached(void);
bool a2j_proxy_get_jack_client_name_noncached(char ** client_name_ptr_ptr);
class a2j_proxy
{
public:
a2j_proxy();
~a2j_proxy();
const char *
get_jack_client_name();
bool
map_jack_port(
bool
a2j_proxy_map_jack_port(
const char * jack_port_name,
std::string& alsa_client_name,
std::string& alsa_port_name,
uint32_t& alsa_client_id);
char ** alsa_client_name_ptr_ptr,
char ** alsa_port_name_ptr_ptr,
uint32_t * alsa_client_id_ptr);
void
start_bridge();
void
stop_bridge();
private:
a2j_proxy_impl * _impl_ptr;
};
bool a2j_proxy_is_started(void);
bool a2j_proxy_start_bridge(void);
bool a2j_proxy_stop_bridge(void);
#endif // #ifndef A2J_PROXY_HPP__24525CB1_8AED_4697_8C56_5C57473839CC__INCLUDED

View File

@ -290,7 +290,7 @@ def build(bld):
'graph_view.c',
#'project_properties.cpp',
#'session.cpp',
#'a2j_proxy.cpp',
'a2j_proxy.c',
'dbus_helpers.c',
'canvas.cpp',
'graph_canvas.c',