# HG changeset patch # User Paper # Date 1699825387 18000 # Node ID 478f3b366199383ab775f41d6ae243a271c83bb1 # Parent 28842a8d0c6b8f46845c8caf080ef5e295279322 dep/animia: separate lots of things, use base class for OS stuff diff -r 28842a8d0c6b -r 478f3b366199 dep/animia/CMakeLists.txt --- a/dep/animia/CMakeLists.txt Sun Nov 12 04:53:19 2023 -0500 +++ b/dep/animia/CMakeLists.txt Sun Nov 12 16:43:07 2023 -0500 @@ -7,6 +7,7 @@ src/util.cc src/strategist.cc ) + if(LINUX) list(APPEND SRC_FILES # linux @@ -23,9 +24,19 @@ src/fd/win32.cc ) endif() + add_library(animia SHARED ${SRC_FILES}) set_target_properties(animia PROPERTIES PUBLIC_HEADER include/animia.h CXX_STANDARD 17 ) + +if(WIN32) + target_compile_definitions(animia PUBLIC WIN32) +elseif(LINUX) + target_compile_definitions(animia PUBLIC LINUX) +elseif(UNIX) + target_compile_definitions(animia PUBLIC UNIX) +endif() + target_include_directories(animia PRIVATE include) diff -r 28842a8d0c6b -r 478f3b366199 dep/animia/include/animia.h --- a/dep/animia/include/animia.h Sun Nov 12 04:53:19 2023 -0500 +++ b/dep/animia/include/animia.h Sun Nov 12 16:43:07 2023 -0500 @@ -3,14 +3,14 @@ #include "animia/media.h" #include "animia/player.h" -#include "animia/util.h" +#include "animia/types.h" namespace animia { /* pid_t should be DWORD on windows, and defined by the system anywhere else */ struct Process { - pid_t pid = 0; + internal::pid_t pid = 0; std::string name; }; diff -r 28842a8d0c6b -r 478f3b366199 dep/animia/include/animia/fd.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dep/animia/include/animia/fd.h Sun Nov 12 16:43:07 2023 -0500 @@ -0,0 +1,25 @@ +#ifndef __animia__animia__fd_h +#define __animia__animia__fd_h + +#include +#include +#include +#include +#include + +#include "animia/types.h" + +namespace animia::internal { + +class BaseFdTools { + public: + virtual bool GetAllPids(std::set& pids) { return false; } + virtual bool GetProcessName(pid_t pid, std::string& result) { return false; } + virtual bool EnumerateOpenFiles(const std::set& pids, std::vector>& files) { return false; } +}; + +extern BaseFdTools& fd; // defined in animia.cc + +} + +#endif // __animia__animia__fd_h diff -r 28842a8d0c6b -r 478f3b366199 dep/animia/include/animia/fd/bsd.h --- a/dep/animia/include/animia/fd/bsd.h Sun Nov 12 04:53:19 2023 -0500 +++ b/dep/animia/include/animia/fd/bsd.h Sun Nov 12 16:43:07 2023 -0500 @@ -6,13 +6,17 @@ #include #include -#include // pid_t +#include "animia/types.h" +#include "animia/fd.h" namespace animia::internal::unix { -bool GetAllPids(std::set& pids); -bool GetProcessName(pid_t pid, std::string& result); -bool EnumerateOpenFiles(const std::set& pids, std::vector>& files); +class UnixFdTools final : public BaseFdTools { + public: + bool GetAllPids(std::set& pids) override; + bool GetProcessName(pid_t pid, std::string& result) override; + bool EnumerateOpenFiles(const std::set& pids, std::vector>& files) override; +}; } diff -r 28842a8d0c6b -r 478f3b366199 dep/animia/include/animia/fd/linux.h --- a/dep/animia/include/animia/fd/linux.h Sun Nov 12 04:53:19 2023 -0500 +++ b/dep/animia/include/animia/fd/linux.h Sun Nov 12 16:43:07 2023 -0500 @@ -6,13 +6,17 @@ #include #include -#include // pid_t +#include "animia/types.h" +#include "animia/fd.h" namespace animia::internal::linux { -bool GetAllPids(std::set& pids); -bool GetProcessName(pid_t pid, std::string& result); -bool EnumerateOpenFiles(const std::set& pids, std::vector>& files); +class LinuxFdTools final : public BaseFdTools { + public: + bool GetAllPids(std::set& pids) override; + bool GetProcessName(pid_t pid, std::string& result) override; + bool EnumerateOpenFiles(const std::set& pids, std::vector>& files) override; +}; } diff -r 28842a8d0c6b -r 478f3b366199 dep/animia/include/animia/fd/win32.h --- a/dep/animia/include/animia/fd/win32.h Sun Nov 12 04:53:19 2023 -0500 +++ b/dep/animia/include/animia/fd/win32.h Sun Nov 12 16:43:07 2023 -0500 @@ -9,7 +9,8 @@ #include -#include "animia/util.h" +#include "animia/types.h" +#include "animia/fd.h" namespace animia::internal::win32 { @@ -20,9 +21,12 @@ using Handle = std::unique_ptr; -bool GetAllPids(std::set& pids); -bool GetProcessName(pid_t pid, std::string& result); -bool EnumerateOpenFiles(const std::set& pids, std::vector>& files); +class Win32FdTools final : public BaseFdTools { + public: + bool GetAllPids(std::set& pids) override; + bool GetProcessName(pid_t pid, std::string& result) override; + bool EnumerateOpenFiles(const std::set& pids, std::vector>& files) override; +}; } diff -r 28842a8d0c6b -r 478f3b366199 dep/animia/include/animia/os.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dep/animia/include/animia/os.h Sun Nov 12 16:43:07 2023 -0500 @@ -0,0 +1,15 @@ +#ifndef __animia__animia__os_h +#define __animia__animia__os_h + +#ifdef __linux__ +# define ANIMIA_ON_LINUX +#elif (defined(unix) || defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) +# if (defined(__APPLE__) && defined(__MACH__)) +# define ANIMIA_ON_OSX +# endif +# define ANIMIA_ON_UNIX +#elif defined(_WIN32) +# define ANIMIA_ON_WIN32 +#endif + +#endif // __animia__animia__os_h diff -r 28842a8d0c6b -r 478f3b366199 dep/animia/include/animia/types.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dep/animia/include/animia/types.h Sun Nov 12 16:43:07 2023 -0500 @@ -0,0 +1,18 @@ +#ifndef __animia__animia__types_h +#define __animia__animia__types_h + +#include "animia/os.h" + +#ifdef ANIMIA_ON_WIN32 +# include +namespace animia::internal { + typedef DWORD pid_t; +} +#else +# include +namespace animia::internal { + typedef ::pid_t pid_t; +} +#endif + +#endif // __animia__animia__types_h \ No newline at end of file diff -r 28842a8d0c6b -r 478f3b366199 dep/animia/include/animia/util.h --- a/dep/animia/include/animia/util.h Sun Nov 12 04:53:19 2023 -0500 +++ b/dep/animia/include/animia/util.h Sun Nov 12 16:43:07 2023 -0500 @@ -1,24 +1,6 @@ #ifndef __animia__animia__util_h #define __animia__animia__util_h -/* these should not be #defines, make them some constexpr - magic at some point */ -#ifdef __linux__ -# define ANIMIA_ON_LINUX -#elif (defined(unix) || defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) -# if (defined(__APPLE__) && defined(__MACH__)) -# define ANIMIA_ON_LINUX -# endif -# define ANIMIA_ON_LINUX -#elif defined(_WIN32) -# define ANIMIA_ON_WIN32 -/* move this to a types.h or something */ -#include -namespace animia { - typedef DWORD pid_t; -} -#endif - namespace animia::internal::util { bool ReadFile(const std::string& path, std::string& data); diff -r 28842a8d0c6b -r 478f3b366199 dep/animia/src/animia.cc --- a/dep/animia/src/animia.cc Sun Nov 12 04:53:19 2023 -0500 +++ b/dep/animia/src/animia.cc Sun Nov 12 16:43:07 2023 -0500 @@ -4,11 +4,35 @@ #include #include "animia.h" -#include "animia/util.h" #include "animia/strategies.h" +#include "animia/types.h" + #ifdef ANIMIA_ON_WIN32 -#include "animia/fd/win32.h" +# include "animia/fd/win32.h" +#elif defined(ANIMIA_ON_LINUX) +# include "animia/fd/linux.h" +#elif defined(ANIMIA_ON_UNIX) +# include "animia/fd/bsd.h" #endif + +namespace animia::internal { + +/* really stupid hack to get fd to point to the right + thing */ +#ifdef ANIMIA_ON_WIN32 +win32::Win32FdTools os_fd; +#elif defined(ANIMIA_ON_LINUX) +linux::LinuxFdTools os_fd; +#elif defined(ANIMIA_ON_UNIX) +unix::UnixFdTools os_fd; +#else +BaseFdTools os_fd; +#endif + +BaseFdTools& fd = os_fd; + +} + #include namespace animia { @@ -26,24 +50,14 @@ } bool GetResults(const std::vector& players, std::vector& results) { - std::set pids; + std::set pids; -#ifdef ANIMIA_ON_WIN32 - /* todo: make these functions also return process names in a tuple, - cause the win32 api gives it to us for free! */ - if (!internal::win32::GetAllPids(pids)) -#elif ANIMIA_ON_LINUX - if (!internal::linux::GetAllPids(pids)) -#elif ANIMIA_ON_UNIX - if (!internal::unix::GetAllPids(pids)) -#endif + if (!internal::fd.GetAllPids(pids)) return false; for (const auto& pid : pids) { std::string name; -#ifdef ANIMIA_ON_WIN32 - animia::internal::win32::GetProcessName(pid, name); -#endif + internal::fd.GetProcessName(pid, name); Player player; if (!ProcessInPlayers(players, name, player)) diff -r 28842a8d0c6b -r 478f3b366199 dep/animia/src/fd/bsd.cc --- a/dep/animia/src/fd/bsd.cc Sun Nov 12 04:53:19 2023 -0500 +++ b/dep/animia/src/fd/bsd.cc Sun Nov 12 16:43:07 2023 -0500 @@ -1,10 +1,7 @@ /** - * bsd.cpp - * - provides support for most* versions of BSD - * - this also works for OS X :) - * more technical details: this is essentially a wrapper - * around the very C-like BSD system functions that are... - * kind of unnatural to use in modern C++. + * fd/bsd.cpp + * - this ONLY* supports OS X as of now + * (*there is some FreeBSD support code) **/ #include #include @@ -25,7 +22,7 @@ /* this is a cleaned up version of a function from... Apple? ...anyway, what it essentially does is gets the size and stuff from sysctl() and reserves the space in a vector to store the PIDs */ -bool GetAllPids(std::set& pids) { +bool UnixFdTools::GetAllPids(std::set& pids) { struct kinfo_proc* result = NULL; size_t length = 0; static const int name[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0}; @@ -53,7 +50,7 @@ pids.push_back(result[i].kp_proc.p_pid); } -bool GetProcessName(pid_t pid, std::string& result) { +bool UnixFdTools::GetProcessName(pid_t pid, std::string& result) { #ifdef __FreeBSD__ struct kinfo_proc* proc = kinfo_getproc(pid); if (!proc) @@ -81,7 +78,7 @@ } /* this only works on OS X :( */ -bool EnumerateOpenFiles(const std::set& pids, std::vector>& files) { +bool UnixFdTools::EnumerateOpenFiles(const std::set& pids, std::vector>& files) { for (const auto& pid : pids) { int bufsz = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, NULL, 0); if (bufsz == -1) diff -r 28842a8d0c6b -r 478f3b366199 dep/animia/src/fd/linux.cc --- a/dep/animia/src/fd/linux.cc Sun Nov 12 04:53:19 2023 -0500 +++ b/dep/animia/src/fd/linux.cc Sun Nov 12 16:43:07 2023 -0500 @@ -91,7 +91,7 @@ return ret.c_str(); } -bool GetAllPids(std::set& pids) { +bool LinuxFdTools::GetAllPids(std::set& pids) { for (const auto& dir : get_all_files_in_dir(PROC_LOCATION)) { pid_t pid; try { @@ -103,14 +103,14 @@ } } -bool GetProcessName(pid_t pid, std::string& result) { +bool LinuxFdTools::GetProcessName(pid_t pid, std::string& result) { const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/comm"; std::string result = util::ReadFile(path); result.erase(std::remove(result.begin(), result.end(), '\n'), result.end()); } -bool EnumerateOpenFiles(const std::set& pids, std::vector>& files) { +bool LinuxFdTools::EnumerateOpenFiles(const std::set& pids, std::vector>& files) { for (const auto& pid : pids) { const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/fd"; diff -r 28842a8d0c6b -r 478f3b366199 dep/animia/src/fd/win32.cc --- a/dep/animia/src/fd/win32.cc Sun Nov 12 04:53:19 2023 -0500 +++ b/dep/animia/src/fd/win32.cc Sun Nov 12 16:43:07 2023 -0500 @@ -199,7 +199,7 @@ return true; } -bool GetAllPids(std::set& pids) { +bool Win32FdTools::GetAllPids(std::set& pids) { HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (hProcessSnap == INVALID_HANDLE_VALUE) return false; @@ -220,7 +220,7 @@ return true; } -bool GetProcessName(pid_t pid, std::string& result) { +bool Win32FdTools::GetProcessName(pid_t pid, std::string& result) { unsigned long ret_size = 0; // size given by GetModuleBaseNameW Handle handle(::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid)); if (!handle.get()) @@ -242,7 +242,7 @@ } /* this could be changed to being a callback, but... I'm too lazy right now :) */ -bool EnumerateOpenFiles(const std::set& pids, std::vector>& files) { +bool Win32FdTools::EnumerateOpenFiles(const std::set& pids, std::vector>& files) { std::unordered_map proc_handles; for (const pid_t& pid : pids) { diff -r 28842a8d0c6b -r 478f3b366199 dep/animia/src/strategist.cc --- a/dep/animia/src/strategist.cc Sun Nov 12 04:53:19 2023 -0500 +++ b/dep/animia/src/strategist.cc Sun Nov 12 16:43:07 2023 -0500 @@ -1,16 +1,9 @@ #include "animia/strategies.h" #include "animia/util.h" +#include "animia/fd.h" #include "animia.h" #include -#ifdef ANIMIA_ON_WIN32 -# include "animia/fd/win32.h" -#elif defined(ANIMIA_ON_LINUX) -# include "animia/fd/linux.h" -#elif defined(ANIMIA_ON_UNIX) -# include "animia/fd/bsd.h" -#endif - namespace animia::internal { class Strategist { @@ -60,13 +53,7 @@ const std::set pids{result_.process.pid}; std::vector> files; -#ifdef ANIMIA_ON_WIN32 - win32::EnumerateOpenFiles(pids, files); -#elif defined(ANIMIA_ON_LINUX) - linux::EnumerateOpenFiles(pids, files); -#elif defined(ANIMIA_ON_UNIX) - unix::EnumerateOpenFiles(pids, files); -#endif + fd.EnumerateOpenFiles(pids, files); for (const auto& [pid, file] : files) { success |= AddMedia({MediaInfoType::File, file}); diff -r 28842a8d0c6b -r 478f3b366199 src/gui/window.cc --- a/src/gui/window.cc Sun Nov 12 04:53:19 2023 -0500 +++ b/src/gui/window.cc Sun Nov 12 16:43:07 2023 -0500 @@ -67,11 +67,12 @@ /* this is very very stinky */ connect(timer, &QTimer::timeout, this, [this] { + bool success = false; + NowPlayingPage* page = reinterpret_cast(stack->widget(static_cast(Pages::NOW_PLAYING))); std::vector files; - if (!Track::Media::GetCurrentlyPlaying(files)) - return; + Track::Media::GetCurrentlyPlaying(files); /* this should really be more intertwined with anitomy */ for (const auto& file : files) { @@ -84,7 +85,12 @@ qDebug() << id; page->SetPlaying(Anime::db.items[id], elements); + + success = true; } + + if (!success) + page->SetDefault(); }); timer->start(5000); } diff -r 28842a8d0c6b -r 478f3b366199 src/track/media.cc --- a/src/track/media.cc Sun Nov 12 04:53:19 2023 -0500 +++ b/src/track/media.cc Sun Nov 12 16:43:07 2023 -0500 @@ -10,6 +10,7 @@ #include #include #include +#include namespace Track { namespace Media {