-
Notifications
You must be signed in to change notification settings - Fork 3
/
dbus_manager.js
78 lines (67 loc) · 2.31 KB
/
dbus_manager.js
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
const Me = imports.misc.extensionUtils.getCurrentExtension();
const DBusIface = Me.imports.dbus;
const MediaInfo = Me.imports.media_info;
var PlayerManager = class Player_Manager {
constructor(callback) {
this._dbus = new DBusIface.DBus();
this.players = {};
this._callback = callback;
// player DBus name pattern
const name_regex = /^org\.mpris\.MediaPlayer2\./;
this._dbus.ListNamesRemote((names) => {
const playerNames = [];
for (const name of names[0]) {
if (name_regex.test(name)) {
playerNames.push(name);
}
}
playerNames.sort();
for (const player of playerNames) {
this._dbus.GetNameOwnerRemote(player, (owner) => {
this.add_player(player, owner);
});
}
});
this._ownerChangedId = this._dbus.connectSignal('NameOwnerChanged',
(proxy, sender, [name, old_owner, new_owner]) => {
if (name_regex.test(name)) {
// if (!this._disabling) {
if (new_owner && !old_owner) {
// this._addPlayer(name, new_owner);
this.add_player(name, new_owner);
}
else if (old_owner && !new_owner) {
this.remove_player(name, old_owner);
}
else {
this.change_player_owner(name, old_owner, new_owner);
}
}
// }
}
);
}
add_player(name, owner) {
this.players[owner] = new MediaInfo.MediaInfo(name, owner, this._callback);
}
remove_player(name, owner) {
try {
this.players[owner].disconnect();
delete this.players[owner];
}catch(e){
}
}
change_player_owner(name, old_owner, new_owner) {
this.remove_player(name, old_owner);
this.add_player(name, new_owner);
}
disconnect_all() {
try {
for (const owner in this.players) {
this.remove_player(this.players[owner], owner);
}
this._dbus.disconnectSignal(this._ownerChangedId);
}catch(e){
}
}
}