1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CONTENT_BROWSER_GAMEPAD_GAMEPAD_PLATFORM_DATA_FETCHER_WIN_H_
6#define CONTENT_BROWSER_GAMEPAD_GAMEPAD_PLATFORM_DATA_FETCHER_WIN_H_
7
8#include "build/build_config.h"
9
10#ifndef WIN32_LEAN_AND_MEAN
11#define WIN32_LEAN_AND_MEAN
12#endif
13#include <stdlib.h>
14#include <Unknwn.h>
15#include <WinDef.h>
16#include <windows.h>
17#include <XInput.h>
18
19#include "base/basictypes.h"
20#include "base/compiler_specific.h"
21#include "base/memory/scoped_ptr.h"
22#include "base/memory/weak_ptr.h"
23#include "base/message_loop/message_loop.h"
24#include "base/scoped_native_library.h"
25#include "content/browser/gamepad/gamepad_data_fetcher.h"
26#include "content/browser/gamepad/gamepad_standard_mappings.h"
27#include "content/browser/gamepad/raw_input_data_fetcher_win.h"
28#include "third_party/WebKit/public/platform/WebGamepads.h"
29
30namespace content {
31
32class GamepadPlatformDataFetcherWin : public GamepadDataFetcher {
33 public:
34  GamepadPlatformDataFetcherWin();
35  virtual ~GamepadPlatformDataFetcherWin();
36  virtual void GetGamepadData(blink::WebGamepads* pads,
37                              bool devices_changed_hint) OVERRIDE;
38  virtual void PauseHint(bool paused) OVERRIDE;
39
40 private:
41  // XInput-specific implementation for GetGamepadData.
42  bool GetXInputGamepadData(blink::WebGamepads* pads,
43                            bool devices_changed_hint);
44
45  // The three function types we use from xinput1_3.dll.
46  typedef void (WINAPI *XInputEnableFunc)(BOOL enable);
47  typedef DWORD (WINAPI *XInputGetCapabilitiesFunc)(
48    DWORD dwUserIndex, DWORD dwFlags, XINPUT_CAPABILITIES* pCapabilities);
49  typedef DWORD (WINAPI *XInputGetStateFunc)(
50      DWORD dwUserIndex, XINPUT_STATE* pState);
51
52  // Get functions from dynamically loaded xinput1_3.dll. We don't use
53  // DELAYLOAD because the import library for Win8 SDK pulls xinput1_4 which
54  // isn't redistributable. Returns true if loading was successful. We include
55  // xinput1_3.dll with Chrome.
56  bool GetXInputDllFunctions();
57
58  // Scan for connected XInput and DirectInput gamepads.
59  void EnumerateDevices(blink::WebGamepads* pads);
60  bool GetXInputPadConnectivity(int i, blink::WebGamepad* pad) const;
61
62  void GetXInputPadData(int i, blink::WebGamepad* pad);
63  void GetRawInputPadData(int i, blink::WebGamepad* pad);
64
65  int FirstAvailableGamepadId() const;
66  bool HasXInputGamepad(int index) const;
67  bool HasRawInputGamepad(const HANDLE handle) const;
68
69  base::ScopedNativeLibrary xinput_dll_;
70  bool xinput_available_;
71
72  // Function pointers to XInput functionality, retrieved in
73  // |GetXinputDllFunctions|.
74  XInputEnableFunc xinput_enable_;
75  XInputGetCapabilitiesFunc xinput_get_capabilities_;
76  XInputGetStateFunc xinput_get_state_;
77
78  enum PadConnectionStatus {
79    DISCONNECTED,
80    XINPUT_CONNECTED,
81    RAWINPUT_CONNECTED
82  };
83
84  struct PadState {
85    PadConnectionStatus status;
86    GamepadStandardMappingFunction mapper;
87
88    int xinput_index; // XInput-only
89    HANDLE raw_input_handle;  // RawInput-only fields.
90  };
91  PadState pad_state_[blink::WebGamepads::itemsLengthCap];
92
93  scoped_ptr<RawInputDataFetcher> raw_input_fetcher_;
94
95  DISALLOW_COPY_AND_ASSIGN(GamepadPlatformDataFetcherWin);
96};
97
98}  // namespace content
99
100#endif  // CONTENT_BROWSER_GAMEPAD_GAMEPAD_PLATFORM_DATA_FETCHER_WIN_H_
101