From 36d22d82aa202bb199967e9512281e9a53db42c9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 21:33:14 +0200 Subject: Adding upstream version 115.7.0esr. Signed-off-by: Daniel Baumann --- dom/gamepad/cocoa/CocoaGamepad.cpp | 665 +++++++++++++++++++++++++++++++++++++ 1 file changed, 665 insertions(+) create mode 100644 dom/gamepad/cocoa/CocoaGamepad.cpp (limited to 'dom/gamepad/cocoa/CocoaGamepad.cpp') diff --git a/dom/gamepad/cocoa/CocoaGamepad.cpp b/dom/gamepad/cocoa/CocoaGamepad.cpp new file mode 100644 index 0000000000..544c2e42b7 --- /dev/null +++ b/dom/gamepad/cocoa/CocoaGamepad.cpp @@ -0,0 +1,665 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +// mostly derived from the Allegro source code at: +// http://alleg.svn.sourceforge.net/viewvc/alleg/allegro/branches/4.9/src/macosx/hidjoy.m?revision=13760&view=markup + +#include "mozilla/dom/GamepadHandle.h" +#include "mozilla/dom/GamepadPlatformService.h" +#include "mozilla/dom/GamepadRemapping.h" +#include "mozilla/ipc/BackgroundParent.h" +#include "mozilla/ArrayUtils.h" +#include "mozilla/Sprintf.h" +#include "mozilla/Tainting.h" +#include "nsComponentManagerUtils.h" +#include "nsITimer.h" +#include "nsThreadUtils.h" +#include +#include +#include +#include + +#include +#include + +namespace { + +using namespace mozilla; +using namespace mozilla::dom; +class DarwinGamepadService; + +DarwinGamepadService* gService = nullptr; + +struct Button { + int id; + bool analog; + IOHIDElementRef element; + CFIndex min; + CFIndex max; + bool pressed; + + Button(int aId, IOHIDElementRef aElement, CFIndex aMin, CFIndex aMax) + : id(aId), + analog((aMax - aMin) > 1), + element(aElement), + min(aMin), + max(aMax), + pressed(false) {} +}; + +struct Axis { + int id; + IOHIDElementRef element; + uint32_t usagePage; + uint32_t usage; + CFIndex min; + CFIndex max; + double value; +}; + +// These values can be found in the USB HID Usage Tables: +// http://www.usb.org/developers/hidpage +const unsigned kDesktopUsagePage = 0x01; +const unsigned kSimUsagePage = 0x02; +const unsigned kAcceleratorUsage = 0xC4; +const unsigned kBrakeUsage = 0xC5; +const unsigned kJoystickUsage = 0x04; +const unsigned kGamepadUsage = 0x05; +const unsigned kAxisUsageMin = 0x30; +const unsigned kAxisUsageMax = 0x35; +const unsigned kDpadUsage = 0x39; +const unsigned kButtonUsagePage = 0x09; +const unsigned kConsumerPage = 0x0C; +const unsigned kHomeUsage = 0x223; +const unsigned kBackUsage = 0x224; + +// We poll it periodically, +// 50ms is arbitrarily chosen. +const uint32_t kDarwinGamepadPollInterval = 50; + +struct GamepadInputReportContext { + DarwinGamepadService* service; + size_t gamepadSlot; +}; + +class Gamepad { + private: + IOHIDDeviceRef mDevice; + nsTArray