Skip to content

Instantly share code, notes, and snippets.

@purpl3F0x
Created June 6, 2019 07:40
Show Gist options
  • Save purpl3F0x/e965d542a76bfdfd0ab0216aca7286f7 to your computer and use it in GitHub Desktop.
Save purpl3F0x/e965d542a76bfdfd0ab0216aca7286f7 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <vector>
#include <windows.h>
#include <tlhelp32.h>
#include <string>
#include <regex>
#include <tuple>
#include <codecvt>
#include <chrono>
#include <thread>
struct EnumWindowsProcParam{
std::vector<DWORD>& pids;
std::string& song;
std::string& artist;
};
inline bool isTIDAL (const PROCESSENTRY32W &entry) {
// return std::wstring(entry.szExeFile) == L"TIDAL.exe";
return wcscmp (entry.szExeFile, L"TIDAL.exe") ==0;
}
BOOL CALLBACK enumWindowsProc(HWND hwnd, LPARAM lParam) {
static const std::wregex rgx(L"(.+) - (?!\\{)(.+)");
const auto &paramRe = *reinterpret_cast<EnumWindowsProcParam*>(lParam);
DWORD winId;
GetWindowThreadProcessId(hwnd, &winId);
for (DWORD pid : (paramRe.pids)) {
if (winId == pid) {
std::cout << pid;
std::wstring title(GetWindowTextLength(hwnd) + 1, L'\0');
GetWindowTextW(hwnd, &title[0], title.size()); //note: >=C++11
std::regex_match(title, rgx);
std::wsmatch matches;
if (std::regex_search(title, matches, rgx)) {
paramRe.song = std::wstring_convert<std::codecvt_utf8<wchar_t>>().to_bytes(matches[1]);
paramRe.artist = std::wstring_convert<std::codecvt_utf8<wchar_t>>().to_bytes(matches[2]);
// std::cout << "RETURNING TRUE\n";
return TRUE;
}
}
}
return TRUE;
}
uint_fast8_t TidalInfo(std::string& song, std::string& artist){
static std::vector<DWORD> pids;
HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32W entry;
entry.dwSize = sizeof entry;
if (!Process32FirstW(snap, &entry))
return 0;
do {
if (isTIDAL (entry)) {
pids.emplace_back(entry.th32ProcessID);
}
} while (Process32NextW(snap, &entry));
song = "";
artist = "";
EnumWindowsProcParam param = {pids, song, artist};
auto res = EnumWindows(enumWindowsProc, reinterpret_cast<LPARAM>(&param));
if (res)
return 1;
else if(pids.size())
return 0;
else
return -1;
}
int main() {
// std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
std::string song = "";
std::string artist = "";
while (1){
auto res = TidalInfo(song, artist);
if (res== 1)
std::cout << song << " - " << artist << std::endl;
else if (res == 0)
std::cout << "Not playing\n";
else if (res ==-1)
std::cout << "No TIDAL\n";
Sleep(1000);
}
// std::chrono::steady_clock::time_point end= std::chrono::steady_clock::now();
// std::cout << "Time difference = " << std::chrono::duration_cast<std::chrono::milliseconds> (end - begin).count() <<std::endl;
// std::cout << song << "\t" << artist <<"\n\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment