[dbus] clear metadata on re-init; don't use exceptions

This commit is contained in:
jackun 2020-05-22 15:00:35 +03:00
parent 5057a47b05
commit 9b595cef1a
No known key found for this signature in database
GPG Key ID: 119DB3F1D05A9ED3
3 changed files with 48 additions and 22 deletions

View File

@ -499,7 +499,7 @@ bool dbus_list_name_to_owner(dbusmgr::dbus_manager& dbus_mgr, string_map& name_o
return true;
}
void dbus_get_player_property(dbusmgr::dbus_manager& dbus_mgr, string_pair_vec& entries, const char * dest, const char * prop)
bool dbus_get_player_property(dbusmgr::dbus_manager& dbus_mgr, string_pair_vec& entries, const char * dest, const char * prop)
{
auto& dbus = dbus_mgr.dbus();
DBusError error;
@ -510,21 +510,27 @@ void dbus_get_player_property(dbusmgr::dbus_manager& dbus_mgr, string_pair_vec&
// dbus-send --print-reply --session --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata'
if (nullptr == (dbus_msg = dbus.message_new_method_call(dest, "/org/mpris/MediaPlayer2", "org.freedesktop.DBus.Properties", "Get"))) {
throw std::runtime_error("unable to allocate memory for dbus message");
std::cerr << "MANGOHUD: unable to allocate memory for dbus message" << std::endl;
dbus.error_free(&error);
return false;
}
const char *v_STRINGS[] = {
static const char *v_STRINGS[] = {
"org.mpris.MediaPlayer2.Player",
};
if (!dbus.message_append_args (dbus_msg, DBUS_TYPE_STRING, &v_STRINGS[0], DBUS_TYPE_STRING, &prop, DBUS_TYPE_INVALID)) {
std::cerr << "MANGOHUD: dbus_message_append_args failed" << std::endl;
dbus.error_free(&error);
dbus.message_unref(dbus_msg);
throw std::runtime_error("dbus_message_append_args failed");
return false;
}
if (nullptr == (dbus_reply = dbus.connection_send_with_reply_and_block(dbus_mgr.get_conn(), dbus_msg, DBUS_TIMEOUT_USE_DEFAULT, &error))) {
dbus.message_unref(dbus_msg);
throw dbusmgr::dbus_error(dbus, &error);
std::cerr << "MANGOHUD: " << error.message << std::endl;
dbus.error_free(&error);
return false;
}
std::string entry;
@ -537,41 +543,50 @@ void dbus_get_player_property(dbusmgr::dbus_manager& dbus_mgr, string_pair_vec&
dbus.message_unref(dbus_msg);
dbus.message_unref(dbus_reply);
dbus.error_free(&error);
return true;
}
void get_media_player_metadata(dbusmgr::dbus_manager& dbus, const std::string& name, metadata& meta)
bool get_media_player_metadata(dbusmgr::dbus_manager& dbus, const std::string& name, metadata& meta)
{
meta.artists.clear();
string_pair_vec_map entries;
std::string dest = "org.mpris.MediaPlayer2." + name;
dbus_get_player_property(dbus, entries["Metadata"], dest.c_str(), "Metadata");
if (!dbus_get_player_property(dbus, entries["Metadata"], dest.c_str(), "Metadata"))
return false;
dbus_get_player_property(dbus, entries["PlaybackStatus"], dest.c_str(), "PlaybackStatus");
assign_metadata(meta, entries);
return true;
}
namespace dbusmgr {
void dbus_manager::init(const std::string& dest)
bool dbus_manager::init(const std::string& dest)
{
if (m_inited)
return;
return true;
if (!m_dbus_ldr.IsLoaded() && !m_dbus_ldr.Load("libdbus-1.so.3"))
throw std::runtime_error("Could not load libdbus-1.so.3");
if (!m_dbus_ldr.IsLoaded() && !m_dbus_ldr.Load("libdbus-1.so.3")) {
std::cerr << "MANGOHUD: Could not load libdbus-1.so.3\n";
return false;
}
m_dbus_ldr.error_init(&m_error);
m_dbus_ldr.threads_init_default();
if ( nullptr == (m_dbus_conn = m_dbus_ldr.bus_get(DBUS_BUS_SESSION, &m_error)) ) {
throw dbus_error(m_dbus_ldr, &m_error);
std::cerr << "MANGOHUD: " << m_error.message << std::endl;
m_dbus_ldr.error_free(&m_error);
return false;
}
std::cout << "Connected to D-Bus as \"" << m_dbus_ldr.bus_get_unique_name(m_dbus_conn) << "\"." << std::endl;
std::cout << "MANGOHUD: Connected to D-Bus as \"" << m_dbus_ldr.bus_get_unique_name(m_dbus_conn) << "\"." << std::endl;
m_dest = dest;
dbus_list_name_to_owner(*this, m_name_owners);
connect_to_signals();
m_inited = true;
return true;
}
void dbus_manager::deinit()

View File

@ -31,6 +31,17 @@ struct metadata {
bool valid = false;
std::mutex mutex;
void clear()
{
artists.clear();
title.clear();
album.clear();
artUrl.clear();
ticker = {};
ticker.dir = -1;
valid = false;
}
};
enum SignalType
@ -58,7 +69,7 @@ namespace dbusmgr {
CB_NEW_METADATA,
};
class dbus_error : public std::runtime_error
/* class dbus_error : public std::runtime_error
{
public:
dbus_error(libdbus_loader& dbus_, DBusError *src) : std::runtime_error(src->message)
@ -71,7 +82,7 @@ namespace dbusmgr {
private:
DBusError error;
libdbus_loader *dbus;
};
};*/
class dbus_manager
{
@ -82,7 +93,7 @@ namespace dbusmgr {
~dbus_manager();
void init(const std::string& dest);
bool init(const std::string& dest);
void deinit();
void add_callback(CBENUM type, callback_func func);
void connect_to_signals();
@ -123,4 +134,4 @@ namespace dbusmgr {
extern dbus_manager dbus_mgr;
}
void get_media_player_metadata(dbusmgr::dbus_manager& dbus, const std::string& name, metadata& meta);
bool get_media_player_metadata(dbusmgr::dbus_manager& dbus, const std::string& name, metadata& meta);

View File

@ -455,11 +455,11 @@ parse_overlay_config(struct overlay_params *params,
#ifdef HAVE_DBUS
if (params->enabled[OVERLAY_PARAM_ENABLED_media_player]) {
try {
dbusmgr::dbus_mgr.init(params->media_player_name);
get_media_player_metadata(dbusmgr::dbus_mgr, params->media_player_name, main_metadata);
} catch (std::runtime_error& e) {
std::cerr << "MANGOHUD: Failed to get initial media player metadata: " << e.what() << std::endl;
main_metadata.clear();
generic_mpris.clear();
if (dbusmgr::dbus_mgr.init(params->media_player_name)) {
if (!get_media_player_metadata(dbusmgr::dbus_mgr, params->media_player_name, main_metadata))
std::cerr << "MANGOHUD: Failed to get initial media player metadata." << std::endl;
}
} else {
dbusmgr::dbus_mgr.deinit();