File: hotkey-helpers.cpp

package info (click to toggle)
obs-advanced-scene-switcher 1.32.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,492 kB
  • sloc: xml: 297,593; cpp: 147,875; python: 387; sh: 280; ansic: 170; makefile: 33
file content (136 lines) | stat: -rw-r--r-- 3,293 bytes parent folder | download | duplicates (3)
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
#include "hotkey-helpers.hpp"
#include "obs-module-helper.hpp"
#include "plugin-state-helpers.hpp"

namespace advss {

std::vector<std::weak_ptr<Hotkey>> Hotkey::_registeredHotkeys = {};
uint32_t Hotkey::_hotkeyCounter = 0;

static bool setup()
{
	AddLoadStep([](obs_data_t *) { Hotkey::ClearAllHotkeys(); });
	return true;
}
static bool setupDone = setup();

std::shared_ptr<Hotkey> Hotkey::GetHotkey(const std::string &description,
					  bool ignoreExistingHotkeys)
{
	// Clean up expired hotkeys
	auto it = _registeredHotkeys.begin();
	while (it != _registeredHotkeys.end()) {
		if (it->expired()) {
			it = _registeredHotkeys.erase(it);
		} else {
			it++;
		}
	}

	// Check for existing hotkey with same description
	for (const auto &h : _registeredHotkeys) {
		auto hotkey = h.lock();
		if (!hotkey) {
			continue;
		}
		if (hotkey->_description == description) {
			hotkey->_ignoreExistingHotkeys = ignoreExistingHotkeys;
			return hotkey;
		}
	}

	// Create new hotkey
	auto hotkey = std::make_shared<Hotkey>(description);
	_registeredHotkeys.emplace_back(hotkey);
	hotkey->_ignoreExistingHotkeys = ignoreExistingHotkeys;
	return hotkey;
}

Hotkey::Hotkey(const std::string &description) : _description(description)
{
	_hotkeyID = obs_hotkey_register_frontend(
		GetNameFromDescription(description).c_str(),
		_description.c_str(), Callback, this);
	_hotkeyCounter++;
}

bool Hotkey::Save(obs_data_t *obj) const
{
	obs_data_set_string(obj, "desc", _description.c_str());
	obs_data_array_t *hotkeyData = obs_hotkey_save(_hotkeyID);
	obs_data_set_array(obj, "keyBind", hotkeyData);
	obs_data_array_release(hotkeyData);
	return true;
}

bool Hotkey::Load(obs_data_t *obj)
{
	auto description = obs_data_get_string(obj, "desc");
	if (!DescriptionAvailable(description)) {
		return false;
	}
	_description = description;
	obs_data_array_t *hotkeyData = obs_data_get_array(obj, "keyBind");
	obs_hotkey_load(_hotkeyID, hotkeyData);
	obs_data_array_release(hotkeyData);
	obs_hotkey_set_description(_hotkeyID, _description.c_str());
	_ignoreExistingHotkeys = false;
	return true;
}

Hotkey::~Hotkey()
{
	obs_hotkey_unregister(_hotkeyID);
}

bool Hotkey::UpdateDescription(const std::string &descritpion)
{
	if (!DescriptionAvailable(descritpion)) {
		return false;
	}
	_description = descritpion;
	obs_hotkey_set_name(_hotkeyID,
			    GetNameFromDescription(descritpion).c_str());
	obs_hotkey_set_description(_hotkeyID, descritpion.c_str());
	return true;
}

bool Hotkey::DescriptionAvailable(const std::string &descritpion)
{
	for (const auto &hotkey : _registeredHotkeys) {
		auto h = hotkey.lock();
		if (!h) {
			continue;
		}
		if (!h->_ignoreExistingHotkeys &&
		    h->_description == descritpion) {
			return false;
		}
	}
	return true;
}

void Hotkey::Callback(void *data, obs_hotkey_id, obs_hotkey_t *, bool pressed)
{
	auto hotkey = static_cast<Hotkey *>(data);
	if (pressed) {
		hotkey->_lastPressed =
			std::chrono::high_resolution_clock::now();
	} else {
		hotkey->_lastReleased =
			std::chrono::high_resolution_clock::now();
	}
	hotkey->_pressed = pressed;
}

std::string Hotkey::GetNameFromDescription(const std::string &description)
{
	return "macro_condition_hotkey_" + description;
}

void Hotkey::ClearAllHotkeys()
{
	_registeredHotkeys.clear();
}

} // namespace advss