-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathMPRISServiceHandler.cpp
721 lines (626 loc) · 23.4 KB
/
MPRISServiceHandler.cpp
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "MPRISServiceHandler.h"
#include <stdint.h>
#include <inttypes.h>
#include <unordered_map>
#include "MPRISInterfaceDescription.h"
#include "mozilla/dom/MediaControlUtils.h"
#include "mozilla/Maybe.h"
#include "mozilla/Sprintf.h"
#include "nsIXULAppInfo.h"
// avoid redefined macro in unified build
#undef LOG
#define LOG(msg, ...) \
MOZ_LOG(gMediaControlLog, LogLevel::Debug, \
("MPRISServiceHandler=%p, " msg, this, ##__VA_ARGS__))
namespace mozilla {
namespace widget {
enum class Method : uint8_t {
eQuit,
eRaise,
eNext,
ePrevious,
ePause,
ePlayPause,
eStop,
ePlay,
eSeek,
eSetPosition,
eOpenUri,
eUnknown
};
static inline Method GetMethod(const gchar* aMethodName) {
const std::unordered_map<std::string, Method> map = {
{"Quit", Method::eQuit}, {"Raise", Method::eRaise},
{"Next", Method::eNext}, {"Previous", Method::ePrevious},
{"Pause", Method::ePause}, {"PlayPause", Method::ePlayPause},
{"Stop", Method::eStop}, {"Play", Method::ePlay},
{"Seek", Method::eSeek}, {"SetPosition", Method::eSetPosition},
{"OpenUri", Method::eOpenUri}};
auto it = map.find(aMethodName);
return (it == map.end() ? Method::eUnknown : it->second);
}
static void HandleMethodCall(GDBusConnection* aConnection, const gchar* aSender,
const gchar* aObjectPath,
const gchar* aInterfaceName,
const gchar* aMethodName, GVariant* aParameters,
GDBusMethodInvocation* aInvocation,
gpointer aUserData) {
MOZ_ASSERT(aUserData);
MOZ_ASSERT(NS_IsMainThread());
MPRISServiceHandler* handler = static_cast<MPRISServiceHandler*>(aUserData);
std::string error;
switch (GetMethod(aMethodName)) {
case Method::eUnknown:
g_dbus_method_invocation_return_error(
aInvocation, G_IO_ERROR, G_IO_ERROR_FAILED, "Invalid Method");
return;
case Method::eQuit:
if (handler->CanQuit()) {
handler->Quit();
} else {
error = "Cannot invoke Quit() when CanQuit() returns false";
}
break;
case Method::eRaise:
if (handler->CanRaise()) {
handler->Raise();
} else {
error = "Cannot invoke Raise() when CanRaise() returns false";
}
break;
case Method::eNext:
if (handler->CanGoNext()) {
handler->Next();
} else {
error = "Cannot invoke Next() when CanGoNext() returns false";
}
break;
case Method::ePrevious:
if (handler->CanGoPrevious()) {
handler->Previous();
} else {
error = "Cannot invoke Previous() when CanGoPrevious() returns false";
}
break;
case Method::ePause:
if (handler->CanPause()) {
handler->Pause();
} else {
error = "Cannot invoke Pause() when CanPause() returns false";
}
break;
case Method::ePlayPause:
// According to Spec this should only fail if canPause is false, but Play
// may be forbidden due to CanPlay. This means in theory even though
// CanPlay is false, this method would be able to Play something which
// means when CanPause is false, CanPlay _has to be_ false as well.
if (handler->CanPlay() && handler->CanPause()) {
handler->PlayPause();
} else {
error =
"Cannot invoke PlayPause() when either CanPlay() or CanPause() "
"returns false";
}
break;
case Method::eStop:
handler->Stop(); // Stop is mandatory
break;
case Method::ePlay:
if (handler->CanPlay()) {
handler->Play();
} else {
error = "Cannot invoke Play() when CanPlay() returns false";
}
break;
case Method::eSeek:
if (handler->CanSeek()) {
gint64 position;
g_variant_get(aParameters, "(x)", &position);
handler->Seek(position);
} else {
error = "Cannot invoke Seek() when CanSeek() returns false";
}
break;
case Method::eSetPosition:
if (handler->CanSeek()) {
gchar* trackId;
gint64 position;
g_variant_get(aParameters, "(ox)", &trackId, &position);
handler->SetPosition(trackId, position);
} else {
error = "Cannot invoke SetPosition() when CanSeek() returns false";
}
break;
case Method::eOpenUri:
gchar* uri;
g_variant_get(aParameters, "(s)", &uri);
if (!handler->OpenUri(uri)) {
error = "Could not open URI";
}
break;
}
if (!error.empty()) {
g_dbus_method_invocation_return_error(
aInvocation, G_IO_ERROR, G_IO_ERROR_READ_ONLY, "%s", error.c_str());
}
}
enum class Property : uint8_t {
eIdentity,
eHasTrackList,
eCanRaise,
eCanQuit,
eSupportedUriSchemes,
eSupportedMimeTypes,
eCanGoNext,
eCanGoPrevious,
eCanPlay,
eCanPause,
eCanSeek,
eCanControl,
eGetVolume,
eGetPosition,
eGetMinimumRate,
eGetMaximumRate,
eGetRate,
eGetPlaybackStatus,
eGetMetadata,
eUnknown
};
static inline Property GetProperty(const gchar* aPropertyName) {
const std::unordered_map<std::string, Property> map = {
{"Identity", Property::eIdentity},
{"HasTrackList", Property::eHasTrackList},
{"CanRaise", Property::eCanRaise},
{"CanQuit", Property::eCanQuit},
{"SupportedUriSchemes", Property::eSupportedUriSchemes},
{"SupportedMimeTypes", Property::eSupportedMimeTypes},
{"CanGoNext", Property::eCanGoNext},
{"CanGoPrevious", Property::eCanGoPrevious},
{"CanPlay", Property::eCanPlay},
{"CanPause", Property::eCanPause},
{"CanSeek", Property::eCanSeek},
{"CanControl", Property::eCanControl},
{"Volume", Property::eGetVolume},
{"Position", Property::eGetPosition},
{"MinimumRate", Property::eGetMinimumRate},
{"MaximumRate", Property::eGetMaximumRate},
{"Rate", Property::eGetRate},
{"PlaybackStatus", Property::eGetPlaybackStatus},
{"Metadata", Property::eGetMetadata}};
auto it = map.find(aPropertyName);
return (it == map.end() ? Property::eUnknown : it->second);
}
static GVariant* HandleGetProperty(GDBusConnection* aConnection,
const gchar* aSender,
const gchar* aObjectPath,
const gchar* aInterfaceName,
const gchar* aPropertyName, GError** aError,
gpointer aUserData) {
MOZ_ASSERT(aUserData);
MOZ_ASSERT(NS_IsMainThread());
MPRISServiceHandler* handler = static_cast<MPRISServiceHandler*>(aUserData);
switch (GetProperty(aPropertyName)) {
case Property::eUnknown:
g_set_error(aError, G_IO_ERROR, G_IO_ERROR_FAILED, "Unknown Property");
return nullptr;
case Property::eIdentity:
return g_variant_new_string(handler->Identity());
case Property::eHasTrackList:
return g_variant_new_boolean(handler->HasTrackList());
case Property::eCanRaise:
return g_variant_new_boolean(handler->CanRaise());
case Property::eCanQuit:
return g_variant_new_boolean(handler->CanQuit());
case Property::eSupportedUriSchemes:
return handler->SupportedUriSchemes();
case Property::eSupportedMimeTypes:
return handler->SupportedMimeTypes();
case Property::eCanGoNext:
return g_variant_new_boolean(handler->CanGoNext());
case Property::eCanGoPrevious:
return g_variant_new_boolean(handler->CanGoPrevious());
case Property::eCanPlay:
return g_variant_new_boolean(handler->CanPlay());
case Property::eCanPause:
return g_variant_new_boolean(handler->CanPause());
case Property::eCanSeek:
return g_variant_new_boolean(handler->CanSeek());
case Property::eCanControl:
return g_variant_new_boolean(handler->CanControl());
case Property::eGetVolume:
return g_variant_new_double(handler->GetVolume());
case Property::eGetPosition:
return g_variant_new_int64(handler->GetPosition());
case Property::eGetMinimumRate:
return g_variant_new_double(handler->GetMinimumRate());
case Property::eGetMaximumRate:
return g_variant_new_double(handler->GetMaximumRate());
case Property::eGetRate:
return g_variant_new_double(handler->GetRate());
case Property::eGetPlaybackStatus:
if (GVariant* state = handler->GetPlaybackStatus()) {
return state;
}
g_set_error(aError, G_IO_ERROR, G_IO_ERROR_FAILED,
"Invalid Playback Status");
return nullptr;
case Property::eGetMetadata:
return handler->GetMetadataAsGVariant();
}
MOZ_ASSERT_UNREACHABLE("Switch Statement incomplete");
return nullptr;
}
static gboolean HandleSetProperty(GDBusConnection* aConnection,
const gchar* aSender,
const gchar* aObjectPath,
const gchar* aInterfaceName,
const gchar* aPropertyName, GVariant* aValue,
GError** aError, gpointer aUserData) {
MOZ_ASSERT(aUserData);
MOZ_ASSERT(NS_IsMainThread());
MPRISServiceHandler* handler = static_cast<MPRISServiceHandler*>(aUserData);
if (g_strcmp0(aPropertyName, "Volume") == 0) {
if (!handler->SetVolume(g_variant_get_double(aValue))) {
g_set_error(aError, G_IO_ERROR, G_IO_ERROR_FAILED,
"Could not set the Volume");
return false;
}
} else if (g_strcmp0(aPropertyName, "Rate") == 0) {
if (!handler->SetRate(g_variant_get_double(aValue))) {
g_set_error(aError, G_IO_ERROR, G_IO_ERROR_FAILED,
"Could not set the Rate");
return false;
}
} else {
g_set_error(aError, G_IO_ERROR, G_IO_ERROR_FAILED, "Unknown Property");
return false;
}
GVariantBuilder
propertiesBuilder; // a builder for the list of changed properties
g_variant_builder_init(&propertiesBuilder, G_VARIANT_TYPE_VARDICT);
g_variant_builder_add(&propertiesBuilder, "{sv}", aPropertyName, aValue);
return g_dbus_connection_emit_signal(
aConnection, nullptr, aObjectPath, "org.freedesktop.DBus.Properties",
"PropertiesChanged",
g_variant_new("(sa{sv}as)", aInterfaceName, &propertiesBuilder, nullptr),
aError);
}
static const GDBusInterfaceVTable gInterfaceVTable = {
HandleMethodCall, HandleGetProperty, HandleSetProperty};
void MPRISServiceHandler::OnNameAcquiredStatic(GDBusConnection* aConnection,
const gchar* aName,
gpointer aUserData) {
MOZ_ASSERT(aUserData);
static_cast<MPRISServiceHandler*>(aUserData)->OnNameAcquired(aConnection,
aName);
}
void MPRISServiceHandler::OnNameLostStatic(GDBusConnection* aConnection,
const gchar* aName,
gpointer aUserData) {
MOZ_ASSERT(aUserData);
static_cast<MPRISServiceHandler*>(aUserData)->OnNameLost(aConnection, aName);
}
void MPRISServiceHandler::OnBusAcquiredStatic(GDBusConnection* aConnection,
const gchar* aName,
gpointer aUserData) {
MOZ_ASSERT(aUserData);
static_cast<MPRISServiceHandler*>(aUserData)->OnBusAcquired(aConnection,
aName);
}
void MPRISServiceHandler::OnNameAcquired(GDBusConnection* aConnection,
const gchar* aName) {
LOG("OnNameAcquired: %s", aName);
mConnection = aConnection;
}
void MPRISServiceHandler::OnNameLost(GDBusConnection* aConnection,
const gchar* aName) {
LOG("OnNameLost: %s", aName);
mConnection = nullptr;
if (!mRootRegistrationId) {
return;
}
if (g_dbus_connection_unregister_object(aConnection, mRootRegistrationId)) {
mRootRegistrationId = 0;
} else {
// Note: Most code examples in the internet probably dont't even check the
// result here, but
// according to the spec it _can_ return false.
LOG("Unable to unregister root object from within onNameLost!");
}
if (!mPlayerRegistrationId) {
return;
}
if (g_dbus_connection_unregister_object(aConnection, mPlayerRegistrationId)) {
mPlayerRegistrationId = 0;
} else {
// Note: Most code examples in the internet probably dont't even check the
// result here, but
// according to the spec it _can_ return false.
LOG("Unable to unregister object from within onNameLost!");
}
}
void MPRISServiceHandler::OnBusAcquired(GDBusConnection* aConnection,
const gchar* aName) {
GError* error = nullptr;
LOG("OnBusAcquired: %s", aName);
mRootRegistrationId = g_dbus_connection_register_object(
aConnection, DBUS_MPRIS_OBJECT_PATH, mIntrospectionData->interfaces[0],
&gInterfaceVTable, this, /* user_data */
nullptr, /* user_data_free_func */
&error); /* GError** */
if (mRootRegistrationId == 0) {
LOG("Failed at root registration: %s",
error ? error->message : "Unknown Error");
if (error) {
g_error_free(error);
}
return;
}
mPlayerRegistrationId = g_dbus_connection_register_object(
aConnection, DBUS_MPRIS_OBJECT_PATH, mIntrospectionData->interfaces[1],
&gInterfaceVTable, this, /* user_data */
nullptr, /* user_data_free_func */
&error); /* GError** */
if (mPlayerRegistrationId == 0) {
LOG("Failed at object registration: %s",
error ? error->message : "Unknown Error");
if (error) {
g_error_free(error);
}
}
}
bool MPRISServiceHandler::Open() {
MOZ_ASSERT(!mInitialized);
MOZ_ASSERT(NS_IsMainThread());
GError* error = nullptr;
gchar serviceName[256];
InitIdentity();
SprintfLiteral(serviceName, DBUS_MRPIS_SERVICE_NAME ".instance%d", getpid());
mOwnerId =
g_bus_own_name(G_BUS_TYPE_SESSION, serviceName,
// Enter a waiting queue until this service name is free
// (likely another FF instance is running/has been crashed)
G_BUS_NAME_OWNER_FLAGS_NONE, OnBusAcquiredStatic,
OnNameAcquiredStatic, OnNameLostStatic, this, nullptr);
/* parse introspection data */
mIntrospectionData = g_dbus_node_info_new_for_xml(introspection_xml, &error);
if (!mIntrospectionData) {
LOG("Failed at parsing XML Interface definition: %s",
error ? error->message : "Unknown Error");
if (error) {
g_error_free(error);
}
return false;
}
mInitialized = true;
return true;
}
MPRISServiceHandler::~MPRISServiceHandler() {
MOZ_ASSERT(!mInitialized); // Close hasn't been called!
}
void MPRISServiceHandler::Close() {
gchar serviceName[256];
SprintfLiteral(serviceName, DBUS_MRPIS_SERVICE_NAME ".instance%d", getpid());
OnNameLost(mConnection, serviceName);
if (mOwnerId != 0) {
g_bus_unown_name(mOwnerId);
}
if (mIntrospectionData) {
g_dbus_node_info_unref(mIntrospectionData);
}
mInitialized = false;
MediaControlKeysEventSource::Close();
}
bool MPRISServiceHandler::IsOpened() const { return mInitialized; }
bool MPRISServiceHandler::HasTrackList() { return false; }
void MPRISServiceHandler::InitIdentity() {
nsresult rv;
nsAutoCString appName;
nsCOMPtr<nsIXULAppInfo> appInfo =
do_GetService("@mozilla.org/xre/app-info;1", &rv);
MOZ_ASSERT(NS_SUCCEEDED(rv));
rv = appInfo->GetVendor(mIdentity);
MOZ_ASSERT(NS_SUCCEEDED(rv));
rv = appInfo->GetName(appName);
MOZ_ASSERT(NS_SUCCEEDED(rv));
mIdentity.Append(' ');
mIdentity.Append(appName);
}
const char* MPRISServiceHandler::Identity() const {
MOZ_ASSERT(mInitialized);
return mIdentity.get();
}
GVariant* MPRISServiceHandler::SupportedUriSchemes() {
GVariantBuilder builder;
g_variant_builder_init(&builder, G_VARIANT_TYPE("as"));
return g_variant_builder_end(&builder);
}
GVariant* MPRISServiceHandler::SupportedMimeTypes() {
GVariantBuilder builder;
g_variant_builder_init(&builder, G_VARIANT_TYPE("as"));
return g_variant_builder_end(&builder);
}
constexpr bool MPRISServiceHandler::CanRaise() { return true; }
constexpr bool MPRISServiceHandler::CanQuit() { return false; }
void MPRISServiceHandler::Quit() {
MOZ_ASSERT_UNREACHABLE("CanQuit is false, this method is not implemented");
}
bool MPRISServiceHandler::CanGoNext() const { return true; }
bool MPRISServiceHandler::CanGoPrevious() const { return true; }
bool MPRISServiceHandler::CanPlay() const { return true; }
bool MPRISServiceHandler::CanPause() const { return true; }
// We don't support Seeking or Setting/Getting the Position yet
bool MPRISServiceHandler::CanSeek() const { return false; }
bool MPRISServiceHandler::CanControl() const {
return true; // we don't support LoopStatus, Shuffle, Rate or Volume, but at
// least KDE blocks Play/Pause when CanControl is false.
}
// We don't support getting the volume (yet) so return a dummy value.
double MPRISServiceHandler::GetVolume() const { return 1.0f; }
// we don't support setting the volume yet, so this is a no-op
bool MPRISServiceHandler::SetVolume(double aVolume) {
if (aVolume > 1.0f || aVolume < 0.0f) {
return false;
}
LOG("Volume set to %f", aVolume);
return true;
}
int64_t MPRISServiceHandler::GetPosition() const { return 0; }
constexpr double MPRISServiceHandler::GetMinimumRate() { return 1.0f; }
constexpr double MPRISServiceHandler::GetMaximumRate() { return 1.0f; }
// Getting and Setting the Rate doesn't work yet, so it will be locked to 1.0
double MPRISServiceHandler::GetRate() const { return 1.0f; }
bool MPRISServiceHandler::SetRate(double aRate) {
if (aRate > GetMaximumRate() || aRate < GetMinimumRate()) {
return false;
}
LOG("Set Playback Rate to %f", aRate);
return true;
}
void MPRISServiceHandler::SetPlaybackState(
dom::MediaSessionPlaybackState aState) {
LOG("SetPlaybackState");
if (mPlaybackState == aState) {
return;
}
MediaControlKeysEventSource::SetPlaybackState(aState);
if (!mConnection) {
return; // No D-Bus Connection, no event
}
GVariant* state = GetPlaybackStatus();
if (!state) {
return; // Invalid state
}
GVariantBuilder builder;
g_variant_builder_init(&builder, G_VARIANT_TYPE("a{sv}"));
g_variant_builder_add(&builder, "{sv}", "PlaybackStatus", state);
GVariant* parameters = g_variant_new(
"(sa{sv}as)", DBUS_MPRIS_PLAYER_INTERFACE, &builder, nullptr);
GError* error = nullptr;
if (!g_dbus_connection_emit_signal(mConnection, nullptr,
DBUS_MPRIS_OBJECT_PATH,
"org.freedesktop.DBus.Properties",
"PropertiesChanged", parameters, &error)) {
LOG("Failed at emitting MPRIS property changes for 'PlaybackStatus': %s",
error ? error->message : "Unknown Error");
if (error) {
g_error_free(error);
}
}
}
GVariant* MPRISServiceHandler::GetPlaybackStatus() const {
switch (GetPlaybackState()) {
case dom::MediaSessionPlaybackState::Playing:
return g_variant_new_string("Playing");
case dom::MediaSessionPlaybackState::Paused:
return g_variant_new_string("Paused");
case dom::MediaSessionPlaybackState::None:
return g_variant_new_string("Stopped");
default:
MOZ_ASSERT_UNREACHABLE("Invalid Playback State");
return nullptr;
}
}
void MPRISServiceHandler::SetMediaMetadata(
const dom::MediaMetadataBase& aMetadata) {
mMetadata = Some(aMetadata);
LOG("Set MediaMetadata: title - %s, Artist - %s, Album - %s",
NS_ConvertUTF16toUTF8(mMetadata->mTitle).get(),
NS_ConvertUTF16toUTF8(mMetadata->mArtist).get(),
NS_ConvertUTF16toUTF8(mMetadata->mAlbum).get());
if (!mConnection) {
LOG("No D-Bus Connection. Drop the update.");
return;
}
GVariantBuilder builder;
g_variant_builder_init(&builder, G_VARIANT_TYPE("a{sv}"));
g_variant_builder_add(&builder, "{sv}", "Metadata", GetMetadataAsGVariant());
GVariant* parameters = g_variant_new(
"(sa{sv}as)", DBUS_MPRIS_PLAYER_INTERFACE, &builder, nullptr);
GError* error = nullptr;
if (!g_dbus_connection_emit_signal(mConnection, nullptr,
DBUS_MPRIS_OBJECT_PATH,
"org.freedesktop.DBus.Properties",
"PropertiesChanged", parameters, &error)) {
LOG("Failed at emitting MPRIS property changes for 'Metadata': %s:",
error ? error->message : "Unknown Error");
if (error) {
g_error_free(error);
}
}
}
GVariant* MPRISServiceHandler::GetMetadataAsGVariant() const {
GVariantBuilder builder;
g_variant_builder_init(&builder, G_VARIANT_TYPE("a{sv}"));
g_variant_builder_add(&builder, "{sv}", "mpris:trackid",
g_variant_new("o", DBUS_MPRIS_TRACK_PATH));
if (mMetadata.isSome()) {
LOG("Get Metadata: title - %s, Artist - %s, Album - %s",
NS_ConvertUTF16toUTF8(mMetadata->mTitle).get(),
NS_ConvertUTF16toUTF8(mMetadata->mArtist).get(),
NS_ConvertUTF16toUTF8(mMetadata->mAlbum).get());
g_variant_builder_add(
&builder, "{sv}", "xesam:title",
g_variant_new_string(NS_ConvertUTF16toUTF8(mMetadata->mTitle).get()));
GVariantBuilder artistBuilder; // Artists is a list.
g_variant_builder_init(&artistBuilder, G_VARIANT_TYPE("as"));
g_variant_builder_add(&artistBuilder, "s",
NS_ConvertUTF16toUTF8(mMetadata->mArtist).get());
g_variant_builder_add(&builder, "{sv}", "xesam:artist",
g_variant_builder_end(&artistBuilder));
}
return g_variant_builder_end(&builder);
}
void MPRISServiceHandler::EmitEvent(mozilla::dom::MediaControlKeysEvent event) {
for (auto& listener : mListeners) {
listener->OnKeyPressed(event);
}
}
void MPRISServiceHandler::Raise() {
LOG("Raise");
EmitEvent(mozilla::dom::MediaControlKeysEvent::eFocus);
}
void MPRISServiceHandler::Next() {
LOG("Next");
EmitEvent(mozilla::dom::MediaControlKeysEvent::eNextTrack);
}
void MPRISServiceHandler::Previous() {
LOG("Previous");
EmitEvent(mozilla::dom::MediaControlKeysEvent::ePrevTrack);
}
void MPRISServiceHandler::Pause() {
LOG("Pause");
EmitEvent(mozilla::dom::MediaControlKeysEvent::ePause);
}
void MPRISServiceHandler::PlayPause() {
LOG("PlayPause");
EmitEvent(mozilla::dom::MediaControlKeysEvent::ePlayPause);
}
void MPRISServiceHandler::Stop() {
LOG("Stop");
EmitEvent(mozilla::dom::MediaControlKeysEvent::eStop);
}
void MPRISServiceHandler::Play() {
LOG("Play");
EmitEvent(mozilla::dom::MediaControlKeysEvent::ePlay);
}
// Caution, Seek can really be negative, like -1000000 during testing
void MPRISServiceHandler::Seek(int64_t aOffset) {
LOG("Seek(%" PRId64 ")", aOffset);
}
// The following two methods are untested as my Desktop Widget didn't issue
// these calls.
void MPRISServiceHandler::SetPosition(char* aTrackId, int64_t aPosition) {
LOG("SetPosition(%s, %" PRId64 ")", aTrackId, aPosition);
}
bool MPRISServiceHandler::OpenUri(char* aUri) {
LOG("OpenUri(%s)", aUri);
return false;
}
} // namespace widget
} // namespace mozilla