changeset 139:478f3b366199

dep/animia: separate lots of things, use base class for OS stuff
author Paper <mrpapersonic@gmail.com>
date Sun, 12 Nov 2023 16:43:07 -0500
parents 28842a8d0c6b
children 1e696863b54c
files dep/animia/CMakeLists.txt dep/animia/include/animia.h dep/animia/include/animia/fd.h dep/animia/include/animia/fd/bsd.h dep/animia/include/animia/fd/linux.h dep/animia/include/animia/fd/win32.h dep/animia/include/animia/os.h dep/animia/include/animia/types.h dep/animia/include/animia/util.h dep/animia/src/animia.cc dep/animia/src/fd/bsd.cc dep/animia/src/fd/linux.cc dep/animia/src/fd/win32.cc dep/animia/src/strategist.cc src/gui/window.cc src/track/media.cc
diffstat 16 files changed, 147 insertions(+), 79 deletions(-) [+]
line wrap: on
line diff
--- 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)
--- 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;
 };
 
--- /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 <set>
+#include <string>
+#include <tuple>
+#include <string>
+#include <vector>
+
+#include "animia/types.h"
+
+namespace animia::internal {
+
+class BaseFdTools {
+	public:
+		virtual bool GetAllPids(std::set<pid_t>& pids) { return false; }
+		virtual bool GetProcessName(pid_t pid, std::string& result) { return false; }
+		virtual bool EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<std::tuple<pid_t, std::string>>& files) { return false; }
+};
+
+extern BaseFdTools& fd; // defined in animia.cc
+
+}
+
+#endif // __animia__animia__fd_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 <string>
 #include <set>
 
-#include <sys/types.h> // pid_t
+#include "animia/types.h"
+#include "animia/fd.h"
 
 namespace animia::internal::unix {
 
-bool GetAllPids(std::set<pid_t>& pids);
-bool GetProcessName(pid_t pid, std::string& result);
-bool EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<std::tuple<pid_t, std::string>>& files);
+class UnixFdTools final : public BaseFdTools {
+	public:
+		bool GetAllPids(std::set<pid_t>& pids) override;
+		bool GetProcessName(pid_t pid, std::string& result) override;
+		bool EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<std::tuple<pid_t, std::string>>& files) override;
+};
 
 }
 
--- 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 <string>
 #include <set>
 
-#include <sys/types.h> // pid_t
+#include "animia/types.h"
+#include "animia/fd.h"
 
 namespace animia::internal::linux {
 
-bool GetAllPids(std::set<pid_t>& pids);
-bool GetProcessName(pid_t pid, std::string& result);
-bool EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<std::tuple<pid_t, std::string>>& files);
+class LinuxFdTools final : public BaseFdTools {
+	public:
+		bool GetAllPids(std::set<pid_t>& pids) override;
+		bool GetProcessName(pid_t pid, std::string& result) override;
+		bool EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<std::tuple<pid_t, std::string>>& files) override;
+};
 
 }
 
--- 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 <windows.h>
 
-#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<HANDLE, HandleDeconstructor>;
 
-bool GetAllPids(std::set<pid_t>& pids);
-bool GetProcessName(pid_t pid, std::string& result);
-bool EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<std::tuple<pid_t, std::string>>& files);
+class Win32FdTools final : public BaseFdTools {
+	public:
+		bool GetAllPids(std::set<pid_t>& pids) override;
+		bool GetProcessName(pid_t pid, std::string& result) override;
+		bool EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<std::tuple<pid_t, std::string>>& files) override;
+};
 
 }
 
--- /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
--- /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 <windows.h>
+namespace animia::internal {
+	typedef DWORD pid_t;
+}
+#else
+#	include <sys/types.h>
+namespace animia::internal {
+	typedef ::pid_t pid_t;
+}
+#endif
+
+#endif // __animia__animia__types_h
\ No newline at end of file
--- 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 <windows.h>
-namespace animia {
-	typedef DWORD pid_t;
-}
-#endif
-
 namespace animia::internal::util {
 
 bool ReadFile(const std::string& path, std::string& data);
--- 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 <windows.h>
 
 #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 <iostream>
 
 namespace animia {
@@ -26,24 +50,14 @@
 }
 
 bool GetResults(const std::vector<Player>& players, std::vector<Result>& results) {
-	std::set<pid_t> pids;
+	std::set<internal::pid_t> 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))
--- 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 <fcntl.h>
 #include <iostream>
@@ -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<pid_t>& pids) {
+bool UnixFdTools::GetAllPids(std::set<pid_t>& 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<pid_t>& pids, std::vector<std::tuple<pid_t, std::string>>& files) {
+bool UnixFdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<std::tuple<pid_t, std::string>>& files) {
 	for (const auto& pid : pids) {
 		int bufsz = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, NULL, 0);
 		if (bufsz == -1)
--- 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<pid_t>& pids) {
+bool LinuxFdTools::GetAllPids(std::set<pid_t>& 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<pid_t>& pids, std::vector<std::tuple<pid_t, std::string>>& files) {
+bool LinuxFdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<std::tuple<pid_t, std::string>>& files) {
 	for (const auto& pid : pids) {
 		const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/fd";
 
--- 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<pid_t>& pids) {
+bool Win32FdTools::GetAllPids(std::set<pid_t>& 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<pid_t>& pids, std::vector<std::tuple<pid_t, std::string>>& files) {
+bool Win32FdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<std::tuple<pid_t, std::string>>& files) {
 	std::unordered_map<pid_t, Handle> proc_handles;
 
 	for (const pid_t& pid : pids) {
--- 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 <iostream>
 
-#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<pid_t> pids{result_.process.pid};
 	std::vector<std::tuple<pid_t, std::string>> 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});
--- 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<NowPlayingPage*>(stack->widget(static_cast<int>(Pages::NOW_PLAYING)));
 
 		std::vector<std::string> 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);
 }
--- 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 <vector>
 #include <iostream>
 #include <QFile>
+#include <QTextStream>
 
 namespace Track {
 namespace Media {