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

Side by Side Diff: native_client_sdk/src/examples/api/input_event/input_event.cc

Issue 14607005: [NaCl SDK] Cleanup examples. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: feedback Created 7 years, 7 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // C headers 5 // C headers
6 #include <cassert> 6 #include <cassert>
7 #include <cstdio> 7 #include <cstdio>
8 8
9 // C++ headers 9 // C++ headers
10 #include <sstream> 10 #include <sstream>
11 #include <string> 11 #include <string>
12 12
13 // PPAPI headers 13 // PPAPI headers
14 #include "ppapi/cpp/completion_callback.h" 14 #include "ppapi/cpp/completion_callback.h"
15 #include "ppapi/cpp/input_event.h" 15 #include "ppapi/cpp/input_event.h"
16 #include "ppapi/cpp/instance.h" 16 #include "ppapi/cpp/instance.h"
17 #include "ppapi/cpp/module.h" 17 #include "ppapi/cpp/module.h"
18 #include "ppapi/cpp/point.h" 18 #include "ppapi/cpp/point.h"
19 #include "ppapi/cpp/var.h" 19 #include "ppapi/cpp/var.h"
20 #include "ppapi/utility/completion_callback_factory.h" 20 #include "ppapi/utility/completion_callback_factory.h"
21 21
22 #include "custom_events.h" 22 #include "custom_events.h"
23 #include "shared_queue.h" 23 #include "shared_queue.h"
24 24
25 #ifdef PostMessage 25 #ifdef PostMessage
26 #undef PostMessage 26 #undef PostMessage
27 #endif 27 #endif
28 28
29 namespace event_queue {
30 const char* const kDidChangeView = "DidChangeView\n"; 29 const char* const kDidChangeView = "DidChangeView\n";
31 const char* const kHandleInputEvent = "DidHandleInputEvent\n"; 30 const char* const kHandleInputEvent = "DidHandleInputEvent\n";
32 const char* const kDidChangeFocus = "DidChangeFocus\n"; 31 const char* const kDidChangeFocus = "DidChangeFocus\n";
33 const char* const kHaveFocus = "HaveFocus\n"; 32 const char* const kHaveFocus = "HaveFocus\n";
34 const char* const kDontHaveFocus = "DontHaveFocus\n"; 33 const char* const kDontHaveFocus = "DontHaveFocus\n";
35 const char* const kCancelMessage = "CANCEL"; 34 const char* const kCancelMessage = "CANCEL";
36 35
37 // Convert a pepper inputevent modifier value into a 36 // Convert a pepper inputevent modifier value into a
38 // custom event modifier. 37 // custom event modifier.
39 unsigned int ConvertEventModifier(uint32_t pp_modifier) { 38 unsigned int ConvertEventModifier(uint32_t pp_modifier) {
(...skipping 27 matching lines...) Expand all
67 } 66 }
68 if (pp_modifier & PP_INPUTEVENT_MODIFIER_CAPSLOCKKEY) { 67 if (pp_modifier & PP_INPUTEVENT_MODIFIER_CAPSLOCKKEY) {
69 custom_modifier |= kCapsLockModifier; 68 custom_modifier |= kCapsLockModifier;
70 } 69 }
71 if (pp_modifier & PP_INPUTEVENT_MODIFIER_NUMLOCKKEY) { 70 if (pp_modifier & PP_INPUTEVENT_MODIFIER_NUMLOCKKEY) {
72 custom_modifier |= kNumLockModifier; 71 custom_modifier |= kNumLockModifier;
73 } 72 }
74 return custom_modifier; 73 return custom_modifier;
75 } 74 }
76 75
77 class EventInstance : public pp::Instance { 76 class InputEventInstance : public pp::Instance {
78 public: 77 public:
79 explicit EventInstance(PP_Instance instance) 78 explicit InputEventInstance(PP_Instance instance)
80 : pp::Instance(instance), event_thread_(NULL), callback_factory_(this) { 79 : pp::Instance(instance), event_thread_(NULL), callback_factory_(this) {
81 RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE | PP_INPUTEVENT_CLASS_WHEEL | 80 RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE | PP_INPUTEVENT_CLASS_WHEEL |
82 PP_INPUTEVENT_CLASS_TOUCH); 81 PP_INPUTEVENT_CLASS_TOUCH);
83 RequestFilteringInputEvents(PP_INPUTEVENT_CLASS_KEYBOARD); 82 RequestFilteringInputEvents(PP_INPUTEVENT_CLASS_KEYBOARD);
84 } 83 }
85 84
86 // Not guaranteed to be called in Pepper, but a good idea to cancel the 85 // Not guaranteed to be called in Pepper, but a good idea to cancel the
87 // queue and signal to workers to die if it is called. 86 // queue and signal to workers to die if it is called.
88 virtual ~EventInstance() { CancelQueueAndWaitForWorker(); } 87 virtual ~InputEventInstance() { CancelQueueAndWaitForWorker(); }
89 88
90 // Create the 'worker thread'. 89 // Create the 'worker thread'.
91 bool Init(uint32_t argc, const char* argn[], const char* argv[]) { 90 bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
92 event_thread_ = new pthread_t; 91 event_thread_ = new pthread_t;
93 pthread_create(event_thread_, NULL, ProcessEventOnWorkerThread, this); 92 pthread_create(event_thread_, NULL, ProcessEventOnWorkerThread, this);
94 return true; 93 return true;
95 } 94 }
96 95
97 /// Clicking outside of the instance's bounding box 96 /// Clicking outside of the instance's bounding box
98 /// will create a DidChangeFocus event (the NaCl instance is 97 /// will create a DidChangeFocus event (the NaCl instance is
99 /// out of focus). Clicking back inside the instance's 98 /// out of focus). Clicking back inside the instance's
100 /// bounding box will create another DidChangeFocus event 99 /// bounding box will create another DidChangeFocus event
101 /// (the NaCl instance is back in focus). The default is 100 /// (the NaCl instance is back in focus). The default is
102 /// that the instance is out of focus. 101 /// that the instance is out of focus.
103 void DidChangeFocus(bool focus) { 102 void DidChangeFocus(bool focus) {
104 PostMessage(pp::Var(kDidChangeFocus)); 103 PostMessage(pp::Var(kDidChangeFocus));
105 if (focus == true) { 104 if (focus == true) {
106 PostMessage(pp::Var(kHaveFocus)); 105 PostMessage(pp::Var(kHaveFocus));
107 } else { 106 } else {
108 PostMessage(pp::Var(kDontHaveFocus)); 107 PostMessage(pp::Var(kDontHaveFocus));
109 } 108 }
110 } 109 }
111 110
112 /// Scrolling the mouse wheel causes a DidChangeView event. 111 /// Scrolling the mouse wheel causes a DidChangeView event.
113 void DidChangeView(const pp::View& view) { 112 void DidChangeView(const pp::View& view) {
114 PostMessage(pp::Var(kDidChangeView)); 113 PostMessage(pp::Var(kDidChangeView));
115 } 114 }
116 115
117 /// Called by the browser to handle the postMessage() call in Javascript.
118 /// Detects which method is being called from the message contents, and
119 /// calls the appropriate function. Posts the result back to the browser
120 /// asynchronously.
121 /// @param[in] var_message The message posted by the browser. The only
122 /// supported message is |kCancelMessage|. If we receive this, we
123 /// cancel the shared queue.
124 virtual void HandleMessage(const pp::Var& var_message) { 116 virtual void HandleMessage(const pp::Var& var_message) {
125 std::string message = var_message.AsString(); 117 std::string message = var_message.AsString();
126 if (kCancelMessage == message) { 118 if (kCancelMessage == message) {
127 std::string reply = 119 std::string reply =
128 "Received cancel : only Focus events will be " 120 "Received cancel : only Focus events will be "
129 "displayed. Worker thread for mouse/wheel/keyboard will exit."; 121 "displayed. Worker thread for mouse/wheel/keyboard will exit.";
130 PostMessage(pp::Var(reply)); 122 PostMessage(pp::Var(reply));
131 printf("Calling cancel queue\n"); 123 printf("Calling cancel queue\n");
132 CancelQueueAndWaitForWorker(); 124 CancelQueueAndWaitForWorker();
133 } 125 }
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 PostMessage(pp::Var(data_to_send)); 266 PostMessage(pp::Var(data_to_send));
275 return 0; 267 return 0;
276 } 268 }
277 269
278 // |ProcessEventOnWorkerThread| is a static method that is run 270 // |ProcessEventOnWorkerThread| is a static method that is run
279 // by a thread. It pulls events from the queue, converts 271 // by a thread. It pulls events from the queue, converts
280 // them to a string, and calls CallOnMainThread so that 272 // them to a string, and calls CallOnMainThread so that
281 // PostStringToBrowser will be called, which will call PostMessage 273 // PostStringToBrowser will be called, which will call PostMessage
282 // to send the converted event back to the browser. 274 // to send the converted event back to the browser.
283 static void* ProcessEventOnWorkerThread(void* param) { 275 static void* ProcessEventOnWorkerThread(void* param) {
284 EventInstance* event_instance = static_cast<EventInstance*>(param); 276 InputEventInstance* event_instance =
277 static_cast<InputEventInstance*>(param);
285 while (1) { 278 while (1) {
286 // Grab a generic Event* so that down below we can call 279 // Grab a generic Event* so that down below we can call
287 // event->ToString(), which will use the correct virtual method 280 // event->ToString(), which will use the correct virtual method
288 // to convert the event to a string. This 'conversion' is 281 // to convert the event to a string. This 'conversion' is
289 // the 'work' being done on the worker thread. In an application 282 // the 'work' being done on the worker thread. In an application
290 // the work might involve changing application state based on 283 // the work might involve changing application state based on
291 // the event that was processed. 284 // the event that was processed.
292 bool queue_cancelled; 285 bool queue_cancelled;
293 const Event* event = event_instance->GetEventFromQueue(&queue_cancelled); 286 const Event* event = event_instance->GetEventFromQueue(&queue_cancelled);
294 if (queue_cancelled) { 287 if (queue_cancelled) {
295 printf("Queue was cancelled, worker thread exiting\n"); 288 printf("Queue was cancelled, worker thread exiting\n");
296 pthread_exit(NULL); 289 pthread_exit(NULL);
297 } 290 }
298 std::string event_string = event->ToString(); 291 std::string event_string = event->ToString();
299 delete event; 292 delete event;
300 // Need to invoke callback on main thread. 293 // Need to invoke callback on main thread.
301 pp::Module::Get()->core()->CallOnMainThread( 294 pp::Module::Get()->core()->CallOnMainThread(
302 0, 295 0,
303 event_instance->callback_factory() 296 event_instance->callback_factory().NewCallback(
304 .NewCallback(&EventInstance::PostStringToBrowser, event_string)); 297 &InputEventInstance::PostStringToBrowser, event_string));
305 } // end of while loop. 298 } // end of while loop.
306 return 0; 299 return 0;
307 } 300 }
308 301
309 // Return the callback factory. 302 // Return the callback factory.
310 // Allows the static method (ProcessEventOnWorkerThread) to use 303 // Allows the static method (ProcessEventOnWorkerThread) to use
311 // the |event_instance| pointer to get the factory. 304 // the |event_instance| pointer to get the factory.
312 pp::CompletionCallbackFactory<EventInstance>& callback_factory() { 305 pp::CompletionCallbackFactory<InputEventInstance>& callback_factory() {
313 return callback_factory_; 306 return callback_factory_;
314 } 307 }
315 308
316 private: 309 private:
317 // Cancels the queue (which will cause the thread to exit). 310 // Cancels the queue (which will cause the thread to exit).
318 // Wait for the thread. Set |event_thread_| to NULL so we only 311 // Wait for the thread. Set |event_thread_| to NULL so we only
319 // execute the body once. 312 // execute the body once.
320 void CancelQueueAndWaitForWorker() { 313 void CancelQueueAndWaitForWorker() {
321 if (event_thread_) { 314 if (event_thread_) {
322 event_queue_.CancelQueue(); 315 event_queue_.CancelQueue();
323 pthread_join(*event_thread_, NULL); 316 pthread_join(*event_thread_, NULL);
324 delete event_thread_; 317 delete event_thread_;
325 event_thread_ = NULL; 318 event_thread_ = NULL;
326 } 319 }
327 } 320 }
328 pthread_t* event_thread_; 321 pthread_t* event_thread_;
329 LockingQueue<Event*> event_queue_; 322 LockingQueue<Event*> event_queue_;
330 pp::CompletionCallbackFactory<EventInstance> callback_factory_; 323 pp::CompletionCallbackFactory<InputEventInstance> callback_factory_;
331 }; 324 };
332 325
333 // The EventModule provides an implementation of pp::Module that creates 326 class InputEventModule : public pp::Module {
334 // EventInstance objects when invoked. This is part of the glue code that makes
335 // our example accessible to ppapi.
336 class EventModule : public pp::Module {
337 public: 327 public:
338 EventModule() : pp::Module() {} 328 InputEventModule() : pp::Module() {}
339 virtual ~EventModule() {} 329 virtual ~InputEventModule() {}
340 330
341 virtual pp::Instance* CreateInstance(PP_Instance instance) { 331 virtual pp::Instance* CreateInstance(PP_Instance instance) {
342 return new EventInstance(instance); 332 return new InputEventInstance(instance);
343 } 333 }
344 }; 334 };
345 335
346 } // namespace
347
348 // Implement the required pp::CreateModule function that creates our specific
349 // kind of Module (in this case, EventModule). This is part of the glue code
350 // that makes our example accessible to ppapi.
351 namespace pp { 336 namespace pp {
352 Module* CreateModule() { return new event_queue::EventModule(); } 337 Module* CreateModule() { return new InputEventModule(); }
353 } 338 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698