OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "examples/mouselock/mouselock.h" |
| 6 |
| 7 #include <cmath> |
| 8 #include <cstdlib> |
| 9 #include <stdarg.h> |
| 10 #include <stdio.h> |
| 11 #include <string.h> |
| 12 |
| 13 // Indicate the direction of the mouse location relative to the center of the |
| 14 // view. These values are used to determine which 2D quadrant the needle lies |
| 15 // in. |
| 16 typedef enum { |
| 17 kLeft = 0, |
| 18 kRight = 1, |
| 19 kUp = 2, |
| 20 kDown = 3 |
| 21 } MouseDirection; |
| 22 |
| 23 namespace { |
| 24 const int kCentralSpotRadius = 5; |
| 25 const uint32_t kReturnKeyCode = 13; |
| 26 const uint32_t kBackgroundColor = 0xff606060; |
| 27 const uint32_t kLockedForegroundColor = 0xfff08080; |
| 28 const uint32_t kUnlockedForegroundColor = 0xff80f080; |
| 29 } // namespace |
| 30 |
| 31 namespace mouselock { |
| 32 |
| 33 MouseLockInstance::~MouseLockInstance() { |
| 34 free(background_scanline_); |
| 35 background_scanline_ = NULL; |
| 36 } |
| 37 |
| 38 bool MouseLockInstance::Init(uint32_t argc, |
| 39 const char* argn[], |
| 40 const char* argv[]) { |
| 41 RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE | |
| 42 PP_INPUTEVENT_CLASS_KEYBOARD); |
| 43 return true; |
| 44 } |
| 45 |
| 46 bool MouseLockInstance::HandleInputEvent(const pp::InputEvent& event) { |
| 47 switch (event.GetType()) { |
| 48 case PP_INPUTEVENT_TYPE_MOUSEDOWN: { |
| 49 is_context_bound_ = false; |
| 50 if (fullscreen_.IsFullscreen()) { |
| 51 // Leaving fullscreen mode also unlocks the mouse if it was locked. |
| 52 // In this case, the browser will call MouseLockLost() on this |
| 53 // instance. |
| 54 if (!fullscreen_.SetFullscreen(false)) { |
| 55 Log("Could not leave fullscreen mode\n"); |
| 56 } |
| 57 } else { |
| 58 if (!fullscreen_.SetFullscreen(true)) { |
| 59 Log("Could not set fullscreen mode\n"); |
| 60 } else { |
| 61 pp::MouseInputEvent mouse_event(event); |
| 62 if (mouse_event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_LEFT && |
| 63 !mouse_locked_) { |
| 64 LockMouse(callback_factory_.NewRequiredCallback( |
| 65 &MouseLockInstance::DidLockMouse)); |
| 66 } |
| 67 } |
| 68 } |
| 69 return true; |
| 70 } |
| 71 |
| 72 case PP_INPUTEVENT_TYPE_MOUSEMOVE: { |
| 73 pp::MouseInputEvent mouse_event(event); |
| 74 mouse_movement_ = mouse_event.GetMovement(); |
| 75 Paint(); |
| 76 return true; |
| 77 } |
| 78 |
| 79 case PP_INPUTEVENT_TYPE_KEYDOWN: { |
| 80 pp::KeyboardInputEvent key_event(event); |
| 81 // Lock the mouse when the Enter key is pressed. |
| 82 if (key_event.GetKeyCode() == kReturnKeyCode) { |
| 83 if (mouse_locked_) { |
| 84 UnlockMouse(); |
| 85 } else { |
| 86 LockMouse(callback_factory_.NewRequiredCallback( |
| 87 &MouseLockInstance::DidLockMouse)); |
| 88 } |
| 89 } |
| 90 return true; |
| 91 } |
| 92 |
| 93 case PP_INPUTEVENT_TYPE_MOUSEUP: |
| 94 case PP_INPUTEVENT_TYPE_MOUSEENTER: |
| 95 case PP_INPUTEVENT_TYPE_MOUSELEAVE: |
| 96 case PP_INPUTEVENT_TYPE_WHEEL: |
| 97 case PP_INPUTEVENT_TYPE_RAWKEYDOWN: |
| 98 case PP_INPUTEVENT_TYPE_KEYUP: |
| 99 case PP_INPUTEVENT_TYPE_CHAR: |
| 100 case PP_INPUTEVENT_TYPE_CONTEXTMENU: |
| 101 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_START: |
| 102 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_UPDATE: |
| 103 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_END: |
| 104 case PP_INPUTEVENT_TYPE_IME_TEXT: |
| 105 case PP_INPUTEVENT_TYPE_UNDEFINED: |
| 106 default: |
| 107 return false; |
| 108 } |
| 109 } |
| 110 |
| 111 void MouseLockInstance::DidChangeView(const pp::Rect& position, |
| 112 const pp::Rect& clip) { |
| 113 int width = position.size().width(); |
| 114 int height = position.size().height(); |
| 115 |
| 116 // When entering into full-screen mode, DidChangeView() gets called twice. |
| 117 // The first time, any 2D context will fail to bind to this pp::Instacne. |
| 118 if (width == width_ && height == height_ && is_context_bound_) { |
| 119 return; |
| 120 } |
| 121 width_ = width; |
| 122 height_ = height; |
| 123 |
| 124 device_context_ = pp::Graphics2D(this, pp::Size(width_, height_), false); |
| 125 waiting_for_flush_completion_ = false; |
| 126 free(background_scanline_); |
| 127 background_scanline_ = NULL; |
| 128 is_context_bound_ = BindGraphics(device_context_); |
| 129 if (!is_context_bound_) { |
| 130 Log("Could not bind to 2D context\n"); |
| 131 return; |
| 132 } |
| 133 background_scanline_ = static_cast<uint32_t*>( |
| 134 malloc(width_ * sizeof(*background_scanline_))); |
| 135 uint32_t* bg_pixel = background_scanline_; |
| 136 for (int x = 0; x < width_; ++x) { |
| 137 *bg_pixel++ = kBackgroundColor; |
| 138 } |
| 139 Paint(); |
| 140 } |
| 141 |
| 142 void MouseLockInstance::MouseLockLost() { |
| 143 if (mouse_locked_) { |
| 144 mouse_locked_ = false; |
| 145 Paint(); |
| 146 } else { |
| 147 PP_NOTREACHED(); |
| 148 } |
| 149 } |
| 150 |
| 151 void MouseLockInstance::DidLockMouse(int32_t result) { |
| 152 mouse_locked_ = result == PP_OK; |
| 153 mouse_movement_.set_x(0); |
| 154 mouse_movement_.set_y(0); |
| 155 Paint(); |
| 156 } |
| 157 |
| 158 void MouseLockInstance::DidFlush(int32_t result) { |
| 159 waiting_for_flush_completion_ = false; |
| 160 } |
| 161 |
| 162 void MouseLockInstance::Paint() { |
| 163 if (waiting_for_flush_completion_) { |
| 164 return; |
| 165 } |
| 166 pp::ImageData image = PaintImage(width_, height_); |
| 167 if (image.is_null()) { |
| 168 Log("Could not create image data\n"); |
| 169 return; |
| 170 } |
| 171 device_context_.ReplaceContents(&image); |
| 172 waiting_for_flush_completion_ = true; |
| 173 device_context_.Flush( |
| 174 callback_factory_.NewRequiredCallback(&MouseLockInstance::DidFlush)); |
| 175 } |
| 176 |
| 177 pp::ImageData MouseLockInstance::PaintImage(int width, int height) { |
| 178 pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL, |
| 179 pp::Size(width, height), false); |
| 180 if (image.is_null() || image.data() == NULL) |
| 181 return image; |
| 182 |
| 183 ClearToBackground(&image); |
| 184 uint32_t foreground_color = mouse_locked_ ? kLockedForegroundColor : |
| 185 kUnlockedForegroundColor; |
| 186 DrawCenterSpot(&image, foreground_color); |
| 187 DrawNeedle(&image, foreground_color); |
| 188 return image; |
| 189 } |
| 190 |
| 191 void MouseLockInstance::ClearToBackground(pp::ImageData* image) { |
| 192 if (image == NULL) { |
| 193 Log("ClearToBackground with NULL image"); |
| 194 return; |
| 195 } |
| 196 if (background_scanline_ == NULL) |
| 197 return; |
| 198 int image_height = image->size().height(); |
| 199 int image_width = image->size().width(); |
| 200 for (int y = 0; y < image_height; ++y) { |
| 201 uint32_t* scanline = image->GetAddr32(pp::Point(0, y)); |
| 202 memcpy(scanline, |
| 203 background_scanline_, |
| 204 image_width * sizeof(*background_scanline_)); |
| 205 } |
| 206 } |
| 207 |
| 208 void MouseLockInstance::DrawCenterSpot(pp::ImageData* image, |
| 209 uint32_t spot_color) { |
| 210 if (image == NULL) { |
| 211 Log("DrawCenterSpot with NULL image"); |
| 212 return; |
| 213 } |
| 214 // Draw the center spot. The ROI is bounded by the size of the spot, plus |
| 215 // one pixel. |
| 216 int center_x = image->size().width() / 2; |
| 217 int center_y = image->size().height() / 2; |
| 218 int region_of_interest_radius = kCentralSpotRadius + 1; |
| 219 |
| 220 for (int y = center_y - region_of_interest_radius; |
| 221 y < center_y + region_of_interest_radius; |
| 222 ++y) { |
| 223 for (int x = center_x - region_of_interest_radius; |
| 224 x < center_x + region_of_interest_radius; |
| 225 ++x) { |
| 226 if (GetDistance(x, y, center_x, center_y) < kCentralSpotRadius) { |
| 227 *image->GetAddr32(pp::Point(x, y)) = spot_color; |
| 228 } |
| 229 } |
| 230 } |
| 231 } |
| 232 |
| 233 void MouseLockInstance::DrawNeedle(pp::ImageData* image, |
| 234 uint32_t needle_color) { |
| 235 if (image == NULL) { |
| 236 Log("DrawNeedle with NULL image"); |
| 237 return; |
| 238 } |
| 239 if (GetDistance(mouse_movement_.x(), mouse_movement_.y(), 0, 0) <= |
| 240 kCentralSpotRadius) { |
| 241 return; |
| 242 } |
| 243 |
| 244 int abs_mouse_x = std::abs(mouse_movement_.x()); |
| 245 int abs_mouse_y = std::abs(mouse_movement_.y()); |
| 246 int center_x = image->size().width() / 2; |
| 247 int center_y = image->size().height() / 2; |
| 248 pp::Point vertex(mouse_movement_.x() + center_x, |
| 249 mouse_movement_.y() + center_y); |
| 250 pp::Point anchor_1; |
| 251 pp::Point anchor_2; |
| 252 MouseDirection direction = kLeft; |
| 253 |
| 254 if (abs_mouse_x >= abs_mouse_y) { |
| 255 anchor_1.set_x(center_x); |
| 256 anchor_1.set_y(center_y - kCentralSpotRadius); |
| 257 anchor_2.set_x(center_x); |
| 258 anchor_2.set_y(center_y + kCentralSpotRadius); |
| 259 direction = (mouse_movement_.x() < 0) ? kLeft : kRight; |
| 260 if (direction == kLeft) |
| 261 anchor_1.swap(anchor_2); |
| 262 } else { |
| 263 anchor_1.set_x(center_x + kCentralSpotRadius); |
| 264 anchor_1.set_y(center_y); |
| 265 anchor_2.set_x(center_x - kCentralSpotRadius); |
| 266 anchor_2.set_y(center_y); |
| 267 direction = (mouse_movement_.y() < 0) ? kUp : kDown; |
| 268 if (direction == kUp) |
| 269 anchor_1.swap(anchor_2); |
| 270 } |
| 271 |
| 272 for (int y = center_y - abs_mouse_y; y < center_y + abs_mouse_y; ++y) { |
| 273 for (int x = center_x - abs_mouse_x; x < center_x + abs_mouse_x; ++x) { |
| 274 bool within_bound_1 = |
| 275 ((y - anchor_1.y()) * (vertex.x() - anchor_1.x())) > |
| 276 ((vertex.y() - anchor_1.y()) * (x - anchor_1.x())); |
| 277 bool within_bound_2 = |
| 278 ((y - anchor_2.y()) * (vertex.x() - anchor_2.x())) < |
| 279 ((vertex.y() - anchor_2.y()) * (x - anchor_2.x())); |
| 280 bool within_bound_3 = |
| 281 (direction == kUp && y < center_y) || |
| 282 (direction == kDown && y > center_y) || |
| 283 (direction == kLeft && x < center_x) || |
| 284 (direction == kRight && x > center_x); |
| 285 |
| 286 if (within_bound_1 && within_bound_2 && within_bound_3) { |
| 287 *image->GetAddr32(pp::Point(x, y)) = needle_color; |
| 288 } |
| 289 } |
| 290 } |
| 291 } |
| 292 |
| 293 |
| 294 void MouseLockInstance::Log(const char* format, ...) { |
| 295 va_list args; |
| 296 va_start(args, format); |
| 297 char buf[512]; |
| 298 vsnprintf(buf, sizeof(buf) - 1, format, args); |
| 299 buf[sizeof(buf) - 1] = '\0'; |
| 300 va_end(args); |
| 301 |
| 302 pp::Var value(buf); |
| 303 PostMessage(value); |
| 304 } |
| 305 |
| 306 } // namespace mouselock |
| 307 |
| 308 // This object is the global object representing this plugin library as long |
| 309 // as it is loaded. |
| 310 class MouseLockModule : public pp::Module { |
| 311 public: |
| 312 MouseLockModule() : pp::Module() {} |
| 313 virtual ~MouseLockModule() {} |
| 314 |
| 315 // Override CreateInstance to create your customized Instance object. |
| 316 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
| 317 return new mouselock::MouseLockInstance(instance); |
| 318 } |
| 319 }; |
| 320 |
| 321 namespace pp { |
| 322 |
| 323 // Factory function for your specialization of the Module object. |
| 324 Module* CreateModule() { |
| 325 return new MouseLockModule(); |
| 326 } |
| 327 |
| 328 } // namespace pp |
| 329 |
OLD | NEW |