MangoHud/src/dbus.cpp

609 lines
19 KiB
C++
Raw Normal View History

2020-02-03 22:31:44 +00:00
#include <cstdio>
#include <iostream>
#include <sstream>
#include <array>
#include "dbus_info.h"
using ms = std::chrono::milliseconds;
struct metadata spotify;
typedef std::vector<std::pair<std::string, std::string>> string_pair_vec;
std::string format_signal(const DBusSignal& s)
{
std::stringstream ss;
ss << "type='signal',interface='" << s.intf << "'";
ss << ",member='" << s.signal << "'";
return ss.str();
}
static bool check_msg_arg(libdbus_loader& dbus, DBusMessageIter *iter, int type)
2020-02-03 22:31:44 +00:00
{
int curr_type = DBUS_TYPE_INVALID;
if ((curr_type = dbus.message_iter_get_arg_type (iter)) != type) {
#ifndef NDEBUG
2020-02-03 22:31:44 +00:00
std::cerr << "Argument is not of type '" << (char)type << "' != '" << (char) curr_type << "'" << std::endl;
#endif
2020-02-03 22:31:44 +00:00
return false;
}
return true;
}
bool get_string_array(libdbus_loader& dbus, DBusMessageIter *iter_, std::vector<std::string>& entries)
2020-02-03 22:31:44 +00:00
{
DBusMessageIter iter = *iter_;
DBusMessageIter subiter;
int current_type = DBUS_TYPE_INVALID;
current_type = dbus.message_iter_get_arg_type (&iter);
2020-02-03 22:31:44 +00:00
if (current_type == DBUS_TYPE_VARIANT) {
dbus.message_iter_recurse (&iter, &iter);
current_type = dbus.message_iter_get_arg_type (&iter);
2020-02-03 22:31:44 +00:00
}
if (current_type != DBUS_TYPE_ARRAY) {
#ifndef NDEBUG
2020-02-03 22:31:44 +00:00
std::cerr << "Not an array: '" << (char)current_type << "'" << std::endl;
#endif
2020-02-03 22:31:44 +00:00
return false;
}
char *val = nullptr;
dbus.message_iter_recurse (&iter, &subiter);
2020-02-03 22:31:44 +00:00
entries.clear();
while ((current_type = dbus.message_iter_get_arg_type (&subiter)) != DBUS_TYPE_INVALID) {
2020-02-03 22:31:44 +00:00
if (current_type == DBUS_TYPE_STRING)
{
dbus.message_iter_get_basic (&subiter, &val);
2020-02-03 22:31:44 +00:00
entries.push_back(val);
}
dbus.message_iter_next (&subiter);
2020-02-03 22:31:44 +00:00
}
return true;
}
static bool get_variant_string(libdbus_loader& dbus, DBusMessageIter *iter_, std::string &val, bool key_or_value = false)
2020-02-03 22:31:44 +00:00
{
DBusMessageIter iter = *iter_;
char *str = nullptr;
int type = dbus.message_iter_get_arg_type (&iter);
2020-02-03 22:31:44 +00:00
if (type != DBUS_TYPE_VARIANT && type != DBUS_TYPE_DICT_ENTRY)
return false;
dbus.message_iter_recurse (&iter, &iter);
2020-02-03 22:31:44 +00:00
if (key_or_value) {
dbus.message_iter_next (&iter);
if (!check_msg_arg (dbus, &iter, DBUS_TYPE_VARIANT))
2020-02-03 22:31:44 +00:00
return false;
dbus.message_iter_recurse (&iter, &iter);
2020-02-03 22:31:44 +00:00
}
if (!check_msg_arg (dbus, &iter, DBUS_TYPE_STRING))
2020-02-03 22:31:44 +00:00
return false;
dbus.message_iter_get_basic(&iter, &str);
2020-02-03 22:31:44 +00:00
val = str;
return true;
}
static bool get_variant_string(libdbus_loader& dbus, DBusMessage *msg, std::string &val, bool key_or_value = false)
2020-02-03 22:31:44 +00:00
{
DBusMessageIter iter;
dbus.message_iter_init (msg, &iter);
return get_variant_string(dbus, &iter, val, key_or_value);
2020-02-03 22:31:44 +00:00
}
static void parse_mpris_metadata(libdbus_loader& dbus, DBusMessageIter *iter_, string_pair_vec& entries)
2020-02-03 22:31:44 +00:00
{
DBusMessageIter subiter, iter = *iter_;
std::string key, val;
std::vector<std::string> list;
while (dbus.message_iter_get_arg_type(&iter) != DBUS_TYPE_INVALID)
2020-02-03 22:31:44 +00:00
{
dbus.message_iter_next (&iter);
//std::cerr << "\ttype: " << (char)dbus.message_iter_get_arg_type(&iter) << std::endl;
if (!get_variant_string(dbus, &iter, key))
2020-02-03 22:31:44 +00:00
return;
dbus.message_iter_recurse (&iter, &subiter);
dbus.message_iter_next (&subiter);
2020-02-03 22:31:44 +00:00
//std::cerr << "\tkey: " << key << std::endl;
if (get_variant_string(dbus, &subiter, val)) {
2020-02-03 22:31:44 +00:00
//std::cerr << "\t\t" << val << std::endl;
entries.push_back({key, val});
}
else if (get_string_array(dbus, &subiter, list)) {
2020-02-03 22:31:44 +00:00
for (auto& s : list) {
//std::cerr << "\t\t" << s << std::endl;
entries.push_back({key, s});
}
}
}
}
static void parse_mpris_properties(libdbus_loader& dbus, DBusMessage *msg, std::string& source, string_pair_vec& entries)
2020-02-03 22:31:44 +00:00
{
const char *val_char = nullptr;
DBusMessageIter iter;
std::string key, val;
std::vector<DBusMessageIter> stack;
stack.push_back({});
dbus.message_iter_init (msg, &stack.back());
2020-02-03 22:31:44 +00:00
// Should be 'org.mpris.MediaPlayer2.Player'
if (!check_msg_arg(dbus, &stack.back(), DBUS_TYPE_STRING))
2020-02-03 22:31:44 +00:00
return;
dbus.message_iter_get_basic(&stack.back(), &val_char);
2020-02-03 22:31:44 +00:00
source = val_char;
if (source != "org.mpris.MediaPlayer2.Player")
return;
dbus.message_iter_next (&stack.back());
//std::cerr << "type: " << (char)dbus.message_iter_get_arg_type(&stack.back()) << std::endl;
if (!check_msg_arg(dbus, &stack.back(), DBUS_TYPE_ARRAY))
2020-02-03 22:31:44 +00:00
return;
dbus.message_iter_recurse (&stack.back(), &iter);
2020-02-03 22:31:44 +00:00
stack.push_back(iter);
while (dbus.message_iter_get_arg_type(&stack.back()) != DBUS_TYPE_INVALID)
2020-02-03 22:31:44 +00:00
{
if (!get_variant_string(dbus, &stack.back(), key)) {
dbus.message_iter_next (&stack.back());
2020-02-03 22:31:44 +00:00
continue;
}
if (key == "Metadata") {
#ifndef NDEBUG
std::cerr << __func__ << ": Found Metadata!" << std::endl;
#endif
// dive into Metadata
dbus.message_iter_recurse (&stack.back(), &iter);
2020-02-03 22:31:44 +00:00
// get the array of entries
dbus.message_iter_next (&iter);
if (!check_msg_arg(dbus, &iter, DBUS_TYPE_VARIANT))
2020-02-03 22:31:44 +00:00
continue;
dbus.message_iter_recurse (&iter, &iter);
2020-02-03 22:31:44 +00:00
if (!check_msg_arg(dbus, &iter, DBUS_TYPE_ARRAY))
2020-02-03 22:31:44 +00:00
continue;
dbus.message_iter_recurse (&iter, &iter);
2020-02-03 22:31:44 +00:00
parse_mpris_metadata(dbus, &iter, entries);
2020-02-03 22:31:44 +00:00
}
else if (key == "PlaybackStatus") {
dbus.message_iter_recurse (&stack.back(), &iter);
dbus.message_iter_next (&iter);
2020-02-03 22:31:44 +00:00
if (get_variant_string(dbus, &iter, val))
2020-02-03 22:31:44 +00:00
entries.push_back({key, val});
}
dbus.message_iter_next (&stack.back());
2020-02-03 22:31:44 +00:00
}
}
static void parse_property_changed(libdbus_loader& dbus, DBusMessage *msg, std::string& source, string_pair_vec& entries)
2020-02-03 22:31:44 +00:00
{
char *name = nullptr;
int i;
uint64_t u64;
double d;
std::vector<DBusMessageIter> stack;
stack.push_back({});
#ifndef NDEBUG
std::vector<char> padding;
padding.push_back('\0');
2020-02-03 22:31:44 +00:00
#endif
dbus.message_iter_init (msg, &stack.back());
2020-02-03 22:31:44 +00:00
int type, prev_type = 0;
type = dbus.message_iter_get_arg_type (&stack.back());
2020-02-03 22:31:44 +00:00
if (type != DBUS_TYPE_STRING) {
#ifndef NDEBUG
2020-02-03 22:31:44 +00:00
std::cerr << __func__ << "First element is not a string" << std::endl;
#endif
2020-02-03 22:31:44 +00:00
return;
}
dbus.message_iter_get_basic(&stack.back(), &name);
2020-02-03 22:31:44 +00:00
source = name;
#ifndef NDEBUG
std::cout << name << std::endl;
#endif
std::pair<std::string, std::string> kv;
dbus.message_iter_next (&stack.back());
// the loop should be able parse the whole message if used for generic use-cases
while ((type = dbus.message_iter_get_arg_type (&stack.back())) != DBUS_TYPE_INVALID) {
2020-02-03 22:31:44 +00:00
#ifndef NDEBUG
padding.back() = ' ';
padding.resize(stack.size() + 1, ' ');
padding.back() = '\0';
std::cout << padding.data() << "Type: " << (char)type;
2020-02-03 22:31:44 +00:00
#endif
if (type == DBUS_TYPE_STRING) {
dbus.message_iter_get_basic(&stack.back(), &name);
2020-02-03 22:31:44 +00:00
#ifndef NDEBUG
std::cout << "=" << name << std::endl;
#endif
if (prev_type == DBUS_TYPE_DICT_ENTRY) // is key ?
kv.first = name;
if (prev_type == DBUS_TYPE_VARIANT || prev_type == DBUS_TYPE_ARRAY) { // is value ?
kv.second = name;
entries.push_back(kv);
}
}
else if (type == DBUS_TYPE_INT32) {
dbus.message_iter_get_basic(&stack.back(), &i);
2020-02-03 22:31:44 +00:00
#ifndef NDEBUG
std::cout << "=" << i << std::endl;
#endif
}
else if (type == DBUS_TYPE_UINT64) {
dbus.message_iter_get_basic(&stack.back(), &u64);
2020-02-03 22:31:44 +00:00
#ifndef NDEBUG
std::cout << "=" << u64 << std::endl;
#endif
}
else if (type == DBUS_TYPE_DOUBLE) {
dbus.message_iter_get_basic(&stack.back(), &d);
2020-02-03 22:31:44 +00:00
#ifndef NDEBUG
std::cout << "=" << d << std::endl;
#endif
}
else if (type == DBUS_TYPE_ARRAY || type == DBUS_TYPE_DICT_ENTRY || type == DBUS_TYPE_VARIANT) {
#ifndef NDEBUG
std::cout << std::endl;
#endif
prev_type = type;
DBusMessageIter iter;
dbus.message_iter_recurse (&stack.back(), &iter);
if (dbus.message_iter_get_arg_type (&stack.back()) != DBUS_TYPE_INVALID)
2020-02-03 22:31:44 +00:00
stack.push_back(iter);
continue;
} else {
#ifndef NDEBUG
std::cout << std::endl;
#endif
}
while(FALSE == dbus.message_iter_next (&stack.back()) && stack.size() > 1) {
2020-02-03 22:31:44 +00:00
stack.pop_back();
prev_type = 0;
}
}
}
bool get_dict_string_array(libdbus_loader& dbus, DBusMessage *msg, string_pair_vec& entries)
2020-02-03 22:31:44 +00:00
{
DBusMessageIter iter, outer_iter;
dbus.message_iter_init (msg, &outer_iter);
2020-02-03 22:31:44 +00:00
int current_type = DBUS_TYPE_INVALID;
current_type = dbus.message_iter_get_arg_type (&outer_iter);
2020-02-03 22:31:44 +00:00
if (current_type == DBUS_TYPE_VARIANT) {
dbus.message_iter_recurse (&outer_iter, &outer_iter);
current_type = dbus.message_iter_get_arg_type (&outer_iter);
2020-02-03 22:31:44 +00:00
}
if (current_type != DBUS_TYPE_ARRAY) {
#ifndef NDEBUG
2020-02-03 22:31:44 +00:00
std::cerr << "Not an array " << (char)current_type << std::endl;
#endif
2020-02-03 22:31:44 +00:00
return false;
}
char *val_key = nullptr, *val_value = nullptr;
dbus.message_iter_recurse (&outer_iter, &outer_iter);
while ((current_type = dbus.message_iter_get_arg_type (&outer_iter)) != DBUS_TYPE_INVALID) {
2020-02-03 22:31:44 +00:00
// printf("type: %d\n", current_type);
if (current_type == DBUS_TYPE_DICT_ENTRY)
{
dbus.message_iter_recurse (&outer_iter, &iter);
2020-02-03 22:31:44 +00:00
// dict entry key
//printf("\tentry: {%c, ", dbus.message_iter_get_arg_type (&iter));
dbus.message_iter_get_basic (&iter, &val_key);
2020-02-03 22:31:44 +00:00
std::string key = val_key;
// dict entry value
dbus.message_iter_next (&iter);
2020-02-03 22:31:44 +00:00
if (dbus.message_iter_get_arg_type (&iter) == DBUS_TYPE_VARIANT)
dbus.message_iter_recurse (&iter, &iter);
2020-02-03 22:31:44 +00:00
if (dbus.message_iter_get_arg_type (&iter) == DBUS_TYPE_ARRAY) {
dbus.message_iter_recurse (&iter, &iter);
if (dbus.message_iter_get_arg_type (&iter) == DBUS_TYPE_STRING) {
//printf("%c}\n", dbus.message_iter_get_arg_type (&iter));
dbus.message_iter_get_basic (&iter, &val_value);
2020-02-03 22:31:44 +00:00
entries.push_back({val_key, val_value});
}
}
else if (dbus.message_iter_get_arg_type (&iter) == DBUS_TYPE_STRING) {
//printf("%c}\n", dbus.message_iter_get_arg_type (&iter));
dbus.message_iter_get_basic (&iter, &val_value);
2020-02-03 22:31:44 +00:00
entries.push_back({val_key, val_value});
}
}
dbus.message_iter_next (&outer_iter);
2020-02-03 22:31:44 +00:00
}
return true;
}
static void assign_metadata(metadata& meta, string_pair_vec& entries)
{
meta.title.clear();
meta.artists.clear();
meta.album.clear();
2020-02-03 22:31:44 +00:00
std::lock_guard<std::mutex> lk(meta.mutex);
std::vector<std::string> artists;
2020-02-03 22:31:44 +00:00
meta.valid = false;
for (auto& kv : entries) {
#ifndef NDEBUG
std::cerr << kv.first << " = " << kv.second << std::endl;
#endif
if (kv.first == "xesam:artist") {
artists.push_back(kv.second);
2020-02-03 22:31:44 +00:00
}
else if (kv.first == "xesam:title")
meta.title = kv.second;
else if (kv.first == "xesam:album")
meta.album = kv.second;
else if (kv.first == "mpris:artUrl")
meta.artUrl = kv.second;
else if (kv.first == "PlaybackStatus")
meta.playing = (kv.second == "Playing");
}
// XXX Spotify only sends one artist anyway
for (auto p = artists.begin(); p != artists.end(); p++) {
meta.artists += *p;
if (p != artists.end() - 1)
meta.artists += ", ";
}
2020-02-03 22:31:44 +00:00
if (meta.artists.size() || !meta.title.empty())
meta.valid = meta.playing;
meta.ticker.needs_recalc = true;
meta.ticker.pos = 0;
meta.ticker.longest = 0;
meta.ticker.dir = -1;
2020-02-03 22:31:44 +00:00
}
void dbus_get_spotify_property(dbusmgr::dbus_manager& dbus_mgr, string_pair_vec& entries, const char * prop)
2020-02-03 22:31:44 +00:00
{
auto& dbus = dbus_mgr.dbus();
2020-02-03 22:31:44 +00:00
DBusError error;
dbus.error_init(&error);
2020-02-03 22:31:44 +00:00
DBusMessage * dbus_reply = nullptr;
DBusMessage * dbus_msg = nullptr;
// 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("org.mpris.MediaPlayer2.spotify", "/org/mpris/MediaPlayer2", "org.freedesktop.DBus.Properties", "Get"))) {
2020-02-03 22:31:44 +00:00
throw std::runtime_error("unable to allocate memory for dbus message");
}
const char *v_STRINGS[] = {
"org.mpris.MediaPlayer2.Player",
};
std::cerr << __func__ << ": " << prop << std::endl;
if (!dbus.message_append_args (dbus_msg, DBUS_TYPE_STRING, &v_STRINGS[0], DBUS_TYPE_STRING, &prop, DBUS_TYPE_INVALID)) {
dbus.message_unref(dbus_msg);
2020-02-03 22:31:44 +00:00
throw std::runtime_error(error.message);
}
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);
2020-02-03 22:31:44 +00:00
}
std::string entry;
if (get_dict_string_array(dbus, dbus_reply, entries)) {
2020-02-03 22:31:44 +00:00
// nothing
} else if (get_variant_string(dbus, dbus_reply, entry)) {
2020-02-03 22:31:44 +00:00
entries.push_back({prop, entry});
}
dbus.message_unref(dbus_msg);
dbus.message_unref(dbus_reply);
dbus.error_free(&error);
2020-02-03 22:31:44 +00:00
}
void get_spotify_metadata(dbusmgr::dbus_manager& dbus, metadata& meta)
{
meta.artists.clear();
string_pair_vec entries;
dbus_get_spotify_property(dbus, entries, "Metadata");
dbus_get_spotify_property(dbus, entries, "PlaybackStatus");
assign_metadata(meta, entries);
}
namespace dbusmgr {
void dbus_manager::init()
{
2020-04-05 10:48:16 +00:00
if (m_inited)
return;
if (!m_dbus_ldr.Load("libdbus-1.so.3"))
throw std::runtime_error("Could not load libdbus-1.so.3");
m_dbus_ldr.error_init(&m_error);
2020-02-03 22:31:44 +00:00
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);
2020-02-03 22:31:44 +00:00
}
std::cout << "Connected to D-Bus as \"" << m_dbus_ldr.bus_get_unique_name(m_dbus_conn) << "\"." << std::endl;
2020-02-03 22:31:44 +00:00
connect_to_signals();
m_inited = true;
}
dbus_manager::~dbus_manager()
{
// unreference system bus connection instead of closing it
if (m_dbus_conn) {
disconnect_from_signals();
m_dbus_ldr.connection_unref(m_dbus_conn);
2020-02-03 22:31:44 +00:00
m_dbus_conn = nullptr;
}
m_dbus_ldr.error_free(&m_error);
2020-02-03 22:31:44 +00:00
}
void dbus_manager::connect_to_signals()
{
for (auto kv : m_signals) {
auto signal = format_signal(kv);
m_dbus_ldr.bus_add_match(m_dbus_conn, signal.c_str(), &m_error);
if (m_dbus_ldr.error_is_set(&m_error)) {
2020-02-03 22:31:44 +00:00
::perror(m_error.name);
::perror(m_error.message);
m_dbus_ldr.error_free(&m_error);
2020-02-03 22:31:44 +00:00
//return;
}
}
start_thread();
}
void dbus_manager::disconnect_from_signals()
{
for (auto kv : m_signals) {
auto signal = format_signal(kv);
m_dbus_ldr.bus_remove_match(m_dbus_conn, signal.c_str(), &m_error);
if (m_dbus_ldr.error_is_set(&m_error)) {
2020-02-03 22:31:44 +00:00
::perror(m_error.name);
::perror(m_error.message);
m_dbus_ldr.error_free(&m_error);
2020-02-03 22:31:44 +00:00
}
}
stop_thread();
}
void dbus_manager::add_callback(CBENUM type, callback_func func)
{
m_callbacks[type] = func;
}
void dbus_manager::stop_thread()
{
m_quit = true;
if (m_thread.joinable())
m_thread.join();
}
void dbus_manager::start_thread()
{
stop_thread();
m_quit = false;
m_thread = std::thread(dbus_thread, this);
}
void dbus_manager::dbus_thread(dbus_manager *pmgr)
{
(void)parse_property_changed;
2020-02-03 22:31:44 +00:00
DBusMessage *msg = nullptr;
auto& dbus = pmgr->dbus();
2020-02-03 22:31:44 +00:00
// loop listening for signals being emmitted
while (!pmgr->m_quit) {
// non blocking read of the next available message
if (!dbus.connection_read_write(pmgr->m_dbus_conn, 0))
2020-02-03 22:31:44 +00:00
return; // connection closed
msg = dbus.connection_pop_message(pmgr->m_dbus_conn);
2020-02-03 22:31:44 +00:00
// loop again if we haven't read a message
if (nullptr == msg) {
std::this_thread::sleep_for(ms(10));
continue;
}
for (auto& sig : pmgr->m_signals) {
if (dbus.message_is_signal(msg, sig.intf, sig.signal))
2020-02-03 22:31:44 +00:00
{
#ifndef NDEBUG
std::cerr << __func__ << ": " << sig.intf << "::" << sig.signal << std::endl;
#endif
switch (sig.type) {
case ST_PROPERTIESCHANGED:
{
std::string source;
std::vector<std::pair<std::string, std::string>> entries;
//parse_property_changed(msg, source, entries);
parse_mpris_properties(dbus, msg, source, entries);
2020-02-03 22:31:44 +00:00
#ifndef NDEBUG
std::cerr << "Source: " << source << std::endl;
#endif
if (source != "org.mpris.MediaPlayer2.Player")
break;
assign_metadata(spotify, entries);
}
break;
case ST_NAMEOWNERCHANGED:
{
DBusMessageIter iter;
dbus.message_iter_init (msg, &iter);
2020-02-03 22:31:44 +00:00
std::vector<std::string> str;
const char *value = nullptr;
while (dbus.message_iter_get_arg_type (&iter) == DBUS_TYPE_STRING) {
dbus.message_iter_get_basic (&iter, &value);
2020-02-03 22:31:44 +00:00
str.push_back(value);
dbus.message_iter_next (&iter);
2020-02-03 22:31:44 +00:00
}
// did spotify quit?
if (str.size() == 3
&& str[0] == "org.mpris.MediaPlayer2.spotify"
&& str[2].empty()
)
{
spotify.valid = false;
}
}
break;
default:
break;
}
}
}
// free the message
dbus.message_unref(msg);
2020-02-03 22:31:44 +00:00
}
}
dbus_manager dbus_mgr;
}