diff options
author | Robert Adam <dev@robert-adam.de> | 2021-11-18 12:51:37 +0300 |
---|---|---|
committer | Robert Adam <dev@robert-adam.de> | 2021-11-18 21:25:28 +0300 |
commit | e981827de591b7f28a85f2097a8b6752fe0ca18e (patch) | |
tree | b31f6aecf3cddefa03f0fc12f1893214f4797de5 /plugins | |
parent | 658b33e4c8b3a962ef6516a38b034cb904970350 (diff) |
MAINT: Add Link plugin tester
This commit adds a small CLI program that just connects to the Link
plugin and sends random positions to it. It is meant to be used as a
test-case for the Link plugin.
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/link/CMakeLists.txt | 9 | ||||
-rw-r--r-- | plugins/link/link_tester.cpp | 112 |
2 files changed, 121 insertions, 0 deletions
diff --git a/plugins/link/CMakeLists.txt b/plugins/link/CMakeLists.txt index 146657f89..83ea8d435 100644 --- a/plugins/link/CMakeLists.txt +++ b/plugins/link/CMakeLists.txt @@ -8,9 +8,18 @@ add_library(link SHARED "SharedMemory.cpp" ) +add_executable(link_tester + "link_tester.cpp" + "SharedMemory.cpp" +) + if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") find_library(LIB_RT "rt") target_link_libraries(link PRIVATE ${LIB_RT}) + target_link_libraries(link_tester PRIVATE ${LIB_RT}) endif() target_include_directories(link PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}") +target_include_directories(link_tester PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}") + +set_target_properties(link_tester PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}") diff --git a/plugins/link/link_tester.cpp b/plugins/link/link_tester.cpp new file mode 100644 index 000000000..c8edd5ea2 --- /dev/null +++ b/plugins/link/link_tester.cpp @@ -0,0 +1,112 @@ +// Copyright 2021 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 "LinkedMem.h" +#include "SharedMemory.h" + +#ifndef _WIN32 +# include <unistd.h> +#endif + +#include <chrono> +#include <csignal> +#include <cstring> +#include <cwchar> +#include <iostream> +#include <random> +#include <thread> + +SharedMemory sharedMem; +LinkedMem *lm = nullptr; +std::random_device dev; +std::mt19937 rng(dev()); +std::uniform_real_distribution< float > generator(0, 100); + +void initMumble() { + lm = static_cast< LinkedMem * >(sharedMem.mapMemory(getLinkedMemoryName(), sizeof(LinkedMem))); +} + +void updateMumble() { + if (!lm) + return; + + if (lm->uiVersion != 2) { + wcsncpy(lm->name, L"TestLink", 256); + wcsncpy(lm->description, L"TestLink is a test of the Link plugin.", 2048); + lm->uiVersion = 2; + } + lm->uiTick++; + + // Left handed coordinate system. + // X positive towards "right". + // Y positive towards "up". + // Z positive towards "front". + // + // 1 unit = 1 meter + + // Unit vector pointing out of the avatar's eyes aka "At"-vector. + lm->fAvatarFront[0] = 0.0f; + lm->fAvatarFront[1] = 0.0f; + lm->fAvatarFront[2] = 1.0f; + + // Unit vector pointing out of the top of the avatar's head aka "Up"-vector (here Top points straight up). + lm->fAvatarTop[0] = 0.0f; + lm->fAvatarTop[1] = 1.0f; + lm->fAvatarTop[2] = 0.0f; + + // Position of the avatar (here standing slightly off the origin) + lm->fAvatarPosition[0] = generator(rng); + lm->fAvatarPosition[1] = generator(rng); + lm->fAvatarPosition[2] = generator(rng); + + // Same as avatar but for the camera. + lm->fCameraPosition[0] = 0.0f; + lm->fCameraPosition[1] = 0.0f; + lm->fCameraPosition[2] = 0.0f; + + lm->fCameraFront[0] = 0.0f; + lm->fCameraFront[1] = 0.0f; + lm->fCameraFront[2] = 1.0f; + + lm->fCameraTop[0] = 0.0f; + lm->fCameraTop[1] = 1.0f; + lm->fCameraTop[2] = 0.0f; + + // Identifier which uniquely identifies a certain player in a context (e.g. the ingame name). + wcsncpy(lm->identity, L"Unique ID", 256); + // Context should be equal for players which should be able to hear each other positional and + // differ for those who shouldn't (e.g. it could contain the server+port and team) + memcpy(lm->context, "ContextBlob\x00\x01\x02\x03\x04", 16); + lm->context_len = 16; +} + +void signalHandler(int signum) { + std::cout << "Interrupt signal (" << signum << ") received - shutting down..." << std::endl; + + lm = nullptr; + sharedMem.close(); + + std::exit(signum); +} + +int main() { + signal(SIGINT, signalHandler); + + initMumble(); + + if (!lm) { + std::cerr << "Failed to create shared memory region (" << sharedMem.lastError() << ")" << std::endl; + return 1; + } + + std::cout << "Shared memory created successfully - Now starting update loop" << std::endl; + + while (true) { + std::cout << "Tick" << std::endl; + updateMumble(); + + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + } +} |