-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathptz-controls.cpp
More file actions
1298 lines (1147 loc) · 42.6 KB
/
Copy pathptz-controls.cpp
File metadata and controls
1298 lines (1147 loc) · 42.6 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
/* Pan Tilt Zoom camera controls
*
* Copyright 2020 Grant Likely <grant.likely@secretlab.ca>
*
* SPDX-License-Identifier: GPLv2
*/
#include <obs-module.h>
#include <obs.hpp>
#include <util/config-file.h>
#include <util/platform.h>
#include <QMainWindow>
#include <QMenuBar>
#include <QVBoxLayout>
#include <QToolTip>
#include <QWindow>
#include <QResizeEvent>
#include <QDockWidget>
#include <QStylePainter>
#include <qt-wrappers.hpp>
#include "touch-control.hpp"
#include "ui_ptz-controls.h"
#include "ptz-controls.hpp"
#include "ptz-list-model.hpp"
#include "settings.hpp"
#include "ptz.h"
const char *ptz_joy_action_axis_names[PTZ_JOY_ACTION_LAST_VALUE] = {"None",
"Pan",
"Pan (Inverted)",
"Tilt",
"Tilt (Inverted)",
"Zoom",
"Zoom (Inverted)",
"Focus",
"Focus (Inverted)"};
void ptz_load_controls(void)
{
const auto main_window = static_cast<QMainWindow *>(obs_frontend_get_main_window());
obs_frontend_push_ui_translation(obs_module_get_string);
auto *ctrls = new PTZControls(main_window);
if (!obs_frontend_add_dock_by_id("ptz-dock", obs_module_text("PTZ.Dock.Name"), ctrls))
blog(LOG_ERROR, "failed to add PTZ controls dock");
obs_frontend_pop_ui_translation();
}
PTZControls *PTZControls::instance = NULL;
/**
* class buttonResizeFilter - Event filter to adjust button minimum height and resize icon
*
* This filter will update the minimumHeight property to keep a button square
* when possible.
*/
class squareResizeFilter : public QObject {
public:
squareResizeFilter(QObject *parent) : QObject(parent) {}
bool eventFilter(QObject *watched, QEvent *event) override
{
auto obj = qobject_cast<QWidget *>(watched);
if (!obj || event->type() != QEvent::Resize)
return false;
auto resEvent = static_cast<QResizeEvent *>(event);
obj->setMinimumHeight(resEvent->size().width());
return true;
}
};
void PTZControls::autoselectDevice(OBSSource scene)
{
auto active_src_cb = [](obs_source_t *, obs_source_t *child, void *data) {
auto index = static_cast<QModelIndex *>(data);
if (!index->isValid())
*index = ptzDeviceList.indexFromName(obs_source_get_name(child));
};
QModelIndex index = ptzDeviceList.indexFromName(obs_source_get_name(scene));
if (!index.isValid())
obs_source_enum_active_sources(scene, active_src_cb, &index);
if (index.isValid())
ui->cameraList->setCurrentIndex(index);
}
void PTZControls::onFrontendEvent(enum obs_frontend_event event, void *ptr)
{
PTZControls *controls = reinterpret_cast<PTZControls *>(ptr);
controls->handleFrontendEvent(event);
}
void PTZControls::handleFrontendEvent(enum obs_frontend_event event)
{
switch (event) {
case OBS_FRONTEND_EVENT_TRANSITION_STOPPED:
updateMoveControls();
break;
case OBS_FRONTEND_EVENT_SCENE_CHANGED:
if (autoselectEnabled() && !obs_frontend_preview_program_mode_active()) {
OBSSourceAutoRelease source = obs_frontend_get_current_scene();
autoselectDevice(source.Get());
}
ptzDeviceList.onSceneChanged();
updateMoveControls();
break;
case OBS_FRONTEND_EVENT_STUDIO_MODE_ENABLED:
if (autoselectEnabled()) {
OBSSourceAutoRelease source = obs_frontend_get_current_scene();
autoselectDevice(source.Get());
}
updateMoveControls();
break;
case OBS_FRONTEND_EVENT_STUDIO_MODE_DISABLED:
case OBS_FRONTEND_EVENT_PREVIEW_SCENE_CHANGED:
if (autoselectEnabled() && obs_frontend_preview_program_mode_active()) {
OBSSourceAutoRelease source = obs_frontend_get_current_preview_scene();
autoselectDevice(source.Get());
}
updateMoveControls();
break;
case OBS_FRONTEND_EVENT_EXIT:
/* OBS is shutting down. Save the configuration and remove the PTZDevice instances */
SaveConfig();
while (!hotkeys.isEmpty())
obs_hotkey_unregister(hotkeys.takeFirst());
obs_frontend_remove_event_callback(onFrontendEvent, this);
ptzDeviceList.delete_all();
break;
default:
break;
}
}
/* Helper funciton for changing currently selected OBS scene */
static void change_current_scene(int delta)
{
struct obs_frontend_source_list scenes = {0};
obs_source_t *cur = obs_frontend_preview_program_mode_active() ? obs_frontend_get_current_preview_scene()
: obs_frontend_get_current_scene();
if (!cur)
return;
obs_frontend_get_scenes(&scenes);
size_t start = delta < 0 ? -delta : 0;
size_t end = scenes.sources.num - (delta > 0 ? delta : 0);
for (size_t i = start; i < end; i++) {
if (cur == scenes.sources.array[i]) {
if (obs_frontend_preview_program_mode_active()) {
obs_frontend_set_current_preview_scene(scenes.sources.array[i + delta]);
} else {
obs_frontend_set_current_scene(scenes.sources.array[i + delta]);
}
}
}
obs_frontend_source_list_free(&scenes);
obs_source_release(cur);
}
PTZControls::PTZControls(QWidget *parent) : QFrame(parent), ui(new Ui::PTZControls)
{
instance = this;
ui->setupUi(this);
/* Compatability: Before OBS Studio 31.1.0 the theme had left and right
* margins on widgets which mess with the grid layout used by this
* plugin. If the version is earlier than 31.1.0 then apply an extra
* style sheet to fix */
if (obs_get_version() < MAKE_SEMANTIC_VERSION(31, 1, 0))
this->setStyleSheet("margin-left: 0px; margin-right: 0px");
ui->cameraList->setModel(&ptzDeviceList);
ui->cameraList->setItemDelegate(new PTZDeviceListDelegate(ui->cameraList));
connect(&ptzDeviceList, &PTZListModel::dataChanged, this, &PTZControls::updateMoveControls);
copyActionsDynamicProperties();
QItemSelectionModel *selectionModel = ui->cameraList->selectionModel();
connect(selectionModel, &QItemSelectionModel::currentChanged, this, &PTZControls::currentChanged);
connect(&accel_timer, &QTimer::timeout, this, &PTZControls::accelTimerHandler);
connect(ui->panTiltTouch, &TouchControl::positionChanged, [this](double p, double t) { setPanTilt(p, t); });
/* Right-click on the dock's Home button → "Save current position as
* Home" (only shown for protocols that override supportsSetHome()).
* Single-click still triggers GotoHome; the context menu is purely
* additive. */
ui->panTiltButton_home->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->panTiltButton_home, &QWidget::customContextMenuRequested, this,
&PTZControls::onHomeButtonContextMenu);
joystickSetup();
LoadConfig();
/* Install an event filter to keep buttons square */
auto filter = new squareResizeFilter(this);
ui->movementControlsWidget->installEventFilter(filter);
ui->pantiltStack->installEventFilter(filter);
obs_frontend_add_event_callback(onFrontendEvent, this);
hide();
/* loadHotkey helpers lifted from obs-studio/UI/window-basic-main.cpp */
auto loadHotkeyData = [&](const char *name) -> OBSData {
config_t *cfg = obs_frontend_get_profile_config();
const char *info = config_get_string(cfg, "Hotkeys", name);
if (!info)
return {};
obs_data_t *data = obs_data_create_from_json(info);
if (!data)
return {};
OBSData res = data;
obs_data_release(data);
return res;
};
auto registerHotkey = [&](const char *name, const char *description, obs_hotkey_func func,
void *hotkey_data) -> obs_hotkey_id {
obs_hotkey_id id;
id = obs_hotkey_register_frontend(name, description, func, hotkey_data);
OBSDataArrayAutoRelease array = obs_data_get_array(loadHotkeyData(name), "bindings");
obs_hotkey_load(id, array);
hotkeys << id;
return id;
};
auto cb = [](void *button_data, obs_hotkey_id, obs_hotkey_t *, bool pressed) {
auto *button = static_cast<QToolButton *>(button_data);
if (pressed)
button->pressed();
else
button->released();
};
auto prevcb = [](void *action_data, obs_hotkey_id, obs_hotkey_t *, bool pressed) {
auto list = static_cast<CircularListView *>(action_data);
if (pressed)
list->cursorUp();
};
auto nextcb = [](void *action_data, obs_hotkey_id, obs_hotkey_t *, bool pressed) {
auto list = static_cast<CircularListView *>(action_data);
if (pressed)
list->cursorDown();
};
auto autofocustogglecb = [](void *ptz_data, obs_hotkey_id, obs_hotkey_t *, bool pressed) {
PTZControls *ptzctrl = static_cast<PTZControls *>(ptz_data);
if (pressed)
ptzctrl->on_focusButton_auto_clicked(!ptzctrl->ui->focusButton_auto->isChecked());
};
auto autofocusoncb = [](void *ptz_data, obs_hotkey_id, obs_hotkey_t *, bool pressed) {
PTZControls *ptzctrl = static_cast<PTZControls *>(ptz_data);
if (pressed)
ptzctrl->on_focusButton_auto_clicked(true);
};
auto autofocusoffcb = [](void *ptz_data, obs_hotkey_id, obs_hotkey_t *, bool pressed) {
PTZControls *ptzctrl = static_cast<PTZControls *>(ptz_data);
if (pressed)
ptzctrl->on_focusButton_auto_clicked(false);
};
registerHotkey("PTZ.PanTiltUpLeft", obs_module_text("PTZ.Action.PanTiltUpLeft"), cb, ui->panTiltButton_upleft);
registerHotkey("PTZ.PanTiltLeft", obs_module_text("PTZ.Action.PanTiltLeft"), cb, ui->panTiltButton_left);
registerHotkey("PTZ.PanTiltDownLeft", obs_module_text("PTZ.Action.PanTiltDownLeft"), cb,
ui->panTiltButton_downleft);
registerHotkey("PTZ.PanTiltUpRight", obs_module_text("PTZ.Action.PanTiltUpRight"), cb,
ui->panTiltButton_upright);
registerHotkey("PTZ.PanTiltRight", obs_module_text("PTZ.Action.PanTiltRight"), cb, ui->panTiltButton_right);
registerHotkey("PTZ.PanTiltDownRight", obs_module_text("PTZ.Action.PanTiltDownRight"), cb,
ui->panTiltButton_downright);
registerHotkey("PTZ.PanTiltUp", obs_module_text("PTZ.Action.PanTiltUp"), cb, ui->panTiltButton_up);
registerHotkey("PTZ.PanTiltDown", obs_module_text("PTZ.Action.PanTiltDown"), cb, ui->panTiltButton_down);
registerHotkey("PTZ.ZoomWide", obs_module_text("PTZ.Action.ZoomWide"), cb, ui->zoomButton_wide);
registerHotkey("PTZ.ZoomTele", obs_module_text("PTZ.Action.ZoomTele"), cb, ui->zoomButton_tele);
registerHotkey("PTZ.FocusAutoToggle", obs_module_text("PTZ.Action.FocusAutoToggle"), autofocustogglecb, this);
registerHotkey("PTZ.FocusAutoOn", obs_module_text("PTZ.Action.FocusAutoOn"), autofocusoncb, this);
registerHotkey("PTZ.FocusAutoOff", obs_module_text("PTZ.Action.FocusAutoOff"), autofocusoffcb, this);
registerHotkey("PTZ.FocusNear", obs_module_text("PTZ.Action.FocusNear"), cb, ui->focusButton_far);
registerHotkey("PTZ.FocusFar", obs_module_text("PTZ.Action.FocusFar"), cb, ui->focusButton_near);
registerHotkey("PTZ.FocusOneTouch", obs_module_text("PTZ.Action.FocusOneTouch"), cb, ui->focusButton_onetouch);
registerHotkey("PTZ.SelectPrev", obs_module_text("PTZ.Action.SelectPrev"), prevcb, ui->cameraList);
registerHotkey("PTZ.SelectNext", obs_module_text("PTZ.Action.SelectNext"), nextcb, ui->cameraList);
registerHotkey(
"PTZ.ScenePrev", obs_module_text("PTZ.Action.ScenePrev"),
[](void *, obs_hotkey_id, obs_hotkey *, bool pressed) {
if (pressed)
change_current_scene(-1);
},
nullptr);
registerHotkey(
"PTZ.SceneNext", obs_module_text("PTZ.Action.SceneNext"),
[](void *, obs_hotkey_id, obs_hotkey *, bool pressed) {
if (pressed)
change_current_scene(1);
},
nullptr);
auto preset_recall_cb = [](void *ptz_data, obs_hotkey_id hotkey, obs_hotkey_t *, bool pressed) {
PTZControls *ptzctrl = static_cast<PTZControls *>(ptz_data);
auto id = ptzctrl->preset_hotkey_map[hotkey];
if (pressed)
ptzctrl->presetRecall(id);
};
auto preset_set_cb = [](void *ptz_data, obs_hotkey_id hotkey, obs_hotkey_t *, bool pressed) {
PTZControls *ptzctrl = static_cast<PTZControls *>(ptz_data);
auto id = ptzctrl->preset_hotkey_map[hotkey];
if (pressed)
ptzctrl->presetSet(id);
};
for (int i = 0; i < 16; i++) {
auto name = QString("PTZ.Recall%1").arg(i + 1);
auto description = QString(obs_module_text("PTZ.Action.Preset.RecallNum")).arg(i + 1);
auto hotkey = registerHotkey(QT_TO_UTF8(name), QT_TO_UTF8(description), preset_recall_cb, this);
preset_hotkey_map[hotkey] = i;
name = QString("PTZ.Save%1").arg(i + 1);
description = QString(obs_module_text("PTZ.Action.Preset.SaveNum")).arg(i + 1);
hotkey = registerHotkey(QT_TO_UTF8(name), QT_TO_UTF8(description), preset_set_cb, this);
preset_hotkey_map[hotkey] = i;
}
}
#if defined(ENABLE_JOYSTICK)
void PTZControls::joystickSetup()
{
auto joysticks = QJoysticks::getInstance();
joysticks->setVirtualJoystickEnabled(false);
joysticks->updateInterfaces();
connect(joysticks, &QJoysticks::axisEvent, this, &PTZControls::joystickAxisEvent);
connect(joysticks, &QJoysticks::buttonEvent, this, &PTZControls::joystickButtonEvent);
connect(joysticks, &QJoysticks::POVEvent, this, &PTZControls::joystickPOVEvent);
}
void PTZControls::setJoystickEnabled(bool enable)
{
/* Stop camera on state change */
setPanTilt(0, 0);
setZoom(0);
m_joystick_enable = enable;
}
void PTZControls::setJoystickSpeed(double speed)
{
m_joystick_speed = speed;
/* Immediatly apply the deadzone */
auto jd = QJoysticks::getInstance()->getInputDevice(m_joystick_id);
joystickAxesChanged(jd, 0b11111111);
}
void PTZControls::setJoystickDeadzone(double deadzone)
{
m_joystick_deadzone = std::clamp(deadzone, 0.0, 0.5);
/* Immediatly apply the deadzone */
auto jd = QJoysticks::getInstance()->getInputDevice(m_joystick_id);
joystickAxesChanged(jd, 0b11111111);
}
double PTZControls::readAxis(const QJoystickDevice *jd, int axis, bool invert)
{
if (axis < 0 || !jd || axis >= jd->axes.size())
return 0.0;
double v = jd->axes.at(axis) * (invert ? -1 : 1);
if (abs(v) < m_joystick_deadzone)
return 0.0;
return std::copysign((abs(v) - m_joystick_deadzone) / (1.0 - m_joystick_deadzone), v);
}
void PTZControls::joystickAxesChanged(const QJoystickDevice *jd, uint32_t updated)
{
bool isLocked = liveMoveLockActive() &&
ui->cameraList->currentIndex().data(PTZListModel::IsLockedRole).toBool();
if (isLocked || !m_joystick_enable || !jd || jd->id != m_joystick_id)
return;
int panTiltMask = (1 << joystick_pan_axis) | (1 << joystick_tilt_axis);
if (updated & panTiltMask)
setPanTilt(readAxis(jd, joystick_pan_axis, joystick_pan_invert),
-readAxis(jd, joystick_tilt_axis, joystick_tilt_invert));
if (updated & (1 << joystick_zoom_axis))
setZoom(-readAxis(jd, joystick_zoom_axis, joystick_zoom_invert));
if (updated & (1 << joystick_focus_axis))
setFocus(-readAxis(jd, joystick_focus_axis, joystick_focus_invert));
}
void PTZControls::joystickAxisEvent(const QJoystickAxisEvent evt)
{
joystickAxesChanged(evt.joystick, 1 << evt.axis);
}
void PTZControls::joystickPOVEvent(const QJoystickPOVEvent evt)
{
if (!m_joystick_enable || evt.joystick->id != m_joystick_id)
return;
switch (evt.angle) {
case 0:
ui->presetListView->cursorUp();
break;
case 180:
ui->presetListView->cursorDown();
break;
}
}
static obs_hotkey_id lookup_obs_hotkey_id(QString hotkey_name)
{
obs_hotkey_id id = OBS_INVALID_HOTKEY_ID;
auto data = std::make_tuple(hotkey_name, &id);
using data_t = decltype(data);
if (hotkey_name == "")
return OBS_INVALID_HOTKEY_ID;
obs_enum_hotkeys(
[](void *data, obs_hotkey_id id, obs_hotkey_t *key) {
data_t &d = *static_cast<data_t *>(data);
if (std::get<0>(d) == obs_hotkey_get_name(key)) {
*std::get<1>(d) = id;
return false;
}
return true;
},
&data);
return id;
}
void PTZControls::joystickButtonEvent(const QJoystickButtonEvent evt)
{
if (!m_joystick_enable || evt.joystick->id != m_joystick_id)
return;
auto hotkey_id = lookup_obs_hotkey_id(joystick_button_hotkey_mappings[evt.button]);
if (hotkey_id != OBS_INVALID_HOTKEY_ID)
obs_hotkey_trigger_routed_callback(hotkey_id, evt.pressed);
}
#endif /* ENABLE_JOYSTICK */
void PTZControls::setJoystickAxisAction(size_t axis, ptz_joy_action_id action)
{
if (joystick_axis_actions[axis] == action)
return;
joystick_axis_actions[axis] = action;
// Clear if this is an axis already used
if (joystick_pan_axis == (int)axis)
joystick_pan_axis = -1;
if (joystick_tilt_axis == (int)axis)
joystick_tilt_axis = -1;
if (joystick_zoom_axis == (int)axis)
joystick_zoom_axis = -1;
if (joystick_focus_axis == (int)axis)
joystick_focus_axis = -1;
int old_axis = -1;
if ((action == PTZ_JOY_ACTION_PAN || action == PTZ_JOY_ACTION_PAN_INVERT) && joystick_pan_axis != (int)axis) {
old_axis = joystick_pan_axis;
joystick_pan_axis = (int)axis;
joystick_pan_invert = action == PTZ_JOY_ACTION_PAN_INVERT;
} else if ((action == PTZ_JOY_ACTION_TILT || action == PTZ_JOY_ACTION_TILT_INVERT) &&
joystick_tilt_axis != (int)axis) {
old_axis = joystick_tilt_axis;
joystick_tilt_axis = (int)axis;
joystick_tilt_invert = action == PTZ_JOY_ACTION_TILT_INVERT;
} else if ((action == PTZ_JOY_ACTION_ZOOM || action == PTZ_JOY_ACTION_ZOOM_INVERT) &&
joystick_zoom_axis != (int)axis) {
old_axis = joystick_zoom_axis;
joystick_zoom_axis = (int)axis;
joystick_zoom_invert = action == PTZ_JOY_ACTION_ZOOM_INVERT;
} else if ((action == PTZ_JOY_ACTION_FOCUS || action == PTZ_JOY_ACTION_FOCUS_INVERT) &&
joystick_focus_axis != (int)axis) {
old_axis = joystick_focus_axis;
joystick_focus_axis = (int)axis;
joystick_focus_invert = action == PTZ_JOY_ACTION_FOCUS_INVERT;
}
if (old_axis != -1)
emit joystickAxisActionChanged(old_axis, PTZ_JOY_ACTION_NONE);
emit joystickAxisActionChanged(axis, action);
}
void PTZControls::setJoystickButtonHotkey(size_t button, QString hotkey_name)
{
if (joystick_button_hotkey_mappings[button] == hotkey_name)
return;
if (hotkey_name == "") {
joystick_button_hotkey_mappings.remove(button);
emit joystickButtonHotkeyChanged(button, "");
return;
}
joystick_button_hotkey_mappings[button] = hotkey_name;
emit joystickButtonHotkeyChanged(button, hotkey_name);
}
void PTZControls::copyActionsDynamicProperties()
{
// Themes need the QAction dynamic properties
for (QAction *x : ui->ptzToolbar->actions()) {
QWidget *temp = ui->ptzToolbar->widgetForAction(x);
for (QByteArray &y : x->dynamicPropertyNames()) {
temp->setProperty(y, x->property(y));
}
}
}
/*
* Save/Load configuration methods
*/
void PTZControls::SaveConfig()
{
char *file = obs_module_config_path("config.json");
if (!file)
return;
OBSDataAutoRelease savedata = obs_data_create();
obs_data_set_string(savedata, "splitter_state", ui->splitter->saveState().toBase64().constData());
obs_data_set_string(savedata, "vertsplitter_state", ui->vertsplitter->saveState().toBase64().constData());
obs_data_set_bool(savedata, "live_moves_disabled", liveMoveLockEnabled());
obs_data_set_bool(savedata, "autoselect_enabled", autoselectEnabled());
obs_data_set_bool(savedata, "speed_ramp_enabled", speedRampEnabled());
obs_data_set_bool(savedata, "onscreen_joystick_enabled", ui->pantiltStack->currentIndex() != 0);
obs_data_set_bool(savedata, "joystick_enable", m_joystick_enable);
obs_data_set_int(savedata, "joystick_id", m_joystick_id);
obs_data_set_double(savedata, "joystick_speed", m_joystick_speed);
obs_data_set_double(savedata, "joystick_deadzone", m_joystick_deadzone);
OBSDataArrayAutoRelease axis_actions = obs_data_array_create();
for (auto axis : joystick_axis_actions.keys()) {
if (joystick_axis_actions[axis] == PTZ_JOY_ACTION_NONE)
continue;
OBSDataAutoRelease d = obs_data_create();
obs_data_set_int(d, "axis", axis);
obs_data_set_int(d, "action", joystick_axis_actions[axis]);
obs_data_array_push_back(axis_actions, d);
}
obs_data_set_array(savedata, "joystick_axis_actions", axis_actions);
OBSDataArrayAutoRelease button_actions = obs_data_array_create();
for (auto button : joystick_button_hotkey_mappings.keys()) {
auto hotkey_name = joystick_button_hotkey_mappings[button];
if (hotkey_name == "")
continue;
OBSDataAutoRelease d = obs_data_create();
obs_data_set_int(d, "button", button);
obs_data_set_string(d, "hotkey", QT_TO_UTF8(hotkey_name));
obs_data_array_push_back(button_actions, d);
}
obs_data_set_array(savedata, "joystick_button_hotkeys", button_actions);
if (ui->cameraList->currentIndex().isValid())
obs_data_set_int(savedata, "current_selected",
ui->cameraList->currentIndex().data(PTZListModel::DeviceIdRole).toInt());
OBSDataArrayAutoRelease devices = obs_data_array_create();
ptzDeviceList.save(devices.Get());
obs_data_set_array(savedata, "devices", devices);
/* Save data structure to json */
if (!obs_data_save_json_pretty_safe(savedata, file, "tmp", "bak")) {
char *path = obs_module_config_path("");
if (path) {
os_mkdirs(path);
bfree(path);
}
obs_data_save_json_safe(savedata, file, "tmp", "bak");
}
bfree(file);
}
void PTZControls::LoadConfig()
{
char *file = obs_module_config_path("config.json");
OBSDataArray array;
if (!file)
return;
OBSDataAutoRelease loaddata = obs_data_create_from_json_file_safe(file, "bak");
if (!loaddata) {
/* Try loading from the old configuration path */
auto f = QString(file).replace("obs-ptz", "ptz-controls");
loaddata = obs_data_create_from_json_file_safe(QT_TO_UTF8(f), "bak");
}
bfree(file);
if (!loaddata)
return;
obs_data_set_default_int(loaddata, "current_speed", 50);
obs_data_set_default_int(loaddata, "debug_log_level", LOG_INFO);
obs_data_set_default_bool(loaddata, "live_moves_disabled", true);
obs_data_set_default_bool(loaddata, "autoselect_enabled", true);
obs_data_set_default_bool(loaddata, "speed_ramp_enabled", true);
obs_data_set_default_bool(loaddata, "onscreen_joystick_enabled", false);
obs_data_set_default_bool(loaddata, "joystick_enable", false);
obs_data_set_default_int(loaddata, "joystick_id", -1);
obs_data_set_default_double(loaddata, "joystick_speed", 1.0);
obs_data_set_default_double(loaddata, "joystick_deadzone", 0.0);
live_move_lock_enabled = obs_data_get_bool(loaddata, "live_moves_disabled");
autoselect_enabled = obs_data_get_bool(loaddata, "autoselect_enabled");
speed_ramp_enabled = obs_data_get_bool(loaddata, "speed_ramp_enabled");
ui->pantiltStack->setCurrentIndex(obs_data_get_bool(loaddata, "onscreen_joystick_enabled") ? 1 : 0);
m_joystick_enable = obs_data_get_bool(loaddata, "joystick_enable");
m_joystick_id = (int)obs_data_get_int(loaddata, "joystick_id");
m_joystick_speed = obs_data_get_double(loaddata, "joystick_speed");
m_joystick_deadzone = obs_data_get_double(loaddata, "joystick_deadzone");
OBSDataArrayAutoRelease axis_actions = obs_data_get_array(loaddata, "joystick_axis_actions");
for (size_t i = 0; i < obs_data_array_count(axis_actions); i++) {
OBSDataAutoRelease d = obs_data_array_item(axis_actions, i);
auto axis = obs_data_get_int(d, "axis");
auto action = obs_data_get_int(d, "action");
setJoystickAxisAction(axis, action);
}
OBSDataArrayAutoRelease button_actions = obs_data_get_array(loaddata, "joystick_button_hotkeys");
for (size_t i = 0; i < obs_data_array_count(button_actions); i++) {
OBSDataAutoRelease d = obs_data_array_item(button_actions, i);
auto button = obs_data_get_int(d, "button");
auto hotkey_name = obs_data_get_string(d, "hotkey");
setJoystickButtonHotkey(button, hotkey_name);
}
const char *splitterStateStr = obs_data_get_string(loaddata, "splitter_state");
if (splitterStateStr) {
QByteArray splitterState = QByteArray::fromBase64(QByteArray(splitterStateStr));
ui->splitter->restoreState(splitterState);
}
const char *vertsplitterStateStr = obs_data_get_string(loaddata, "vertsplitter_state");
if (vertsplitterStateStr) {
QByteArray splitterState = QByteArray::fromBase64(QByteArray(vertsplitterStateStr));
ui->vertsplitter->restoreState(splitterState);
}
array = obs_data_get_array(loaddata, "devices");
obs_data_array_release(array);
ptz_devices_set_config(array);
ui->cameraList->setCurrentIndex(
ptzDeviceList.indexFromDeviceId(obs_data_get_int(loaddata, "current_selected")));
}
void PTZControls::setAutoselectEnabled(bool enabled)
{
if (enabled == autoselect_enabled)
return;
autoselect_enabled = enabled;
emit autoselectEnabledChanged(enabled);
}
void PTZControls::setLiveMoveLockEnabled(bool enable)
{
if (enable == live_move_lock_enabled)
return;
live_move_lock_enabled = enable;
updateMoveControls();
emit liveMoveLockEnabledChanged(enable);
}
void PTZControls::setSpeedRampEnabled(bool enabled)
{
if (enabled == speed_ramp_enabled)
return;
speed_ramp_enabled = enabled;
emit speedRampEnabledChanged(enabled);
}
/**
* PTZControls::callCurrentDevice - Helpers for sending device commands
*
* These are helper methods for sending commands through to a PTZ
* device. The first method accepts a calldata structure with all the
* command's arguments. The variants are argument helper versions that
* handle the most common calling cases, freeing the caller from
* creating a calldata structure.
*
* The argument helpers all use a fixed-stack instance of calldata. This
* is faster because it puts all the data onto the stack and doesn't
* require calls to bzalloc()/bfree() in the calldata code.
*/
bool PTZControls::callCurrentDevice(const char *method, calldata_t *cd) const
{
return ptzDeviceList.callDevice(ui->cameraList->currentIndex(), method, cd);
}
bool PTZControls::callCurrentDevice(const char *method, const char *arg, long long val) const
{
calldata cd;
uint8_t stack[128];
calldata_init_fixed(&cd, stack, sizeof(stack));
calldata_set_int(&cd, arg, val);
return callCurrentDevice(method, &cd);
}
bool PTZControls::callCurrentDevice(const char *method, const char *arg, double val) const
{
calldata cd;
uint8_t stack[128];
calldata_init_fixed(&cd, stack, sizeof(stack));
calldata_set_float(&cd, arg, val);
return callCurrentDevice(method, &cd);
}
bool PTZControls::callCurrentDevice(const char *method, const char *arg, bool val) const
{
calldata cd;
uint8_t stack[128];
calldata_init_fixed(&cd, stack, sizeof(stack));
calldata_set_bool(&cd, arg, val);
return callCurrentDevice(method, &cd);
}
void PTZControls::accelTimerHandler()
{
calldata cd;
uint8_t stack[128];
calldata_init_fixed(&cd, stack, sizeof(stack));
if (!ui->cameraList->currentIndex().isValid()) {
accel_timer.stop();
return;
}
if (pan_accel || tilt_accel) {
pan_speed = std::clamp(pan_speed + pan_accel, -1.0, 1.0);
if (std::abs(pan_speed) == 1.0)
pan_accel = 0.0;
tilt_speed = std::clamp(tilt_speed + tilt_accel, -1.0, 1.0);
if (std::abs(tilt_speed) == 1.0)
tilt_accel = 0.0;
calldata_set_float(&cd, "pan", pan_speed);
calldata_set_float(&cd, "tilt", tilt_speed);
}
if (zoom_accel) {
zoom_speed = std::clamp(zoom_speed + zoom_accel, -1.0, 1.0);
if (std::abs(zoom_speed) == 1.0)
zoom_accel = 0.0;
calldata_set_float(&cd, "zoom", zoom_speed);
}
if (focus_accel) {
focus_speed = std::clamp(focus_speed + focus_accel, -1.0, 1.0);
if (std::abs(focus_speed) == 1.0)
focus_accel = 0.0;
calldata_set_float(&cd, "focus", focus_speed);
}
callCurrentDevice("ptz_move", &cd);
if (pan_accel == 0.0 && tilt_accel == 0.0 && zoom_accel == 0.0 && focus_accel == 0.0)
accel_timer.stop();
}
void PTZControls::setPanTilt(double pan, double tilt, double pan_accel_, double tilt_accel_)
{
pan_speed = pan;
tilt_speed = tilt;
pan_accel = pan_accel_;
tilt_accel = tilt_accel_;
pantiltingFlag = pan != 0 || tilt != 0;
if (pan_accel != 0 || tilt_accel != 0)
accel_timer.start(2000 / 20);
calldata cd;
uint8_t stack[128];
calldata_init_fixed(&cd, stack, sizeof(stack));
calldata_set_float(&cd, "pan", pan_speed);
calldata_set_float(&cd, "tilt", tilt_speed);
callCurrentDevice("ptz_move", &cd);
calldata_free(&cd);
}
void PTZControls::keypressPanTilt(double pan, double tilt)
{
auto modifiers = QGuiApplication::keyboardModifiers();
double speed = 0.5;
double ramp = 0;
if (modifiers.testFlag(Qt::ControlModifier))
speed = 1.0;
else if (modifiers.testFlag(Qt::ShiftModifier))
speed = 0.05;
else if (speedRampEnabled())
speed = ramp = 0.05;
setPanTilt(pan * speed, tilt * speed, pan * ramp, tilt * ramp);
}
/** setZoom(double speed)
*
* Direction:
* speed < 0: Zoom out (wide)
* speed = 0: Stop Zooming
* speed > 0: Zoom in (tele)
*/
void PTZControls::setZoom(double zoom)
{
auto modifiers = QGuiApplication::keyboardModifiers();
double speed = 0.5;
zoomingFlag = (zoom != 0.0);
if (modifiers.testFlag(Qt::ControlModifier))
speed = 1.0;
else if (modifiers.testFlag(Qt::ShiftModifier))
speed = 0.1;
callCurrentDevice("ptz_move", "zoom", zoom * speed);
}
void PTZControls::setFocus(double focus)
{
auto modifiers = QGuiApplication::keyboardModifiers();
double speed = 0.5;
focusingFlag = (focus != 0.0);
if (modifiers.testFlag(Qt::ControlModifier))
speed = 1.0;
else if (modifiers.testFlag(Qt::ShiftModifier))
speed = 0.1;
callCurrentDevice("ptz_move", "focus", focus * speed);
}
/* The pan/tilt buttons are a large block of simple and mostly identical code.
* Use C preprocessor macro to create all the duplicate functions */
#define button_pantilt_actions(direction, x, y) \
void PTZControls::on_panTiltButton_##direction##_pressed() \
{ \
keypressPanTilt(x, y); \
} \
void PTZControls::on_panTiltButton_##direction##_released() \
{ \
keypressPanTilt(0, 0); \
}
button_pantilt_actions(up, 0, 1);
button_pantilt_actions(upleft, -1, 1);
button_pantilt_actions(upright, 1, 1);
button_pantilt_actions(left, -1, 0);
button_pantilt_actions(right, 1, 0);
button_pantilt_actions(down, 0, -1);
button_pantilt_actions(downleft, -1, -1);
button_pantilt_actions(downright, 1, -1);
void PTZControls::on_panTiltButton_home_released()
{
callCurrentDevice("ptz_home_recall");
}
void PTZControls::onHomeButtonContextMenu(const QPoint &pos)
{
if (!ui->cameraList->currentIndex().data(PTZListModel::SupportsSetHomeRole).toBool())
return;
QMenu menu(this);
QAction *setHome = menu.addAction(obs_module_text("PTZ.Action.SetHome"));
QAction *picked = menu.exec(ui->panTiltButton_home->mapToGlobal(pos));
if (picked == setHome)
callCurrentDevice("ptz_home_save");
}
/* There are fewer buttons for zoom or focus; so don't bother with macros */
void PTZControls::on_zoomButton_tele_pressed()
{
setZoom(1);
}
void PTZControls::on_zoomButton_tele_released()
{
setZoom(0);
}
void PTZControls::on_zoomButton_wide_pressed()
{
setZoom(-1);
}
void PTZControls::on_zoomButton_wide_released()
{
setZoom(0);
}
void PTZControls::on_focusButton_auto_clicked(bool checked)
{
setAutofocusEnabled(checked);
callCurrentDevice("ptz_set", "focus_af_enabled", checked);
}
void PTZControls::on_focusButton_near_pressed()
{
setFocus(1);
}
void PTZControls::on_focusButton_near_released()
{
setFocus(0);
}
void PTZControls::on_focusButton_far_pressed()
{
setFocus(-1);
}
void PTZControls::on_focusButton_far_released()
{
setFocus(0);
}
void PTZControls::on_focusButton_onetouch_clicked()
{
callCurrentDevice("ptz_set", "focus_onetouch_trigger", true);
}
void PTZControls::setAutofocusEnabled(bool autofocus_on)
{
ui->focusButton_auto->setChecked(autofocus_on);
ui->focusButton_near->setEnabled(!autofocus_on);
ui->focusButton_far->setEnabled(!autofocus_on);
ui->focusButton_onetouch->setEnabled(!autofocus_on);
}
void PTZControls::updateMoveControls()
{
bool is_locked = liveMoveLockActive() &&
ui->cameraList->currentIndex().data(PTZListModel::IsLockedRole).toBool();
ui->movementControlsWidget->setEnabled(!is_locked);
ui->cameraList->update();
ui->presetListView->setEnabled(!is_locked);
RefreshToolBarStyling(ui->ptzToolbar);
}
void PTZControls::currentChanged(QModelIndex current, QModelIndex previous)
{
PTZDevice *ptz = ptzDeviceList.getDevice(previous);
accel_timer.stop();
if (ptz)
disconnect(ptz, nullptr, this, nullptr);
if (pantiltingFlag || zoomingFlag || focusingFlag)
ptzDeviceList.callDevice(previous, "ptz_stop");
pantiltingFlag = false;
zoomingFlag = false;
focusingFlag = false;
pan_speed = pan_accel = 0.0;
tilt_speed = tilt_accel = 0.0;
zoom_speed = zoom_accel = 0.0;
focus_speed = focus_accel = 0.0;
ptz = ptzDeviceList.getDevice(current);
if (ptz) {
ui->presetListView->setModel(ptz->presetModel());
presetUpdateActions();
auto *selectionModel = ui->presetListView->selectionModel();
if (selectionModel)
connect(selectionModel, &QItemSelectionModel::currentChanged, this,
&PTZControls::presetUpdateActions);
connect(ptz, &PTZDevice::settingsChanged, this, &PTZControls::settingsChanged);
settingsChanged();
}
updateMoveControls();
}
void PTZControls::settingsChanged()
{
auto index = ui->cameraList->currentIndex();
calldata cd = {};
calldata_set_string(&cd, "property", "focus_af_enabled");
ptzDeviceList.callDevice(index, "ptz_get", &cd);
setAutofocusEnabled(calldata_bool(&cd, "focus_af_enabled"));
calldata_free(&cd);
}
void PTZControls::presetSet(long long preset_id)
{
callCurrentDevice("ptz_preset_save", "preset_id", preset_id);
}
void PTZControls::presetRecall(long long preset_id)
{
callCurrentDevice("ptz_preset_recall", "preset_id", preset_id);
}
void PTZControls::presetReset(long long preset_id)
{
callCurrentDevice("ptz_preset_clear", "preset_id", preset_id);
}
int PTZControls::presetIndexToId(QModelIndex index)
{
if (index.isValid())
return index.data(Qt::UserRole).toInt();
return -1;
}
void PTZControls::presetUpdateActions()
{
auto index = ui->presetListView->currentIndex();
auto model = ui->presetListView->model();
int count = model ? model->rowCount() : 0;
ui->actionPresetAdd->setEnabled(model != nullptr);
ui->actionPresetRemove->setEnabled(index.isValid());
ui->actionPresetMoveUp->setEnabled(index.isValid() && count > 1 && index.row() > 0);
ui->actionPresetMoveDown->setEnabled(index.isValid() && count > 1 && index.row() < count - 1);
RefreshToolBarStyling(ui->ptzToolbar);
}