github.com/razvanm/vanadium-go-1.3@v0.0.0-20160721203343-4a65068e5915/src/runtime/ppapi/input_event_nacl.go (about) 1 // Copyright 2014 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package ppapi 6 7 // InputEvent represents a generic input event, including keyboard events, mouse events, 8 // or some other kind of event. Use the Type() method to get the type of event. 9 type InputEvent struct { 10 Resource 11 } 12 13 func makeInputEvent(id pp_Resource) (e InputEvent) { 14 e.id = id 15 return 16 } 17 18 // Type returns the type of the input event. 19 func (event InputEvent) Type() InputEventType { 20 return ppb_inputevent_get_type(event.id) 21 } 22 23 // GetTimeStamp returns the time that the event was generated. 24 // 25 // This will be before the current time since processing and dispatching the 26 // event has some overhead. Use this value to compare the times the user 27 // generated two events without being sensitive to variable processing time. 28 func (event InputEvent) TimeStamp() TimeTicks { 29 return TimeTicks(ppb_inputevent_get_time_stamp(event.id)) 30 } 31 32 // GetModifiers returns a bitfield indicating which modifiers were down at the 33 // time of the event. 34 // 35 // This is a combination of the flags in the PP_InputEvent_Modifier enum. 36 func (event InputEvent) Modifiers() uint32 { 37 return ppb_inputevent_get_modifiers(event.id) 38 } 39 40 // MouseInputEvent represents all mouse events except mouse wheel events. 41 type MouseInputEvent struct { 42 InputEvent 43 Modifier uint32 44 Button InputEventMouseButton 45 Position Point 46 Movement Point 47 ClickCount int 48 } 49 50 // MouseInputEvent converts the generic InputEvent to a MouseInputEvent. 51 // Unspecified if the event is not a mouse input event. 52 func (event InputEvent) MouseInputEvent() (e MouseInputEvent) { 53 id := event.id 54 e.id = id 55 e.Modifier = ppb_inputevent_get_modifiers(id) 56 e.Button = ppb_mouseinputevent_get_button(id) 57 ppb_mouseinputevent_get_position(&e.Position, id) 58 ppb_mouseinputevent_get_movement(&e.Movement, id) 59 e.ClickCount = int(ppb_mouseinputevent_get_click_count(id)) 60 return 61 } 62 63 // WheelInputEvent represents all mouse wheel events. 64 type WheelInputEvent struct { 65 InputEvent 66 Modifier uint32 67 Delta FloatPoint 68 Ticks FloatPoint 69 ScrollByPage bool 70 } 71 72 // WheelInputEvent converts the generic InputEvent to a WheelInputEvent. 73 // Unspecified if the event is not a wheel input event. 74 func (event InputEvent) WheelInputEvent() (e WheelInputEvent) { 75 id := event.id 76 e.id = id 77 e.Modifier = ppb_inputevent_get_modifiers(id) 78 ppb_wheelinputevent_get_delta(&e.Delta, id) 79 ppb_wheelinputevent_get_ticks(&e.Ticks, id) 80 e.ScrollByPage = ppb_wheelinputevent_get_scroll_by_page(id) != ppFalse 81 return 82 } 83 84 // KeyInputEvent represents a key up or key down event. 85 // 86 // Key up and key down events correspond to physical keys on the keyboard. The 87 // actual character that the user typed (if any) will be delivered in a 88 // "character" event. 89 // 90 // If the user loses focus on the module while a key is down, a key up event 91 // might not occur. For example, if the module has focus and the user presses 92 // and holds the shift key, the module will see a "shift down" message. Then if 93 // the user clicks elsewhere on the web page, the module's focus will be lost 94 // and no more input events will be delivered. 95 // 96 // If your module depends on receiving key up events, it should also handle 97 // "lost focus" as the equivalent of "all keys up." 98 type KeyInputEvent struct { 99 InputEvent 100 Modifier uint32 101 KeyCode uint32 102 } 103 104 func (event InputEvent) KeyInputEvent() (e KeyInputEvent) { 105 id := event.id 106 e.id = id 107 e.Modifier = ppb_inputevent_get_modifiers(id) 108 e.KeyCode = ppb_keyboardinputevent_get_key_code(id) 109 return 110 } 111 112 // GetCharacterText returns the typed character as a UTF-8 string for the given 113 // character event. 114 func (e KeyInputEvent) GetCharacterText() string { 115 var ppVar pp_Var 116 ppb_keyboardinputevent_get_character_text(&ppVar, e.id) 117 var v Var 118 v.fromPP(ppVar) 119 s, _ := v.AsString() 120 v.Release() 121 return s 122 } 123 124 // GetCode returns the DOM |code| field for this keyboard event, as defined in 125 // the DOM3 Events spec: http://www.w3.org/TR/DOM-Level-3-Events/. 126 func (e KeyInputEvent) GetCode() string { 127 var ppVar pp_Var 128 ppb_keyboardinputevent_get_code(&ppVar, e.id) 129 var v Var 130 v.fromPP(ppVar) 131 s, _ := v.AsString() 132 v.Release() 133 return s 134 } 135 136 // ClearInputEventRequest requests that input events corresponding to the given 137 // input classes no longer be delivered to the instance. 138 // 139 // By default, no input events are delivered. If you have previously requested 140 // input events via RequestInputEvents() or RequestFilteringInputEvents(), this 141 // function will unregister handling for the given instance. This will allow 142 // greater browser performance for those events. 143 // 144 // Note that you may still get some input events after clearing the flag if they 145 // were dispatched before the request was cleared. For example, if there are 3 146 // mouse move events waiting to be delivered, and you clear the mouse event 147 // class during the processing of the first one, you'll still receive the next 148 // two. You just won't get more events generated. 149 func (inst Instance) ClearInputEventRequest(eventClasses uint32) { 150 ppb_inputevent_clear_input_event_request(inst.id, eventClasses) 151 } 152 153 // RequestFilteringInputEvents requests that input events corresponding to the 154 // given input events are delivered to the instance for filtering. 155 // 156 // By default, no input events are delivered. In most cases you would register 157 // to receive events by calling RequestInputEvents(). In some cases, however, 158 // you may wish to filter events such that they can be bubbled up to the default 159 // handlers. In this case, register for those classes of events using this 160 // function instead of RequestInputEvents(). 161 // 162 // Filtering input events requires significantly more overhead than just 163 // delivering them to the instance. As such, you should only request filtering 164 // in those cases where it's absolutely necessary. The reason is that it 165 // requires the browser to stop and block for the instance to handle the input 166 // event, rather than sending the input event asynchronously. This can have 167 // significant overhead. 168 func (inst Instance) RequestFilteringInputEvents(eventClasses uint32) (code uint32, err error) { 169 c := ppb_inputevent_request_filtering_input_events(inst.id, eventClasses) 170 if c < 0 { 171 err = decodeError(Error(c)) 172 return 173 } 174 code = uint32(c) 175 return 176 } 177 178 // RequestInputEvent requests that input events corresponding to the given input 179 // events are delivered to the instance. 180 // 181 // It's recommended that you use RequestFilteringInputEvents() for keyboard 182 // events instead of this function so that you don't interfere with normal 183 // browser accelerators. 184 // 185 // By default, no input events are delivered. Call this function with the 186 // classes of events you are interested in to have them be delivered to the 187 // instance. Calling this function will override any previous setting for each 188 // specified class of input events (for example, if you previously called 189 // RequestFilteringInputEvents(), this function will set those events to 190 // non-filtering mode). 191 // 192 // Input events may have high overhead, so you should only request input events 193 // that your plugin will actually handle. For example, the browser may do 194 // optimizations for scroll or touch events that can be processed substantially 195 // faster if it knows there are no non-default receivers for that 196 // message. Requesting that such messages be delivered, even if they are 197 // processed very quickly, may have a noticeable effect on the performance of 198 // the page. 199 // 200 // Note that synthetic mouse events will be generated from touch events if (and 201 // only if) you do not request touch events. 202 // 203 // When requesting input events through this function, the events will be 204 // delivered and not bubbled to the default handlers. 205 func (inst Instance) RequestInputEvents(eventClasses uint32) (code uint32, err error) { 206 c := ppb_inputevent_request_filtering_input_events(inst.id, eventClasses) 207 if c < 0 { 208 err = decodeError(Error(c)) 209 return 210 } 211 code = uint32(c) 212 return 213 }