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.
9 #include "ppapi/c/ppb_gamepad.h"
10 #include "ppapi/c/ppb_input_event.h"
11 #include "ppapi/cpp/completion_callback.h"
12 #include "ppapi/cpp/graphics_2d.h"
13 #include "ppapi/cpp/image_data.h"
14 #include "ppapi/cpp/input_event.h"
15 #include "ppapi/cpp/instance.h"
16 #include "ppapi/cpp/logging.h"
17 #include "ppapi/cpp/module.h"
18 #include "ppapi/cpp/rect.h"
19 #include "ppapi/cpp/var.h"
20 #include "ppapi/cpp/view.h"
21 #include "ppapi/utility/completion_callback_factory.h"
23 void FillRect(pp::ImageData
* image
, int left
, int top
, int width
, int height
,
25 for (int y
= std::max(0, top
);
26 y
< std::min(image
->size().height() - 1, top
+ height
);
28 for (int x
= std::max(0, left
);
29 x
< std::min(image
->size().width() - 1, left
+ width
);
31 *image
->GetAddr32(pp::Point(x
, y
)) = color
;
35 class MyInstance
: public pp::Instance
{
37 explicit MyInstance(PP_Instance instance
)
38 : pp::Instance(instance
),
41 callback_factory_(this),
44 virtual ~MyInstance() {}
46 virtual bool Init(uint32_t argc
, const char* argn
[], const char* argv
[]) {
47 gamepad_
= reinterpret_cast<const PPB_Gamepad
*>(
48 pp::Module::Get()->GetBrowserInterface(PPB_GAMEPAD_INTERFACE
));
54 virtual void DidChangeView(const pp::View
& view
) {
55 pp::Rect rect
= view
.GetRect();
56 if (rect
.size().width() == width_
&&
57 rect
.size().height() == height_
)
58 return; // We don't care about the position, only the size.
60 width_
= rect
.size().width();
61 height_
= rect
.size().height();
63 device_context_
= pp::Graphics2D(this, pp::Size(width_
, height_
), false);
64 if (!BindGraphics(device_context_
))
70 void OnFlush(int32_t) {
76 pp::ImageData image
= PaintImage(device_context_
.size());
77 if (!image
.is_null()) {
78 device_context_
.ReplaceContents(&image
);
79 device_context_
.Flush(
80 callback_factory_
.NewCallback(&MyInstance::OnFlush
));
82 printf("NullImage\n");
86 pp::ImageData
PaintImage(const pp::Size
& size
) {
87 pp::ImageData
image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL
, size
, true);
91 PP_GamepadsSampleData gamepad_data
;
92 gamepad_
->Sample(pp_instance(), &gamepad_data
);
94 if (gamepad_data
.length
> 1 && gamepad_data
.items
[0].connected
) {
95 int width2
= size
.width() / 2;
96 int height2
= size
.height() / 2;
98 for (size_t i
= 0; i
< gamepad_data
.items
[0].axes_length
; i
+= 2) {
99 int x
= static_cast<int>(
100 gamepad_data
.items
[0].axes
[i
+ 0] * width2
+ width2
);
101 int y
= static_cast<int>(
102 gamepad_data
.items
[0].axes
[i
+ 1] * height2
+ height2
);
103 uint32_t box_bgra
= 0x80000000; // Alpha 50%.
104 FillRect(&image
, x
- 3, y
- 3, 7, 7, box_bgra
);
107 for (size_t i
= 0; i
< gamepad_data
.items
[0].buttons_length
; ++i
) {
108 float button_val
= gamepad_data
.items
[0].buttons
[i
];
109 uint32_t colour
= static_cast<uint32_t>((button_val
* 192) + 63) << 24;
112 FillRect(&image
, x
- 3, y
- 3, 7, 7, colour
);
121 pp::CompletionCallbackFactory
<MyInstance
> callback_factory_
;
123 const PPB_Gamepad
* gamepad_
;
125 pp::Graphics2D device_context_
;
128 // This object is the global object representing this plugin library as long
130 class MyModule
: public pp::Module
{
132 MyModule() : pp::Module() {}
133 virtual ~MyModule() {}
135 // Override CreateInstance to create your customized Instance object.
136 virtual pp::Instance
* CreateInstance(PP_Instance instance
) {
137 return new MyInstance(instance
);
143 // Factory function for your specialization of the Module object.
144 Module
* CreateModule() {
145 return new MyModule();