File: link.cpp

package info (click to toggle)
mumble 1.5.735-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 90,008 kB
  • sloc: cpp: 556,921; ansic: 81,662; python: 3,606; sh: 659; makefile: 506; asm: 371; cs: 306; sql: 228; javascript: 143; perl: 80; xml: 13
file content (241 lines) | stat: -rw-r--r-- 6,175 bytes parent folder | download | duplicates (2)
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
// Copyright 2007-2023 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#include <chrono>
#include <codecvt>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <locale>
#include <string>

#include "LinkedMem.h"
#include "MumblePlugin.h"
#include "SharedMemory.h"

#define UNUSED(x) (void) x


constexpr const char *defaultName        = "Link";
constexpr const char *defaultDescription = "Reads positional data from a linked game/software";

std::string pluginName(defaultName);
std::string applicationName;
std::string pluginDescription(defaultDescription);
std::string pluginContext;
std::string pluginIdentity;

SharedMemory sharedMem;

std::uint32_t last_tick     = 0;
std::int64_t last_tick_time = 0;

/**
 * @returns Time in ms since Epoch
 */
static std::uint64_t getTimeSinceEpoch() {
	return std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1);
}

mumble_error_t mumble_init(mumble_plugin_id_t id) {
	UNUSED(id);

	if (!sharedMem.mapMemory(getLinkedMemoryName())) {
		std::cerr << "Link plugin: Failed to setup shared memory: " << sharedMem.lastError() << std::endl;

		return MUMBLE_EC_INTERNAL_ERROR;
	}

	return MUMBLE_STATUS_OK;
}

void mumble_shutdown() {
	sharedMem.close();
}

MumbleStringWrapper mumble_getName() {
	MumbleStringWrapper wrapper;
	wrapper.data           = pluginName.c_str();
	wrapper.size           = pluginName.size();
	wrapper.needsReleasing = false;

	return wrapper;
}

mumble_version_t mumble_getAPIVersion() {
	return MUMBLE_PLUGIN_API_VERSION;
}

void mumble_registerAPIFunctions(void *apiStruct) {
	UNUSED(apiStruct);
}

void mumble_releaseResource(const void *pointer) {
	// This function should never be called
	UNUSED(pointer);

	std::terminate();
}

mumble_version_t mumble_getVersion() {
	return { 1, 3, 0 };
}

MumbleStringWrapper mumble_getAuthor() {
	static const char *author = "Mumble Developers";

	MumbleStringWrapper wrapper;
	wrapper.data           = author;
	wrapper.size           = std::strlen(author);
	wrapper.needsReleasing = false;

	return wrapper;
}

MumbleStringWrapper mumble_getDescription() {
	MumbleStringWrapper wrapper;
	wrapper.data           = pluginDescription.c_str();
	wrapper.size           = pluginDescription.size();
	wrapper.needsReleasing = false;

	return wrapper;
}

uint32_t mumble_getFeatures() {
	return MUMBLE_FEATURE_POSITIONAL;
}

uint8_t mumble_initPositionalData(const char *const *programNames, const uint64_t *programPIDs, size_t programCount) {
	UNUSED(programNames);
	UNUSED(programPIDs);
	UNUSED(programCount);

	if (!sharedMem.isMemoryMapped()) {
		return MUMBLE_PDEC_ERROR_TEMP;
	}

	LinkedMem lm = sharedMem.read();

	if ((lm.uiVersion == 1) || (lm.uiVersion == 2)) {
		if (lm.uiTick != last_tick) {
			last_tick      = lm.uiTick;
			last_tick_time = getTimeSinceEpoch();

			wchar_t buff[2048];

			if (lm.name[0]) {
				wcsncpy(buff, lm.name, 256);
				buff[255]       = 0;
				applicationName = std::wstring_convert< std::codecvt_utf8< wchar_t > >().to_bytes(buff);

				// Call the plugin itself "Link (<whatever>)"
				pluginName += " (" + applicationName + ")";
			}

			if (lm.description[0]) {
				wcsncpy(buff, lm.description, 2048);
				buff[2047]        = 0;
				pluginDescription = std::wstring_convert< std::codecvt_utf8< wchar_t > >().to_bytes(buff);
			}

			return MUMBLE_PDEC_OK;
		}
	}

	return MUMBLE_PDEC_ERROR_TEMP;
}

#define SET_TO_ZERO(name) \
	name[0] = 0.0f;       \
	name[1] = 0.0f;       \
	name[2] = 0.0f
bool mumble_fetchPositionalData(float *avatarPos, float *avatarDir, float *avatarAxis, float *cameraPos,
								float *cameraDir, float *cameraAxis, const char **context, const char **identity) {
	SET_TO_ZERO(avatarPos);
	SET_TO_ZERO(avatarDir);
	SET_TO_ZERO(avatarAxis);
	SET_TO_ZERO(cameraPos);
	SET_TO_ZERO(cameraDir);
	SET_TO_ZERO(cameraAxis);

	LinkedMem lm = sharedMem.read();

	if (lm.uiTick != last_tick) {
		last_tick      = lm.uiTick;
		last_tick_time = getTimeSinceEpoch();
	} else if ((getTimeSinceEpoch() - last_tick_time) > 5000) {
		return false;
	}

	if ((lm.uiVersion != 1) && (lm.uiVersion != 2)) {
		return false;
	}

	for (int i = 0; i < 3; ++i) {
		avatarPos[i]  = lm.fAvatarPosition[i];
		avatarDir[i]  = lm.fAvatarFront[i];
		avatarAxis[i] = lm.fAvatarTop[i];
	}

	if (lm.uiVersion == 2) {
		for (int i = 0; i < 3; ++i) {
			cameraPos[i]  = lm.fCameraPosition[i];
			cameraDir[i]  = lm.fCameraFront[i];
			cameraAxis[i] = lm.fCameraTop[i];
		}

		if (lm.context_len > 255) {
			lm.context_len = 255;
		}
		lm.identity[255] = 0;

		pluginContext.assign(reinterpret_cast< const char * >(lm.context), lm.context_len);
		pluginIdentity = std::wstring_convert< std::codecvt_utf8< wchar_t > >().to_bytes(lm.identity);
	} else {
		for (int i = 0; i < 3; ++i) {
			cameraPos[i]  = lm.fAvatarPosition[i];
			cameraDir[i]  = lm.fAvatarFront[i];
			cameraAxis[i] = lm.fAvatarTop[i];
		}

		pluginContext.clear();
		pluginIdentity.clear();
	}

	*context  = pluginContext.c_str();
	*identity = pluginIdentity.c_str();

	return true;
}

#undef SET_TO_ZERO

void mumble_shutdownPositionalData() {
	if (!applicationName.empty()) {
		// We know that pluginName is in the format "Link (<whatever>)" where <whatever> is the applicationName
		pluginName.erase(pluginName.size() - applicationName.size() - 3, std::string::npos);
	} else if (applicationName.size() != std::strlen(defaultName)) {
		// This code part should actually never run, since we expect the pluginName to be modified in the described way
		// as soon as applicationName is defined.
		pluginName.clear();
		pluginName.append(defaultName);
	}

	applicationName.clear();
	pluginDescription = std::string(defaultDescription);
	pluginContext.clear();
	pluginIdentity.clear();

	sharedMem.reset();
}

MumbleStringWrapper mumble_getPositionalDataContextPrefix() {
	MumbleStringWrapper wrapper;
	wrapper.data           = applicationName.c_str();
	wrapper.size           = applicationName.size();
	wrapper.needsReleasing = false;

	return wrapper;
}