Chromium Code Reviews| Index: examples/mouselock/mouselock.cc |
| =================================================================== |
| --- examples/mouselock/mouselock.cc (revision 0) |
| +++ examples/mouselock/mouselock.cc (revision 0) |
| @@ -0,0 +1,269 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <cmath> |
| +#include <stdarg.h> |
| +#include <stdio.h> |
| + |
| +#include "examples/mouselock/mouselock.h" |
|
cstefansen
2011/11/10 17:53:03
Nit: Google style suggests putting this include fi
der Springer
2011/11/11 19:23:01
Done.
|
| + |
| +typedef enum { |
|
gwink1
2011/11/10 14:54:33
It would be nice to know what these are used for.
der Springer
2011/11/11 19:23:01
Done.
|
| + kLeft = 0, |
| + kRight = 1, |
| + kUp = 2, |
| + kDown = 3 |
| +} MouseDirection; |
| + |
| +namespace { |
| +const int kCentralSpotRadius = 5; |
| +const uint32_t kReturnKeyCode = 13; |
| +const uint32_t kBackgroundColor = 0xff606060; |
| +const uint32_t kLockedForegroundColor = 0xfff08080; |
| +const uint32_t kUnlockedForegroundColor = 0xff80f080; |
| +} // namespace |
| + |
| +namespace mouselock { |
| + |
| +bool MouseLockInstance::Init(uint32_t argc, |
| + const char* argn[], |
| + const char* argv[]) { |
| + RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE | |
| + PP_INPUTEVENT_CLASS_KEYBOARD); |
| + return true; |
| +} |
| + |
| +bool MouseLockInstance::HandleInputEvent(const pp::InputEvent& event) { |
| + switch (event.GetType()) { |
| + case PP_INPUTEVENT_TYPE_UNDEFINED: |
|
cstefansen
2011/11/10 17:53:03
Why not move this one down to the list of all the
der Springer
2011/11/11 19:23:01
Done.
|
| + break; |
| + |
| + case PP_INPUTEVENT_TYPE_MOUSEDOWN: { |
| + if (fullscreen_.IsFullscreen()) { |
| + // Leaving fullscreen mode also unlocks the mouse if it was locked. |
| + // In this case, the browser will call MouseLockLost() on this |
| + // instance. |
| + fullscreen_pending_ = false; |
| + if (!fullscreen_.SetFullscreen(false)) { |
| + Log("Could not leave fullscreen mode\n"); |
| + } |
| + } else { |
| + if (!fullscreen_.SetFullscreen(true)) { |
| + Log("Could not set fullscreen mode\n"); |
| + } else { |
| + fullscreen_pending_ = true; |
| + pp::MouseInputEvent mouse_event(event); |
| + if (mouse_event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_LEFT && |
| + !mouse_locked_) { |
| + LockMouse(callback_factory_.NewRequiredCallback( |
| + &MouseLockInstance::DidLockMouse)); |
| + } |
| + } |
| + } |
| + return true; |
| + } |
| + |
| + case PP_INPUTEVENT_TYPE_MOUSEMOVE: { |
| + pp::MouseInputEvent mouse_event(event); |
| + mouse_movement_ = mouse_event.GetMovement(); |
| + Paint(); |
| + return true; |
| + } |
| + |
| + case PP_INPUTEVENT_TYPE_KEYDOWN: { |
| + pp::KeyboardInputEvent key_event(event); |
| + // Lock the mouse when the Enter key is pressed. |
| + if (key_event.GetKeyCode() == kReturnKeyCode) { |
| + if (mouse_locked_) { |
| + UnlockMouse(); |
| + } else { |
| + LockMouse(callback_factory_.NewRequiredCallback( |
| + &MouseLockInstance::DidLockMouse)); |
| + } |
| + } |
| + return true; |
| + } |
| + |
| + case PP_INPUTEVENT_TYPE_MOUSEUP: |
| + case PP_INPUTEVENT_TYPE_MOUSEENTER: |
| + case PP_INPUTEVENT_TYPE_MOUSELEAVE: |
| + case PP_INPUTEVENT_TYPE_WHEEL: |
| + case PP_INPUTEVENT_TYPE_RAWKEYDOWN: |
| + case PP_INPUTEVENT_TYPE_KEYUP: |
| + case PP_INPUTEVENT_TYPE_CHAR: |
| + case PP_INPUTEVENT_TYPE_CONTEXTMENU: |
| + case PP_INPUTEVENT_TYPE_IME_COMPOSITION_START: |
| + case PP_INPUTEVENT_TYPE_IME_COMPOSITION_UPDATE: |
| + case PP_INPUTEVENT_TYPE_IME_COMPOSITION_END: |
| + case PP_INPUTEVENT_TYPE_IME_TEXT: |
| + default: |
| + break; |
|
Matt Ball
2011/11/10 15:03:11
If you're mixing some cases using 'break' and othe
der Springer
2011/11/11 19:23:01
Done.
|
| + } |
| + return false; |
| +} |
| + |
| +void MouseLockInstance::DidChangeView(const pp::Rect& position, |
| + const pp::Rect& clip) { |
| + if (fullscreen_pending_ && !saw_first_didchangeview_) { |
|
gwink1
2011/11/10 14:54:33
One comment I got from Polina, on the fullscreen s
der Springer
2011/11/11 19:23:01
Done.
|
| + saw_first_didchangeview_ = true; |
| + // Wait for the 2nd DidChangeView. |
| + return; |
| + } else if (fullscreen_pending_) { |
| + fullscreen_pending_ = false; |
| + saw_first_didchangeview_ = false; // Reset. |
| + } |
| + |
| + int width = position.size().width(); |
| + int height = position.size().height(); |
| + if (width == width_ && height == height_) { |
| + return; |
| + } |
| + width_ = width; |
| + height_ = height; |
| + |
| + device_context_ = pp::Graphics2D(this, pp::Size(width_, height_), false); |
| + waiting_for_flush_completion_ = false; |
| + if (!BindGraphics(device_context_)) { |
|
cstefansen
2011/11/10 17:53:03
Maybe issue a log message here?
der Springer
2011/11/11 19:23:01
Done.
|
| + return; |
| + } |
| + |
| + Paint(); |
| +} |
| + |
| +void MouseLockInstance::MouseLockLost() { |
| + if (mouse_locked_) { |
| + mouse_locked_ = false; |
| + Paint(); |
| + } else { |
| + PP_NOTREACHED(); |
| + } |
| +} |
| + |
| +void MouseLockInstance::DidLockMouse(int32_t result) { |
| + mouse_locked_ = result == PP_OK; |
| + mouse_movement_.set_x(0); |
| + mouse_movement_.set_y(0); |
| + Paint(); |
| +} |
| + |
| +void MouseLockInstance::DidFlush(int32_t result) { |
| + waiting_for_flush_completion_ = false; |
| +} |
| + |
| +void MouseLockInstance::Paint() { |
| + if (waiting_for_flush_completion_) { |
| + return; |
| + } |
| + pp::ImageData image = PaintImage(width_, height_); |
| + if (image.is_null()) { |
|
cstefansen
2011/11/10 17:53:03
Log message? (This seems serious.)
der Springer
2011/11/11 19:23:01
Done.
|
| + return; |
| + } |
| + device_context_.ReplaceContents(&image); |
| + waiting_for_flush_completion_ = true; |
| + device_context_.Flush( |
| + callback_factory_.NewRequiredCallback(&MouseLockInstance::DidFlush)); |
| +} |
| + |
| +pp::ImageData MouseLockInstance::PaintImage(int width, int height) { |
| + pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL, |
| + pp::Size(width, height), false); |
| + if (image.is_null() || image.data() == NULL) |
| + return image; |
| + |
| + int center_x = width / 2; |
| + int center_y = height / 2; |
| + pp::Point vertex(mouse_movement_.x() + center_x, |
| + mouse_movement_.y() + center_y); |
| + pp::Point anchor_1; |
| + pp::Point anchor_2; |
| + MouseDirection direction = kLeft; |
| + bool draw_needle = GetDistance(mouse_movement_.x(), mouse_movement_.y(), |
| + 0, 0) > kCentralSpotRadius; |
| + if (draw_needle) { |
| + if (abs(mouse_movement_.x()) >= abs(mouse_movement_.y())) { |
| + anchor_1.set_x(center_x); |
| + anchor_1.set_y(center_y - kCentralSpotRadius); |
| + anchor_2.set_x(center_x); |
| + anchor_2.set_y(center_y + kCentralSpotRadius); |
| + direction = (mouse_movement_.x() < 0) ? kLeft : kRight; |
| + if (direction == kLeft) |
| + anchor_1.swap(anchor_2); |
| + } else { |
| + anchor_1.set_x(center_x + kCentralSpotRadius); |
| + anchor_1.set_y(center_y); |
| + anchor_2.set_x(center_x - kCentralSpotRadius); |
| + anchor_2.set_y(center_y); |
| + direction = (mouse_movement_.y() < 0) ? kUp : kDown; |
| + if (direction == kUp) |
| + anchor_1.swap(anchor_2); |
| + } |
| + } |
| + uint32_t foreground_color = mouse_locked_ ? kLockedForegroundColor : |
| + kUnlockedForegroundColor; |
| + for (int y = 0; y < image.size().height(); ++y) { |
|
cstefansen
2011/11/10 17:53:03
Nit: it may be faster to init the entire canvas to
der Springer
2011/11/11 19:23:01
Factored this routine into three parts and made it
|
| + for (int x = 0; x < image.size().width(); ++x) { |
| + if (GetDistance(x, y, center_x, center_y) < kCentralSpotRadius) { |
| + *image.GetAddr32(pp::Point(x, y)) = foreground_color; |
| + continue; |
| + } |
| + if (draw_needle) { |
| + bool within_bound_1 = |
| + ((y - anchor_1.y()) * (vertex.x() - anchor_1.x())) > |
| + ((vertex.y() - anchor_1.y()) * (x - anchor_1.x())); |
| + bool within_bound_2 = |
| + ((y - anchor_2.y()) * (vertex.x() - anchor_2.x())) < |
| + ((vertex.y() - anchor_2.y()) * (x - anchor_2.x())); |
| + bool within_bound_3 = |
| + (direction == kUp && y < center_y) || |
| + (direction == kDown && y > center_y) || |
| + (direction == kLeft && x < center_x) || |
| + (direction == kRight && x > center_x); |
| + |
| + if (within_bound_1 && within_bound_2 && within_bound_3) { |
| + *image.GetAddr32(pp::Point(x, y)) = foreground_color; |
| + continue; |
| + } |
| + } |
| + *image.GetAddr32(pp::Point(x, y)) = kBackgroundColor; |
| + } |
| + } |
| + |
| + return image; |
| +} |
| + |
| +void MouseLockInstance::Log(const char* format, ...) { |
| + va_list args; |
| + va_start(args, format); |
| + char buf[512]; |
| + vsnprintf(buf, sizeof(buf) - 1, format, args); |
| + buf[sizeof(buf) - 1] = '\0'; |
| + va_end(args); |
| + |
| + pp::Var value(buf); |
| + PostMessage(value); |
| +} |
| + |
| +} // namespace mouselock |
| + |
| +// This object is the global object representing this plugin library as long |
| +// as it is loaded. |
| +class MouseLockModule : public pp::Module { |
| + public: |
| + MouseLockModule() : pp::Module() {} |
| + virtual ~MouseLockModule() {} |
| + |
| + // Override CreateInstance to create your customized Instance object. |
| + virtual pp::Instance* CreateInstance(PP_Instance instance) { |
| + return new mouselock::MouseLockInstance(instance); |
| + } |
| +}; |
| + |
| +namespace pp { |
| + |
| +// Factory function for your specialization of the Module object. |
| +Module* CreateModule() { |
| + return new MouseLockModule(); |
| +} |
| + |
| +} // namespace pp |
| + |
| Property changes on: examples/mouselock/mouselock.cc |
| ___________________________________________________________________ |
| Added: svn:eol-style |
| + LF |