Home | History | Annotate | Download | only in input_event
      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 <sstream>
      6 
      7 #include "custom_events.h"
      8 
      9 // Convert a given modifier to a descriptive string.  Note that the actual
     10 // declared type of modifier in each of the event classes is uint32_t, but it is
     11 // expected to be interpreted as a bitfield of 'or'ed PP_InputEvent_Modifier
     12 // values.
     13 std::string ModifierToString(uint32_t modifier) {
     14   std::string s;
     15   if (modifier & kShiftKeyModifier) {
     16     s += "shift ";
     17   }
     18   if (modifier & kControlKeyModifier) {
     19     s += "ctrl ";
     20   }
     21   if (modifier & kAltKeyModifier) {
     22     s += "alt ";
     23   }
     24   if (modifier & kMetaKeyModifer) {
     25     s += "meta ";
     26   }
     27   if (modifier & kKeyPadModifier) {
     28     s += "keypad ";
     29   }
     30   if (modifier & kAutoRepeatModifier) {
     31     s += "autorepeat ";
     32   }
     33   if (modifier & kLeftButtonModifier) {
     34     s += "left-button-down ";
     35   }
     36   if (modifier & kMiddleButtonModifier) {
     37     s += "middle-button-down ";
     38   }
     39   if (modifier & kRightButtonModifier) {
     40     s += "right-button-down ";
     41   }
     42   if (modifier & kCapsLockModifier) {
     43     s += "caps-lock ";
     44   }
     45   if (modifier & kNumLockModifier) {
     46     s += "num-lock ";
     47   }
     48   return s;
     49 }
     50 
     51 std::string KeyEvent::ToString() const {
     52   std::ostringstream stream;
     53   stream << "Key event:"
     54          << " modifier:" << string_event_modifiers()
     55          << " key_code:" << key_code_ << " time:" << timestamp_
     56          << " text:" << text_ << "\n";
     57   return stream.str();
     58 }
     59 
     60 std::string MouseEvent::ToString() const {
     61   std::ostringstream stream;
     62   stream << "Mouse event:"
     63          << " modifier:" << string_event_modifiers()
     64          << " button:" << MouseButtonToString(mouse_button_)
     65          << " x:" << x_position_ << " y:" << y_position_
     66          << " click_count:" << click_count_ << " time:" << timestamp_
     67          << " is_context_menu: " << is_context_menu_ << "\n";
     68   return stream.str();
     69 }
     70 
     71 std::string WheelEvent::ToString() const {
     72   std::ostringstream stream;
     73   stream << "Wheel event:"
     74          << " modifier:" << string_event_modifiers() << " deltax:" << delta_x_
     75          << " deltay:" << delta_y_ << " wheel_ticks_x:" << ticks_x_
     76          << " wheel_ticks_y:" << ticks_y_
     77          << " scroll_by_page: " << scroll_by_page_ << " time:" << timestamp_
     78          << "\n";
     79   return stream.str();
     80 }
     81 
     82 std::string MouseEvent::MouseButtonToString(MouseButton button) const {
     83   switch (button) {
     84     case kNone:
     85       return "None";
     86     case kLeft:
     87       return "Left";
     88     case kMiddle:
     89       return "Middle";
     90     case kRight:
     91       return "Right";
     92     default:
     93       std::ostringstream stream;
     94       stream << "Unrecognized (" << static_cast<int32_t>(button) << ")";
     95       return stream.str();
     96   }
     97 }
     98 
     99 std::string TouchEvent::KindToString(Kind kind) const {
    100   switch (kind) {
    101     case kNone:
    102       return "None";
    103     case kStart:
    104       return "Start";
    105     case kMove:
    106       return "Move";
    107     case kEnd:
    108       return "End";
    109     case kCancel:
    110       return "Cancel";
    111     default:
    112       std::ostringstream stream;
    113       stream << "Unrecognized (" << static_cast<int32_t>(kind) << ")";
    114       return stream.str();
    115   }
    116 }
    117 
    118 void TouchEvent::AddTouch(uint32_t id,
    119                           float x,
    120                           float y,
    121                           float radii_x,
    122                           float radii_y,
    123                           float angle,
    124                           float pressure) {
    125   Touch touch;
    126   touch.id = id;
    127   touch.x = x;
    128   touch.y = y;
    129   touch.radii_x = radii_x;
    130   touch.radii_y = radii_y;
    131   touch.angle = angle;
    132   touch.pressure = pressure;
    133   touches.push_back(touch);
    134 }
    135 
    136 std::string TouchEvent::ToString() const {
    137   std::ostringstream stream;
    138   stream << "Touch event:" << KindToString(kind_)
    139          << " modifier:" << string_event_modifiers();
    140   for (size_t i = 0; i < touches.size(); ++i) {
    141     const Touch& touch = touches[i];
    142     stream << " x[" << touch.id << "]:" << touch.x << " y[" << touch.id
    143            << "]:" << touch.y << " radii_x[" << touch.id
    144            << "]:" << touch.radii_x << " radii_y[" << touch.id
    145            << "]:" << touch.radii_y << " angle[" << touch.id
    146            << "]:" << touch.angle << " pressure[" << touch.id
    147            << "]:" << touch.pressure;
    148   }
    149   stream << " time:" << timestamp_ << "\n";
    150   return stream.str();
    151 }
    152