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