1/*
2 * Copyright (C) 2011 Samsung Electronics
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "WebEventFactory.h"
28
29#include <WebCore/NotImplemented.h>
30#include <WebCore/Scrollbar.h>
31
32using namespace WebCore;
33
34namespace WebKit {
35
36enum {
37 VerticalScrollDirection = 0,
38 HorizontalScrollDirection = 1
39};
40
41static inline WebEvent::Modifiers modifiersForEvent(const Evas_Modifier* modifiers)
42{
43 unsigned result = 0;
44
45 if (evas_key_modifier_is_set(modifiers, "Shift"))
46 result |= WebEvent::ShiftKey;
47 if (evas_key_modifier_is_set(modifiers, "Control"))
48 result |= WebEvent::ControlKey;
49 if (evas_key_modifier_is_set(modifiers, "Alt"))
50 result |= WebEvent::AltKey;
51 if (evas_key_modifier_is_set(modifiers, "Meta"))
52 result |= WebEvent::MetaKey;
53
54 return static_cast<WebEvent::Modifiers>(result);
55}
56
57static inline WebMouseEvent::Button buttonForEvent(int button)
58{
59 if (button == 1)
60 return WebMouseEvent::LeftButton;
61 if (button == 2)
62 return WebMouseEvent::MiddleButton;
63 if (button == 3)
64 return WebMouseEvent::RightButton;
65
66 return WebMouseEvent::NoButton;
67}
68
69static inline int clickCountForEvent(const Evas_Button_Flags flags)
70{
71 if (flags & EVAS_BUTTON_TRIPLE_CLICK)
72 return 3;
73 if (flags & EVAS_BUTTON_DOUBLE_CLICK)
74 return 2;
75
76 return 1;
77}
78
79WebMouseEvent WebEventFactory::createWebMouseEvent(const Evas_Event_Mouse_Down* event, const Evas_Point* position)
80{
81 return WebMouseEvent(WebEvent::MouseDown,
82 buttonForEvent(event->button),
83 IntPoint(event->canvas.x - position->x, event->canvas.y - position->y),
84 IntPoint(event->canvas.x, event->canvas.y),
85 0 /* deltaX */,
86 0 /* deltaY */,
87 0 /* deltaZ */,
88 clickCountForEvent(event->flags),
89 modifiersForEvent(event->modifiers),
90 event->timestamp);
91}
92
93WebMouseEvent WebEventFactory::createWebMouseEvent(const Evas_Event_Mouse_Up* event, const Evas_Point* position)
94{
95 return WebMouseEvent(WebEvent::MouseUp,
96 buttonForEvent(event->button),
97 IntPoint(event->canvas.x - position->x, event->canvas.y - position->y),
98 IntPoint(event->canvas.x, event->canvas.y),
99 0 /* deltaX */,
100 0 /* deltaY */,
101 0 /* deltaZ */,
102 clickCountForEvent(event->flags),
103 modifiersForEvent(event->modifiers),
104 event->timestamp);
105}
106
107WebMouseEvent WebEventFactory::createWebMouseEvent(const Evas_Event_Mouse_Move* event, const Evas_Point* position)
108{
109 return WebMouseEvent(WebEvent::MouseMove,
110 buttonForEvent(event->buttons),
111 IntPoint(event->cur.canvas.x - position->x, event->cur.canvas.y - position->y),
112 IntPoint(event->cur.canvas.x, event->cur.canvas.y),
113 0 /* deltaX */,
114 0 /* deltaY */,
115 0 /* deltaZ */,
116 0 /* clickCount */,
117 modifiersForEvent(event->modifiers),
118 event->timestamp);
119}
120
121WebWheelEvent WebEventFactory::createWebWheelEvent(const Evas_Event_Mouse_Wheel* event, const Evas_Point* position)
122{
123 float deltaX = 0;
124 float deltaY = 0;
125 float wheelTicksX = 0;
126 float wheelTicksY = 0;
127
128 // A negative z value means (in EFL) that we are scrolling down, so we need
129 // to invert the value.
130 if (event->direction == VerticalScrollDirection) {
131 deltaX = 0;
132 deltaY = - event->z;
133 } else if (event->direction == HorizontalScrollDirection) {
134 deltaX = - event->z;
135 deltaY = 0;
136 }
137
138 wheelTicksX = deltaX;
139 wheelTicksY = deltaY;
140 deltaX *= static_cast<float>(Scrollbar::pixelsPerLineStep());
141 deltaY *= static_cast<float>(Scrollbar::pixelsPerLineStep());
142
143 return WebWheelEvent(WebEvent::Wheel,
144 IntPoint(event->canvas.x - position->x, event->canvas.y - position->y),
145 IntPoint(event->canvas.x, event->canvas.y),
146 FloatSize(deltaX, deltaY),
147 FloatSize(wheelTicksX, wheelTicksY),
148 WebWheelEvent::ScrollByPixelWheelEvent,
149 modifiersForEvent(event->modifiers),
150 event->timestamp);
151}
152
153} // namespace WebKit