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

Side by Side Diff: ppapi/native_client/tests/ppapi_example_events/ppapi_example_events.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, 3 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 // C headers
6 #include <cassert>
7 #include <cstdio>
8
9 // C++ headers
10 #include <sstream>
11 #include <string>
12
13 // NaCl
14 #include "ppapi/cpp/input_event.h"
15 #include "ppapi/cpp/instance.h"
16 #include "ppapi/cpp/module.h"
17 #include "ppapi/cpp/point.h"
18 #include "ppapi/cpp/var.h"
19
20 namespace {
21 const char* const kEventsPropertyName = "events";
22
23 // Convert a given modifier to a descriptive string. Note that the actual
24 // declared type of modifier in each of the event classes is uint32_t, but it is
25 // expected to be interpreted as a bitfield of 'or'ed PP_InputEvent_Modifier
26 // values.
27 std::string ModifierToString(uint32_t modifier) {
28 std::string s;
29 if (modifier & PP_INPUTEVENT_MODIFIER_SHIFTKEY) {
30 s += "shift ";
31 }
32 if (modifier & PP_INPUTEVENT_MODIFIER_CONTROLKEY) {
33 s += "ctrl ";
34 }
35 if (modifier & PP_INPUTEVENT_MODIFIER_ALTKEY) {
36 s += "alt ";
37 }
38 if (modifier & PP_INPUTEVENT_MODIFIER_METAKEY) {
39 s += "meta ";
40 }
41 if (modifier & PP_INPUTEVENT_MODIFIER_ISKEYPAD) {
42 s += "keypad ";
43 }
44 if (modifier & PP_INPUTEVENT_MODIFIER_ISAUTOREPEAT) {
45 s += "autorepeat ";
46 }
47 if (modifier & PP_INPUTEVENT_MODIFIER_LEFTBUTTONDOWN) {
48 s += "left-button-down ";
49 }
50 if (modifier & PP_INPUTEVENT_MODIFIER_MIDDLEBUTTONDOWN) {
51 s += "middle-button-down ";
52 }
53 if (modifier & PP_INPUTEVENT_MODIFIER_RIGHTBUTTONDOWN) {
54 s += "right-button-down ";
55 }
56 if (modifier & PP_INPUTEVENT_MODIFIER_CAPSLOCKKEY) {
57 s += "caps-lock ";
58 }
59 if (modifier & PP_INPUTEVENT_MODIFIER_NUMLOCKKEY) {
60 s += "num-lock ";
61 }
62 return s;
63 }
64
65 std::string MouseButtonToString(PP_InputEvent_MouseButton button) {
66 switch (button) {
67 case PP_INPUTEVENT_MOUSEBUTTON_NONE:
68 return "None";
69 case PP_INPUTEVENT_MOUSEBUTTON_LEFT:
70 return "Left";
71 case PP_INPUTEVENT_MOUSEBUTTON_MIDDLE:
72 return "Middle";
73 case PP_INPUTEVENT_MOUSEBUTTON_RIGHT:
74 return "Right";
75 default:
76 std::ostringstream stream;
77 stream << "Unrecognized ("
78 << static_cast<int32_t>(button)
79 << ")";
80 return stream.str();
81 }
82 }
83
84 } // namespace
85
86 class EventInstance : public pp::Instance {
87 public:
88 explicit EventInstance(PP_Instance instance)
89 : pp::Instance(instance) {
90 std::printf("EventInstance created.\n");
91 RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE | PP_INPUTEVENT_CLASS_WHEEL);
92 RequestFilteringInputEvents(PP_INPUTEVENT_CLASS_KEYBOARD);
93 }
94 virtual ~EventInstance() {}
95
96 void GotKeyEvent(const pp::KeyboardInputEvent& key_event,
97 const std::string& kind) {
98 std::ostringstream stream;
99 stream << pp_instance() << ":"
100 << " Key event:" << kind
101 << " modifier:" << ModifierToString(key_event.GetModifiers())
102 << " key_code:" << key_event.GetKeyCode()
103 << " time:" << key_event.GetTimeStamp()
104 << " text:" << key_event.GetCharacterText().DebugString()
105 << "\n";
106 std::printf("%s", stream.str().c_str());
107 PostMessage(stream.str());
108 }
109
110 void GotMouseEvent(const pp::MouseInputEvent& mouse_event,
111 const std::string& kind) {
112 std::ostringstream stream;
113 stream << pp_instance() << ":"
114 << " Mouse event:" << kind
115 << " modifier:" << ModifierToString(mouse_event.GetModifiers())
116 << " button:" << MouseButtonToString(mouse_event.GetButton())
117 << " x:" << mouse_event.GetPosition().x()
118 << " y:" << mouse_event.GetPosition().y()
119 << " click_count:" << mouse_event.GetClickCount()
120 << " time:" << mouse_event.GetTimeStamp()
121 << "\n";
122 std::printf("%s", stream.str().c_str());
123 PostMessage(stream.str());
124 }
125
126 void GotWheelEvent(const pp::WheelInputEvent& wheel_event) {
127 std::ostringstream stream;
128 stream << pp_instance() << ": Wheel event."
129 << " modifier:" << ModifierToString(wheel_event.GetModifiers())
130 << " deltax:" << wheel_event.GetDelta().x()
131 << " deltay:" << wheel_event.GetDelta().y()
132 << " wheel_ticks_x:" << wheel_event.GetTicks().x()
133 << " wheel_ticks_y:" << wheel_event.GetTicks().y()
134 << " scroll_by_page:"
135 << (wheel_event.GetScrollByPage() ? "true" : "false")
136 << "\n";
137 std::printf("%s", stream.str().c_str());
138 PostMessage(stream.str());
139 }
140
141 // Handle an incoming input event by switching on type and dispatching
142 // to the appropriate subtype handler.
143 virtual bool HandleInputEvent(const pp::InputEvent& event) {
144 std::printf("HandleInputEvent called\n");
145 switch (event.GetType()) {
146 case PP_INPUTEVENT_TYPE_UNDEFINED:
147 std::printf("Undefined event.\n");
148 break;
149 case PP_INPUTEVENT_TYPE_MOUSEDOWN:
150 GotMouseEvent(pp::MouseInputEvent(event), "Down");
151 break;
152 case PP_INPUTEVENT_TYPE_MOUSEUP:
153 GotMouseEvent(pp::MouseInputEvent(event), "Up");
154 break;
155 case PP_INPUTEVENT_TYPE_MOUSEMOVE:
156 GotMouseEvent(pp::MouseInputEvent(event), "Move");
157 break;
158 case PP_INPUTEVENT_TYPE_MOUSEENTER:
159 GotMouseEvent(pp::MouseInputEvent(event), "Enter");
160 break;
161 case PP_INPUTEVENT_TYPE_MOUSELEAVE:
162 GotMouseEvent(pp::MouseInputEvent(event), "Leave");
163 break;
164 case PP_INPUTEVENT_TYPE_WHEEL:
165 GotWheelEvent(pp::WheelInputEvent(event));
166 break;
167 case PP_INPUTEVENT_TYPE_RAWKEYDOWN:
168 GotKeyEvent(pp::KeyboardInputEvent(event), "RawKeyDown");
169 break;
170 case PP_INPUTEVENT_TYPE_KEYDOWN:
171 GotKeyEvent(pp::KeyboardInputEvent(event), "Down");
172 break;
173 case PP_INPUTEVENT_TYPE_KEYUP:
174 GotKeyEvent(pp::KeyboardInputEvent(event), "Up");
175 break;
176 case PP_INPUTEVENT_TYPE_CHAR:
177 GotKeyEvent(pp::KeyboardInputEvent(event), "Character");
178 break;
179 default:
180 std::printf("Unrecognized event type: %d\n", event.GetType());
181 assert(false);
182 return false;
183 }
184 return true;
185 }
186 };
187
188 // The EventModule provides an implementation of pp::Module that creates
189 // EventInstance objects when invoked. This is part of the glue code that makes
190 // our example accessible to ppapi.
191 class EventModule : public pp::Module {
192 public:
193 EventModule() : pp::Module() {}
194 virtual ~EventModule() {}
195
196 virtual pp::Instance* CreateInstance(PP_Instance instance) {
197 std::printf("Creating EventInstance.\n");
198 return new EventInstance(instance);
199 }
200 };
201
202 // Implement the required pp::CreateModule function that creates our specific
203 // kind of Module (in this case, EventModule). This is part of the glue code
204 // that makes our example accessible to ppapi.
205 namespace pp {
206 Module* CreateModule() {
207 std::printf("Creating EventModule.\n");
208 return new EventModule();
209 }
210 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698