Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: spotify integration (windows only) #192

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions src/Features/Spotify.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include "Spotify.hpp"

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

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

#ifdef _WIN32
# include <TlHelp32.h>
#endif

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();
}

#ifdef _WIN32
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;
}
#endif

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

g_hWnd = findMainWindow(pid);
#endif
}

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

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;
#else
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);
#endif
}

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);
}
32 changes: 32 additions & 0 deletions src/Features/Spotify.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once
#include "Hud/Hud.hpp"
#include "Variable.hpp"

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

class Spotify : public Hud {
private:
#ifdef _WIN32
HWND g_hWnd;
#endif

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>