forked from sonicnext-dev/MarathonRecomp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmedia_linux.cpp
More file actions
328 lines (282 loc) · 8.79 KB
/
Copy pathmedia_linux.cpp
File metadata and controls
328 lines (282 loc) · 8.79 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include <algorithm>
#include <atomic>
#include <optional>
#include <string>
#include <thread>
#include <unordered_map>
#include <ranges>
#include <gio/gio.h>
#include <os/media.h>
#include <os/logger.h>
enum class PlaybackStatus
{
Stopped,
Playing,
Paused
};
static const char* DBusInterface = "org.freedesktop.DBus";
static const char* DBusPropertiesInterface = "org.freedesktop.DBus.Properties";
static const char* DBusPath = "/org/freedesktop/DBus";
static const char* MPRIS2Interface = "org.mpris.MediaPlayer2";
static const char* MPRIS2PlayerInterface = "org.mpris.MediaPlayer2.Player";
static const char* MPRIS2Path = "/org/mpris/MediaPlayer2";
static std::optional<std::thread> g_dbusThread;
static std::unordered_map<std::string, PlaybackStatus> g_playerStatus;
static std::atomic<bool> g_isPlaying = false;
static PlaybackStatus PlaybackStatusFromString(const char* str)
{
if (g_str_equal(str, "Playing"))
return PlaybackStatus::Playing;
else if (g_str_equal(str, "Paused"))
return PlaybackStatus::Paused;
else
return PlaybackStatus::Stopped;
}
static void UpdateActiveStatus()
{
g_isPlaying = std::ranges::any_of(
g_playerStatus | std::views::values,
[](PlaybackStatus status) { return status == PlaybackStatus::Playing; }
);
}
static void UpdateActivePlayers(const char* name, PlaybackStatus status)
{
g_playerStatus.insert_or_assign(name, status);
UpdateActiveStatus();
}
static PlaybackStatus MPRISGetPlaybackStatus(GDBusConnection* connection, const gchar* name)
{
GError* error;
GVariant* response;
GVariant* tupleChild;
GVariant* value;
PlaybackStatus status;
error = NULL;
response = g_dbus_connection_call_sync(
connection,
name,
MPRIS2Path,
DBusPropertiesInterface,
"Get",
g_variant_new("(ss)", MPRIS2PlayerInterface, "PlaybackStatus"),
G_VARIANT_TYPE("(v)"),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error
);
if (!response)
{
LOGF_ERROR("Failed to process D-Bus Get: {}", error->message);
g_clear_error(&error);
return PlaybackStatus::Stopped;
}
tupleChild = g_variant_get_child_value(response, 0);
value = g_variant_get_variant(tupleChild);
if (!g_variant_is_of_type(value, G_VARIANT_TYPE_STRING))
{
LOG_ERROR("Failed to process D-Bus Get");
g_variant_unref(tupleChild);
return PlaybackStatus::Stopped;
}
status = PlaybackStatusFromString(g_variant_get_string(value, NULL));
g_variant_unref(value);
g_variant_unref(tupleChild);
g_variant_unref(response);
return status;
}
// Something is very wrong with the system if this happens
static void DBusConnectionClosed(GDBusConnection* connection,
gboolean remotePeerVanished,
GError* error,
gpointer userData)
{
LOG_ERROR("D-Bus connection closed");
g_isPlaying = false;
g_main_loop_quit((GMainLoop*)userData);
}
static void DBusNameOwnerChanged(GDBusConnection* connection,
const gchar* senderName,
const gchar* objectPath,
const gchar* interfaceName,
const gchar* signalName,
GVariant* parameters,
gpointer userData)
{
const char* name;
const char* oldOwner;
const char* newOwner;
g_variant_get(parameters, "(&s&s&s)", &name, &oldOwner, &newOwner);
if (g_str_has_prefix(name, MPRIS2Interface))
{
if (oldOwner[0])
{
g_playerStatus.erase(oldOwner);
}
UpdateActiveStatus();
}
}
static void MPRISPropertiesChanged(GDBusConnection* connection,
const gchar* senderName,
const gchar* objectPath,
const gchar* interfaceName,
const gchar* signalName,
GVariant* parameters,
gpointer userData)
{
const char* interface;
GVariant* changed;
GVariantIter iter;
const char* key;
GVariant* value;
PlaybackStatus playbackStatus;
g_variant_get_child(parameters, 0, "&s", &interface);
g_variant_get_child(parameters, 1, "@a{sv}", &changed);
g_variant_iter_init(&iter, changed);
while (g_variant_iter_next(&iter, "{&sv}", &key, &value))
{
if (g_str_equal(key, "PlaybackStatus"))
{
playbackStatus = PlaybackStatusFromString(g_variant_get_string(value, NULL));
UpdateActivePlayers(senderName, playbackStatus);
g_variant_unref(value);
break;
}
g_variant_unref(value);
}
g_variant_unref(changed);
}
/* Called upon CONNECT to discover already active MPRIS2 players by looking for
well-known bus names that begin with the MPRIS2 path.
g_playerStatus stores unique connection names,
not their well-known ones, as the PropertiesChanged signal only provides the
former. */
static void DBusListNamesReceived(GObject* object, GAsyncResult* res, gpointer userData)
{
GDBusConnection* connection;
GError* error;
GVariant* response;
GVariant* tupleChild;
GVariantIter iter;
const gchar* name;
connection = G_DBUS_CONNECTION(object);
error = NULL;
response = g_dbus_connection_call_finish(connection, res, &error);
if (!response)
{
LOGF_ERROR("Failed to process D-Bus ListNames: {}", error->message);
g_clear_error(&error);
return;
}
tupleChild = g_variant_get_child_value(response, 0);
g_variant_iter_init(&iter, tupleChild);
while (g_variant_iter_next(&iter, "&s", &name))
{
GVariant* ownerResponse;
const gchar* ownerName;
PlaybackStatus status;
if (!g_str_has_prefix(name, MPRIS2Interface))
continue;
ownerResponse = g_dbus_connection_call_sync(
connection,
DBusInterface,
DBusPath,
DBusInterface,
"GetNameOwner",
g_variant_new("(s)", name),
G_VARIANT_TYPE("(s)"),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error
);
if (!ownerResponse)
{
LOGF_ERROR("Failed to process D-Bus GetNameOwner: {}", error->message);
g_clear_error(&error);
g_variant_unref(tupleChild);
g_variant_unref(response);
return;
}
g_variant_get(ownerResponse, "(&s)", &ownerName);
status = MPRISGetPlaybackStatus(connection, ownerName);
g_playerStatus.insert_or_assign(ownerName, status);
g_variant_unref(ownerResponse);
}
UpdateActiveStatus();
g_variant_unref(tupleChild);
g_variant_unref(response);
}
static void DBusThreadProc()
{
GMainContext* mainContext;
GMainLoop* mainLoop;
GError* error;
GDBusConnection* connection;
mainContext = g_main_context_new();
g_main_context_push_thread_default(mainContext);
mainLoop = g_main_loop_new(mainContext, FALSE);
error = NULL;
connection = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
if (!connection)
{
LOGF_ERROR("Failed to connect to D-Bus: {}", error->message);
g_clear_error(&error);
g_main_context_unref(mainContext);
g_main_loop_unref(mainLoop);
return;
}
g_dbus_connection_set_exit_on_close(connection, FALSE);
g_signal_connect(connection, "closed", G_CALLBACK(DBusConnectionClosed), mainLoop);
// Listen for player connection changes
g_dbus_connection_signal_subscribe(
connection,
DBusInterface,
DBusInterface,
"NameOwnerChanged",
DBusPath,
NULL,
G_DBUS_SIGNAL_FLAGS_NONE,
DBusNameOwnerChanged,
NULL,
NULL
);
// Listen for player status changes
g_dbus_connection_signal_subscribe(
connection,
NULL,
DBusPropertiesInterface,
"PropertiesChanged",
MPRIS2Path,
NULL,
G_DBUS_SIGNAL_FLAGS_NONE,
MPRISPropertiesChanged,
NULL,
NULL
);
// Request list of current players
g_dbus_connection_call(
connection,
DBusInterface,
DBusPath,
DBusInterface,
"ListNames",
NULL,
G_VARIANT_TYPE("(as)"),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
DBusListNamesReceived,
NULL
);
g_main_loop_run(mainLoop);
}
bool os::media::IsExternalMediaPlaying()
{
if (!g_dbusThread)
{
g_dbusThread.emplace(DBusThreadProc);
g_dbusThread->detach();
}
return g_isPlaying;
}