Commit 2a92b328 authored by thestig@chromium.org's avatar thestig@chromium.org

Cleanup: More random cleanup for gamepad code.

BUG=none
TEST=none

Review URL: http://codereview.chromium.org/9514017

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@124398 0039d316-1c4b-4281-b951-d872f2087c98
parent 45e29542
......@@ -14,6 +14,7 @@
#include "content/browser/gamepad/data_fetcher.h"
#include "content/browser/gamepad/gamepad_provider.h"
#include "content/browser/gamepad/platform_data_fetcher.h"
#include "content/common/gamepad_hardware_buffer.h"
#include "content/common/gamepad_messages.h"
#include "content/public/browser/browser_thread.h"
......
......@@ -11,18 +11,17 @@
#include "base/shared_memory.h"
#include "base/synchronization/lock.h"
#include "base/system_monitor/system_monitor.h"
#include "content/browser/gamepad/data_fetcher.h"
#include "content/common/content_export.h"
#include "content/common/gamepad_hardware_buffer.h"
namespace base {
class Thread;
}
struct GamepadMsg_Updated_Params;
namespace content {
class GamepadDataFetcher;
struct GamepadHardwareBuffer;
class CONTENT_EXPORT GamepadProvider :
public base::RefCountedThreadSafe<GamepadProvider>,
public base::SystemMonitor::DevicesChangedObserver {
......@@ -37,6 +36,9 @@ class CONTENT_EXPORT GamepadProvider :
void Pause();
void Resume();
// base::SystemMonitor::DevicesChangedObserver implementation.
virtual void OnDevicesChanged() OVERRIDE;
private:
friend class base::RefCountedThreadSafe<GamepadProvider>;
......@@ -49,8 +51,6 @@ class CONTENT_EXPORT GamepadProvider :
void DoPoll();
void ScheduleDoPoll();
virtual void OnDevicesChanged() OVERRIDE;
GamepadHardwareBuffer* SharedMemoryAsHardwareBuffer();
enum { kDesiredSamplingIntervalMs = 16 };
......
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// 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.
......@@ -6,9 +6,12 @@
#include "base/process_util.h"
#include "base/synchronization/waitable_event.h"
#include "base/system_monitor/system_monitor.h"
#include "content/browser/gamepad/data_fetcher.h"
#include "content/browser/gamepad/gamepad_provider.h"
#include "content/common/gamepad_hardware_buffer.h"
#include "content/common/gamepad_messages.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebGamepads.h"
namespace content {
......@@ -18,8 +21,9 @@ using WebKit::WebGamepads;
class MockDataFetcher : public GamepadDataFetcher {
public:
MockDataFetcher(WebGamepads& test_data) : read_data_(false, false) {
test_data_ = test_data;
explicit MockDataFetcher(const WebGamepads& test_data)
: test_data_(test_data),
read_data_(false, false) {
}
virtual void GetGamepadData(WebGamepads* pads,
bool devices_changed_hint) OVERRIDE {
......@@ -36,7 +40,7 @@ class MockDataFetcher : public GamepadDataFetcher {
// Main test fixture
class GamepadProviderTest : public testing::Test {
public:
GamepadProvider* CreateProvider(WebGamepads& test_data) {
GamepadProvider* CreateProvider(const WebGamepads& test_data) {
#if defined(OS_MACOSX)
base::SystemMonitor::AllocateSystemIOPorts();
#endif
......
......@@ -30,6 +30,33 @@ void CloseFileDescriptorIfValid(int fd) {
close(fd);
}
bool IsGamepad(udev_device* dev, int* index, std::string* path) {
if (!udev_device_get_property_value(dev, "ID_INPUT_JOYSTICK"))
return false;
const char* node_path = udev_device_get_devnode(dev);
if (!node_path)
return false;
static const char kJoystickRoot[] = "/dev/input/js";
bool is_gamepad = StartsWithASCII(node_path, kJoystickRoot, true);
if (!is_gamepad)
return false;
int tmp_idx = -1;
const int base_len = sizeof(kJoystickRoot) - 1;
base::StringPiece str(&node_path[base_len], strlen(node_path) - base_len);
if (!base::StringToInt(str, &tmp_idx))
return false;
if (tmp_idx < 0 ||
tmp_idx >= static_cast<int>(WebKit::WebGamepads::itemsLengthCap)) {
return false;
}
*index = tmp_idx;
*path = node_path;
return true;
}
} // namespace
namespace content {
......@@ -108,33 +135,6 @@ void GamepadPlatformDataFetcherLinux::OnFileCanReadWithoutBlocking(int fd) {
void GamepadPlatformDataFetcherLinux::OnFileCanWriteWithoutBlocking(int fd) {
}
bool GamepadPlatformDataFetcherLinux::IsGamepad(udev_device* dev,
int* index,
std::string* path) {
if (!udev_device_get_property_value(dev, "ID_INPUT_JOYSTICK"))
return false;
const char* node_path = udev_device_get_devnode(dev);
if (!node_path)
return false;
static const char kJoystickRoot[] = "/dev/input/js";
bool is_gamepad = StartsWithASCII(node_path, kJoystickRoot, true);
if (!is_gamepad)
return false;
int tmp_idx = -1;
const int base_len = sizeof(kJoystickRoot) - 1;
base::StringPiece str(&node_path[base_len], strlen(node_path) - base_len);
if (!base::StringToInt(str, &tmp_idx))
return false;
if (tmp_idx < 0 || tmp_idx >= static_cast<int>(WebGamepads::itemsLengthCap))
return false;
*index = tmp_idx;
*path = node_path;
return true;
}
// Used during enumeration, and monitor notifications.
void GamepadPlatformDataFetcherLinux::RefreshDevice(udev_device* dev) {
int index;
......@@ -228,7 +228,7 @@ void GamepadPlatformDataFetcherLinux::ReadDeviceData(size_t index) {
return;
}
int& fd = device_fds_[index];
const int& fd = device_fds_[index];
WebGamepad& pad = data_.items[index];
DCHECK_GE(fd, 0);
......
......@@ -39,7 +39,6 @@ class GamepadPlatformDataFetcherLinux :
virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE;
private:
bool IsGamepad(udev_device* dev, int* index, std::string* path);
void RefreshDevice(udev_device* dev);
void EnumerateDevices();
void ReadDeviceData(size_t index);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment