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