blob: a49dc85674ab03b200273966519360e222cceac1 [file] [log] [blame]
// Copyright 2019 The Fuchsia 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 <fuchsia/hardware/input/cpp/fidl.h>
#include <zircon/types.h>
#include <memory>
#include <utility>
#include <vector>
#include <hid-parser/report.h>
#include <hid-parser/usages.h>
// This class connects to input devices and allows for queries to input buttons or switches.
// Currently, this class only supports checking the state of the mute switch.
class ButtonChecker {
enum class ButtonState {
UNKNOWN, // Button state could not be determined or is undefined.
DOWN, // Button is pressed, on, or active.
UP, // Button is not pressed, off, or inactive.
// Creates a new ButtonChecker instance. Returns nullptr on failure.
static std::unique_ptr<ButtonChecker> Create();
// Gets the state of the Mute button/switch, if available.
ButtonState GetMuteState();
// Create a device proxy on the provided filesystem path. Returns an unbound proxy on failure.
static fuchsia::hardware::input::DeviceSyncPtr BindDevice(const std::string& path);
// Populates a report field for a mute button, if present, on the device. Returns false iff the
// field was successfully populated.
static bool GetMuteFieldForDevice(fuchsia::hardware::input::DeviceSyncPtr& device,
hid::ReportField* mute_field_out);
std::vector<std::pair<fuchsia::hardware::input::DeviceSyncPtr, hid::ReportField>> devices_;
// Convenience wrapper to check the mute state of a device. Returns true if the device is confirmed
// to be unmuted. If the device is muted or its mute state could not be determined, a warning is
// printed to stderr.
bool VerifyDeviceUnmuted(bool consider_unknown_as_unmuted = false);