2020-02-03 22:31:44 +00:00
|
|
|
#pragma once
|
2020-07-06 17:31:40 +00:00
|
|
|
#ifndef MANGOHUD_DBUS_INFO_H
|
|
|
|
#define MANGOHUD_DBUS_INFO_H
|
|
|
|
|
2020-04-21 10:29:27 +00:00
|
|
|
#include <array>
|
2020-02-03 22:31:44 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <map>
|
|
|
|
#include <mutex>
|
2020-07-02 17:37:45 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
|
2020-04-04 20:47:48 +00:00
|
|
|
#include "loaders/loader_dbus.h"
|
2020-02-03 22:31:44 +00:00
|
|
|
|
|
|
|
struct metadata {
|
2020-07-02 17:37:45 +00:00
|
|
|
// std::vector<std::string> artists;
|
|
|
|
std::string artists; // pre-concatenate
|
2020-02-03 22:31:44 +00:00
|
|
|
std::string title;
|
|
|
|
std::string album;
|
|
|
|
std::string something;
|
|
|
|
std::string artUrl;
|
|
|
|
bool playing = false;
|
2020-07-02 17:37:45 +00:00
|
|
|
|
2020-02-03 22:31:44 +00:00
|
|
|
bool valid = false;
|
2020-07-01 11:19:06 +00:00
|
|
|
bool got_song_data = false;
|
|
|
|
bool got_playback_data = false;
|
2020-02-03 22:31:44 +00:00
|
|
|
};
|
|
|
|
|
2021-09-09 17:52:52 +00:00
|
|
|
struct mp_fmt
|
|
|
|
{
|
|
|
|
std::string text;
|
|
|
|
float width;
|
|
|
|
};
|
|
|
|
|
2020-07-01 02:28:02 +00:00
|
|
|
struct mutexed_metadata {
|
|
|
|
std::mutex mtx;
|
|
|
|
metadata meta;
|
2020-07-01 13:17:07 +00:00
|
|
|
struct {
|
|
|
|
float pos;
|
|
|
|
float longest;
|
|
|
|
int dir = -1;
|
|
|
|
bool needs_recalc = true;
|
|
|
|
|
2021-09-09 17:52:52 +00:00
|
|
|
std::vector<mp_fmt> formatted;
|
2020-07-01 13:17:07 +00:00
|
|
|
} ticker;
|
2020-07-01 02:28:02 +00:00
|
|
|
};
|
|
|
|
|
2020-07-02 17:37:45 +00:00
|
|
|
enum SignalType {
|
2020-02-03 22:31:44 +00:00
|
|
|
ST_NAMEOWNERCHANGED,
|
|
|
|
ST_PROPERTIESCHANGED,
|
|
|
|
};
|
|
|
|
|
2020-07-01 02:28:02 +00:00
|
|
|
extern struct mutexed_metadata main_metadata;
|
2020-02-03 22:31:44 +00:00
|
|
|
|
|
|
|
namespace dbusmgr {
|
2020-06-30 14:24:49 +00:00
|
|
|
|
2020-07-02 17:37:45 +00:00
|
|
|
class dbus_manager;
|
|
|
|
using signal_handler_func = bool (dbus_manager::*)(DBusMessage*, const char*);
|
2020-02-03 22:31:44 +00:00
|
|
|
|
2021-08-21 15:12:12 +00:00
|
|
|
enum Service
|
|
|
|
{
|
|
|
|
SRV_NONE = 0,
|
|
|
|
SRV_MPRIS = (1ul << 0),
|
|
|
|
SRV_GAMEMODE = (1ul << 1),
|
|
|
|
SRV_ALL = 0xFFFFFFFF,
|
|
|
|
};
|
|
|
|
|
2020-07-02 17:37:45 +00:00
|
|
|
struct DBusSignal {
|
2021-08-21 15:12:12 +00:00
|
|
|
Service srv;
|
2020-07-02 17:37:45 +00:00
|
|
|
const char* intf;
|
|
|
|
const char* signal;
|
|
|
|
signal_handler_func handler;
|
|
|
|
};
|
2020-02-03 22:31:44 +00:00
|
|
|
|
2020-07-02 17:37:45 +00:00
|
|
|
class dbus_manager {
|
|
|
|
public:
|
|
|
|
dbus_manager() {}
|
|
|
|
|
|
|
|
~dbus_manager();
|
|
|
|
|
2021-08-21 15:12:12 +00:00
|
|
|
bool init(Service srv);
|
|
|
|
bool init_mpris(const std::string& requested_player);
|
|
|
|
void deinit(Service srv);
|
2020-07-02 17:37:45 +00:00
|
|
|
bool get_media_player_metadata(metadata& meta, std::string name = "");
|
2021-08-21 15:12:12 +00:00
|
|
|
void connect_to_signals(Service srv);
|
|
|
|
void disconnect_from_signals(Service srv);
|
2020-07-02 17:37:45 +00:00
|
|
|
DBusConnection* get_conn() const { return m_dbus_conn; }
|
|
|
|
|
2021-08-21 15:12:12 +00:00
|
|
|
bool gamemode_enabled(int32_t pid);
|
|
|
|
|
2020-07-02 17:37:45 +00:00
|
|
|
libdbus_loader& dbus() { return m_dbus_ldr; }
|
|
|
|
|
|
|
|
protected:
|
2021-08-21 15:12:12 +00:00
|
|
|
bool init_internal();
|
2020-07-02 17:37:45 +00:00
|
|
|
void stop_thread();
|
|
|
|
void start_thread();
|
|
|
|
void dbus_thread();
|
|
|
|
|
|
|
|
bool dbus_list_name_to_owner();
|
|
|
|
bool select_active_player();
|
|
|
|
|
|
|
|
static DBusHandlerResult filter_signals(DBusConnection*, DBusMessage*,
|
|
|
|
void*);
|
|
|
|
|
|
|
|
bool handle_properties_changed(DBusMessage*, const char*);
|
|
|
|
bool handle_name_owner_changed(DBusMessage*, const char*);
|
2021-08-21 15:12:12 +00:00
|
|
|
bool handle_game_registered(DBusMessage*, const char*);
|
|
|
|
bool handle_game_unregistered(DBusMessage*, const char*);
|
2020-07-02 17:37:45 +00:00
|
|
|
|
|
|
|
void onNewPlayer(
|
|
|
|
metadata& meta); // A different player has become the active player
|
|
|
|
void onNoPlayer(); // There is no longer any player active
|
|
|
|
void onPlayerUpdate(
|
|
|
|
metadata& meta); // The active player has sent an update
|
|
|
|
|
|
|
|
DBusError m_error;
|
|
|
|
DBusConnection* m_dbus_conn = nullptr;
|
|
|
|
bool m_quit = false;
|
|
|
|
bool m_inited = false;
|
|
|
|
std::thread m_thread;
|
|
|
|
libdbus_loader m_dbus_ldr;
|
|
|
|
std::unordered_map<std::string, std::string> m_name_owners;
|
|
|
|
std::string m_requested_player;
|
|
|
|
std::string m_active_player;
|
2021-08-21 15:12:12 +00:00
|
|
|
uint32_t m_active_srvs = SRV_NONE;
|
2020-07-02 17:37:45 +00:00
|
|
|
|
|
|
|
const std::array<DBusSignal, 2> m_signals{{
|
2021-08-21 15:12:12 +00:00
|
|
|
{SRV_MPRIS, "org.freedesktop.DBus", "NameOwnerChanged",
|
2020-07-02 17:37:45 +00:00
|
|
|
&dbus_manager::handle_name_owner_changed},
|
2021-08-21 15:12:12 +00:00
|
|
|
{SRV_MPRIS, "org.freedesktop.DBus.Properties", "PropertiesChanged",
|
2020-07-02 17:37:45 +00:00
|
|
|
&dbus_manager::handle_properties_changed},
|
2021-08-21 15:12:12 +00:00
|
|
|
// {SRV_GAMEMODE, "com.feralinteractive.GameMode", "GameRegistered",
|
|
|
|
// &dbus_manager::handle_game_registered},
|
|
|
|
// {SRV_GAMEMODE, "com.feralinteractive.GameMode", "GameUnregistered",
|
|
|
|
// &dbus_manager::handle_game_unregistered},
|
2020-07-02 17:37:45 +00:00
|
|
|
}};
|
|
|
|
};
|
2020-02-03 22:31:44 +00:00
|
|
|
|
2020-07-02 17:37:45 +00:00
|
|
|
extern dbus_manager dbus_mgr;
|
|
|
|
} // namespace dbusmgr
|
2020-05-22 12:00:35 +00:00
|
|
|
bool get_media_player_metadata(dbusmgr::dbus_manager& dbus, const std::string& name, metadata& meta);
|
2020-07-06 17:31:40 +00:00
|
|
|
|
|
|
|
#endif //MANGOHUD_DBUS_INFO_H
|