-
Notifications
You must be signed in to change notification settings - Fork 1
/
empris.py
233 lines (166 loc) · 5.19 KB
/
empris.py
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
from __future__ import annotations
import os
import sys
import time
import subprocess
class Rofi:
def __init__(self, args: str) -> None:
self.args = args
def select(self, prompt: str, options: list[str], selected: int) -> int:
items = "\n".join(options)
ans = (
os.popen(f"echo '{items}' | rofi -dmenu -p '{prompt}' -format i \
-selected-row {selected} -me-select-entry ''\
-me-accept-entry 'MousePrimary' {self.args}")
.read()
.strip()
)
if ans == "":
return -1
return int(ans)
class PlayerList:
def __init__(self) -> None:
self.players: list[Player] = []
def add_player(self, player: Player) -> None:
self.players.append(player)
def labels(self) -> list[str]:
return [p.label for p in self.players]
def playing(self) -> list[int]:
playing = []
for i, player in enumerate(self.players):
if player.playing:
playing.append(i)
return playing
def name(self, index: int) -> str:
return self.players[index].name
def index(self, name: str) -> int:
for i, player in enumerate(self.players):
if name == player.name:
return i
return -1
def empty(self) -> None:
self.players = []
playerlist = PlayerList()
class Player:
def __init__(self, name: str) -> None:
self.name = name
label = name.split(".")[0]
status = os.popen(f"playerctl status -p {name}").read().strip()
if status == "Playing":
self.playing = True
else:
self.playing = False
if self.playing:
label += " (Playing)"
self.label = label
def get_players() -> None:
playerlist.empty()
splist = os.popen("playerctl --list-all").read().strip().split("\n")
splist.sort()
for name in splist:
playerlist.add_player(Player(name))
def show_menu() -> None:
rofi = Rofi("-font 'sans-serif 16' -theme-str 'window { width: 600px; }'")
options = []
options += playerlist.labels()
options.append("---------")
options.append("Pause All")
options.append("Next Track")
options.append("Prev Track")
selected = 0
playing = playerlist.playing()
if len(playing) > 0:
selected = playing[0]
index = rofi.select("Select Player", options, selected)
if index == -1:
return
if index < len(playerlist.players):
pause_all_except(index)
toggleplay(index)
elif options[index] == "Pause All":
pause_all()
elif options[index] == "Next Track":
go_next()
elif options[index] == "Prev Track":
go_prev()
def toggleplay(index: int) -> None:
player = playerlist.players[index]
if player.playing:
pause(index)
else:
play(index)
def play(index: int) -> None:
player = playerlist.players[index]
if not player.playing:
os.popen(f"playerctl -p {playerlist.name(index)} play").read()
def pause(index: int) -> None:
player = playerlist.players[index]
if player.playing:
os.popen(f"playerctl -p {playerlist.name(index)} pause").read()
def pause_all_except(index: int) -> None:
for i, _ in enumerate(playerlist.players):
if i != index:
pause(i)
def pause_all() -> None:
for i, _ in enumerate(playerlist.players):
pause(i)
def go_next() -> None:
for player in playerlist.players:
if player.playing:
os.popen(f"playerctl -p {player.name} next").read()
return
def go_prev() -> None:
for player in playerlist.players:
if player.playing:
os.popen(f"playerctl -p {player.name} previous").read()
return
def start_autopause() -> None:
p = subprocess.Popen(
[
"playerctl",
"status",
"--follow",
"-f",
"autopause - {{playerInstance}} - {{status}}",
],
stdout=subprocess.PIPE,
)
if (not p) or (not p.stdout):
return
for line in iter(p.stdout.readline, ""):
item = line.decode("UTF-8").strip()
if item.startswith("autopause - "):
split = item.split(" - ")
name = split[1]
status = split[2]
if status == "Playing":
# This sleep is to avoid conflict
# When changing players through empris manually
time.sleep(0.25)
get_players()
index = playerlist.index(name)
if index >= 0:
player = playerlist.players[index]
if player.playing:
pause_all_except(index)
def main() -> None:
mode = ""
if len(sys.argv) > 1:
mode = sys.argv[1]
if mode == "autopause":
try:
start_autopause()
except KeyboardInterrupt:
pass
else:
get_players()
if mode == "pauseall":
pause_all()
elif mode == "next":
go_next()
elif mode == "prev":
go_prev()
else:
show_menu()
if __name__ == "__main__":
main()