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 3 // found in the LICENSE file. 4 5 #ifndef CUSTOM_EVENTS_H 6 #define CUSTOM_EVENTS_H 7 8 #include <stdint.h> 9 #include <string> 10 #include <vector> 11 12 // These functions and classes are used to define a non-Pepper set of 13 // events. This is typical of what many developers might do, since it 14 // would be common to convert a Pepper event into some other more 15 // application-specific type of event (SDL, Qt, etc.). 16 17 // Constants used for Event::event_modifers_ (which is an int) 18 // are given below. Use powers of 2 so we can use bitwise AND/OR operators. 19 const uint32_t kShiftKeyModifier = 1 << 0; 20 const uint32_t kControlKeyModifier = 1 << 1; 21 const uint32_t kAltKeyModifier = 1 << 2; 22 const uint32_t kMetaKeyModifer = 1 << 3; 23 const uint32_t kKeyPadModifier = 1 << 4; 24 const uint32_t kAutoRepeatModifier = 1 << 5; 25 const uint32_t kLeftButtonModifier = 1 << 6; 26 const uint32_t kMiddleButtonModifier = 1 << 7; 27 const uint32_t kRightButtonModifier = 1 << 8; 28 const uint32_t kCapsLockModifier = 1 << 9; 29 const uint32_t kNumLockModifier = 1 << 10; 30 31 std::string ModifierToString(uint32_t modifier); 32 33 // Abstract base class for an Event -- ToString() is not defined. 34 // With polymorphism, we can have a collection of Event* and call 35 // ToString() on each one to be able to display the details of each 36 // event. 37 class Event { 38 public: 39 // Constructor for the base class. 40 // |modifiers| is an int that uses bit fields to set specific 41 // changes, such as the Alt key, specific button, etc. See 42 // ModifierToString() and constants defined in custom_events.cc. 43 explicit Event(uint32_t modifiers) : event_modifiers_(modifiers) {} 44 uint32_t event_modifiers() const { return event_modifiers_; } 45 std::string string_event_modifiers() const { 46 return ModifierToString(event_modifiers_); 47 } 48 // Convert the WheelEvent to a string 49 virtual std::string ToString() const = 0; 50 virtual ~Event() {} 51 52 private: 53 uint32_t event_modifiers_; 54 }; 55 56 // Class for a keyboard event. 57 class KeyEvent : public Event { 58 public: 59 // KeyEvent Constructor. |modifiers| is passed to Event base class. 60 // |keycode| is the ASCII value, |time| is a timestamp, 61 // |text| is the value as a string. 62 KeyEvent(uint32_t modifiers, uint32_t keycode, double time, std::string text) 63 : Event(modifiers), key_code_(keycode), timestamp_(time), text_(text) {} 64 // Convert the WheelEvent to a string 65 virtual std::string ToString() const; 66 67 private: 68 uint32_t key_code_; 69 double timestamp_; 70 std::string text_; 71 }; 72 73 class MouseEvent : public Event { 74 public: 75 // Specify a mouse button, with kNone available for initialization. 76 enum MouseButton { 77 kNone, 78 kLeft, 79 kMiddle, 80 kRight 81 }; 82 83 // MouseEvent Constructor. |modifiers| is passed to Event base class. 84 // |button| specifies which button 85 // |xpos| and |ypos| give the location, 86 // |clicks| is how many times this same |xpos|,|ypos| 87 // has been clicked in a row. |time| is a timestamp, 88 MouseEvent(uint32_t modifiers, 89 MouseButton button, 90 uint32_t xpos, 91 uint32_t ypos, 92 uint32_t clicks, 93 double time, 94 bool is_context_menu) 95 : Event(modifiers), 96 mouse_button_(button), 97 x_position_(xpos), 98 y_position_(ypos), 99 click_count_(clicks), 100 timestamp_(time), 101 is_context_menu_(is_context_menu) {} 102 // Convert the WheelEvent to a string 103 virtual std::string ToString() const; 104 105 private: 106 MouseButton mouse_button_; 107 uint32_t x_position_; 108 uint32_t y_position_; 109 uint32_t click_count_; 110 double timestamp_; 111 bool is_context_menu_; 112 113 std::string MouseButtonToString(MouseButton button) const; 114 }; 115 116 class WheelEvent : public Event { 117 public: 118 // WheelEvent Constructor. |modifiers| is passed to Event base class. 119 // |xticks| and |yticks| specify number of mouse wheel ticks. 120 // |scroll_by_page| indicates if we have scrolled past the current 121 // page. |time| is a timestamp, 122 WheelEvent(int modifiers, 123 uint32_t dx, 124 uint32_t dy, 125 uint32_t xticks, 126 uint32_t yticks, 127 bool scroll_by_page, 128 float time) 129 : Event(modifiers), 130 delta_x_(dx), 131 delta_y_(dy), 132 ticks_x_(xticks), 133 ticks_y_(yticks), 134 scroll_by_page_(scroll_by_page), 135 timestamp_(time) {} 136 // Convert the WheelEvent to a string 137 virtual std::string ToString() const; 138 139 private: 140 uint32_t delta_x_; 141 uint32_t delta_y_; 142 uint32_t ticks_x_; 143 uint32_t ticks_y_; 144 bool scroll_by_page_; 145 double timestamp_; 146 }; 147 148 class TouchEvent : public Event { 149 public: 150 // The kind of touch event that occurred. 151 enum Kind { 152 kNone, 153 kStart, 154 kMove, 155 kEnd, 156 kCancel 157 }; 158 159 // TouchEvent constructor. 160 TouchEvent(int modifiers, Kind kind, float time) 161 : Event(modifiers), kind_(kind), timestamp_(time) {} 162 // Add a changed touch to this touch event. 163 void AddTouch(uint32_t id, 164 float x, 165 float y, 166 float radii_x, 167 float radii_y, 168 float angle, 169 float pressure); 170 // Convert the TouchEvent to a string 171 virtual std::string ToString() const; 172 173 private: 174 std::string KindToString(Kind kind) const; 175 176 struct Touch { 177 uint32_t id; 178 float x; 179 float y; 180 float radii_x; 181 float radii_y; 182 float angle; 183 float pressure; 184 }; 185 186 Kind kind_; 187 std::vector<Touch> touches; 188 double timestamp_; 189 }; 190 191 #endif // CUSTOM_EVENTS_H 192