2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2016-11-05 13:13:09 -07:00
|
|
|
|
2025-11-07 19:57:55 -05:00
|
|
|
#ifndef CSCORE_NOTIFIER_HPP_
|
|
|
|
|
#define CSCORE_NOTIFIER_HPP_
|
2016-11-05 13:13:09 -07:00
|
|
|
|
|
|
|
|
#include <functional>
|
2021-01-26 23:07:16 -08:00
|
|
|
#include <utility>
|
2016-11-05 13:13:09 -07:00
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "Handle.hpp"
|
|
|
|
|
#include "wpi/cs/cscore_cpp.hpp"
|
2025-11-07 19:57:55 -05:00
|
|
|
#include "wpi/util/CallbackManager.hpp"
|
2016-11-05 13:13:09 -07:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
namespace wpi::cs {
|
2016-11-05 13:13:09 -07:00
|
|
|
|
2016-11-18 07:49:33 -08:00
|
|
|
class SinkImpl;
|
|
|
|
|
class SourceImpl;
|
|
|
|
|
|
2021-01-26 23:07:16 -08:00
|
|
|
namespace impl {
|
|
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
struct ListenerData : public wpi::util::CallbackListenerData<
|
2021-01-26 23:07:16 -08:00
|
|
|
std::function<void(const RawEvent& event)>> {
|
|
|
|
|
ListenerData() = default;
|
|
|
|
|
ListenerData(std::function<void(const RawEvent& event)> callback_,
|
|
|
|
|
int eventMask_)
|
|
|
|
|
: CallbackListenerData(std::move(callback_)), eventMask(eventMask_) {}
|
|
|
|
|
ListenerData(unsigned int pollerUid_, int eventMask_)
|
|
|
|
|
: CallbackListenerData(pollerUid_), eventMask(eventMask_) {}
|
|
|
|
|
|
|
|
|
|
int eventMask;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class NotifierThread
|
2025-11-07 20:00:05 -05:00
|
|
|
: public wpi::util::CallbackThread<NotifierThread, RawEvent, ListenerData> {
|
2021-01-26 23:07:16 -08:00
|
|
|
public:
|
2021-10-13 08:46:07 -07:00
|
|
|
NotifierThread(std::function<void()> on_start, std::function<void()> on_exit)
|
|
|
|
|
: CallbackThread(std::move(on_start), std::move(on_exit)) {}
|
|
|
|
|
|
|
|
|
|
bool Matches(const ListenerData& listener, const RawEvent& data) {
|
|
|
|
|
return (data.kind & listener.eventMask) != 0;
|
2021-01-26 23:07:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SetListener(RawEvent* data, unsigned int listener_uid) {
|
|
|
|
|
data->listener = Handle(listener_uid, Handle::kListener);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DoCallback(std::function<void(const RawEvent& event)> callback,
|
|
|
|
|
const RawEvent& data) {
|
|
|
|
|
callback(data);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace impl
|
|
|
|
|
|
2025-11-07 20:01:58 -05:00
|
|
|
class Notifier
|
|
|
|
|
: public wpi::util::CallbackManager<Notifier, impl::NotifierThread> {
|
2016-11-05 13:13:09 -07:00
|
|
|
friend class NotifierTest;
|
|
|
|
|
|
|
|
|
|
public:
|
2018-10-31 20:22:58 -07:00
|
|
|
Notifier();
|
2016-11-05 13:13:09 -07:00
|
|
|
~Notifier();
|
|
|
|
|
|
|
|
|
|
void Start();
|
|
|
|
|
|
2021-01-26 23:07:16 -08:00
|
|
|
unsigned int Add(std::function<void(const RawEvent& event)> callback,
|
|
|
|
|
int eventMask);
|
|
|
|
|
unsigned int AddPolled(unsigned int pollerUid, int eventMask);
|
2016-11-05 13:13:09 -07:00
|
|
|
|
|
|
|
|
// Notification events
|
2021-06-06 16:13:58 -07:00
|
|
|
void NotifySource(std::string_view name, CS_Source source, CS_EventKind kind);
|
2016-11-18 07:49:33 -08:00
|
|
|
void NotifySource(const SourceImpl& source, CS_EventKind kind);
|
|
|
|
|
void NotifySourceVideoMode(const SourceImpl& source, const VideoMode& mode);
|
|
|
|
|
void NotifySourceProperty(const SourceImpl& source, CS_EventKind kind,
|
2021-06-06 16:13:58 -07:00
|
|
|
std::string_view propertyName, int property,
|
2017-01-02 19:43:04 -08:00
|
|
|
CS_PropertyKind propertyKind, int value,
|
2021-06-06 16:13:58 -07:00
|
|
|
std::string_view valueStr);
|
|
|
|
|
void NotifySink(std::string_view name, CS_Sink sink, CS_EventKind kind);
|
2016-11-18 07:49:33 -08:00
|
|
|
void NotifySink(const SinkImpl& sink, CS_EventKind kind);
|
2021-06-06 16:13:58 -07:00
|
|
|
void NotifySinkSourceChanged(std::string_view name, CS_Sink sink,
|
2016-11-18 08:56:24 -08:00
|
|
|
CS_Source source);
|
2018-07-27 22:12:30 -07:00
|
|
|
void NotifySinkProperty(const SinkImpl& sink, CS_EventKind kind,
|
2021-06-06 16:13:58 -07:00
|
|
|
std::string_view propertyName, int property,
|
2018-07-27 22:12:30 -07:00
|
|
|
CS_PropertyKind propertyKind, int value,
|
2021-06-06 16:13:58 -07:00
|
|
|
std::string_view valueStr);
|
2016-11-18 12:38:54 -08:00
|
|
|
void NotifyNetworkInterfacesChanged();
|
2018-03-04 21:01:55 -08:00
|
|
|
void NotifyTelemetryUpdated();
|
2021-01-31 18:52:48 -08:00
|
|
|
void NotifyUsbCamerasChanged();
|
2016-11-05 13:13:09 -07:00
|
|
|
};
|
|
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
} // namespace wpi::cs
|
2016-11-05 13:13:09 -07:00
|
|
|
|
2025-11-07 19:57:55 -05:00
|
|
|
#endif // CSCORE_NOTIFIER_HPP_
|