Home | History | Annotate | Download | only in cocoa
      1 // Copyright 2014 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 "ui/events/event_utils.h"
      6 
      7 #include <Cocoa/Cocoa.h>
      8 
      9 #include "base/logging.h"
     10 #include "base/time/time.h"
     11 #include "build/build_config.h"
     12 #include "ui/events/cocoa/cocoa_event_utils.h"
     13 #include "ui/events/event_utils.h"
     14 #import "ui/events/keycodes/keyboard_code_conversion_mac.h"
     15 #include "ui/gfx/point.h"
     16 #include "ui/gfx/vector2d.h"
     17 
     18 namespace ui {
     19 
     20 void UpdateDeviceList() {
     21   NOTIMPLEMENTED();
     22 }
     23 
     24 EventType EventTypeFromNative(const base::NativeEvent& native_event) {
     25   NSEventType type = [native_event type];
     26   switch (type) {
     27     case NSKeyDown:
     28       return ET_KEY_PRESSED;
     29     case NSKeyUp:
     30       return ET_KEY_RELEASED;
     31     case NSLeftMouseDown:
     32     case NSRightMouseDown:
     33     case NSOtherMouseDown:
     34       return ET_MOUSE_PRESSED;
     35     case NSLeftMouseUp:
     36     case NSRightMouseUp:
     37     case NSOtherMouseUp:
     38       return ET_MOUSE_RELEASED;
     39     case NSLeftMouseDragged:
     40     case NSRightMouseDragged:
     41     case NSOtherMouseDragged:
     42       return ET_MOUSE_DRAGGED;
     43     case NSMouseMoved:
     44       return ET_MOUSE_MOVED;
     45     case NSScrollWheel:
     46       return ET_MOUSEWHEEL;
     47     case NSMouseEntered:
     48       return ET_MOUSE_ENTERED;
     49     case NSMouseExited:
     50       return ET_MOUSE_EXITED;
     51     case NSEventTypeSwipe:
     52       return ET_SCROLL_FLING_START;
     53     case NSFlagsChanged:
     54     case NSAppKitDefined:
     55     case NSSystemDefined:
     56     case NSApplicationDefined:
     57     case NSPeriodic:
     58     case NSCursorUpdate:
     59     case NSTabletPoint:
     60     case NSTabletProximity:
     61     case NSEventTypeGesture:
     62     case NSEventTypeMagnify:
     63     case NSEventTypeRotate:
     64     case NSEventTypeBeginGesture:
     65     case NSEventTypeEndGesture:
     66       NOTIMPLEMENTED() << type;
     67       break;
     68     default:
     69       NOTIMPLEMENTED() << type;
     70       break;
     71   }
     72   return ET_UNKNOWN;
     73 }
     74 
     75 int EventFlagsFromNative(const base::NativeEvent& event) {
     76   NSUInteger modifiers = [event modifierFlags];
     77   return EventFlagsFromNSEventWithModifiers(event, modifiers);
     78 }
     79 
     80 base::TimeDelta EventTimeFromNative(const base::NativeEvent& native_event) {
     81   NSTimeInterval since_system_startup = [native_event timestamp];
     82   // Truncate to extract seconds before doing floating point arithmetic.
     83   int64_t seconds = since_system_startup;
     84   since_system_startup -= seconds;
     85   int64_t microseconds = since_system_startup * 1000000;
     86   return base::TimeDelta::FromSeconds(seconds) +
     87       base::TimeDelta::FromMicroseconds(microseconds);
     88 }
     89 
     90 gfx::Point EventLocationFromNative(const base::NativeEvent& native_event) {
     91   NSWindow* window = [native_event window];
     92   if (!window) {
     93     NOTIMPLEMENTED();  // Point will be in screen coordinates.
     94     return gfx::Point();
     95   }
     96   NSPoint location = [native_event locationInWindow];
     97   NSRect content_rect = [window contentRectForFrameRect:[window frame]];
     98   return gfx::Point(location.x, NSHeight(content_rect) - location.y);
     99 }
    100 
    101 gfx::Point EventSystemLocationFromNative(
    102     const base::NativeEvent& native_event) {
    103   NOTIMPLEMENTED();
    104   return gfx::Point();
    105 }
    106 
    107 int EventButtonFromNative(const base::NativeEvent& native_event) {
    108   NOTIMPLEMENTED();
    109   return 0;
    110 }
    111 
    112 int GetChangedMouseButtonFlagsFromNative(
    113     const base::NativeEvent& native_event) {
    114   NSEventType type = [native_event type];
    115   switch (type) {
    116     case NSLeftMouseDown:
    117     case NSLeftMouseUp:
    118     case NSLeftMouseDragged:
    119       return EF_LEFT_MOUSE_BUTTON;
    120     case NSRightMouseDown:
    121     case NSRightMouseUp:
    122     case NSRightMouseDragged:
    123       return EF_RIGHT_MOUSE_BUTTON;
    124     case NSOtherMouseDown:
    125     case NSOtherMouseUp:
    126     case NSOtherMouseDragged:
    127       return EF_MIDDLE_MOUSE_BUTTON;
    128   }
    129   return 0;
    130 }
    131 
    132 gfx::Vector2d GetMouseWheelOffset(const base::NativeEvent& event) {
    133   // Empirically, a value of 0.1 is typical for one mousewheel click. Positive
    134   // values when scrolling up or to the left. Scrolling quickly results in a
    135   // higher delta per click, up to about 15.0. (Quartz documentation suggests
    136   // +/-10).
    137   // Multiply by 1000 to vaguely approximate WHEEL_DELTA on Windows (120).
    138   const CGFloat kWheelDeltaMultiplier = 1000;
    139   return gfx::Vector2d(kWheelDeltaMultiplier * [event deltaX],
    140                        kWheelDeltaMultiplier * [event deltaY]);
    141 }
    142 
    143 base::NativeEvent CopyNativeEvent(const base::NativeEvent& event) {
    144   return [event copy];
    145 }
    146 
    147 void ReleaseCopiedNativeEvent(const base::NativeEvent& event) {
    148   [event release];
    149 }
    150 
    151 void ClearTouchIdIfReleased(const base::NativeEvent& native_event) {
    152   NOTIMPLEMENTED();
    153 }
    154 
    155 int GetTouchId(const base::NativeEvent& native_event) {
    156   NOTIMPLEMENTED();
    157   return 0;
    158 }
    159 
    160 float GetTouchRadiusX(const base::NativeEvent& native_event) {
    161   NOTIMPLEMENTED();
    162   return 0.f;
    163 }
    164 
    165 float GetTouchRadiusY(const base::NativeEvent& native_event) {
    166   NOTIMPLEMENTED();
    167   return 0.f;
    168 }
    169 
    170 float GetTouchAngle(const base::NativeEvent& native_event) {
    171   NOTIMPLEMENTED();
    172   return 0.f;
    173 }
    174 
    175 float GetTouchForce(const base::NativeEvent& native_event) {
    176   NOTIMPLEMENTED();
    177   return 0.f;
    178 }
    179 
    180 bool GetScrollOffsets(const base::NativeEvent& native_event,
    181                       float* x_offset,
    182                       float* y_offset,
    183                       float* x_offset_ordinal,
    184                       float* y_offset_ordinal,
    185                       int* finger_count) {
    186   NOTIMPLEMENTED();
    187   return false;
    188 }
    189 
    190 bool GetFlingData(const base::NativeEvent& native_event,
    191                   float* vx,
    192                   float* vy,
    193                   float* vx_ordinal,
    194                   float* vy_ordinal,
    195                   bool* is_cancel) {
    196   NOTIMPLEMENTED();
    197   return false;
    198 }
    199 
    200 bool GetGestureTimes(const base::NativeEvent& native_event,
    201                      double* start_time,
    202                      double* end_time) {
    203   NOTIMPLEMENTED();
    204   return false;
    205 }
    206 
    207 void SetNaturalScroll(bool enabled) {
    208   NOTIMPLEMENTED();
    209 }
    210 
    211 bool IsNaturalScrollEnabled() {
    212   NOTIMPLEMENTED();
    213   return false;
    214 }
    215 
    216 bool IsTouchpadEvent(const base::NativeEvent& native_event) {
    217   NOTIMPLEMENTED();
    218   return false;
    219 }
    220 
    221 KeyboardCode KeyboardCodeFromNative(const base::NativeEvent& native_event) {
    222   return KeyboardCodeFromNSEvent(native_event);
    223 }
    224 
    225 const char* CodeFromNative(const base::NativeEvent& native_event) {
    226   return CodeFromNSEvent(native_event);
    227 }
    228 
    229 uint32 PlatformKeycodeFromNative(const base::NativeEvent& native_event) {
    230   return native_event.keyCode;
    231 }
    232 
    233 }  // namespace ui
    234