Last active
May 21, 2023 13:31
-
-
Save swoopae/7bb4d04d4d596e1e061f5efa06cefad5 to your computer and use it in GitHub Desktop.
stupid simple spotify song detection for C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void spotify ( ) { | |
const static auto & spotify = CONFIG_GET ( HASH_CT ( "crescent:spotify" ) , bool ); | |
if ( !spotify ) | |
return; | |
static HWND spotify_hwnd = nullptr; | |
static float last_hwnd_time = 0.f , last_title_time = 0.f; | |
/* hilariously overengineered solution to get fucking spotify song name */ | |
if ( ( !spotify_hwnd || spotify_hwnd == INVALID_HANDLE_VALUE ) && last_hwnd_time < ui::globals::realtime ) { | |
/* game freezes if we try to open a handle 300 times per second (understandable) so this workaround will do */ | |
last_hwnd_time = ui::globals::realtime + 10.f; | |
for ( HWND hwnd = GetTopWindow ( 0 ); hwnd; hwnd = GetWindow ( hwnd , GW_HWNDNEXT ) ) { | |
if ( !APICALL ( IsWindowVisible )( hwnd ) ) | |
continue; | |
int length = APICALL ( GetWindowTextLengthW )( hwnd ); | |
if ( length == 0 ) | |
continue; | |
WCHAR filename [ 300 ]; | |
DWORD pid; | |
APICALL ( GetWindowThreadProcessId )( hwnd , &pid ); | |
const auto spotify_handle = APICALL ( OpenProcess )( PROCESS_QUERY_INFORMATION , FALSE , pid ); | |
APICALL ( K32GetModuleFileNameExW )( spotify_handle , nullptr , filename , 300 ); | |
std::wstring sane_filename { filename }; | |
APICALL ( CloseHandle )( spotify_handle ); | |
/* fucking finally found spotify */ | |
if ( sane_filename.find ( _ ( L"Spotify.exe" ) ) != std::string::npos ) | |
spotify_hwnd = hwnd; | |
} | |
} else if ( spotify_hwnd && spotify_hwnd != INVALID_HANDLE_VALUE && last_title_time < ui::globals::realtime ) { | |
last_title_time = ui::globals::realtime + 1.f; | |
WCHAR title [ 300 ]; | |
/* returns 0 if something went wrong, so try to get handle again */ | |
if ( !APICALL ( GetWindowTextW )( spotify_hwnd , title , 300 ) ) { | |
spotify_hwnd = nullptr; | |
} else { | |
const std::wstring sane_title { title }; | |
const std::string ascii_title { sane_title.begin ( ) , sane_title.end ( ) }; | |
static std::uint32_t hash = 0; | |
/* if we find a dash, song is likely playing */ | |
if ( sane_title.find ( _ ( L"-" ) ) != std::string::npos ) { | |
if ( hash != HASH_RT ( ascii_title.data ( ) ) ) { | |
hash = HASH_RT ( ascii_title.data ( ) ); | |
csgo::hacks::notifications->dispatch ( fmt::format ( _ ( "Now playing on Spotify: {}" ) , ascii_title ) , ui::SPOTIFY_ICON , 5.f ); | |
} | |
// csgo::render::text_wide ( { 25 , 250 } , c_color ( 255 , 255 , 255 ) , fmt::format ( _ ( L"currently playing on spotify: {}" ) , sane_title ) , csgo::render::fonts::menu_title_font ); | |
} | |
/* no song playing das fucked :\ */ | |
// else { | |
// csgo::render::text ( { 25 , 250 } , c_color ( 255 , 255 , 255 ) , _ ( "nothing is currently playing on spotify" ) , csgo::render::fonts::menu_title_font ); | |
// } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
paste