-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1142 lines (963 loc) · 41.9 KB
/
main.cpp
File metadata and controls
1142 lines (963 loc) · 41.9 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <QApplication>
#include <QQueue> // Add this include
#include <QMainWindow>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QWidget>
#include <QProcess>
#include <QDialog>
#include <QComboBox>
#include <QDialogButtonBox>
#include <QFormLayout>
#include <QDir>
#include <QFileInfo>
#include <QDebug>
#include <QMouseEvent>
#include <QSettings>
#include <QPoint>
#include <QFrame>
#include <QLabel>
#include <QMessageBox>
#include <QTimer>
#include <linux/input.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/time.h>
#include <libgen.h>
#include <limits.h>
// X11 includes for window management
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/Xresource.h>
class MouseMonitor : public QObject {
Q_OBJECT
public:
MouseMonitor(QObject *parent = nullptr) : QObject(parent) {
monitorTimer = new QTimer(this);
connect(monitorTimer, &QTimer::timeout, this, &MouseMonitor::recordMousePosition);
monitorTimer->start(50); // Check every 50ms (20 samples per second)
}
~MouseMonitor() {
monitorTimer->stop();
}
QPoint getMostFrequentPosition() {
if (positionQueue.isEmpty()) {
return QCursor::pos();
}
// Group positions within 25px tolerance and count frequency
QHash<QPoint, int> frequency;
for (const QPoint &pos : positionQueue) {
// Find existing position within 25px tolerance, or use current position
QPoint groupedPos = findGroupedPosition(pos, frequency);
frequency[groupedPos]++;
}
// Find the most frequent position
QPoint mostFrequent;
int maxCount = 0;
for (auto it = frequency.begin(); it != frequency.end(); ++it) {
if (it.value() > maxCount) {
maxCount = it.value();
mostFrequent = it.key();
}
}
qDebug() << "Most frequent mouse position (25px fuzzy, 3s window):" << mostFrequent << "count:" << maxCount << "total samples:" << positionQueue.size();
return mostFrequent;
}
private slots:
void recordMousePosition() {
QPoint currentPos = QCursor::pos();
positionQueue.enqueue(currentPos);
// Keep last 3 seconds of data (60 samples at 50ms interval)
while (positionQueue.size() > 60) {
positionQueue.dequeue();
}
}
private:
QPoint findGroupedPosition(const QPoint &pos, const QHash<QPoint, int> &frequency) {
const int tolerance = 25; // 25px fuzzy tolerance
// Look for existing position within tolerance
for (auto it = frequency.begin(); it != frequency.end(); ++it) {
const QPoint &existingPos = it.key();
if (abs(pos.x() - existingPos.x()) <= tolerance &&
abs(pos.y() - existingPos.y()) <= tolerance) {
return existingPos; // Return the existing grouped position
}
}
// No existing position within tolerance, return the original position
return pos;
}
QTimer *monitorTimer;
QQueue<QPoint> positionQueue; // FIFO queue for last 3 seconds of positions
};
class SetupDialog : public QDialog {
Q_OBJECT
public:
SetupDialog(QWidget *parent = nullptr) : QDialog(parent) {
setWindowFlags(Qt::FramelessWindowHint);
setFixedSize(350, 220);
// Open X11 display
xdisplay = XOpenDisplay(NULL);
if (!xdisplay) {
QMessageBox::critical(this, "Error", "Failed to open X11 display");
return;
}
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->setSpacing(8);
mainLayout->setContentsMargins(12, 12, 12, 12);
// Title label
QLabel *titleLabel = new QLabel("Select Target", this);
titleLabel->setStyleSheet("QLabel { font-weight: bold; color: white; background-color: #333333; padding: 4px; border-radius: 3px; }");
titleLabel->setAlignment(Qt::AlignCenter);
mainLayout->addWidget(titleLabel);
// Window selection with label and refresh button
QHBoxLayout *windowLayout = new QHBoxLayout();
QLabel *windowLabel = new QLabel("Window:", this);
windowLabel->setStyleSheet("QLabel { color: #cccccc; }");
windowLayout->addWidget(windowLabel);
windowLayout->addStretch();
QPushButton *refreshButton = new QPushButton("Refresh", this);
refreshButton->setFixedSize(60, 28);
refreshButton->setStyleSheet("QPushButton { background-color: #666666; color: white; border: 1px solid #888888; border-radius: 3px; font-size: 10px; } QPushButton:hover { background-color: #777777; }");
windowLayout->addWidget(refreshButton);
mainLayout->addLayout(windowLayout);
windowCombo = new QComboBox(this);
windowCombo->setFixedHeight(28);
populateWindows();
mainLayout->addWidget(windowCombo);
// Keyboard selection with label
QLabel *keyboardLabel = new QLabel("Keyboard:", this);
keyboardLabel->setStyleSheet("QLabel { color: #cccccc; }");
mainLayout->addWidget(keyboardLabel);
keyboardCombo = new QComboBox(this);
keyboardCombo->setFixedHeight(28);
populateKeyboards();
mainLayout->addWidget(keyboardCombo);
// Status label
statusLabel = new QLabel("", this);
statusLabel->setStyleSheet("QLabel { color: #aaaaaa; font-size: 10px; }");
statusLabel->setAlignment(Qt::AlignCenter);
mainLayout->addWidget(statusLabel);
// OK button
QPushButton *okButton = new QPushButton("Start Control", this);
okButton->setFixedHeight(32);
okButton->setStyleSheet("QPushButton { background-color: #4444ff; color: white; border: 1px solid #6666ff; border-radius: 4px; font-weight: bold; } QPushButton:hover { background-color: #6666ff; }");
mainLayout->addWidget(okButton);
connect(okButton, &QPushButton::clicked, this, &QDialog::accept);
connect(refreshButton, &QPushButton::clicked, this, &SetupDialog::refreshWindows);
updateStatus();
}
~SetupDialog() {
if (xdisplay) {
XCloseDisplay(xdisplay);
}
}
QString selectedWindow() const {
return windowCombo->currentText();
}
QString selectedKeyboard() const {
return keyboardCombo->currentText();
}
QString selectedWindowId() const {
return windowCombo->currentData().toString();
}
private slots:
void refreshWindows() {
windowCombo->clear();
populateWindows();
updateStatus();
}
private:
void updateStatus() {
QString status = QString("Found %1 windows, %2 keyboards").arg(windowCombo->count()).arg(keyboardCombo->count());
statusLabel->setText(status);
}
QString getWindowTitle(Window win) {
QString title;
// Method 1: Try _NET_WM_NAME (UTF-8)
Atom netWmName = XInternAtom(xdisplay, "_NET_WM_NAME", False);
Atom utf8String = XInternAtom(xdisplay, "UTF8_STRING", False);
Atom actualType;
int actualFormat;
unsigned long nitems, bytesAfter;
unsigned char *prop = NULL;
if (XGetWindowProperty(xdisplay, win, netWmName, 0, 1024, False, utf8String,
&actualType, &actualFormat, &nitems, &bytesAfter, &prop) == Success) {
if (actualType == utf8String && actualFormat == 8 && nitems > 0) {
title = QString::fromUtf8((char *)prop, nitems);
}
if (prop) XFree(prop);
}
// Method 2: Try WM_NAME (legacy)
if (title.isEmpty()) {
char *name = NULL;
if (XFetchName(xdisplay, win, &name) != 0 && name != NULL) {
title = QString::fromLocal8Bit(name);
XFree(name);
}
}
// Method 3: Try _NET_WM_VISIBLE_NAME
if (title.isEmpty()) {
Atom netWmVisibleName = XInternAtom(xdisplay, "_NET_WM_VISIBLE_NAME", False);
if (XGetWindowProperty(xdisplay, win, netWmVisibleName, 0, 1024, False, utf8String,
&actualType, &actualFormat, &nitems, &bytesAfter, &prop) == Success) {
if (actualType == utf8String && actualFormat == 8 && nitems > 0) {
title = QString::fromUtf8((char *)prop, nitems);
}
if (prop) XFree(prop);
}
}
return title.trimmed();
}
void populateWindows() {
if (!xdisplay) return;
QMap<QString, QString> windows; // title -> windowId
Window root = DefaultRootWindow(xdisplay);
// Method 1: _NET_CLIENT_LIST (most window managers)
Atom netClientList = XInternAtom(xdisplay, "_NET_CLIENT_LIST", False);
Atom actualType;
int actualFormat;
unsigned long nitems, bytesAfter;
unsigned char *clientListProp = NULL;
if (XGetWindowProperty(xdisplay, root, netClientList, 0, 1024, False, XA_WINDOW,
&actualType, &actualFormat, &nitems, &bytesAfter, &clientListProp) == Success) {
if (actualType == XA_WINDOW && actualFormat == 32 && nitems > 0) {
Window *windowList = (Window *)clientListProp;
for (unsigned long i = 0; i < nitems; i++) {
Window win = windowList[i];
if (win == None) continue;
QString title = getWindowTitle(win);
if (!title.isEmpty() && !isSystemWindow(title, win)) {
QString windowIdStr = QString::number(win, 16);
windows[title] = windowIdStr;
}
}
}
if (clientListProp) XFree(clientListProp);
}
// Method 2: XQueryTree (fallback - finds all child windows)
if (windows.isEmpty()) {
Window root_return, parent_return;
Window *children = NULL;
unsigned int nchildren;
if (XQueryTree(xdisplay, root, &root_return, &parent_return, &children, &nchildren)) {
for (unsigned int i = 0; i < nchildren; i++) {
Window win = children[i];
if (win == None) continue;
// Basic window attribute check
XWindowAttributes attrs;
if (!XGetWindowAttributes(xdisplay, win, &attrs)) continue;
// Skip invisible and override_redirect windows (usually menus/tooltips)
if (attrs.map_state != IsViewable || attrs.override_redirect) continue;
QString title = getWindowTitle(win);
if (!title.isEmpty() && !isSystemWindow(title, win)) {
QString windowIdStr = QString::number(win, 16);
windows[title] = windowIdStr;
}
}
if (children) XFree(children);
}
}
// Method 3: Try _NET_CLIENT_LIST_STACKING (alternative property)
Atom netClientListStacking = XInternAtom(xdisplay, "_NET_CLIENT_LIST_STACKING", False);
if (XGetWindowProperty(xdisplay, root, netClientListStacking, 0, 1024, False, XA_WINDOW,
&actualType, &actualFormat, &nitems, &bytesAfter, &clientListProp) == Success) {
if (actualType == XA_WINDOW && actualFormat == 32 && nitems > 0) {
Window *windowList = (Window *)clientListProp;
for (unsigned long i = 0; i < nitems; i++) {
Window win = windowList[i];
if (win == None) continue;
QString title = getWindowTitle(win);
if (!title.isEmpty() && !isSystemWindow(title, win)) {
QString windowIdStr = QString::number(win, 16);
windows[title] = windowIdStr;
}
}
}
if (clientListProp) XFree(clientListProp);
}
// Add windows to combo box
for (auto it = windows.begin(); it != windows.end(); ++it) {
windowCombo->addItem(it.key(), it.value());
}
if (windowCombo->count() == 0) {
windowCombo->addItem("No windows found - Click Refresh");
}
}
bool isSystemWindow(const QString& title, Window win) {
if (title.isEmpty()) return true;
// Common system window titles/classes to skip
QStringList systemTitles = {
"Desktop", "Xfdesktop", "xfdesktop", "gnome-shell", "plasmashell",
"kicker", "panel", "tray", "dock", "launcher", "notify-osd",
"xfce4-panel", "plasma-desktop", "cairo-dock"
};
for (const QString& systemTitle : systemTitles) {
if (title.contains(systemTitle, Qt::CaseInsensitive)) {
return true;
}
}
// Check window class
XClassHint classHint;
if (XGetClassHint(xdisplay, win, &classHint)) {
QString resName = classHint.res_name ? QString::fromLocal8Bit(classHint.res_name) : "";
QString resClass = classHint.res_class ? QString::fromLocal8Bit(classHint.res_class) : "";
QStringList systemClasses = {
"xfdesktop", "gnome-shell", "plasmashell", "kicker", "xfce4-panel",
"plasma-desktop", "cairo-dock", "docky", "avant-window-navigator"
};
for (const QString& systemClass : systemClasses) {
if (resName.contains(systemClass, Qt::CaseInsensitive) ||
resClass.contains(systemClass, Qt::CaseInsensitive)) {
XFree(classHint.res_name);
XFree(classHint.res_class);
return true;
}
}
XFree(classHint.res_name);
XFree(classHint.res_class);
}
return false;
}
void populateKeyboards() {
keyboardCombo->clear();
QDir devDir("/dev/input/by-id");
QFileInfoList devices = devDir.entryInfoList(QDir::Files);
foreach (const QFileInfo &device, devices) {
if (device.fileName().contains("kbd") || device.fileName().contains("keyboard")) {
keyboardCombo->addItem(device.fileName());
}
}
if (keyboardCombo->count() == 0) {
keyboardCombo->addItem("No keyboard devices found");
}
}
QComboBox *windowCombo;
QComboBox *keyboardCombo;
QLabel *statusLabel;
Display* xdisplay = nullptr;
};
class ControlPanel : public QMainWindow {
Q_OBJECT
public:
ControlPanel(QWidget *parent = nullptr) : QMainWindow(parent) {
SetupDialog setupDialog;
if (setupDialog.exec() != QDialog::Accepted) {
QApplication::quit();
return;
}
selectedWindow = setupDialog.selectedWindow();
selectedKeyboard = setupDialog.selectedKeyboard();
selectedWindowId = setupDialog.selectedWindowId();
// Open X11 display for window management
xdisplay = XOpenDisplay(NULL);
if (!xdisplay) {
QMessageBox::critical(this, "Error", "Failed to open X11 display");
QApplication::quit();
return;
}
// Open keyboard device
keyboardFd = openKeyboardDevice(selectedKeyboard);
if (keyboardFd == -1) {
QMessageBox::critical(this, "Error",
"Failed to open keyboard device: " + selectedKeyboard +
"\n\nYou may need to run this program with sudo or add yourself to the input group:\n"
"sudo usermod -a -G input $USER");
QApplication::quit();
return;
}
// Start mouse monitoring
mouseMonitor = new MouseMonitor(this);
setupUI();
loadSettings();
// Set window to always be on top of all other windows
setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
setFixedSize(220, 180);
// Ensure the window is visible and raised to top
raise();
activateWindow();
}
~ControlPanel() {
saveSettings();
if (keyboardFd != -1) {
::close(keyboardFd);
}
if (xdisplay) {
XCloseDisplay(xdisplay);
}
}
void showEvent(QShowEvent *event) override {
QMainWindow::showEvent(event);
// Ensure window stays on top when shown
raise();
activateWindow();
}
void turnOffNonLockedModifiers() {
if (toggle1State && !toggle1Locked) {
sendKeyEvent(KEY_LEFTSHIFT, 0);
toggle1State = false;
updateButtonAppearance(btnToggle1, false, false);
}
if (toggle2State && !toggle2Locked) {
sendKeyEvent(KEY_LEFTCTRL, 0);
toggle2State = false;
updateButtonAppearance(btnToggle2, false, false);
}
if (toggle3State && !toggle3Locked) {
sendKeyEvent(KEY_LEFTALT, 0);
toggle3State = false;
updateButtonAppearance(btnToggle3, false, false);
}
}
private slots:
void onToggle1Clicked() {
// Toggle between off -> on -> locked -> off
if (!toggle1State && !toggle1Locked) {
// First click: turn on (green)
toggle1State = true;
toggle1Locked = false;
} else if (toggle1State && !toggle1Locked) {
// Second click: lock (red)
toggle1State = true;
toggle1Locked = true;
} else {
// Third click: turn off
toggle1State = false;
toggle1Locked = false;
}
updateButtonAppearance(btnToggle1, toggle1State, toggle1Locked);
sendKeyEvent(KEY_LEFTSHIFT, toggle1State ? 1 : 0);
}
void onToggle2Clicked() {
// Toggle between off -> on -> locked -> off
if (!toggle2State && !toggle2Locked) {
// First click: turn on (green)
toggle2State = true;
toggle2Locked = false;
} else if (toggle2State && !toggle2Locked) {
// Second click: lock (red)
toggle2State = true;
toggle2Locked = true;
} else {
// Third click: turn off
toggle2State = false;
toggle2Locked = false;
}
updateButtonAppearance(btnToggle2, toggle2State, toggle2Locked);
sendKeyEvent(KEY_LEFTCTRL, toggle2State ? 1 : 0);
}
void onToggle3Clicked() {
// Toggle between off -> on -> locked -> off
if (!toggle3State && !toggle3Locked) {
// First click: turn on (green)
toggle3State = true;
toggle3Locked = false;
} else if (toggle3State && !toggle3Locked) {
// Second click: lock (red)
toggle3State = true;
toggle3Locked = true;
} else {
// Third click: turn off
toggle3State = false;
toggle3Locked = false;
}
updateButtonAppearance(btnToggle3, toggle3State, toggle3Locked);
sendKeyEvent(KEY_LEFTALT, toggle3State ? 1 : 0);
}
void onAction4Clicked() {
QPoint originalMousePos = QCursor::pos();
// Move mouse away before focusing window
moveMouseAway();
focusSelectedWindow();
QTimer::singleShot(150, this, [this, originalMousePos]() {
sendKeyEvent(KEY_ESC, 1);
QTimer::singleShot(50, this, [this, originalMousePos]() {
sendKeyEvent(KEY_ESC, 0);
QTimer::singleShot(100, this, [this, originalMousePos]() {
sendKeyEvent(KEY_KPPLUS, 1);
QTimer::singleShot(50, this, [this, originalMousePos]() {
sendKeyEvent(KEY_KPPLUS, 0);
// Only return mouse for kp_plus
returnMouseToPosition(originalMousePos);
});
});
});
});
}
void onTabClicked() {
QPoint originalMousePos = QCursor::pos();
// Move mouse away before focusing window
moveMouseAway();
focusSelectedWindow();
QTimer::singleShot(100, this, [this]() {
// Send Tab key first
sendKeyEvent(KEY_TAB, 1);
QTimer::singleShot(50, this, [this]() {
sendKeyEvent(KEY_TAB, 0);
// Then turn off all modifiers after sending Tab
QTimer::singleShot(50, this, [this]() {
turnOffNonLockedModifiers();
});
});
});
}
void onDelClicked() {
QPoint originalMousePos = QCursor::pos();
// Move mouse away before focusing window
moveMouseAway();
focusSelectedWindow();
QTimer::singleShot(100, this, [this]() {
// Send Del key first
sendKeyEvent(KEY_DELETE, 1);
QTimer::singleShot(50, this, [this]() {
sendKeyEvent(KEY_DELETE, 0);
// Then turn off all modifiers after sending Del
QTimer::singleShot(50, this, [this]() {
turnOffNonLockedModifiers();
});
});
});
}
void onKeyClicked(int keyCode, const QString& keyName) {
QPoint originalMousePos = QCursor::pos();
// Move mouse away before focusing window
moveMouseAway();
focusSelectedWindow();
QTimer::singleShot(100, this, [this, keyCode]() {
// Send key first
sendKeyEvent(keyCode, 1);
QTimer::singleShot(50, this, [this, keyCode]() {
sendKeyEvent(keyCode, 0);
// Then turn off all modifiers after sending key
QTimer::singleShot(50, this, [this]() {
turnOffNonLockedModifiers();
});
});
});
}
void onXClicked() { onKeyClicked(KEY_X, "X"); }
void onYClicked() { onKeyClicked(KEY_Y, "Y"); }
void onZClicked() { onKeyClicked(KEY_Z, "Z"); }
void onGClicked() { onKeyClicked(KEY_G, "G"); }
void onRClicked() { onKeyClicked(KEY_R, "R"); }
void onSClicked() { onKeyClicked(KEY_S, "S"); }
void onEClicked() { onKeyClicked(KEY_E, "E"); }
void onBClicked() { onKeyClicked(KEY_B, "B"); }
void onCloseClicked() {
close();
}
void onNumpadToggleClicked() {
// Toggle numpad visibility
numpadVisible = !numpadVisible;
updateNumpadVisibility();
// Update button appearance
updateButtonAppearance(btnNumpadToggle, numpadVisible, false);
// Resize window based on numpad state
if (numpadVisible) {
setFixedSize(220 + 210, 180); // Expand width for numpad
} else {
setFixedSize(220, 180); // Collapse to original size
}
}
void onNumpadKeyClicked(int keyCode, const QString& keyName) {
QPoint originalMousePos = QCursor::pos();
// Move mouse away before focusing window
moveMouseAway();
focusSelectedWindow();
QTimer::singleShot(100, this, [this, keyCode, originalMousePos]() {
// Send key first
sendKeyEvent(keyCode, 1);
QTimer::singleShot(50, this, [this, keyCode, originalMousePos]() {
sendKeyEvent(keyCode, 0);
// Then turn off all modifiers after sending key
QTimer::singleShot(50, this, [this]() {
turnOffNonLockedModifiers();
});
// Return mouse for numpad keys
returnMouseToPosition(originalMousePos);
});
});
}
void onNum1Clicked() { onNumpadKeyClicked(KEY_KP1, "1"); }
void onNum2Clicked() { onNumpadKeyClicked(KEY_KP2, "2"); }
void onNum3Clicked() { onNumpadKeyClicked(KEY_KP3, "3"); }
void onNum4Clicked() { onNumpadKeyClicked(KEY_KP4, "4"); }
void onNum5Clicked() { onNumpadKeyClicked(KEY_KP5, "5"); }
void onNum6Clicked() { onNumpadKeyClicked(KEY_KP6, "6"); }
void onNum7Clicked() { onNumpadKeyClicked(KEY_KP7, "7"); }
void onNum8Clicked() { onNumpadKeyClicked(KEY_KP8, "8"); }
void onNum9Clicked() { onNumpadKeyClicked(KEY_KP9, "9"); }
void onNum0Clicked() { onNumpadKeyClicked(KEY_KP0, "0"); }
void onNumDivideClicked() { onNumpadKeyClicked(KEY_KPSLASH, "/"); }
void onNumMultiplyClicked() { onNumpadKeyClicked(KEY_KPASTERISK, "*"); }
void onNumMinusClicked() { onNumpadKeyClicked(KEY_KPMINUS, "-"); }
void onNumPlusClicked() { onNumpadKeyClicked(KEY_KPPLUS, "+"); }
private:
void setupUI() {
QWidget *centralWidget = new QWidget(this);
mainLayout = new QHBoxLayout(centralWidget);
mainLayout->setSpacing(2);
mainLayout->setContentsMargins(4, 4, 4, 4);
// Create main panel (left side)
QWidget *mainPanel = new QWidget();
QGridLayout *gridLayout = new QGridLayout(mainPanel);
gridLayout->setSpacing(2);
gridLayout->setContentsMargins(0, 0, 0, 0);
// First row: Shift, Ctrl, Alt, Close
btnToggle1 = createLargeButton("Shift", 36);
btnToggle2 = createLargeButton("Ctrl", 36);
btnToggle3 = createLargeButton("Alt", 36);
QPushButton *closeButton = createLargeButton("×", 36);
closeButton->setStyleSheet("QPushButton { background-color: #ff4444; color: white; border: 1px solid #ff6666; font-size: 16px; } QPushButton:hover { background-color: #ff6666; }");
gridLayout->addWidget(btnToggle1, 0, 0);
gridLayout->addWidget(btnToggle2, 0, 1);
gridLayout->addWidget(btnToggle3, 0, 2);
gridLayout->addWidget(closeButton, 0, 3);
// Second row: X, Y, Z, Drag handle
QPushButton *btnX = createLargeButton("X", 36);
QPushButton *btnY = createLargeButton("Y", 36);
QPushButton *btnZ = createLargeButton("Z", 36);
dragButton = createLargeButton("≡", 36);
dragButton->setStyleSheet("QPushButton { background-color: #333333; color: white; border: 1px solid #555555; font-size: 20px; } QPushButton:hover { background-color: #444444; }");
dragButton->setCursor(Qt::SizeAllCursor);
gridLayout->addWidget(btnX, 1, 0);
gridLayout->addWidget(btnY, 1, 1);
gridLayout->addWidget(btnZ, 1, 2);
gridLayout->addWidget(dragButton, 1, 3);
// Third row: G, R, S, E
QPushButton *btnG = createLargeButton("G", 36);
QPushButton *btnR = createLargeButton("R", 36);
QPushButton *btnS = createLargeButton("S", 36);
QPushButton *btnE = createLargeButton("E", 36);
gridLayout->addWidget(btnG, 2, 0);
gridLayout->addWidget(btnR, 2, 1);
gridLayout->addWidget(btnS, 2, 2);
gridLayout->addWidget(btnE, 2, 3);
// Fourth row: B, Tab, Del, Numpad Toggle
QPushButton *btnB = createLargeButton("B", 36);
QPushButton *btnTab = createLargeButton("Tab", 36);
QPushButton *btnDel = createLargeButton("Del", 36);
btnNumpadToggle = createLargeButton("+", 36);
btnNumpadToggle->setStyleSheet("QPushButton { background-color: #4444aa; color: white; border: 1px solid #6666cc; } QPushButton:hover { background-color: #6666cc; }");
gridLayout->addWidget(btnB, 3, 0);
gridLayout->addWidget(btnTab, 3, 1);
gridLayout->addWidget(btnDel, 3, 2);
gridLayout->addWidget(btnNumpadToggle, 3, 3);
// Create numpad panel (right side, initially hidden)
numpadPanel = new QWidget();
QGridLayout *numpadLayout = new QGridLayout(numpadPanel);
numpadLayout->setSpacing(2);
numpadLayout->setContentsMargins(0, 0, 0, 0);
// Numpad buttons
QPushButton *btnNum7 = createLargeButton("7", 36);
QPushButton *btnNum8 = createLargeButton("8", 36);
QPushButton *btnNum9 = createLargeButton("9", 36);
QPushButton *btnNumDivide = createLargeButton("/", 36);
QPushButton *btnNum4 = createLargeButton("4", 36);
QPushButton *btnNum5 = createLargeButton("5", 36);
QPushButton *btnNum6 = createLargeButton("6", 36);
QPushButton *btnNumMultiply = createLargeButton("*", 36);
QPushButton *btnNum1 = createLargeButton("1", 36);
QPushButton *btnNum2 = createLargeButton("2", 36);
QPushButton *btnNum3 = createLargeButton("3", 36);
QPushButton *btnNumMinus = createLargeButton("-", 36);
QPushButton *btnNum0 = createLargeButton("0", 36);
QPushButton *btnNumPlus = createLargeButton("+", 36);
numpadLayout->addWidget(btnNum7, 0, 0);
numpadLayout->addWidget(btnNum8, 0, 1);
numpadLayout->addWidget(btnNum9, 0, 2);
numpadLayout->addWidget(btnNumDivide, 0, 3);
numpadLayout->addWidget(btnNum4, 1, 0);
numpadLayout->addWidget(btnNum5, 1, 1);
numpadLayout->addWidget(btnNum6, 1, 2);
numpadLayout->addWidget(btnNumMultiply, 1, 3);
numpadLayout->addWidget(btnNum1, 2, 0);
numpadLayout->addWidget(btnNum2, 2, 1);
numpadLayout->addWidget(btnNum3, 2, 2);
numpadLayout->addWidget(btnNumMinus, 2, 3);
numpadLayout->addWidget(btnNum0, 3, 0, 1, 2); // Span 2 columns
numpadLayout->addWidget(btnNumPlus, 3, 2, 1, 2); // Span 2 columns
// Add panels to main layout
mainLayout->addWidget(mainPanel);
mainLayout->addWidget(numpadPanel);
setCentralWidget(centralWidget);
// Connect signals
connect(btnToggle1, &QPushButton::clicked, this, &ControlPanel::onToggle1Clicked);
connect(btnToggle2, &QPushButton::clicked, this, &ControlPanel::onToggle2Clicked);
connect(btnToggle3, &QPushButton::clicked, this, &ControlPanel::onToggle3Clicked);
connect(btnNumpadToggle, &QPushButton::clicked, this, &ControlPanel::onNumpadToggleClicked);
connect(btnTab, &QPushButton::clicked, this, &ControlPanel::onTabClicked);
connect(btnDel, &QPushButton::clicked, this, &ControlPanel::onDelClicked);
connect(btnG, &QPushButton::clicked, this, &ControlPanel::onGClicked);
connect(btnR, &QPushButton::clicked, this, &ControlPanel::onRClicked);
connect(btnS, &QPushButton::clicked, this, &ControlPanel::onSClicked);
connect(btnE, &QPushButton::clicked, this, &ControlPanel::onEClicked);
connect(btnX, &QPushButton::clicked, this, &ControlPanel::onXClicked);
connect(btnY, &QPushButton::clicked, this, &ControlPanel::onYClicked);
connect(btnZ, &QPushButton::clicked, this, &ControlPanel::onZClicked);
connect(btnB, &QPushButton::clicked, this, &ControlPanel::onBClicked);
connect(closeButton, &QPushButton::clicked, this, &ControlPanel::onCloseClicked);
// Connect numpad buttons
connect(btnNum0, &QPushButton::clicked, this, &ControlPanel::onNum0Clicked);
connect(btnNum1, &QPushButton::clicked, this, &ControlPanel::onNum1Clicked);
connect(btnNum2, &QPushButton::clicked, this, &ControlPanel::onNum2Clicked);
connect(btnNum3, &QPushButton::clicked, this, &ControlPanel::onNum3Clicked);
connect(btnNum4, &QPushButton::clicked, this, &ControlPanel::onNum4Clicked);
connect(btnNum5, &QPushButton::clicked, this, &ControlPanel::onNum5Clicked);
connect(btnNum6, &QPushButton::clicked, this, &ControlPanel::onNum6Clicked);
connect(btnNum7, &QPushButton::clicked, this, &ControlPanel::onNum7Clicked);
connect(btnNum8, &QPushButton::clicked, this, &ControlPanel::onNum8Clicked);
connect(btnNum9, &QPushButton::clicked, this, &ControlPanel::onNum9Clicked);
connect(btnNumDivide, &QPushButton::clicked, this, &ControlPanel::onNumDivideClicked);
connect(btnNumMultiply, &QPushButton::clicked, this, &ControlPanel::onNumMultiplyClicked);
connect(btnNumMinus, &QPushButton::clicked, this, &ControlPanel::onNumMinusClicked);
connect(btnNumPlus, &QPushButton::clicked, this, &ControlPanel::onNumPlusClicked);
// Install event filter to detect when mouse enters the dialog
centralWidget->installEventFilter(this);
// Initial button states
updateButtonAppearance(btnToggle1, toggle1State, toggle1Locked);
updateButtonAppearance(btnToggle2, toggle2State, toggle2Locked);
updateButtonAppearance(btnToggle3, toggle3State, toggle3Locked);
// Make only the drag button draggable
dragButton->installEventFilter(this);
// Initially hide numpad
numpadVisible = false;
updateNumpadVisibility();
}
void updateNumpadVisibility() {
numpadPanel->setVisible(numpadVisible);
if (numpadVisible) {
btnNumpadToggle->setText("−"); // Minus symbol when expanded
} else {
btnNumpadToggle->setText("+"); // Plus symbol when collapsed
}
}
QPushButton* createLargeButton(const QString &text, int size) {
QPushButton *button = new QPushButton(text, this);
button->setFixedSize(size, size);
button->setStyleSheet(R"(
QPushButton {
font-size: 10px;
font-weight: bold;
border: 1px solid #666666;
border-radius: 4px;
background-color: #444444;
color: white;
}
QPushButton:pressed {
background-color: #666666;
}
QPushButton:hover {
border: 1px solid #888888;
}
)");
return button;
}
void updateButtonAppearance(QPushButton *button, bool state, bool locked) {
QString style;
if (locked) {
// Locked state: Red appearance
style = QString(R"(
QPushButton {
font-size: 10px;
font-weight: bold;
border: 2px solid #FF0000;
border-radius: 4px;
background-color: #FF4444;
color: black;
}
QPushButton:pressed {
background-color: #FF6666;
}
QPushButton:hover {
border: 2px solid #FF3333;
}
)");
} else if (state) {
// Active state: Green appearance
style = QString(R"(
QPushButton {
font-size: 10px;
font-weight: bold;
border: 1px solid #00AA00;
border-radius: 4px;
background-color: #00FF00;
color: black;
}
QPushButton:pressed {
background-color: #00CC00;
}
QPushButton:hover {
border: 1px solid #00FF00;
}
)");
} else {
// Inactive state: Default appearance
style = QString(R"(
QPushButton {
font-size: 10px;
font-weight: bold;
border: 1px solid #666666;
border-radius: 4px;
background-color: #444444;
color: white;
}
QPushButton:pressed {
background-color: #666666;
}
QPushButton:hover {
border: 1px solid #888888;
}
)");
}
button->setStyleSheet(style);
}
void moveMouseAway() {
// Get the most frequent mouse position from the last second
QCursor::setPos(storedMousePosition);
qDebug() << "Mouse moved to most frequent position:" << storedMousePosition;
}
void returnMouseToPosition(const QPoint &position) {
QCursor::setPos(position);
qDebug() << "Mouse returned to position:" << position;
}
void focusSelectedWindow() {
if (!selectedWindowId.isEmpty() && xdisplay) {
bool ok;
Window windowId = selectedWindowId.toULong(&ok, 16);
if (ok) {
qDebug() << "Focusing Blender window:" << selectedWindowId << "(" << windowId << ")";
XRaiseWindow(xdisplay, windowId);
XSetInputFocus(xdisplay, windowId, RevertToParent, CurrentTime);
Atom netActiveWindow = XInternAtom(xdisplay, "_NET_ACTIVE_WINDOW", False);
if (netActiveWindow != None) {
XEvent event;
memset(&event, 0, sizeof(event));
event.xclient.type = ClientMessage;
event.xclient.serial = 0;
event.xclient.send_event = True;
event.xclient.display = xdisplay;
event.xclient.window = windowId;
event.xclient.message_type = netActiveWindow;
event.xclient.format = 32;
event.xclient.data.l[0] = 1;
event.xclient.data.l[1] = CurrentTime;
event.xclient.data.l[2] = 0;
event.xclient.data.l[3] = 0;
event.xclient.data.l[4] = 0;