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#include <algorithm>
6#include <cmath>
7#include <stdarg.h>
8#include <stdio.h>
9
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"
23
24void FillRect(pp::ImageData* image, int left, int top, int width, int height,
25              uint32_t color) {
26  for (int y = std::max(0, top);
27       y < std::min(image->size().height() - 1, top + height);
28       y++) {
29    for (int x = std::max(0, left);
30         x < std::min(image->size().width() - 1, left + width);
31         x++)
32      *image->GetAddr32(pp::Point(x, y)) = color;
33  }
34}
35
36class MyInstance : public pp::Instance {
37 public:
38  explicit MyInstance(PP_Instance instance)
39      : pp::Instance(instance),
40        width_(0),
41        height_(0),
42        callback_factory_(this),
43        gamepad_(NULL) {
44  }
45  virtual ~MyInstance() {}
46
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));
50    if (!gamepad_)
51      return false;
52    return true;
53  }
54
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.
60
61    width_ = rect.size().width();
62    height_ = rect.size().height();
63
64    device_context_ = pp::Graphics2D(this, pp::Size(width_, height_), false);
65    if (!BindGraphics(device_context_))
66      return;
67
68    Paint();
69  }
70
71  void OnFlush(int32_t) {
72    Paint();
73  }
74
75 private:
76  void Paint() {
77    pp::ImageData image = PaintImage(device_context_.size());
78    if (!image.is_null()) {
79      device_context_.ReplaceContents(&image);
80      device_context_.Flush(
81          callback_factory_.NewCallback(&MyInstance::OnFlush));
82    } else {
83      printf("NullImage\n");
84    }
85  }
86
87  pp::ImageData PaintImage(const pp::Size& size) {
88    pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL, size, true);
89    if (image.is_null())
90      return image;
91
92    PP_GamepadsSampleData gamepad_data;
93    gamepad_->Sample(pp_instance(), &gamepad_data);
94
95    if (gamepad_data.length > 1 && gamepad_data.items[0].connected) {
96      int width2 = size.width() / 2;
97      int height2 = size.height() / 2;
98      // Draw 2 axes
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);
106      }
107
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 = i * 8 + 10;
112        int y = 10;
113        FillRect(&image, x - 3, y - 3, 7, 7, colour);
114      }
115    }
116    return image;
117  }
118
119  int width_;
120  int height_;
121
122  pp::CompletionCallbackFactory<MyInstance> callback_factory_;
123
124  const PPB_Gamepad* gamepad_;
125
126  pp::Graphics2D device_context_;
127};
128
129// This object is the global object representing this plugin library as long
130// as it is loaded.
131class MyModule : public pp::Module {
132 public:
133  MyModule() : pp::Module() {}
134  virtual ~MyModule() {}
135
136  // Override CreateInstance to create your customized Instance object.
137  virtual pp::Instance* CreateInstance(PP_Instance instance) {
138    return new MyInstance(instance);
139  }
140};
141
142namespace pp {
143
144// Factory function for your specialization of the Module object.
145Module* CreateModule() {
146  return new MyModule();
147}
148
149}  // namespace pp
150
151