-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpotifyTitleWatcher.cs
106 lines (89 loc) · 3.41 KB
/
SpotifyTitleWatcher.cs
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
using System;
using System.Diagnostics;
using System.Linq;
namespace SpotifySongTracker {
public enum WindowTitleChangeType {
PlayingTrack,
NoTrack,
NoSpotify
}
public delegate void SpotifyWindowTitleChangeEventHandler(string title, WindowTitleChangeType type);
public class SpotifyTitleWatcher {
public event SpotifyWindowTitleChangeEventHandler SpotifyWindowTitleChange;
private Interop.WinEventDelegate spotifyWindowNameChangeDelegate;
private IntPtr spotifyWindowNameChangeHook;
private Interop.WinEventDelegate spotifyWindowDestroyDelegate;
private IntPtr spotifyWindowDestroyHook;
private Process spotifyProc {
get {
var procs = Process.GetProcessesByName("Spotify");
return procs.FirstOrDefault(proc => proc.MainWindowTitle != "");
}
}
private string lastTitle = "";
public void Start() {
spotifyWindowNameChangeDelegate = new Interop.WinEventDelegate(this.SpotifyEventNameChange);
spotifyWindowDestroyDelegate = new Interop.WinEventDelegate(this.SpotifyWindowDestroy);
this.StartupSpotifyWatch();
var processForm = new SystemProcessHookForm();
processForm.WindowCreated += (sender, process) => {
if (process.ProcessName == "Spotify") {
this.StartupSpotifyWatch();
}
};
}
private void TitleChange(string title, WindowTitleChangeType type) {
if (lastTitle != title) {
lastTitle = title;
this.SpotifyWindowTitleChange?.Invoke(title, type);
}
}
private void StartupSpotifyWatch() {
if (this.spotifyProc != null) {
this.EmitMainWindowTitle();
this.SetupSpotifyNameChangeHook();
this.SetupWindowDestroyHook();
} else {
TitleChange(LoadedConfig.config.spotifyNotOpenMessage, WindowTitleChangeType.NoSpotify);
}
}
private void EmitMainWindowTitle() {
var title = this.spotifyProc.MainWindowTitle;
if (!title.Contains("-") && title.Contains("Spotify")) {
TitleChange(LoadedConfig.config.noSongPlayingMessage, WindowTitleChangeType.NoTrack);
} else {
TitleChange(title, WindowTitleChangeType.PlayingTrack);
}
}
private void SetupWindowDestroyHook() {
this.spotifyWindowDestroyHook = Interop.SetWinEventHook(
Interop.EVENT_OBJECT_DESTROY,
Interop.EVENT_OBJECT_DESTROY,
IntPtr.Zero,
spotifyWindowDestroyDelegate,
(uint)spotifyProc.Id,
0,
Interop.WINEVENT_OUTOFCONTEXT
);
}
private void SpotifyWindowDestroy(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) {
TitleChange(LoadedConfig.config.spotifyNotOpenMessage, WindowTitleChangeType.NoSpotify);
Interop.UnhookWinEvent(spotifyWindowNameChangeHook);
Interop.UnhookWinEvent(spotifyWindowDestroyHook);
}
private void SpotifyEventNameChange(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) {
this.EmitMainWindowTitle();
}
private void SetupSpotifyNameChangeHook() {
this.spotifyWindowNameChangeHook = Interop.SetWinEventHook(
Interop.EVENT_OBJECT_NAMECHANGE,
Interop.EVENT_OBJECT_NAMECHANGE,
IntPtr.Zero,
spotifyWindowNameChangeDelegate,
(uint)spotifyProc.Id,
0,
Interop.WINEVENT_OUTOFCONTEXT
);
}
}
}