90 lines
2.8 KiB
C++
90 lines
2.8 KiB
C++
|
|
#include "pen_channel.h"
|
||
|
|
|
||
|
|
#include <flutter/encodable_value.h>
|
||
|
|
#include <flutter/event_channel.h>
|
||
|
|
#include <flutter/event_stream_handler_functions.h>
|
||
|
|
#include <flutter/flutter_engine.h>
|
||
|
|
#include <flutter/standard_method_codec.h>
|
||
|
|
|
||
|
|
#include <memory>
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
|
||
|
|
std::unique_ptr<flutter::EventSink<flutter::EncodableValue>> g_pen_sink;
|
||
|
|
|
||
|
|
std::unique_ptr<flutter::EventChannel<flutter::EncodableValue>> g_pen_channel;
|
||
|
|
|
||
|
|
} // namespace
|
||
|
|
|
||
|
|
void RegisterPenChannel(flutter::FlutterEngine* engine) {
|
||
|
|
g_pen_channel =
|
||
|
|
std::make_unique<flutter::EventChannel<flutter::EncodableValue>>(
|
||
|
|
engine->messenger(), "badnote/pen",
|
||
|
|
&flutter::StandardMethodCodec::GetInstance());
|
||
|
|
|
||
|
|
auto handler = std::make_unique<
|
||
|
|
flutter::StreamHandlerFunctions<flutter::EncodableValue>>(
|
||
|
|
[](const flutter::EncodableValue* arguments,
|
||
|
|
std::unique_ptr<flutter::EventSink<flutter::EncodableValue>>&&
|
||
|
|
events)
|
||
|
|
-> std::unique_ptr<
|
||
|
|
flutter::StreamHandlerError<flutter::EncodableValue>> {
|
||
|
|
g_pen_sink = std::move(events);
|
||
|
|
return nullptr;
|
||
|
|
},
|
||
|
|
[](const flutter::EncodableValue* arguments)
|
||
|
|
-> std::unique_ptr<
|
||
|
|
flutter::StreamHandlerError<flutter::EncodableValue>> {
|
||
|
|
g_pen_sink = nullptr;
|
||
|
|
return nullptr;
|
||
|
|
});
|
||
|
|
|
||
|
|
g_pen_channel->SetStreamHandler(std::move(handler));
|
||
|
|
}
|
||
|
|
|
||
|
|
void ObservePenMessage(UINT message, WPARAM wparam, LPARAM lparam) {
|
||
|
|
if (message != WM_POINTERENTER && message != WM_POINTERDOWN &&
|
||
|
|
message != WM_POINTERUPDATE && message != WM_POINTERUP) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!g_pen_sink) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
UINT32 pointerId = GET_POINTERID_WPARAM(wparam);
|
||
|
|
|
||
|
|
POINTER_INPUT_TYPE type = PT_POINTER;
|
||
|
|
if (!GetPointerType(pointerId, &type) || type != PT_PEN) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
POINTER_PEN_INFO ppi{};
|
||
|
|
if (!GetPointerPenInfo(pointerId, &ppi)) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
int flags = 0;
|
||
|
|
if (ppi.penFlags & PEN_FLAG_BARREL) flags |= 1;
|
||
|
|
if (ppi.penFlags & PEN_FLAG_INVERTED) flags |= 2;
|
||
|
|
if (ppi.penFlags & PEN_FLAG_ERASER) flags |= 4;
|
||
|
|
|
||
|
|
flutter::EncodableMap payload{
|
||
|
|
{flutter::EncodableValue("flags"), flutter::EncodableValue(flags)},
|
||
|
|
{flutter::EncodableValue("tiltX"), flutter::EncodableValue(static_cast<double>(ppi.tiltX))},
|
||
|
|
{flutter::EncodableValue("tiltY"), flutter::EncodableValue(static_cast<double>(ppi.tiltY))},
|
||
|
|
};
|
||
|
|
|
||
|
|
g_pen_sink->Success(flutter::EncodableValue(payload));
|
||
|
|
|
||
|
|
// On pointer up, send a cleared flags event to signal lift-off.
|
||
|
|
if (message == WM_POINTERUP) {
|
||
|
|
flutter::EncodableMap clear{
|
||
|
|
{flutter::EncodableValue("flags"), flutter::EncodableValue(0)},
|
||
|
|
{flutter::EncodableValue("tiltX"), flutter::EncodableValue(0.0)},
|
||
|
|
{flutter::EncodableValue("tiltY"), flutter::EncodableValue(0.0)},
|
||
|
|
};
|
||
|
|
g_pen_sink->Success(flutter::EncodableValue(clear));
|
||
|
|
}
|
||
|
|
}
|