summaryrefslogtreecommitdiffhomepage
path: root/opentrack/shortcuts.cpp
blob: c9e9ce4146c43ee6438cf360f2bee6c675941c2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "shortcuts.h"
#include <QMutexLocker>

KeyboardShortcutDialog::KeyboardShortcutDialog()
{
    ui.setupUi( this );

    connect(ui.buttonBox, SIGNAL(accepted()), this, SLOT(doOK()));
    connect(ui.buttonBox, SIGNAL(rejected()), this, SLOT(doCancel()));

    for ( int i = 0; i < global_key_sequences.size(); i++) {
        ui.cbxCenterKey->addItem(global_key_sequences.at(i));
        ui.cbxToggleKey->addItem(global_key_sequences.at(i));
        ui.cbxZeroKey->addItem(global_key_sequences.at(i));
    }

    tie_setting(s.center.key_index, ui.cbxCenterKey);
    tie_setting(s.center.alt, ui.chkCenterAlt);
    tie_setting(s.center.shift, ui.chkCenterShift);
    tie_setting(s.center.ctrl, ui.chkCenterCtrl);

    tie_setting(s.toggle.key_index, ui.cbxToggleKey);
    tie_setting(s.toggle.alt, ui.chkToggleAlt);
    tie_setting(s.toggle.shift, ui.chkToggleShift);
    tie_setting(s.toggle.ctrl, ui.chkToggleCtrl);

    tie_setting(s.zero.key_index, ui.cbxZeroKey);
    tie_setting(s.zero.alt, ui.chkZeroAlt);
    tie_setting(s.zero.shift, ui.chkZeroShift);
    tie_setting(s.zero.ctrl, ui.chkZeroCtrl);

    tie_setting(s.s_main.tray_enabled, ui.trayp);
}

void KeyboardShortcutDialog::doOK() {
    s.b->save();
    this->close();
    emit reload();
}

void KeyboardShortcutDialog::doCancel() {
    s.b->reload();
    close();
}

#if defined(_WIN32)
#include <windows.h>

void KeybindingWorker::set_keys(Key kCenter_, Key kToggle_, Key kZero_)
{
    QMutexLocker l(&mtx);

    kCenter = kCenter_;
    kToggle = kToggle_;
    kZero = kZero_;
}

KeybindingWorker::~KeybindingWorker() {
    should_quit = true;
    wait();
    if (dinkeyboard) {
        dinkeyboard->Unacquire();
        dinkeyboard->Release();
    }
    if (din)
        din->Release();
}

KeybindingWorker::KeybindingWorker(Key keyCenter, Key keyToggle, Key keyZero, WId handle, Shortcuts& sc) :
    sc(sc), din(0), dinkeyboard(0), kCenter(keyCenter), kToggle(keyToggle), kZero(keyZero), should_quit(true)
{
    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;
}

static bool isKeyPressed( const Key *key, const BYTE *keystate ) {
    bool shift;
    bool ctrl;
    bool alt;

    if (key->keycode != 0 && keystate[key->keycode] & 0x80)
    {
        shift = ( (keystate[DIK_LSHIFT] & 0x80) || (keystate[DIK_RSHIFT] & 0x80) );
        ctrl  = ( (keystate[DIK_LCONTROL] & 0x80) || (keystate[DIK_RCONTROL] & 0x80) );
        alt   = ( (keystate[DIK_LALT] & 0x80) || (keystate[DIK_RALT] & 0x80) );

        if (key->shift && !shift) return false;
        if (key->ctrl && !ctrl) return false;
        if (key->alt && !alt) return false;

        return true;
    }
    return 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);

        if (isKeyPressed(&kCenter, keystate) && kCenter.should_process())
            emit sc.center();

        if (isKeyPressed(&kToggle, keystate) && kToggle.should_process())
            emit sc.toggle();

        if (isKeyPressed(&kZero, keystate) && kZero.should_process())
            emit sc.zero();

        // keypresses get dropped with high values
        Sleep(15);
    }
}
#endif

void Shortcuts::bind_keyboard_shortcut(K &key, key_opts& k)
{
#if !defined(_WIN32)
    const int idx = k.key_index;
    if (!key)
        key = std::make_shared<QxtGlobalShortcut>();
    else {
        key->setEnabled(false);
        key->setShortcut(QKeySequence::UnknownKey);
    }
    if (idx > 0)
    {
        QString seq(global_key_sequences.value(idx, ""));
        if (!seq.isEmpty())
        {
            if (k.shift)
                seq = "Shift+" + seq;
            if (k.alt)
                seq = "Alt+" + seq;
            if (k.ctrl)
                seq = "Ctrl+" + seq;
            key->setShortcut(QKeySequence::fromString(seq, QKeySequence::PortableText));
            key->setEnabled();
        }
    }
#else
    key = K();
    int idx = k.key_index;
    key.keycode = 0;
    key.shift = key.alt = key.ctrl = 0;
    if (idx > 0 && idx < global_windows_key_sequences.size())
        key.keycode = global_windows_key_sequences[idx];
    key.shift = k.shift;
    key.alt = k.alt;
    key.ctrl = k.ctrl;
#endif
}

void Shortcuts::reload() {
#ifndef _WIN32
    if (keyCenter)
    {
        keyCenter->setShortcut(QKeySequence::UnknownKey);
        keyCenter->setEnabled(false);
    }
    if (keyToggle)
    {
        keyToggle->setShortcut(QKeySequence::UnknownKey);
        keyToggle->setEnabled(false);
    }
    if (keyZero)
    {
        keyZero->setShortcut(QKeySequence::UnknownKey);
        keyZero->setEnabled(false);
    }
#endif
    bind_keyboard_shortcut(keyCenter, s.center);
    bind_keyboard_shortcut(keyToggle, s.toggle);
    bind_keyboard_shortcut(keyZero, s.zero);
#ifdef _WIN32
    bool is_new = keybindingWorker == nullptr;
    if (is_new)
    {
        keybindingWorker = std::make_shared<KeybindingWorker>(keyCenter, keyToggle, keyZero, handle, *this);
        keybindingWorker->start();
    }
    else
        keybindingWorker->set_keys(keyCenter, keyToggle, keyZero);
#endif
}