Skip to content

Commit

Permalink
ControllerInterface: don't call InvokeDevicesChangedCallbacks more th…
Browse files Browse the repository at this point in the history
…an once when refreshing
  • Loading branch information
ligfx committed Nov 10, 2017
1 parent fd7cbd6 commit 8e6677b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Expand Up @@ -47,6 +47,7 @@ void ControllerInterface::Initialize(void* const hwnd)
return;

m_hwnd = hwnd;
m_is_populating_devices = true;

#ifdef CIFACE_USE_DINPUT
// nothing needed
Expand Down Expand Up @@ -88,6 +89,8 @@ void ControllerInterface::RefreshDevices()
m_devices.clear();
}

m_is_populating_devices = true;

#ifdef CIFACE_USE_DINPUT
ciface::DInput::PopulateDevices(reinterpret_cast<HWND>(m_hwnd));
#endif
Expand All @@ -113,6 +116,9 @@ void ControllerInterface::RefreshDevices()
#ifdef CIFACE_USE_PIPES
ciface::Pipes::PopulateDevices();
#endif

m_is_populating_devices = false;
InvokeDevicesChangedCallbacks();
}

//
Expand Down Expand Up @@ -188,7 +194,9 @@ void ControllerInterface::AddDevice(std::shared_ptr<ciface::Core::Device> device
NOTICE_LOG(SERIALINTERFACE, "Added device: %s", device->GetQualifiedName().c_str());
m_devices.emplace_back(std::move(device));
}
InvokeDevicesChangedCallbacks();

if (!m_is_populating_devices)
InvokeDevicesChangedCallbacks();
}

void ControllerInterface::RemoveDevice(std::function<bool(const ciface::Core::Device*)> callback)
Expand All @@ -205,7 +213,9 @@ void ControllerInterface::RemoveDevice(std::function<bool(const ciface::Core::De
});
m_devices.erase(it, m_devices.end());
}
InvokeDevicesChangedCallbacks();

if (!m_is_populating_devices)
InvokeDevicesChangedCallbacks();
}

//
Expand Down
Expand Up @@ -4,6 +4,7 @@

#pragma once

#include <atomic>
#include <functional>
#include <memory>
#include <mutex>
Expand Down Expand Up @@ -57,6 +58,7 @@ class ControllerInterface : public ciface::Core::DeviceContainer
std::vector<std::function<void()>> m_devices_changed_callbacks;
mutable std::mutex m_callbacks_mutex;
bool m_is_init;
std::atomic<bool> m_is_populating_devices{false};
void* m_hwnd;
};

Expand Down

0 comments on commit 8e6677b

Please sign in to comment.