From 9a0cb353b8a19b186fe3c9138e058779cd53bd33 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Thu, 13 Aug 2015 10:53:19 +0200 Subject: initial broken multi-platform process lister --- facetracknoir/process_detector.cpp | 34 +----------- facetracknoir/process_detector.h | 17 ------ opentrack-compat/process-list.hpp | 105 +++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 50 deletions(-) mode change 100644 => 100755 facetracknoir/process_detector.cpp mode change 100644 => 100755 facetracknoir/process_detector.h create mode 100755 opentrack-compat/process-list.hpp diff --git a/facetracknoir/process_detector.cpp b/facetracknoir/process_detector.cpp old mode 100644 new mode 100755 index df38eb29..19611241 --- a/facetracknoir/process_detector.cpp +++ b/facetracknoir/process_detector.cpp @@ -8,6 +8,7 @@ #include "process_detector.h" #include "facetracknoir/ui.h" +#include "opentrack-compat/process-list.hpp" #include #include #include @@ -153,36 +154,6 @@ void process_detector::remove() ui.tableWidget->removeRow(r); } -#ifdef _WIN32 - -#include -#include - -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; -} - bool process_detector_worker::should_stop() { if (last_exe_name == "") @@ -237,6 +208,3 @@ bool process_detector_worker::config_to_start(QString& str) return false; } - - -#endif diff --git a/facetracknoir/process_detector.h b/facetracknoir/process_detector.h old mode 100644 new mode 100755 index 792a941f..f6497c90 --- a/facetracknoir/process_detector.h +++ b/facetracknoir/process_detector.h @@ -74,8 +74,6 @@ public slots: void browse(); }; -#ifdef _WIN32 - class process_detector_worker : QObject { Q_OBJECT @@ -86,18 +84,3 @@ public: bool should_stop(); }; -#else - -class process_detector_worker : QObject -{ - Q_OBJECT -public: - bool config_to_start(QString&) - { - return false; - } - bool should_stop() { return false; } - -}; - -#endif 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 +#include + +#if defined _WIN32 + +#include +#include + +template +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 +#include +#include +#include + +// link to libproc +template +static QStringList get_all_executable_names() +{ + QStringList ret; + std::vector 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 +#include +template +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 +static QStringList get_all_executable_names() +{ + return QStringList(); +} +#endif -- cgit v1.2.3