// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "ppapi/cpp/input_event.h" #include "ppapi/cpp/instance.h" #include "ppapi/cpp/module.h" #include "ppapi/cpp/module_impl.h" #include "ppapi/cpp/point.h" #include "ppapi/cpp/var.h" namespace pp { namespace { template <> const char* interface_name() { return PPB_INPUT_EVENT_INTERFACE; } template <> const char* interface_name() { return PPB_KEYBOARD_INPUT_EVENT_INTERFACE; } template <> const char* interface_name() { return PPB_MOUSE_INPUT_EVENT_INTERFACE; } template <> const char* interface_name() { return PPB_WHEEL_INPUT_EVENT_INTERFACE; } } // namespace // InputEvent ------------------------------------------------------------------ InputEvent::InputEvent() : Resource() { } InputEvent::InputEvent(PP_Resource input_event_resource) : Resource() { // Type check the input event before setting it. if (!has_interface()) return; if (get_interface()->IsInputEvent(input_event_resource)) { Module::Get()->core()->AddRefResource(input_event_resource); PassRefFromConstructor(input_event_resource); } } InputEvent::~InputEvent() { } PP_InputEvent_Type InputEvent::GetType() const { if (!has_interface()) return PP_INPUTEVENT_TYPE_UNDEFINED; return get_interface()->GetType(pp_resource()); } PP_TimeTicks InputEvent::GetTimeStamp() const { if (!has_interface()) return 0.0f; return get_interface()->GetTimeStamp(pp_resource()); } uint32_t InputEvent::GetModifiers() const { if (!has_interface()) return 0; return get_interface()->GetModifiers(pp_resource()); } // MouseInputEvent ------------------------------------------------------------- MouseInputEvent::MouseInputEvent() : InputEvent() { } MouseInputEvent::MouseInputEvent(const InputEvent& event) : InputEvent() { // Type check the input event before setting it. if (!has_interface()) return; if (get_interface()->IsMouseInputEvent( event.pp_resource())) { Module::Get()->core()->AddRefResource(event.pp_resource()); PassRefFromConstructor(event.pp_resource()); } } MouseInputEvent::MouseInputEvent(Instance* instance, PP_InputEvent_Type type, PP_TimeTicks time_stamp, uint32_t modifiers, PP_InputEvent_MouseButton mouse_button, const Point& mouse_position, int32_t click_count, const Point& mouse_movement) { // Type check the input event before setting it. if (!has_interface()) return; PassRefFromConstructor(get_interface()->Create( instance->pp_instance(), type, time_stamp, modifiers, mouse_button, &mouse_position.pp_point(), click_count, &mouse_movement.pp_point())); } PP_InputEvent_MouseButton MouseInputEvent::GetButton() const { if (!has_interface()) return PP_INPUTEVENT_MOUSEBUTTON_NONE; return get_interface()->GetButton(pp_resource()); } Point MouseInputEvent::GetPosition() const { if (!has_interface()) return Point(); return get_interface()->GetPosition(pp_resource()); } int32_t MouseInputEvent::GetClickCount() const { if (!has_interface()) return 0; return get_interface()->GetClickCount(pp_resource()); } Point MouseInputEvent::GetMovement() const { if (!has_interface()) return Point(); return get_interface()->GetMovement(pp_resource()); } // WheelInputEvent ------------------------------------------------------------- WheelInputEvent::WheelInputEvent() : InputEvent() { } WheelInputEvent::WheelInputEvent(const InputEvent& event) : InputEvent() { // Type check the input event before setting it. if (!has_interface()) return; if (get_interface()->IsWheelInputEvent( event.pp_resource())) { Module::Get()->core()->AddRefResource(event.pp_resource()); PassRefFromConstructor(event.pp_resource()); } } WheelInputEvent::WheelInputEvent(Instance* instance, PP_TimeTicks time_stamp, uint32_t modifiers, const FloatPoint& wheel_delta, const FloatPoint& wheel_ticks, bool scroll_by_page) { // Type check the input event before setting it. if (!has_interface()) return; PassRefFromConstructor(get_interface()->Create( instance->pp_instance(), time_stamp, modifiers, &wheel_delta.pp_float_point(), &wheel_ticks.pp_float_point(), PP_FromBool(scroll_by_page))); } FloatPoint WheelInputEvent::GetDelta() const { if (!has_interface()) return FloatPoint(); return get_interface()->GetDelta(pp_resource()); } FloatPoint WheelInputEvent::GetTicks() const { if (!has_interface()) return FloatPoint(); return get_interface()->GetTicks(pp_resource()); } bool WheelInputEvent::GetScrollByPage() const { if (!has_interface()) return false; return PP_ToBool( get_interface()->GetScrollByPage(pp_resource())); } // KeyboardInputEvent ---------------------------------------------------------- KeyboardInputEvent::KeyboardInputEvent() : InputEvent() { } KeyboardInputEvent::KeyboardInputEvent(const InputEvent& event) : InputEvent() { // Type check the input event before setting it. if (!has_interface()) return; if (get_interface()->IsKeyboardInputEvent( event.pp_resource())) { Module::Get()->core()->AddRefResource(event.pp_resource()); PassRefFromConstructor(event.pp_resource()); } } KeyboardInputEvent::KeyboardInputEvent(Instance* instance, PP_InputEvent_Type type, PP_TimeTicks time_stamp, uint32_t modifiers, uint32_t key_code, const Var& character_text) { // Type check the input event before setting it. if (!has_interface()) return; PassRefFromConstructor(get_interface()->Create( instance->pp_instance(), type, time_stamp, modifiers, key_code, character_text.pp_var())); } uint32_t KeyboardInputEvent::GetKeyCode() const { if (!has_interface()) return 0; return get_interface()->GetKeyCode(pp_resource()); } Var KeyboardInputEvent::GetCharacterText() const { if (!has_interface()) return Var(); return Var(Var::PassRef(), get_interface()->GetCharacterText( pp_resource())); } } // namespace pp