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
|
// deck-it - music panel
// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
function clock(s) {
s = Math.max(0, Math.floor(s || 0));
return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, "0")}`;
}
// Minutes as "2h 05m", or "59m" under an hour.
export function hours(min) {
min = Math.max(0, Math.floor(min || 0));
return min < 60 ? `${min}m` : `${Math.floor(min / 60)}h ${String(min % 60).padStart(2, "0")}m`;
}
// The game the card shows instead of the music, from /api/games and
// /api/xbox: a PC game first, then the Xbox's.
export function pickGame(pc, xbox) {
if (pc?.playing) return pc.playing;
const g = xbox?.playing;
if (!g) return null;
const t = (xbox.titles ?? []).find((x) => x.id === g.id);
return { platform: "xbox", name: g.name, cover: t?.cover ?? null, minutes: t?.minutes ?? null,
session: xbox.session };
}
// Platform -> [Nerd Font glyph, label]: nf-md-steam, sony_playstation, microsoft_xbox.
// A PlayStation's generation is a small tag beside the glyph.
const PLATFORM = { steam: ["\u{f04d3}", "Steam"], ps1: ["\u{f0414}", "PlayStation", "1"],
ps2: ["\u{f0414}", "PlayStation 2", "2"], xbox: ["\u{f05b9}", "Xbox"] };
async function post(action, body = {}) {
try {
await fetch(`/api/music/${action}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
} catch {
// the next poll shows the real state
}
}
// Nerd Font glyphs, as on the other tabs.
const G = { prev: "\uf048", play: "\uf04b", pause: "\uf04c", next: "\uf051",
vol: "\uf028", muted: "\uf026" };
function button(glyph, action, label) {
const b = document.createElement("button");
b.className = `nf ${action}`;
b.textContent = glyph;
b.setAttribute("aria-label", label);
b.onclick = () => post(action);
return b;
}
export default {
title: "Music",
interval: 2,
mount(el) {
el.innerHTML = `
<div class="music">
<div class="top">
<div class="art empty">♪</div>
<div class="meta">
<div class="title">Nothing playing</div>
<div class="artist muted"></div>
<div class="album muted"></div>
</div>
</div>
<div class="seek">
<input type="range" min="0" max="1" step="1" value="0" aria-label="Seek">
<span class="time num">0:00</span>
</div>
<div class="controls">
<div class="vol">
<input type="range" min="0" max="100" step="1" value="0" aria-label="Volume">
<span class="level num"></span>
</div>
</div>
</div>
<div class="music game" hidden>
<img class="cover" alt="" hidden><div class="cover empty nf">\uf11b</div>
<div class="meta">
<div class="plat nf"></div>
<div class="title"></div>
<div class="stat num" title="This session"><i class="nf">\uf017</i><span class="now"></span></div>
<div class="stat num" title="Total"><i class="nf">\u{f02da}</i><span class="total"></span></div>
</div>
</div>`;
el.querySelector(".controls").prepend(
button(G.prev, "prev", "Previous"), button(G.play, "play", "Play/pause"),
button(G.next, "next", "Next"));
el.querySelector(".vol").prepend(button(G.vol, "mute", "Mute"));
const vol = el.querySelector(".vol input");
vol.addEventListener("pointerdown", () => { el._sliding = true; });
vol.addEventListener("pointerup", () => { el._sliding = false; });
vol.addEventListener("input", () => {
el.querySelector(".level").textContent = `${vol.value}%`;
post("volume", { level: Number(vol.value) });
});
const seek = el.querySelector(".seek input");
seek.addEventListener("pointerdown", () => { el._dragging = true; });
seek.addEventListener("pointerup", () => { el._dragging = false; });
seek.addEventListener("change", () => post("seek", { position: Number(seek.value) }));
},
update(st, el, game = null) {
const [musicEl, gameEl] = el.querySelectorAll(":scope > .music");
musicEl.hidden = Boolean(game);
gameEl.hidden = !game;
if (game) {
const img = gameEl.querySelector("img.cover");
img.hidden = !game.cover;
img.classList.toggle("sq", game.platform === "xbox"); // Xbox Live's art is square
gameEl.querySelector(".cover.empty").hidden = Boolean(game.cover);
if (game.cover && img.getAttribute("src") !== game.cover) img.src = game.cover;
const [glyph, label, tag] = PLATFORM[game.platform] ?? ["", ""];
const plat = gameEl.querySelector(".plat");
plat.textContent = glyph;
if (tag) plat.append(Object.assign(document.createElement("small"), { textContent: tag }));
plat.title = label;
plat.setAttribute("aria-label", label);
gameEl.querySelector(".title").textContent = game.name ?? "";
const session = game.session ? (Date.now() / 1000 - game.session) / 60 : 0;
gameEl.querySelector(".now").textContent = hours(session);
gameEl.querySelector(".total").parentElement.hidden = game.minutes == null;
gameEl.querySelector(".total").textContent = hours((game.minutes ?? 0) + session);
return;
}
if (st.art) {
let img = musicEl.querySelector("img.art");
if (!img) {
img = document.createElement("img");
img.className = "art";
img.alt = "";
musicEl.querySelector(".art").replaceWith(img);
}
if (img.getAttribute("src") !== st.art) img.src = st.art;
} else {
const img = musicEl.querySelector("img.art");
if (img) {
const empty = document.createElement("div");
empty.className = "art empty";
empty.textContent = "♪";
img.replaceWith(empty);
}
}
musicEl.querySelector(".title").textContent = st.title || "Nothing playing";
musicEl.querySelector(".artist").textContent = st.artist || "";
musicEl.querySelector(".album").textContent = st.album || "";
musicEl.querySelector("button.play").textContent = st.status === "Playing" ? G.pause : G.play;
const muted = Boolean(st.volume?.muted);
const mute = musicEl.querySelector("button.mute");
mute.classList.toggle("on", muted);
mute.textContent = muted ? G.muted : G.vol;
mute.setAttribute("aria-label", muted ? "Unmute" : "Mute");
const vol = musicEl.querySelector(".vol input");
vol.classList.toggle("muted", muted);
if (!el._sliding) {
vol.value = st.volume?.level ?? 0;
musicEl.querySelector(".level").textContent = `${vol.value}%`;
}
const seek = musicEl.querySelector(".seek input");
seek.max = Math.max(1, Math.floor(st.length || 0));
if (!el._dragging) seek.value = Math.min(Number(seek.max), Math.floor(st.position || 0));
musicEl.querySelector(".time").textContent = `${clock(st.position)} / ${clock(st.length)}`;
},
};
|