-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathaudacious-plugin-rpc.cc
148 lines (119 loc) · 3.98 KB
/
audacious-plugin-rpc.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <iostream>
#include <string.h>
#include <libaudcore/drct.h>
#include <libaudcore/i18n.h>
#include <libaudcore/plugin.h>
#include <libaudcore/hook.h>
#include <libaudcore/audstrings.h>
#include <libaudcore/tuple.h>
#include <libaudcore/preferences.h>
#include <libaudcore/runtime.h>
#include <discord_rpc.h>
#define EXPORT __attribute__((visibility("default")))
#define APPLICATION_ID "484736379171897344"
static const char *SETTING_EXTRA_TEXT = "extra_text";
class RPCPlugin : public GeneralPlugin {
public:
static const char about[];
static const PreferencesWidget widgets[];
static const PluginPreferences prefs;
static constexpr PluginInfo info = {
N_("Discord RPC"),
"audacious-plugin-rpc",
about,
&prefs
};
constexpr RPCPlugin() : GeneralPlugin (info, false) {}
bool init();
void cleanup();
};
EXPORT RPCPlugin aud_plugin_instance;
DiscordEventHandlers handlers;
DiscordRichPresence presence;
std::string fullTitle;
std::string playingStatus;
void init_discord() {
memset(&handlers, 0, sizeof(handlers));
Discord_Initialize(APPLICATION_ID, &handlers, 1, NULL);
}
void update_presence() {
Discord_UpdatePresence(&presence);
}
void init_presence() {
memset(&presence, 0, sizeof(presence));
presence.state = "Initialized";
presence.details = "Waiting...";
presence.largeImageKey = "logo";
presence.smallImageKey = "stop";
update_presence();
}
void cleanup_discord() {
Discord_ClearPresence();
Discord_Shutdown();
}
void title_changed() {
if (!aud_drct_get_ready()) {
return;
}
if (aud_drct_get_playing()) {
bool paused = aud_drct_get_paused();
Tuple tuple = aud_drct_get_tuple();
String artist = tuple.get_str(Tuple::Artist);
std::string title(tuple.get_str(Tuple::Title));
if (artist) {
fullTitle = (std::string(artist) + " - " + title).substr(0, 127);
} else {
fullTitle = title.substr(0, 127);
}
playingStatus = paused ? "Paused" : "Listening";
presence.details = fullTitle.c_str();
presence.smallImageKey = paused ? "pause" : "play";
} else {
playingStatus = "Stopped";
presence.state = "Stopped";
presence.smallImageKey = "stop";
}
std::string extraText(aud_get_str("audacious-plugin-rpc", SETTING_EXTRA_TEXT));
playingStatus = (playingStatus + " " + extraText).substr(0, 127);
presence.state = playingStatus.c_str();
update_presence();
}
void update_title_presence(void*, void*) {
title_changed();
}
void open_github() {
system("xdg-open https://github.com/darktohka/audacious-plugin-rpc");
}
bool RPCPlugin::init() {
init_discord();
init_presence();
hook_associate("playback ready", update_title_presence, nullptr);
hook_associate("playback end", update_title_presence, nullptr);
hook_associate("playback stop", update_title_presence, nullptr);
hook_associate("playback pause", update_title_presence, nullptr);
hook_associate("playback unpause", update_title_presence, nullptr);
hook_associate("title change", update_title_presence, nullptr);
return true;
}
void RPCPlugin::cleanup() {
hook_dissociate("playback ready", update_title_presence);
hook_dissociate("playback end", update_title_presence);
hook_dissociate("playback stop", update_title_presence);
hook_dissociate("playback pause", update_title_presence);
hook_dissociate("playback unpause", update_title_presence);
hook_dissociate("title change", update_title_presence);
cleanup_discord();
}
const char RPCPlugin::about[] = N_("Discord RPC music status plugin\n\nWritten by: Derzsi Daniel <daniel@tohka.us>");
const PreferencesWidget RPCPlugin::widgets[] =
{
WidgetEntry(
N_("Extra status text:"),
WidgetString("audacious-plugin-rpc", SETTING_EXTRA_TEXT, title_changed)
),
WidgetButton(
N_("Fork on GitHub"),
{open_github}
)
};
const PluginPreferences RPCPlugin::prefs = {{ widgets }};