Home | History | Annotate | Download | only in shared_impl
      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 #include "ppapi/shared_impl/ppb_input_event_shared.h"
      6 
      7 #include "ppapi/shared_impl/var.h"
      8 
      9 using ppapi::thunk::PPB_InputEvent_API;
     10 
     11 namespace ppapi {
     12 
     13 InputEventData::InputEventData()
     14     : is_filtered(false),
     15       event_type(PP_INPUTEVENT_TYPE_UNDEFINED),
     16       event_time_stamp(0.0),
     17       event_modifiers(0),
     18       mouse_button(PP_INPUTEVENT_MOUSEBUTTON_NONE),
     19       mouse_position(PP_MakePoint(0, 0)),
     20       mouse_click_count(0),
     21       mouse_movement(PP_MakePoint(0, 0)),
     22       wheel_delta(PP_MakeFloatPoint(0.0f, 0.0f)),
     23       wheel_ticks(PP_MakeFloatPoint(0.0f, 0.0f)),
     24       wheel_scroll_by_page(false),
     25       key_code(0),
     26       usb_key_code(0),
     27       code(),
     28       character_text(),
     29       composition_target_segment(-1),
     30       composition_selection_start(0),
     31       composition_selection_end(0),
     32       touches(),
     33       changed_touches(),
     34       target_touches() {
     35 }
     36 
     37 InputEventData::~InputEventData() {
     38 }
     39 
     40 PPB_InputEvent_Shared::PPB_InputEvent_Shared(ResourceObjectType type,
     41                                              PP_Instance instance,
     42                                              const InputEventData& data)
     43     : Resource(type, instance),
     44       data_(data) {
     45 }
     46 
     47 PPB_InputEvent_API* PPB_InputEvent_Shared::AsPPB_InputEvent_API() {
     48   return this;
     49 }
     50 
     51 const InputEventData& PPB_InputEvent_Shared::GetInputEventData() const {
     52   return data_;
     53 }
     54 
     55 PP_InputEvent_Type PPB_InputEvent_Shared::GetType() {
     56   return data_.event_type;
     57 }
     58 
     59 PP_TimeTicks PPB_InputEvent_Shared::GetTimeStamp() {
     60   return data_.event_time_stamp;
     61 }
     62 
     63 uint32_t PPB_InputEvent_Shared::GetModifiers() {
     64   return data_.event_modifiers;
     65 }
     66 
     67 PP_InputEvent_MouseButton PPB_InputEvent_Shared::GetMouseButton() {
     68   return data_.mouse_button;
     69 }
     70 
     71 PP_Point PPB_InputEvent_Shared::GetMousePosition() {
     72   return data_.mouse_position;
     73 }
     74 
     75 int32_t PPB_InputEvent_Shared::GetMouseClickCount() {
     76   return data_.mouse_click_count;
     77 }
     78 
     79 PP_Point PPB_InputEvent_Shared::GetMouseMovement() {
     80   return data_.mouse_movement;
     81 }
     82 
     83 PP_FloatPoint PPB_InputEvent_Shared::GetWheelDelta() {
     84   return data_.wheel_delta;
     85 }
     86 
     87 PP_FloatPoint PPB_InputEvent_Shared::GetWheelTicks() {
     88   return data_.wheel_ticks;
     89 }
     90 
     91 PP_Bool PPB_InputEvent_Shared::GetWheelScrollByPage() {
     92   return PP_FromBool(data_.wheel_scroll_by_page);
     93 }
     94 
     95 uint32_t PPB_InputEvent_Shared::GetKeyCode() {
     96   return data_.key_code;
     97 }
     98 
     99 PP_Var PPB_InputEvent_Shared::GetCharacterText() {
    100   return StringVar::StringToPPVar(data_.character_text);
    101 }
    102 
    103 PP_Bool PPB_InputEvent_Shared::SetUsbKeyCode(uint32_t usb_key_code) {
    104   data_.usb_key_code = usb_key_code;
    105   return PP_TRUE;
    106 }
    107 
    108 uint32_t PPB_InputEvent_Shared::GetUsbKeyCode() {
    109   return data_.usb_key_code;
    110 }
    111 
    112 PP_Var PPB_InputEvent_Shared::GetCode() {
    113   return StringVar::StringToPPVar(data_.code);
    114 }
    115 
    116 uint32_t PPB_InputEvent_Shared::GetIMESegmentNumber() {
    117   if (data_.composition_segment_offsets.empty())
    118     return 0;
    119   return static_cast<uint32_t>(data_.composition_segment_offsets.size() - 1);
    120 }
    121 
    122 uint32_t PPB_InputEvent_Shared::GetIMESegmentOffset(uint32_t index) {
    123   if (index >= data_.composition_segment_offsets.size())
    124     return 0;
    125   return data_.composition_segment_offsets[index];
    126 }
    127 
    128 int32_t PPB_InputEvent_Shared::GetIMETargetSegment() {
    129   return data_.composition_target_segment;
    130 }
    131 
    132 void PPB_InputEvent_Shared::GetIMESelection(uint32_t* start, uint32_t* end) {
    133   if (start)
    134     *start = data_.composition_selection_start;
    135   if (end)
    136     *end = data_.composition_selection_end;
    137 }
    138 
    139 void PPB_InputEvent_Shared::AddTouchPoint(PP_TouchListType list,
    140                                           const PP_TouchPoint& point) {
    141   switch (list) {
    142     case PP_TOUCHLIST_TYPE_TOUCHES:
    143       data_.touches.push_back(point);
    144       break;
    145     case PP_TOUCHLIST_TYPE_CHANGEDTOUCHES:
    146       data_.changed_touches.push_back(point);
    147       break;
    148     case PP_TOUCHLIST_TYPE_TARGETTOUCHES:
    149       data_.target_touches.push_back(point);
    150       break;
    151     default:
    152       break;
    153   }
    154 }
    155 
    156 uint32_t PPB_InputEvent_Shared::GetTouchCount(PP_TouchListType list) {
    157   switch (list) {
    158     case PP_TOUCHLIST_TYPE_TOUCHES:
    159       return static_cast<uint32_t>(data_.touches.size());
    160     case PP_TOUCHLIST_TYPE_CHANGEDTOUCHES:
    161       return static_cast<uint32_t>(data_.changed_touches.size());
    162     case PP_TOUCHLIST_TYPE_TARGETTOUCHES:
    163       return static_cast<uint32_t>(data_.target_touches.size());
    164   }
    165 
    166   return 0;
    167 }
    168 
    169 PP_TouchPoint PPB_InputEvent_Shared::GetTouchByIndex(PP_TouchListType list,
    170                                                      uint32_t index) {
    171   std::vector<PP_TouchPoint>* points;
    172   switch (list) {
    173     case PP_TOUCHLIST_TYPE_TOUCHES:
    174       points = &data_.touches;
    175       break;
    176     case PP_TOUCHLIST_TYPE_CHANGEDTOUCHES:
    177       points = &data_.changed_touches;
    178       break;
    179     case PP_TOUCHLIST_TYPE_TARGETTOUCHES:
    180       points = &data_.target_touches;
    181       break;
    182     default:
    183       return PP_MakeTouchPoint();
    184   }
    185   if (index >= points->size()) {
    186     return PP_MakeTouchPoint();
    187   }
    188   return points->at(index);
    189 }
    190 
    191 PP_TouchPoint PPB_InputEvent_Shared::GetTouchById(PP_TouchListType list,
    192                                                   uint32_t id) {
    193   const std::vector<PP_TouchPoint>* points;
    194   switch (list) {
    195     case PP_TOUCHLIST_TYPE_TOUCHES:
    196       points = &data_.touches;
    197       break;
    198     case PP_TOUCHLIST_TYPE_CHANGEDTOUCHES:
    199       points = &data_.changed_touches;
    200       break;
    201     case PP_TOUCHLIST_TYPE_TARGETTOUCHES:
    202       points = &data_.target_touches;
    203       break;
    204     default:
    205       return PP_MakeTouchPoint();
    206   }
    207   for (size_t i = 0; i < points->size(); i++) {
    208     if (points->at(i).id == id)
    209       return points->at(i);
    210   }
    211   return PP_MakeTouchPoint();
    212 }
    213 
    214 //static
    215 PP_Resource PPB_InputEvent_Shared::CreateIMEInputEvent(
    216     ResourceObjectType type,
    217     PP_Instance instance,
    218     PP_InputEvent_Type event_type,
    219     PP_TimeTicks time_stamp,
    220     struct PP_Var text,
    221     uint32_t segment_number,
    222     const uint32_t* segment_offsets,
    223     int32_t target_segment,
    224     uint32_t selection_start,
    225     uint32_t selection_end) {
    226   if (event_type != PP_INPUTEVENT_TYPE_IME_COMPOSITION_START &&
    227       event_type != PP_INPUTEVENT_TYPE_IME_COMPOSITION_UPDATE &&
    228       event_type != PP_INPUTEVENT_TYPE_IME_COMPOSITION_END &&
    229       event_type != PP_INPUTEVENT_TYPE_IME_TEXT)
    230     return 0;
    231 
    232   InputEventData data;
    233   data.event_type = event_type;
    234   data.event_time_stamp = time_stamp;
    235   if (text.type == PP_VARTYPE_STRING) {
    236     StringVar* text_str = StringVar::FromPPVar(text);
    237     if (!text_str)
    238       return 0;
    239     data.character_text = text_str->value();
    240   }
    241   data.composition_target_segment = target_segment;
    242   if (segment_number != 0) {
    243     data.composition_segment_offsets.assign(
    244         &segment_offsets[0], &segment_offsets[segment_number + 1]);
    245   }
    246   data.composition_selection_start = selection_start;
    247   data.composition_selection_end = selection_end;
    248 
    249   return (new PPB_InputEvent_Shared(type, instance, data))->GetReference();
    250 }
    251 
    252 //static
    253 PP_Resource PPB_InputEvent_Shared::CreateKeyboardInputEvent(
    254     ResourceObjectType type,
    255     PP_Instance instance,
    256     PP_InputEvent_Type event_type,
    257     PP_TimeTicks time_stamp,
    258     uint32_t modifiers,
    259     uint32_t key_code,
    260     struct PP_Var character_text) {
    261   if (event_type != PP_INPUTEVENT_TYPE_RAWKEYDOWN &&
    262       event_type != PP_INPUTEVENT_TYPE_KEYDOWN &&
    263       event_type != PP_INPUTEVENT_TYPE_KEYUP &&
    264       event_type != PP_INPUTEVENT_TYPE_CHAR)
    265     return 0;
    266 
    267   InputEventData data;
    268   data.event_type = event_type;
    269   data.event_time_stamp = time_stamp;
    270   data.event_modifiers = modifiers;
    271   data.key_code = key_code;
    272   if (character_text.type == PP_VARTYPE_STRING) {
    273     StringVar* text_str = StringVar::FromPPVar(character_text);
    274     if (!text_str)
    275       return 0;
    276     data.character_text = text_str->value();
    277   }
    278 
    279   return (new PPB_InputEvent_Shared(type, instance, data))->GetReference();
    280 }
    281 
    282 //static
    283 PP_Resource PPB_InputEvent_Shared::CreateMouseInputEvent(
    284     ResourceObjectType type,
    285     PP_Instance instance,
    286     PP_InputEvent_Type event_type,
    287     PP_TimeTicks time_stamp,
    288     uint32_t modifiers,
    289     PP_InputEvent_MouseButton mouse_button,
    290     const PP_Point* mouse_position,
    291     int32_t click_count,
    292     const PP_Point* mouse_movement) {
    293   if (event_type != PP_INPUTEVENT_TYPE_MOUSEDOWN &&
    294       event_type != PP_INPUTEVENT_TYPE_MOUSEUP &&
    295       event_type != PP_INPUTEVENT_TYPE_MOUSEMOVE &&
    296       event_type != PP_INPUTEVENT_TYPE_MOUSEENTER &&
    297       event_type != PP_INPUTEVENT_TYPE_MOUSELEAVE)
    298     return 0;
    299 
    300   InputEventData data;
    301   data.event_type = event_type;
    302   data.event_time_stamp = time_stamp;
    303   data.event_modifiers = modifiers;
    304   data.mouse_button = mouse_button;
    305   data.mouse_position = *mouse_position;
    306   data.mouse_click_count = click_count;
    307   data.mouse_movement = *mouse_movement;
    308 
    309   return (new PPB_InputEvent_Shared(type, instance, data))->GetReference();
    310 }
    311 
    312 //static
    313 PP_Resource PPB_InputEvent_Shared::CreateWheelInputEvent(
    314     ResourceObjectType type,
    315     PP_Instance instance,
    316     PP_TimeTicks time_stamp,
    317     uint32_t modifiers,
    318     const PP_FloatPoint* wheel_delta,
    319     const PP_FloatPoint* wheel_ticks,
    320     PP_Bool scroll_by_page) {
    321   InputEventData data;
    322   data.event_type = PP_INPUTEVENT_TYPE_WHEEL;
    323   data.event_time_stamp = time_stamp;
    324   data.event_modifiers = modifiers;
    325   data.wheel_delta = *wheel_delta;
    326   data.wheel_ticks = *wheel_ticks;
    327   data.wheel_scroll_by_page = PP_ToBool(scroll_by_page);
    328 
    329   return (new PPB_InputEvent_Shared(type, instance, data))->GetReference();
    330 }
    331 
    332 // static
    333 PP_Resource PPB_InputEvent_Shared::CreateTouchInputEvent(
    334     ResourceObjectType type,
    335     PP_Instance instance,
    336     PP_InputEvent_Type event_type,
    337     PP_TimeTicks time_stamp,
    338     uint32_t modifiers) {
    339   if (event_type != PP_INPUTEVENT_TYPE_TOUCHSTART &&
    340       event_type != PP_INPUTEVENT_TYPE_TOUCHMOVE &&
    341       event_type != PP_INPUTEVENT_TYPE_TOUCHEND &&
    342       event_type != PP_INPUTEVENT_TYPE_TOUCHCANCEL)
    343     return 0;
    344 
    345   InputEventData data;
    346   data.event_type = event_type;
    347   data.event_time_stamp = time_stamp;
    348   data.event_modifiers = modifiers;
    349   return (new PPB_InputEvent_Shared(type, instance, data))->GetReference();
    350 }
    351 
    352 }  // namespace ppapi
    353