-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
170 lines (145 loc) · 5.23 KB
/
Copy pathProgram.cs
File metadata and controls
170 lines (145 loc) · 5.23 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
using System.Diagnostics;
using System.Runtime.InteropServices;
using Windows.Media.Control;
// ITaskbarList3 COM Interface
[ComImport, Guid("56fdf344-fd6d-11d0-958a-006097c9a090"),
ClassInterface(ClassInterfaceType.None)]
class TaskbarListCoClass { }
[ComImport, Guid("ea1afb91-9e28-4b86-90e9-9e9f8a5eefaf"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface ITaskbarList3
{
// ITaskbarList
void HrInit();
void AddTab(IntPtr hwnd);
void DeleteTab(IntPtr hwnd);
void ActivateTab(IntPtr hwnd);
void SetActiveAlt(IntPtr hwnd);
// ITaskbarList2
void MarkFullscreenWindow(IntPtr hwnd,
[MarshalAs(UnmanagedType.Bool)] bool fFullscreen);
// ITaskbarList3
void SetProgressValue(IntPtr hwnd, ulong ullCompleted, ulong ullTotal);
void SetProgressState(IntPtr hwnd, TBPFLAG tbpFlags);
}
enum TBPFLAG
{
TBPF_NOPROGRESS = 0,
TBPF_INDETERMINATE = 1,
TBPF_NORMAL = 2,
TBPF_ERROR = 4,
TBPF_PAUSED = 8
}
// Main Entry Point
class Program
{
static async Task Main()
{
Log("SpotifyTaskbarProgress starting...");
// Initialize ITaskbarList3 via COM
var taskbar = (ITaskbarList3)new TaskbarListCoClass();
taskbar.HrInit();
// Request SMTC manager from Windows
var manager = await GlobalSystemMediaTransportControlsSessionManager
.RequestAsync();
Log("Running. Press Ctrl+C to stop.");
Log("(Change OutputType to WinExe in .csproj to run without this window)");
IntPtr lastHwnd = IntPtr.Zero;
TBPFLAG lastFlag = TBPFLAG.TBPF_NOPROGRESS;
while (true)
{
try
{
// Find Spotify main window
var hwnd = GetSpotifyHwnd();
if (hwnd == IntPtr.Zero)
{
await Task.Delay(1500);
continue;
}
// Clear progress bar if window changed
if (hwnd != lastHwnd)
{
taskbar.SetProgressState(hwnd, TBPFLAG.TBPF_NOPROGRESS);
lastHwnd = hwnd;
}
// Get SMTC session
var session = manager.GetSessions()
.FirstOrDefault(s => s.SourceAppUserModelId.Contains(
"Spotify", StringComparison.OrdinalIgnoreCase));
if (session is null)
{
SetState(taskbar, hwnd, TBPFLAG.TBPF_NOPROGRESS,
ref lastFlag);
await Task.Delay(1500);
continue;
}
var playback = session.GetPlaybackInfo();
var timeline = session.GetTimelineProperties();
var duration = timeline.EndTime - timeline.StartTime;
var position = timeline.Position - timeline.StartTime;
if (duration.TotalSeconds <= 0)
{
await Task.Delay(500);
continue;
}
// Update taskbar progress
var status = playback.PlaybackStatus;
if (status == GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing)
{
// Get position
var extra = DateTime.UtcNow - timeline.LastUpdatedTime.UtcDateTime;
var realPos = position + extra;
if (realPos > duration) realPos = duration;
SetState(taskbar, hwnd, TBPFLAG.TBPF_NORMAL, ref lastFlag);
taskbar.SetProgressValue(
hwnd,
(ulong)realPos.Ticks,
(ulong)duration.Ticks);
}
else if (status == GlobalSystemMediaTransportControlsSessionPlaybackStatus.Paused)
{
SetState(taskbar, hwnd, TBPFLAG.TBPF_PAUSED, ref lastFlag);
taskbar.SetProgressValue(
hwnd,
(ulong)position.Ticks,
(ulong)duration.Ticks);
}
else
{
SetState(taskbar, hwnd, TBPFLAG.TBPF_NOPROGRESS, ref lastFlag);
}
}
catch (Exception ex)
{
Log($"[erro] {ex.Message}");
await Task.Delay(1000);
}
await Task.Delay(500);
}
}
// Avoid calling SetProgressState repeatedly unnecessarily
static void SetState(ITaskbarList3 tb, IntPtr hwnd,
TBPFLAG flag, ref TBPFLAG last)
{
if (flag == last) return;
tb.SetProgressState(hwnd, flag);
last = flag;
}
static IntPtr GetSpotifyHwnd()
{
// Find MainWindowHandle beteween spotify's processes
foreach (var p in Process.GetProcessesByName("Spotify"))
{
try
{
if (p.MainWindowHandle != IntPtr.Zero)
return p.MainWindowHandle;
}
catch { /* process may have exited */ }
}
return IntPtr.Zero;
}
static void Log(string msg) =>
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] {msg}");
}