12f3c0cdceab629dbd31fbca1073b1558eb93a87
[platform/core/uifw/libds.git] / src / DSSeat / DSPointer.cpp
1 /*
2 * Copyright © 2020 Samsung Electronics co., Ltd. All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "DSPointer.h"
25 #include "DSSeat.h"
26 #include "DSWindow.h"
27 #include "DSInputEvent.h"
28 #include "DSWaylandPointer.h"
29 #include "DSWaylandSurface.h"
30 #include "DSStruct.h"
31
32 namespace display_server
33 {
34
35 DSPointer::DSPointer(DSSeat *seat)
36         : __seat(seat),
37           __dswlPointer(nullptr),
38           __dswlCompositor(nullptr),
39           __ptrFocus(nullptr)
40 {
41 }
42
43 DSPointer::DSPointer(DSSeat *seat, DSWaylandPointer *pointer)
44         : __seat(seat),
45           __dswlPointer(pointer),
46           __dswlCompositor(nullptr),
47           __ptrFocus(nullptr)
48 {
49 }
50
51 DSPointer::~DSPointer()
52 {}
53
54 void DSPointer::processEvent(DSInputMouseEvent *ev, void *data)
55 {
56         //TODO : get ptrFocus and send event to it via DSWaylandPointer instance
57
58         if (!__ptrFocus)
59         {
60                 DSLOG_ERR("DSPointer", "No ptrFocus available.");
61                 return;
62         }
63
64         if (ev->getType() == DSInputEvent::MouseInEvent)
65         {
66                 DSLOG_DBG("DSTouch", "[mouseIn] devicename: %s, timestamp: %u\n",
67                                         ev->getDevice()->getName().c_str(), ev->getTimestamp());
68                 mouseIn(ev->getX(), ev->getY());
69         }
70         else if (ev->getType() == DSInputEvent::MouseOutEvent)
71         {
72                 DSLOG_DBG("DSTouch", "[mouseOut] devicename: %s, timestamp: %u\n",
73                                         ev->getDevice()->getName().c_str(), ev->getTimestamp());
74                 mouseOut();
75         }
76         else if (ev->getType() == DSInputEvent::MouseDownEvent)
77         {
78                 DSLOG_DBG("DSTouch", "[MouseDown] devicename: %s, timestamp: %u\n",
79                                         ev->getDevice()->getName().c_str(), ev->getTimestamp());
80                 mouseDown(ev->getButton());
81         }
82         else if (ev->getType() == DSInputEvent::MouseMoveEvent)
83         {
84                 DSLOG_DBG("DSTouch", "[mouseMove] devicename: %s, timestamp: %u\n",
85                                         ev->getDevice()->getName().c_str(), ev->getTimestamp());
86                 mouseMove(ev->getX(), ev->getY());
87         }
88         else if (ev->getType() == DSInputEvent::MouseUpEvent)
89         {
90                 DSLOG_DBG("DSTouch", "[mouseUp] devicename: %s, timestamp: %u\n",
91                                         ev->getDevice()->getName().c_str(), ev->getTimestamp());
92                 mouseUp(ev->getButton());
93         }
94         else
95         {
96                 DSLOG_INF("DSPointer", "Event type %d won't be handled right now.", ev->getType());
97                 return;
98         }
99 }
100
101 void DSPointer::mouseDown(uint32_t button)
102 {
103         if (__dswlPointer)
104         {
105                 __dswlPointer->sendButtonDown(button);
106         }
107 }
108
109 void DSPointer::mouseUp(uint32_t button)
110 {
111         if (__dswlPointer)
112         {
113                 __dswlPointer->sendButtonUp(button);
114         }
115 }
116
117 void DSPointer::mouseMove(int x, int y)
118 {
119         if (__dswlPointer)
120         {
121                 __dswlPointer->sendMotion(x, y);
122         }
123 }
124
125 void DSPointer::mouseIn(int x, int y)
126 {
127         if (__dswlPointer)
128         {
129                 __dswlPointer->sendEnter(x, y);
130         }
131 }
132
133 void DSPointer::mouseOut()
134 {
135         if (__dswlPointer)
136         {
137                 __dswlPointer->sendLeave();
138         }
139 }
140
141 void DSPointer::setFocus(std::shared_ptr<DSWindow> window)
142 {
143         if (!window)
144         {
145                 DSLOG_ERR("DSPointer", "__ptrFocus has been changed (%p -> %p)", __ptrFocus, window);
146                 __ptrFocus = window;
147                 if (__dswlPointer)
148                         __dswlPointer->setFocus(nullptr);
149                 return;
150         }
151
152         DSWaylandSurface * waylandSurface = window->surface();
153
154         if (!waylandSurface)
155         {
156                 DSLOG_ERR("DSPointer", "Wayland surface of the given window is INVALID. (window : %p, surface : %p)", window, waylandSurface);
157                 return;
158         }
159
160         DSLOG_INF("DSPointer", "focus window has been changed. (%p -> %p)", __ptrFocus, window);
161         __ptrFocus = window;
162
163         if (!__dswlPointer)
164         {
165                 DSLOG_ERR("DSPointer", "waylandPointer is NOT available !");
166                 return;
167         }
168
169         __dswlPointer->setFocus(waylandSurface);
170 }
171
172 void DSPointer::resetFocus()
173 {
174         if (__ptrFocus)
175         {
176                 __ptrFocus = nullptr;
177                 DSLOG_INF("DSPointer", "ptrFocus has been reset.");
178         }
179
180         if (__dswlPointer)
181                 __dswlPointer->resetFocus();
182 }
183
184 std::shared_ptr<DSWindow> DSPointer::getFocus()
185 {
186         return __ptrFocus;
187 }
188
189 } // namespace display_server