diff --git a/driver_files/src/Driver/IVRDevice.hpp b/driver_files/src/Driver/IVRDevice.hpp index 79f50d32..c2b59ed5 100644 --- a/driver_files/src/Driver/IVRDevice.hpp +++ b/driver_files/src/Driver/IVRDevice.hpp @@ -50,14 +50,6 @@ namespace ExampleDriver { return out_pose; } - // Inherited via ITrackedDeviceServerDriver - virtual vr::EVRInitError Activate(uint32_t unObjectId) = 0; - virtual void Deactivate() = 0; - virtual void EnterStandby() = 0; - virtual void* GetComponent(const char* pchComponentNameAndVersion) = 0; - virtual void DebugRequest(const char* pchRequest, char* pchResponseBuffer, uint32_t unResponseBufferSize) = 0; - virtual vr::DriverPose_t GetPose() = 0; - ~IVRDevice() = default; }; }; \ No newline at end of file diff --git a/driver_files/src/Driver/IVRDriver.hpp b/driver_files/src/Driver/IVRDriver.hpp index e9ebc478..7eb3b922 100644 --- a/driver_files/src/Driver/IVRDriver.hpp +++ b/driver_files/src/Driver/IVRDriver.hpp @@ -9,7 +9,7 @@ namespace ExampleDriver { typedef std::variant SettingsValue; - class IVRDriver : protected vr::IServerTrackedDeviceProvider { + class IVRDriver : public vr::IServerTrackedDeviceProvider { public: /// diff --git a/driver_files/src/Driver/Key.cpp b/driver_files/src/Driver/Key.cpp index d2c999fa..259ee081 100644 --- a/driver_files/src/Driver/Key.cpp +++ b/driver_files/src/Driver/Key.cpp @@ -31,8 +31,8 @@ namespace Key { } #else (void)key; - return false; #endif + return false; } }; // namespace Key diff --git a/driver_files/src/Driver/TrackerDevice.cpp b/driver_files/src/Driver/TrackerDevice.cpp index 1545c441..f0c13ae2 100644 --- a/driver_files/src/Driver/TrackerDevice.cpp +++ b/driver_files/src/Driver/TrackerDevice.cpp @@ -1,8 +1,9 @@ #include "TrackerDevice.hpp" -void normalizeQuat(double pose[]) -{ +#include +static void normalizeQuat(ExampleDriver::TrackerDevice::PoseInfo& pose) +{ //normalize double mag = sqrt(pose[3] * pose[3] + pose[4] * pose[4] + @@ -15,12 +16,13 @@ void normalizeQuat(double pose[]) pose[6] /= mag; } -ExampleDriver::TrackerDevice::TrackerDevice(std::string serial, std::string role): +ExampleDriver::TrackerDevice::TrackerDevice(std::string serial, std::string role) : serial_(serial), role_(role) { - this->last_pose_ = MakeDefaultPose(); - this->isSetup = false; + const auto now = std::chrono::system_clock::now(); + _pose_timestamp = now; + last_update = now; } std::string ExampleDriver::TrackerDevice::GetSerial() @@ -30,6 +32,8 @@ std::string ExampleDriver::TrackerDevice::GetSerial() void ExampleDriver::TrackerDevice::reinit(int msaved, double mtime, double msmooth) { + std::lock_guard guard(pose_mutex); + if (msaved < 5) //prevent having too few values to calculate linear interpolation, and prevent crash on 0 msaved = 5; @@ -38,13 +42,12 @@ void ExampleDriver::TrackerDevice::reinit(int msaved, double mtime, double msmoo else if (msmooth > 0.99) msmooth = 0.99; - max_saved = msaved; - std::vector> temp(msaved, std::vector(8,-1)); - prev_positions = temp; + prev_positions.clear(); + prev_positions.resize(msaved); max_time = mtime; smoothing = msmooth; - //Log("Settings changed! " + std::to_string(msaved) + " " + std::to_string(mtime)); + Log("Settings changed! " + std::to_string(msaved) + ' ' + std::to_string(mtime) + ' ' + std::to_string(msmooth)); } void ExampleDriver::TrackerDevice::Update() @@ -52,70 +55,36 @@ void ExampleDriver::TrackerDevice::Update() if (this->device_index_ == vr::k_unTrackedDeviceIndexInvalid) return; - // Check if this device was asked to be identified - auto events = GetDriver()->GetOpenVREvents(); - for (auto event : events) { - // Note here, event.trackedDeviceIndex does not necissarily equal this->device_index_, not sure why, but the component handle will match so we can just use that instead - //if (event.trackedDeviceIndex == this->device_index_) { - if (event.eventType == vr::EVREventType::VREvent_Input_HapticVibration) { - if (event.data.hapticVibration.componentHandle == this->haptic_component_) { - this->did_vibrate_ = true; - } - } - //} - } - - // Check if we need to keep vibrating - if (this->did_vibrate_) { - this->vibrate_anim_state_ += (GetDriver()->GetLastFrameTime().count() / 1000.f); - if (this->vibrate_anim_state_ > 1.0f) { - this->did_vibrate_ = false; - this->vibrate_anim_state_ = 0.0f; - } - } - // Setup pose for this frame auto pose = this->last_pose_; // Update time delta (for working out velocity) - std::chrono::milliseconds time_since_epoch = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()); - double time_since_epoch_seconds = time_since_epoch.count() / 1000.0; - double pose_time_delta_seconds = (time_since_epoch - _pose_timestamp).count() / 1000.0; + const auto time_now = std::chrono::system_clock::now(); + const double pose_time_delta_seconds = std::chrono::duration_cast(time_now - _pose_timestamp).count(); // Update pose timestamp - - _pose_timestamp = time_since_epoch; + _pose_timestamp = time_now; // Copy the previous position data double previous_position[3] = { 0 }; std::copy(std::begin(pose.vecPosition), std::end(pose.vecPosition), std::begin(previous_position)); - double next_pose[7]; - if (get_next_pose(0, next_pose) != 0) + PoseInfo next_pose, pose_rate; + if (get_next_pose(Seconds(0), next_pose, &pose_rate) != 0) return; normalizeQuat(next_pose); - bool pose_nan = false; - for (int i = 0; i < 7; i++) - { - if (std::isnan(next_pose[i])) - pose_nan = true; + const bool pose_nan = std::any_of(next_pose.begin(), next_pose.end(), [](double d) { return std::isnan(d); }); + if (pose_nan) { + Log("Not submitting pose! NaNs were seen"); + return; } - if (smoothing == 0 || pose_nan) - { - pose.vecPosition[0] = next_pose[0]; - pose.vecPosition[1] = next_pose[1]; - pose.vecPosition[2] = next_pose[2]; - - pose.qRotation.w = next_pose[3]; - pose.qRotation.x = next_pose[4]; - pose.qRotation.y = next_pose[5]; - pose.qRotation.z = next_pose[6]; - } - else { + // Guard around uses of |smoothing| + std::lock_guard guard(pose_mutex); + pose.vecPosition[0] = next_pose[0] * (1 - smoothing) + pose.vecPosition[0] * smoothing; pose.vecPosition[1] = next_pose[1] * (1 - smoothing) + pose.vecPosition[1] * smoothing; pose.vecPosition[2] = next_pose[2] * (1 - smoothing) + pose.vecPosition[2] * smoothing; @@ -125,6 +94,7 @@ void ExampleDriver::TrackerDevice::Update() pose.qRotation.y = next_pose[5] * (1 - smoothing) + pose.qRotation.y * smoothing; pose.qRotation.z = next_pose[6] * (1 - smoothing) + pose.qRotation.z * smoothing; } + //normalize double mag = sqrt(pose.qRotation.w * pose.qRotation.w + pose.qRotation.x * pose.qRotation.x + @@ -148,6 +118,10 @@ void ExampleDriver::TrackerDevice::Update() pose.poseTimeOffset = 0; + pose.vecVelocity[0] = pose_rate[0]; + pose.vecVelocity[1] = pose_rate[1]; + pose.vecVelocity[2] = pose_rate[2]; + //pose.vecVelocity[0] = (pose.vecPosition[0] - previous_position[0]) / pose_time_delta_seconds; //pose.vecVelocity[1] = (pose.vecPosition[1] - previous_position[1]) / pose_time_delta_seconds; //pose.vecVelocity[2] = (pose.vecPosition[2] - previous_position[2]) / pose_time_delta_seconds; @@ -163,16 +137,19 @@ void ExampleDriver::TrackerDevice::Log(std::string message) vr::VRDriverLog()->Log(message_endl.c_str()); } -int ExampleDriver::TrackerDevice::get_next_pose(double time_offset, double pred[]) +int ExampleDriver::TrackerDevice::get_next_pose(Seconds time_offset, PoseInfo& next_pose, PoseInfo* pose_rate_) const { - int statuscode = 0; + std::lock_guard guard(pose_mutex); - std::chrono::milliseconds time_since_epoch = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()); - double time_since_epoch_seconds = time_since_epoch.count() / 1000.0; + int statuscode = 0; - double req_time = time_since_epoch_seconds - time_offset; + PoseInfo dummy_rate; + PoseInfo& pose_rate = pose_rate_ ? *pose_rate_ : dummy_rate; + pose_rate.fill(0); - double new_time = last_update - req_time; + const auto time_now = std::chrono::system_clock::now(); + const auto req_time = time_now - time_offset; + double new_time = std::chrono::duration_cast(last_update - req_time).count(); if (new_time < -0.2) //limit prediction to max 0.2 second into the future to prevent your feet from being yeeted into oblivion { @@ -181,100 +158,58 @@ int ExampleDriver::TrackerDevice::get_next_pose(double time_offset, double pred[ } int curr_saved = 0; - //double pred[7] = {0}; double avg_time = 0; double avg_time2 = 0; - for (int i = 0; i < max_saved; i++) + for (const PrevPose& prev_pose : prev_positions) { - if (prev_positions[i][0] < 0) + if (prev_pose.time < 0) break; curr_saved++; - avg_time += prev_positions[i][0]; - avg_time2 += (prev_positions[i][0] * prev_positions[i][0]); + avg_time += prev_pose.time; + avg_time2 += prev_pose.time * prev_pose.time; } - //Log("saved values: " + std::to_string(curr_saved)); - - //printf("curr saved %d\n", curr_saved); - if (curr_saved < 4) + if (curr_saved == 0) { - if (curr_saved > 0) - { - for (int i = 1; i < 8; i++) - { - pred[i - 1] = prev_positions[0][i]; - } - return statuscode; - } - //printf("Too few values"); - statuscode = -1; - return statuscode; - //return 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0; + return -1; } - avg_time /= curr_saved; - avg_time2 /= curr_saved; - - //printf("avg time %f\n", avg_time); - - double st = 0; - for (int j = 0; j < curr_saved; j++) + else if (curr_saved < 4) { - st += ((prev_positions[j][0] - avg_time) * (prev_positions[j][0] - avg_time)); + next_pose = prev_positions.front().pose; + return statuscode; } - st = sqrt(st * (1.0 / curr_saved)); + avg_time /= curr_saved; + avg_time2 /= curr_saved; + const double st = std::sqrt(avg_time2 - avg_time * avg_time); - for (int i = 1; i < 8; i++) + for (int i = 0; i < next_pose.size(); i++) { double avg_val = 0; - double avg_val2 = 0; double avg_tval = 0; for (int ii = 0; ii < curr_saved; ii++) { - avg_val += prev_positions[ii][i]; - avg_tval += (prev_positions[ii][0] * prev_positions[ii][i]); - avg_val2 += (prev_positions[ii][i] * prev_positions[ii][i]); + const PrevPose& prev_pose = prev_positions[ii]; + avg_val += prev_pose.pose[i]; + avg_tval += (prev_pose.time * prev_pose.pose[i]); } avg_val /= curr_saved; avg_tval /= curr_saved; - avg_val2 /= curr_saved; - - //printf("--avg: %f\n", avg_val); - - double sv = 0; - for (int j = 0; j < curr_saved; j++) - { - sv += ((prev_positions[j][i] - avg_val) * (prev_positions[j][i] - avg_val)); - } - sv = sqrt(sv * (1.0 / curr_saved)); - - //printf("----sv: %f\n", sv); - - double rxy = (avg_tval - (avg_val * avg_time)) / sqrt((avg_time2 - (avg_time * avg_time)) * (avg_val2 - (avg_val * avg_val))); - double b = rxy * (sv / st); - double a = avg_val - (b * avg_time); - - //printf("a: %f, b: %f\n", a, b); - - double y = a + b * new_time; - //Log("aha: " + std::to_string(y) + std::to_string(avg_val)); - if (abs(avg_val2 - (avg_val * avg_val)) < 0.00000001) //bloody floating point rounding errors - y = avg_val; - - pred[i - 1] = y; - //printf("<<<< %f --> %f\n",y, pred[i-1]); + const double m = (avg_tval - (avg_val * avg_time)) / (st * st); + const double y = avg_val + m * (new_time - avg_time); + next_pose[i] = y; + pose_rate[i] = -m; // -ve since |new_time| and |PrevPose::time| increase into the past } - //printf("::: %f\n", pred[0]); + return statuscode; - //return pred[0], pred[1], pred[2], pred[3], pred[4], pred[5], pred[6]; } -void ExampleDriver::TrackerDevice::save_current_pose(double a, double b, double c, double w, double x, double y, double z, double time_offset) +void ExampleDriver::TrackerDevice::save_current_pose(double a, double b, double c, double w, double x, double y, double z, Seconds time_offset) { - double next_pose[7]; + PoseInfo next_pose; int pose_valid = get_next_pose(time_offset, next_pose); double dot = x * next_pose[4] + y * next_pose[5] + z * next_pose[6] + w * next_pose[3]; @@ -287,51 +222,45 @@ void ExampleDriver::TrackerDevice::save_current_pose(double a, double b, double w = -w; } + std::lock_guard guard(pose_mutex); + if (max_time == 0) { - std::chrono::milliseconds time_since_epoch = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()); - double time_since_epoch_seconds = time_since_epoch.count() / 1000.0; - double curr_time = time_since_epoch_seconds; - this->last_update = curr_time; - prev_positions[0][0] = time_offset; - prev_positions[0][1] = a; - prev_positions[0][2] = b; - prev_positions[0][3] = c; - prev_positions[0][4] = w; - prev_positions[0][5] = x; - prev_positions[0][6] = y; - prev_positions[0][7] = z; - + this->last_update = std::chrono::system_clock::now(); + PrevPose& prev_pose = prev_positions.front(); + prev_pose.time = time_offset.count(); + prev_pose.pose[0] = a; + prev_pose.pose[1] = b; + prev_pose.pose[2] = c; + prev_pose.pose[3] = w; + prev_pose.pose[4] = x; + prev_pose.pose[5] = y; + prev_pose.pose[6] = z; return; } //update times - std::chrono::milliseconds time_since_epoch = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()); - double time_since_epoch_seconds = time_since_epoch.count() / 1000.0; - - //Log("time since epoch: " + std::to_string(time_since_epoch_seconds)); - //lock_t curr_time = clock(); - //clock_t capture_time = curr_time - (timeOffset*1000); - double curr_time = time_since_epoch_seconds; - double time_since_update = curr_time - this->last_update; - this->last_update = curr_time; + const auto time_now = std::chrono::system_clock::now(); + const double time_since_update = std::chrono::duration_cast(time_now - this->last_update).count(); + this->last_update = time_now; - for (int i = 0; i < max_saved; i++) + for (PrevPose& prev_pose : prev_positions) { - if (prev_positions[i][0] >= 0) - prev_positions[i][0] += time_since_update; - if (prev_positions[i][0] > max_time) - prev_positions[i][0] = -1; + double& prev_time = prev_pose.time; + if (prev_time >= 0) + prev_time += time_since_update; + if (prev_time > max_time) + prev_time = -1; } - double time = time_offset; + double time = time_offset.count(); // double offset = (rand() % 100) / 10000.; // time += offset; // printf("%f %f\n", time, offset); //Log("Time: " + std::to_string(time)); - double dist = sqrt(pow(next_pose[0] - a, 2) + pow(next_pose[1] - b, 2) + pow(next_pose[2] - c, 2)); + double dist = std::sqrt(std::pow(next_pose[0] - a, 2) + std::pow(next_pose[1] - b, 2) + std::pow(next_pose[2] - c, 2)); if (pose_valid == 0 && dist > 0.5) { Log("Dropped a pose! its error was " + std::to_string(dist)); @@ -339,45 +268,34 @@ void ExampleDriver::TrackerDevice::save_current_pose(double a, double b, double return; } - dist = sqrt(pow(a, 2) + pow(b, 2) + pow(c, 2)); + dist = std::sqrt(a * a + b * b + c * c); if (dist > 10) { Log("Dropped a pose! Was outside of playspace: " + std::to_string(dist)); return; } - if (time > max_time) + if (time > max_time) { + Log("Dropped a pose! It was too old"); return; + } - if (prev_positions[max_saved - 1][0] < time && prev_positions[max_saved - 1][0] >= 0) + auto first_outdated = std::find_if(prev_positions.begin(), prev_positions.end(), [time](const PrevPose& prev_pose) { return prev_pose.time < 0 || prev_pose.time > time; }); + if (first_outdated == prev_positions.end()) { + Log("Dropped a pose! All previous poses are newer"); return; + } - int i = 0; - while (prev_positions[i][0] < time&& prev_positions[i][0] >= 0) - i++; + std::rotate(first_outdated, std::prev(prev_positions.end()), prev_positions.end()); + first_outdated->time = time; + first_outdated->pose[0] = a; + first_outdated->pose[1] = b; + first_outdated->pose[2] = c; + first_outdated->pose[3] = w; + first_outdated->pose[4] = x; + first_outdated->pose[5] = y; + first_outdated->pose[6] = z; - for (int j = max_saved - 1; j > i; j--) - { - if (prev_positions[j - 1][0] >= 0) - { - for (int k = 0; k < 8; k++) - { - prev_positions[j][k] = prev_positions[j - 1][k]; - } - } - else - { - prev_positions[j][0] = -1; - } - } - prev_positions[i][0] = time; - prev_positions[i][1] = a; - prev_positions[i][2] = b; - prev_positions[i][3] = c; - prev_positions[i][4] = w; - prev_positions[i][5] = x; - prev_positions[i][6] = y; - prev_positions[i][7] = z; /* //for debugging Log("------------------------------------------------"); for (int i = 0; i < max_saved; i++) diff --git a/driver_files/src/Driver/TrackerDevice.hpp b/driver_files/src/Driver/TrackerDevice.hpp index 57406799..5ab3c647 100644 --- a/driver_files/src/Driver/TrackerDevice.hpp +++ b/driver_files/src/Driver/TrackerDevice.hpp @@ -8,14 +8,20 @@ #include #include +#include #include #include #include #include +#include namespace ExampleDriver { class TrackerDevice : public IVRDevice { public: + // [position[x, y, z], rotation[w, x, y, z]] + using PoseInfo = std::array; + + using Seconds = std::chrono::duration; TrackerDevice(std::string serial, std::string role); ~TrackerDevice() = default; @@ -23,10 +29,6 @@ namespace ExampleDriver { // Inherited via IVRDevice virtual std::string GetSerial() override; virtual void Update() override; - //virtual void UpdatePos(double a, double b, double c, double time, double smoothing); - //virtual void UpdateRot(double qw, double qx, double qy, double qz, double time, double smoothing); - virtual void save_current_pose(double a, double b, double c, double qw, double qx, double qy, double qz, double time); - virtual int get_next_pose(double req_time, double pred[]); virtual vr::TrackedDeviceIndex_t GetDeviceIndex() override; virtual DeviceType GetDeviceType() override; virtual void Log(std::string message); @@ -37,29 +39,28 @@ namespace ExampleDriver { virtual void* GetComponent(const char* pchComponentNameAndVersion) override; virtual void DebugRequest(const char* pchRequest, char* pchResponseBuffer, uint32_t unResponseBufferSize) override; virtual vr::DriverPose_t GetPose() override; - virtual void reinit(int msaved, double mtime, double msmooth); + + void reinit(int msaved, double mtime, double msmooth); + void save_current_pose(double a, double b, double c, double qw, double qx, double qy, double qz, Seconds time_offset); + int get_next_pose(Seconds time_offset, PoseInfo& next_pose, PoseInfo* pose_rate = nullptr) const; private: vr::TrackedDeviceIndex_t device_index_ = vr::k_unTrackedDeviceIndexInvalid; std::string serial_; std::string role_; - bool isSetup; - std::chrono::milliseconds _pose_timestamp; + std::chrono::system_clock::time_point _pose_timestamp; vr::DriverPose_t last_pose_ = IVRDevice::MakeDefaultPose(); - bool did_vibrate_ = false; - float vibrate_anim_state_ = 0.f; - - vr::VRInputComponentHandle_t haptic_component_ = 0; - - vr::VRInputComponentHandle_t system_click_component_ = 0; - vr::VRInputComponentHandle_t system_touch_component_ = 0; + struct PrevPose { + double time = -1; + PoseInfo pose; + }; - int max_saved = 10; - std::vector> prev_positions; // prev_positions[:][0] je time since now (koliko cajta nazaj se je naredl, torej min-->max) - double last_update = 0; + mutable std::mutex pose_mutex; + std::vector prev_positions; // koliko cajta nazaj se je naredl, torej min-->max + std::chrono::system_clock::time_point last_update; double max_time = 1; double smoothing = 0; diff --git a/driver_files/src/Driver/VRDriver.cpp b/driver_files/src/Driver/VRDriver.cpp index f4e835b8..724e69ef 100644 --- a/driver_files/src/Driver/VRDriver.cpp +++ b/driver_files/src/Driver/VRDriver.cpp @@ -50,11 +50,8 @@ void ExampleDriver::VRDriver::PipeThread() //we go and read it into our buffer if (ipcConnection.recv(buffer, sizeof(buffer))) { - //MessageBoxA(NULL, "connected2", "Example Driver", MB_OK); //convert our buffer to string - - //MessageBoxA(NULL, buffer, "Example Driver", MB_OK); - + buffer[sizeof(buffer) - 1] = '\0'; std::string rec = buffer; //Log("Received message: " + rec); @@ -118,7 +115,7 @@ void ExampleDriver::VRDriver::PipeThread() } else if (word == "addstation") { - auto addstation = std::make_shared("AprilCamera" + std::to_string(this->devices_.size())); + auto addstation = std::make_shared("AprilCamera" + std::to_string(this->stations_.size())); this->AddDevice(addstation); this->stations_.push_back(addstation); s = s + " added"; @@ -156,7 +153,7 @@ void ExampleDriver::VRDriver::PipeThread() { if(time < 0) time = -time; - this->trackers_[idx]->save_current_pose(a, b, c, qw, qx, qy, qz, time); + this->trackers_[idx]->save_current_pose(a, b, c, qw, qx, qy, qz, TrackerDevice::Seconds(time)); //this->trackers_[idx]->UpdatePos(a, b, c, time, 1-smoothing); //this->trackers_[idx]->UpdateRot(qw, qx, qy, qz, time, 1-smoothing); @@ -176,7 +173,7 @@ void ExampleDriver::VRDriver::PipeThread() double a, b, c, time, smoothing; iss >> idx; iss >> a; iss >> b; iss >> c; iss >> time; iss >> smoothing; - if (idx < this->devices_.size()) + if (idx < this->trackers_.size()) { this->trackers_[idx]->UpdatePos(a, b, c, time, smoothing); this->trackers_[idx]->Update(); @@ -194,7 +191,7 @@ void ExampleDriver::VRDriver::PipeThread() double qw, qx, qy, qz, time, smoothing; iss >> qw; iss >> qx; iss >> qy; iss >> qz; iss >> time; iss >> smoothing; - if (idx < this->devices_.size()) + if (idx < this->trackers_.size()) { this->trackers_[idx]->UpdateRot(qw, qx, qy, qz, time, smoothing); this->trackers_[idx]->Update(); @@ -233,12 +230,12 @@ void ExampleDriver::VRDriver::PipeThread() iss >> idx; iss >> time_offset; - if (idx < this->devices_.size()) + if (idx < this->trackers_.size()) { s = s + " trackerpose " + std::to_string(idx); - double pose[7]; - int statuscode = this->trackers_[idx]->get_next_pose(time_offset, pose); + TrackerDevice::PoseInfo pose; + int statuscode = this->trackers_[idx]->get_next_pose(TrackerDevice::Seconds(time_offset), pose); s = s + " " + std::to_string(pose[0]) + " " + std::to_string(pose[1]) + @@ -318,7 +315,7 @@ void ExampleDriver::VRDriver::RunFrame() this->frame_timing_avg_ = this->frame_timing_avg_ * 0.9 + ((double)this->frame_timing_.count()) * 0.1; //MessageBox(NULL, std::to_string(((double)this->frame_timing_.count()) * 0.1).c_str(), "Example Driver", MB_OK); - for (auto& device : this->trackers_) + for (auto& device : this->devices_) device->Update(); } diff --git a/driver_files/src/Native/DriverFactory.cpp b/driver_files/src/Native/DriverFactory.cpp index 02f78545..14e523d9 100644 --- a/driver_files/src/Native/DriverFactory.cpp +++ b/driver_files/src/Native/DriverFactory.cpp @@ -13,7 +13,7 @@ void* HmdDriverFactory(const char* interface_name, int* return_code) { driver = std::make_shared(); } // We always have at least 1 ref to the shared ptr in "driver" so passing out raw pointer is ok - return driver.get(); + return static_cast(driver.get()); } if (return_code) diff --git a/libraries/ipc/Ipc.cpp b/libraries/ipc/Ipc.cpp index d2e74c7a..9a95d9a5 100644 --- a/libraries/ipc/Ipc.cpp +++ b/libraries/ipc/Ipc.cpp @@ -20,7 +20,18 @@ namespace Ipc { Connection::~Connection() { - DisconnectNamedPipe(inPipe); + if (inPipe != INVALID_HANDLE_VALUE) { + DisconnectNamedPipe(inPipe); + } + } + + Connection::Connection(Connection &&other) noexcept : inPipe(INVALID_HANDLE_VALUE) { + operator=(std::move(other)); + } + + Connection& Connection::operator=(Connection &&other) noexcept { + std::swap(inPipe, other.inPipe); + return *this; } bool Connection::send(const char *buffer, size_t length) @@ -167,7 +178,18 @@ namespace Ipc { Connection::~Connection() { - ::close(connfd); + if (connfd != -1) { + ::close(connfd); + } + } + + Connection::Connection(Connection&& other) noexcept : connfd(-1) { + operator=(std::move(other)); + } + + Connection& Connection::operator=(Connection&& other) noexcept { + std::swap(connfd, other.connfd); + return *this; } bool Connection::send(const char *buf, size_t len) @@ -252,6 +274,8 @@ namespace Ipc { Connection::Connection() { } Connection::~Connection() { } + Connection::Connection(Connection&&) noexcept = default; + Connection& Connection::operator=(Connection&&) noexcept = default; bool Connection::send(const char *buf, size_t len) { return false; diff --git a/libraries/ipc/Ipc.hpp b/libraries/ipc/Ipc.hpp index ca14440a..3573bc6f 100644 --- a/libraries/ipc/Ipc.hpp +++ b/libraries/ipc/Ipc.hpp @@ -18,8 +18,8 @@ namespace Ipc { Connection& operator=(Connection const &) = delete; // Moving is allowed - Connection(Connection &&) = default; - Connection& operator=(Connection &&) = default; + Connection(Connection &&) noexcept; + Connection& operator=(Connection &&) noexcept; bool send(const char *buffer, size_t length); bool recv(char *buffer, size_t length);