Windows UWP: Add support for emulated mouse cursor for targets such as XBOX (flutter/engine#24698)

Adds an emulated mouse cursor for devices that don't typically have a traditional mouse. Enables mouse-like user interactions by using gamepad devices.

flutter/flutter#76092
flutter/flutter#14967

Unit tests blocked by: flutter/flutter#70197 label added
This commit is contained in:
James Clarke
2021-03-31 16:25:27 -07:00
committed by GitHub
parent b59df14302
commit fb1e309778
10 changed files with 1002 additions and 174 deletions

View File

@@ -1504,6 +1504,8 @@ FILE: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/plu
FILE: ../../../flutter/shell/platform/windows/client_wrapper/plugin_registrar_windows_unittests.cc
FILE: ../../../flutter/shell/platform/windows/cursor_handler.cc
FILE: ../../../flutter/shell/platform/windows/cursor_handler.h
FILE: ../../../flutter/shell/platform/windows/display_helper_winuwp.cc
FILE: ../../../flutter/shell/platform/windows/display_helper_winuwp.h
FILE: ../../../flutter/shell/platform/windows/dpi_utils_win32.cc
FILE: ../../../flutter/shell/platform/windows/dpi_utils_win32.h
FILE: ../../../flutter/shell/platform/windows/dpi_utils_win32_unittests.cc
@@ -1529,6 +1531,10 @@ FILE: ../../../flutter/shell/platform/windows/flutter_windows_view.cc
FILE: ../../../flutter/shell/platform/windows/flutter_windows_view.h
FILE: ../../../flutter/shell/platform/windows/flutter_windows_win32.cc
FILE: ../../../flutter/shell/platform/windows/flutter_windows_winuwp.cc
FILE: ../../../flutter/shell/platform/windows/game_pad_cursor_winuwp.cc
FILE: ../../../flutter/shell/platform/windows/game_pad_cursor_winuwp.h
FILE: ../../../flutter/shell/platform/windows/game_pad_winuwp.cc
FILE: ../../../flutter/shell/platform/windows/game_pad_winuwp.h
FILE: ../../../flutter/shell/platform/windows/keyboard_handler_base.h
FILE: ../../../flutter/shell/platform/windows/keyboard_key_channel_handler.cc
FILE: ../../../flutter/shell/platform/windows/keyboard_key_channel_handler.h

View File

@@ -93,9 +93,15 @@ source_set("flutter_windows_source") {
# Target-specific sources.
if (target_os == "winuwp") {
sources += [
"display_helper_winuwp.cc",
"display_helper_winuwp.h",
"flutter_window_winuwp.cc",
"flutter_window_winuwp.h",
"flutter_windows_winuwp.cc",
"game_pad_cursor_winuwp.cc",
"game_pad_cursor_winuwp.h",
"game_pad_winuwp.cc",
"game_pad_winuwp.h",
"platform_handler_winuwp.cc",
"platform_handler_winuwp.h",
"system_utils_winuwp.cc",
@@ -146,7 +152,6 @@ source_set("flutter_windows_source") {
defines = [
"_SILENCE_CLANG_COROUTINE_MESSAGE",
"FLUTTER_ENGINE_NO_PROTOTYPES",
"USECOREWINDOW",
]
} else {
defines = [ "FLUTTER_ENGINE_NO_PROTOTYPES" ]

View File

@@ -0,0 +1,97 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/shell/platform/windows/display_helper_winuwp.h"
namespace flutter {
DisplayHelperWinUWP::DisplayHelperWinUWP() {
current_display_info_ = winrt::Windows::Graphics::Display::
DisplayInformation::GetForCurrentView();
ConfigureXboxSpecific();
}
bool DisplayHelperWinUWP::IsRunningOnLargeScreenDevice() {
return large_screen_device_;
}
float DisplayHelperWinUWP::GetRenderTargetXOffset() {
return render_target_x_offset_;
}
float DisplayHelperWinUWP::GetRenderTargetYOffset() {
return render_target_y_offset_;
}
WindowBoundsWinUWP DisplayHelperWinUWP::GetPhysicalBounds() {
return GetBounds(current_display_info_, true);
}
WindowBoundsWinUWP DisplayHelperWinUWP::GetLogicalBounds() {
return GetBounds(current_display_info_, false);
}
WindowBoundsWinUWP DisplayHelperWinUWP::GetBounds(
winrt::Windows::Graphics::Display::DisplayInformation const& disp,
bool physical) {
winrt::Windows::UI::ViewManagement::ApplicationView app_view =
winrt::Windows::UI::ViewManagement::ApplicationView::GetForCurrentView();
winrt::Windows::Foundation::Rect bounds = app_view.VisibleBounds();
if (large_screen_device_) {
return {bounds.Width + (bounds.X), bounds.Height + (bounds.Y)};
}
if (physical) {
// Return the height in physical pixels
return {bounds.Width * static_cast<float>(disp.RawPixelsPerViewPixel()),
bounds.Height * static_cast<float>(disp.RawPixelsPerViewPixel())};
}
return {bounds.Width, bounds.Height};
}
void DisplayHelperWinUWP::ConfigureXboxSpecific() {
bool running_on_xbox =
winrt::Windows::System::Profile::AnalyticsInfo::VersionInfo()
.DeviceFamily() == L"Windows.Xbox";
if (running_on_xbox) {
large_screen_device_ = running_on_xbox;
bool result =
winrt::Windows::UI::ViewManagement::ApplicationView::GetForCurrentView()
.SetDesiredBoundsMode(winrt::Windows::UI::ViewManagement::
ApplicationViewBoundsMode::UseCoreWindow);
if (!result) {
OutputDebugString(L"Couldn't set bounds mode.");
}
winrt::Windows::UI::ViewManagement::ApplicationView app_view = winrt::
Windows::UI::ViewManagement::ApplicationView::GetForCurrentView();
winrt::Windows::Foundation::Rect bounds = app_view.VisibleBounds();
// The offset /2 represents how much off-screan the CoreWindow is
// positioned.
render_target_x_offset_ = bounds.X / 2;
render_target_y_offset_ = bounds.Y / 2;
}
}
float DisplayHelperWinUWP::GetDpiScale() {
double raw_per_view = current_display_info_.RawPixelsPerViewPixel();
// TODO(clarkezone): ensure DPI handling is correct:
// because XBOX has display scaling off, logicalDpi retuns 96 which is
// incorrect check if raw_per_view is more acurate.
// Also confirm if it is necessary to use this workaround on 10X
// https://github.com/flutter/flutter/issues/70198
if (large_screen_device_) {
return 1.5;
}
return static_cast<float>(raw_per_view);
}
} // namespace flutter

View File

@@ -0,0 +1,79 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_DISPLAY_HELPER_WINUWP_H_
#define FLUTTER_SHELL_PLATFORM_WINDOWS_DISPLAY_HELPER_WINUWP_H_
#include <DispatcherQueue.h>
#include <third_party/cppwinrt/generated/winrt/Windows.Graphics.Display.h>
#include <third_party/cppwinrt/generated/winrt/Windows.System.Profile.h>
#include <third_party/cppwinrt/generated/winrt/Windows.UI.Composition.h>
#include <third_party/cppwinrt/generated/winrt/Windows.UI.Core.h>
#include <third_party/cppwinrt/generated/winrt/Windows.UI.ViewManagement.Core.h>
#include "third_party/cppwinrt/generated/winrt/Windows.System.Threading.h"
#include "flutter/shell/platform/windows/flutter_windows_view.h"
#include "flutter/shell/platform/windows/game_pad_winuwp.h"
namespace flutter {
struct WindowBoundsWinUWP {
float width;
float height;
};
// Helper that enables consumers to determine window bounds, DPI and some XBOX
// specific characteristics in the case when the current application is running
// on those devices.
class DisplayHelperWinUWP {
public:
DisplayHelperWinUWP();
// physical = true
// Returns with the physical bounds of the active window.
WindowBoundsWinUWP GetPhysicalBounds();
// Returns with the logical bounds of the active window.
WindowBoundsWinUWP GetLogicalBounds();
// Returns a bounds structure containing width and height information
// for the backing CoreWindow in either view or physical pixels depending on
// |physical|.
WindowBoundsWinUWP GetBounds(
winrt::Windows::Graphics::Display::DisplayInformation const& disp,
bool physical);
// Returns a scaling factor representing the current DPI scale applied to the
// backing CoreWindow.
float GetDpiScale();
bool IsRunningOnLargeScreenDevice();
// Returns a value representing the overscan to correct for X.
float GetRenderTargetXOffset();
// Returns a value representing the overscan to correct for Y.
float GetRenderTargetYOffset();
private:
// Test is current context is running on an xbox device and perform device
// specific initialization.
void ConfigureXboxSpecific();
// Most recent display information.
winrt::Windows::Graphics::Display::DisplayInformation current_display_info_{
nullptr};
// Is current context is executing on a large screen device.
bool large_screen_device_ = false;
// Current X overscan compensation factor.
float render_target_x_offset_ = 1.0f;
// Current Y overscan compensation factor.
float render_target_y_offset_ = 1.0f;
};
} // namespace flutter
#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_DISPLAY_HELPER_WINUWP_H_

View File

@@ -16,10 +16,8 @@ FlutterWindowWinUWP::FlutterWindowWinUWP(
window_ = cw;
SetEventHandlers();
ConfigureXboxSpecific();
current_display_info_ = winrt::Windows::Graphics::Display::
DisplayInformation::GetForCurrentView();
display_helper_ = std::make_unique<DisplayHelperWinUWP>();
}
WindowsRenderTarget FlutterWindowWinUWP::GetRenderTarget() {
@@ -31,19 +29,17 @@ WindowsRenderTarget FlutterWindowWinUWP::GetRenderTarget() {
visual_tree_root_ = compositor_.CreateContainerVisual();
target_.Root(visual_tree_root_);
cursor_visual_ = CreateCursorVisual();
render_target_ = compositor_.CreateSpriteVisual();
if (running_on_xbox_) {
render_target_.Offset(
{xbox_overscan_x_offset_, xbox_overscan_y_offset_, 1.0});
} else {
render_target_.Offset({1.0, 1.0, 1.0});
render_target_.Offset({display_helper_->GetRenderTargetXOffset(),
display_helper_->GetRenderTargetYOffset(), 1.0});
if (!display_helper_->IsRunningOnLargeScreenDevice()) {
ApplyInverseDpiScalingTransform();
}
visual_tree_root_.Children().InsertAtBottom(render_target_);
WindowBoundsWinUWP bounds = GetBounds(current_display_info_, true);
game_pad_cursor_ = std::make_unique<GamepadCursorWinUWP>(
binding_handler_delegate_, display_helper_.get(), window_, compositor_,
visual_tree_root_.Children());
WindowBoundsWinUWP bounds = display_helper_->GetPhysicalBounds();
render_target_.Size({bounds.width, bounds.height});
return WindowsRenderTarget(render_target_);
@@ -58,7 +54,7 @@ void FlutterWindowWinUWP::ApplyInverseDpiScalingTransform() {
}
PhysicalWindowBounds FlutterWindowWinUWP::GetPhysicalWindowBounds() {
WindowBoundsWinUWP bounds = GetBounds(current_display_info_, true);
WindowBoundsWinUWP bounds = display_helper_->GetPhysicalBounds();
return {static_cast<size_t>(bounds.width),
static_cast<size_t>(bounds.height)};
}
@@ -74,48 +70,6 @@ void FlutterWindowWinUWP::OnCursorRectUpdated(const Rect& rect) {
void FlutterWindowWinUWP::OnWindowResized() {}
float FlutterWindowWinUWP::GetDpiScale() {
auto disp = winrt::Windows::Graphics::Display::DisplayInformation::
GetForCurrentView();
return GetDpiScale(disp);
}
WindowBoundsWinUWP FlutterWindowWinUWP::GetBounds(
winrt::Windows::Graphics::Display::DisplayInformation const& disp,
bool physical) {
winrt::Windows::UI::ViewManagement::ApplicationView app_view =
winrt::Windows::UI::ViewManagement::ApplicationView::GetForCurrentView();
winrt::Windows::Foundation::Rect bounds = app_view.VisibleBounds();
if (running_on_xbox_) {
return {bounds.Width + (bounds.X), bounds.Height + (bounds.Y)};
}
if (physical) {
// Return the height in physical pixels
return {bounds.Width * static_cast<float>(disp.RawPixelsPerViewPixel()),
bounds.Height * static_cast<float>(disp.RawPixelsPerViewPixel())};
}
return {bounds.Width, bounds.Height};
}
float FlutterWindowWinUWP::GetDpiScale(
winrt::Windows::Graphics::Display::DisplayInformation const& disp) {
double raw_per_view = disp.RawPixelsPerViewPixel();
// TODO(clarkezone): ensure DPI handling is correct:
// because XBOX has display scaling off, logicalDpi retuns 96 which is
// incorrect check if raw_per_view is more acurate.
// Also confirm if it is necessary to use this workaround on 10X
// https://github.com/flutter/flutter/issues/70198
if (running_on_xbox_) {
return 1.5;
}
return static_cast<float>(raw_per_view);
}
FlutterWindowWinUWP::~FlutterWindowWinUWP() {}
void FlutterWindowWinUWP::SetView(WindowBindingHandlerDelegate* view) {
@@ -152,29 +106,8 @@ void FlutterWindowWinUWP::SetEventHandlers() {
display.DpiChanged({this, &FlutterWindowWinUWP::OnDpiChanged});
}
void FlutterWindowWinUWP::ConfigureXboxSpecific() {
running_on_xbox_ =
winrt::Windows::System::Profile::AnalyticsInfo::VersionInfo()
.DeviceFamily() == L"Windows.Xbox";
if (running_on_xbox_) {
bool result =
winrt::Windows::UI::ViewManagement::ApplicationView::GetForCurrentView()
.SetDesiredBoundsMode(winrt::Windows::UI::ViewManagement::
ApplicationViewBoundsMode::UseCoreWindow);
if (!result) {
OutputDebugString(L"Couldn't set bounds mode.");
}
winrt::Windows::UI::ViewManagement::ApplicationView app_view = winrt::
Windows::UI::ViewManagement::ApplicationView::GetForCurrentView();
winrt::Windows::Foundation::Rect bounds = app_view.VisibleBounds();
// the offset /2 represents how much off-screan the core window is
// positioned unclear why disabling overscan doesn't correct this
xbox_overscan_x_offset_ = bounds.X / 2;
xbox_overscan_y_offset_ = bounds.Y / 2;
}
float FlutterWindowWinUWP::GetDpiScale() {
return display_helper_->GetDpiScale();
}
void FlutterWindowWinUWP::OnDpiChanged(
@@ -182,7 +115,7 @@ void FlutterWindowWinUWP::OnDpiChanged(
winrt::Windows::Foundation::IInspectable const&) {
ApplyInverseDpiScalingTransform();
WindowBoundsWinUWP bounds = GetBounds(current_display_info_, true);
WindowBoundsWinUWP bounds = display_helper_->GetPhysicalBounds();
binding_handler_delegate_->OnWindowSizeChanged(
static_cast<size_t>(bounds.width), static_cast<size_t>(bounds.height));
@@ -230,23 +163,24 @@ double FlutterWindowWinUWP::GetPosX(
winrt::Windows::UI::Core::PointerEventArgs const& args) {
const double inverse_dpi_scale = GetDpiScale();
return (args.CurrentPoint().Position().X - xbox_overscan_x_offset_) *
return (args.CurrentPoint().Position().X -
display_helper_->GetRenderTargetXOffset()) *
inverse_dpi_scale;
}
double FlutterWindowWinUWP::GetPosY(
winrt::Windows::UI::Core::PointerEventArgs const& args) {
const double inverse_dpi_scale = GetDpiScale();
return static_cast<double>(
(args.CurrentPoint().Position().Y - xbox_overscan_y_offset_) *
inverse_dpi_scale);
return static_cast<double>((args.CurrentPoint().Position().Y -
display_helper_->GetRenderTargetYOffset()) *
inverse_dpi_scale);
}
void FlutterWindowWinUWP::OnBoundsChanged(
winrt::Windows::UI::ViewManagement::ApplicationView const& app_view,
winrt::Windows::Foundation::IInspectable const&) {
if (binding_handler_delegate_) {
auto bounds = GetBounds(current_display_info_, true);
auto bounds = display_helper_->GetPhysicalBounds();
binding_handler_delegate_->OnWindowSizeChanged(
static_cast<size_t>(bounds.width), static_cast<size_t>(bounds.height));
@@ -301,43 +235,6 @@ void FlutterWindowWinUWP::OnCharacterReceived(
}
}
winrt::Windows::UI::Composition::Visual
FlutterWindowWinUWP::CreateCursorVisual() {
auto container = compositor_.CreateContainerVisual();
container.Offset(
{window_.Bounds().Width / 2, window_.Bounds().Height / 2, 1.0});
// size of the simulated mouse cursor
const float size = 30;
auto cursor_visual = compositor_.CreateShapeVisual();
cursor_visual.Size({size, size});
// compensate for overscan in cursor visual
cursor_visual.Offset({xbox_overscan_x_offset_, xbox_overscan_y_offset_, 1.0});
winrt::Windows::UI::Composition::CompositionEllipseGeometry circle =
compositor_.CreateEllipseGeometry();
circle.Radius({size / 2, size / 2});
auto circleshape = compositor_.CreateSpriteShape(circle);
circleshape.FillBrush(
compositor_.CreateColorBrush(winrt::Windows::UI::Colors::Black()));
circleshape.Offset({size / 2, size / 2});
cursor_visual.Shapes().Append(circleshape);
winrt::Windows::UI::Composition::Visual visual =
cursor_visual.as<winrt::Windows::UI::Composition::Visual>();
visual.CompositeMode(winrt::Windows::UI::Composition::
CompositionCompositeMode::DestinationInvert);
visual.AnchorPoint({0.5, 0.5});
container.Children().InsertAtTop(visual);
return container;
}
bool FlutterWindowWinUWP::OnBitmapSurfaceUpdated(const void* allocation,
size_t row_bytes,
size_t height) {

View File

@@ -5,25 +5,19 @@
#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_UWP_FLUTTER_WINDOW_H_
#define FLUTTER_SHELL_PLATFORM_WINDOWS_UWP_FLUTTER_WINDOW_H_
#include <third_party/cppwinrt/generated/winrt/Windows.Foundation.Collections.h>
#include <third_party/cppwinrt/generated/winrt/Windows.Graphics.Display.h>
#include <third_party/cppwinrt/generated/winrt/Windows.System.Profile.h>
#include <third_party/cppwinrt/generated/winrt/Windows.UI.Composition.h>
#include <third_party/cppwinrt/generated/winrt/Windows.UI.Core.h>
#include <third_party/cppwinrt/generated/winrt/Windows.UI.Input.h>
#include <third_party/cppwinrt/generated/winrt/Windows.UI.ViewManagement.Core.h>
#include <windows.ui.core.h>
#include <winrt/Windows.UI.Composition.h>
#include <winrt/Windows.UI.Input.h>
#include <winrt/Windows.UI.Text.Core.h>
#include <winrt/Windows.UI.ViewManagement.Core.h>
#include <winrt/Windows.UI.ViewManagement.h>
#include "flutter/shell/platform/embedder/embedder.h"
#include "flutter/shell/platform/windows/display_helper_winuwp.h"
#include "flutter/shell/platform/windows/flutter_windows_view.h"
#include "flutter/shell/platform/windows/game_pad_cursor_winuwp.h"
namespace flutter {
struct WindowBoundsWinUWP {
float width;
float height;
};
// Implements a UWP CoreWindow. Underlying window has been created and provided
// by the runner.
//
@@ -61,18 +55,6 @@ class FlutterWindowWinUWP : public WindowBindingHandler {
size_t height) override;
private:
// Returns a bounds structure containing width and height information
// for the backing CoreWindow in either view or physical pixels depending on
// |physical|.
WindowBoundsWinUWP GetBounds(
winrt::Windows::Graphics::Display::DisplayInformation const& disp,
bool physical);
// Returns a scaling factor representing the current DPI scale applied to the
// backing CoreWindow.
float GetDpiScale(
winrt::Windows::Graphics::Display::DisplayInformation const&);
// Undoes the scale transform applied by the Windows compositor in order to
// render at native scale and produce smooth results on high DPI screens.
void ApplyInverseDpiScalingTransform();
@@ -123,23 +105,12 @@ class FlutterWindowWinUWP : public WindowBindingHandler {
winrt::Windows::Foundation::IInspectable const&,
winrt::Windows::UI::Core::CharacterReceivedEventArgs const& args);
// Creates a visual representing the emulated cursor and add to the visual
// tree
winrt::Windows::UI::Composition::Visual CreateCursorVisual();
// Test is current context is running on an xbox device and perform device
// specific initialization.
void ConfigureXboxSpecific();
// Converts from logical point to physical X value.
double GetPosX(winrt::Windows::UI::Core::PointerEventArgs const& args);
// Converts from logical point to physical Y value.
double GetPosY(winrt::Windows::UI::Core::PointerEventArgs const& args);
// Token representing current worker thread.
winrt::Windows::Foundation::IAsyncAction worker_loop_{nullptr};
// Backing CoreWindow. nullptr if not set.
winrt::Windows::UI::Core::CoreWindow window_{nullptr};
@@ -157,27 +128,16 @@ class FlutterWindowWinUWP : public WindowBindingHandler {
// Composition tree root object.
winrt::Windows::UI::Composition::ContainerVisual visual_tree_root_{nullptr};
// Composition visual representing the emulated
// cursor visual.
winrt::Windows::UI::Composition::Visual cursor_visual_{nullptr};
// Compositor object that represents the render target binding the backing
// SwapChain to the CoreWindow.
winrt::Windows::UI::Composition::SpriteVisual render_target_{nullptr};
// Is current context is executing on an XBOX
// device.
bool running_on_xbox_ = false;
// GamepadCursorWinUWP object used to manage an emulated cursor visual driven
// by gamepad.
std::unique_ptr<GamepadCursorWinUWP> game_pad_cursor_{nullptr};
// Current X overscan compensation factor.
float xbox_overscan_x_offset_ = 0.0f;
// Current Y overscan compensation factor.
float xbox_overscan_y_offset_ = 0.0f;
// Most recent display information.
winrt::Windows::Graphics::Display::DisplayInformation current_display_info_{
nullptr};
// DisplayHelper object used to determine window bounds, DPI etc.
std::unique_ptr<DisplayHelperWinUWP> display_helper_ = {nullptr};
};
} // namespace flutter

View File

@@ -0,0 +1,313 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/shell/platform/windows/game_pad_cursor_winuwp.h"
#include "game_pad_cursor_winuwp.h"
namespace flutter {
GamepadCursorWinUWP::GamepadCursorWinUWP(
WindowBindingHandlerDelegate* view,
DisplayHelperWinUWP* displayhelper,
winrt::Windows::UI::Core::CoreWindow const& window,
winrt::Windows::UI::Composition::Compositor const& compositor,
winrt::Windows::UI::Composition::VisualCollection const& rootcollection) {
window_ = window;
root_collection_ = rootcollection;
binding_handler_delegate_ = view;
display_helper_ = displayhelper;
compositor_ = compositor;
cursor_visual_ = CreateCursorVisual();
ConfigureGamepad();
}
void GamepadCursorWinUWP::StartGamepadTimer() {
winrt::Windows::UI::Core::CoreDispatcher dispatcher = window_.Dispatcher();
dispatcher.RunAsync(
winrt::Windows::UI::Core::CoreDispatcherPriority::Normal,
[this, dispatcher]() {
auto queue =
winrt::Windows::System::DispatcherQueue::GetForCurrentThread();
if (cursor_move_timer_ == nullptr) {
cursor_move_timer_ = queue.CreateTimer();
cursor_move_timer_.Interval(
std::chrono::milliseconds(kNormalPollingFrequency));
cursor_move_timer_.Tick(
[=](winrt::Windows::System::DispatcherQueueTimer timer,
winrt::Windows::Foundation::IInspectable const&) {
game_pad_->Process();
});
cursor_move_timer_.Start();
}
});
}
void GamepadCursorWinUWP::StopGamepadTimer() {
winrt::Windows::UI::Core::CoreDispatcher dispatcher = window_.Dispatcher();
dispatcher.RunAsync(winrt::Windows::UI::Core::CoreDispatcherPriority::Normal,
[this, dispatcher]() {
if (cursor_move_timer_ != nullptr) {
cursor_move_timer_.Stop();
cursor_move_timer_ = nullptr;
}
});
}
void GamepadCursorWinUWP::ConfigureGamepad() {
GamepadDualAxisCallback leftStick = [=](double x, double y) {
OnGamepadLeftStickMoved(x, y);
};
GamepadDualAxisCallback rightStick = [=](double x, double y) {
OnGamepadRightStickMoved(x, y);
};
GamepadButtonCallback pressedcallback =
[=](winrt::Windows::Gaming::Input::GamepadButtons buttons) {
OnGamepadButtonPressed(buttons);
};
GamepadButtonCallback releasedcallback =
[=](winrt::Windows::Gaming::Input::GamepadButtons buttons) {
OnGamepadButtonReleased(buttons);
};
GamepadAddedRemovedCallback changed = [=]() {
OnGamepadControllersChanged();
};
game_pad_ = std::make_unique<GamepadWinUWP>(leftStick, rightStick, nullptr,
nullptr, pressedcallback,
releasedcallback, changed);
game_pad_->Initialize();
SetCursorTimeout();
}
void GamepadCursorWinUWP::SetCursorTimeout() {
if (cursor_visual_ == nullptr) {
return;
}
auto queue = winrt::Windows::System::DispatcherQueue::GetForCurrentThread();
// Lazily create timer.
if (emulated_cursor_hide_timer_ == nullptr) {
emulated_cursor_hide_timer_ = queue.CreateTimer();
emulated_cursor_hide_timer_.Interval(std::chrono::seconds(kInactivePeriod));
emulated_cursor_hide_timer_.Tick(
[=](winrt::Windows::System::DispatcherQueueTimer timer,
winrt::Windows::Foundation::IInspectable const&) {
// Timer fires when user has been inactive. At this point stop the
// timer. It will be restarted next time controller interaction
// happens triggering SetCursorTimeout
timer.Stop();
// If the timer fires, the user hasn't move the cursor for more than
// the interval hence hide the cursor
cursor_visual_.IsVisible(false);
// Reduce mouse move polling frequency while user not interacting with
// controller
SetMouseMovePollingFrequency(kReducedPollingFrequency);
});
}
// Ensure the cursor is visible and restart the timer as the user is
// interacting.
cursor_visual_.IsVisible(true);
emulated_cursor_hide_timer_.Start();
SetMouseMovePollingFrequency(kNormalPollingFrequency);
}
void GamepadCursorWinUWP::SetMouseMovePollingFrequency(int ms) {
if (cursor_move_timer_ != nullptr) {
cursor_move_timer_.Interval(std::chrono::milliseconds(ms));
}
}
winrt::Windows::Foundation::Numerics::float3
GamepadCursorWinUWP::GetScaledInput(
const winrt::Windows::Foundation::Numerics::float3 input) {
if (!display_helper_->IsRunningOnLargeScreenDevice()) {
const float inverse_dpi_scale = display_helper_->GetDpiScale();
return {cursor_visual_.Offset().x * inverse_dpi_scale,
cursor_visual_.Offset().y * inverse_dpi_scale, 1.0f};
} else {
return std::move(input);
}
}
void GamepadCursorWinUWP::OnGamepadLeftStickMoved(double x, double y) {
SetCursorTimeout();
float new_x =
cursor_visual_.Offset().x + (kCursorScale * static_cast<float>(x));
float new_y =
cursor_visual_.Offset().y + (kCursorScale * -static_cast<float>(y));
WindowBoundsWinUWP logical_bounds = display_helper_->GetLogicalBounds();
if (new_x > 0 && new_y > 0 && new_x < logical_bounds.width &&
new_y < logical_bounds.height) {
cursor_visual_.Offset({new_x, new_y, 0});
winrt::Windows::Foundation::Numerics::float3 scaled =
GetScaledInput(cursor_visual_.Offset());
binding_handler_delegate_->OnPointerMove(scaled.x, scaled.y);
}
}
void GamepadCursorWinUWP::OnGamepadRightStickMoved(double x, double y) {
winrt::Windows::Foundation::Numerics::float3 scaled =
GetScaledInput(cursor_visual_.Offset());
binding_handler_delegate_->OnScroll(scaled.x, scaled.y,
x * kControllerScrollMultiplier,
y * kControllerScrollMultiplier, 1);
}
void GamepadCursorWinUWP::OnGamepadButtonPressed(
winrt::Windows::Gaming::Input::GamepadButtons buttons) {
if ((buttons & winrt::Windows::Gaming::Input::GamepadButtons::A) ==
winrt::Windows::Gaming::Input::GamepadButtons::A) {
winrt::Windows::Foundation::Numerics::float3 scaled =
GetScaledInput(cursor_visual_.Offset());
// TODO(clarkezone) complete keyboard handling including
// system key (back), unicode handling, shortcut support,
// handling defered delivery, remove the need for action value.
// https://github.com/flutter/flutter/issues/70202
binding_handler_delegate_->OnPointerDown(
scaled.x, scaled.y,
FlutterPointerMouseButtons::kFlutterPointerButtonMousePrimary);
} else if ((buttons &
winrt::Windows::Gaming::Input::GamepadButtons::DPadLeft) ==
winrt::Windows::Gaming::Input::GamepadButtons::DPadLeft) {
binding_handler_delegate_->OnKey(
static_cast<int>(winrt::Windows::System::VirtualKey::Left), kScanLeft,
0x0100, 0, true, true);
} else if ((buttons &
winrt::Windows::Gaming::Input::GamepadButtons::DPadRight) ==
winrt::Windows::Gaming::Input::GamepadButtons::DPadRight) {
binding_handler_delegate_->OnKey(
static_cast<int>(winrt::Windows::System::VirtualKey::Right), kScanRight,
0x0100, 0, true, true);
} else if ((buttons &
winrt::Windows::Gaming::Input::GamepadButtons::DPadUp) ==
winrt::Windows::Gaming::Input::GamepadButtons::DPadUp) {
binding_handler_delegate_->OnKey(
static_cast<int>(winrt::Windows::System::VirtualKey::Up), kScanUp,
0x0100, 0, true, true);
} else if ((buttons &
winrt::Windows::Gaming::Input::GamepadButtons::DPadDown) ==
winrt::Windows::Gaming::Input::GamepadButtons::DPadDown) {
binding_handler_delegate_->OnKey(
static_cast<int>(winrt::Windows::System::VirtualKey::Down), kScanDown,
0x0100, 0, true, true);
}
}
void GamepadCursorWinUWP::OnGamepadButtonReleased(
winrt::Windows::Gaming::Input::GamepadButtons buttons) {
if ((buttons & winrt::Windows::Gaming::Input::GamepadButtons::A) ==
winrt::Windows::Gaming::Input::GamepadButtons::A) {
winrt::Windows::Foundation::Numerics::float3 scaled =
GetScaledInput(cursor_visual_.Offset());
// TODO(clarkezone) complete keyboard handling including
// system key (back), unicode handling, shortcut support,
// handling defered delivery, remove the need for action value.
// https://github.com/flutter/flutter/issues/70202
binding_handler_delegate_->OnPointerUp(
scaled.x, scaled.y,
FlutterPointerMouseButtons::kFlutterPointerButtonMousePrimary);
} else if ((buttons &
winrt::Windows::Gaming::Input::GamepadButtons::DPadLeft) ==
winrt::Windows::Gaming::Input::GamepadButtons::DPadLeft) {
binding_handler_delegate_->OnKey(
static_cast<int>(winrt::Windows::System::VirtualKey::Left), kScanLeft,
0x0100, 0, true, false);
} else if ((buttons &
winrt::Windows::Gaming::Input::GamepadButtons::DPadRight) ==
winrt::Windows::Gaming::Input::GamepadButtons::DPadRight) {
binding_handler_delegate_->OnKey(
static_cast<int>(winrt::Windows::System::VirtualKey::Right), kScanRight,
0x0100, 0, true, false);
} else if ((buttons &
winrt::Windows::Gaming::Input::GamepadButtons::DPadUp) ==
winrt::Windows::Gaming::Input::GamepadButtons::DPadUp) {
binding_handler_delegate_->OnKey(
static_cast<int>(winrt::Windows::System::VirtualKey::Up), kScanUp,
0x0100, 0, true, false);
} else if ((buttons &
winrt::Windows::Gaming::Input::GamepadButtons::DPadDown) ==
winrt::Windows::Gaming::Input::GamepadButtons::DPadDown) {
binding_handler_delegate_->OnKey(
static_cast<int>(winrt::Windows::System::VirtualKey::Down), kScanDown,
0x0100, 0, true, false);
}
}
void GamepadCursorWinUWP::OnGamepadControllersChanged() {
if (game_pad_->HasController()) {
if (!cursor_move_timer_) {
if (cursor_visual_ != nullptr) {
root_collection_.InsertAtTop(cursor_visual_);
}
StartGamepadTimer();
} else {
if (cursor_visual_ != nullptr) {
root_collection_.Remove(cursor_visual_);
}
StopGamepadTimer();
}
}
}
winrt::Windows::UI::Composition::Visual
GamepadCursorWinUWP::CreateCursorVisual() {
auto container = compositor_.CreateContainerVisual();
container.Offset(
{window_.Bounds().Width / 2, window_.Bounds().Height / 2, 1.0});
// size of the simulated mouse cursor
constexpr float size = 20;
constexpr float container_size = size + 10;
auto cursor_visual = compositor_.CreateShapeVisual();
cursor_visual.Size({container_size, container_size});
// compensate for overscan in cursor visual
cursor_visual.Offset({display_helper_->GetRenderTargetXOffset(),
display_helper_->GetRenderTargetYOffset(), 1.0});
winrt::Windows::UI::Composition::CompositionEllipseGeometry circle =
compositor_.CreateEllipseGeometry();
circle.Radius({size / 2, size / 2});
auto circleshape = compositor_.CreateSpriteShape(circle);
circleshape.FillBrush(
compositor_.CreateColorBrush(winrt::Windows::UI::Colors::Black()));
circleshape.StrokeBrush(
compositor_.CreateColorBrush(winrt::Windows::UI::Colors::White()));
circleshape.StrokeThickness(5.0);
circleshape.Offset({container_size / 2, container_size / 2});
cursor_visual.Shapes().Append(circleshape);
winrt::Windows::UI::Composition::Visual visual =
cursor_visual.as<winrt::Windows::UI::Composition::Visual>();
visual.AnchorPoint({0.5, 0.5});
container.Children().InsertAtTop(visual);
return container;
}
} // namespace flutter

View File

@@ -0,0 +1,141 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_GAME_PAD_CURSOR_WINUWP_H_
#define FLUTTER_SHELL_PLATFORM_WINDOWS_GAME_PAD_CURSOR_WINUWP_H_
#include <DispatcherQueue.h>
#include <third_party/cppwinrt/generated/winrt/Windows.Foundation.Collections.h>
#include <third_party/cppwinrt/generated/winrt/Windows.Graphics.Display.h>
#include <third_party/cppwinrt/generated/winrt/Windows.System.Profile.h>
#include <third_party/cppwinrt/generated/winrt/Windows.UI.Composition.h>
#include <third_party/cppwinrt/generated/winrt/Windows.UI.Core.h>
#include <third_party/cppwinrt/generated/winrt/Windows.UI.ViewManagement.Core.h>
#include "third_party/cppwinrt/generated/winrt/Windows.System.Threading.h"
#include "flutter/shell/platform/windows/display_helper_winuwp.h"
#include "flutter/shell/platform/windows/flutter_windows_view.h"
#include "flutter/shell/platform/windows/game_pad_winuwp.h"
namespace flutter {
// A class abstraction for a mouse cursor driven by a gamepad. This abstraction
// is primarily intended for devices like XBOX that do not have traditional
// mouse input.
class GamepadCursorWinUWP {
public:
GamepadCursorWinUWP(
WindowBindingHandlerDelegate* view,
DisplayHelperWinUWP* displayhelper,
winrt::Windows::UI::Core::CoreWindow const& window,
winrt::Windows::UI::Composition::Compositor const& compositor,
winrt::Windows::UI::Composition::VisualCollection const& rootcollection);
private:
// Pointer to a DisplayHelperWinUWP object used to get window bounds, DPI and
// other display-related aspects.
DisplayHelperWinUWP* display_helper_;
// Current active compositor. nullptr if not set.
winrt::Windows::UI::Composition::Compositor compositor_{nullptr};
// Window that is hosting this emulated cursor.
winrt::Windows::UI::Core::CoreWindow window_{nullptr};
// The root of the composition tree that this class will add a cursor visual
// to.
winrt::Windows::UI::Composition::VisualCollection root_collection_{nullptr};
// Called when the user is interacting with the GamePad and moving the mouse
// cursor. Prevents the mouse cursor from being hidden by resetting the
// inactivity timer.
void SetCursorTimeout();
// Sets the frequency, in milliseconds, that the gamepad is polled for
// movement updates.
void SetMouseMovePollingFrequency(int ms);
// Notifies current |WindowBindingHandlerDelegate| of gamepad right stick
// events as emulated mouse move events.
void OnGamepadLeftStickMoved(double x, double y);
// Notifies current |WindowBindingHandlerDelegate| of gamepad right stick
// events delivered as scroll events.
void OnGamepadRightStickMoved(double x, double y);
// Notifies current |WindowBindingHandlerDelegate| of left gamepad move events
// delivered as emulated mouse button events.
void OnGamepadButtonPressed(
winrt::Windows::Gaming::Input::GamepadButtons buttons);
// Notifies current |WindowBindingHandlerDelegate| of left gamepad move events
// delivered as emulated mouse button events.
void OnGamepadButtonReleased(
winrt::Windows::Gaming::Input::GamepadButtons buttons);
// Show and hide the emulated mouse cursor when a gamepad arrives / departs
void OnGamepadControllersChanged();
// Creates a visual representing the emulated cursor and add to the visual
// tree.
winrt::Windows::UI::Composition::Visual CreateCursorVisual();
// Starts a timer used to update the position of the cursor visual as the
// gamepad updates the position.
void StartGamepadTimer();
// Stops time used to move the cursor.
void StopGamepadTimer();
// Configure callbacks to notify when Gamepad hardware events are received.
void ConfigureGamepad();
winrt::Windows::Foundation::Numerics::float3 GetScaledInput(
const winrt::Windows::Foundation::Numerics::float3 input);
// Object responsible for handling the low level interactions with the
// gamepad.
std::unique_ptr<GamepadWinUWP> game_pad_{nullptr};
// Pointer to a FlutterWindowsView that can be
// used to update engine windowing and input state.
WindowBindingHandlerDelegate* binding_handler_delegate_;
// Multiplier used to map controller velocity to an appropriate scroll input.
static constexpr double kControllerScrollMultiplier = 3;
// Multiplier used to scale gamepad input to mouse equivalent response.
static constexpr int kCursorScale = 7;
// Number of inactive seconds after which emulated cursor is hidden (ms).
static constexpr int kInactivePeriod = 5;
// Frequency to poll for input when no active interaction (ms).
static constexpr int kReducedPollingFrequency = 250;
// Frequency to poll for input when user is interacting (ms).
static constexpr int kNormalPollingFrequency = 10;
// Scancode definitions for dpad to cursor mapping.
static constexpr int kScanLeft = 75;
static constexpr int kScanRight = 77;
static constexpr int kScanUp = 72;
static constexpr int kScanDown = 80;
// Timer object used to update the cursor visual.
winrt::Windows::System::DispatcherQueueTimer cursor_move_timer_{nullptr};
// Timer object used to hide the mouse cursor when the user isn't interacting
// with the Gamepad.
winrt::Windows::System::DispatcherQueueTimer emulated_cursor_hide_timer_{
nullptr};
// Composition visual representing the emulated
// cursor visual.
winrt::Windows::UI::Composition::Visual cursor_visual_{nullptr};
};
} // namespace flutter
#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_GAME_PAD_CURSOR_WINUWP_H_

View File

@@ -0,0 +1,218 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/shell/platform/windows/game_pad_winuwp.h"
namespace flutter {
GamepadWinUWP::GamepadWinUWP(GamepadDualAxisCallback leftstick,
GamepadDualAxisCallback rightstick,
GamepadSingleAxisCallback lefttrigger,
GamepadSingleAxisCallback righttrigger,
GamepadButtonCallback pressedcb,
GamepadButtonCallback releasedcb,
GamepadAddedRemovedCallback changedcb)
: pressed_buttons_(winrt::Windows::Gaming::Input::GamepadButtons::None),
left_stick_x_value_(0),
left_stick_y_value_(0),
right_stick_x_value_(0),
right_stick_y_value_(0),
left_trigger_value_(0),
right_trigger_value_(0) {
left_stick_callback_ = leftstick;
right_stick_callback_ = rightstick;
left_trigger_callback_ = lefttrigger;
right_trigger_callback_ = righttrigger;
button_pressed_callback_ = pressedcb;
button_released_callback_ = releasedcb;
arrival_departure_callback_ = changedcb;
}
void GamepadWinUWP::Initialize() {
RefreshCachedGamepads();
winrt::Windows::Gaming::Input::Gamepad::GamepadAdded(
{this, &GamepadWinUWP::OnGamepadAdded});
winrt::Windows::Gaming::Input::Gamepad::GamepadRemoved(
{this, &GamepadWinUWP::OnGamepadRemoved});
const std::lock_guard<std::mutex> lock(gamepad_mutex_);
current_game_pad_ = GetLastGamepad();
}
bool GamepadWinUWP::HasController() const {
const std::lock_guard<std::mutex> lock(gamepad_mutex_);
return current_game_pad_ != nullptr;
}
void GamepadWinUWP::OnGamepadAdded(
winrt::Windows::Foundation::IInspectable const& th,
winrt::Windows::Gaming::Input::Gamepad const& args) {
enumerated_game_pads_.push_back(args);
{
const std::lock_guard<std::mutex> lock(gamepad_mutex_);
auto most_recent = GetLastGamepad();
if (current_game_pad_ != most_recent) {
current_game_pad_ = most_recent;
}
}
if (this->arrival_departure_callback_ != nullptr) {
arrival_departure_callback_();
}
}
void GamepadWinUWP::OnGamepadRemoved(
winrt::Windows::Foundation::IInspectable const&,
winrt::Windows::Gaming::Input::Gamepad const& /*args*/) {
RefreshCachedGamepads();
}
void GamepadWinUWP::RefreshCachedGamepads() {
enumerated_game_pads_.clear();
auto gamepads = winrt::Windows::Gaming::Input::Gamepad::Gamepads();
for (auto gamepad : gamepads) {
enumerated_game_pads_.push_back(gamepad);
}
}
const winrt::Windows::Gaming::Input::Gamepad* GamepadWinUWP::GetLastGamepad()
const {
const winrt::Windows::Gaming::Input::Gamepad* gamepad = nullptr;
if (!enumerated_game_pads_.empty()) {
gamepad = &enumerated_game_pads_.back();
}
return gamepad;
}
static bool IsValid(double value) {
return value > 0.1 || value < -0.1;
}
void GamepadWinUWP::Process() {
{
const std::lock_guard<std::mutex> lock(gamepad_mutex_);
if (current_game_pad_ == nullptr) {
return;
}
last_reading_ = current_game_pad_->GetCurrentReading();
}
namespace gi = winrt::Windows::Gaming::Input;
GamepadButtonPressedInternal(last_reading_.Buttons);
// work out which values have changed since the last reading.
if (last_reading_.LeftThumbstickX != left_stick_x_value_) {
left_stick_x_value_ = last_reading_.LeftThumbstickX;
}
if (last_reading_.LeftThumbstickY != left_stick_y_value_) {
left_stick_y_value_ = last_reading_.LeftThumbstickY;
}
if (IsValid(left_stick_x_value_) || IsValid(left_stick_y_value_)) {
RaiseLeftStickMoved(left_stick_x_value_, left_stick_y_value_);
}
if (last_reading_.RightThumbstickX != right_stick_x_value_) {
right_stick_x_value_ = last_reading_.RightThumbstickX;
}
if (last_reading_.RightThumbstickY != right_stick_y_value_) {
right_stick_y_value_ = last_reading_.RightThumbstickY;
}
if (IsValid(right_stick_x_value_) || IsValid(right_stick_y_value_)) {
RaiseRightStickMoved(right_stick_x_value_, right_stick_y_value_);
}
// Raise the requisit events based on what's changed since the last reading
// was taken.
if (last_reading_.LeftTrigger != 0 &&
last_reading_.LeftTrigger != left_trigger_value_) {
left_trigger_value_ = last_reading_.LeftTrigger;
RaiseLeftTriggerMoved(left_trigger_value_);
}
if (last_reading_.RightTrigger != 0 &&
last_reading_.RightTrigger != right_trigger_value_) {
right_trigger_value_ = last_reading_.RightTrigger;
RaiseRightTriggerMoved(right_trigger_value_);
}
}
void GamepadWinUWP::GamepadButtonPressedInternal(
winrt::Windows::Gaming::Input::GamepadButtons state) {
namespace wgi = winrt::Windows::Gaming::Input;
static const wgi::GamepadButtons AllButtons[] = {
wgi::GamepadButtons::A, wgi::GamepadButtons::B,
wgi::GamepadButtons::DPadDown, wgi::GamepadButtons::DPadLeft,
wgi::GamepadButtons::DPadRight, wgi::GamepadButtons::DPadUp};
for (const auto e : AllButtons) {
// if button is pressed we have not sent a pressed already
if (((e & state) == e) && ((pressed_buttons_ & e) != e)) {
// send pressed callback
if (button_pressed_callback_ != nullptr) {
button_pressed_callback_(e);
}
// set the bit
pressed_buttons_ |= e;
}
// if button is not pressed and we have sent a pressed already
if (((e & state) != e) && ((pressed_buttons_ & e) == e)) {
// send callback
if (button_released_callback_ != nullptr) {
button_released_callback_(e);
}
// set the pressed bit
pressed_buttons_ &= ~e;
}
}
}
void GamepadWinUWP::RaiseGameGamepadButtonPressed(
winrt::Windows::Gaming::Input::GamepadButtons b) {}
void GamepadWinUWP::RaiseGameGamepadButtonReleased(
winrt::Windows::Gaming::Input::GamepadButtons b) {}
void GamepadWinUWP::RaiseLeftStickMoved(double x, double y) {
if (left_stick_callback_ != nullptr) {
left_stick_callback_(x, y);
}
}
void GamepadWinUWP::RaiseRightStickMoved(double x, double y) {
if (right_stick_callback_ != nullptr) {
right_stick_callback_(x, y);
}
}
void GamepadWinUWP::RaiseLeftTriggerMoved(double value) {
if (left_trigger_callback_ != nullptr) {
left_trigger_callback_(value);
}
}
void GamepadWinUWP::RaiseRightTriggerMoved(double value) {
if (right_trigger_callback_ != nullptr) {
right_trigger_callback_(value);
}
}
} // namespace flutter

View File

@@ -0,0 +1,112 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_GAME_PAD_H_
#define FLUTTER_SHELL_PLATFORM_WINDOWS_GAME_PAD_H_
#include <third_party/cppwinrt/generated/winrt/Windows.Foundation.Collections.h>
#include <third_party/cppwinrt/generated/winrt/Windows.Gaming.Input.h>
#include <functional>
#include <mutex>
#include <vector>
namespace flutter {
// Type definitions for callback funtions.
using GamepadSingleAxisCallback = std::function<void(double)>;
using GamepadDualAxisCallback = std::function<void(double, double)>;
using GamepadButtonCallback =
std::function<void(winrt::Windows::Gaming::Input::GamepadButtons)>;
using GamepadAddedRemovedCallback = std::function<void()>;
// A class abstraction for a Gamepad input device. Used to detect when devices
// come and go and provide event hooks for receiving scalar and discrete inputs
// from buttons and analog control surfaces.
class GamepadWinUWP {
public:
GamepadWinUWP(GamepadDualAxisCallback,
GamepadDualAxisCallback,
GamepadSingleAxisCallback,
GamepadSingleAxisCallback,
GamepadButtonCallback,
GamepadButtonCallback,
GamepadAddedRemovedCallback);
// Enumerate and cache any gamepads that are already connected and hook up
// handlers to detect arrival and departure of new devices.
void Initialize();
// Update state of the currently selected gamepad buttons and controllers if
// one exists. Raise resulting events with any attached event consumers. Must
// be called regularly in order to keep state updated.
void Process();
// There is a controller currently attached.
bool HasController() const;
private:
// Returns the last item in the collection of currently attached Gamepads.
const winrt::Windows::Gaming::Input::Gamepad* GetLastGamepad() const;
// Handers fired when a Gamepad is attached or removed from the system
// respectively.
void OnGamepadAdded(winrt::Windows::Foundation::IInspectable const& sender,
winrt::Windows::Gaming::Input::Gamepad const& args);
void OnGamepadRemoved(winrt::Windows::Foundation::IInspectable const& sender,
winrt::Windows::Gaming::Input::Gamepad const& args);
// Scan for attached Gamepad devices.
void RefreshCachedGamepads();
// Internal handler used to intercept raw device button presses.
void GamepadButtonPressedInternal(
winrt::Windows::Gaming::Input::GamepadButtons b);
// Functions to propogate controller events to external consumers.
void RaiseLeftStickMoved(double x, double y);
void RaiseRightStickMoved(double x, double y);
void RaiseLeftTriggerMoved(double value);
void RaiseRightTriggerMoved(double value);
void RaiseGameGamepadButtonPressed(
winrt::Windows::Gaming::Input::GamepadButtons);
void RaiseGameGamepadButtonReleased(
winrt::Windows::Gaming::Input::GamepadButtons);
// Storage for attached Gampads.
std::vector<winrt::Windows::Gaming::Input::Gamepad> enumerated_game_pads_;
// Storage for the last hardware state returned from the current controller.
winrt::Windows::Gaming::Input::GamepadReading last_reading_;
// Pointer to the current GamePad instance if there is one.
const winrt::Windows::Gaming::Input::Gamepad* current_game_pad_;
// Mutex to protect access to current gamepad.
mutable std::mutex gamepad_mutex_;
// Storage for most recently communicated trigger and stick values.
double left_trigger_value_;
double right_trigger_value_;
double left_stick_x_value_;
double left_stick_y_value_;
double right_stick_x_value_;
double right_stick_y_value_;
// Storage for most recently pressed buttons.
winrt::Windows::Gaming::Input::GamepadButtons pressed_buttons_;
// Storage for callbacks.
GamepadDualAxisCallback left_stick_callback_;
GamepadDualAxisCallback right_stick_callback_;
GamepadSingleAxisCallback left_trigger_callback_;
GamepadSingleAxisCallback right_trigger_callback_;
GamepadButtonCallback button_pressed_callback_;
GamepadButtonCallback button_released_callback_;
GamepadAddedRemovedCallback arrival_departure_callback_;
};
} // namespace flutter
#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_GAME_PAD_H_