diff options
author | scottmg@chromium.org <scottmg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-10 19:24:28 +0000 |
---|---|---|
committer | scottmg@chromium.org <scottmg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-10 19:24:28 +0000 |
commit | 0a424afc9c94380ecd80967be73cfb684b6ae3f4 (patch) | |
tree | 8741005d16fd3313f9d9430d30bec6497cd63225 /ppapi | |
parent | 336648ae73fa2d95d5042b00503c406fee1a6d0e (diff) | |
download | chromium_src-0a424afc9c94380ecd80967be73cfb684b6ae3f4.zip chromium_src-0a424afc9c94380ecd80967be73cfb684b6ae3f4.tar.gz chromium_src-0a424afc9c94380ecd80967be73cfb684b6ae3f4.tar.bz2 |
Pepper gamepad support
BUG=79098
Review URL: http://codereview.chromium.org/9085027
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@117077 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ppapi')
-rw-r--r-- | ppapi/api/dev/ppb_gamepad_dev.idl | 87 | ||||
-rw-r--r-- | ppapi/c/dev/ppb_gamepad_dev.h | 106 | ||||
-rw-r--r-- | ppapi/examples/gamepad/gamepad.cc | 151 | ||||
-rw-r--r-- | ppapi/examples/gamepad/gamepad.html | 17 | ||||
-rw-r--r-- | ppapi/ppapi_shared.gypi | 1 | ||||
-rw-r--r-- | ppapi/ppapi_sources.gypi | 1 | ||||
-rw-r--r-- | ppapi/ppapi_tests.gypi | 13 | ||||
-rw-r--r-- | ppapi/proxy/ppb_instance_proxy.cc | 5 | ||||
-rw-r--r-- | ppapi/proxy/ppb_instance_proxy.h | 4 | ||||
-rw-r--r-- | ppapi/tests/all_c_includes.h | 1 | ||||
-rw-r--r-- | ppapi/thunk/interfaces_ppb_public_dev.h | 1 | ||||
-rw-r--r-- | ppapi/thunk/ppb_gamepad_thunk.cc | 34 | ||||
-rw-r--r-- | ppapi/thunk/ppb_instance_api.h | 7 |
13 files changed, 425 insertions, 3 deletions
diff --git a/ppapi/api/dev/ppb_gamepad_dev.idl b/ppapi/api/dev/ppb_gamepad_dev.idl new file mode 100644 index 0000000..209a21c --- /dev/null +++ b/ppapi/api/dev/ppb_gamepad_dev.idl @@ -0,0 +1,87 @@ +/* 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. + */ + +/** + * This file defines the <code>PPB_Gamepad_Dev</code> interface, which + * provides access to gamepad devices. + */ + +label Chrome { + M18 = 0.1 +}; + +/** + * The data for one gamepad device. + */ +struct PP_GamepadData_Dev { + /** + * Is there a gamepad connected at this index? If this is false, no other + * data in this structure is valid. + */ + char connected; + + /** + * String identifier for the type of device/manufacturer. + */ + uint16_t[128] id; + + /** + * Monotonically increasing value that is incremented when the data have + * been updated. + */ + uint64_t timestamp; + + /** + * Number of valid elements in the |axes| array. + */ + uint32_t axes_length; + + /** + * Normalized values for the axes, indices valid up to |axesLength|-1. Axis + * values range from -1..1, and are in order of "importance". + */ + float_t[16] axes; + + /** + * Number of valid elements in the |buttons| array. + */ + uint32_t buttons_length; + + /** + * Normalized values for the buttons, indices valid up to |buttonsLength| + * - 1. Button values range from 0..1, and are in order of importance. + */ + float_t[32] buttons; +}; + +/** + * The data for all gamepads connected to the system. + */ +struct PP_GamepadsData_Dev { + /** + * Number of valid elements in the |items| array. + */ + uint32_t length; + + /** + * Data for an individual gamepad device connected to the system. + */ + PP_GamepadData_Dev[4] items; +}; + +/** + * The <code>PPB_Gamepad_Dev</code> interface allows retrieving data from + * gamepad/joystick devices that are connected to the system. + */ +[version=0.1, macro="PPB_GAMEPAD_DEV_INTERFACE"] +interface PPB_Gamepad_Dev { + /** + * Samples the current state of the connected gamepads. + */ + void SampleGamepads( + [in] PP_Instance instance, + [out] PP_GamepadsData_Dev data); + +}; diff --git a/ppapi/c/dev/ppb_gamepad_dev.h b/ppapi/c/dev/ppb_gamepad_dev.h new file mode 100644 index 0000000..63197bc --- /dev/null +++ b/ppapi/c/dev/ppb_gamepad_dev.h @@ -0,0 +1,106 @@ +/* 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. + */ + +/* From dev/ppb_gamepad_dev.idl modified Mon Jan 9 13:16:43 2012. */ + +#ifndef PPAPI_C_DEV_PPB_GAMEPAD_DEV_H_ +#define PPAPI_C_DEV_PPB_GAMEPAD_DEV_H_ + +#include "ppapi/c/pp_instance.h" +#include "ppapi/c/pp_macros.h" +#include "ppapi/c/pp_stdint.h" + +#define PPB_GAMEPAD_DEV_INTERFACE_0_1 "PPB_Gamepad(Dev);0.1" +#define PPB_GAMEPAD_DEV_INTERFACE PPB_GAMEPAD_DEV_INTERFACE_0_1 + +/** + * @file + * This file defines the <code>PPB_Gamepad_Dev</code> interface, which + * provides access to gamepad devices. + */ + + +/** + * @addtogroup Structs + * @{ + */ +/** + * The data for one gamepad device. + */ +struct PP_GamepadData_Dev { + /** + * Is there a gamepad connected at this index? If this is false, no other + * data in this structure is valid. + */ + char connected; + /** + * String identifier for the type of device/manufacturer. + */ + uint16_t id[128]; + /** + * Monotonically increasing value that is incremented when the data have + * been updated. + */ + uint64_t timestamp; + /** + * Number of valid elements in the |axes| array. + */ + uint32_t axes_length; + /** + * Normalized values for the axes, indices valid up to |axesLength|-1. Axis + * values range from -1..1, and are in order of "importance". + */ + float axes[16]; + /** + * Number of valid elements in the |buttons| array. + */ + uint32_t buttons_length; + /** + * Normalized values for the buttons, indices valid up to |buttonsLength| + * - 1. Button values range from 0..1, and are in order of importance. + */ + float buttons[32]; +}; + +/** + * The data for all gamepads connected to the system. + */ +struct PP_GamepadsData_Dev { + /** + * Number of valid elements in the |items| array. + */ + uint32_t length; + /** + * Data for an individual gamepad device connected to the system. + */ + struct PP_GamepadData_Dev items[4]; +}; +/** + * @} + */ + +/** + * @addtogroup Interfaces + * @{ + */ +/** + * The <code>PPB_Gamepad_Dev</code> interface allows retrieving data from + * gamepad/joystick devices that are connected to the system. + */ +struct PPB_Gamepad_Dev_0_1 { + /** + * Samples the current state of the connected gamepads. + */ + void (*SampleGamepads)(PP_Instance instance, + struct PP_GamepadsData_Dev* data); +}; + +typedef struct PPB_Gamepad_Dev_0_1 PPB_Gamepad_Dev; +/** + * @} + */ + +#endif /* PPAPI_C_DEV_PPB_GAMEPAD_DEV_H_ */ + diff --git a/ppapi/examples/gamepad/gamepad.cc b/ppapi/examples/gamepad/gamepad.cc new file mode 100644 index 0000000..60ef589 --- /dev/null +++ b/ppapi/examples/gamepad/gamepad.cc @@ -0,0 +1,151 @@ +// 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 <cmath> +#include <stdarg.h> +#include <stdio.h> + +#include "ppapi/c/dev/ppb_console_dev.h" +#include "ppapi/c/dev/ppb_gamepad_dev.h" +#include "ppapi/c/ppb_input_event.h" +#include "ppapi/cpp/completion_callback.h" +#include "ppapi/cpp/graphics_2d.h" +#include "ppapi/cpp/image_data.h" +#include "ppapi/cpp/input_event.h" +#include "ppapi/cpp/instance.h" +#include "ppapi/cpp/logging.h" +#include "ppapi/cpp/module.h" +#include "ppapi/cpp/rect.h" +#include "ppapi/cpp/var.h" +#include "ppapi/cpp/view.h" +#include "ppapi/utility/completion_callback_factory.h" + +void FillRect(pp::ImageData* image, int left, int top, int width, int height, + uint32_t color) { + for (int y = std::max(0, top); + y < std::min(image->size().height() - 1, top + height); + y++) { + for (int x = std::max(0, left); + x < std::min(image->size().width() - 1, left + width); + x++) + *image->GetAddr32(pp::Point(x, y)) = color; + } +} + +class MyInstance : public pp::Instance { + public: + explicit MyInstance(PP_Instance instance) + : pp::Instance(instance), + width_(0), + height_(0), + callback_factory_(this), + gamepad_(NULL) { + } + virtual ~MyInstance() {} + + virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { + gamepad_ = reinterpret_cast<const PPB_Gamepad_Dev*>( + pp::Module::Get()->GetBrowserInterface(PPB_GAMEPAD_DEV_INTERFACE)); + if (!gamepad_) + return false; + + RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE | + PP_INPUTEVENT_CLASS_KEYBOARD); + return true; + } + + virtual void DidChangeView(const pp::View& view) { + pp::Rect rect = view.GetRect(); + if (rect.size().width() == width_ && + rect.size().height() == height_) + return; // We don't care about the position, only the size. + + width_ = rect.size().width(); + height_ = rect.size().height(); + + device_context_ = pp::Graphics2D(this, pp::Size(width_, height_), false); + if (!BindGraphics(device_context_)) + return; + + Paint(); + } + + void OnFlush(int32_t) { + Paint(); + } + + private: + void Paint() { + pp::ImageData image = PaintImage(device_context_.size()); + if (!image.is_null()) { + device_context_.ReplaceContents(&image); + device_context_.Flush( + callback_factory_.NewRequiredCallback(&MyInstance::OnFlush)); + } else { + printf("NullImage\n"); + } + } + + pp::ImageData PaintImage(const pp::Size& size) { + pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL, size, false); + if (image.is_null()) + return image; + + PP_GamepadsData_Dev gamepad_data; + gamepad_->SampleGamepads(pp_instance(), &gamepad_data); + + int width2 = size.width() / 2; + int height2 = size.height() / 2; + // Draw 2 axes + for (size_t i = 0; i < gamepad_data.items[0].axes_length; i += 2) { + int x = static_cast<int>( + gamepad_data.items[0].axes[i + 0] * width2 + width2); + int y = static_cast<int>( + gamepad_data.items[0].axes[i + 1] * height2 + height2); + uint32_t box_bgra = 0x80000000; // Alpha 50%. + FillRect(&image, x - 3, y - 3, 7, 7, box_bgra); + } + + for (size_t i = 0; i < gamepad_data.items[0].buttons_length; ++i) { + float button_val = gamepad_data.items[0].buttons[i]; + uint32_t colour = static_cast<uint32_t>((button_val * 192) + 63) << 24; + int x = i * 8 + 10; + int y = 10; + FillRect(&image, x - 3, y - 3, 7, 7, colour); + } + return image; + } + + int width_; + int height_; + + pp::CompletionCallbackFactory<MyInstance> callback_factory_; + + const PPB_Gamepad_Dev* gamepad_; + + pp::Graphics2D device_context_; +}; + +// This object is the global object representing this plugin library as long +// as it is loaded. +class MyModule : public pp::Module { + public: + MyModule() : pp::Module() {} + virtual ~MyModule() {} + + // Override CreateInstance to create your customized Instance object. + virtual pp::Instance* CreateInstance(PP_Instance instance) { + return new MyInstance(instance); + } +}; + +namespace pp { + +// Factory function for your specialization of the Module object. +Module* CreateModule() { + return new MyModule(); +} + +} // namespace pp + diff --git a/ppapi/examples/gamepad/gamepad.html b/ppapi/examples/gamepad/gamepad.html new file mode 100644 index 0000000..6e400dd --- /dev/null +++ b/ppapi/examples/gamepad/gamepad.html @@ -0,0 +1,17 @@ +<!DOCTYPE html> +<html> + <!-- + 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. + --> +<head> + <title>Gamepad Example</title> +</head> +<body> +<div> + <object id="plugin" type="application/x-ppapi-example-gamepad" + width="300" height="300" border="2px"></object> +</div> +</body> +</html> diff --git a/ppapi/ppapi_shared.gypi b/ppapi/ppapi_shared.gypi index 0cafb51..2802b5d 100644 --- a/ppapi/ppapi_shared.gypi +++ b/ppapi/ppapi_shared.gypi @@ -158,6 +158,7 @@ 'thunk/ppb_font_api.h', 'thunk/ppb_font_thunk.cc', 'thunk/ppb_fullscreen_thunk.cc', + 'thunk/ppb_gamepad_thunk.cc', 'thunk/ppb_gles_chromium_texture_mapping_thunk.cc', 'thunk/ppb_graphics_2d_api.h', 'thunk/ppb_graphics_2d_thunk.cc', diff --git a/ppapi/ppapi_sources.gypi b/ppapi/ppapi_sources.gypi index f517a12..a2c6884 100644 --- a/ppapi/ppapi_sources.gypi +++ b/ppapi/ppapi_sources.gypi @@ -61,6 +61,7 @@ 'c/dev/ppb_find_dev.h', 'c/dev/ppb_font_dev.h', 'c/dev/ppb_fullscreen_dev.h', + 'c/dev/ppb_gamepad_dev.h', 'c/dev/ppb_ime_input_event_dev.h', 'c/dev/ppb_memory_dev.h', 'c/dev/ppb_resource_array_dev.h', diff --git a/ppapi/ppapi_tests.gypi b/ppapi/ppapi_tests.gypi index 26d2527..0e23631 100644 --- a/ppapi/ppapi_tests.gypi +++ b/ppapi/ppapi_tests.gypi @@ -1,4 +1,4 @@ -# 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. @@ -200,6 +200,17 @@ }, { + 'target_name': 'ppapi_example_gamepad', + 'dependencies': [ + 'ppapi_example_skeleton', + 'ppapi.gyp:ppapi_cpp', + ], + 'sources': [ + 'examples/gamepad/gamepad.cc', + ], + }, + + { 'target_name': 'ppapi_example_c_stub', 'dependencies': [ 'ppapi_example_skeleton', diff --git a/ppapi/proxy/ppb_instance_proxy.cc b/ppapi/proxy/ppb_instance_proxy.cc index f8dd8a2..1a71966 100644 --- a/ppapi/proxy/ppb_instance_proxy.cc +++ b/ppapi/proxy/ppb_instance_proxy.cc @@ -267,6 +267,11 @@ PP_Bool PPB_Instance_Proxy::FlashGetScreenSize(PP_Instance instance, return result; } +void PPB_Instance_Proxy::SampleGamepads(PP_Instance instance, + PP_GamepadsData_Dev* data) { + NOTIMPLEMENTED(); +} + int32_t PPB_Instance_Proxy::RequestInputEvents(PP_Instance instance, uint32_t event_classes) { dispatcher()->Send(new PpapiHostMsg_PPBInstance_RequestInputEvents( diff --git a/ppapi/proxy/ppb_instance_proxy.h b/ppapi/proxy/ppb_instance_proxy.h index a88bd6f..0ce2562 100644 --- a/ppapi/proxy/ppb_instance_proxy.h +++ b/ppapi/proxy/ppb_instance_proxy.h @@ -1,4 +1,4 @@ -// 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. @@ -75,6 +75,8 @@ class PPB_Instance_Proxy : public InterfaceProxy, PP_Bool fullscreen) OVERRIDE; virtual PP_Bool FlashGetScreenSize(PP_Instance instance, PP_Size* size) OVERRIDE; + virtual void SampleGamepads(PP_Instance instance, PP_GamepadsData_Dev* data) + OVERRIDE; virtual int32_t RequestInputEvents(PP_Instance instance, uint32_t event_classes) OVERRIDE; virtual int32_t RequestFilteringInputEvents(PP_Instance instance, diff --git a/ppapi/tests/all_c_includes.h b/ppapi/tests/all_c_includes.h index 10b66d3..6189968 100644 --- a/ppapi/tests/all_c_includes.h +++ b/ppapi/tests/all_c_includes.h @@ -21,6 +21,7 @@ #include "ppapi/c/dev/ppb_find_dev.h" #include "ppapi/c/dev/ppb_font_dev.h" #include "ppapi/c/dev/ppb_fullscreen_dev.h" +#include "ppapi/c/dev/ppb_gamepad_dev.h" #include "ppapi/c/dev/ppb_ime_input_event_dev.h" #include "ppapi/c/dev/ppb_layer_compositor_dev.h" #include "ppapi/c/dev/ppb_memory_dev.h" diff --git a/ppapi/thunk/interfaces_ppb_public_dev.h b/ppapi/thunk/interfaces_ppb_public_dev.h index f72fa7c..de3b62b 100644 --- a/ppapi/thunk/interfaces_ppb_public_dev.h +++ b/ppapi/thunk/interfaces_ppb_public_dev.h @@ -43,6 +43,7 @@ PROXIED_IFACE(PPB_FileChooser, PPB_FILECHOOSER_DEV_INTERFACE_0_5, PROXIED_IFACE(PPB_Font, PPB_FONT_DEV_INTERFACE_0_6, PPB_Font_Dev_0_6) PROXIED_IFACE(PPB_Instance, PPB_CHAR_SET_DEV_INTERFACE_0_4, PPB_CharSet_Dev_0_4) PROXIED_IFACE(PPB_Instance, PPB_CONSOLE_DEV_INTERFACE_0_1, PPB_Console_Dev_0_1) +PROXIED_IFACE(PPB_Instance, PPB_GAMEPAD_DEV_INTERFACE_0_1, PPB_Gamepad_Dev_0_1) PROXIED_IFACE(PPB_Instance, PPB_URLUTIL_DEV_INTERFACE_0_6, PPB_URLUtil_Dev_0_6) UNPROXIED_IFACE(PPB_Instance, PPB_ZOOM_DEV_INTERFACE_0_2, PPB_Zoom_Dev_0_2) UNPROXIED_IFACE(PPB_LayerCompositor, PPB_LAYER_COMPOSITOR_DEV_INTERFACE_0_2, diff --git a/ppapi/thunk/ppb_gamepad_thunk.cc b/ppapi/thunk/ppb_gamepad_thunk.cc new file mode 100644 index 0000000..9bf2122 --- /dev/null +++ b/ppapi/thunk/ppb_gamepad_thunk.cc @@ -0,0 +1,34 @@ +// 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 "ppapi/c/dev/ppb_gamepad_dev.h" +#include "ppapi/thunk/thunk.h" +#include "ppapi/thunk/enter.h" +#include "ppapi/thunk/ppb_instance_api.h" +#include "ppapi/thunk/resource_creation_api.h" + +namespace ppapi { +namespace thunk { + +namespace { + +void SampleGamepads(PP_Instance instance, PP_GamepadsData_Dev* data) { + EnterFunction<PPB_Instance_FunctionAPI> enter(instance, true); + if (enter.failed()) + return; + enter.functions()->SampleGamepads(instance, data); +} + +const PPB_Gamepad_Dev g_ppb_gamepad_thunk = { + &SampleGamepads, +}; + +} // namespace + +const PPB_Gamepad_Dev* GetPPB_Gamepad_Dev_0_1_Thunk() { + return &g_ppb_gamepad_thunk; +} + +} // namespace thunk +} // namespace ppapi diff --git a/ppapi/thunk/ppb_instance_api.h b/ppapi/thunk/ppb_instance_api.h index 8151479..013f61b 100644 --- a/ppapi/thunk/ppb_instance_api.h +++ b/ppapi/thunk/ppb_instance_api.h @@ -1,10 +1,11 @@ -// 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. #ifndef PPAPI_THUNK_INSTANCE_API_H_ #define PPAPI_THUNK_INSTANCE_API_H_ +#include "ppapi/c/dev/ppb_gamepad_dev.h" #include "ppapi/c/dev/ppb_url_util_dev.h" #include "ppapi/c/pp_completion_callback.h" #include "ppapi/c/ppb_instance.h" @@ -71,6 +72,10 @@ class PPB_Instance_FunctionAPI { PP_Bool fullscreen) = 0; virtual PP_Bool FlashGetScreenSize(PP_Instance instance, PP_Size* size) = 0; + // Gamepad. + virtual void SampleGamepads(PP_Instance instance, + PP_GamepadsData_Dev* data) = 0; + // InputEvent. virtual int32_t RequestInputEvents(PP_Instance instance, uint32_t event_classes) = 0; |