blob: bb6afb290dde074efb325bc7924fad5dc42e0473 [file] [log] [blame]
// Copyright 2022 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 SRC_EMBEDDER_MOUSE_DELEGATE_H_
#define SRC_EMBEDDER_MOUSE_DELEGATE_H_
#include <fuchsia/ui/pointer/cpp/fidl.h>
#include <functional>
#include <optional>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "src/embedder/engine/embedder.h"
#include "src/embedder/logging.h"
namespace embedder {
/// Channel processor for fuchsia.ui.pointer.MouseSource protocol
/// protocol. It manages the channel state, collects mouse events, and
/// converts them to generic FlutterPointerEvent types before
/// returning them to the embedder's main function, which
/// dispatches them to the flutter engine using the embedder API
class MouseDelegate {
public:
/// Initializes the MouseDelegate. Does NOT start watching for events.
/// |mouse_source| - the FIDL handle for mouse input events
MouseDelegate(fuchsia::ui::pointer::MouseSourceHandle mouse_source);
/// This function collects Fuchsia's ui::pointer::MouseEvent's and
/// transforms them into FlutterPointerEvent structs. It then calls
/// the supplied callback with a vector of FlutterPointerEvent, which
/// uses the embedder API to forward these to the flutter engine
void WatchLoop(std::function<void(std::vector<FlutterPointerEvent>)> callback);
private:
/// Channel for mouse events from Scenic.
fuchsia::ui::pointer::MouseSourcePtr mouse_source_;
/// Receive mouse events from Scenic. Must be copyable as the FIDL protocol
/// accepting this callback will take this by value.
std::function<void(std::vector<fuchsia::ui::pointer::MouseEvent>)> mouse_responder_;
/// The set of mouse devices that are currently interacting with the UI.
/// A mouse is considered flutter::PointerData::Change::kDown if any button is
/// pressed. This set is used to correctly set the phase in
/// flutter::PointerData.change, with this high-level algorithm:
/// if !mouse_down[id] && !button then: change = kHover
/// if !mouse_down[id] && button then: change = kDown; mouse_down.add(id)
/// if mouse_down[id] && button then: change = kMove
/// if mouse_down[id] && !button then: change = kUp; mouse_down.remove(id)
std::unordered_set</*mouse device ID*/ uint32_t> mouse_down_;
/// For each mouse device, its device-specific information, such as mouse
/// button priority order.
std::unordered_map</*mouse device ID*/ uint32_t, fuchsia::ui::pointer::MouseDeviceInfo>
mouse_device_info_;
/// The fuchsia.ui.pointer.MouseSource protocol issues channel-global view
/// parameters on connection and on change. Events must apply these view
/// parameters to correctly map to logical view coordinates. The "nullopt"
/// state represents the absence of view parameters, early in the protocol
/// lifecycle.
std::optional<fuchsia::ui::pointer::ViewParameters> mouse_view_parameters_;
};
} // namespace embedder
#endif // SRC_EMBEDDER_MOUSE_DELEGATE_H_