Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(102)

Side by Side Diff: ppapi/native_client/tests/ppapi_simple_tests/event.cc

Issue 7740013: Cloning a bunch of stuff from the native_client repository at r6528 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Native Client 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 <stdint.h>
6 #include <stdlib.h>
7
8 #include <sstream>
9 #include <string>
10 #include <queue>
11
12 #include "ppapi/c/pp_input_event.h"
13 #include "ppapi/c/ppp_input_event.h"
14 #include "ppapi/cpp/instance.h"
15 #include "ppapi/cpp/module.h"
16 #include "ppapi/cpp/point.h"
17 #include "ppapi/cpp/var.h"
18 #include "ppapi/cpp/input_event.h"
19
20
21 using std::string;
22 using std::queue;
23 using std::ostringstream;
24
25 const int kDefaultEventBufferSize = 10;
26
27 namespace {
28
29 string ModifierToString(uint32_t modifier) {
30 string s;
31 if (modifier & PP_INPUTEVENT_MODIFIER_SHIFTKEY) {
32 s += "shift ";
33 }
34 if (modifier & PP_INPUTEVENT_MODIFIER_CONTROLKEY) {
35 s += "ctrl ";
36 }
37 if (modifier & PP_INPUTEVENT_MODIFIER_ALTKEY) {
38 s += "alt ";
39 }
40 if (modifier & PP_INPUTEVENT_MODIFIER_METAKEY) {
41 s += "meta ";
42 }
43 if (modifier & PP_INPUTEVENT_MODIFIER_ISKEYPAD) {
44 s += "keypad ";
45 }
46 if (modifier & PP_INPUTEVENT_MODIFIER_ISAUTOREPEAT) {
47 s += "autorepeat ";
48 }
49 if (modifier & PP_INPUTEVENT_MODIFIER_LEFTBUTTONDOWN) {
50 s += "left-button-down ";
51 }
52 if (modifier & PP_INPUTEVENT_MODIFIER_MIDDLEBUTTONDOWN) {
53 s += "middle-button-down ";
54 }
55 if (modifier & PP_INPUTEVENT_MODIFIER_RIGHTBUTTONDOWN) {
56 s += "right-button-down ";
57 }
58 if (modifier & PP_INPUTEVENT_MODIFIER_CAPSLOCKKEY) {
59 s += "caps-lock ";
60 }
61 if (modifier & PP_INPUTEVENT_MODIFIER_NUMLOCKKEY) {
62 s += "num-lock ";
63 }
64 return s;
65 }
66
67
68 string MouseButtonToString(PP_InputEvent_MouseButton button) {
69 switch (button) {
70 case PP_INPUTEVENT_MOUSEBUTTON_NONE:
71 return "None";
72 case PP_INPUTEVENT_MOUSEBUTTON_LEFT:
73 return "Left";
74 case PP_INPUTEVENT_MOUSEBUTTON_MIDDLE:
75 return "Middle";
76 case PP_INPUTEVENT_MOUSEBUTTON_RIGHT:
77 return "Right";
78 default:
79 ostringstream stream;
80 stream << "Unrecognized (" << static_cast<int32_t>(button) << ")";
81 return stream.str();
82 }
83 }
84
85
86 string KeyEvent(const pp::KeyboardInputEvent& key_event,
87 const string& kind) {
88 ostringstream stream;
89 stream << "Key event:" << kind
90 << " modifier:" << ModifierToString(key_event.GetModifiers())
91 << " key_code:" << key_event.GetKeyCode()
92 << " time:" << key_event.GetTimeStamp()
93 << " text:" << key_event.GetCharacterText().DebugString()
94 << "\n";
95 return stream.str();
96 }
97
98
99 string MouseEvent(const pp::MouseInputEvent& mouse_event,
100 const string& kind) {
101 ostringstream stream;
102 stream << "Mouse event:" << kind
103 << " modifier:" << ModifierToString(mouse_event.GetModifiers())
104 << " button:" << MouseButtonToString(mouse_event.GetButton())
105 << " x:" << mouse_event.GetPosition().x()
106 << " y:" << mouse_event.GetPosition().y()
107 << " click_count:" << mouse_event.GetClickCount()
108 << " time:" << mouse_event.GetTimeStamp()
109 << "\n";
110 return stream.str();
111 }
112
113
114 string WheelEvent(const pp::WheelInputEvent& wheel_event) {
115 ostringstream stream;
116 stream << "Wheel event."
117 << " modifier:" << ModifierToString(wheel_event.GetModifiers())
118 << " deltax:" << wheel_event.GetDelta().x()
119 << " deltay:" << wheel_event.GetDelta().y()
120 << " wheel_ticks_x:" << wheel_event.GetTicks().x()
121 << " wheel_ticks_y:" << wheel_event.GetTicks().y()
122 << " scroll_by_page:"
123 << (wheel_event.GetScrollByPage() ? "true" : "false")
124 << "\n";
125 return stream.str();
126 }
127
128
129 string EventToString(const pp::InputEvent& event) {
130 ostringstream stream;
131 switch (event.GetType()) {
132 default:
133 case PP_INPUTEVENT_TYPE_UNDEFINED:
134 stream << "Unrecognized Event (" << static_cast<int32_t>(event.GetType())
135 << ")";
136 return stream.str();
137
138 case PP_INPUTEVENT_TYPE_MOUSEDOWN:
139 return MouseEvent(pp::MouseInputEvent(event), "Down");
140 case PP_INPUTEVENT_TYPE_MOUSEUP:
141 return MouseEvent(pp::MouseInputEvent(event), "Up");
142 case PP_INPUTEVENT_TYPE_MOUSEMOVE:
143 return MouseEvent(pp::MouseInputEvent(event), "Move");
144 case PP_INPUTEVENT_TYPE_MOUSEENTER:
145 return MouseEvent(pp::MouseInputEvent(event), "Enter");
146 case PP_INPUTEVENT_TYPE_MOUSELEAVE:
147 return MouseEvent(pp::MouseInputEvent(event), "Leave");
148
149 case PP_INPUTEVENT_TYPE_WHEEL:
150 return WheelEvent(pp::WheelInputEvent(event));
151
152 case PP_INPUTEVENT_TYPE_RAWKEYDOWN:
153 return KeyEvent(pp::KeyboardInputEvent(event), "RawKeyDown");
154 case PP_INPUTEVENT_TYPE_KEYDOWN:
155 return KeyEvent(pp::KeyboardInputEvent(event), "Down");
156 case PP_INPUTEVENT_TYPE_KEYUP:
157 return KeyEvent(pp::KeyboardInputEvent(event), "Up");
158 case PP_INPUTEVENT_TYPE_CHAR:
159 return KeyEvent(pp::KeyboardInputEvent(event), "Char");
160 }
161 }
162
163 void StringReplace(string* input,
164 const string& find,
165 const string& replace) {
166 if (find.length() == 0 || input->length() == 0) {
167 return;
168 }
169
170 size_t start_pos = 0;
171 while (1) {
172 start_pos = input->find(find, start_pos);
173 if (start_pos == string::npos) {
174 break;
175 }
176 input->replace(start_pos, find.length(), replace);
177 start_pos += replace.length();
178 }
179 }
180
181 } // namespace
182
183
184 class MyInstance : public pp::Instance {
185 private:
186 size_t max_buffer_size_;
187 queue<string> event_buffer_;
188
189 void ParseArgs(uint32_t argc, const char* argn[], const char* argv[]) {
190 for (uint32_t i = 0; i < argc; ++i) {
191 const std::string tag = argn[i];
192 if (tag == "buffer_size") max_buffer_size_ = strtol(argv[i], 0, 0);
193 // ignore other tags
194 }
195 }
196
197 // Dump all the event via PostMessage for testing
198 void FlushEventBuffer() {
199 while (event_buffer_.size() > 0) {
200 string s = event_buffer_.front();
201 event_buffer_.pop();
202 // Replace space with underscore to simplify testing
203 StringReplace(&s, " ", "_");
204 pp::Var message(s);
205 PostMessage(message);
206 }
207 }
208
209 public:
210 explicit MyInstance(PP_Instance instance)
211 : pp::Instance(instance), max_buffer_size_(kDefaultEventBufferSize) {
212 RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE | PP_INPUTEVENT_CLASS_WHEEL);
213 RequestFilteringInputEvents(PP_INPUTEVENT_CLASS_KEYBOARD);
214 }
215
216 virtual ~MyInstance() {}
217
218 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
219 ParseArgs(argc, argn, argv);
220 return true;
221 }
222
223 virtual bool HandleInputEvent(const pp::InputEvent& event) {
224 ostringstream stream;
225 stream << pp_instance() << ": " << EventToString(event);
226 event_buffer_.push(stream.str());
227 if (event_buffer_.size() >= max_buffer_size_) {
228 FlushEventBuffer();
229 }
230 return true;
231 }
232
233 virtual void DidChangeFocus(bool has_focus) {
234 FlushEventBuffer();
235 }
236 };
237
238 // standard boilerplate code below
239 class MyModule : public pp::Module {
240 public:
241 virtual pp::Instance* CreateInstance(PP_Instance instance) {
242 return new MyInstance(instance);
243 }
244 };
245
246 namespace pp {
247 Module* CreateModule() {
248 return new MyModule();
249 }
250 }
OLDNEW
« no previous file with comments | « ppapi/native_client/tests/ppapi_simple_tests/audio.stdout ('k') | ppapi/native_client/tests/ppapi_simple_tests/event.stdin » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698