2026-06-22 02:10:05 +08:00
|
|
|
#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;
|
|
|
|
|
|
2026-06-22 22:02:17 +08:00
|
|
|
// Diagnostic counters so the Dart side can see WHAT the observer receives:
|
|
|
|
|
// - g_ptr_msgs: WM_POINTER* messages seen (is WM_POINTER reaching us at all?)
|
|
|
|
|
// - g_pen_msgs: of those, PT_PEN with a successful GetPointerPenInfo
|
|
|
|
|
// - g_mouse_msgs: legacy mouse/touch input messages (Flutter on the old path?)
|
|
|
|
|
int g_ptr_msgs = 0;
|
|
|
|
|
int g_pen_msgs = 0;
|
|
|
|
|
int g_mouse_msgs = 0;
|
|
|
|
|
int g_last_msg = 0;
|
|
|
|
|
|
2026-06-22 02:10:05 +08:00
|
|
|
} // 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 (!g_pen_sink) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-22 22:02:17 +08:00
|
|
|
const bool is_pointer =
|
|
|
|
|
message == WM_POINTERENTER || message == WM_POINTERDOWN ||
|
|
|
|
|
message == WM_POINTERUPDATE || message == WM_POINTERUP;
|
|
|
|
|
// Legacy input path: if Flutter is feeding us mouse/touch instead of pointer
|
|
|
|
|
// messages, these tell us so (so we know WM_POINTER never arrives here).
|
|
|
|
|
const bool is_legacy_input =
|
|
|
|
|
message == WM_LBUTTONDOWN || message == WM_LBUTTONUP ||
|
|
|
|
|
message == WM_MOUSEMOVE || message == WM_TOUCH;
|
2026-06-22 02:10:05 +08:00
|
|
|
|
2026-06-22 22:02:17 +08:00
|
|
|
if (!is_pointer && !is_legacy_input) {
|
2026-06-22 02:10:05 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2026-06-22 22:02:17 +08:00
|
|
|
g_last_msg = static_cast<int>(message);
|
|
|
|
|
if (is_legacy_input) {
|
|
|
|
|
++g_mouse_msgs;
|
2026-06-22 02:10:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int flags = 0;
|
2026-06-22 22:02:17 +08:00
|
|
|
double tilt_x = 0.0;
|
|
|
|
|
double tilt_y = 0.0;
|
|
|
|
|
|
|
|
|
|
if (is_pointer) {
|
|
|
|
|
++g_ptr_msgs;
|
|
|
|
|
UINT32 pointerId = GET_POINTERID_WPARAM(wparam);
|
|
|
|
|
POINTER_INPUT_TYPE type = PT_POINTER;
|
|
|
|
|
if (GetPointerType(pointerId, &type) && type == PT_PEN) {
|
|
|
|
|
POINTER_PEN_INFO ppi{};
|
|
|
|
|
if (GetPointerPenInfo(pointerId, &ppi)) {
|
|
|
|
|
++g_pen_msgs;
|
|
|
|
|
if (ppi.penFlags & PEN_FLAG_BARREL) flags |= 1;
|
|
|
|
|
if (ppi.penFlags & PEN_FLAG_INVERTED) flags |= 2;
|
|
|
|
|
if (ppi.penFlags & PEN_FLAG_ERASER) flags |= 4;
|
|
|
|
|
tilt_x = static_cast<double>(ppi.tiltX);
|
|
|
|
|
tilt_y = static_cast<double>(ppi.tiltY);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (message == WM_POINTERUP) {
|
|
|
|
|
flags = 0; // lift-off clears held flags
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-06-22 02:10:05 +08:00
|
|
|
|
2026-06-22 22:02:17 +08:00
|
|
|
// Always emit the diagnostic counters so the Dart overlay can show whether
|
|
|
|
|
// WM_POINTER / PT_PEN ever reach this observer.
|
2026-06-22 02:10:05 +08:00
|
|
|
flutter::EncodableMap payload{
|
2026-06-22 22:02:17 +08:00
|
|
|
{flutter::EncodableValue("flags"), flutter::EncodableValue(flags)},
|
|
|
|
|
{flutter::EncodableValue("tiltX"), flutter::EncodableValue(tilt_x)},
|
|
|
|
|
{flutter::EncodableValue("tiltY"), flutter::EncodableValue(tilt_y)},
|
|
|
|
|
{flutter::EncodableValue("diagPtr"), flutter::EncodableValue(g_ptr_msgs)},
|
|
|
|
|
{flutter::EncodableValue("diagPen"), flutter::EncodableValue(g_pen_msgs)},
|
|
|
|
|
{flutter::EncodableValue("diagMouse"), flutter::EncodableValue(g_mouse_msgs)},
|
|
|
|
|
{flutter::EncodableValue("diagMsg"), flutter::EncodableValue(g_last_msg)},
|
2026-06-22 02:10:05 +08:00
|
|
|
};
|
|
|
|
|
g_pen_sink->Success(flutter::EncodableValue(payload));
|
|
|
|
|
}
|