diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2016-08-12 17:03:51 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2016-08-12 17:03:51 +0200 |
commit | eda47922d9bd6d10e4e2fb3d3191edfaa126aef5 (patch) | |
tree | 86cef123f082a743275842d0c110dfa3d6115020 | |
parent | 0ed45348c428a950db589caa227efa86af499170 (diff) |
few: use existing com threading init function
-rw-r--r-- | cv/camera-dialog.cpp | 13 | ||||
-rw-r--r-- | cv/camera-dialog.hpp | 8 | ||||
-rw-r--r-- | gui/main.cpp | 2 | ||||
-rw-r--r-- | opentrack-compat/camera-names.cpp | 9 | ||||
-rw-r--r-- | opentrack-dinput/dinput.cpp | 5 |
5 files changed, 11 insertions, 26 deletions
diff --git a/cv/camera-dialog.cpp b/cv/camera-dialog.cpp index 1be54ece..c3e8f6fe 100644 --- a/cv/camera-dialog.cpp +++ b/cv/camera-dialog.cpp @@ -20,27 +20,19 @@ void camera_dialog::maybe_grab_frame(cv::VideoCapture& cap) } } -#ifdef _WIN32 -void camera_dialog::init_com_threading() -{ - HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); - if (FAILED(hr)) - qDebug() << "failed CoInitializeEx" << hr << "code" << GetLastError(); -} -#endif - camera_dialog::~camera_dialog() {} void camera_dialog::open_camera_settings(cv::VideoCapture* cap, const QString& camera_name, QMutex* camera_mtx) { #ifdef _WIN32 + init_com_threading(com_apartment); + if (cap) { QMutexLocker l(camera_mtx); if (cap->isOpened()) { - init_com_threading(); maybe_grab_frame(*cap); cap->set(cv::CAP_PROP_SETTINGS, 1); return; @@ -54,7 +46,6 @@ void camera_dialog::open_camera_settings(cv::VideoCapture* cap, const QString& c if (!t.isSingleShot()) QObject::connect(&t, &QTimer::timeout, [&]() -> void { delete_capture(); }); - init_com_threading(); fake_capture = cv::VideoCapture(camera_name_to_index(camera_name)); maybe_grab_frame(fake_capture); fake_capture.set(cv::CAP_PROP_SETTINGS, 1); diff --git a/cv/camera-dialog.hpp b/cv/camera-dialog.hpp index 15ae2bc9..3fd853c3 100644 --- a/cv/camera-dialog.hpp +++ b/cv/camera-dialog.hpp @@ -10,6 +10,7 @@ #include "opentrack-compat/camera-names.hpp" #include "opentrack-compat/sleep.hpp" +#include "opentrack-compat/win32-com.hpp" #ifdef __linux # include <QProcess> @@ -17,9 +18,6 @@ #ifdef _WIN32 # include <QTimer> -# include <objbase.h> -# include <winerror.h> -# include <windows.h> #endif #include <opencv2/videoio.hpp> @@ -28,10 +26,6 @@ class camera_dialog { static void maybe_grab_frame(cv::VideoCapture& cap); -#ifdef _WIN32 - static void init_com_threading(); -#endif - public: virtual ~camera_dialog(); void open_camera_settings(cv::VideoCapture*, const QString&, QMutex*); diff --git a/gui/main.cpp b/gui/main.cpp index eb040337..a9252c4d 100644 --- a/gui/main.cpp +++ b/gui/main.cpp @@ -10,6 +10,7 @@ #include "main-window.hpp" #include "opentrack-compat/options.hpp" +#include "opentrack-compat/win32-com.hpp" using namespace options; #include <QApplication> #include <QCommandLineParser> @@ -111,6 +112,7 @@ static void add_program_library_path() int main(int argc, char** argv) { #ifdef _WIN32 + init_com_threading(com_apartment); add_program_library_path(); #elif !defined(__linux) // workaround QTBUG-38598 diff --git a/opentrack-compat/camera-names.cpp b/opentrack-compat/camera-names.cpp index f17a259a..21ff3b52 100644 --- a/opentrack-compat/camera-names.cpp +++ b/opentrack-compat/camera-names.cpp @@ -2,8 +2,9 @@ #ifdef _WIN32 # define NO_DSHOW_STRSAFE -# include <windows.h> # include <dshow.h> +# include "win32-com.hpp" +# include <cwchar> #elif defined(__unix) || defined(__linux) || defined(__APPLE__) # include <unistd.h> #endif @@ -32,9 +33,7 @@ OPENTRACK_COMPAT_EXPORT QList<QString> get_camera_names() #if defined(_WIN32) // Create the System Device Enumerator. HRESULT hr; - hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); - if (FAILED(hr)) - qDebug() << "failed CoInitializeEx" << hr << GetLastError(); + init_com_threading(com_apartment); ICreateDevEnum *pSysDevEnum = NULL; hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, (void **)&pSysDevEnum); if (FAILED(hr)) @@ -61,7 +60,7 @@ OPENTRACK_COMPAT_EXPORT QList<QString> get_camera_names() if (SUCCEEDED(hr)) { // Display the name in your UI somehow. - QString str((QChar*)varName.bstrVal, wcslen(varName.bstrVal)); + QString str((QChar*)varName.bstrVal, int(std::wcslen(varName.bstrVal))); ret.append(str); } VariantClear(&varName); diff --git a/opentrack-dinput/dinput.cpp b/opentrack-dinput/dinput.cpp index 06b5e4f2..217df6a7 100644 --- a/opentrack-dinput/dinput.cpp +++ b/opentrack-dinput/dinput.cpp @@ -1,6 +1,7 @@ #ifdef _WIN32 #include "dinput.hpp" +#include "opentrack-compat/win32-com.hpp" #include <QDebug> std::atomic<int> dinput_handle::refcnt; @@ -9,9 +10,7 @@ dinput_handle::di_t dinput_handle::handle(dinput_handle::make_di()); LPDIRECTINPUT8& dinput_handle::init_di() { - HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); - if (FAILED(hr)) - qDebug() << "dinput: failed CoInitializeEx" << hr << GetLastError(); + init_com_threading(com_multithreaded); static LPDIRECTINPUT8 di_ = nullptr; if (di_ == nullptr) |