summaryrefslogtreecommitdiffhomepage
path: root/opentrack-compat/process-list.hpp
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2015-08-13 10:53:19 +0200
committerStanislaw Halik <sthalik@misaki.pl>2015-08-13 10:53:19 +0200
commit9a0cb353b8a19b186fe3c9138e058779cd53bd33 (patch)
treee8ac0cc42d63045277d5d2bd7af45c5112ec2338 /opentrack-compat/process-list.hpp
parent96841349464fa38e6d0d5f79d66d4012b6ab315c (diff)
initial broken multi-platform process lister
Diffstat (limited to 'opentrack-compat/process-list.hpp')
-rwxr-xr-xopentrack-compat/process-list.hpp105
1 files changed, 105 insertions, 0 deletions
diff --git a/opentrack-compat/process-list.hpp b/opentrack-compat/process-list.hpp
new file mode 100755
index 00000000..a00cbb17
--- /dev/null
+++ b/opentrack-compat/process-list.hpp
@@ -0,0 +1,105 @@
+#pragma once
+
+#include <QDebug>
+#include <QStringList>
+
+#if defined _WIN32
+
+#include <windows.h>
+#include <TlHelp32.h>
+
+template<typename = void>
+static QStringList get_all_executable_names()
+{
+ QStringList ret;
+ HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
+ if (h == INVALID_HANDLE_VALUE)
+ return ret;
+
+ PROCESSENTRY32 e;
+ e.dwSize = sizeof(e);
+
+ if (Process32First(h, &e) != TRUE)
+ {
+ CloseHandle(h);
+ return ret;
+ }
+
+ do {
+ ret.append(e.szExeFile);
+ } while (Process32Next(h, &e) == TRUE);
+
+ CloseHandle(h);
+
+ return ret;
+}
+#elif defined __APPLE__
+#include <libproc.h>
+#include <sys/param.h>
+#include <cerrno>
+#include <vector>
+
+// link to libproc
+template<typename = void>
+static QStringList get_all_executable_names()
+{
+ QStringList ret;
+ std::vector<int> vec;
+
+ while (true)
+ {
+ int numproc = proc_listpids(PROC_ALL_PIDS, 0, nullptr, 0);
+ if (numproc == -1)
+ {
+ qDebug() << "numproc failed" << errno;
+ break;
+ }
+ vec.resize(numproc);
+ int cnt = proc_listpids(PROC_ALL_PIDS, 0, &vec[0], sizeof(int) * numproc);
+ if (cnt <= numproc)
+ {
+ char name[2 * 2 * MAXCOMLEN + 1];
+ for (int i = 0; i < cnt; i++)
+ {
+ int ret = proc_name(vec[i], name, sizeof(name)-1);
+ if (ret <= 0)
+ continue;
+ name[ret] = '\0';
+ ret.append(name);
+ }
+ return ret;
+ }
+ }
+}
+
+#elif defined __linux
+
+// link to procps
+#include <proc/readproc.h>
+#include <cerrno>
+template<typename = void>
+static QStringList get_all_executable_names()
+{
+ QStringList ret;
+ proc_t** procs = readproctab(PROC_FILLCOM);
+ if (procs == nullptr)
+ {
+ qDebug() << "readproctab" << errno;
+ return ret;
+ }
+ for (int i = 0; procs[i]; i++)
+ {
+ auto& proc = *procs[i];
+ ret.append(proc.cmd);
+ }
+ freeproctab(procs);
+ return ret;
+}
+
+#else
+template<typename = void>
+static QStringList get_all_executable_names()
+{
+ return QStringList();
+}
+#endif