Skip to content

Commit

Permalink
feat: spotify integration (windows only)
Browse files Browse the repository at this point in the history
  • Loading branch information
hero622 committed Aug 22, 2023
1 parent ab5d038 commit 0e4d271
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 2 deletions.
124 changes: 124 additions & 0 deletions src/Features/Spotify.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#include "Spotify.hpp"

#include "Modules/Scheme.hpp"
#include "Modules/Surface.hpp"
#include "Modules/Console.hpp"

#include "Variable.hpp"
#include "Command.hpp"

#include <TlHelp32.h>

Variable sar_spotify_hud("sar_spotify_hud", "0", 0, "Draw the current playing song on spotify.\n");
Variable sar_spotify_hud_x("sar_spotify_hud_x", "2", 0, "X offset of the HUD.\n");
Variable sar_spotify_hud_y("sar_spotify_hud_y", "2", 0, "Y offset of the HUD.\n");
Variable sar_spotify_hud_font_index("sar_spotify_hud_font_index", "0", 0, "Font index of the HUD.\n");

Spotify spotify;

Spotify::Spotify()
: Hud(HudType_InGame | HudType_Paused | HudType_Menu, false) {
}
bool Spotify::ShouldDraw() {
return sar_spotify_hud.GetBool() && Hud::ShouldDraw();
}

void getHandleFromProcessPath(const char *szExeName, DWORD &dwPID) {
PROCESSENTRY32 entry;
entry.dwSize = sizeof(PROCESSENTRY32);

HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

if (Process32First(snapshot, &entry) == TRUE) {
while (Process32Next(snapshot, &entry) == TRUE) {
if (_stricmp(entry.szExeFile, szExeName) == 0) {
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID);

dwPID = entry.th32ProcessID;

CloseHandle(hProcess);

break;
}
}
}

CloseHandle(snapshot);
}

struct handle_data {
unsigned long process_id;
HWND window_handle;
};

BOOL isMainWindow(HWND handle) {
return GetWindow(handle, GW_OWNER) == (HWND)0 && IsWindowVisible(handle);
}

BOOL CALLBACK enumWindowsCallback(HWND handle, LPARAM lParam) {
handle_data &data = *(handle_data *)lParam;
unsigned long process_id = 0;
GetWindowThreadProcessId(handle, &process_id);
if (data.process_id != process_id || !isMainWindow(handle))
return TRUE;
data.window_handle = handle;
return FALSE;
}

HWND findMainWindow(unsigned long process_id) {
handle_data data;
data.process_id = process_id;
data.window_handle = 0;
EnumWindows(enumWindowsCallback, (LPARAM)&data);
return data.window_handle;
}

void Spotify::Init() {
DWORD pid;
getHandleFromProcessPath("Spotify.exe", pid);

g_hWnd = findMainWindow(pid);
}

void Spotify::SendCommand(SpotifyAction command) {
SendMessageA(g_hWnd, WM_APPCOMMAND, NULL, (LPARAM)command);
}

void Spotify::Paint(int slot) {
#ifndef _WIN32
console->Print("sar_spotify_hud is currently working for Windows only.\n");
sar_spotify_hud.SetValue(0);
return;
#endif
if (!g_hWnd || !IsWindow(g_hWnd)) {
Init();
if (!g_hWnd) {
console->Print("Spotify is not open, open spotify and set sar_spotify_hud to 1 again.\n");
sar_spotify_hud.SetValue(0);
}
}

char title[256];
GetWindowTextA(g_hWnd, title, sizeof(title));

// if there is - most likely song is playing
if (!strstr(title, "-"))
return;

auto font = scheme->GetFontByID(sar_spotify_hud_font_index.GetInt());
surface->DrawTxt(font, sar_spotify_hud_x.GetInt(), sar_spotify_hud_y.GetInt(), {255, 255, 255}, "%s", title);
}

bool Spotify::GetCurrentSize(int &xSize, int &ySize) {
return false;
}

CON_COMMAND(sar_spotify_next, "Skips to the next song.\n") {
spotify.SendCommand(SpotifyAction::NextTrack);
}
CON_COMMAND(sar_spotify_prev, "Skips to the previous song.\n") {
spotify.SendCommand(SpotifyAction::PreviousTrack);
}
CON_COMMAND(sar_spotify_togglepause, "Plays/pauses the playback.\n") {
spotify.SendCommand(SpotifyAction::PlayPause);
}
30 changes: 30 additions & 0 deletions src/Features/Spotify.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once
#include "Hud/Hud.hpp"
#include "Variable.hpp"

enum class SpotifyAction {
PlayPause = 917504,
PreviousTrack = 786432,
NextTrack = 720896,
};

class Spotify : public Hud {
private:
HWND g_hWnd;

public:
Spotify();
bool ShouldDraw() override;
void Paint(int slot) override;
bool GetCurrentSize(int &xSize, int &ySize) override;

void Init();
void SendCommand(SpotifyAction command);
};

extern Spotify spotify;

extern Variable sar_spotify_hud;
extern Variable sar_spotify_hud_x;
extern Variable sar_spotify_hud_y;
extern Variable sar_spotify_hud_font_index;
4 changes: 3 additions & 1 deletion src/SourceAutoRecord.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@
<ClCompile Include="Offsets.cpp" />
<ClCompile Include="Plugin.cpp" />
<ClCompile Include="SAR.cpp" />
<ClCompile Include="Features\Spotify.cpp" />
<ClCompile Include="Utils.cpp" />
<ClCompile Include="Utils\ed25519\add_scalar.cpp" />
<ClCompile Include="Utils\ed25519\fe.cpp" />
Expand Down Expand Up @@ -436,6 +437,7 @@
<ClInclude Include="Offsets.hpp" />
<ClInclude Include="Plugin.hpp" />
<ClInclude Include="SAR.hpp" />
<ClInclude Include="Features\Spotify.hpp" />
<ClInclude Include="Utils.hpp" />
<ClInclude Include="Utils\ed25519\ed25519.h" />
<ClInclude Include="Utils\ed25519\fe.h" />
Expand Down Expand Up @@ -495,4 +497,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
8 changes: 7 additions & 1 deletion src/SourceAutoRecord.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,9 @@
<ClCompile Include="Features\Tas\TasScript.cpp">
<Filter>SourceAutoRecord\Features\Tas</Filter>
</ClCompile>
<ClCompile Include="Features\Spotify.cpp">
<Filter>SourceAutoRecord\Features</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\lib\minhook\buffer.h">
Expand Down Expand Up @@ -1272,6 +1275,9 @@
<ClInclude Include="Features\Tas\TasScript.hpp">
<Filter>SourceAutoRecord\Features\Tas</Filter>
</ClInclude>
<ClInclude Include="Features\Spotify.hpp">
<Filter>SourceAutoRecord\Features</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="SourceAutoRecord">
Expand Down Expand Up @@ -1360,4 +1366,4 @@
<Filter>lib\SFML\System</Filter>
</None>
</ItemGroup>
</Project>
</Project>

0 comments on commit 0e4d271

Please sign in to comment.