<commit_before>// Copyright (c) 2012 The Chromium 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 "content/browser/gamepad/platform_data_fetcher_win.h"

#include "base/debug/trace_event.h"
#include "content/common/gamepad_messages.h"
#include "content/common/gamepad_hardware_buffer.h"

#include <delayimp.h>

#pragma comment(lib, "delayimp.lib")
#pragma comment(lib, "xinput.lib")

namespace content {

using namespace WebKit;

namespace {

// See http://goo.gl/5VSJR. These are not available in all versions of the
// header, but they can be returned from the driver, so we define our own
// versions here.
static const BYTE kDeviceSubTypeGamepad = 1;
static const BYTE kDeviceSubTypeWheel = 2;
static const BYTE kDeviceSubTypeArcadeStick = 3;
static const BYTE kDeviceSubTypeFlightStick = 4;
static const BYTE kDeviceSubTypeDancePad = 5;
static const BYTE kDeviceSubTypeGuitar = 6;
static const BYTE kDeviceSubTypeGuitarAlternate = 7;
static const BYTE kDeviceSubTypeDrumKit = 8;
static const BYTE kDeviceSubTypeGuitarBass = 11;
static const BYTE kDeviceSubTypeArcadePad = 19;

const WebUChar* const GamepadSubTypeName(BYTE sub_type) {
  switch (sub_type) {
    case kDeviceSubTypeGamepad: return L"GAMEPAD";
    case kDeviceSubTypeWheel: return L"WHEEL";
    case kDeviceSubTypeArcadeStick: return L"ARCADE_STICK";
    case kDeviceSubTypeFlightStick: return L"FLIGHT_STICK";
    case kDeviceSubTypeDancePad: return L"DANCE_PAD";
    case kDeviceSubTypeGuitar: return L"GUITAR";
    case kDeviceSubTypeGuitarAlternate: return L"GUITAR_ALTERNATE";
    case kDeviceSubTypeDrumKit: return L"DRUM_KIT";
    case kDeviceSubTypeGuitarBass: return L"GUITAR_BASS";
    case kDeviceSubTypeArcadePad: return L"ARCADE_PAD";
    default: return L"<UNKNOWN>";
  }
}

// Trap only the exceptions that DELAYLOAD can throw, otherwise rethrow.
// See http://msdn.microsoft.com/en-us/library/1c9e046h(v=VS.90).aspx.
LONG WINAPI DelayLoadDllExceptionFilter(PEXCEPTION_POINTERS pExcPointers) {
 LONG disposition = EXCEPTION_EXECUTE_HANDLER;
 switch (pExcPointers->ExceptionRecord->ExceptionCode) {
   case VcppException(ERROR_SEVERITY_ERROR, ERROR_MOD_NOT_FOUND):
   case VcppException(ERROR_SEVERITY_ERROR, ERROR_PROC_NOT_FOUND):
      break;
   default:
      // Exception is not related to delay loading.
      disposition = EXCEPTION_CONTINUE_SEARCH;
      break;
   }
   return disposition;
}

bool EnableXInput() {
  // We have specified DELAYLOAD for xinput1_3.dll. If the DLL is not
  // installed (XP w/o DirectX redist installed), we disable functionality.
  __try {
    ;//XInputEnable(true);
  } __except(DelayLoadDllExceptionFilter(GetExceptionInformation())) {
    return false;
  }
  return true;
}

}

GamepadPlatformDataFetcherWin::GamepadPlatformDataFetcherWin()
    : xinput_available_(EnableXInput()) {
}

void GamepadPlatformDataFetcherWin::GetGamepadData(WebGamepads* pads,
                                        bool devices_changed_hint) {
  TRACE_EVENT0("GAMEPAD", "GetGamepadData");<commit_msg>re-enable call to XInputEnable<commit_after>// Copyright (c) 2012 The Chromium 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 "content/browser/gamepad/platform_data_fetcher_win.h"

#include "base/debug/trace_event.h"
#include "content/common/gamepad_messages.h"
#include "content/common/gamepad_hardware_buffer.h"

#include <delayimp.h>

#pragma comment(lib, "delayimp.lib")
#pragma comment(lib, "xinput.lib")

namespace content {

using namespace WebKit;

namespace {

// See http://goo.gl/5VSJR. These are not available in all versions of the
// header, but they can be returned from the driver, so we define our own
// versions here.
static const BYTE kDeviceSubTypeGamepad = 1;
static const BYTE kDeviceSubTypeWheel = 2;
static const BYTE kDeviceSubTypeArcadeStick = 3;
static const BYTE kDeviceSubTypeFlightStick = 4;
static const BYTE kDeviceSubTypeDancePad = 5;
static const BYTE kDeviceSubTypeGuitar = 6;
static const BYTE kDeviceSubTypeGuitarAlternate = 7;
static const BYTE kDeviceSubTypeDrumKit = 8;
static const BYTE kDeviceSubTypeGuitarBass = 11;
static const BYTE kDeviceSubTypeArcadePad = 19;

const WebUChar* const GamepadSubTypeName(BYTE sub_type) {
  switch (sub_type) {
    case kDeviceSubTypeGamepad: return L"GAMEPAD";
    case kDeviceSubTypeWheel: return L"WHEEL";
    case kDeviceSubTypeArcadeStick: return L"ARCADE_STICK";
    case kDeviceSubTypeFlightStick: return L"FLIGHT_STICK";
    case kDeviceSubTypeDancePad: return L"DANCE_PAD";
    case kDeviceSubTypeGuitar: return L"GUITAR";
    case kDeviceSubTypeGuitarAlternate: return L"GUITAR_ALTERNATE";
    case kDeviceSubTypeDrumKit: return L"DRUM_KIT";
    case kDeviceSubTypeGuitarBass: return L"GUITAR_BASS";
    case kDeviceSubTypeArcadePad: return L"ARCADE_PAD";
    default: return L"<UNKNOWN>";
  }
}

// Trap only the exceptions that DELAYLOAD can throw, otherwise rethrow.
// See http://msdn.microsoft.com/en-us/library/1c9e046h(v=VS.90).aspx.
LONG WINAPI DelayLoadDllExceptionFilter(PEXCEPTION_POINTERS pExcPointers) {
 LONG disposition = EXCEPTION_EXECUTE_HANDLER;
 switch (pExcPointers->ExceptionRecord->ExceptionCode) {
   case VcppException(ERROR_SEVERITY_ERROR, ERROR_MOD_NOT_FOUND):
   case VcppException(ERROR_SEVERITY_ERROR, ERROR_PROC_NOT_FOUND):
      break;
   default:
      // Exception is not related to delay loading.
      disposition = EXCEPTION_CONTINUE_SEARCH;
      break;
   }
   return disposition;
}

bool EnableXInput() {
  // We have specified DELAYLOAD for xinput1_3.dll. If the DLL is not
  // installed (XP w/o DirectX redist installed), we disable functionality.
  __try {
    XInputEnable(true);
  } __except(DelayLoadDllExceptionFilter(GetExceptionInformation())) {
    return false;
  }
  return true;
}

}

GamepadPlatformDataFetcherWin::GamepadPlatformDataFetcherWin()
    : xinput_available_(EnableXInput()) {
}

void GamepadPlatformDataFetcherWin::GetGamepadData(WebGamepads* pads,
                                        bool devices_changed_hint) {
  TRACE_EVENT0("GAMEPAD", "GetGamepadData");
{'fraction_non_alphanumeric': 0.08432571612487931, 'fraction_numerical': 0.008690054715159318, 'mean_word_length': 4.770659238625813, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5211955', 'n_tokens_mistral': 2104, 'n_tokens_neox': 2017, 'n_words': 516}