diff options
Diffstat (limited to 'opentrack')
| -rw-r--r-- | opentrack/keybinding-worker.cpp | 112 | ||||
| -rw-r--r-- | opentrack/keybinding-worker.hpp | 70 | ||||
| -rw-r--r-- | opentrack/shortcuts.cpp | 104 | ||||
| -rw-r--r-- | opentrack/shortcuts.h | 69 | 
4 files changed, 186 insertions, 169 deletions
| diff --git a/opentrack/keybinding-worker.cpp b/opentrack/keybinding-worker.cpp new file mode 100644 index 00000000..29b2cf9f --- /dev/null +++ b/opentrack/keybinding-worker.cpp @@ -0,0 +1,112 @@ +/* Copyright (c) 2014-2015, Stanislaw Halik <sthalik@misaki.pl> + + * Permission to use, copy, modify, and/or distribute this + * software for any purpose with or without fee is hereby granted, + * provided that the above copyright notice and this permission + * notice appear in all copies. + */ + +#include "keybinding-worker.hpp" + +#if defined(_WIN32) +#   include <functional> +#   include <windows.h> +#   include <QDebug> + +KeybindingWorker::~KeybindingWorker() { +    should_quit = true; +    wait(); +    if (dinkeyboard) { +        dinkeyboard->Unacquire(); +        dinkeyboard->Release(); +    } +    if (din) +        din->Release(); +} + +KeybindingWorker::KeybindingWorker(std::function<void(Key&)> receiver, WId h) : +    should_quit(true), receiver(receiver) +{ +    HWND handle = reinterpret_cast<HWND>(h); + +    if (DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&din, NULL) != DI_OK) { +        qDebug() << "setup DirectInput8 Creation failed!" << GetLastError(); +        return; +    } +    if (din->CreateDevice(GUID_SysKeyboard, &dinkeyboard, NULL) != DI_OK) { +        din->Release(); +        din = 0; +        qDebug() << "setup CreateDevice function failed!" << GetLastError(); +        return; +    } +    if (dinkeyboard->SetDataFormat(&c_dfDIKeyboard) != DI_OK) { +        qDebug() << "setup SetDataFormat function failed!" << GetLastError(); +        dinkeyboard->Release(); +        dinkeyboard = 0; +        din->Release(); +        din = 0; +        return; +    } +    if (dinkeyboard->SetCooperativeLevel((HWND) handle, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND) != DI_OK) { +        dinkeyboard->Release(); +        din->Release(); +        din = 0; +        dinkeyboard = 0; +        qDebug() << "setup SetCooperativeLevel function failed!" << GetLastError(); +        return; +    } +    if (dinkeyboard->Acquire() != DI_OK) +    { +        dinkeyboard->Release(); +        din->Release(); +        din = 0; +        dinkeyboard = 0; +        qDebug() << "setup dinkeyboard Acquire failed!" << GetLastError(); +        return; +    } +    should_quit = false; +} + +void KeybindingWorker::run() { +    BYTE keystate[256]; + +    while (!should_quit) +    { +        if (dinkeyboard->GetDeviceState(256, (LPVOID)keystate) != DI_OK) { +            qDebug() << "Tracker::run GetDeviceState function failed!" << GetLastError(); +            Sleep(25); +            continue; +        } + +        QMutexLocker l(&mtx); + +        for (int i = 0; i < 256; i++) +        { +            Key k; +            if (keystate[i] & 0x80) +            { +                switch (i) +                { +                case DIK_LCONTROL: +                case DIK_LSHIFT: +                case DIK_LALT: +                case DIK_RCONTROL: +                case DIK_RSHIFT: +                case DIK_RALT: +                    break; +                default: +                    k.shift = !!(keystate[DIK_LSHIFT] & 0x80) || !!(keystate[DIK_RSHIFT] & 0x80); +                    k.alt = !!(keystate[DIK_LALT] & 0x80) || !!(keystate[DIK_RALT] & 0x80); +                    k.ctrl = !!(keystate[DIK_LCONTROL] & 0x80) || !!(keystate[DIK_RCONTROL] & 0x80); +                    k.keycode = i; +                    receiver(k); +                    break; +                } +            } +        } + +        // keypresses get dropped with high values +        Sleep(4); +    } +} +#endif
\ No newline at end of file diff --git a/opentrack/keybinding-worker.hpp b/opentrack/keybinding-worker.hpp new file mode 100644 index 00000000..e720ffdc --- /dev/null +++ b/opentrack/keybinding-worker.hpp @@ -0,0 +1,70 @@ +/* Copyright (c) 2014-2015, Stanislaw Halik <sthalik@misaki.pl> + + * Permission to use, copy, modify, and/or distribute this + * software for any purpose with or without fee is hereby granted, + * provided that the above copyright notice and this permission + * notice appear in all copies. + */ + +#pragma once + +#ifdef BUILD_api +#   include "opentrack-compat/export.hpp" +#else +#   include "opentrack-compat/import.hpp" +#endif + +#include "opentrack-compat/timer.hpp" +#include <QThread> +#include <QMutex> +#include <QWidget> +#include <functional> + +#ifdef _WIN32 +#   undef DIRECTINPUT_VERSION +#   define DIRECTINPUT_VERSION 0x0800 +#   include <windows.h> +#   include <dinput.h> +struct Key { +    BYTE keycode; +    bool shift; +    bool ctrl; +    bool alt; +    Timer timer; +public: +    Key() : keycode(0), shift(false), ctrl(false), alt(false) +    { +    } + +    bool should_process() +    { +        if (keycode == 0) +            return false; +        bool ret = timer.elapsed_ms() > 100; +        timer.start(); +        return ret; +    } +}; +#else +typedef unsigned char BYTE; +struct Key { int foo; }; +#endif + +struct OPENTRACK_EXPORT KeybindingWorker : public QThread { +#ifdef _WIN32 +private: +    LPDIRECTINPUT8 din; +    LPDIRECTINPUTDEVICE8 dinkeyboard; +    QMutex mtx; +public: +    volatile bool should_quit; +    std::function<void(Key&)> receiver; +    ~KeybindingWorker(); +    KeybindingWorker(std::function<void(Key&)> receiver, WId h); +    void run(); +#else +public: +    KeybindingWorker(Key, Key, Key, WId) {} +    void run() {} +#endif +};
\ No newline at end of file diff --git a/opentrack/shortcuts.cpp b/opentrack/shortcuts.cpp index a33cf088..5f5ad922 100644 --- a/opentrack/shortcuts.cpp +++ b/opentrack/shortcuts.cpp @@ -7,110 +7,8 @@   */  #include "shortcuts.h" -#include <QMutexLocker> - -#if defined(_WIN32) -#include <functional> -#include <windows.h>  #include "win32-shortcuts.h" - -KeybindingWorker::~KeybindingWorker() { -    should_quit = true; -    wait(); -    if (dinkeyboard) { -        dinkeyboard->Unacquire(); -        dinkeyboard->Release(); -    } -    if (din) -        din->Release(); -} - -KeybindingWorker::KeybindingWorker(std::function<void(Key&)> receiver, WId h) : -    should_quit(true), receiver(receiver) -{ -    HWND handle = reinterpret_cast<HWND>(h); - -    if (DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&din, NULL) != DI_OK) { -        qDebug() << "setup DirectInput8 Creation failed!" << GetLastError(); -        return; -    } -    if (din->CreateDevice(GUID_SysKeyboard, &dinkeyboard, NULL) != DI_OK) { -        din->Release(); -        din = 0; -        qDebug() << "setup CreateDevice function failed!" << GetLastError(); -        return; -    } -    if (dinkeyboard->SetDataFormat(&c_dfDIKeyboard) != DI_OK) { -        qDebug() << "setup SetDataFormat function failed!" << GetLastError(); -        dinkeyboard->Release(); -        dinkeyboard = 0; -        din->Release(); -        din = 0; -        return; -    } -    if (dinkeyboard->SetCooperativeLevel((HWND) handle, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND) != DI_OK) { -        dinkeyboard->Release(); -        din->Release(); -        din = 0; -        dinkeyboard = 0; -        qDebug() << "setup SetCooperativeLevel function failed!" << GetLastError(); -        return; -    } -    if (dinkeyboard->Acquire() != DI_OK) -    { -        dinkeyboard->Release(); -        din->Release(); -        din = 0; -        dinkeyboard = 0; -        qDebug() << "setup dinkeyboard Acquire failed!" << GetLastError(); -        return; -    } -    should_quit = false; -} - -void KeybindingWorker::run() { -    BYTE keystate[256]; - -    while (!should_quit) -    { -        if (dinkeyboard->GetDeviceState(256, (LPVOID)keystate) != DI_OK) { -            qDebug() << "Tracker::run GetDeviceState function failed!" << GetLastError(); -            Sleep(25); -            continue; -        } - -        QMutexLocker l(&mtx); - -        for (int i = 0; i < 256; i++) -        { -            Key k; -            if (keystate[i] & 0x80) -            { -                switch (i) -                { -                case DIK_LCONTROL: -                case DIK_LSHIFT: -                case DIK_LALT: -                case DIK_RCONTROL: -                case DIK_RSHIFT: -                case DIK_RALT: -                    break; -                default: -                    k.shift = !!(keystate[DIK_LSHIFT] & 0x80) || !!(keystate[DIK_RSHIFT] & 0x80); -                    k.alt = !!(keystate[DIK_LALT] & 0x80) || !!(keystate[DIK_RALT] & 0x80); -                    k.ctrl = !!(keystate[DIK_LCONTROL] & 0x80) || !!(keystate[DIK_RCONTROL] & 0x80); -                    k.keycode = i; -                    receiver(k); -                    break; -                } -            } -        } - -        // keypresses get dropped with high values -        Sleep(4); -    } -} -#endif +#include <QMutexLocker>  void Shortcuts::bind_keyboard_shortcut(K &key, key_opts& k)  { diff --git a/opentrack/shortcuts.h b/opentrack/shortcuts.h index 81c1bb80..84231850 100644 --- a/opentrack/shortcuts.h +++ b/opentrack/shortcuts.h @@ -8,14 +8,6 @@  #pragma once  #include <QObject> -#include <QWidget> -#include "opentrack-compat/timer.hpp" -#include <QThread> -#include <QMessageBox> -#include <QCheckBox> -#include <QComboBox> -#include <QSettings> -#include <QMutex>  #ifdef BUILD_api  #   include "opentrack-compat/export.hpp" @@ -24,69 +16,14 @@  #endif  #include "qxt-mini/QxtGlobalShortcut" -#include "opentrack/plugin-support.hpp"  #include "opentrack-compat/options.hpp"  #include "opentrack/main-settings.hpp" -using namespace options; - -namespace { -    extern QList<QString> global_key_sequences; -} - -#if defined(_WIN32) - -namespace { -    extern QList<int> global_windows_key_sequences; -} -#   undef DIRECTINPUT_VERSION -#   define DIRECTINPUT_VERSION 0x0800 -#   include <windows.h> -#   include <dinput.h> - -struct Key { -    BYTE keycode; -    bool shift; -    bool ctrl; -    bool alt; -    Timer timer; -public: -    Key() : keycode(0), shift(false), ctrl(false), alt(false) -    { -    } - -    bool should_process() -    { -        if (keycode == 0) -            return false; -        bool ret = timer.elapsed_ms() > 100; -        timer.start(); -        return ret; -    } -}; -#else -typedef unsigned char BYTE; -struct Key { int foo; }; -#endif - -struct OPENTRACK_EXPORT KeybindingWorker : public QThread {  #ifdef _WIN32 -private: -    LPDIRECTINPUT8 din; -    LPDIRECTINPUTDEVICE8 dinkeyboard; -    QMutex mtx; -public: -    volatile bool should_quit; -    std::function<void(Key&)> receiver; -    ~KeybindingWorker(); -    KeybindingWorker(std::function<void(Key&)> receiver, WId h); -    void run(); -#else -public: -    KeybindingWorker(Key, Key, Key, WId) {} -    void run() {} +#   include "keybinding-worker.hpp"  #endif -}; + +using namespace options;  struct OPENTRACK_EXPORT Shortcuts : public QObject {      Q_OBJECT | 
