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.
10 #include "ppapi/c/ppb_gamepad.h"
11 #include "ppapi/c/ppb_input_event.h"
12 #include "ppapi/cpp/completion_callback.h"
13 #include "ppapi/cpp/graphics_2d.h"
14 #include "ppapi/cpp/image_data.h"
15 #include "ppapi/cpp/input_event.h"
16 #include "ppapi/cpp/instance.h"
17 #include "ppapi/cpp/logging.h"
18 #include "ppapi/cpp/module.h"
19 #include "ppapi/cpp/rect.h"
20 #include "ppapi/cpp/var.h"
21 #include "ppapi/cpp/view.h"
22 #include "ppapi/utility/completion_callback_factory.h"
24 void FillRect(pp::ImageData
* image
, int left
, int top
, int width
, int height
,
26 for (int y
= std::max(0, top
);
27 y
< std::min(image
->size().height() - 1, top
+ height
);
29 for (int x
= std::max(0, left
);
30 x
< std::min(image
->size().width() - 1, left
+ width
);
32 *image
->GetAddr32(pp::Point(x
, y
)) = color
;
36 class MyInstance
: public pp::Instance
{
38 explicit MyInstance(PP_Instance instance
)
39 : pp::Instance(instance
),
42 callback_factory_(this),
45 virtual ~MyInstance() {}
47 virtual bool Init(uint32_t argc
, const char* argn
[], const char* argv
[]) {
48 gamepad_
= reinterpret_cast<const PPB_Gamepad
*>(
49 pp::Module::Get()->GetBrowserInterface(PPB_GAMEPAD_INTERFACE
));
55 virtual void DidChangeView(const pp::View
& view
) {
56 pp::Rect rect
= view
.GetRect();
57 if (rect
.size().width() == width_
&&
58 rect
.size().height() == height_
)
59 return; // We don't care about the position, only the size.
61 width_
= rect
.size().width();
62 height_
= rect
.size().height();
64 device_context_
= pp::Graphics2D(this, pp::Size(width_
, height_
), false);
65 if (!BindGraphics(device_context_
))
71 void OnFlush(int32_t) {
72 // This plugin continuously paints because it continously samples the
73 // gamepad and paints its updated state.
79 pp::ImageData image
= PaintImage(device_context_
.size());
80 if (!image
.is_null()) {
81 device_context_
.ReplaceContents(&image
);
82 device_context_
.Flush(
83 callback_factory_
.NewCallback(&MyInstance::OnFlush
));
87 pp::ImageData
PaintImage(const pp::Size
& size
) {
88 pp::ImageData
image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL
, size
, true);
92 PP_GamepadsSampleData gamepad_data
;
93 gamepad_
->Sample(pp_instance(), &gamepad_data
);
95 if (gamepad_data
.length
> 0 && gamepad_data
.items
[0].connected
) {
96 int width2
= size
.width() / 2;
97 int height2
= size
.height() / 2;
99 for (size_t i
= 0; i
< gamepad_data
.items
[0].axes_length
; i
+= 2) {
100 int x
= static_cast<int>(
101 gamepad_data
.items
[0].axes
[i
+ 0] * width2
+ width2
);
102 int y
= static_cast<int>(
103 gamepad_data
.items
[0].axes
[i
+ 1] * height2
+ height2
);
104 uint32_t box_bgra
= 0x80000000; // Alpha 50%.
105 FillRect(&image
, x
- 3, y
- 3, 7, 7, box_bgra
);
108 for (size_t i
= 0; i
< gamepad_data
.items
[0].buttons_length
; ++i
) {
109 float button_val
= gamepad_data
.items
[0].buttons
[i
];
110 uint32_t colour
= static_cast<uint32_t>((button_val
* 192) + 63) << 24;
111 int x
= static_cast<int>(i
) * 8 + 10;
113 FillRect(&image
, x
- 3, y
- 3, 7, 7, colour
);
122 pp::CompletionCallbackFactory
<MyInstance
> callback_factory_
;
124 const PPB_Gamepad
* gamepad_
;
126 pp::Graphics2D device_context_
;
129 // This object is the global object representing this plugin library as long
131 class MyModule
: public pp::Module
{
133 MyModule() : pp::Module() {}
134 virtual ~MyModule() {}
136 // Override CreateInstance to create your customized Instance object.
137 virtual pp::Instance
* CreateInstance(PP_Instance instance
) {
138 return new MyInstance(instance
);
144 // Factory function for your specialization of the Module object.
145 Module
* CreateModule() {
146 return new MyModule();