OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2010 The Native Client Authors. All rights reserved. | |
sehr (please use chromium)
2011/07/20 00:33:41
2011
| |
2 // Use of this source code is governed by a BSD-style license that can | |
3 // be found in the LICENSE file. | |
4 | |
5 #include "native_client/src/shared/ppapi_proxy/plugin_ppb_input_event.h" | |
6 | |
7 #include <stdio.h> | |
8 #include <string.h> | |
9 | |
10 #include "native_client/src/include/portability.h" | |
11 #include "native_client/src/shared/ppapi_proxy/plugin_globals.h" | |
12 #include "native_client/src/shared/ppapi_proxy/plugin_resource.h" | |
13 #include "native_client/src/shared/ppapi_proxy/untrusted/srpcgen/ppb_rpc.h" | |
14 #include "native_client/src/shared/ppapi_proxy/utility.h" | |
15 #include "native_client/src/shared/srpc/nacl_srpc.h" | |
16 #include "native_client/src/third_party/ppapi/c/pp_errors.h" | |
17 #include "native_client/src/third_party/ppapi/c/ppb_input_event.h" | |
18 | |
19 namespace { | |
20 | |
21 using ppapi_proxy::DebugPrintf; | |
22 using ppapi_proxy::GetMainSrpcChannel; | |
23 using ppapi_proxy::PluginInputEvent; | |
24 using ppapi_proxy::PluginResource; | |
25 | |
26 // InputEvent ------------------------------------------------------------------ | |
27 | |
28 int32_t RequestInputEvents(PP_Instance instance, uint32_t event_classes) { | |
29 DebugPrintf("PPB_InputEvent::RequestInputEvents: instance=%"NACL_PRIu32", " | |
30 "event_classes=%"NACL_PRIu32"\n", | |
31 instance, event_classes); | |
32 uint32_t success = 0; | |
33 NaClSrpcError srpc_result = | |
34 PpbInputEventRpcClient::PPB_InputEvent_RequestInputEvents( | |
35 GetMainSrpcChannel(), | |
36 instance, | |
37 static_cast<int32_t>(event_classes), | |
38 false, // false -> Don't filter. | |
39 reinterpret_cast<int32_t*>(&success)); | |
40 if (srpc_result == NACL_SRPC_RESULT_OK) { | |
41 return success; | |
42 } | |
43 return PP_ERROR_FAILED; | |
44 } | |
45 | |
46 int32_t RequestFilteringInputEvents(PP_Instance instance, | |
47 uint32_t event_classes) { | |
48 DebugPrintf("PPB_InputEvent::RequestFilteringInputEvents: instance=" | |
49 "%"NACL_PRIu32", event_classes=%"NACL_PRIu32"\n", | |
50 instance, event_classes); | |
51 uint32_t success = 0; | |
52 NaClSrpcError srpc_result = | |
53 PpbInputEventRpcClient::PPB_InputEvent_RequestInputEvents( | |
54 GetMainSrpcChannel(), | |
55 instance, | |
56 static_cast<int32_t>(event_classes), | |
57 true, // true -> Filter. | |
58 reinterpret_cast<int32_t*>(&success)); | |
59 if (srpc_result == NACL_SRPC_RESULT_OK) { | |
60 return success; | |
61 } | |
62 return PP_ERROR_FAILED; | |
63 } | |
64 | |
65 void ClearInputEventRequest(PP_Instance instance, | |
66 uint32_t event_classes) { | |
67 DebugPrintf("PPB_InputEvent::ClearInputEventRequest: instance=%"NACL_PRIu32 | |
68 ", event_classes=%"NACL_PRIu32"\n", | |
69 instance, event_classes); | |
70 PpbInputEventRpcClient::PPB_InputEvent_ClearInputEventRequest( | |
71 GetMainSrpcChannel(), | |
72 instance, | |
73 static_cast<int32_t>(event_classes)); | |
74 } | |
75 | |
76 // Helper RAII class to get the PluginInputEvent from the PP_Resource and hold | |
77 // it with a scoped_refptr. Also does a DebugPrintf. | |
78 class InputEventGetter { | |
79 public: | |
80 InputEventGetter(PP_Resource resource, const char* function_name) { | |
81 DebugPrintf("PPB_InputEvent::%s: resource=%"NACL_PRIu32"\n", | |
82 function_name, | |
83 resource); | |
84 input_event_ = | |
85 PluginResource::GetAs<PluginInputEvent>(resource); | |
86 } | |
87 | |
88 PluginInputEvent* get() { | |
89 return input_event_.get(); | |
90 } | |
91 | |
92 private: | |
93 NACL_DISALLOW_COPY_AND_ASSIGN(InputEventGetter); | |
94 scoped_refptr<PluginInputEvent> input_event_; | |
95 }; | |
96 // This macro is for starting a resource function. It makes sure resource_arg | |
97 // is of type PluginInputEvent, and returns error_return if it's not. | |
98 #define BEGIN_RESOURCE_THUNK(function_name, resource_arg, error_return) \ | |
99 InputEventGetter input_event(resource_arg, function_name); \ | |
100 if (!input_event.get()) { \ | |
101 return error_return; \ | |
102 } \ | |
103 do {} while(0) | |
104 | |
105 PP_Bool IsInputEvent(PP_Resource resource) { | |
106 BEGIN_RESOURCE_THUNK("IsInputEvent", resource, PP_FALSE); | |
107 return PP_TRUE; | |
108 } | |
109 | |
110 #define IMPLEMENT_RESOURCE_THUNK(function, resource_arg, error_return) \ | |
111 BEGIN_RESOURCE_THUNK(#function, resource_arg, error_return); \ | |
112 return input_event.get()->function() | |
113 | |
114 PP_InputEvent_Type GetType(PP_Resource event) { | |
115 IMPLEMENT_RESOURCE_THUNK(GetType, event, PP_INPUTEVENT_TYPE_UNDEFINED); | |
116 } | |
117 | |
118 PP_TimeTicks GetTimeStamp(PP_Resource event) { | |
119 IMPLEMENT_RESOURCE_THUNK(GetTimeStamp, event, 0.0); | |
120 } | |
121 | |
122 uint32_t GetModifiers(PP_Resource event) { | |
123 IMPLEMENT_RESOURCE_THUNK(GetModifiers, event, 0); | |
124 } | |
125 | |
126 // Mouse ----------------------------------------------------------------------- | |
127 | |
128 PP_Bool IsMouseInputEvent(PP_Resource resource) { | |
129 if (!IsInputEvent(resource)) | |
130 return PP_FALSE; | |
131 PP_InputEvent_Type type = GetType(resource); | |
132 return PP_FromBool(type == PP_INPUTEVENT_TYPE_MOUSEDOWN || | |
133 type == PP_INPUTEVENT_TYPE_MOUSEUP || | |
134 type == PP_INPUTEVENT_TYPE_MOUSEMOVE || | |
135 type == PP_INPUTEVENT_TYPE_MOUSEENTER || | |
136 type == PP_INPUTEVENT_TYPE_MOUSELEAVE || | |
137 type == PP_INPUTEVENT_TYPE_CONTEXTMENU); | |
138 } | |
139 | |
140 PP_InputEvent_MouseButton GetMouseButton(PP_Resource mouse_event) { | |
141 IMPLEMENT_RESOURCE_THUNK(GetMouseButton, mouse_event, | |
142 PP_INPUTEVENT_MOUSEBUTTON_NONE); | |
143 } | |
144 | |
145 PP_Point GetMousePosition(PP_Resource mouse_event) { | |
146 IMPLEMENT_RESOURCE_THUNK(GetMousePosition, mouse_event, PP_MakePoint(0, 0)); | |
147 } | |
148 | |
149 int32_t GetMouseClickCount(PP_Resource mouse_event) { | |
150 IMPLEMENT_RESOURCE_THUNK(GetMouseClickCount, mouse_event, 0); | |
151 } | |
152 | |
153 // Wheel ----------------------------------------------------------------------- | |
154 | |
155 PP_Bool IsWheelInputEvent(PP_Resource resource) { | |
156 if (!IsInputEvent(resource)) | |
157 return PP_FALSE; | |
158 PP_InputEvent_Type type = GetType(resource); | |
159 return PP_FromBool(type == PP_INPUTEVENT_TYPE_MOUSEWHEEL); | |
160 } | |
161 | |
162 PP_FloatPoint GetWheelDelta(PP_Resource wheel_event) { | |
163 IMPLEMENT_RESOURCE_THUNK(GetWheelDelta, wheel_event, PP_MakeFloatPoint(0, 0)); | |
164 } | |
165 | |
166 PP_FloatPoint GetWheelTicks(PP_Resource wheel_event) { | |
167 IMPLEMENT_RESOURCE_THUNK(GetWheelTicks, wheel_event, PP_MakeFloatPoint(0, 0)); | |
168 } | |
169 | |
170 PP_Bool GetWheelScrollByPage(PP_Resource wheel_event) { | |
171 IMPLEMENT_RESOURCE_THUNK(GetWheelScrollByPage, wheel_event, PP_FALSE); | |
172 } | |
173 | |
174 PP_Bool IsKeyboardInputEvent(PP_Resource resource) { | |
175 if (!IsInputEvent(resource)) | |
176 return PP_FALSE; | |
177 PP_InputEvent_Type type = GetType(resource); | |
178 return PP_FromBool(type == PP_INPUTEVENT_TYPE_KEYDOWN || | |
179 type == PP_INPUTEVENT_TYPE_KEYUP || | |
180 type == PP_INPUTEVENT_TYPE_CHAR); | |
181 } | |
182 | |
183 uint32_t GetKeyCode(PP_Resource key_event) { | |
184 IMPLEMENT_RESOURCE_THUNK(GetKeyCode, key_event, 0); | |
185 } | |
186 | |
187 PP_Var GetCharacterText(PP_Resource character_event) { | |
188 IMPLEMENT_RESOURCE_THUNK(GetCharacterText, character_event, | |
189 PP_MakeUndefined()); | |
190 } | |
191 | |
192 } // namespace | |
193 | |
194 namespace ppapi_proxy { | |
195 | |
196 // static | |
197 const PPB_InputEvent* PluginInputEvent::GetInterface() { | |
198 static const PPB_InputEvent input_event_interface = { | |
199 &::RequestInputEvents, | |
sehr (please use chromium)
2011/07/20 00:33:41
I think in the rest of the proxy we've not used th
dmichael (off chromium)
2011/07/20 03:50:22
Good point. The & is definitely unnecessary. The l
| |
200 &::RequestFilteringInputEvents, | |
201 &::ClearInputEventRequest, | |
202 &::IsInputEvent, | |
203 &::GetType, | |
204 &::GetTimeStamp, | |
205 &::GetModifiers | |
206 }; | |
207 return &input_event_interface; | |
208 } | |
209 | |
210 // static | |
211 const PPB_MouseInputEvent* PluginInputEvent::GetMouseInterface() { | |
212 static const PPB_MouseInputEvent mouse_input_event_interface = { | |
213 &::IsMouseInputEvent, | |
214 &::GetMouseButton, | |
215 &::GetMousePosition, | |
216 &::GetMouseClickCount | |
217 }; | |
218 return &mouse_input_event_interface; | |
219 } | |
220 | |
221 // static | |
222 const PPB_WheelInputEvent* PluginInputEvent::GetWheelInterface() { | |
223 static const PPB_WheelInputEvent wheel_input_event_interface = { | |
224 &::IsWheelInputEvent, | |
225 &::GetWheelDelta, | |
226 &::GetWheelTicks, | |
227 &::GetWheelScrollByPage | |
228 }; | |
229 return &wheel_input_event_interface; | |
230 } | |
231 | |
232 // static | |
233 const PPB_KeyboardInputEvent* PluginInputEvent::GetKeyboardInterface() { | |
234 static const PPB_KeyboardInputEvent keyboard_input_event_interface = { | |
235 &::IsKeyboardInputEvent, | |
236 &::GetKeyCode, | |
237 &::GetCharacterText | |
238 }; | |
239 return &keyboard_input_event_interface; | |
240 } | |
241 | |
242 PluginInputEvent::PluginInputEvent() | |
243 : character_text_(PP_MakeUndefined()) { | |
244 } | |
245 | |
246 void PluginInputEvent::Init(const InputEventData& input_event_data, | |
247 PP_Var character_text) { | |
248 input_event_data_ = input_event_data; | |
249 character_text_ = character_text; | |
250 } | |
251 | |
252 PluginInputEvent::~PluginInputEvent() { | |
253 // Release the character text. This is a no-op if it's not a string. | |
254 PPBVarInterface()->Release(character_text_); | |
255 } | |
256 | |
257 PP_InputEvent_Type PluginInputEvent::GetType() const { | |
258 return input_event_data_.event_type; | |
259 } | |
260 | |
261 PP_TimeTicks PluginInputEvent::GetTimeStamp() const { | |
262 return input_event_data_.event_time_stamp; | |
263 } | |
264 | |
265 uint32_t PluginInputEvent::GetModifiers() const { | |
266 return input_event_data_.event_modifiers; | |
267 } | |
268 | |
269 PP_InputEvent_MouseButton PluginInputEvent::GetMouseButton() const { | |
270 return input_event_data_.mouse_button; | |
271 } | |
272 | |
273 PP_Point PluginInputEvent::GetMousePosition() const { | |
274 return input_event_data_.mouse_position; | |
275 } | |
276 | |
277 int32_t PluginInputEvent::GetMouseClickCount() const { | |
278 return input_event_data_.mouse_click_count; | |
279 } | |
280 | |
281 PP_FloatPoint PluginInputEvent::GetWheelDelta() const { | |
282 return input_event_data_.wheel_delta; | |
283 } | |
284 | |
285 PP_FloatPoint PluginInputEvent::GetWheelTicks() const { | |
286 return input_event_data_.wheel_ticks; | |
287 } | |
288 | |
289 PP_Bool PluginInputEvent::GetWheelScrollByPage() const { | |
290 return PP_FromBool(input_event_data_.wheel_scroll_by_page); | |
291 } | |
292 | |
293 uint32_t PluginInputEvent::GetKeyCode() const { | |
294 return input_event_data_.key_code; | |
295 } | |
296 | |
297 PP_Var PluginInputEvent::GetCharacterText() const { | |
298 PPBVarInterface()->AddRef(character_text_); | |
299 return character_text_; | |
300 } | |
301 | |
302 } // namespace ppapi_proxy | |
303 | |
OLD | NEW |