2016-11-05 13:13:09 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-08-25 17:48:06 -07:00
|
|
|
/* Copyright (c) 2015-2017 FIRST. All Rights Reserved. */
|
2016-11-05 13:13:09 -07:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "Notifier.h"
|
|
|
|
|
|
|
|
|
|
#include <queue>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
#include "Handle.h"
|
2016-11-18 07:49:33 -08:00
|
|
|
#include "SinkImpl.h"
|
|
|
|
|
#include "SourceImpl.h"
|
2016-11-05 13:13:09 -07:00
|
|
|
|
|
|
|
|
using namespace cs;
|
|
|
|
|
|
|
|
|
|
bool Notifier::s_destroyed = false;
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
// Vector which provides an integrated freelist for removal and reuse of
|
|
|
|
|
// individual elements.
|
|
|
|
|
template <typename T>
|
|
|
|
|
class UidVector {
|
|
|
|
|
public:
|
|
|
|
|
typedef typename std::vector<T>::size_type size_type;
|
|
|
|
|
|
|
|
|
|
size_type size() const { return m_vector.size(); }
|
|
|
|
|
T& operator[](size_type i) { return m_vector[i]; }
|
|
|
|
|
const T& operator[](size_type i) const { return m_vector[i]; }
|
|
|
|
|
|
|
|
|
|
// Add a new T to the vector. If there are elements on the freelist,
|
|
|
|
|
// reuses the last one; otherwise adds to the end of the vector.
|
|
|
|
|
// Returns the resulting element index (+1).
|
|
|
|
|
template <class... Args>
|
|
|
|
|
unsigned int emplace_back(Args&&... args) {
|
|
|
|
|
unsigned int uid;
|
|
|
|
|
if (m_free.empty()) {
|
|
|
|
|
uid = m_vector.size();
|
|
|
|
|
m_vector.emplace_back(std::forward<Args>(args)...);
|
|
|
|
|
} else {
|
|
|
|
|
uid = m_free.back();
|
|
|
|
|
m_free.pop_back();
|
|
|
|
|
m_vector[uid] = T(std::forward<Args>(args)...);
|
|
|
|
|
}
|
|
|
|
|
return uid + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Removes the identified element by replacing it with a default-constructed
|
|
|
|
|
// one. The element is added to the freelist for later reuse.
|
|
|
|
|
void erase(unsigned int uid) {
|
|
|
|
|
--uid;
|
|
|
|
|
if (uid >= m_vector.size() || !m_vector[uid]) return;
|
|
|
|
|
m_free.push_back(uid);
|
|
|
|
|
m_vector[uid] = T();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::vector<T> m_vector;
|
|
|
|
|
std::vector<unsigned int> m_free;
|
|
|
|
|
};
|
|
|
|
|
|
2017-11-14 22:07:56 -08:00
|
|
|
} // namespace
|
2016-11-05 13:13:09 -07:00
|
|
|
|
|
|
|
|
class Notifier::Thread : public wpi::SafeThread {
|
|
|
|
|
public:
|
|
|
|
|
Thread(std::function<void()> on_start, std::function<void()> on_exit)
|
|
|
|
|
: m_on_start(on_start), m_on_exit(on_exit) {}
|
|
|
|
|
|
|
|
|
|
void Main();
|
|
|
|
|
|
|
|
|
|
struct Listener {
|
|
|
|
|
Listener() = default;
|
|
|
|
|
Listener(std::function<void(const RawEvent& event)> callback_,
|
|
|
|
|
int eventMask_)
|
|
|
|
|
: callback(callback_), eventMask(eventMask_) {}
|
|
|
|
|
|
2017-08-25 17:48:06 -07:00
|
|
|
explicit operator bool() const { return static_cast<bool>(callback); }
|
2016-11-05 13:13:09 -07:00
|
|
|
|
|
|
|
|
std::string prefix;
|
|
|
|
|
std::function<void(const RawEvent& event)> callback;
|
|
|
|
|
int eventMask;
|
|
|
|
|
};
|
|
|
|
|
UidVector<Listener> m_listeners;
|
|
|
|
|
|
|
|
|
|
std::queue<RawEvent> m_notifications;
|
|
|
|
|
|
|
|
|
|
std::function<void()> m_on_start;
|
|
|
|
|
std::function<void()> m_on_exit;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Notifier::Notifier() { s_destroyed = false; }
|
|
|
|
|
|
|
|
|
|
Notifier::~Notifier() { s_destroyed = true; }
|
|
|
|
|
|
|
|
|
|
void Notifier::Start() {
|
|
|
|
|
auto thr = m_owner.GetThread();
|
|
|
|
|
if (!thr) m_owner.Start(new Thread(m_on_start, m_on_exit));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Notifier::Stop() { m_owner.Stop(); }
|
|
|
|
|
|
|
|
|
|
void Notifier::Thread::Main() {
|
|
|
|
|
if (m_on_start) m_on_start();
|
|
|
|
|
|
2017-11-13 09:51:26 -08:00
|
|
|
std::unique_lock<wpi::mutex> lock(m_mutex);
|
2016-11-05 13:13:09 -07:00
|
|
|
while (m_active) {
|
|
|
|
|
while (m_notifications.empty()) {
|
|
|
|
|
m_cond.wait(lock);
|
|
|
|
|
if (!m_active) goto done;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (!m_notifications.empty()) {
|
|
|
|
|
if (!m_active) goto done;
|
|
|
|
|
auto item = std::move(m_notifications.front());
|
|
|
|
|
m_notifications.pop();
|
|
|
|
|
|
|
|
|
|
// Use index because iterator might get invalidated.
|
2017-08-25 17:48:06 -07:00
|
|
|
for (size_t i = 0; i < m_listeners.size(); ++i) {
|
2016-11-05 13:13:09 -07:00
|
|
|
if (!m_listeners[i]) continue; // removed
|
|
|
|
|
|
|
|
|
|
// Event type must be within requested set for this listener.
|
2016-11-15 23:54:50 -08:00
|
|
|
if ((item.kind & m_listeners[i].eventMask) == 0) continue;
|
2016-11-05 13:13:09 -07:00
|
|
|
|
|
|
|
|
// make a copy of the callback so we can safely release the mutex
|
|
|
|
|
auto callback = m_listeners[i].callback;
|
|
|
|
|
|
|
|
|
|
// Don't hold mutex during callback execution!
|
|
|
|
|
lock.unlock();
|
|
|
|
|
callback(item);
|
|
|
|
|
lock.lock();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
done:
|
|
|
|
|
if (m_on_exit) m_on_exit();
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-25 17:48:06 -07:00
|
|
|
int Notifier::AddListener(std::function<void(const RawEvent& event)> callback,
|
|
|
|
|
int eventMask) {
|
2016-11-05 13:13:09 -07:00
|
|
|
Start();
|
|
|
|
|
auto thr = m_owner.GetThread();
|
|
|
|
|
return thr->m_listeners.emplace_back(callback, eventMask);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Notifier::RemoveListener(int uid) {
|
|
|
|
|
auto thr = m_owner.GetThread();
|
|
|
|
|
if (!thr) return;
|
|
|
|
|
thr->m_listeners.erase(uid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Notifier::NotifySource(llvm::StringRef name, CS_Source source,
|
2016-11-18 07:49:33 -08:00
|
|
|
CS_EventKind kind) {
|
2016-11-05 13:13:09 -07:00
|
|
|
auto thr = m_owner.GetThread();
|
|
|
|
|
if (!thr) return;
|
2016-11-18 07:49:33 -08:00
|
|
|
thr->m_notifications.emplace(name, source, static_cast<RawEvent::Kind>(kind));
|
2016-11-05 13:13:09 -07:00
|
|
|
thr->m_cond.notify_one();
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-18 07:49:33 -08:00
|
|
|
void Notifier::NotifySource(const SourceImpl& source, CS_EventKind kind) {
|
|
|
|
|
auto handleData = Sources::GetInstance().Find(source);
|
|
|
|
|
NotifySource(source.GetName(), handleData.first, kind);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Notifier::NotifySourceVideoMode(const SourceImpl& source,
|
2016-11-05 13:13:09 -07:00
|
|
|
const VideoMode& mode) {
|
|
|
|
|
auto thr = m_owner.GetThread();
|
|
|
|
|
if (!thr) return;
|
2016-11-18 07:49:33 -08:00
|
|
|
|
|
|
|
|
auto handleData = Sources::GetInstance().Find(source);
|
|
|
|
|
|
|
|
|
|
thr->m_notifications.emplace(source.GetName(), handleData.first, mode);
|
2016-11-05 13:13:09 -07:00
|
|
|
thr->m_cond.notify_one();
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-18 07:49:33 -08:00
|
|
|
void Notifier::NotifySourceProperty(const SourceImpl& source, CS_EventKind kind,
|
2017-01-02 19:43:04 -08:00
|
|
|
llvm::StringRef propertyName, int property,
|
|
|
|
|
CS_PropertyKind propertyKind, int value,
|
|
|
|
|
llvm::StringRef valueStr) {
|
2016-11-05 13:13:09 -07:00
|
|
|
auto thr = m_owner.GetThread();
|
|
|
|
|
if (!thr) return;
|
2016-11-18 07:49:33 -08:00
|
|
|
|
|
|
|
|
auto handleData = Sources::GetInstance().Find(source);
|
|
|
|
|
|
|
|
|
|
thr->m_notifications.emplace(
|
2017-01-02 19:43:04 -08:00
|
|
|
propertyName, handleData.first, static_cast<RawEvent::Kind>(kind),
|
2016-11-18 07:49:33 -08:00
|
|
|
Handle{handleData.first, property, Handle::kProperty}, propertyKind,
|
|
|
|
|
value, valueStr);
|
2016-11-05 13:13:09 -07:00
|
|
|
thr->m_cond.notify_one();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Notifier::NotifySink(llvm::StringRef name, CS_Sink sink,
|
2016-11-18 07:49:33 -08:00
|
|
|
CS_EventKind kind) {
|
2016-11-05 13:13:09 -07:00
|
|
|
auto thr = m_owner.GetThread();
|
|
|
|
|
if (!thr) return;
|
2016-11-18 07:49:33 -08:00
|
|
|
|
|
|
|
|
thr->m_notifications.emplace(name, sink, static_cast<RawEvent::Kind>(kind));
|
2016-11-05 13:13:09 -07:00
|
|
|
thr->m_cond.notify_one();
|
|
|
|
|
}
|
2016-11-18 07:49:33 -08:00
|
|
|
|
|
|
|
|
void Notifier::NotifySink(const SinkImpl& sink, CS_EventKind kind) {
|
|
|
|
|
auto handleData = Sinks::GetInstance().Find(sink);
|
|
|
|
|
NotifySink(sink.GetName(), handleData.first, kind);
|
|
|
|
|
}
|
2016-11-18 08:56:24 -08:00
|
|
|
|
|
|
|
|
void Notifier::NotifySinkSourceChanged(llvm::StringRef name, CS_Sink sink,
|
|
|
|
|
CS_Source source) {
|
|
|
|
|
auto thr = m_owner.GetThread();
|
|
|
|
|
if (!thr) return;
|
|
|
|
|
|
|
|
|
|
RawEvent event{name, sink, RawEvent::kSinkSourceChanged};
|
|
|
|
|
event.sourceHandle = source;
|
|
|
|
|
|
|
|
|
|
thr->m_notifications.emplace(std::move(event));
|
|
|
|
|
thr->m_cond.notify_one();
|
|
|
|
|
}
|
2016-11-18 12:38:54 -08:00
|
|
|
|
|
|
|
|
void Notifier::NotifyNetworkInterfacesChanged() {
|
|
|
|
|
auto thr = m_owner.GetThread();
|
|
|
|
|
if (!thr) return;
|
|
|
|
|
|
|
|
|
|
thr->m_notifications.emplace(RawEvent::kNetworkInterfacesChanged);
|
|
|
|
|
thr->m_cond.notify_one();
|
|
|
|
|
}
|