xref: /OK3568_Linux_fs/external/xserver/hw/xwayland/xwayland-input.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright © 2014 Intel Corporation
3*4882a593Smuzhiyun  * Copyright © 2008 Kristian Høgsberg
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Permission to use, copy, modify, distribute, and sell this software
6*4882a593Smuzhiyun  * and its documentation for any purpose is hereby granted without
7*4882a593Smuzhiyun  * fee, provided that the above copyright notice appear in all copies
8*4882a593Smuzhiyun  * and that both that copyright notice and this permission notice
9*4882a593Smuzhiyun  * appear in supporting documentation, and that the name of the
10*4882a593Smuzhiyun  * copyright holders not be used in advertising or publicity
11*4882a593Smuzhiyun  * pertaining to distribution of the software without specific,
12*4882a593Smuzhiyun  * written prior permission.  The copyright holders make no
13*4882a593Smuzhiyun  * representations about the suitability of this software for any
14*4882a593Smuzhiyun  * purpose.  It is provided "as is" without express or implied
15*4882a593Smuzhiyun  * warranty.
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
18*4882a593Smuzhiyun  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
19*4882a593Smuzhiyun  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
20*4882a593Smuzhiyun  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21*4882a593Smuzhiyun  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
22*4882a593Smuzhiyun  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
23*4882a593Smuzhiyun  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
24*4882a593Smuzhiyun  * SOFTWARE.
25*4882a593Smuzhiyun  */
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #include "xwayland.h"
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun #include <linux/input.h>
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #include <sys/mman.h>
32*4882a593Smuzhiyun #include <xkbsrv.h>
33*4882a593Smuzhiyun #include <xserver-properties.h>
34*4882a593Smuzhiyun #include <inpututils.h>
35*4882a593Smuzhiyun #include <mipointer.h>
36*4882a593Smuzhiyun #include <mipointrst.h>
37*4882a593Smuzhiyun #include <misc.h>
38*4882a593Smuzhiyun #include "tablet-unstable-v2-client-protocol.h"
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun struct axis_discrete_pending {
41*4882a593Smuzhiyun     struct xorg_list l;
42*4882a593Smuzhiyun     uint32_t axis;
43*4882a593Smuzhiyun     int32_t discrete;
44*4882a593Smuzhiyun };
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun struct sync_pending {
47*4882a593Smuzhiyun     struct xorg_list l;
48*4882a593Smuzhiyun     DeviceIntPtr pending_dev;
49*4882a593Smuzhiyun };
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun static DevPrivateKeyRec xwl_tablet_private_key;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun static void
54*4882a593Smuzhiyun xwl_pointer_warp_emulator_handle_motion(struct xwl_pointer_warp_emulator *warp_emulator,
55*4882a593Smuzhiyun                                         double dx,
56*4882a593Smuzhiyun                                         double dy,
57*4882a593Smuzhiyun                                         double dx_unaccel,
58*4882a593Smuzhiyun                                         double dy_unaccel);
59*4882a593Smuzhiyun static void
60*4882a593Smuzhiyun xwl_pointer_warp_emulator_maybe_lock(struct xwl_pointer_warp_emulator *warp_emulator,
61*4882a593Smuzhiyun                                      struct xwl_window *xwl_window,
62*4882a593Smuzhiyun                                      SpritePtr sprite,
63*4882a593Smuzhiyun                                      int x, int y);
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun static void
66*4882a593Smuzhiyun xwl_seat_destroy_confined_pointer(struct xwl_seat *xwl_seat);
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun static void
69*4882a593Smuzhiyun init_tablet_manager_seat(struct xwl_screen *xwl_screen,
70*4882a593Smuzhiyun                          struct xwl_seat *xwl_seat);
71*4882a593Smuzhiyun static void
72*4882a593Smuzhiyun release_tablet_manager_seat(struct xwl_seat *xwl_seat);
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun static void
xwl_pointer_control(DeviceIntPtr device,PtrCtrl * ctrl)75*4882a593Smuzhiyun xwl_pointer_control(DeviceIntPtr device, PtrCtrl *ctrl)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun     /* Nothing to do, dix handles all settings */
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun static Bool
init_pointer_buttons(DeviceIntPtr device)81*4882a593Smuzhiyun init_pointer_buttons(DeviceIntPtr device)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun #define NBUTTONS 10
84*4882a593Smuzhiyun     BYTE map[NBUTTONS + 1];
85*4882a593Smuzhiyun     int i = 0;
86*4882a593Smuzhiyun     Atom btn_labels[NBUTTONS] = { 0 };
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun     for (i = 1; i <= NBUTTONS; i++)
89*4882a593Smuzhiyun         map[i] = i;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun     btn_labels[0] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_LEFT);
92*4882a593Smuzhiyun     btn_labels[1] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_MIDDLE);
93*4882a593Smuzhiyun     btn_labels[2] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_RIGHT);
94*4882a593Smuzhiyun     btn_labels[3] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_WHEEL_UP);
95*4882a593Smuzhiyun     btn_labels[4] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_WHEEL_DOWN);
96*4882a593Smuzhiyun     btn_labels[5] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_HWHEEL_LEFT);
97*4882a593Smuzhiyun     btn_labels[6] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_HWHEEL_RIGHT);
98*4882a593Smuzhiyun     /* don't know about the rest */
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun     if (!InitButtonClassDeviceStruct(device, NBUTTONS, btn_labels, map))
101*4882a593Smuzhiyun         return FALSE;
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun     return TRUE;
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun static int
xwl_pointer_proc(DeviceIntPtr device,int what)107*4882a593Smuzhiyun xwl_pointer_proc(DeviceIntPtr device, int what)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun #define NAXES 4
110*4882a593Smuzhiyun     Atom axes_labels[NAXES] = { 0 };
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun     switch (what) {
113*4882a593Smuzhiyun     case DEVICE_INIT:
114*4882a593Smuzhiyun         device->public.on = FALSE;
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun         if (!init_pointer_buttons(device))
117*4882a593Smuzhiyun             return BadValue;
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun         axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_X);
120*4882a593Smuzhiyun         axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_Y);
121*4882a593Smuzhiyun         axes_labels[2] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_HWHEEL);
122*4882a593Smuzhiyun         axes_labels[3] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_WHEEL);
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun         if (!InitValuatorClassDeviceStruct(device, NAXES, axes_labels,
125*4882a593Smuzhiyun                                            GetMotionHistorySize(), Absolute))
126*4882a593Smuzhiyun             return BadValue;
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun         /* Valuators */
129*4882a593Smuzhiyun         InitValuatorAxisStruct(device, 0, axes_labels[0],
130*4882a593Smuzhiyun                                0, 0xFFFF, 10000, 0, 10000, Absolute);
131*4882a593Smuzhiyun         InitValuatorAxisStruct(device, 1, axes_labels[1],
132*4882a593Smuzhiyun                                0, 0xFFFF, 10000, 0, 10000, Absolute);
133*4882a593Smuzhiyun         InitValuatorAxisStruct(device, 2, axes_labels[2],
134*4882a593Smuzhiyun                                NO_AXIS_LIMITS, NO_AXIS_LIMITS, 0, 0, 0, Relative);
135*4882a593Smuzhiyun         InitValuatorAxisStruct(device, 3, axes_labels[3],
136*4882a593Smuzhiyun                                NO_AXIS_LIMITS, NO_AXIS_LIMITS, 0, 0, 0, Relative);
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun         SetScrollValuator(device, 2, SCROLL_TYPE_HORIZONTAL, 1.0, SCROLL_FLAG_NONE);
139*4882a593Smuzhiyun         SetScrollValuator(device, 3, SCROLL_TYPE_VERTICAL, 1.0, SCROLL_FLAG_PREFERRED);
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun         if (!InitPtrFeedbackClassDeviceStruct(device, xwl_pointer_control))
142*4882a593Smuzhiyun             return BadValue;
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun         return Success;
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun     case DEVICE_ON:
147*4882a593Smuzhiyun         device->public.on = TRUE;
148*4882a593Smuzhiyun         return Success;
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun     case DEVICE_OFF:
151*4882a593Smuzhiyun     case DEVICE_CLOSE:
152*4882a593Smuzhiyun         device->public.on = FALSE;
153*4882a593Smuzhiyun         return Success;
154*4882a593Smuzhiyun     }
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun     return BadMatch;
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun #undef NBUTTONS
159*4882a593Smuzhiyun #undef NAXES
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun static int
xwl_pointer_proc_relative(DeviceIntPtr device,int what)163*4882a593Smuzhiyun xwl_pointer_proc_relative(DeviceIntPtr device, int what)
164*4882a593Smuzhiyun {
165*4882a593Smuzhiyun #define NAXES 2
166*4882a593Smuzhiyun     Atom axes_labels[NAXES] = { 0 };
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun     switch (what) {
169*4882a593Smuzhiyun     case DEVICE_INIT:
170*4882a593Smuzhiyun         device->public.on = FALSE;
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun         axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_X);
173*4882a593Smuzhiyun         axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_Y);
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun         /*
176*4882a593Smuzhiyun          * We'll never send buttons, but XGetPointerMapping might in certain
177*4882a593Smuzhiyun          * situations make the client think we have no buttons.
178*4882a593Smuzhiyun          */
179*4882a593Smuzhiyun         if (!init_pointer_buttons(device))
180*4882a593Smuzhiyun             return BadValue;
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun         if (!InitValuatorClassDeviceStruct(device, NAXES, axes_labels,
183*4882a593Smuzhiyun                                            GetMotionHistorySize(), Relative))
184*4882a593Smuzhiyun             return BadValue;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun         /* Valuators */
187*4882a593Smuzhiyun         InitValuatorAxisStruct(device, 0, axes_labels[0],
188*4882a593Smuzhiyun                                NO_AXIS_LIMITS, NO_AXIS_LIMITS, 1, 0, 1, Relative);
189*4882a593Smuzhiyun         InitValuatorAxisStruct(device, 1, axes_labels[1],
190*4882a593Smuzhiyun                                NO_AXIS_LIMITS, NO_AXIS_LIMITS, 1, 0, 1, Relative);
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun         if (!InitPtrFeedbackClassDeviceStruct(device, xwl_pointer_control))
193*4882a593Smuzhiyun             return BadValue;
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun         return Success;
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun     case DEVICE_ON:
198*4882a593Smuzhiyun         device->public.on = TRUE;
199*4882a593Smuzhiyun         return Success;
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun     case DEVICE_OFF:
202*4882a593Smuzhiyun     case DEVICE_CLOSE:
203*4882a593Smuzhiyun         device->public.on = FALSE;
204*4882a593Smuzhiyun         return Success;
205*4882a593Smuzhiyun     }
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun     return BadMatch;
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun #undef NAXES
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun static void
xwl_keyboard_control(DeviceIntPtr device,KeybdCtrl * ctrl)213*4882a593Smuzhiyun xwl_keyboard_control(DeviceIntPtr device, KeybdCtrl *ctrl)
214*4882a593Smuzhiyun {
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun static int
xwl_keyboard_proc(DeviceIntPtr device,int what)218*4882a593Smuzhiyun xwl_keyboard_proc(DeviceIntPtr device, int what)
219*4882a593Smuzhiyun {
220*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = device->public.devicePrivate;
221*4882a593Smuzhiyun     int len;
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun     switch (what) {
224*4882a593Smuzhiyun     case DEVICE_INIT:
225*4882a593Smuzhiyun         device->public.on = FALSE;
226*4882a593Smuzhiyun         if (xwl_seat->keymap)
227*4882a593Smuzhiyun             len = strnlen(xwl_seat->keymap, xwl_seat->keymap_size);
228*4882a593Smuzhiyun         else
229*4882a593Smuzhiyun             len = 0;
230*4882a593Smuzhiyun         if (!InitKeyboardDeviceStructFromString(device, xwl_seat->keymap,
231*4882a593Smuzhiyun                                                 len,
232*4882a593Smuzhiyun                                                 NULL, xwl_keyboard_control))
233*4882a593Smuzhiyun             return BadValue;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun         return Success;
236*4882a593Smuzhiyun     case DEVICE_ON:
237*4882a593Smuzhiyun         device->public.on = TRUE;
238*4882a593Smuzhiyun         return Success;
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun     case DEVICE_OFF:
241*4882a593Smuzhiyun     case DEVICE_CLOSE:
242*4882a593Smuzhiyun         device->public.on = FALSE;
243*4882a593Smuzhiyun         return Success;
244*4882a593Smuzhiyun     }
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun     return BadMatch;
247*4882a593Smuzhiyun }
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun static int
xwl_touch_proc(DeviceIntPtr device,int what)250*4882a593Smuzhiyun xwl_touch_proc(DeviceIntPtr device, int what)
251*4882a593Smuzhiyun {
252*4882a593Smuzhiyun #define NTOUCHPOINTS 20
253*4882a593Smuzhiyun #define NBUTTONS 1
254*4882a593Smuzhiyun #define NAXES 2
255*4882a593Smuzhiyun     Atom btn_labels[NBUTTONS] = { 0 };
256*4882a593Smuzhiyun     Atom axes_labels[NAXES] = { 0 };
257*4882a593Smuzhiyun     BYTE map[NBUTTONS + 1] = { 0 };
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun     switch (what) {
260*4882a593Smuzhiyun     case DEVICE_INIT:
261*4882a593Smuzhiyun         device->public.on = FALSE;
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun         axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_MT_POSITION_X);
264*4882a593Smuzhiyun         axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_MT_POSITION_Y);
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun         if (!InitValuatorClassDeviceStruct(device, NAXES, axes_labels,
267*4882a593Smuzhiyun                                            GetMotionHistorySize(), Absolute))
268*4882a593Smuzhiyun             return BadValue;
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun         if (!InitButtonClassDeviceStruct(device, NBUTTONS, btn_labels, map))
271*4882a593Smuzhiyun             return BadValue;
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun         if (!InitTouchClassDeviceStruct(device, NTOUCHPOINTS,
274*4882a593Smuzhiyun                                         XIDirectTouch, NAXES))
275*4882a593Smuzhiyun             return BadValue;
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun         /* Valuators */
278*4882a593Smuzhiyun         InitValuatorAxisStruct(device, 0, axes_labels[0],
279*4882a593Smuzhiyun                                0, 0xFFFF, 10000, 0, 10000, Absolute);
280*4882a593Smuzhiyun         InitValuatorAxisStruct(device, 1, axes_labels[1],
281*4882a593Smuzhiyun                                0, 0xFFFF, 10000, 0, 10000, Absolute);
282*4882a593Smuzhiyun         return Success;
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun     case DEVICE_ON:
285*4882a593Smuzhiyun         device->public.on = TRUE;
286*4882a593Smuzhiyun         return Success;
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun     case DEVICE_OFF:
289*4882a593Smuzhiyun     case DEVICE_CLOSE:
290*4882a593Smuzhiyun         device->public.on = FALSE;
291*4882a593Smuzhiyun         return Success;
292*4882a593Smuzhiyun     }
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun     return BadMatch;
295*4882a593Smuzhiyun #undef NAXES
296*4882a593Smuzhiyun #undef NBUTTONS
297*4882a593Smuzhiyun #undef NTOUCHPOINTS
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun static int
xwl_tablet_proc(DeviceIntPtr device,int what)301*4882a593Smuzhiyun xwl_tablet_proc(DeviceIntPtr device, int what)
302*4882a593Smuzhiyun {
303*4882a593Smuzhiyun #define NBUTTONS 9
304*4882a593Smuzhiyun #define NAXES 6
305*4882a593Smuzhiyun     Atom btn_labels[NBUTTONS] = { 0 };
306*4882a593Smuzhiyun     Atom axes_labels[NAXES] = { 0 };
307*4882a593Smuzhiyun     BYTE map[NBUTTONS + 1] = { 0 };
308*4882a593Smuzhiyun     int i;
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun     switch (what) {
311*4882a593Smuzhiyun     case DEVICE_INIT:
312*4882a593Smuzhiyun         device->public.on = FALSE;
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun         for (i = 1; i <= NBUTTONS; i++)
315*4882a593Smuzhiyun             map[i] = i;
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun         axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_X);
318*4882a593Smuzhiyun         axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_Y);
319*4882a593Smuzhiyun         axes_labels[2] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_PRESSURE);
320*4882a593Smuzhiyun         axes_labels[3] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_TILT_X);
321*4882a593Smuzhiyun         axes_labels[4] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_TILT_Y);
322*4882a593Smuzhiyun         axes_labels[5] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_WHEEL);
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun         if (!InitValuatorClassDeviceStruct(device, NAXES, axes_labels,
325*4882a593Smuzhiyun                                            GetMotionHistorySize(), Absolute))
326*4882a593Smuzhiyun             return BadValue;
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun         /* Valuators - match the xf86-input-wacom ranges */
329*4882a593Smuzhiyun         InitValuatorAxisStruct(device, 0, axes_labels[0],
330*4882a593Smuzhiyun                                0, 262143, 10000, 0, 10000, Absolute);
331*4882a593Smuzhiyun         InitValuatorAxisStruct(device, 1, axes_labels[1],
332*4882a593Smuzhiyun                                0, 262143, 10000, 0, 10000, Absolute);
333*4882a593Smuzhiyun         /* pressure */
334*4882a593Smuzhiyun         InitValuatorAxisStruct(device, 2, axes_labels[2],
335*4882a593Smuzhiyun                                0, 65535, 1, 0, 1, Absolute);
336*4882a593Smuzhiyun         /* tilt x */
337*4882a593Smuzhiyun         InitValuatorAxisStruct(device, 3, axes_labels[3],
338*4882a593Smuzhiyun                                -64, 63, 57, 0, 57, Absolute);
339*4882a593Smuzhiyun         /* tilt y */
340*4882a593Smuzhiyun         InitValuatorAxisStruct(device, 4, axes_labels[4],
341*4882a593Smuzhiyun                                -64, 63, 57, 0, 57, Absolute);
342*4882a593Smuzhiyun         /* abs wheel (airbrush) or rotation (artpen) */
343*4882a593Smuzhiyun         InitValuatorAxisStruct(device, 5, axes_labels[5],
344*4882a593Smuzhiyun                                -900, 899, 1, 0, 1, Absolute);
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun         if (!InitPtrFeedbackClassDeviceStruct(device, xwl_pointer_control))
347*4882a593Smuzhiyun             return BadValue;
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun         if (!InitButtonClassDeviceStruct(device, NBUTTONS, btn_labels, map))
350*4882a593Smuzhiyun             return BadValue;
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun         return Success;
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun     case DEVICE_ON:
355*4882a593Smuzhiyun         device->public.on = TRUE;
356*4882a593Smuzhiyun         return Success;
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun     case DEVICE_OFF:
359*4882a593Smuzhiyun     case DEVICE_CLOSE:
360*4882a593Smuzhiyun         device->public.on = FALSE;
361*4882a593Smuzhiyun         return Success;
362*4882a593Smuzhiyun     }
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun     return BadMatch;
365*4882a593Smuzhiyun #undef NAXES
366*4882a593Smuzhiyun #undef NBUTTONS
367*4882a593Smuzhiyun }
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun static void
pointer_handle_enter(void * data,struct wl_pointer * pointer,uint32_t serial,struct wl_surface * surface,wl_fixed_t sx_w,wl_fixed_t sy_w)370*4882a593Smuzhiyun pointer_handle_enter(void *data, struct wl_pointer *pointer,
371*4882a593Smuzhiyun                      uint32_t serial, struct wl_surface *surface,
372*4882a593Smuzhiyun                      wl_fixed_t sx_w, wl_fixed_t sy_w)
373*4882a593Smuzhiyun {
374*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = data;
375*4882a593Smuzhiyun     DeviceIntPtr dev = xwl_seat->pointer;
376*4882a593Smuzhiyun     DeviceIntPtr master;
377*4882a593Smuzhiyun     int i;
378*4882a593Smuzhiyun     int sx = wl_fixed_to_int(sx_w);
379*4882a593Smuzhiyun     int sy = wl_fixed_to_int(sy_w);
380*4882a593Smuzhiyun     int dx, dy;
381*4882a593Smuzhiyun     ScreenPtr pScreen = xwl_seat->xwl_screen->screen;
382*4882a593Smuzhiyun     ValuatorMask mask;
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun     /* There's a race here where if we create and then immediately
385*4882a593Smuzhiyun      * destroy a surface, we might end up in a state where the Wayland
386*4882a593Smuzhiyun      * compositor sends us an event for a surface that doesn't exist.
387*4882a593Smuzhiyun      *
388*4882a593Smuzhiyun      * Don't process enter events in this case.
389*4882a593Smuzhiyun      */
390*4882a593Smuzhiyun     if (surface == NULL)
391*4882a593Smuzhiyun         return;
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun     xwl_seat->xwl_screen->serial = serial;
394*4882a593Smuzhiyun     xwl_seat->pointer_enter_serial = serial;
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun     xwl_seat->focus_window = wl_surface_get_user_data(surface);
397*4882a593Smuzhiyun     dx = xwl_seat->focus_window->window->drawable.x;
398*4882a593Smuzhiyun     dy = xwl_seat->focus_window->window->drawable.y;
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun     /* We just entered a new xwindow, forget about the old last xwindow */
401*4882a593Smuzhiyun     xwl_seat->last_xwindow = NullWindow;
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun     master = GetMaster(dev, POINTER_OR_FLOAT);
404*4882a593Smuzhiyun     (*pScreen->SetCursorPosition) (dev, pScreen, dx + sx, dy + sy, TRUE);
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun     miPointerInvalidateSprite(master);
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun     CheckMotion(NULL, master);
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun     /* Ideally, X clients shouldn't see these button releases.  When
411*4882a593Smuzhiyun      * the pointer leaves a window with buttons down, it means that
412*4882a593Smuzhiyun      * the wayland compositor has grabbed the pointer.  The button
413*4882a593Smuzhiyun      * release event is consumed by whatever grab in the compositor
414*4882a593Smuzhiyun      * and won't be sent to clients (the X server is a client).
415*4882a593Smuzhiyun      * However, we need to reset X's idea of which buttons are up and
416*4882a593Smuzhiyun      * down, and they're all up (by definition) when the pointer
417*4882a593Smuzhiyun      * enters a window.  We should figure out a way to swallow these
418*4882a593Smuzhiyun      * events, perhaps using an X grab whenever the pointer is not in
419*4882a593Smuzhiyun      * any X window, but for now just send the events. */
420*4882a593Smuzhiyun     valuator_mask_zero(&mask);
421*4882a593Smuzhiyun     for (i = 0; i < dev->button->numButtons; i++)
422*4882a593Smuzhiyun         if (BitIsOn(dev->button->down, i))
423*4882a593Smuzhiyun             QueuePointerEvents(dev, ButtonRelease, i, 0, &mask);
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun     /* The last cursor frame we commited before the pointer left one
426*4882a593Smuzhiyun      * of our surfaces might not have been shown. In that case we'll
427*4882a593Smuzhiyun      * have a cursor surface frame callback pending which we need to
428*4882a593Smuzhiyun      * clear so that we can continue submitting new cursor frames. */
429*4882a593Smuzhiyun     if (xwl_seat->cursor.frame_cb) {
430*4882a593Smuzhiyun         wl_callback_destroy(xwl_seat->cursor.frame_cb);
431*4882a593Smuzhiyun         xwl_seat->cursor.frame_cb = NULL;
432*4882a593Smuzhiyun         xwl_seat_set_cursor(xwl_seat);
433*4882a593Smuzhiyun     }
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun     if (xwl_seat->pointer_warp_emulator) {
436*4882a593Smuzhiyun         xwl_pointer_warp_emulator_maybe_lock(xwl_seat->pointer_warp_emulator,
437*4882a593Smuzhiyun                                              xwl_seat->focus_window,
438*4882a593Smuzhiyun                                              NULL, 0, 0);
439*4882a593Smuzhiyun     }
440*4882a593Smuzhiyun }
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun static void
pointer_handle_leave(void * data,struct wl_pointer * pointer,uint32_t serial,struct wl_surface * surface)443*4882a593Smuzhiyun pointer_handle_leave(void *data, struct wl_pointer *pointer,
444*4882a593Smuzhiyun                      uint32_t serial, struct wl_surface *surface)
445*4882a593Smuzhiyun {
446*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = data;
447*4882a593Smuzhiyun     DeviceIntPtr dev = xwl_seat->pointer;
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun     xwl_seat->xwl_screen->serial = serial;
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun     /* The pointer has left a known xwindow, save it for a possible match
452*4882a593Smuzhiyun      * in sprite_check_lost_focus()
453*4882a593Smuzhiyun      */
454*4882a593Smuzhiyun     if (xwl_seat->focus_window) {
455*4882a593Smuzhiyun         xwl_seat->last_xwindow = xwl_seat->focus_window->window;
456*4882a593Smuzhiyun         xwl_seat->focus_window = NULL;
457*4882a593Smuzhiyun         CheckMotion(NULL, GetMaster(dev, POINTER_OR_FLOAT));
458*4882a593Smuzhiyun     }
459*4882a593Smuzhiyun }
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun static void
dispatch_pointer_motion_event(struct xwl_seat * xwl_seat)462*4882a593Smuzhiyun dispatch_pointer_motion_event(struct xwl_seat *xwl_seat)
463*4882a593Smuzhiyun {
464*4882a593Smuzhiyun     ValuatorMask mask;
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun     if (xwl_seat->pointer_warp_emulator &&
467*4882a593Smuzhiyun         xwl_seat->pending_pointer_event.has_relative) {
468*4882a593Smuzhiyun         double dx;
469*4882a593Smuzhiyun         double dy;
470*4882a593Smuzhiyun         double dx_unaccel;
471*4882a593Smuzhiyun         double dy_unaccel;
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun         dx = xwl_seat->pending_pointer_event.dx;
474*4882a593Smuzhiyun         dy = xwl_seat->pending_pointer_event.dy;
475*4882a593Smuzhiyun         dx_unaccel = xwl_seat->pending_pointer_event.dx_unaccel;
476*4882a593Smuzhiyun         dy_unaccel = xwl_seat->pending_pointer_event.dy_unaccel;
477*4882a593Smuzhiyun         xwl_pointer_warp_emulator_handle_motion(xwl_seat->pointer_warp_emulator,
478*4882a593Smuzhiyun                                                 dx, dy,
479*4882a593Smuzhiyun                                                 dx_unaccel, dy_unaccel);
480*4882a593Smuzhiyun     } else if (xwl_seat->pending_pointer_event.has_absolute ||
481*4882a593Smuzhiyun                xwl_seat->pending_pointer_event.has_relative) {
482*4882a593Smuzhiyun         int x;
483*4882a593Smuzhiyun         int y;
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun         if (xwl_seat->pending_pointer_event.has_absolute) {
486*4882a593Smuzhiyun             int sx = wl_fixed_to_int(xwl_seat->pending_pointer_event.x);
487*4882a593Smuzhiyun             int sy = wl_fixed_to_int(xwl_seat->pending_pointer_event.y);
488*4882a593Smuzhiyun             int dx = xwl_seat->focus_window->window->drawable.x;
489*4882a593Smuzhiyun             int dy = xwl_seat->focus_window->window->drawable.y;
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun             x = dx + sx;
492*4882a593Smuzhiyun             y = dy + sy;
493*4882a593Smuzhiyun         } else {
494*4882a593Smuzhiyun             miPointerGetPosition(xwl_seat->pointer, &x, &y);
495*4882a593Smuzhiyun         }
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun         valuator_mask_zero(&mask);
498*4882a593Smuzhiyun         if (xwl_seat->pending_pointer_event.has_relative) {
499*4882a593Smuzhiyun             double dx_unaccel;
500*4882a593Smuzhiyun             double dy_unaccel;
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun             dx_unaccel = xwl_seat->pending_pointer_event.dx_unaccel;
503*4882a593Smuzhiyun             dy_unaccel = xwl_seat->pending_pointer_event.dy_unaccel;
504*4882a593Smuzhiyun             valuator_mask_set_absolute_unaccelerated(&mask, 0, x, dx_unaccel);
505*4882a593Smuzhiyun             valuator_mask_set_absolute_unaccelerated(&mask, 1, y, dy_unaccel);
506*4882a593Smuzhiyun         } else {
507*4882a593Smuzhiyun             valuator_mask_set(&mask, 0, x);
508*4882a593Smuzhiyun             valuator_mask_set(&mask, 1, y);
509*4882a593Smuzhiyun         }
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun         QueuePointerEvents(xwl_seat->pointer, MotionNotify, 0,
512*4882a593Smuzhiyun                            POINTER_ABSOLUTE | POINTER_SCREEN, &mask);
513*4882a593Smuzhiyun     }
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun     xwl_seat->pending_pointer_event.has_absolute = FALSE;
516*4882a593Smuzhiyun     xwl_seat->pending_pointer_event.has_relative = FALSE;
517*4882a593Smuzhiyun }
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun static void
pointer_handle_motion(void * data,struct wl_pointer * pointer,uint32_t time,wl_fixed_t sx_w,wl_fixed_t sy_w)520*4882a593Smuzhiyun pointer_handle_motion(void *data, struct wl_pointer *pointer,
521*4882a593Smuzhiyun                       uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
522*4882a593Smuzhiyun {
523*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = data;
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun     if (!xwl_seat->focus_window)
526*4882a593Smuzhiyun         return;
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun     xwl_seat->pending_pointer_event.has_absolute = TRUE;
529*4882a593Smuzhiyun     xwl_seat->pending_pointer_event.x = sx_w;
530*4882a593Smuzhiyun     xwl_seat->pending_pointer_event.y = sy_w;
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun     if (wl_proxy_get_version((struct wl_proxy *) xwl_seat->wl_pointer) < 5)
533*4882a593Smuzhiyun         dispatch_pointer_motion_event(xwl_seat);
534*4882a593Smuzhiyun }
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun static void
pointer_handle_button(void * data,struct wl_pointer * pointer,uint32_t serial,uint32_t time,uint32_t button,uint32_t state)537*4882a593Smuzhiyun pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
538*4882a593Smuzhiyun                       uint32_t time, uint32_t button, uint32_t state)
539*4882a593Smuzhiyun {
540*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = data;
541*4882a593Smuzhiyun     int index;
542*4882a593Smuzhiyun     ValuatorMask mask;
543*4882a593Smuzhiyun 
544*4882a593Smuzhiyun     xwl_seat->xwl_screen->serial = serial;
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun     switch (button) {
547*4882a593Smuzhiyun     case BTN_LEFT:
548*4882a593Smuzhiyun         index = 1;
549*4882a593Smuzhiyun         break;
550*4882a593Smuzhiyun     case BTN_MIDDLE:
551*4882a593Smuzhiyun         index = 2;
552*4882a593Smuzhiyun         break;
553*4882a593Smuzhiyun     case BTN_RIGHT:
554*4882a593Smuzhiyun         index = 3;
555*4882a593Smuzhiyun         break;
556*4882a593Smuzhiyun     default:
557*4882a593Smuzhiyun         /* Skip indexes 4-7: they are used for vertical and horizontal scroll.
558*4882a593Smuzhiyun            The rest of the buttons go in order: BTN_SIDE becomes 8, etc. */
559*4882a593Smuzhiyun         index = 8 + button - BTN_SIDE;
560*4882a593Smuzhiyun         break;
561*4882a593Smuzhiyun     }
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun     valuator_mask_zero(&mask);
564*4882a593Smuzhiyun     QueuePointerEvents(xwl_seat->pointer,
565*4882a593Smuzhiyun                        state ? ButtonPress : ButtonRelease, index, 0, &mask);
566*4882a593Smuzhiyun }
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun static void
pointer_handle_axis(void * data,struct wl_pointer * pointer,uint32_t time,uint32_t axis,wl_fixed_t value)569*4882a593Smuzhiyun pointer_handle_axis(void *data, struct wl_pointer *pointer,
570*4882a593Smuzhiyun                     uint32_t time, uint32_t axis, wl_fixed_t value)
571*4882a593Smuzhiyun {
572*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = data;
573*4882a593Smuzhiyun     int index;
574*4882a593Smuzhiyun     const int divisor = 10;
575*4882a593Smuzhiyun     ValuatorMask mask;
576*4882a593Smuzhiyun     struct axis_discrete_pending *pending = NULL;
577*4882a593Smuzhiyun     struct axis_discrete_pending *iter;
578*4882a593Smuzhiyun 
579*4882a593Smuzhiyun     switch (axis) {
580*4882a593Smuzhiyun     case WL_POINTER_AXIS_VERTICAL_SCROLL:
581*4882a593Smuzhiyun         index = 3;
582*4882a593Smuzhiyun         break;
583*4882a593Smuzhiyun     case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
584*4882a593Smuzhiyun         index = 2;
585*4882a593Smuzhiyun         break;
586*4882a593Smuzhiyun     default:
587*4882a593Smuzhiyun         return;
588*4882a593Smuzhiyun     }
589*4882a593Smuzhiyun 
590*4882a593Smuzhiyun     xorg_list_for_each_entry(iter, &xwl_seat->axis_discrete_pending, l) {
591*4882a593Smuzhiyun         if (iter->axis == axis) {
592*4882a593Smuzhiyun             pending = iter;
593*4882a593Smuzhiyun             break;
594*4882a593Smuzhiyun         }
595*4882a593Smuzhiyun     }
596*4882a593Smuzhiyun 
597*4882a593Smuzhiyun     valuator_mask_zero(&mask);
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun     if (pending) {
600*4882a593Smuzhiyun         valuator_mask_set(&mask, index, pending->discrete);
601*4882a593Smuzhiyun         xorg_list_del(&pending->l);
602*4882a593Smuzhiyun         free(pending);
603*4882a593Smuzhiyun     } else {
604*4882a593Smuzhiyun         valuator_mask_set_double(&mask, index, wl_fixed_to_double(value) / divisor);
605*4882a593Smuzhiyun     }
606*4882a593Smuzhiyun     QueuePointerEvents(xwl_seat->pointer, MotionNotify, 0, POINTER_RELATIVE, &mask);
607*4882a593Smuzhiyun }
608*4882a593Smuzhiyun 
609*4882a593Smuzhiyun static void
pointer_handle_frame(void * data,struct wl_pointer * wl_pointer)610*4882a593Smuzhiyun pointer_handle_frame(void *data, struct wl_pointer *wl_pointer)
611*4882a593Smuzhiyun {
612*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = data;
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun     if (!xwl_seat->focus_window)
615*4882a593Smuzhiyun         return;
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun     dispatch_pointer_motion_event(xwl_seat);
618*4882a593Smuzhiyun }
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun static void
pointer_handle_axis_source(void * data,struct wl_pointer * wl_pointer,uint32_t axis_source)621*4882a593Smuzhiyun pointer_handle_axis_source(void *data, struct wl_pointer *wl_pointer, uint32_t axis_source)
622*4882a593Smuzhiyun {
623*4882a593Smuzhiyun }
624*4882a593Smuzhiyun 
625*4882a593Smuzhiyun static void
pointer_handle_axis_stop(void * data,struct wl_pointer * wl_pointer,uint32_t time,uint32_t axis)626*4882a593Smuzhiyun pointer_handle_axis_stop(void *data, struct wl_pointer *wl_pointer,
627*4882a593Smuzhiyun                          uint32_t time, uint32_t axis)
628*4882a593Smuzhiyun {
629*4882a593Smuzhiyun }
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun static void
pointer_handle_axis_discrete(void * data,struct wl_pointer * wl_pointer,uint32_t axis,int32_t discrete)632*4882a593Smuzhiyun pointer_handle_axis_discrete(void *data, struct wl_pointer *wl_pointer,
633*4882a593Smuzhiyun                              uint32_t axis, int32_t discrete)
634*4882a593Smuzhiyun {
635*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = data;
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun     struct axis_discrete_pending *pending = malloc(sizeof *pending);
638*4882a593Smuzhiyun     if (!pending)
639*4882a593Smuzhiyun         return;
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun     pending->axis = axis;
642*4882a593Smuzhiyun     pending->discrete = discrete;
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun     xorg_list_add(&pending->l, &xwl_seat->axis_discrete_pending);
645*4882a593Smuzhiyun }
646*4882a593Smuzhiyun 
647*4882a593Smuzhiyun static const struct wl_pointer_listener pointer_listener = {
648*4882a593Smuzhiyun     pointer_handle_enter,
649*4882a593Smuzhiyun     pointer_handle_leave,
650*4882a593Smuzhiyun     pointer_handle_motion,
651*4882a593Smuzhiyun     pointer_handle_button,
652*4882a593Smuzhiyun     pointer_handle_axis,
653*4882a593Smuzhiyun     pointer_handle_frame,
654*4882a593Smuzhiyun     pointer_handle_axis_source,
655*4882a593Smuzhiyun     pointer_handle_axis_stop,
656*4882a593Smuzhiyun     pointer_handle_axis_discrete,
657*4882a593Smuzhiyun };
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun static void
relative_pointer_handle_relative_motion(void * data,struct zwp_relative_pointer_v1 * zwp_relative_pointer_v1,uint32_t utime_hi,uint32_t utime_lo,wl_fixed_t dxf,wl_fixed_t dyf,wl_fixed_t dx_unaccelf,wl_fixed_t dy_unaccelf)660*4882a593Smuzhiyun relative_pointer_handle_relative_motion(void *data,
661*4882a593Smuzhiyun                                         struct zwp_relative_pointer_v1 *zwp_relative_pointer_v1,
662*4882a593Smuzhiyun                                         uint32_t utime_hi,
663*4882a593Smuzhiyun                                         uint32_t utime_lo,
664*4882a593Smuzhiyun                                         wl_fixed_t dxf,
665*4882a593Smuzhiyun                                         wl_fixed_t dyf,
666*4882a593Smuzhiyun                                         wl_fixed_t dx_unaccelf,
667*4882a593Smuzhiyun                                         wl_fixed_t dy_unaccelf)
668*4882a593Smuzhiyun {
669*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = data;
670*4882a593Smuzhiyun 
671*4882a593Smuzhiyun     xwl_seat->pending_pointer_event.has_relative = TRUE;
672*4882a593Smuzhiyun     xwl_seat->pending_pointer_event.dx = wl_fixed_to_double(dxf);
673*4882a593Smuzhiyun     xwl_seat->pending_pointer_event.dy = wl_fixed_to_double(dyf);
674*4882a593Smuzhiyun     xwl_seat->pending_pointer_event.dx_unaccel = wl_fixed_to_double(dx_unaccelf);
675*4882a593Smuzhiyun     xwl_seat->pending_pointer_event.dy_unaccel = wl_fixed_to_double(dy_unaccelf);
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun     if (!xwl_seat->focus_window)
678*4882a593Smuzhiyun         return;
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun     if (wl_proxy_get_version((struct wl_proxy *) xwl_seat->wl_pointer) < 5)
681*4882a593Smuzhiyun         dispatch_pointer_motion_event(xwl_seat);
682*4882a593Smuzhiyun }
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun static const struct zwp_relative_pointer_v1_listener relative_pointer_listener = {
685*4882a593Smuzhiyun     relative_pointer_handle_relative_motion,
686*4882a593Smuzhiyun };
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun static void
keyboard_handle_key(void * data,struct wl_keyboard * keyboard,uint32_t serial,uint32_t time,uint32_t key,uint32_t state)689*4882a593Smuzhiyun keyboard_handle_key(void *data, struct wl_keyboard *keyboard, uint32_t serial,
690*4882a593Smuzhiyun                     uint32_t time, uint32_t key, uint32_t state)
691*4882a593Smuzhiyun {
692*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = data;
693*4882a593Smuzhiyun     uint32_t *k, *end;
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun     xwl_seat->xwl_screen->serial = serial;
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun     end = (uint32_t *) ((char *) xwl_seat->keys.data + xwl_seat->keys.size);
698*4882a593Smuzhiyun     for (k = xwl_seat->keys.data; k < end; k++) {
699*4882a593Smuzhiyun         if (*k == key)
700*4882a593Smuzhiyun             *k = *--end;
701*4882a593Smuzhiyun     }
702*4882a593Smuzhiyun     xwl_seat->keys.size = (char *) end - (char *) xwl_seat->keys.data;
703*4882a593Smuzhiyun     if (state) {
704*4882a593Smuzhiyun         k = wl_array_add(&xwl_seat->keys, sizeof *k);
705*4882a593Smuzhiyun         *k = key;
706*4882a593Smuzhiyun     }
707*4882a593Smuzhiyun 
708*4882a593Smuzhiyun     QueueKeyboardEvents(xwl_seat->keyboard,
709*4882a593Smuzhiyun                         state ? KeyPress : KeyRelease, key + 8);
710*4882a593Smuzhiyun }
711*4882a593Smuzhiyun 
712*4882a593Smuzhiyun static void
keyboard_handle_keymap(void * data,struct wl_keyboard * keyboard,uint32_t format,int fd,uint32_t size)713*4882a593Smuzhiyun keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
714*4882a593Smuzhiyun                        uint32_t format, int fd, uint32_t size)
715*4882a593Smuzhiyun {
716*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = data;
717*4882a593Smuzhiyun     DeviceIntPtr master;
718*4882a593Smuzhiyun     XkbDescPtr xkb;
719*4882a593Smuzhiyun     XkbChangesRec changes = { 0 };
720*4882a593Smuzhiyun 
721*4882a593Smuzhiyun     if (xwl_seat->keymap)
722*4882a593Smuzhiyun         munmap(xwl_seat->keymap, xwl_seat->keymap_size);
723*4882a593Smuzhiyun 
724*4882a593Smuzhiyun     xwl_seat->keymap_size = size;
725*4882a593Smuzhiyun     xwl_seat->keymap = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
726*4882a593Smuzhiyun     if (xwl_seat->keymap == MAP_FAILED) {
727*4882a593Smuzhiyun         xwl_seat->keymap_size = 0;
728*4882a593Smuzhiyun         xwl_seat->keymap = NULL;
729*4882a593Smuzhiyun         goto out;
730*4882a593Smuzhiyun     }
731*4882a593Smuzhiyun 
732*4882a593Smuzhiyun     xkb = XkbCompileKeymapFromString(xwl_seat->keyboard, xwl_seat->keymap,
733*4882a593Smuzhiyun                                      strnlen(xwl_seat->keymap,
734*4882a593Smuzhiyun                                              xwl_seat->keymap_size));
735*4882a593Smuzhiyun     if (!xkb)
736*4882a593Smuzhiyun         goto out;
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun     XkbUpdateDescActions(xkb, xkb->min_key_code, XkbNumKeys(xkb), &changes);
739*4882a593Smuzhiyun 
740*4882a593Smuzhiyun     if (xwl_seat->keyboard->key)
741*4882a593Smuzhiyun         /* Keep the current controls */
742*4882a593Smuzhiyun         XkbCopyControls(xkb, xwl_seat->keyboard->key->xkbInfo->desc);
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun     XkbDeviceApplyKeymap(xwl_seat->keyboard, xkb);
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun     master = GetMaster(xwl_seat->keyboard, MASTER_KEYBOARD);
747*4882a593Smuzhiyun     if (master)
748*4882a593Smuzhiyun         XkbDeviceApplyKeymap(master, xkb);
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun     XkbFreeKeyboard(xkb, XkbAllComponentsMask, TRUE);
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun  out:
753*4882a593Smuzhiyun     close(fd);
754*4882a593Smuzhiyun }
755*4882a593Smuzhiyun 
756*4882a593Smuzhiyun static void
keyboard_handle_enter(void * data,struct wl_keyboard * keyboard,uint32_t serial,struct wl_surface * surface,struct wl_array * keys)757*4882a593Smuzhiyun keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
758*4882a593Smuzhiyun                       uint32_t serial,
759*4882a593Smuzhiyun                       struct wl_surface *surface, struct wl_array *keys)
760*4882a593Smuzhiyun {
761*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = data;
762*4882a593Smuzhiyun     uint32_t *k;
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun     xwl_seat->xwl_screen->serial = serial;
765*4882a593Smuzhiyun     xwl_seat->keyboard_focus = surface;
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun     wl_array_copy(&xwl_seat->keys, keys);
768*4882a593Smuzhiyun     wl_array_for_each(k, &xwl_seat->keys)
769*4882a593Smuzhiyun         QueueKeyboardEvents(xwl_seat->keyboard, EnterNotify, *k + 8);
770*4882a593Smuzhiyun }
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun static void
keyboard_handle_leave(void * data,struct wl_keyboard * keyboard,uint32_t serial,struct wl_surface * surface)773*4882a593Smuzhiyun keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
774*4882a593Smuzhiyun                       uint32_t serial, struct wl_surface *surface)
775*4882a593Smuzhiyun {
776*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = data;
777*4882a593Smuzhiyun     uint32_t *k;
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun     xwl_seat->xwl_screen->serial = serial;
780*4882a593Smuzhiyun 
781*4882a593Smuzhiyun     wl_array_for_each(k, &xwl_seat->keys)
782*4882a593Smuzhiyun         QueueKeyboardEvents(xwl_seat->keyboard, LeaveNotify, *k + 8);
783*4882a593Smuzhiyun 
784*4882a593Smuzhiyun     xwl_seat->keyboard_focus = NULL;
785*4882a593Smuzhiyun }
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun static void
keyboard_handle_modifiers(void * data,struct wl_keyboard * keyboard,uint32_t serial,uint32_t mods_depressed,uint32_t mods_latched,uint32_t mods_locked,uint32_t group)788*4882a593Smuzhiyun keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
789*4882a593Smuzhiyun                           uint32_t serial, uint32_t mods_depressed,
790*4882a593Smuzhiyun                           uint32_t mods_latched, uint32_t mods_locked,
791*4882a593Smuzhiyun                           uint32_t group)
792*4882a593Smuzhiyun {
793*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = data;
794*4882a593Smuzhiyun     DeviceIntPtr dev;
795*4882a593Smuzhiyun     XkbStateRec old_state, *new_state;
796*4882a593Smuzhiyun     xkbStateNotify sn;
797*4882a593Smuzhiyun     CARD16 changed;
798*4882a593Smuzhiyun 
799*4882a593Smuzhiyun     mieqProcessInputEvents();
800*4882a593Smuzhiyun 
801*4882a593Smuzhiyun     for (dev = inputInfo.devices; dev; dev = dev->next) {
802*4882a593Smuzhiyun         if (dev != xwl_seat->keyboard &&
803*4882a593Smuzhiyun             dev != GetMaster(xwl_seat->keyboard, MASTER_KEYBOARD))
804*4882a593Smuzhiyun             continue;
805*4882a593Smuzhiyun 
806*4882a593Smuzhiyun         old_state = dev->key->xkbInfo->state;
807*4882a593Smuzhiyun         new_state = &dev->key->xkbInfo->state;
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun         new_state->locked_group = group & XkbAllGroupsMask;
810*4882a593Smuzhiyun         new_state->base_mods = mods_depressed & XkbAllModifiersMask;
811*4882a593Smuzhiyun         new_state->locked_mods = mods_locked & XkbAllModifiersMask;
812*4882a593Smuzhiyun         XkbLatchModifiers(dev, XkbAllModifiersMask,
813*4882a593Smuzhiyun                           mods_latched & XkbAllModifiersMask);
814*4882a593Smuzhiyun 
815*4882a593Smuzhiyun         XkbComputeDerivedState(dev->key->xkbInfo);
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun         changed = XkbStateChangedFlags(&old_state, new_state);
818*4882a593Smuzhiyun         if (!changed)
819*4882a593Smuzhiyun             continue;
820*4882a593Smuzhiyun 
821*4882a593Smuzhiyun         sn.keycode = 0;
822*4882a593Smuzhiyun         sn.eventType = 0;
823*4882a593Smuzhiyun         sn.requestMajor = XkbReqCode;
824*4882a593Smuzhiyun         sn.requestMinor = X_kbLatchLockState;   /* close enough */
825*4882a593Smuzhiyun         sn.changed = changed;
826*4882a593Smuzhiyun         XkbSendStateNotify(dev, &sn);
827*4882a593Smuzhiyun     }
828*4882a593Smuzhiyun }
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun static void
remove_sync_pending(DeviceIntPtr dev)831*4882a593Smuzhiyun remove_sync_pending(DeviceIntPtr dev)
832*4882a593Smuzhiyun {
833*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = dev->public.devicePrivate;
834*4882a593Smuzhiyun     struct sync_pending *p, *npd;
835*4882a593Smuzhiyun 
836*4882a593Smuzhiyun     if (!xwl_seat)
837*4882a593Smuzhiyun         return;
838*4882a593Smuzhiyun 
839*4882a593Smuzhiyun     xorg_list_for_each_entry_safe(p, npd, &xwl_seat->sync_pending, l) {
840*4882a593Smuzhiyun         if (p->pending_dev == dev) {
841*4882a593Smuzhiyun             xorg_list_del(&xwl_seat->sync_pending);
842*4882a593Smuzhiyun             free (p);
843*4882a593Smuzhiyun             return;
844*4882a593Smuzhiyun         }
845*4882a593Smuzhiyun     }
846*4882a593Smuzhiyun }
847*4882a593Smuzhiyun 
848*4882a593Smuzhiyun static void
sync_callback(void * data,struct wl_callback * callback,uint32_t serial)849*4882a593Smuzhiyun sync_callback(void *data, struct wl_callback *callback, uint32_t serial)
850*4882a593Smuzhiyun {
851*4882a593Smuzhiyun     DeviceIntPtr dev = (DeviceIntPtr) data;
852*4882a593Smuzhiyun 
853*4882a593Smuzhiyun     remove_sync_pending(dev);
854*4882a593Smuzhiyun     wl_callback_destroy(callback);
855*4882a593Smuzhiyun }
856*4882a593Smuzhiyun 
857*4882a593Smuzhiyun static const struct wl_callback_listener sync_listener = {
858*4882a593Smuzhiyun    sync_callback
859*4882a593Smuzhiyun };
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun static Bool
keyboard_check_repeat(DeviceIntPtr dev,XkbSrvInfoPtr xkbi,unsigned key)862*4882a593Smuzhiyun keyboard_check_repeat (DeviceIntPtr dev, XkbSrvInfoPtr xkbi, unsigned key)
863*4882a593Smuzhiyun {
864*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = dev->public.devicePrivate;
865*4882a593Smuzhiyun     struct xwl_screen *xwl_screen;
866*4882a593Smuzhiyun     struct wl_callback *callback;
867*4882a593Smuzhiyun     struct sync_pending *p;
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun     if (!xwl_seat)
870*4882a593Smuzhiyun         return FALSE;
871*4882a593Smuzhiyun 
872*4882a593Smuzhiyun     /* Make sure we didn't miss a possible reply from the compositor */
873*4882a593Smuzhiyun     xwl_screen = xwl_seat->xwl_screen;
874*4882a593Smuzhiyun     xwl_sync_events (xwl_screen);
875*4882a593Smuzhiyun 
876*4882a593Smuzhiyun     xorg_list_for_each_entry(p, &xwl_seat->sync_pending, l) {
877*4882a593Smuzhiyun         if (p->pending_dev == dev) {
878*4882a593Smuzhiyun             ErrorF("Key repeat discarded, Wayland compositor doesn't "
879*4882a593Smuzhiyun                    "seem to be processing events fast enough!\n");
880*4882a593Smuzhiyun 
881*4882a593Smuzhiyun             return FALSE;
882*4882a593Smuzhiyun         }
883*4882a593Smuzhiyun     }
884*4882a593Smuzhiyun 
885*4882a593Smuzhiyun     p = xnfalloc(sizeof(struct sync_pending));
886*4882a593Smuzhiyun     p->pending_dev = dev;
887*4882a593Smuzhiyun     callback = wl_display_sync (xwl_screen->display);
888*4882a593Smuzhiyun     xorg_list_add(&p->l, &xwl_seat->sync_pending);
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun     wl_callback_add_listener(callback, &sync_listener, dev);
891*4882a593Smuzhiyun 
892*4882a593Smuzhiyun     return TRUE;
893*4882a593Smuzhiyun }
894*4882a593Smuzhiyun 
895*4882a593Smuzhiyun static void
keyboard_handle_repeat_info(void * data,struct wl_keyboard * keyboard,int32_t rate,int32_t delay)896*4882a593Smuzhiyun keyboard_handle_repeat_info (void *data, struct wl_keyboard *keyboard,
897*4882a593Smuzhiyun                              int32_t rate, int32_t delay)
898*4882a593Smuzhiyun {
899*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = data;
900*4882a593Smuzhiyun     DeviceIntPtr dev;
901*4882a593Smuzhiyun     XkbControlsPtr ctrl;
902*4882a593Smuzhiyun 
903*4882a593Smuzhiyun     if (rate < 0 || delay < 0) {
904*4882a593Smuzhiyun         ErrorF("Wrong rate/delay: %d, %d\n", rate, delay);
905*4882a593Smuzhiyun         return;
906*4882a593Smuzhiyun     }
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun     for (dev = inputInfo.devices; dev; dev = dev->next) {
909*4882a593Smuzhiyun         if (dev != xwl_seat->keyboard &&
910*4882a593Smuzhiyun             dev != GetMaster(xwl_seat->keyboard, MASTER_KEYBOARD))
911*4882a593Smuzhiyun             continue;
912*4882a593Smuzhiyun 
913*4882a593Smuzhiyun         if (rate != 0) {
914*4882a593Smuzhiyun             ctrl = dev->key->xkbInfo->desc->ctrls;
915*4882a593Smuzhiyun             ctrl->repeat_delay = delay;
916*4882a593Smuzhiyun             /* rate is number of keys per second */
917*4882a593Smuzhiyun             ctrl->repeat_interval = 1000 / rate;
918*4882a593Smuzhiyun 
919*4882a593Smuzhiyun             XkbSetRepeatKeys(dev, -1, AutoRepeatModeOn);
920*4882a593Smuzhiyun         } else
921*4882a593Smuzhiyun             XkbSetRepeatKeys(dev, -1, AutoRepeatModeOff);
922*4882a593Smuzhiyun     }
923*4882a593Smuzhiyun }
924*4882a593Smuzhiyun 
925*4882a593Smuzhiyun static const struct wl_keyboard_listener keyboard_listener = {
926*4882a593Smuzhiyun     keyboard_handle_keymap,
927*4882a593Smuzhiyun     keyboard_handle_enter,
928*4882a593Smuzhiyun     keyboard_handle_leave,
929*4882a593Smuzhiyun     keyboard_handle_key,
930*4882a593Smuzhiyun     keyboard_handle_modifiers,
931*4882a593Smuzhiyun     keyboard_handle_repeat_info,
932*4882a593Smuzhiyun };
933*4882a593Smuzhiyun 
934*4882a593Smuzhiyun static struct xwl_touch *
xwl_seat_lookup_touch(struct xwl_seat * xwl_seat,int32_t id)935*4882a593Smuzhiyun xwl_seat_lookup_touch(struct xwl_seat *xwl_seat, int32_t id)
936*4882a593Smuzhiyun {
937*4882a593Smuzhiyun     struct xwl_touch *xwl_touch, *next_xwl_touch;
938*4882a593Smuzhiyun 
939*4882a593Smuzhiyun     xorg_list_for_each_entry_safe(xwl_touch, next_xwl_touch,
940*4882a593Smuzhiyun                                   &xwl_seat->touches, link_touch) {
941*4882a593Smuzhiyun         if (xwl_touch->id == id)
942*4882a593Smuzhiyun             return xwl_touch;
943*4882a593Smuzhiyun     }
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun     return NULL;
946*4882a593Smuzhiyun }
947*4882a593Smuzhiyun 
948*4882a593Smuzhiyun static void
xwl_touch_send_event(struct xwl_touch * xwl_touch,struct xwl_seat * xwl_seat,int type)949*4882a593Smuzhiyun xwl_touch_send_event(struct xwl_touch *xwl_touch,
950*4882a593Smuzhiyun                      struct xwl_seat *xwl_seat, int type)
951*4882a593Smuzhiyun {
952*4882a593Smuzhiyun     double dx, dy, x, y;
953*4882a593Smuzhiyun     ValuatorMask mask;
954*4882a593Smuzhiyun 
955*4882a593Smuzhiyun     dx = xwl_touch->window->window->drawable.x;
956*4882a593Smuzhiyun     dy = xwl_touch->window->window->drawable.y;
957*4882a593Smuzhiyun 
958*4882a593Smuzhiyun     x = (dx + xwl_touch->x) * 0xFFFF / xwl_seat->xwl_screen->width;
959*4882a593Smuzhiyun     y = (dy + xwl_touch->y) * 0xFFFF / xwl_seat->xwl_screen->height;
960*4882a593Smuzhiyun 
961*4882a593Smuzhiyun     valuator_mask_zero(&mask);
962*4882a593Smuzhiyun     valuator_mask_set_double(&mask, 0, x);
963*4882a593Smuzhiyun     valuator_mask_set_double(&mask, 1, y);
964*4882a593Smuzhiyun     QueueTouchEvents(xwl_seat->touch, type, xwl_touch->id, 0, &mask);
965*4882a593Smuzhiyun }
966*4882a593Smuzhiyun 
967*4882a593Smuzhiyun static void
touch_handle_down(void * data,struct wl_touch * wl_touch,uint32_t serial,uint32_t time,struct wl_surface * surface,int32_t id,wl_fixed_t sx_w,wl_fixed_t sy_w)968*4882a593Smuzhiyun touch_handle_down(void *data, struct wl_touch *wl_touch,
969*4882a593Smuzhiyun                   uint32_t serial, uint32_t time,
970*4882a593Smuzhiyun                   struct wl_surface *surface,
971*4882a593Smuzhiyun                   int32_t id, wl_fixed_t sx_w, wl_fixed_t sy_w)
972*4882a593Smuzhiyun {
973*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = data;
974*4882a593Smuzhiyun     struct xwl_touch *xwl_touch;
975*4882a593Smuzhiyun 
976*4882a593Smuzhiyun     if (surface == NULL)
977*4882a593Smuzhiyun         return;
978*4882a593Smuzhiyun 
979*4882a593Smuzhiyun     xwl_touch = calloc(1, sizeof *xwl_touch);
980*4882a593Smuzhiyun     if (xwl_touch == NULL) {
981*4882a593Smuzhiyun         ErrorF("%s: ENOMEM\n", __func__);
982*4882a593Smuzhiyun         return;
983*4882a593Smuzhiyun     }
984*4882a593Smuzhiyun 
985*4882a593Smuzhiyun     xwl_touch->window = wl_surface_get_user_data(surface);
986*4882a593Smuzhiyun     xwl_touch->id = id;
987*4882a593Smuzhiyun     xwl_touch->x = wl_fixed_to_int(sx_w);
988*4882a593Smuzhiyun     xwl_touch->y = wl_fixed_to_int(sy_w);
989*4882a593Smuzhiyun     xorg_list_add(&xwl_touch->link_touch, &xwl_seat->touches);
990*4882a593Smuzhiyun 
991*4882a593Smuzhiyun     xwl_touch_send_event(xwl_touch, xwl_seat, XI_TouchBegin);
992*4882a593Smuzhiyun }
993*4882a593Smuzhiyun 
994*4882a593Smuzhiyun static void
touch_handle_up(void * data,struct wl_touch * wl_touch,uint32_t serial,uint32_t time,int32_t id)995*4882a593Smuzhiyun touch_handle_up(void *data, struct wl_touch *wl_touch,
996*4882a593Smuzhiyun                 uint32_t serial, uint32_t time, int32_t id)
997*4882a593Smuzhiyun {
998*4882a593Smuzhiyun     struct xwl_touch *xwl_touch;
999*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = data;
1000*4882a593Smuzhiyun 
1001*4882a593Smuzhiyun     xwl_touch = xwl_seat_lookup_touch(xwl_seat, id);
1002*4882a593Smuzhiyun 
1003*4882a593Smuzhiyun     if (!xwl_touch)
1004*4882a593Smuzhiyun         return;
1005*4882a593Smuzhiyun 
1006*4882a593Smuzhiyun     xwl_touch_send_event(xwl_touch, xwl_seat, XI_TouchEnd);
1007*4882a593Smuzhiyun     xorg_list_del(&xwl_touch->link_touch);
1008*4882a593Smuzhiyun     free(xwl_touch);
1009*4882a593Smuzhiyun }
1010*4882a593Smuzhiyun 
1011*4882a593Smuzhiyun static void
touch_handle_motion(void * data,struct wl_touch * wl_touch,uint32_t time,int32_t id,wl_fixed_t sx_w,wl_fixed_t sy_w)1012*4882a593Smuzhiyun touch_handle_motion(void *data, struct wl_touch *wl_touch,
1013*4882a593Smuzhiyun                     uint32_t time, int32_t id,
1014*4882a593Smuzhiyun                     wl_fixed_t sx_w, wl_fixed_t sy_w)
1015*4882a593Smuzhiyun {
1016*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = data;
1017*4882a593Smuzhiyun     struct xwl_touch *xwl_touch;
1018*4882a593Smuzhiyun 
1019*4882a593Smuzhiyun     xwl_touch = xwl_seat_lookup_touch(xwl_seat, id);
1020*4882a593Smuzhiyun 
1021*4882a593Smuzhiyun     if (!xwl_touch)
1022*4882a593Smuzhiyun         return;
1023*4882a593Smuzhiyun 
1024*4882a593Smuzhiyun     xwl_touch->x = wl_fixed_to_int(sx_w);
1025*4882a593Smuzhiyun     xwl_touch->y = wl_fixed_to_int(sy_w);
1026*4882a593Smuzhiyun     xwl_touch_send_event(xwl_touch, xwl_seat, XI_TouchUpdate);
1027*4882a593Smuzhiyun }
1028*4882a593Smuzhiyun 
1029*4882a593Smuzhiyun static void
touch_handle_frame(void * data,struct wl_touch * wl_touch)1030*4882a593Smuzhiyun touch_handle_frame(void *data, struct wl_touch *wl_touch)
1031*4882a593Smuzhiyun {
1032*4882a593Smuzhiyun }
1033*4882a593Smuzhiyun 
1034*4882a593Smuzhiyun static void
touch_handle_cancel(void * data,struct wl_touch * wl_touch)1035*4882a593Smuzhiyun touch_handle_cancel(void *data, struct wl_touch *wl_touch)
1036*4882a593Smuzhiyun {
1037*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = data;
1038*4882a593Smuzhiyun     struct xwl_touch *xwl_touch, *next_xwl_touch;
1039*4882a593Smuzhiyun 
1040*4882a593Smuzhiyun     xorg_list_for_each_entry_safe(xwl_touch, next_xwl_touch,
1041*4882a593Smuzhiyun                                   &xwl_seat->touches, link_touch) {
1042*4882a593Smuzhiyun         /* We can't properly notify of cancellation to the X client
1043*4882a593Smuzhiyun          * once it thinks it has the ownership, send at least a
1044*4882a593Smuzhiyun          * TouchEnd event.
1045*4882a593Smuzhiyun          */
1046*4882a593Smuzhiyun         xwl_touch_send_event(xwl_touch, xwl_seat, XI_TouchEnd);
1047*4882a593Smuzhiyun         xorg_list_del(&xwl_touch->link_touch);
1048*4882a593Smuzhiyun         free(xwl_touch);
1049*4882a593Smuzhiyun     }
1050*4882a593Smuzhiyun }
1051*4882a593Smuzhiyun 
1052*4882a593Smuzhiyun static const struct wl_touch_listener touch_listener = {
1053*4882a593Smuzhiyun     touch_handle_down,
1054*4882a593Smuzhiyun     touch_handle_up,
1055*4882a593Smuzhiyun     touch_handle_motion,
1056*4882a593Smuzhiyun     touch_handle_frame,
1057*4882a593Smuzhiyun     touch_handle_cancel
1058*4882a593Smuzhiyun };
1059*4882a593Smuzhiyun 
1060*4882a593Smuzhiyun static struct xwl_seat *
find_matching_seat(DeviceIntPtr device)1061*4882a593Smuzhiyun find_matching_seat(DeviceIntPtr device)
1062*4882a593Smuzhiyun {
1063*4882a593Smuzhiyun     DeviceIntPtr dev;
1064*4882a593Smuzhiyun 
1065*4882a593Smuzhiyun     for (dev = inputInfo.devices; dev; dev = dev->next)
1066*4882a593Smuzhiyun         if (dev->deviceProc == xwl_keyboard_proc &&
1067*4882a593Smuzhiyun             device == GetMaster(dev, MASTER_KEYBOARD))
1068*4882a593Smuzhiyun                 return (struct xwl_seat *) dev->public.devicePrivate;
1069*4882a593Smuzhiyun 
1070*4882a593Smuzhiyun     return NULL;
1071*4882a593Smuzhiyun }
1072*4882a593Smuzhiyun 
1073*4882a593Smuzhiyun static void
release_grab(struct xwl_seat * xwl_seat)1074*4882a593Smuzhiyun release_grab(struct xwl_seat *xwl_seat)
1075*4882a593Smuzhiyun {
1076*4882a593Smuzhiyun     if (xwl_seat->keyboard_grab)
1077*4882a593Smuzhiyun         zwp_xwayland_keyboard_grab_v1_destroy(xwl_seat->keyboard_grab);
1078*4882a593Smuzhiyun     xwl_seat->keyboard_grab = NULL;
1079*4882a593Smuzhiyun }
1080*4882a593Smuzhiyun 
1081*4882a593Smuzhiyun static void
set_grab(struct xwl_seat * xwl_seat,struct xwl_window * xwl_window)1082*4882a593Smuzhiyun set_grab(struct xwl_seat *xwl_seat, struct xwl_window *xwl_window)
1083*4882a593Smuzhiyun {
1084*4882a593Smuzhiyun     struct xwl_screen *xwl_screen;
1085*4882a593Smuzhiyun 
1086*4882a593Smuzhiyun     if (!xwl_window)
1087*4882a593Smuzhiyun         return;
1088*4882a593Smuzhiyun 
1089*4882a593Smuzhiyun     /* We already have a grab */
1090*4882a593Smuzhiyun     if (xwl_seat->keyboard_grab)
1091*4882a593Smuzhiyun         release_grab (xwl_seat);
1092*4882a593Smuzhiyun 
1093*4882a593Smuzhiyun     xwl_screen = xwl_seat->xwl_screen;
1094*4882a593Smuzhiyun     xwl_seat->keyboard_grab =
1095*4882a593Smuzhiyun         zwp_xwayland_keyboard_grab_manager_v1_grab_keyboard(xwl_screen->wp_grab,
1096*4882a593Smuzhiyun                                                             xwl_window->surface,
1097*4882a593Smuzhiyun                                                             xwl_seat->seat);
1098*4882a593Smuzhiyun }
1099*4882a593Smuzhiyun 
1100*4882a593Smuzhiyun static void
xwl_keyboard_activate_grab(DeviceIntPtr device,GrabPtr grab,TimeStamp time,Bool passive)1101*4882a593Smuzhiyun xwl_keyboard_activate_grab(DeviceIntPtr device, GrabPtr grab, TimeStamp time, Bool passive)
1102*4882a593Smuzhiyun {
1103*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = device->public.devicePrivate;
1104*4882a593Smuzhiyun 
1105*4882a593Smuzhiyun     /* We are not interested in passive grabs */
1106*4882a593Smuzhiyun     if (!passive) {
1107*4882a593Smuzhiyun         /* If the device is the MASTER_KEYBOARD, we don't have an xwl_seat */
1108*4882a593Smuzhiyun         if (xwl_seat == NULL)
1109*4882a593Smuzhiyun             xwl_seat = find_matching_seat(device);
1110*4882a593Smuzhiyun         if (xwl_seat)
1111*4882a593Smuzhiyun             set_grab(xwl_seat, xwl_window_from_window(grab->window));
1112*4882a593Smuzhiyun     }
1113*4882a593Smuzhiyun 
1114*4882a593Smuzhiyun     ActivateKeyboardGrab(device, grab, time, passive);
1115*4882a593Smuzhiyun }
1116*4882a593Smuzhiyun 
1117*4882a593Smuzhiyun static void
xwl_keyboard_deactivate_grab(DeviceIntPtr device)1118*4882a593Smuzhiyun xwl_keyboard_deactivate_grab(DeviceIntPtr device)
1119*4882a593Smuzhiyun {
1120*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = device->public.devicePrivate;
1121*4882a593Smuzhiyun 
1122*4882a593Smuzhiyun     /* If the device is the MASTER_KEYBOARD, we don't have an xwl_seat */
1123*4882a593Smuzhiyun     if (xwl_seat == NULL)
1124*4882a593Smuzhiyun         xwl_seat = find_matching_seat(device);
1125*4882a593Smuzhiyun     if (xwl_seat)
1126*4882a593Smuzhiyun         release_grab (xwl_seat);
1127*4882a593Smuzhiyun 
1128*4882a593Smuzhiyun     DeactivateKeyboardGrab(device);
1129*4882a593Smuzhiyun }
1130*4882a593Smuzhiyun 
1131*4882a593Smuzhiyun static void
setup_keyboard_grab_handler(DeviceIntPtr device)1132*4882a593Smuzhiyun setup_keyboard_grab_handler (DeviceIntPtr device)
1133*4882a593Smuzhiyun {
1134*4882a593Smuzhiyun     device->deviceGrab.ActivateGrab = xwl_keyboard_activate_grab;
1135*4882a593Smuzhiyun     device->deviceGrab.DeactivateGrab = xwl_keyboard_deactivate_grab;
1136*4882a593Smuzhiyun }
1137*4882a593Smuzhiyun 
1138*4882a593Smuzhiyun static DeviceIntPtr
add_device(struct xwl_seat * xwl_seat,const char * driver,DeviceProc device_proc)1139*4882a593Smuzhiyun add_device(struct xwl_seat *xwl_seat,
1140*4882a593Smuzhiyun            const char *driver, DeviceProc device_proc)
1141*4882a593Smuzhiyun {
1142*4882a593Smuzhiyun     DeviceIntPtr dev = NULL;
1143*4882a593Smuzhiyun     static Atom type_atom;
1144*4882a593Smuzhiyun     char name[32];
1145*4882a593Smuzhiyun 
1146*4882a593Smuzhiyun     dev = AddInputDevice(serverClient, device_proc, TRUE);
1147*4882a593Smuzhiyun     if (dev == NULL)
1148*4882a593Smuzhiyun         return NULL;
1149*4882a593Smuzhiyun 
1150*4882a593Smuzhiyun     if (type_atom == None)
1151*4882a593Smuzhiyun         type_atom = MakeAtom(driver, strlen(driver), TRUE);
1152*4882a593Smuzhiyun     snprintf(name, sizeof name, "%s:%d", driver, xwl_seat->id);
1153*4882a593Smuzhiyun     AssignTypeAndName(dev, type_atom, name);
1154*4882a593Smuzhiyun     dev->public.devicePrivate = xwl_seat;
1155*4882a593Smuzhiyun     dev->type = SLAVE;
1156*4882a593Smuzhiyun     dev->spriteInfo->spriteOwner = FALSE;
1157*4882a593Smuzhiyun 
1158*4882a593Smuzhiyun     return dev;
1159*4882a593Smuzhiyun }
1160*4882a593Smuzhiyun 
1161*4882a593Smuzhiyun static void
disable_device(DeviceIntPtr dev)1162*4882a593Smuzhiyun disable_device(DeviceIntPtr dev)
1163*4882a593Smuzhiyun {
1164*4882a593Smuzhiyun     DisableDevice(dev, TRUE);
1165*4882a593Smuzhiyun     dev->public.devicePrivate = NULL;
1166*4882a593Smuzhiyun }
1167*4882a593Smuzhiyun 
1168*4882a593Smuzhiyun static void
enable_device(struct xwl_seat * xwl_seat,DeviceIntPtr dev)1169*4882a593Smuzhiyun enable_device(struct xwl_seat *xwl_seat, DeviceIntPtr dev)
1170*4882a593Smuzhiyun {
1171*4882a593Smuzhiyun     dev->public.devicePrivate = xwl_seat;
1172*4882a593Smuzhiyun     EnableDevice(dev, TRUE);
1173*4882a593Smuzhiyun }
1174*4882a593Smuzhiyun 
1175*4882a593Smuzhiyun 
1176*4882a593Smuzhiyun static void
init_pointer(struct xwl_seat * xwl_seat)1177*4882a593Smuzhiyun init_pointer(struct xwl_seat *xwl_seat)
1178*4882a593Smuzhiyun {
1179*4882a593Smuzhiyun     xwl_seat->wl_pointer = wl_seat_get_pointer(xwl_seat->seat);
1180*4882a593Smuzhiyun     wl_pointer_add_listener(xwl_seat->wl_pointer,
1181*4882a593Smuzhiyun                             &pointer_listener, xwl_seat);
1182*4882a593Smuzhiyun 
1183*4882a593Smuzhiyun     if (xwl_seat->pointer == NULL) {
1184*4882a593Smuzhiyun         xwl_seat_set_cursor(xwl_seat);
1185*4882a593Smuzhiyun         xwl_seat->pointer =
1186*4882a593Smuzhiyun             add_device(xwl_seat, "xwayland-pointer", xwl_pointer_proc);
1187*4882a593Smuzhiyun         ActivateDevice(xwl_seat->pointer, TRUE);
1188*4882a593Smuzhiyun     }
1189*4882a593Smuzhiyun     enable_device(xwl_seat, xwl_seat->pointer);
1190*4882a593Smuzhiyun }
1191*4882a593Smuzhiyun 
1192*4882a593Smuzhiyun static void
release_pointer(struct xwl_seat * xwl_seat)1193*4882a593Smuzhiyun release_pointer(struct xwl_seat *xwl_seat)
1194*4882a593Smuzhiyun {
1195*4882a593Smuzhiyun     wl_pointer_release(xwl_seat->wl_pointer);
1196*4882a593Smuzhiyun     xwl_seat->wl_pointer = NULL;
1197*4882a593Smuzhiyun 
1198*4882a593Smuzhiyun     if (xwl_seat->pointer)
1199*4882a593Smuzhiyun         disable_device(xwl_seat->pointer);
1200*4882a593Smuzhiyun }
1201*4882a593Smuzhiyun 
1202*4882a593Smuzhiyun static void
init_relative_pointer(struct xwl_seat * xwl_seat)1203*4882a593Smuzhiyun init_relative_pointer(struct xwl_seat *xwl_seat)
1204*4882a593Smuzhiyun {
1205*4882a593Smuzhiyun     struct zwp_relative_pointer_manager_v1 *relative_pointer_manager =
1206*4882a593Smuzhiyun         xwl_seat->xwl_screen->relative_pointer_manager;
1207*4882a593Smuzhiyun 
1208*4882a593Smuzhiyun     if (relative_pointer_manager) {
1209*4882a593Smuzhiyun         xwl_seat->wp_relative_pointer =
1210*4882a593Smuzhiyun             zwp_relative_pointer_manager_v1_get_relative_pointer(
1211*4882a593Smuzhiyun                 relative_pointer_manager, xwl_seat->wl_pointer);
1212*4882a593Smuzhiyun         zwp_relative_pointer_v1_add_listener(xwl_seat->wp_relative_pointer,
1213*4882a593Smuzhiyun                                              &relative_pointer_listener,
1214*4882a593Smuzhiyun                                              xwl_seat);
1215*4882a593Smuzhiyun     }
1216*4882a593Smuzhiyun 
1217*4882a593Smuzhiyun     if (xwl_seat->relative_pointer == NULL) {
1218*4882a593Smuzhiyun         xwl_seat->relative_pointer =
1219*4882a593Smuzhiyun             add_device(xwl_seat, "xwayland-relative-pointer",
1220*4882a593Smuzhiyun                        xwl_pointer_proc_relative);
1221*4882a593Smuzhiyun         ActivateDevice(xwl_seat->relative_pointer, TRUE);
1222*4882a593Smuzhiyun     }
1223*4882a593Smuzhiyun     enable_device(xwl_seat, xwl_seat->relative_pointer);
1224*4882a593Smuzhiyun }
1225*4882a593Smuzhiyun 
1226*4882a593Smuzhiyun static void
release_relative_pointer(struct xwl_seat * xwl_seat)1227*4882a593Smuzhiyun release_relative_pointer(struct xwl_seat *xwl_seat)
1228*4882a593Smuzhiyun {
1229*4882a593Smuzhiyun     if (xwl_seat->wp_relative_pointer) {
1230*4882a593Smuzhiyun         zwp_relative_pointer_v1_destroy(xwl_seat->wp_relative_pointer);
1231*4882a593Smuzhiyun         xwl_seat->wp_relative_pointer = NULL;
1232*4882a593Smuzhiyun     }
1233*4882a593Smuzhiyun 
1234*4882a593Smuzhiyun     if (xwl_seat->relative_pointer)
1235*4882a593Smuzhiyun         disable_device(xwl_seat->relative_pointer);
1236*4882a593Smuzhiyun }
1237*4882a593Smuzhiyun 
1238*4882a593Smuzhiyun static void
init_keyboard(struct xwl_seat * xwl_seat)1239*4882a593Smuzhiyun init_keyboard(struct xwl_seat *xwl_seat)
1240*4882a593Smuzhiyun {
1241*4882a593Smuzhiyun     DeviceIntPtr master;
1242*4882a593Smuzhiyun 
1243*4882a593Smuzhiyun     xwl_seat->wl_keyboard = wl_seat_get_keyboard(xwl_seat->seat);
1244*4882a593Smuzhiyun     wl_keyboard_add_listener(xwl_seat->wl_keyboard,
1245*4882a593Smuzhiyun                              &keyboard_listener, xwl_seat);
1246*4882a593Smuzhiyun 
1247*4882a593Smuzhiyun     if (xwl_seat->keyboard == NULL) {
1248*4882a593Smuzhiyun         xwl_seat->keyboard =
1249*4882a593Smuzhiyun             add_device(xwl_seat, "xwayland-keyboard", xwl_keyboard_proc);
1250*4882a593Smuzhiyun         ActivateDevice(xwl_seat->keyboard, TRUE);
1251*4882a593Smuzhiyun     }
1252*4882a593Smuzhiyun     enable_device(xwl_seat, xwl_seat->keyboard);
1253*4882a593Smuzhiyun     xwl_seat->keyboard->key->xkbInfo->checkRepeat = keyboard_check_repeat;
1254*4882a593Smuzhiyun 
1255*4882a593Smuzhiyun     if (xwl_seat->xwl_screen->wp_grab) {
1256*4882a593Smuzhiyun         /* We have Xwayland grab protocol supported by the compositor */
1257*4882a593Smuzhiyun         master = GetMaster(xwl_seat->keyboard, MASTER_KEYBOARD);
1258*4882a593Smuzhiyun         if (master)
1259*4882a593Smuzhiyun             setup_keyboard_grab_handler(master);
1260*4882a593Smuzhiyun     }
1261*4882a593Smuzhiyun }
1262*4882a593Smuzhiyun 
1263*4882a593Smuzhiyun static void
release_keyboard(struct xwl_seat * xwl_seat)1264*4882a593Smuzhiyun release_keyboard(struct xwl_seat *xwl_seat)
1265*4882a593Smuzhiyun {
1266*4882a593Smuzhiyun     release_grab(xwl_seat);
1267*4882a593Smuzhiyun     wl_keyboard_release(xwl_seat->wl_keyboard);
1268*4882a593Smuzhiyun     xwl_seat->wl_keyboard = NULL;
1269*4882a593Smuzhiyun 
1270*4882a593Smuzhiyun     if (xwl_seat->keyboard) {
1271*4882a593Smuzhiyun         remove_sync_pending(xwl_seat->keyboard);
1272*4882a593Smuzhiyun         disable_device(xwl_seat->keyboard);
1273*4882a593Smuzhiyun     }
1274*4882a593Smuzhiyun }
1275*4882a593Smuzhiyun 
1276*4882a593Smuzhiyun static void
init_touch(struct xwl_seat * xwl_seat)1277*4882a593Smuzhiyun init_touch(struct xwl_seat *xwl_seat)
1278*4882a593Smuzhiyun {
1279*4882a593Smuzhiyun     xwl_seat->wl_touch = wl_seat_get_touch(xwl_seat->seat);
1280*4882a593Smuzhiyun     wl_touch_add_listener(xwl_seat->wl_touch,
1281*4882a593Smuzhiyun                           &touch_listener, xwl_seat);
1282*4882a593Smuzhiyun 
1283*4882a593Smuzhiyun     if (xwl_seat->touch == NULL) {
1284*4882a593Smuzhiyun         xwl_seat->touch =
1285*4882a593Smuzhiyun             add_device(xwl_seat, "xwayland-touch", xwl_touch_proc);
1286*4882a593Smuzhiyun         ActivateDevice(xwl_seat->touch, TRUE);
1287*4882a593Smuzhiyun     }
1288*4882a593Smuzhiyun     enable_device(xwl_seat, xwl_seat->touch);
1289*4882a593Smuzhiyun }
1290*4882a593Smuzhiyun 
1291*4882a593Smuzhiyun static void
release_touch(struct xwl_seat * xwl_seat)1292*4882a593Smuzhiyun release_touch(struct xwl_seat *xwl_seat)
1293*4882a593Smuzhiyun {
1294*4882a593Smuzhiyun     wl_touch_release(xwl_seat->wl_touch);
1295*4882a593Smuzhiyun     xwl_seat->wl_touch = NULL;
1296*4882a593Smuzhiyun 
1297*4882a593Smuzhiyun     if (xwl_seat->touch)
1298*4882a593Smuzhiyun         disable_device(xwl_seat->touch);
1299*4882a593Smuzhiyun }
1300*4882a593Smuzhiyun 
1301*4882a593Smuzhiyun static void
seat_handle_capabilities(void * data,struct wl_seat * seat,enum wl_seat_capability caps)1302*4882a593Smuzhiyun seat_handle_capabilities(void *data, struct wl_seat *seat,
1303*4882a593Smuzhiyun                          enum wl_seat_capability caps)
1304*4882a593Smuzhiyun {
1305*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = data;
1306*4882a593Smuzhiyun 
1307*4882a593Smuzhiyun     if (caps & WL_SEAT_CAPABILITY_POINTER && xwl_seat->wl_pointer == NULL) {
1308*4882a593Smuzhiyun         init_pointer(xwl_seat);
1309*4882a593Smuzhiyun         init_relative_pointer(xwl_seat);
1310*4882a593Smuzhiyun     } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && xwl_seat->wl_pointer) {
1311*4882a593Smuzhiyun         release_pointer(xwl_seat);
1312*4882a593Smuzhiyun         release_relative_pointer(xwl_seat);
1313*4882a593Smuzhiyun     }
1314*4882a593Smuzhiyun 
1315*4882a593Smuzhiyun     if (caps & WL_SEAT_CAPABILITY_KEYBOARD && xwl_seat->wl_keyboard == NULL) {
1316*4882a593Smuzhiyun         init_keyboard(xwl_seat);
1317*4882a593Smuzhiyun     } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && xwl_seat->wl_keyboard) {
1318*4882a593Smuzhiyun         release_keyboard(xwl_seat);
1319*4882a593Smuzhiyun     }
1320*4882a593Smuzhiyun 
1321*4882a593Smuzhiyun     if (caps & WL_SEAT_CAPABILITY_TOUCH && xwl_seat->wl_touch == NULL) {
1322*4882a593Smuzhiyun         init_touch(xwl_seat);
1323*4882a593Smuzhiyun     } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && xwl_seat->wl_touch) {
1324*4882a593Smuzhiyun         release_touch(xwl_seat);
1325*4882a593Smuzhiyun     }
1326*4882a593Smuzhiyun 
1327*4882a593Smuzhiyun     xwl_seat->xwl_screen->expecting_event--;
1328*4882a593Smuzhiyun }
1329*4882a593Smuzhiyun 
1330*4882a593Smuzhiyun static void
seat_handle_name(void * data,struct wl_seat * seat,const char * name)1331*4882a593Smuzhiyun seat_handle_name(void *data, struct wl_seat *seat,
1332*4882a593Smuzhiyun                  const char *name)
1333*4882a593Smuzhiyun {
1334*4882a593Smuzhiyun 
1335*4882a593Smuzhiyun }
1336*4882a593Smuzhiyun 
1337*4882a593Smuzhiyun static const struct wl_seat_listener seat_listener = {
1338*4882a593Smuzhiyun     seat_handle_capabilities,
1339*4882a593Smuzhiyun     seat_handle_name
1340*4882a593Smuzhiyun };
1341*4882a593Smuzhiyun 
1342*4882a593Smuzhiyun static void
xwl_cursor_init(struct xwl_cursor * xwl_cursor,struct xwl_screen * xwl_screen,void (* update_proc)(struct xwl_cursor *))1343*4882a593Smuzhiyun xwl_cursor_init(struct xwl_cursor *xwl_cursor, struct xwl_screen *xwl_screen,
1344*4882a593Smuzhiyun                 void (* update_proc)(struct xwl_cursor *))
1345*4882a593Smuzhiyun {
1346*4882a593Smuzhiyun     xwl_cursor->surface = wl_compositor_create_surface(xwl_screen->compositor);
1347*4882a593Smuzhiyun     xwl_cursor->update_proc = update_proc;
1348*4882a593Smuzhiyun     xwl_cursor->frame_cb = NULL;
1349*4882a593Smuzhiyun     xwl_cursor->needs_update = FALSE;
1350*4882a593Smuzhiyun }
1351*4882a593Smuzhiyun 
1352*4882a593Smuzhiyun static void
xwl_cursor_release(struct xwl_cursor * xwl_cursor)1353*4882a593Smuzhiyun xwl_cursor_release(struct xwl_cursor *xwl_cursor)
1354*4882a593Smuzhiyun {
1355*4882a593Smuzhiyun     wl_surface_destroy(xwl_cursor->surface);
1356*4882a593Smuzhiyun     if (xwl_cursor->frame_cb)
1357*4882a593Smuzhiyun         wl_callback_destroy(xwl_cursor->frame_cb);
1358*4882a593Smuzhiyun }
1359*4882a593Smuzhiyun 
1360*4882a593Smuzhiyun static void
xwl_seat_update_cursor(struct xwl_cursor * xwl_cursor)1361*4882a593Smuzhiyun xwl_seat_update_cursor(struct xwl_cursor *xwl_cursor)
1362*4882a593Smuzhiyun {
1363*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = wl_container_of(xwl_cursor, xwl_seat, cursor);
1364*4882a593Smuzhiyun     xwl_seat_set_cursor(xwl_seat);
1365*4882a593Smuzhiyun }
1366*4882a593Smuzhiyun 
1367*4882a593Smuzhiyun static void
create_input_device(struct xwl_screen * xwl_screen,uint32_t id,uint32_t version)1368*4882a593Smuzhiyun create_input_device(struct xwl_screen *xwl_screen, uint32_t id, uint32_t version)
1369*4882a593Smuzhiyun {
1370*4882a593Smuzhiyun     struct xwl_seat *xwl_seat;
1371*4882a593Smuzhiyun 
1372*4882a593Smuzhiyun     xwl_seat = calloc(1, sizeof *xwl_seat);
1373*4882a593Smuzhiyun     if (xwl_seat == NULL) {
1374*4882a593Smuzhiyun         ErrorF("%s: ENOMEM\n", __func__);
1375*4882a593Smuzhiyun         return;
1376*4882a593Smuzhiyun     }
1377*4882a593Smuzhiyun 
1378*4882a593Smuzhiyun     xwl_seat->xwl_screen = xwl_screen;
1379*4882a593Smuzhiyun     xorg_list_add(&xwl_seat->link, &xwl_screen->seat_list);
1380*4882a593Smuzhiyun 
1381*4882a593Smuzhiyun     xwl_seat->seat =
1382*4882a593Smuzhiyun         wl_registry_bind(xwl_screen->registry, id,
1383*4882a593Smuzhiyun                          &wl_seat_interface, min(version, 5));
1384*4882a593Smuzhiyun     xwl_seat->id = id;
1385*4882a593Smuzhiyun 
1386*4882a593Smuzhiyun     xwl_cursor_init(&xwl_seat->cursor, xwl_seat->xwl_screen,
1387*4882a593Smuzhiyun                     xwl_seat_update_cursor);
1388*4882a593Smuzhiyun     wl_seat_add_listener(xwl_seat->seat, &seat_listener, xwl_seat);
1389*4882a593Smuzhiyun 
1390*4882a593Smuzhiyun     init_tablet_manager_seat(xwl_screen, xwl_seat);
1391*4882a593Smuzhiyun 
1392*4882a593Smuzhiyun     wl_array_init(&xwl_seat->keys);
1393*4882a593Smuzhiyun 
1394*4882a593Smuzhiyun     xorg_list_init(&xwl_seat->touches);
1395*4882a593Smuzhiyun     xorg_list_init(&xwl_seat->axis_discrete_pending);
1396*4882a593Smuzhiyun     xorg_list_init(&xwl_seat->sync_pending);
1397*4882a593Smuzhiyun }
1398*4882a593Smuzhiyun 
1399*4882a593Smuzhiyun void
xwl_seat_destroy(struct xwl_seat * xwl_seat)1400*4882a593Smuzhiyun xwl_seat_destroy(struct xwl_seat *xwl_seat)
1401*4882a593Smuzhiyun {
1402*4882a593Smuzhiyun     struct xwl_touch *xwl_touch, *next_xwl_touch;
1403*4882a593Smuzhiyun     struct sync_pending *p, *npd;
1404*4882a593Smuzhiyun     struct axis_discrete_pending *ad, *ad_next;
1405*4882a593Smuzhiyun 
1406*4882a593Smuzhiyun     xorg_list_for_each_entry_safe(xwl_touch, next_xwl_touch,
1407*4882a593Smuzhiyun                                   &xwl_seat->touches, link_touch) {
1408*4882a593Smuzhiyun         xorg_list_del(&xwl_touch->link_touch);
1409*4882a593Smuzhiyun         free(xwl_touch);
1410*4882a593Smuzhiyun     }
1411*4882a593Smuzhiyun 
1412*4882a593Smuzhiyun     xorg_list_for_each_entry_safe(p, npd, &xwl_seat->sync_pending, l) {
1413*4882a593Smuzhiyun         xorg_list_del(&xwl_seat->sync_pending);
1414*4882a593Smuzhiyun         free (p);
1415*4882a593Smuzhiyun     }
1416*4882a593Smuzhiyun 
1417*4882a593Smuzhiyun     xorg_list_for_each_entry_safe(ad, ad_next, &xwl_seat->axis_discrete_pending, l) {
1418*4882a593Smuzhiyun         xorg_list_del(&ad->l);
1419*4882a593Smuzhiyun         free(ad);
1420*4882a593Smuzhiyun     }
1421*4882a593Smuzhiyun 
1422*4882a593Smuzhiyun     release_tablet_manager_seat(xwl_seat);
1423*4882a593Smuzhiyun 
1424*4882a593Smuzhiyun     release_grab(xwl_seat);
1425*4882a593Smuzhiyun     wl_seat_destroy(xwl_seat->seat);
1426*4882a593Smuzhiyun     xwl_cursor_release(&xwl_seat->cursor);
1427*4882a593Smuzhiyun     wl_array_release(&xwl_seat->keys);
1428*4882a593Smuzhiyun     free(xwl_seat);
1429*4882a593Smuzhiyun }
1430*4882a593Smuzhiyun 
1431*4882a593Smuzhiyun static void
tablet_handle_name(void * data,struct zwp_tablet_v2 * tablet,const char * name)1432*4882a593Smuzhiyun tablet_handle_name(void *data, struct zwp_tablet_v2 *tablet, const char *name)
1433*4882a593Smuzhiyun {
1434*4882a593Smuzhiyun }
1435*4882a593Smuzhiyun 
1436*4882a593Smuzhiyun static void
tablet_handle_id(void * data,struct zwp_tablet_v2 * tablet,uint32_t vid,uint32_t pid)1437*4882a593Smuzhiyun tablet_handle_id(void *data, struct zwp_tablet_v2 *tablet, uint32_t vid,
1438*4882a593Smuzhiyun                   uint32_t pid)
1439*4882a593Smuzhiyun {
1440*4882a593Smuzhiyun }
1441*4882a593Smuzhiyun 
1442*4882a593Smuzhiyun static void
tablet_handle_path(void * data,struct zwp_tablet_v2 * tablet,const char * path)1443*4882a593Smuzhiyun tablet_handle_path(void *data, struct zwp_tablet_v2 *tablet, const char *path)
1444*4882a593Smuzhiyun {
1445*4882a593Smuzhiyun }
1446*4882a593Smuzhiyun 
1447*4882a593Smuzhiyun static void
tablet_handle_done(void * data,struct zwp_tablet_v2 * tablet)1448*4882a593Smuzhiyun tablet_handle_done(void *data, struct zwp_tablet_v2 *tablet)
1449*4882a593Smuzhiyun {
1450*4882a593Smuzhiyun     struct xwl_tablet *xwl_tablet = data;
1451*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = xwl_tablet->seat;
1452*4882a593Smuzhiyun 
1453*4882a593Smuzhiyun     if (xwl_seat->stylus == NULL) {
1454*4882a593Smuzhiyun         xwl_seat->stylus = add_device(xwl_seat, "xwayland-stylus", xwl_tablet_proc);
1455*4882a593Smuzhiyun         ActivateDevice(xwl_seat->stylus, TRUE);
1456*4882a593Smuzhiyun     }
1457*4882a593Smuzhiyun     enable_device(xwl_seat, xwl_seat->stylus);
1458*4882a593Smuzhiyun 
1459*4882a593Smuzhiyun     if (xwl_seat->eraser == NULL) {
1460*4882a593Smuzhiyun         xwl_seat->eraser = add_device(xwl_seat, "xwayland-eraser", xwl_tablet_proc);
1461*4882a593Smuzhiyun         ActivateDevice(xwl_seat->eraser, TRUE);
1462*4882a593Smuzhiyun     }
1463*4882a593Smuzhiyun     enable_device(xwl_seat, xwl_seat->eraser);
1464*4882a593Smuzhiyun 
1465*4882a593Smuzhiyun     if (xwl_seat->puck == NULL) {
1466*4882a593Smuzhiyun         xwl_seat->puck = add_device(xwl_seat, "xwayland-cursor", xwl_tablet_proc);
1467*4882a593Smuzhiyun         ActivateDevice(xwl_seat->puck, TRUE);
1468*4882a593Smuzhiyun     }
1469*4882a593Smuzhiyun     enable_device(xwl_seat, xwl_seat->puck);
1470*4882a593Smuzhiyun }
1471*4882a593Smuzhiyun 
1472*4882a593Smuzhiyun static void
tablet_handle_removed(void * data,struct zwp_tablet_v2 * tablet)1473*4882a593Smuzhiyun tablet_handle_removed(void *data, struct zwp_tablet_v2 *tablet)
1474*4882a593Smuzhiyun {
1475*4882a593Smuzhiyun     struct xwl_tablet *xwl_tablet = data;
1476*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = xwl_tablet->seat;
1477*4882a593Smuzhiyun 
1478*4882a593Smuzhiyun     xorg_list_del(&xwl_tablet->link);
1479*4882a593Smuzhiyun 
1480*4882a593Smuzhiyun     /* The tablet is merely disabled, not removed. The next tablet
1481*4882a593Smuzhiyun        will re-use the same X devices */
1482*4882a593Smuzhiyun     if (xorg_list_is_empty(&xwl_seat->tablets)) {
1483*4882a593Smuzhiyun         if (xwl_seat->stylus)
1484*4882a593Smuzhiyun             disable_device(xwl_seat->stylus);
1485*4882a593Smuzhiyun         if (xwl_seat->eraser)
1486*4882a593Smuzhiyun             disable_device(xwl_seat->eraser);
1487*4882a593Smuzhiyun         if (xwl_seat->puck)
1488*4882a593Smuzhiyun             disable_device(xwl_seat->puck);
1489*4882a593Smuzhiyun         /* pads are removed separately */
1490*4882a593Smuzhiyun     }
1491*4882a593Smuzhiyun 
1492*4882a593Smuzhiyun     zwp_tablet_v2_destroy(tablet);
1493*4882a593Smuzhiyun     free(xwl_tablet);
1494*4882a593Smuzhiyun }
1495*4882a593Smuzhiyun 
1496*4882a593Smuzhiyun static const struct zwp_tablet_v2_listener tablet_listener = {
1497*4882a593Smuzhiyun     tablet_handle_name,
1498*4882a593Smuzhiyun     tablet_handle_id,
1499*4882a593Smuzhiyun     tablet_handle_path,
1500*4882a593Smuzhiyun     tablet_handle_done,
1501*4882a593Smuzhiyun     tablet_handle_removed
1502*4882a593Smuzhiyun };
1503*4882a593Smuzhiyun 
1504*4882a593Smuzhiyun static void
tablet_tool_receive_type(void * data,struct zwp_tablet_tool_v2 * tool,uint32_t type)1505*4882a593Smuzhiyun tablet_tool_receive_type(void *data, struct zwp_tablet_tool_v2 *tool,
1506*4882a593Smuzhiyun                          uint32_t type)
1507*4882a593Smuzhiyun {
1508*4882a593Smuzhiyun     struct xwl_tablet_tool *xwl_tablet_tool = data;
1509*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = xwl_tablet_tool->seat;
1510*4882a593Smuzhiyun 
1511*4882a593Smuzhiyun     switch (type) {
1512*4882a593Smuzhiyun         case ZWP_TABLET_TOOL_V2_TYPE_ERASER:
1513*4882a593Smuzhiyun             xwl_tablet_tool->xdevice = xwl_seat->eraser;
1514*4882a593Smuzhiyun             break;
1515*4882a593Smuzhiyun         case ZWP_TABLET_TOOL_V2_TYPE_MOUSE:
1516*4882a593Smuzhiyun         case ZWP_TABLET_TOOL_V2_TYPE_LENS:
1517*4882a593Smuzhiyun             xwl_tablet_tool->xdevice = xwl_seat->puck;
1518*4882a593Smuzhiyun             break;
1519*4882a593Smuzhiyun         default:
1520*4882a593Smuzhiyun             xwl_tablet_tool->xdevice = xwl_seat->stylus;
1521*4882a593Smuzhiyun             break;
1522*4882a593Smuzhiyun     }
1523*4882a593Smuzhiyun }
1524*4882a593Smuzhiyun 
1525*4882a593Smuzhiyun static void
tablet_tool_receive_hardware_serial(void * data,struct zwp_tablet_tool_v2 * tool,uint32_t hi,uint32_t low)1526*4882a593Smuzhiyun tablet_tool_receive_hardware_serial(void *data, struct zwp_tablet_tool_v2 *tool,
1527*4882a593Smuzhiyun                                     uint32_t hi, uint32_t low)
1528*4882a593Smuzhiyun {
1529*4882a593Smuzhiyun }
1530*4882a593Smuzhiyun 
1531*4882a593Smuzhiyun static void
tablet_tool_receive_hardware_id_wacom(void * data,struct zwp_tablet_tool_v2 * tool,uint32_t hi,uint32_t low)1532*4882a593Smuzhiyun tablet_tool_receive_hardware_id_wacom(void *data, struct zwp_tablet_tool_v2 *tool,
1533*4882a593Smuzhiyun                                       uint32_t hi, uint32_t low)
1534*4882a593Smuzhiyun {
1535*4882a593Smuzhiyun }
1536*4882a593Smuzhiyun 
1537*4882a593Smuzhiyun static void
tablet_tool_receive_capability(void * data,struct zwp_tablet_tool_v2 * tool,uint32_t capability)1538*4882a593Smuzhiyun tablet_tool_receive_capability(void *data, struct zwp_tablet_tool_v2 *tool,
1539*4882a593Smuzhiyun                                uint32_t capability)
1540*4882a593Smuzhiyun {
1541*4882a593Smuzhiyun }
1542*4882a593Smuzhiyun 
1543*4882a593Smuzhiyun static void
tablet_tool_receive_done(void * data,struct zwp_tablet_tool_v2 * tool)1544*4882a593Smuzhiyun tablet_tool_receive_done(void *data, struct zwp_tablet_tool_v2 *tool)
1545*4882a593Smuzhiyun {
1546*4882a593Smuzhiyun }
1547*4882a593Smuzhiyun 
1548*4882a593Smuzhiyun static void
tablet_tool_receive_removed(void * data,struct zwp_tablet_tool_v2 * tool)1549*4882a593Smuzhiyun tablet_tool_receive_removed(void *data, struct zwp_tablet_tool_v2 *tool)
1550*4882a593Smuzhiyun {
1551*4882a593Smuzhiyun     struct xwl_tablet_tool *xwl_tablet_tool = data;
1552*4882a593Smuzhiyun 
1553*4882a593Smuzhiyun     xorg_list_del(&xwl_tablet_tool->link);
1554*4882a593Smuzhiyun     xwl_cursor_release(&xwl_tablet_tool->cursor);
1555*4882a593Smuzhiyun     zwp_tablet_tool_v2_destroy(tool);
1556*4882a593Smuzhiyun     free(xwl_tablet_tool);
1557*4882a593Smuzhiyun }
1558*4882a593Smuzhiyun 
1559*4882a593Smuzhiyun static void
tablet_tool_proximity_in(void * data,struct zwp_tablet_tool_v2 * tool,uint32_t serial,struct zwp_tablet_v2 * tablet,struct wl_surface * wl_surface)1560*4882a593Smuzhiyun tablet_tool_proximity_in(void *data, struct zwp_tablet_tool_v2 *tool,
1561*4882a593Smuzhiyun                          uint32_t serial, struct zwp_tablet_v2 *tablet,
1562*4882a593Smuzhiyun                          struct wl_surface *wl_surface)
1563*4882a593Smuzhiyun {
1564*4882a593Smuzhiyun     struct xwl_tablet_tool *xwl_tablet_tool = data;
1565*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = xwl_tablet_tool->seat;
1566*4882a593Smuzhiyun 
1567*4882a593Smuzhiyun     /* There's a race here where if we create and then immediately
1568*4882a593Smuzhiyun      * destroy a surface, we might end up in a state where the Wayland
1569*4882a593Smuzhiyun      * compositor sends us an event for a surface that doesn't exist.
1570*4882a593Smuzhiyun      *
1571*4882a593Smuzhiyun      * Don't process enter events in this case.
1572*4882a593Smuzhiyun      *
1573*4882a593Smuzhiyun      * see pointer_handle_enter()
1574*4882a593Smuzhiyun      */
1575*4882a593Smuzhiyun     if (wl_surface == NULL)
1576*4882a593Smuzhiyun         return;
1577*4882a593Smuzhiyun 
1578*4882a593Smuzhiyun     xwl_tablet_tool->proximity_in_serial = serial;
1579*4882a593Smuzhiyun     xwl_seat->tablet_focus_window = wl_surface_get_user_data(wl_surface);
1580*4882a593Smuzhiyun 
1581*4882a593Smuzhiyun     xwl_tablet_tool_set_cursor(xwl_tablet_tool);
1582*4882a593Smuzhiyun }
1583*4882a593Smuzhiyun 
1584*4882a593Smuzhiyun static void
tablet_tool_proximity_out(void * data,struct zwp_tablet_tool_v2 * tool)1585*4882a593Smuzhiyun tablet_tool_proximity_out(void *data, struct zwp_tablet_tool_v2 *tool)
1586*4882a593Smuzhiyun {
1587*4882a593Smuzhiyun     struct xwl_tablet_tool *xwl_tablet_tool = data;
1588*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = xwl_tablet_tool->seat;
1589*4882a593Smuzhiyun 
1590*4882a593Smuzhiyun     xwl_tablet_tool->proximity_in_serial = 0;
1591*4882a593Smuzhiyun     xwl_seat->tablet_focus_window = NULL;
1592*4882a593Smuzhiyun 
1593*4882a593Smuzhiyun     xwl_tablet_tool->pressure = 0;
1594*4882a593Smuzhiyun     xwl_tablet_tool->tilt_x = 0;
1595*4882a593Smuzhiyun     xwl_tablet_tool->tilt_y = 0;
1596*4882a593Smuzhiyun     xwl_tablet_tool->rotation = 0;
1597*4882a593Smuzhiyun     xwl_tablet_tool->slider = 0;
1598*4882a593Smuzhiyun }
1599*4882a593Smuzhiyun 
1600*4882a593Smuzhiyun static void
tablet_tool_down(void * data,struct zwp_tablet_tool_v2 * tool,uint32_t serial)1601*4882a593Smuzhiyun tablet_tool_down(void *data, struct zwp_tablet_tool_v2 *tool, uint32_t serial)
1602*4882a593Smuzhiyun {
1603*4882a593Smuzhiyun     struct xwl_tablet_tool *xwl_tablet_tool = data;
1604*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = xwl_tablet_tool->seat;
1605*4882a593Smuzhiyun     ValuatorMask mask;
1606*4882a593Smuzhiyun 
1607*4882a593Smuzhiyun     xwl_seat->xwl_screen->serial = serial;
1608*4882a593Smuzhiyun 
1609*4882a593Smuzhiyun     valuator_mask_zero(&mask);
1610*4882a593Smuzhiyun     QueuePointerEvents(xwl_tablet_tool->xdevice, ButtonPress, 1, 0, &mask);
1611*4882a593Smuzhiyun }
1612*4882a593Smuzhiyun 
1613*4882a593Smuzhiyun static void
tablet_tool_up(void * data,struct zwp_tablet_tool_v2 * tool)1614*4882a593Smuzhiyun tablet_tool_up(void *data, struct zwp_tablet_tool_v2 *tool)
1615*4882a593Smuzhiyun {
1616*4882a593Smuzhiyun     struct xwl_tablet_tool *xwl_tablet_tool = data;
1617*4882a593Smuzhiyun     ValuatorMask mask;
1618*4882a593Smuzhiyun 
1619*4882a593Smuzhiyun     valuator_mask_zero(&mask);
1620*4882a593Smuzhiyun     QueuePointerEvents(xwl_tablet_tool->xdevice, ButtonRelease, 1, 0, &mask);
1621*4882a593Smuzhiyun }
1622*4882a593Smuzhiyun 
1623*4882a593Smuzhiyun static void
tablet_tool_motion(void * data,struct zwp_tablet_tool_v2 * tool,wl_fixed_t x,wl_fixed_t y)1624*4882a593Smuzhiyun tablet_tool_motion(void *data, struct zwp_tablet_tool_v2 *tool,
1625*4882a593Smuzhiyun                    wl_fixed_t x, wl_fixed_t y)
1626*4882a593Smuzhiyun {
1627*4882a593Smuzhiyun     struct xwl_tablet_tool *xwl_tablet_tool = data;
1628*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = xwl_tablet_tool->seat;
1629*4882a593Smuzhiyun     int32_t dx, dy;
1630*4882a593Smuzhiyun     double sx = wl_fixed_to_double(x);
1631*4882a593Smuzhiyun     double sy = wl_fixed_to_double(y);
1632*4882a593Smuzhiyun 
1633*4882a593Smuzhiyun     if (!xwl_seat->tablet_focus_window)
1634*4882a593Smuzhiyun         return;
1635*4882a593Smuzhiyun 
1636*4882a593Smuzhiyun     dx = xwl_seat->tablet_focus_window->window->drawable.x;
1637*4882a593Smuzhiyun     dy = xwl_seat->tablet_focus_window->window->drawable.y;
1638*4882a593Smuzhiyun 
1639*4882a593Smuzhiyun     xwl_tablet_tool->x = (double) dx + sx;
1640*4882a593Smuzhiyun     xwl_tablet_tool->y = (double) dy + sy;
1641*4882a593Smuzhiyun }
1642*4882a593Smuzhiyun 
1643*4882a593Smuzhiyun static void
tablet_tool_pressure(void * data,struct zwp_tablet_tool_v2 * tool,uint32_t pressure)1644*4882a593Smuzhiyun tablet_tool_pressure(void *data, struct zwp_tablet_tool_v2 *tool,
1645*4882a593Smuzhiyun                      uint32_t pressure)
1646*4882a593Smuzhiyun {
1647*4882a593Smuzhiyun     struct xwl_tablet_tool *xwl_tablet_tool = data;
1648*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = xwl_tablet_tool->seat;
1649*4882a593Smuzhiyun 
1650*4882a593Smuzhiyun     if (!xwl_seat->tablet_focus_window)
1651*4882a593Smuzhiyun         return;
1652*4882a593Smuzhiyun 
1653*4882a593Smuzhiyun     /* normalized to 65535 already */
1654*4882a593Smuzhiyun     xwl_tablet_tool->pressure = pressure;
1655*4882a593Smuzhiyun }
1656*4882a593Smuzhiyun 
1657*4882a593Smuzhiyun static void
tablet_tool_distance(void * data,struct zwp_tablet_tool_v2 * tool,uint32_t distance_raw)1658*4882a593Smuzhiyun tablet_tool_distance(void *data, struct zwp_tablet_tool_v2 *tool,
1659*4882a593Smuzhiyun                      uint32_t distance_raw)
1660*4882a593Smuzhiyun {
1661*4882a593Smuzhiyun }
1662*4882a593Smuzhiyun 
1663*4882a593Smuzhiyun static void
tablet_tool_tilt(void * data,struct zwp_tablet_tool_v2 * tool,wl_fixed_t tilt_x,wl_fixed_t tilt_y)1664*4882a593Smuzhiyun tablet_tool_tilt(void *data, struct zwp_tablet_tool_v2 *tool,
1665*4882a593Smuzhiyun                  wl_fixed_t tilt_x, wl_fixed_t tilt_y)
1666*4882a593Smuzhiyun {
1667*4882a593Smuzhiyun     struct xwl_tablet_tool *xwl_tablet_tool = data;
1668*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = xwl_tablet_tool->seat;
1669*4882a593Smuzhiyun 
1670*4882a593Smuzhiyun     if (!xwl_seat->tablet_focus_window)
1671*4882a593Smuzhiyun         return;
1672*4882a593Smuzhiyun 
1673*4882a593Smuzhiyun     xwl_tablet_tool->tilt_x = wl_fixed_to_double(tilt_x);
1674*4882a593Smuzhiyun     xwl_tablet_tool->tilt_y = wl_fixed_to_double(tilt_y);
1675*4882a593Smuzhiyun }
1676*4882a593Smuzhiyun 
1677*4882a593Smuzhiyun static void
tablet_tool_rotation(void * data,struct zwp_tablet_tool_v2 * tool,wl_fixed_t angle)1678*4882a593Smuzhiyun tablet_tool_rotation(void *data, struct zwp_tablet_tool_v2 *tool,
1679*4882a593Smuzhiyun                      wl_fixed_t angle)
1680*4882a593Smuzhiyun {
1681*4882a593Smuzhiyun     struct xwl_tablet_tool *xwl_tablet_tool = data;
1682*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = xwl_tablet_tool->seat;
1683*4882a593Smuzhiyun     double rotation = wl_fixed_to_double(angle);
1684*4882a593Smuzhiyun 
1685*4882a593Smuzhiyun     if (!xwl_seat->tablet_focus_window)
1686*4882a593Smuzhiyun         return;
1687*4882a593Smuzhiyun 
1688*4882a593Smuzhiyun     /* change origin (buttons facing right [libinput +90 degrees]) and
1689*4882a593Smuzhiyun      * scaling (5 points per degree) to match wacom driver behavior
1690*4882a593Smuzhiyun      */
1691*4882a593Smuzhiyun     rotation = remainderf(rotation + 90.0f, 360.0f);
1692*4882a593Smuzhiyun     rotation *= 5.0f;
1693*4882a593Smuzhiyun     xwl_tablet_tool->rotation = rotation;
1694*4882a593Smuzhiyun }
1695*4882a593Smuzhiyun 
1696*4882a593Smuzhiyun static void
tablet_tool_slider(void * data,struct zwp_tablet_tool_v2 * tool,int32_t position_raw)1697*4882a593Smuzhiyun tablet_tool_slider(void *data, struct zwp_tablet_tool_v2 *tool,
1698*4882a593Smuzhiyun                    int32_t position_raw)
1699*4882a593Smuzhiyun {
1700*4882a593Smuzhiyun     struct xwl_tablet_tool *xwl_tablet_tool = data;
1701*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = xwl_tablet_tool->seat;
1702*4882a593Smuzhiyun     float position = position_raw / 65535.0;
1703*4882a593Smuzhiyun 
1704*4882a593Smuzhiyun     if (!xwl_seat->tablet_focus_window)
1705*4882a593Smuzhiyun         return;
1706*4882a593Smuzhiyun 
1707*4882a593Smuzhiyun     xwl_tablet_tool->slider = (position * 1799.0f) - 900.0f;
1708*4882a593Smuzhiyun }
1709*4882a593Smuzhiyun 
1710*4882a593Smuzhiyun static void
tablet_tool_wheel(void * data,struct zwp_tablet_tool_v2 * tool,wl_fixed_t degrees,int32_t clicks)1711*4882a593Smuzhiyun tablet_tool_wheel(void *data, struct zwp_tablet_tool_v2 *tool,
1712*4882a593Smuzhiyun                   wl_fixed_t degrees, int32_t clicks)
1713*4882a593Smuzhiyun {
1714*4882a593Smuzhiyun     struct xwl_tablet_tool *xwl_tablet_tool = data;
1715*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = xwl_tablet_tool->seat;
1716*4882a593Smuzhiyun 
1717*4882a593Smuzhiyun     if (!xwl_seat->tablet_focus_window)
1718*4882a593Smuzhiyun         return;
1719*4882a593Smuzhiyun 
1720*4882a593Smuzhiyun     xwl_tablet_tool->wheel_clicks = clicks;
1721*4882a593Smuzhiyun }
1722*4882a593Smuzhiyun 
1723*4882a593Smuzhiyun static void
tablet_tool_button_state(void * data,struct zwp_tablet_tool_v2 * tool,uint32_t serial,uint32_t button,uint32_t state)1724*4882a593Smuzhiyun tablet_tool_button_state(void *data, struct zwp_tablet_tool_v2 *tool,
1725*4882a593Smuzhiyun                          uint32_t serial, uint32_t button, uint32_t state)
1726*4882a593Smuzhiyun {
1727*4882a593Smuzhiyun     struct xwl_tablet_tool *xwl_tablet_tool = data;
1728*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = xwl_tablet_tool->seat;
1729*4882a593Smuzhiyun     uint32_t *mask = &xwl_tablet_tool->buttons_now;
1730*4882a593Smuzhiyun     int xbtn = 0;
1731*4882a593Smuzhiyun 
1732*4882a593Smuzhiyun     /* BTN_0 .. BTN_9 */
1733*4882a593Smuzhiyun     if (button >= 0x100 && button <= 0x109) {
1734*4882a593Smuzhiyun         xbtn = button - 0x100 + 1;
1735*4882a593Smuzhiyun     }
1736*4882a593Smuzhiyun     /* BTN_A .. BTN_Z */
1737*4882a593Smuzhiyun     else if (button >= 0x130 && button <= 0x135) {
1738*4882a593Smuzhiyun         xbtn = button - 0x130 + 10;
1739*4882a593Smuzhiyun     }
1740*4882a593Smuzhiyun     /* BTN_BASE .. BTN_BASE6 */
1741*4882a593Smuzhiyun     else if (button >= 0x126 && button <= 0x12b) {
1742*4882a593Smuzhiyun         xbtn = button - 0x126 + 16;
1743*4882a593Smuzhiyun     }
1744*4882a593Smuzhiyun     else {
1745*4882a593Smuzhiyun         switch (button) {
1746*4882a593Smuzhiyun         case 0x110: /* BTN_LEFT    */
1747*4882a593Smuzhiyun         case 0x14a: /* BTN_TOUCH   */
1748*4882a593Smuzhiyun             xbtn = 1;
1749*4882a593Smuzhiyun             break;
1750*4882a593Smuzhiyun 
1751*4882a593Smuzhiyun         case 0x112: /* BTN_MIDDLE  */
1752*4882a593Smuzhiyun         case 0x14b: /* BTN_STYLUS  */
1753*4882a593Smuzhiyun             xbtn = 2;
1754*4882a593Smuzhiyun             break;
1755*4882a593Smuzhiyun 
1756*4882a593Smuzhiyun         case 0x111: /* BTN_RIGHT   */
1757*4882a593Smuzhiyun         case 0x14c: /* BTN_STYLUS2 */
1758*4882a593Smuzhiyun             xbtn = 3;
1759*4882a593Smuzhiyun             break;
1760*4882a593Smuzhiyun 
1761*4882a593Smuzhiyun         case 0x113: /* BTN_SIDE    */
1762*4882a593Smuzhiyun         case 0x116: /* BTN_BACK    */
1763*4882a593Smuzhiyun         case 0x149: /* BTN_STYLUS3 */
1764*4882a593Smuzhiyun             xbtn = 8;
1765*4882a593Smuzhiyun             break;
1766*4882a593Smuzhiyun 
1767*4882a593Smuzhiyun         case 0x114: /* BTN_EXTRA   */
1768*4882a593Smuzhiyun         case 0x115: /* BTN_FORWARD */
1769*4882a593Smuzhiyun             xbtn = 9;
1770*4882a593Smuzhiyun             break;
1771*4882a593Smuzhiyun         }
1772*4882a593Smuzhiyun     }
1773*4882a593Smuzhiyun 
1774*4882a593Smuzhiyun     if (!xbtn) {
1775*4882a593Smuzhiyun         ErrorF("unknown tablet button number %d\n", button);
1776*4882a593Smuzhiyun         return;
1777*4882a593Smuzhiyun     }
1778*4882a593Smuzhiyun 
1779*4882a593Smuzhiyun     BUG_RETURN(xbtn >= 8 * sizeof(*mask));
1780*4882a593Smuzhiyun 
1781*4882a593Smuzhiyun     if (state)
1782*4882a593Smuzhiyun         SetBit(mask, xbtn - 1);
1783*4882a593Smuzhiyun     else
1784*4882a593Smuzhiyun         ClearBit(mask, xbtn - 1);
1785*4882a593Smuzhiyun 
1786*4882a593Smuzhiyun     xwl_seat->xwl_screen->serial = serial;
1787*4882a593Smuzhiyun }
1788*4882a593Smuzhiyun 
1789*4882a593Smuzhiyun static void
tablet_tool_frame(void * data,struct zwp_tablet_tool_v2 * tool,uint32_t time)1790*4882a593Smuzhiyun tablet_tool_frame(void *data, struct zwp_tablet_tool_v2 *tool, uint32_t time)
1791*4882a593Smuzhiyun {
1792*4882a593Smuzhiyun     struct xwl_tablet_tool *xwl_tablet_tool = data;
1793*4882a593Smuzhiyun     ValuatorMask mask;
1794*4882a593Smuzhiyun     uint32_t released, pressed, diff;
1795*4882a593Smuzhiyun     int button;
1796*4882a593Smuzhiyun 
1797*4882a593Smuzhiyun     valuator_mask_zero(&mask);
1798*4882a593Smuzhiyun     valuator_mask_set_double(&mask, 0, xwl_tablet_tool->x);
1799*4882a593Smuzhiyun     valuator_mask_set_double(&mask, 1, xwl_tablet_tool->y);
1800*4882a593Smuzhiyun     valuator_mask_set(&mask, 2, xwl_tablet_tool->pressure);
1801*4882a593Smuzhiyun     valuator_mask_set_double(&mask, 3, xwl_tablet_tool->tilt_x);
1802*4882a593Smuzhiyun     valuator_mask_set_double(&mask, 4, xwl_tablet_tool->tilt_y);
1803*4882a593Smuzhiyun     valuator_mask_set_double(&mask, 5, xwl_tablet_tool->rotation + xwl_tablet_tool->slider);
1804*4882a593Smuzhiyun 
1805*4882a593Smuzhiyun     QueuePointerEvents(xwl_tablet_tool->xdevice, MotionNotify, 0,
1806*4882a593Smuzhiyun                POINTER_ABSOLUTE | POINTER_DESKTOP, &mask);
1807*4882a593Smuzhiyun 
1808*4882a593Smuzhiyun     valuator_mask_zero(&mask);
1809*4882a593Smuzhiyun 
1810*4882a593Smuzhiyun     diff = xwl_tablet_tool->buttons_prev ^ xwl_tablet_tool->buttons_now;
1811*4882a593Smuzhiyun     released = diff & ~xwl_tablet_tool->buttons_now;
1812*4882a593Smuzhiyun     pressed = diff & xwl_tablet_tool->buttons_now;
1813*4882a593Smuzhiyun 
1814*4882a593Smuzhiyun     button = 1;
1815*4882a593Smuzhiyun     while (released) {
1816*4882a593Smuzhiyun         if (released & 0x1)
1817*4882a593Smuzhiyun             QueuePointerEvents(xwl_tablet_tool->xdevice,
1818*4882a593Smuzhiyun                                ButtonRelease, button, 0, &mask);
1819*4882a593Smuzhiyun         button++;
1820*4882a593Smuzhiyun         released >>= 1;
1821*4882a593Smuzhiyun     }
1822*4882a593Smuzhiyun 
1823*4882a593Smuzhiyun     button = 1;
1824*4882a593Smuzhiyun     while (pressed) {
1825*4882a593Smuzhiyun         if (pressed & 0x1)
1826*4882a593Smuzhiyun             QueuePointerEvents(xwl_tablet_tool->xdevice,
1827*4882a593Smuzhiyun                                ButtonPress, button, 0, &mask);
1828*4882a593Smuzhiyun         button++;
1829*4882a593Smuzhiyun         pressed >>= 1;
1830*4882a593Smuzhiyun     }
1831*4882a593Smuzhiyun 
1832*4882a593Smuzhiyun     xwl_tablet_tool->buttons_prev = xwl_tablet_tool->buttons_now;
1833*4882a593Smuzhiyun 
1834*4882a593Smuzhiyun     while (xwl_tablet_tool->wheel_clicks) {
1835*4882a593Smuzhiyun             if (xwl_tablet_tool->wheel_clicks < 0) {
1836*4882a593Smuzhiyun                 button = 4;
1837*4882a593Smuzhiyun                 xwl_tablet_tool->wheel_clicks++;
1838*4882a593Smuzhiyun             }
1839*4882a593Smuzhiyun             else {
1840*4882a593Smuzhiyun                 button = 5;
1841*4882a593Smuzhiyun                 xwl_tablet_tool->wheel_clicks--;
1842*4882a593Smuzhiyun             }
1843*4882a593Smuzhiyun 
1844*4882a593Smuzhiyun             QueuePointerEvents(xwl_tablet_tool->xdevice,
1845*4882a593Smuzhiyun                                ButtonPress, button, 0, &mask);
1846*4882a593Smuzhiyun             QueuePointerEvents(xwl_tablet_tool->xdevice,
1847*4882a593Smuzhiyun                                ButtonRelease, button, 0, &mask);
1848*4882a593Smuzhiyun 
1849*4882a593Smuzhiyun     }
1850*4882a593Smuzhiyun }
1851*4882a593Smuzhiyun 
1852*4882a593Smuzhiyun static const struct zwp_tablet_tool_v2_listener tablet_tool_listener = {
1853*4882a593Smuzhiyun     tablet_tool_receive_type,
1854*4882a593Smuzhiyun     tablet_tool_receive_hardware_serial,
1855*4882a593Smuzhiyun     tablet_tool_receive_hardware_id_wacom,
1856*4882a593Smuzhiyun     tablet_tool_receive_capability,
1857*4882a593Smuzhiyun     tablet_tool_receive_done,
1858*4882a593Smuzhiyun     tablet_tool_receive_removed,
1859*4882a593Smuzhiyun     tablet_tool_proximity_in,
1860*4882a593Smuzhiyun     tablet_tool_proximity_out,
1861*4882a593Smuzhiyun     tablet_tool_down,
1862*4882a593Smuzhiyun     tablet_tool_up,
1863*4882a593Smuzhiyun     tablet_tool_motion,
1864*4882a593Smuzhiyun     tablet_tool_pressure,
1865*4882a593Smuzhiyun     tablet_tool_distance,
1866*4882a593Smuzhiyun     tablet_tool_tilt,
1867*4882a593Smuzhiyun     tablet_tool_rotation,
1868*4882a593Smuzhiyun     tablet_tool_slider,
1869*4882a593Smuzhiyun     tablet_tool_wheel,
1870*4882a593Smuzhiyun     tablet_tool_button_state,
1871*4882a593Smuzhiyun     tablet_tool_frame
1872*4882a593Smuzhiyun };
1873*4882a593Smuzhiyun 
1874*4882a593Smuzhiyun static void
tablet_pad_ring_destroy(struct xwl_tablet_pad_ring * ring)1875*4882a593Smuzhiyun tablet_pad_ring_destroy(struct xwl_tablet_pad_ring *ring)
1876*4882a593Smuzhiyun {
1877*4882a593Smuzhiyun     zwp_tablet_pad_ring_v2_destroy(ring->ring);
1878*4882a593Smuzhiyun     xorg_list_del(&ring->link);
1879*4882a593Smuzhiyun     free(ring);
1880*4882a593Smuzhiyun }
1881*4882a593Smuzhiyun 
1882*4882a593Smuzhiyun static void
tablet_pad_ring_source(void * data,struct zwp_tablet_pad_ring_v2 * zwp_tablet_pad_ring_v2,uint32_t source)1883*4882a593Smuzhiyun tablet_pad_ring_source(void *data,
1884*4882a593Smuzhiyun                        struct zwp_tablet_pad_ring_v2 *zwp_tablet_pad_ring_v2,
1885*4882a593Smuzhiyun                        uint32_t source)
1886*4882a593Smuzhiyun {
1887*4882a593Smuzhiyun }
1888*4882a593Smuzhiyun 
1889*4882a593Smuzhiyun static void
tablet_pad_ring_angle(void * data,struct zwp_tablet_pad_ring_v2 * zwp_tablet_pad_ring_v2,wl_fixed_t degrees)1890*4882a593Smuzhiyun tablet_pad_ring_angle(void *data,
1891*4882a593Smuzhiyun                       struct zwp_tablet_pad_ring_v2 *zwp_tablet_pad_ring_v2,
1892*4882a593Smuzhiyun                       wl_fixed_t degrees)
1893*4882a593Smuzhiyun {
1894*4882a593Smuzhiyun     struct xwl_tablet_pad_ring *ring = data;
1895*4882a593Smuzhiyun     struct xwl_tablet_pad *pad = ring->group->pad;
1896*4882a593Smuzhiyun     double deg = wl_fixed_to_double(degrees);
1897*4882a593Smuzhiyun     ValuatorMask mask;
1898*4882a593Smuzhiyun 
1899*4882a593Smuzhiyun     valuator_mask_zero(&mask);
1900*4882a593Smuzhiyun     valuator_mask_set(&mask, 5 + ring->index, deg/360.0  * 71);
1901*4882a593Smuzhiyun     QueuePointerEvents(pad->xdevice, MotionNotify, 0, 0, &mask);
1902*4882a593Smuzhiyun }
1903*4882a593Smuzhiyun 
1904*4882a593Smuzhiyun static void
tablet_pad_ring_stop(void * data,struct zwp_tablet_pad_ring_v2 * zwp_tablet_pad_ring_v2)1905*4882a593Smuzhiyun tablet_pad_ring_stop(void *data,
1906*4882a593Smuzhiyun                      struct zwp_tablet_pad_ring_v2 *zwp_tablet_pad_ring_v2)
1907*4882a593Smuzhiyun {
1908*4882a593Smuzhiyun }
1909*4882a593Smuzhiyun 
1910*4882a593Smuzhiyun static void
tablet_pad_ring_frame(void * data,struct zwp_tablet_pad_ring_v2 * zwp_tablet_pad_ring_v2,uint32_t time)1911*4882a593Smuzhiyun tablet_pad_ring_frame(void *data,
1912*4882a593Smuzhiyun                       struct zwp_tablet_pad_ring_v2 *zwp_tablet_pad_ring_v2,
1913*4882a593Smuzhiyun                       uint32_t time)
1914*4882a593Smuzhiyun {
1915*4882a593Smuzhiyun }
1916*4882a593Smuzhiyun 
1917*4882a593Smuzhiyun static const struct zwp_tablet_pad_ring_v2_listener tablet_pad_ring_listener = {
1918*4882a593Smuzhiyun     tablet_pad_ring_source,
1919*4882a593Smuzhiyun     tablet_pad_ring_angle,
1920*4882a593Smuzhiyun     tablet_pad_ring_stop,
1921*4882a593Smuzhiyun     tablet_pad_ring_frame,
1922*4882a593Smuzhiyun };
1923*4882a593Smuzhiyun 
1924*4882a593Smuzhiyun 
1925*4882a593Smuzhiyun static void
tablet_pad_strip_destroy(struct xwl_tablet_pad_strip * strip)1926*4882a593Smuzhiyun tablet_pad_strip_destroy(struct xwl_tablet_pad_strip *strip)
1927*4882a593Smuzhiyun {
1928*4882a593Smuzhiyun     zwp_tablet_pad_strip_v2_destroy(strip->strip);
1929*4882a593Smuzhiyun     xorg_list_del(&strip->link);
1930*4882a593Smuzhiyun     free(strip);
1931*4882a593Smuzhiyun }
1932*4882a593Smuzhiyun 
1933*4882a593Smuzhiyun static void
tablet_pad_strip_source(void * data,struct zwp_tablet_pad_strip_v2 * zwp_tablet_pad_strip_v2,uint32_t source)1934*4882a593Smuzhiyun tablet_pad_strip_source(void *data,
1935*4882a593Smuzhiyun                         struct zwp_tablet_pad_strip_v2 *zwp_tablet_pad_strip_v2,
1936*4882a593Smuzhiyun                         uint32_t source)
1937*4882a593Smuzhiyun {
1938*4882a593Smuzhiyun }
1939*4882a593Smuzhiyun 
1940*4882a593Smuzhiyun static void
tablet_pad_strip_position(void * data,struct zwp_tablet_pad_strip_v2 * zwp_tablet_pad_strip_v2,uint32_t position)1941*4882a593Smuzhiyun tablet_pad_strip_position(void *data,
1942*4882a593Smuzhiyun                           struct zwp_tablet_pad_strip_v2 *zwp_tablet_pad_strip_v2,
1943*4882a593Smuzhiyun                           uint32_t position)
1944*4882a593Smuzhiyun {
1945*4882a593Smuzhiyun     struct xwl_tablet_pad_strip *strip = data;
1946*4882a593Smuzhiyun     struct xwl_tablet_pad *pad = strip->group->pad;
1947*4882a593Smuzhiyun     ValuatorMask mask;
1948*4882a593Smuzhiyun 
1949*4882a593Smuzhiyun     valuator_mask_zero(&mask);
1950*4882a593Smuzhiyun     valuator_mask_set(&mask, 3 + strip->index, position/65535.0 * 2048);
1951*4882a593Smuzhiyun     QueuePointerEvents(pad->xdevice, MotionNotify, 0, 0, &mask);
1952*4882a593Smuzhiyun }
1953*4882a593Smuzhiyun 
1954*4882a593Smuzhiyun static void
tablet_pad_strip_stop(void * data,struct zwp_tablet_pad_strip_v2 * zwp_tablet_pad_strip_v2)1955*4882a593Smuzhiyun tablet_pad_strip_stop(void *data,
1956*4882a593Smuzhiyun                       struct zwp_tablet_pad_strip_v2 *zwp_tablet_pad_strip_v2)
1957*4882a593Smuzhiyun {
1958*4882a593Smuzhiyun }
1959*4882a593Smuzhiyun 
1960*4882a593Smuzhiyun static void
tablet_pad_strip_frame(void * data,struct zwp_tablet_pad_strip_v2 * zwp_tablet_pad_strip_v2,uint32_t time)1961*4882a593Smuzhiyun tablet_pad_strip_frame(void *data,
1962*4882a593Smuzhiyun                        struct zwp_tablet_pad_strip_v2 *zwp_tablet_pad_strip_v2,
1963*4882a593Smuzhiyun                        uint32_t time)
1964*4882a593Smuzhiyun {
1965*4882a593Smuzhiyun }
1966*4882a593Smuzhiyun 
1967*4882a593Smuzhiyun static const struct zwp_tablet_pad_strip_v2_listener tablet_pad_strip_listener = {
1968*4882a593Smuzhiyun     tablet_pad_strip_source,
1969*4882a593Smuzhiyun     tablet_pad_strip_position,
1970*4882a593Smuzhiyun     tablet_pad_strip_stop,
1971*4882a593Smuzhiyun     tablet_pad_strip_frame,
1972*4882a593Smuzhiyun };
1973*4882a593Smuzhiyun 
1974*4882a593Smuzhiyun static void
tablet_pad_group_destroy(struct xwl_tablet_pad_group * group)1975*4882a593Smuzhiyun tablet_pad_group_destroy(struct xwl_tablet_pad_group *group)
1976*4882a593Smuzhiyun {
1977*4882a593Smuzhiyun     struct xwl_tablet_pad_ring *r, *tr;
1978*4882a593Smuzhiyun     struct xwl_tablet_pad_strip *s, *ts;
1979*4882a593Smuzhiyun 
1980*4882a593Smuzhiyun     xorg_list_for_each_entry_safe(r, tr,
1981*4882a593Smuzhiyun                                   &group->pad_group_ring_list,
1982*4882a593Smuzhiyun                                   link)
1983*4882a593Smuzhiyun         tablet_pad_ring_destroy(r);
1984*4882a593Smuzhiyun 
1985*4882a593Smuzhiyun     xorg_list_for_each_entry_safe(s, ts,
1986*4882a593Smuzhiyun                                   &group->pad_group_strip_list,
1987*4882a593Smuzhiyun                                   link)
1988*4882a593Smuzhiyun         tablet_pad_strip_destroy(s);
1989*4882a593Smuzhiyun 
1990*4882a593Smuzhiyun     zwp_tablet_pad_group_v2_destroy(group->group);
1991*4882a593Smuzhiyun     xorg_list_del(&group->link);
1992*4882a593Smuzhiyun     free(group);
1993*4882a593Smuzhiyun }
1994*4882a593Smuzhiyun 
1995*4882a593Smuzhiyun static void
tablet_pad_group_buttons(void * data,struct zwp_tablet_pad_group_v2 * zwp_tablet_pad_group_v2,struct wl_array * buttons)1996*4882a593Smuzhiyun tablet_pad_group_buttons(void *data,
1997*4882a593Smuzhiyun                          struct zwp_tablet_pad_group_v2 *zwp_tablet_pad_group_v2,
1998*4882a593Smuzhiyun                          struct wl_array *buttons)
1999*4882a593Smuzhiyun {
2000*4882a593Smuzhiyun 
2001*4882a593Smuzhiyun }
2002*4882a593Smuzhiyun 
2003*4882a593Smuzhiyun static void
tablet_pad_group_ring(void * data,struct zwp_tablet_pad_group_v2 * zwp_tablet_pad_group_v2,struct zwp_tablet_pad_ring_v2 * wp_ring)2004*4882a593Smuzhiyun tablet_pad_group_ring(void *data,
2005*4882a593Smuzhiyun                       struct zwp_tablet_pad_group_v2 *zwp_tablet_pad_group_v2,
2006*4882a593Smuzhiyun                       struct zwp_tablet_pad_ring_v2 *wp_ring)
2007*4882a593Smuzhiyun {
2008*4882a593Smuzhiyun     static unsigned int ring_index = 0;
2009*4882a593Smuzhiyun     struct xwl_tablet_pad_group *group = data;
2010*4882a593Smuzhiyun     struct xwl_tablet_pad_ring *ring;
2011*4882a593Smuzhiyun 
2012*4882a593Smuzhiyun     ring = calloc(1, sizeof *ring);
2013*4882a593Smuzhiyun     if (ring == NULL) {
2014*4882a593Smuzhiyun         ErrorF("%s ENOMEM\n", __func__);
2015*4882a593Smuzhiyun         return;
2016*4882a593Smuzhiyun     }
2017*4882a593Smuzhiyun 
2018*4882a593Smuzhiyun     ring->index = ring_index++;
2019*4882a593Smuzhiyun     ring->group = group;
2020*4882a593Smuzhiyun     ring->ring = wp_ring;
2021*4882a593Smuzhiyun 
2022*4882a593Smuzhiyun     xorg_list_add(&ring->link, &group->pad_group_ring_list);
2023*4882a593Smuzhiyun 
2024*4882a593Smuzhiyun     zwp_tablet_pad_ring_v2_add_listener(wp_ring, &tablet_pad_ring_listener,
2025*4882a593Smuzhiyun                                         ring);
2026*4882a593Smuzhiyun }
2027*4882a593Smuzhiyun 
2028*4882a593Smuzhiyun static void
tablet_pad_group_strip(void * data,struct zwp_tablet_pad_group_v2 * zwp_tablet_pad_group_v2,struct zwp_tablet_pad_strip_v2 * wp_strip)2029*4882a593Smuzhiyun tablet_pad_group_strip(void *data,
2030*4882a593Smuzhiyun                        struct zwp_tablet_pad_group_v2 *zwp_tablet_pad_group_v2,
2031*4882a593Smuzhiyun                        struct zwp_tablet_pad_strip_v2 *wp_strip)
2032*4882a593Smuzhiyun {
2033*4882a593Smuzhiyun     static unsigned int strip_index = 0;
2034*4882a593Smuzhiyun     struct xwl_tablet_pad_group *group = data;
2035*4882a593Smuzhiyun     struct xwl_tablet_pad_strip *strip;
2036*4882a593Smuzhiyun 
2037*4882a593Smuzhiyun     strip = calloc(1, sizeof *strip);
2038*4882a593Smuzhiyun     if (strip == NULL) {
2039*4882a593Smuzhiyun         ErrorF("%s ENOMEM\n", __func__);
2040*4882a593Smuzhiyun         return;
2041*4882a593Smuzhiyun     }
2042*4882a593Smuzhiyun 
2043*4882a593Smuzhiyun     strip->index = strip_index++;
2044*4882a593Smuzhiyun     strip->group = group;
2045*4882a593Smuzhiyun     strip->strip = wp_strip;
2046*4882a593Smuzhiyun 
2047*4882a593Smuzhiyun     xorg_list_add(&strip->link, &group->pad_group_strip_list);
2048*4882a593Smuzhiyun 
2049*4882a593Smuzhiyun     zwp_tablet_pad_strip_v2_add_listener(wp_strip, &tablet_pad_strip_listener,
2050*4882a593Smuzhiyun                                          strip);
2051*4882a593Smuzhiyun }
2052*4882a593Smuzhiyun 
2053*4882a593Smuzhiyun static void
tablet_pad_group_modes(void * data,struct zwp_tablet_pad_group_v2 * zwp_tablet_pad_group_v2,uint32_t modes)2054*4882a593Smuzhiyun tablet_pad_group_modes(void *data,
2055*4882a593Smuzhiyun                        struct zwp_tablet_pad_group_v2 *zwp_tablet_pad_group_v2,
2056*4882a593Smuzhiyun                        uint32_t modes)
2057*4882a593Smuzhiyun {
2058*4882a593Smuzhiyun 
2059*4882a593Smuzhiyun }
2060*4882a593Smuzhiyun 
2061*4882a593Smuzhiyun static void
tablet_pad_group_done(void * data,struct zwp_tablet_pad_group_v2 * zwp_tablet_pad_group_v2)2062*4882a593Smuzhiyun tablet_pad_group_done(void *data,
2063*4882a593Smuzhiyun                       struct zwp_tablet_pad_group_v2 *zwp_tablet_pad_group_v2)
2064*4882a593Smuzhiyun {
2065*4882a593Smuzhiyun 
2066*4882a593Smuzhiyun }
2067*4882a593Smuzhiyun 
2068*4882a593Smuzhiyun static void
tablet_pad_group_mode_switch(void * data,struct zwp_tablet_pad_group_v2 * zwp_tablet_pad_group_v2,uint32_t time,uint32_t serial,uint32_t mode)2069*4882a593Smuzhiyun tablet_pad_group_mode_switch(void *data,
2070*4882a593Smuzhiyun                              struct zwp_tablet_pad_group_v2 *zwp_tablet_pad_group_v2,
2071*4882a593Smuzhiyun                              uint32_t time,
2072*4882a593Smuzhiyun                              uint32_t serial,
2073*4882a593Smuzhiyun                              uint32_t mode)
2074*4882a593Smuzhiyun {
2075*4882a593Smuzhiyun 
2076*4882a593Smuzhiyun }
2077*4882a593Smuzhiyun 
2078*4882a593Smuzhiyun static struct zwp_tablet_pad_group_v2_listener tablet_pad_group_listener = {
2079*4882a593Smuzhiyun     tablet_pad_group_buttons,
2080*4882a593Smuzhiyun     tablet_pad_group_ring,
2081*4882a593Smuzhiyun     tablet_pad_group_strip,
2082*4882a593Smuzhiyun     tablet_pad_group_modes,
2083*4882a593Smuzhiyun     tablet_pad_group_done,
2084*4882a593Smuzhiyun     tablet_pad_group_mode_switch,
2085*4882a593Smuzhiyun };
2086*4882a593Smuzhiyun 
2087*4882a593Smuzhiyun static int
xwl_tablet_pad_proc(DeviceIntPtr device,int what)2088*4882a593Smuzhiyun xwl_tablet_pad_proc(DeviceIntPtr device, int what)
2089*4882a593Smuzhiyun {
2090*4882a593Smuzhiyun     struct xwl_tablet_pad *pad = dixGetPrivate(&device->devPrivates,
2091*4882a593Smuzhiyun                                                &xwl_tablet_private_key);
2092*4882a593Smuzhiyun     /* Axis layout mirrors that of xf86-input-wacom to have better
2093*4882a593Smuzhiyun        compatibility with existing clients */
2094*4882a593Smuzhiyun #define NAXES 7
2095*4882a593Smuzhiyun     Atom axes_labels[NAXES] = { 0 };
2096*4882a593Smuzhiyun     BYTE map[MAX_BUTTONS + 1];
2097*4882a593Smuzhiyun     int i = 0;
2098*4882a593Smuzhiyun     Atom btn_labels[MAX_BUTTONS] = { 0 }; /* btn labels are meaningless */
2099*4882a593Smuzhiyun     int nbuttons;
2100*4882a593Smuzhiyun 
2101*4882a593Smuzhiyun     switch (what) {
2102*4882a593Smuzhiyun     case DEVICE_INIT:
2103*4882a593Smuzhiyun         device->public.on = FALSE;
2104*4882a593Smuzhiyun 
2105*4882a593Smuzhiyun         axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_X);
2106*4882a593Smuzhiyun         axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_Y);
2107*4882a593Smuzhiyun         /* The others have no good mapping */
2108*4882a593Smuzhiyun 
2109*4882a593Smuzhiyun         if (!InitValuatorClassDeviceStruct(device, NAXES, axes_labels,
2110*4882a593Smuzhiyun                                            GetMotionHistorySize(), Absolute))
2111*4882a593Smuzhiyun             return BadValue;
2112*4882a593Smuzhiyun 
2113*4882a593Smuzhiyun         for (i = 1; i <= MAX_BUTTONS; i++)
2114*4882a593Smuzhiyun             map[i] = i;
2115*4882a593Smuzhiyun 
2116*4882a593Smuzhiyun         /* We need at least 7 buttons to allow scrolling */
2117*4882a593Smuzhiyun         nbuttons = min(max(pad->nbuttons + 4, 7), MAX_BUTTONS);
2118*4882a593Smuzhiyun 
2119*4882a593Smuzhiyun         if (!InitButtonClassDeviceStruct(device, nbuttons,
2120*4882a593Smuzhiyun                                          btn_labels, map))
2121*4882a593Smuzhiyun             return BadValue;
2122*4882a593Smuzhiyun 
2123*4882a593Smuzhiyun         /* Valuators */
2124*4882a593Smuzhiyun         InitValuatorAxisStruct(device, 0, axes_labels[0],
2125*4882a593Smuzhiyun                                0, 100, 1, 0, 1, Absolute);
2126*4882a593Smuzhiyun         InitValuatorAxisStruct(device, 1, axes_labels[1],
2127*4882a593Smuzhiyun                                0, 100, 1, 0, 1, Absolute);
2128*4882a593Smuzhiyun         /* Pressure - unused, for backwards compat only */
2129*4882a593Smuzhiyun         InitValuatorAxisStruct(device, 2, axes_labels[2],
2130*4882a593Smuzhiyun                                0, 2048, 1, 0, 1, Absolute);
2131*4882a593Smuzhiyun         /* strip x */
2132*4882a593Smuzhiyun         InitValuatorAxisStruct(device, 3, axes_labels[3],
2133*4882a593Smuzhiyun                                0, 2048, 1, 0, 1, Absolute);
2134*4882a593Smuzhiyun         /* strip y */
2135*4882a593Smuzhiyun         InitValuatorAxisStruct(device, 4, axes_labels[4],
2136*4882a593Smuzhiyun                                0, 2048, 1, 0, 1, Absolute);
2137*4882a593Smuzhiyun         /* ring */
2138*4882a593Smuzhiyun         InitValuatorAxisStruct(device, 5, axes_labels[5],
2139*4882a593Smuzhiyun                                0, 71, 1, 0, 1, Absolute);
2140*4882a593Smuzhiyun         /* ring2 */
2141*4882a593Smuzhiyun         InitValuatorAxisStruct(device, 6, axes_labels[6],
2142*4882a593Smuzhiyun                                0, 71, 1, 0, 1, Absolute);
2143*4882a593Smuzhiyun 
2144*4882a593Smuzhiyun         if (!InitPtrFeedbackClassDeviceStruct(device, xwl_pointer_control))
2145*4882a593Smuzhiyun             return BadValue;
2146*4882a593Smuzhiyun 
2147*4882a593Smuzhiyun         return Success;
2148*4882a593Smuzhiyun 
2149*4882a593Smuzhiyun     case DEVICE_ON:
2150*4882a593Smuzhiyun         device->public.on = TRUE;
2151*4882a593Smuzhiyun         return Success;
2152*4882a593Smuzhiyun 
2153*4882a593Smuzhiyun     case DEVICE_OFF:
2154*4882a593Smuzhiyun     case DEVICE_CLOSE:
2155*4882a593Smuzhiyun         device->public.on = FALSE;
2156*4882a593Smuzhiyun         return Success;
2157*4882a593Smuzhiyun     }
2158*4882a593Smuzhiyun 
2159*4882a593Smuzhiyun     return BadMatch;
2160*4882a593Smuzhiyun #undef NAXES
2161*4882a593Smuzhiyun }
2162*4882a593Smuzhiyun 
2163*4882a593Smuzhiyun static void
tablet_pad_group(void * data,struct zwp_tablet_pad_v2 * zwp_tablet_pad_v2,struct zwp_tablet_pad_group_v2 * pad_group)2164*4882a593Smuzhiyun tablet_pad_group(void *data,
2165*4882a593Smuzhiyun                  struct zwp_tablet_pad_v2 *zwp_tablet_pad_v2,
2166*4882a593Smuzhiyun                  struct zwp_tablet_pad_group_v2 *pad_group)
2167*4882a593Smuzhiyun {
2168*4882a593Smuzhiyun     struct xwl_tablet_pad *pad = data;
2169*4882a593Smuzhiyun     struct xwl_tablet_pad_group *group;
2170*4882a593Smuzhiyun 
2171*4882a593Smuzhiyun     group = calloc(1, sizeof *group);
2172*4882a593Smuzhiyun     if (pad == NULL) {
2173*4882a593Smuzhiyun         ErrorF("%s ENOMEM\n", __func__);
2174*4882a593Smuzhiyun         return;
2175*4882a593Smuzhiyun     }
2176*4882a593Smuzhiyun 
2177*4882a593Smuzhiyun     group->pad = pad;
2178*4882a593Smuzhiyun     group->group = pad_group;
2179*4882a593Smuzhiyun     xorg_list_init(&group->pad_group_ring_list);
2180*4882a593Smuzhiyun     xorg_list_init(&group->pad_group_strip_list);
2181*4882a593Smuzhiyun 
2182*4882a593Smuzhiyun     xorg_list_add(&group->link, &pad->pad_group_list);
2183*4882a593Smuzhiyun 
2184*4882a593Smuzhiyun     zwp_tablet_pad_group_v2_add_listener(pad_group,
2185*4882a593Smuzhiyun                                          &tablet_pad_group_listener,
2186*4882a593Smuzhiyun                                          group);
2187*4882a593Smuzhiyun }
2188*4882a593Smuzhiyun 
2189*4882a593Smuzhiyun static void
tablet_pad_path(void * data,struct zwp_tablet_pad_v2 * zwp_tablet_pad_v2,const char * path)2190*4882a593Smuzhiyun tablet_pad_path(void *data,
2191*4882a593Smuzhiyun                 struct zwp_tablet_pad_v2 *zwp_tablet_pad_v2,
2192*4882a593Smuzhiyun                 const char *path)
2193*4882a593Smuzhiyun {
2194*4882a593Smuzhiyun 
2195*4882a593Smuzhiyun }
2196*4882a593Smuzhiyun 
2197*4882a593Smuzhiyun static void
tablet_pad_buttons(void * data,struct zwp_tablet_pad_v2 * zwp_tablet_pad_v2,uint32_t buttons)2198*4882a593Smuzhiyun tablet_pad_buttons(void *data,
2199*4882a593Smuzhiyun                    struct zwp_tablet_pad_v2 *zwp_tablet_pad_v2,
2200*4882a593Smuzhiyun                    uint32_t buttons)
2201*4882a593Smuzhiyun {
2202*4882a593Smuzhiyun     struct xwl_tablet_pad *pad = data;
2203*4882a593Smuzhiyun 
2204*4882a593Smuzhiyun     pad->nbuttons = buttons;
2205*4882a593Smuzhiyun }
2206*4882a593Smuzhiyun 
2207*4882a593Smuzhiyun static void
tablet_pad_done(void * data,struct zwp_tablet_pad_v2 * zwp_tablet_pad_v2)2208*4882a593Smuzhiyun tablet_pad_done(void *data,
2209*4882a593Smuzhiyun                 struct zwp_tablet_pad_v2 *zwp_tablet_pad_v2)
2210*4882a593Smuzhiyun {
2211*4882a593Smuzhiyun     struct xwl_tablet_pad *pad = data;
2212*4882a593Smuzhiyun 
2213*4882a593Smuzhiyun     pad->xdevice = add_device(pad->seat, "xwayland-pad",
2214*4882a593Smuzhiyun                               xwl_tablet_pad_proc);
2215*4882a593Smuzhiyun     dixSetPrivate(&pad->xdevice->devPrivates, &xwl_tablet_private_key, pad);
2216*4882a593Smuzhiyun     ActivateDevice(pad->xdevice, TRUE);
2217*4882a593Smuzhiyun     EnableDevice(pad->xdevice, TRUE);
2218*4882a593Smuzhiyun }
2219*4882a593Smuzhiyun 
2220*4882a593Smuzhiyun static void
tablet_pad_button(void * data,struct zwp_tablet_pad_v2 * zwp_tablet_pad_v2,uint32_t time,uint32_t button,uint32_t state)2221*4882a593Smuzhiyun tablet_pad_button(void *data,
2222*4882a593Smuzhiyun                   struct zwp_tablet_pad_v2 *zwp_tablet_pad_v2,
2223*4882a593Smuzhiyun                   uint32_t time,
2224*4882a593Smuzhiyun                   uint32_t button,
2225*4882a593Smuzhiyun                   uint32_t state)
2226*4882a593Smuzhiyun {
2227*4882a593Smuzhiyun     struct xwl_tablet_pad *pad = data;
2228*4882a593Smuzhiyun     ValuatorMask mask;
2229*4882a593Smuzhiyun 
2230*4882a593Smuzhiyun     button++; /* wayland index vs X's 1-offset */
2231*4882a593Smuzhiyun     /* skip scroll wheel buttons 4-7 */
2232*4882a593Smuzhiyun     button = button > 3 ? button + 4 : button;
2233*4882a593Smuzhiyun 
2234*4882a593Smuzhiyun     valuator_mask_zero(&mask);
2235*4882a593Smuzhiyun     QueuePointerEvents(pad->xdevice,
2236*4882a593Smuzhiyun                        state ? ButtonPress : ButtonRelease, button, 0, &mask);
2237*4882a593Smuzhiyun }
2238*4882a593Smuzhiyun 
2239*4882a593Smuzhiyun static void
tablet_pad_enter(void * data,struct zwp_tablet_pad_v2 * zwp_tablet_pad_v2,uint32_t serial,struct zwp_tablet_v2 * tablet,struct wl_surface * surface)2240*4882a593Smuzhiyun tablet_pad_enter(void *data,
2241*4882a593Smuzhiyun                  struct zwp_tablet_pad_v2 *zwp_tablet_pad_v2,
2242*4882a593Smuzhiyun                  uint32_t serial,
2243*4882a593Smuzhiyun                  struct zwp_tablet_v2 *tablet,
2244*4882a593Smuzhiyun                  struct wl_surface *surface)
2245*4882a593Smuzhiyun {
2246*4882a593Smuzhiyun     /* pairs the pad with the tablet but also to set the focus. We
2247*4882a593Smuzhiyun      * don't care about the pairing and always use X's focus */
2248*4882a593Smuzhiyun }
2249*4882a593Smuzhiyun 
2250*4882a593Smuzhiyun static void
tablet_pad_leave(void * data,struct zwp_tablet_pad_v2 * zwp_tablet_pad_v2,uint32_t serial,struct wl_surface * surface)2251*4882a593Smuzhiyun tablet_pad_leave(void *data,
2252*4882a593Smuzhiyun                  struct zwp_tablet_pad_v2 *zwp_tablet_pad_v2,
2253*4882a593Smuzhiyun                  uint32_t serial,
2254*4882a593Smuzhiyun                  struct wl_surface *surface)
2255*4882a593Smuzhiyun {
2256*4882a593Smuzhiyun     /* pairs the pad with the tablet but also to set the focus. We
2257*4882a593Smuzhiyun      * don't care about the pairing and always use X's focus */
2258*4882a593Smuzhiyun }
2259*4882a593Smuzhiyun 
2260*4882a593Smuzhiyun static void
tablet_pad_removed(void * data,struct zwp_tablet_pad_v2 * zwp_tablet_pad_v2)2261*4882a593Smuzhiyun tablet_pad_removed(void *data,
2262*4882a593Smuzhiyun                    struct zwp_tablet_pad_v2 *zwp_tablet_pad_v2)
2263*4882a593Smuzhiyun {
2264*4882a593Smuzhiyun     struct xwl_tablet_pad *pad = data;
2265*4882a593Smuzhiyun     struct xwl_tablet_pad_group *g, *tg;
2266*4882a593Smuzhiyun 
2267*4882a593Smuzhiyun     xorg_list_for_each_entry_safe(g, tg, &pad->pad_group_list, link)
2268*4882a593Smuzhiyun         tablet_pad_group_destroy(g);
2269*4882a593Smuzhiyun 
2270*4882a593Smuzhiyun     RemoveDevice(pad->xdevice, TRUE);
2271*4882a593Smuzhiyun     xorg_list_del(&pad->link);
2272*4882a593Smuzhiyun     zwp_tablet_pad_v2_destroy(pad->pad);
2273*4882a593Smuzhiyun     free(pad);
2274*4882a593Smuzhiyun }
2275*4882a593Smuzhiyun 
2276*4882a593Smuzhiyun static const struct zwp_tablet_pad_v2_listener tablet_pad_listener = {
2277*4882a593Smuzhiyun     tablet_pad_group,
2278*4882a593Smuzhiyun     tablet_pad_path,
2279*4882a593Smuzhiyun     tablet_pad_buttons,
2280*4882a593Smuzhiyun     tablet_pad_done,
2281*4882a593Smuzhiyun     tablet_pad_button,
2282*4882a593Smuzhiyun     tablet_pad_enter,
2283*4882a593Smuzhiyun     tablet_pad_leave,
2284*4882a593Smuzhiyun     tablet_pad_removed,
2285*4882a593Smuzhiyun };
2286*4882a593Smuzhiyun 
2287*4882a593Smuzhiyun static void
tablet_seat_handle_add_tablet(void * data,struct zwp_tablet_seat_v2 * tablet_seat,struct zwp_tablet_v2 * tablet)2288*4882a593Smuzhiyun tablet_seat_handle_add_tablet(void *data, struct zwp_tablet_seat_v2 *tablet_seat,
2289*4882a593Smuzhiyun                               struct zwp_tablet_v2 *tablet)
2290*4882a593Smuzhiyun {
2291*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = data;
2292*4882a593Smuzhiyun     struct xwl_tablet *xwl_tablet;
2293*4882a593Smuzhiyun 
2294*4882a593Smuzhiyun     xwl_tablet = calloc(sizeof *xwl_tablet, 1);
2295*4882a593Smuzhiyun     if (xwl_tablet == NULL) {
2296*4882a593Smuzhiyun         ErrorF("%s ENOMEM\n", __func__);
2297*4882a593Smuzhiyun         return;
2298*4882a593Smuzhiyun     }
2299*4882a593Smuzhiyun 
2300*4882a593Smuzhiyun     xwl_tablet->tablet = tablet;
2301*4882a593Smuzhiyun     xwl_tablet->seat = xwl_seat;
2302*4882a593Smuzhiyun 
2303*4882a593Smuzhiyun     xorg_list_add(&xwl_tablet->link, &xwl_seat->tablets);
2304*4882a593Smuzhiyun 
2305*4882a593Smuzhiyun     zwp_tablet_v2_add_listener(tablet, &tablet_listener, xwl_tablet);
2306*4882a593Smuzhiyun }
2307*4882a593Smuzhiyun 
2308*4882a593Smuzhiyun static void
xwl_tablet_tool_update_cursor(struct xwl_cursor * xwl_cursor)2309*4882a593Smuzhiyun xwl_tablet_tool_update_cursor(struct xwl_cursor *xwl_cursor)
2310*4882a593Smuzhiyun {
2311*4882a593Smuzhiyun     struct xwl_tablet_tool *xwl_tablet_tool = wl_container_of(xwl_cursor,
2312*4882a593Smuzhiyun                                                               xwl_tablet_tool,
2313*4882a593Smuzhiyun                                                               cursor);
2314*4882a593Smuzhiyun     xwl_tablet_tool_set_cursor(xwl_tablet_tool);
2315*4882a593Smuzhiyun }
2316*4882a593Smuzhiyun 
2317*4882a593Smuzhiyun static void
tablet_seat_handle_add_tool(void * data,struct zwp_tablet_seat_v2 * tablet_seat,struct zwp_tablet_tool_v2 * tool)2318*4882a593Smuzhiyun tablet_seat_handle_add_tool(void *data, struct zwp_tablet_seat_v2 *tablet_seat,
2319*4882a593Smuzhiyun                             struct zwp_tablet_tool_v2 *tool)
2320*4882a593Smuzhiyun {
2321*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = data;
2322*4882a593Smuzhiyun     struct xwl_screen *xwl_screen = xwl_seat->xwl_screen;
2323*4882a593Smuzhiyun     struct xwl_tablet_tool *xwl_tablet_tool;
2324*4882a593Smuzhiyun 
2325*4882a593Smuzhiyun     xwl_tablet_tool = calloc(sizeof *xwl_tablet_tool, 1);
2326*4882a593Smuzhiyun     if (xwl_tablet_tool == NULL) {
2327*4882a593Smuzhiyun         ErrorF("%s ENOMEM\n", __func__);
2328*4882a593Smuzhiyun         return;
2329*4882a593Smuzhiyun     }
2330*4882a593Smuzhiyun 
2331*4882a593Smuzhiyun     xwl_tablet_tool->tool = tool;
2332*4882a593Smuzhiyun     xwl_tablet_tool->seat = xwl_seat;
2333*4882a593Smuzhiyun     xwl_cursor_init(&xwl_tablet_tool->cursor, xwl_screen,
2334*4882a593Smuzhiyun                     xwl_tablet_tool_update_cursor);
2335*4882a593Smuzhiyun 
2336*4882a593Smuzhiyun     xorg_list_add(&xwl_tablet_tool->link, &xwl_seat->tablet_tools);
2337*4882a593Smuzhiyun 
2338*4882a593Smuzhiyun     zwp_tablet_tool_v2_add_listener(tool, &tablet_tool_listener, xwl_tablet_tool);
2339*4882a593Smuzhiyun }
2340*4882a593Smuzhiyun 
2341*4882a593Smuzhiyun static void
tablet_seat_handle_add_pad(void * data,struct zwp_tablet_seat_v2 * tablet_seat,struct zwp_tablet_pad_v2 * pad)2342*4882a593Smuzhiyun tablet_seat_handle_add_pad(void *data, struct zwp_tablet_seat_v2 *tablet_seat,
2343*4882a593Smuzhiyun                            struct zwp_tablet_pad_v2 *pad)
2344*4882a593Smuzhiyun {
2345*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = data;
2346*4882a593Smuzhiyun     struct xwl_tablet_pad *xwl_tablet_pad;
2347*4882a593Smuzhiyun 
2348*4882a593Smuzhiyun     xwl_tablet_pad = calloc(sizeof *xwl_tablet_pad, 1);
2349*4882a593Smuzhiyun     if (xwl_tablet_pad == NULL) {
2350*4882a593Smuzhiyun         ErrorF("%s ENOMEM\n", __func__);
2351*4882a593Smuzhiyun         return;
2352*4882a593Smuzhiyun     }
2353*4882a593Smuzhiyun 
2354*4882a593Smuzhiyun     xwl_tablet_pad->pad = pad;
2355*4882a593Smuzhiyun     xwl_tablet_pad->seat = xwl_seat;
2356*4882a593Smuzhiyun     xorg_list_init(&xwl_tablet_pad->pad_group_list);
2357*4882a593Smuzhiyun 
2358*4882a593Smuzhiyun     xorg_list_add(&xwl_tablet_pad->link, &xwl_seat->tablet_pads);
2359*4882a593Smuzhiyun 
2360*4882a593Smuzhiyun     zwp_tablet_pad_v2_add_listener(pad, &tablet_pad_listener,
2361*4882a593Smuzhiyun                                    xwl_tablet_pad);
2362*4882a593Smuzhiyun }
2363*4882a593Smuzhiyun 
2364*4882a593Smuzhiyun static const struct zwp_tablet_seat_v2_listener tablet_seat_listener = {
2365*4882a593Smuzhiyun     tablet_seat_handle_add_tablet,
2366*4882a593Smuzhiyun     tablet_seat_handle_add_tool,
2367*4882a593Smuzhiyun     tablet_seat_handle_add_pad
2368*4882a593Smuzhiyun };
2369*4882a593Smuzhiyun 
2370*4882a593Smuzhiyun static void
init_tablet_manager_seat(struct xwl_screen * xwl_screen,struct xwl_seat * xwl_seat)2371*4882a593Smuzhiyun init_tablet_manager_seat(struct xwl_screen *xwl_screen,
2372*4882a593Smuzhiyun                          struct xwl_seat *xwl_seat)
2373*4882a593Smuzhiyun {
2374*4882a593Smuzhiyun     xorg_list_init(&xwl_seat->tablets);
2375*4882a593Smuzhiyun     xorg_list_init(&xwl_seat->tablet_tools);
2376*4882a593Smuzhiyun     xorg_list_init(&xwl_seat->tablet_pads);
2377*4882a593Smuzhiyun 
2378*4882a593Smuzhiyun     if (!xwl_screen->tablet_manager)
2379*4882a593Smuzhiyun         return;
2380*4882a593Smuzhiyun 
2381*4882a593Smuzhiyun     xwl_seat->tablet_seat =
2382*4882a593Smuzhiyun         zwp_tablet_manager_v2_get_tablet_seat(xwl_screen->tablet_manager,
2383*4882a593Smuzhiyun                                               xwl_seat->seat);
2384*4882a593Smuzhiyun 
2385*4882a593Smuzhiyun     zwp_tablet_seat_v2_add_listener(xwl_seat->tablet_seat, &tablet_seat_listener, xwl_seat);
2386*4882a593Smuzhiyun }
2387*4882a593Smuzhiyun 
2388*4882a593Smuzhiyun static void
release_tablet_manager_seat(struct xwl_seat * xwl_seat)2389*4882a593Smuzhiyun release_tablet_manager_seat(struct xwl_seat *xwl_seat)
2390*4882a593Smuzhiyun {
2391*4882a593Smuzhiyun     struct xwl_tablet *xwl_tablet, *next_xwl_tablet;
2392*4882a593Smuzhiyun     struct xwl_tablet_tool *xwl_tablet_tool, *next_xwl_tablet_tool;
2393*4882a593Smuzhiyun     struct xwl_tablet_pad *xwl_tablet_pad, *next_xwl_tablet_pad;
2394*4882a593Smuzhiyun 
2395*4882a593Smuzhiyun     xorg_list_for_each_entry_safe(xwl_tablet_pad, next_xwl_tablet_pad,
2396*4882a593Smuzhiyun                                   &xwl_seat->tablet_pads, link) {
2397*4882a593Smuzhiyun         xorg_list_del(&xwl_tablet_pad->link);
2398*4882a593Smuzhiyun         zwp_tablet_pad_v2_destroy(xwl_tablet_pad->pad);
2399*4882a593Smuzhiyun         free(xwl_tablet_pad);
2400*4882a593Smuzhiyun     }
2401*4882a593Smuzhiyun 
2402*4882a593Smuzhiyun     xorg_list_for_each_entry_safe(xwl_tablet_tool, next_xwl_tablet_tool,
2403*4882a593Smuzhiyun                                   &xwl_seat->tablet_tools, link) {
2404*4882a593Smuzhiyun         xorg_list_del(&xwl_tablet_tool->link);
2405*4882a593Smuzhiyun         zwp_tablet_tool_v2_destroy(xwl_tablet_tool->tool);
2406*4882a593Smuzhiyun         free(xwl_tablet_tool);
2407*4882a593Smuzhiyun     }
2408*4882a593Smuzhiyun 
2409*4882a593Smuzhiyun     xorg_list_for_each_entry_safe(xwl_tablet, next_xwl_tablet,
2410*4882a593Smuzhiyun                                   &xwl_seat->tablets, link) {
2411*4882a593Smuzhiyun         xorg_list_del(&xwl_tablet->link);
2412*4882a593Smuzhiyun         zwp_tablet_v2_destroy(xwl_tablet->tablet);
2413*4882a593Smuzhiyun         free(xwl_tablet);
2414*4882a593Smuzhiyun     }
2415*4882a593Smuzhiyun 
2416*4882a593Smuzhiyun     if (xwl_seat->tablet_seat) {
2417*4882a593Smuzhiyun         zwp_tablet_seat_v2_destroy(xwl_seat->tablet_seat);
2418*4882a593Smuzhiyun         xwl_seat->tablet_seat = NULL;
2419*4882a593Smuzhiyun     }
2420*4882a593Smuzhiyun }
2421*4882a593Smuzhiyun 
2422*4882a593Smuzhiyun static void
init_tablet_manager(struct xwl_screen * xwl_screen,uint32_t id,uint32_t version)2423*4882a593Smuzhiyun init_tablet_manager(struct xwl_screen *xwl_screen, uint32_t id, uint32_t version)
2424*4882a593Smuzhiyun {
2425*4882a593Smuzhiyun     struct xwl_seat *xwl_seat;
2426*4882a593Smuzhiyun 
2427*4882a593Smuzhiyun     xwl_screen->tablet_manager = wl_registry_bind(xwl_screen->registry,
2428*4882a593Smuzhiyun                                                   id,
2429*4882a593Smuzhiyun                                                   &zwp_tablet_manager_v2_interface,
2430*4882a593Smuzhiyun                                                   min(version,1));
2431*4882a593Smuzhiyun 
2432*4882a593Smuzhiyun     xorg_list_for_each_entry(xwl_seat, &xwl_screen->seat_list, link) {
2433*4882a593Smuzhiyun         init_tablet_manager_seat(xwl_screen, xwl_seat);
2434*4882a593Smuzhiyun     }
2435*4882a593Smuzhiyun }
2436*4882a593Smuzhiyun 
2437*4882a593Smuzhiyun void
xwl_screen_release_tablet_manager(struct xwl_screen * xwl_screen)2438*4882a593Smuzhiyun xwl_screen_release_tablet_manager(struct xwl_screen *xwl_screen)
2439*4882a593Smuzhiyun {
2440*4882a593Smuzhiyun     if (xwl_screen->tablet_manager) {
2441*4882a593Smuzhiyun         zwp_tablet_manager_v2_destroy(xwl_screen->tablet_manager);
2442*4882a593Smuzhiyun         xwl_screen->tablet_manager = NULL;
2443*4882a593Smuzhiyun     }
2444*4882a593Smuzhiyun }
2445*4882a593Smuzhiyun 
2446*4882a593Smuzhiyun static void
init_relative_pointer_manager(struct xwl_screen * xwl_screen,uint32_t id,uint32_t version)2447*4882a593Smuzhiyun init_relative_pointer_manager(struct xwl_screen *xwl_screen,
2448*4882a593Smuzhiyun                               uint32_t id, uint32_t version)
2449*4882a593Smuzhiyun {
2450*4882a593Smuzhiyun     xwl_screen->relative_pointer_manager =
2451*4882a593Smuzhiyun         wl_registry_bind(xwl_screen->registry, id,
2452*4882a593Smuzhiyun                          &zwp_relative_pointer_manager_v1_interface,
2453*4882a593Smuzhiyun                          1);
2454*4882a593Smuzhiyun }
2455*4882a593Smuzhiyun 
2456*4882a593Smuzhiyun static void
init_pointer_constraints(struct xwl_screen * xwl_screen,uint32_t id,uint32_t version)2457*4882a593Smuzhiyun init_pointer_constraints(struct xwl_screen *xwl_screen,
2458*4882a593Smuzhiyun                          uint32_t id, uint32_t version)
2459*4882a593Smuzhiyun {
2460*4882a593Smuzhiyun     xwl_screen->pointer_constraints =
2461*4882a593Smuzhiyun         wl_registry_bind(xwl_screen->registry, id,
2462*4882a593Smuzhiyun                          &zwp_pointer_constraints_v1_interface,
2463*4882a593Smuzhiyun                          1);
2464*4882a593Smuzhiyun }
2465*4882a593Smuzhiyun 
2466*4882a593Smuzhiyun static void
init_keyboard_grab(struct xwl_screen * xwl_screen,uint32_t id,uint32_t version)2467*4882a593Smuzhiyun init_keyboard_grab(struct xwl_screen *xwl_screen,
2468*4882a593Smuzhiyun                    uint32_t id, uint32_t version)
2469*4882a593Smuzhiyun {
2470*4882a593Smuzhiyun     struct xwl_seat *xwl_seat;
2471*4882a593Smuzhiyun     DeviceIntPtr master;
2472*4882a593Smuzhiyun 
2473*4882a593Smuzhiyun     xwl_screen->wp_grab =
2474*4882a593Smuzhiyun          wl_registry_bind(xwl_screen->registry, id,
2475*4882a593Smuzhiyun                           &zwp_xwayland_keyboard_grab_manager_v1_interface,
2476*4882a593Smuzhiyun                           1);
2477*4882a593Smuzhiyun 
2478*4882a593Smuzhiyun     xorg_list_for_each_entry(xwl_seat, &xwl_screen->seat_list, link) {
2479*4882a593Smuzhiyun         if (xwl_seat->keyboard) {
2480*4882a593Smuzhiyun             master = GetMaster(xwl_seat->keyboard, MASTER_KEYBOARD);
2481*4882a593Smuzhiyun             if (master)
2482*4882a593Smuzhiyun                 setup_keyboard_grab_handler(master);
2483*4882a593Smuzhiyun         }
2484*4882a593Smuzhiyun     }
2485*4882a593Smuzhiyun }
2486*4882a593Smuzhiyun 
2487*4882a593Smuzhiyun static void
input_handler(void * data,struct wl_registry * registry,uint32_t id,const char * interface,uint32_t version)2488*4882a593Smuzhiyun input_handler(void *data, struct wl_registry *registry, uint32_t id,
2489*4882a593Smuzhiyun               const char *interface, uint32_t version)
2490*4882a593Smuzhiyun {
2491*4882a593Smuzhiyun     struct xwl_screen *xwl_screen = data;
2492*4882a593Smuzhiyun 
2493*4882a593Smuzhiyun     if (strcmp(interface, "wl_seat") == 0 && version >= 3) {
2494*4882a593Smuzhiyun         create_input_device(xwl_screen, id, version);
2495*4882a593Smuzhiyun         xwl_screen->expecting_event++;
2496*4882a593Smuzhiyun     } else if (strcmp(interface, "zwp_relative_pointer_manager_v1") == 0) {
2497*4882a593Smuzhiyun         init_relative_pointer_manager(xwl_screen, id, version);
2498*4882a593Smuzhiyun     } else if (strcmp(interface, "zwp_pointer_constraints_v1") == 0) {
2499*4882a593Smuzhiyun         init_pointer_constraints(xwl_screen, id, version);
2500*4882a593Smuzhiyun     } else if (strcmp(interface, "zwp_tablet_manager_v2") == 0) {
2501*4882a593Smuzhiyun         init_tablet_manager(xwl_screen, id, version);
2502*4882a593Smuzhiyun     } else if (strcmp(interface, "zwp_xwayland_keyboard_grab_manager_v1") == 0) {
2503*4882a593Smuzhiyun         init_keyboard_grab(xwl_screen, id, version);
2504*4882a593Smuzhiyun     }
2505*4882a593Smuzhiyun }
2506*4882a593Smuzhiyun 
2507*4882a593Smuzhiyun static void
global_remove(void * data,struct wl_registry * registry,uint32_t name)2508*4882a593Smuzhiyun global_remove(void *data, struct wl_registry *registry, uint32_t name)
2509*4882a593Smuzhiyun {
2510*4882a593Smuzhiyun }
2511*4882a593Smuzhiyun 
2512*4882a593Smuzhiyun static const struct wl_registry_listener input_listener = {
2513*4882a593Smuzhiyun     input_handler,
2514*4882a593Smuzhiyun     global_remove,
2515*4882a593Smuzhiyun };
2516*4882a593Smuzhiyun 
2517*4882a593Smuzhiyun Bool
LegalModifier(unsigned int key,DeviceIntPtr pDev)2518*4882a593Smuzhiyun LegalModifier(unsigned int key, DeviceIntPtr pDev)
2519*4882a593Smuzhiyun {
2520*4882a593Smuzhiyun     return TRUE;
2521*4882a593Smuzhiyun }
2522*4882a593Smuzhiyun 
2523*4882a593Smuzhiyun void
ProcessInputEvents(void)2524*4882a593Smuzhiyun ProcessInputEvents(void)
2525*4882a593Smuzhiyun {
2526*4882a593Smuzhiyun     mieqProcessInputEvents();
2527*4882a593Smuzhiyun }
2528*4882a593Smuzhiyun 
2529*4882a593Smuzhiyun void
DDXRingBell(int volume,int pitch,int duration)2530*4882a593Smuzhiyun DDXRingBell(int volume, int pitch, int duration)
2531*4882a593Smuzhiyun {
2532*4882a593Smuzhiyun }
2533*4882a593Smuzhiyun 
2534*4882a593Smuzhiyun static Bool
sprite_check_lost_focus(SpritePtr sprite,WindowPtr window)2535*4882a593Smuzhiyun sprite_check_lost_focus(SpritePtr sprite, WindowPtr window)
2536*4882a593Smuzhiyun {
2537*4882a593Smuzhiyun     DeviceIntPtr device, master;
2538*4882a593Smuzhiyun     struct xwl_seat *xwl_seat;
2539*4882a593Smuzhiyun 
2540*4882a593Smuzhiyun     for (device = inputInfo.devices; device; device = device->next) {
2541*4882a593Smuzhiyun         /* Ignore non-wayland devices */
2542*4882a593Smuzhiyun         if (device->deviceProc == xwl_pointer_proc &&
2543*4882a593Smuzhiyun             device->spriteInfo->sprite == sprite)
2544*4882a593Smuzhiyun             break;
2545*4882a593Smuzhiyun     }
2546*4882a593Smuzhiyun 
2547*4882a593Smuzhiyun     if (!device)
2548*4882a593Smuzhiyun         return FALSE;
2549*4882a593Smuzhiyun 
2550*4882a593Smuzhiyun     xwl_seat = device->public.devicePrivate;
2551*4882a593Smuzhiyun     if (!xwl_seat)
2552*4882a593Smuzhiyun         return FALSE;
2553*4882a593Smuzhiyun 
2554*4882a593Smuzhiyun     master = GetMaster(device, POINTER_OR_FLOAT);
2555*4882a593Smuzhiyun     if (!master || !master->lastSlave)
2556*4882a593Smuzhiyun         return FALSE;
2557*4882a593Smuzhiyun 
2558*4882a593Smuzhiyun     /* We do want the last active slave, we only check on slave xwayland
2559*4882a593Smuzhiyun      * devices so we can find out the xwl_seat, but those don't actually own
2560*4882a593Smuzhiyun      * their sprite, so the match doesn't mean a lot.
2561*4882a593Smuzhiyun      */
2562*4882a593Smuzhiyun     if (master->lastSlave == xwl_seat->pointer &&
2563*4882a593Smuzhiyun         xwl_seat->focus_window == NULL &&
2564*4882a593Smuzhiyun         xwl_seat->last_xwindow != NullWindow &&
2565*4882a593Smuzhiyun         IsParent(xwl_seat->last_xwindow, window))
2566*4882a593Smuzhiyun         return TRUE;
2567*4882a593Smuzhiyun 
2568*4882a593Smuzhiyun     return FALSE;
2569*4882a593Smuzhiyun }
2570*4882a593Smuzhiyun 
2571*4882a593Smuzhiyun static WindowPtr
xwl_xy_to_window(ScreenPtr screen,SpritePtr sprite,int x,int y)2572*4882a593Smuzhiyun xwl_xy_to_window(ScreenPtr screen, SpritePtr sprite, int x, int y)
2573*4882a593Smuzhiyun {
2574*4882a593Smuzhiyun     struct xwl_screen *xwl_screen;
2575*4882a593Smuzhiyun     WindowPtr ret;
2576*4882a593Smuzhiyun 
2577*4882a593Smuzhiyun     xwl_screen = xwl_screen_get(screen);
2578*4882a593Smuzhiyun 
2579*4882a593Smuzhiyun     screen->XYToWindow = xwl_screen->XYToWindow;
2580*4882a593Smuzhiyun     ret = screen->XYToWindow(screen, sprite, x, y);
2581*4882a593Smuzhiyun     xwl_screen->XYToWindow = screen->XYToWindow;
2582*4882a593Smuzhiyun     screen->XYToWindow = xwl_xy_to_window;
2583*4882a593Smuzhiyun 
2584*4882a593Smuzhiyun     /* If the device controlling the sprite has left the Wayland surface but
2585*4882a593Smuzhiyun      * the DIX still finds the pointer within the X11 window, it means that
2586*4882a593Smuzhiyun      * the pointer has crossed to another native Wayland window, in this
2587*4882a593Smuzhiyun      * case, pretend we entered the root window so that a LeaveNotify
2588*4882a593Smuzhiyun      * event is emitted.
2589*4882a593Smuzhiyun      */
2590*4882a593Smuzhiyun     if (sprite_check_lost_focus(sprite, ret)) {
2591*4882a593Smuzhiyun         sprite->spriteTraceGood = 1;
2592*4882a593Smuzhiyun         return sprite->spriteTrace[0];
2593*4882a593Smuzhiyun     }
2594*4882a593Smuzhiyun 
2595*4882a593Smuzhiyun     return ret;
2596*4882a593Smuzhiyun }
2597*4882a593Smuzhiyun 
2598*4882a593Smuzhiyun void
xwl_seat_clear_touch(struct xwl_seat * xwl_seat,WindowPtr window)2599*4882a593Smuzhiyun xwl_seat_clear_touch(struct xwl_seat *xwl_seat, WindowPtr window)
2600*4882a593Smuzhiyun {
2601*4882a593Smuzhiyun     struct xwl_touch *xwl_touch, *next_xwl_touch;
2602*4882a593Smuzhiyun 
2603*4882a593Smuzhiyun     xorg_list_for_each_entry_safe(xwl_touch, next_xwl_touch,
2604*4882a593Smuzhiyun                                   &xwl_seat->touches, link_touch) {
2605*4882a593Smuzhiyun         if (xwl_touch->window->window == window) {
2606*4882a593Smuzhiyun             xorg_list_del(&xwl_touch->link_touch);
2607*4882a593Smuzhiyun             free(xwl_touch);
2608*4882a593Smuzhiyun         }
2609*4882a593Smuzhiyun     }
2610*4882a593Smuzhiyun }
2611*4882a593Smuzhiyun 
2612*4882a593Smuzhiyun static void
xwl_pointer_warp_emulator_set_fake_pos(struct xwl_pointer_warp_emulator * warp_emulator,int x,int y)2613*4882a593Smuzhiyun xwl_pointer_warp_emulator_set_fake_pos(struct xwl_pointer_warp_emulator *warp_emulator,
2614*4882a593Smuzhiyun                                        int x,
2615*4882a593Smuzhiyun                                        int y)
2616*4882a593Smuzhiyun {
2617*4882a593Smuzhiyun     struct zwp_locked_pointer_v1 *locked_pointer =
2618*4882a593Smuzhiyun         warp_emulator->locked_pointer;
2619*4882a593Smuzhiyun     WindowPtr window;
2620*4882a593Smuzhiyun     int sx, sy;
2621*4882a593Smuzhiyun 
2622*4882a593Smuzhiyun     if (!warp_emulator->locked_pointer)
2623*4882a593Smuzhiyun         return;
2624*4882a593Smuzhiyun 
2625*4882a593Smuzhiyun     if (!warp_emulator->xwl_seat->focus_window)
2626*4882a593Smuzhiyun         return;
2627*4882a593Smuzhiyun 
2628*4882a593Smuzhiyun     window = warp_emulator->xwl_seat->focus_window->window;
2629*4882a593Smuzhiyun     if (x >= window->drawable.x ||
2630*4882a593Smuzhiyun         y >= window->drawable.y ||
2631*4882a593Smuzhiyun         x < (window->drawable.x + window->drawable.width) ||
2632*4882a593Smuzhiyun         y < (window->drawable.y + window->drawable.height)) {
2633*4882a593Smuzhiyun         sx = x - window->drawable.x;
2634*4882a593Smuzhiyun         sy = y - window->drawable.y;
2635*4882a593Smuzhiyun         zwp_locked_pointer_v1_set_cursor_position_hint(locked_pointer,
2636*4882a593Smuzhiyun                                                        wl_fixed_from_int(sx),
2637*4882a593Smuzhiyun                                                        wl_fixed_from_int(sy));
2638*4882a593Smuzhiyun         wl_surface_commit(warp_emulator->xwl_seat->focus_window->surface);
2639*4882a593Smuzhiyun     }
2640*4882a593Smuzhiyun }
2641*4882a593Smuzhiyun 
2642*4882a593Smuzhiyun static Bool
xwl_pointer_warp_emulator_is_locked(struct xwl_pointer_warp_emulator * warp_emulator)2643*4882a593Smuzhiyun xwl_pointer_warp_emulator_is_locked(struct xwl_pointer_warp_emulator *warp_emulator)
2644*4882a593Smuzhiyun {
2645*4882a593Smuzhiyun     if (warp_emulator->locked_pointer)
2646*4882a593Smuzhiyun         return TRUE;
2647*4882a593Smuzhiyun     else
2648*4882a593Smuzhiyun         return FALSE;
2649*4882a593Smuzhiyun }
2650*4882a593Smuzhiyun 
2651*4882a593Smuzhiyun static void
xwl_pointer_warp_emulator_lock(struct xwl_pointer_warp_emulator * warp_emulator)2652*4882a593Smuzhiyun xwl_pointer_warp_emulator_lock(struct xwl_pointer_warp_emulator *warp_emulator)
2653*4882a593Smuzhiyun {
2654*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = warp_emulator->xwl_seat;
2655*4882a593Smuzhiyun     struct xwl_screen *xwl_screen = xwl_seat->xwl_screen;
2656*4882a593Smuzhiyun     struct zwp_pointer_constraints_v1 *pointer_constraints =
2657*4882a593Smuzhiyun         xwl_screen->pointer_constraints;
2658*4882a593Smuzhiyun     struct xwl_window *lock_window = xwl_seat->focus_window;
2659*4882a593Smuzhiyun 
2660*4882a593Smuzhiyun     warp_emulator->locked_window = lock_window;
2661*4882a593Smuzhiyun 
2662*4882a593Smuzhiyun     warp_emulator->locked_pointer =
2663*4882a593Smuzhiyun         zwp_pointer_constraints_v1_lock_pointer(pointer_constraints,
2664*4882a593Smuzhiyun                                                 lock_window->surface,
2665*4882a593Smuzhiyun                                                 xwl_seat->wl_pointer,
2666*4882a593Smuzhiyun                                                 NULL,
2667*4882a593Smuzhiyun                                                 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT);
2668*4882a593Smuzhiyun }
2669*4882a593Smuzhiyun 
2670*4882a593Smuzhiyun static void
xwl_pointer_warp_emulator_maybe_lock(struct xwl_pointer_warp_emulator * warp_emulator,struct xwl_window * xwl_window,SpritePtr sprite,int x,int y)2671*4882a593Smuzhiyun xwl_pointer_warp_emulator_maybe_lock(struct xwl_pointer_warp_emulator *warp_emulator,
2672*4882a593Smuzhiyun                                      struct xwl_window *xwl_window,
2673*4882a593Smuzhiyun                                      SpritePtr sprite,
2674*4882a593Smuzhiyun                                      int x, int y)
2675*4882a593Smuzhiyun {
2676*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = warp_emulator->xwl_seat;
2677*4882a593Smuzhiyun     GrabPtr pointer_grab = xwl_seat->pointer->deviceGrab.grab;
2678*4882a593Smuzhiyun 
2679*4882a593Smuzhiyun     if (warp_emulator->locked_pointer)
2680*4882a593Smuzhiyun         return;
2681*4882a593Smuzhiyun 
2682*4882a593Smuzhiyun     /*
2683*4882a593Smuzhiyun      * If there is no grab, and the window doesn't have pointer focus, ignore
2684*4882a593Smuzhiyun      * the warp, as under Wayland it won't receive input anyway.
2685*4882a593Smuzhiyun      */
2686*4882a593Smuzhiyun     if (!pointer_grab && xwl_seat->focus_window != xwl_window)
2687*4882a593Smuzhiyun         return;
2688*4882a593Smuzhiyun 
2689*4882a593Smuzhiyun     /*
2690*4882a593Smuzhiyun      * If there is a grab, but it's not an ownerEvents grab and the destination
2691*4882a593Smuzhiyun      * is not the pointer focus, ignore it, as events wouldn't be delivered
2692*4882a593Smuzhiyun      * there anyway.
2693*4882a593Smuzhiyun      */
2694*4882a593Smuzhiyun     if (pointer_grab &&
2695*4882a593Smuzhiyun         !pointer_grab->ownerEvents &&
2696*4882a593Smuzhiyun         sprite &&
2697*4882a593Smuzhiyun         XYToWindow(sprite, x, y) != xwl_seat->focus_window->window)
2698*4882a593Smuzhiyun         return;
2699*4882a593Smuzhiyun 
2700*4882a593Smuzhiyun     xwl_pointer_warp_emulator_lock(warp_emulator);
2701*4882a593Smuzhiyun }
2702*4882a593Smuzhiyun 
2703*4882a593Smuzhiyun static void
xwl_pointer_warp_emulator_warp(struct xwl_pointer_warp_emulator * warp_emulator,struct xwl_window * xwl_window,SpritePtr sprite,int x,int y)2704*4882a593Smuzhiyun xwl_pointer_warp_emulator_warp(struct xwl_pointer_warp_emulator *warp_emulator,
2705*4882a593Smuzhiyun                                struct xwl_window *xwl_window,
2706*4882a593Smuzhiyun                                SpritePtr sprite,
2707*4882a593Smuzhiyun                                int x, int y)
2708*4882a593Smuzhiyun {
2709*4882a593Smuzhiyun     xwl_pointer_warp_emulator_maybe_lock(warp_emulator,
2710*4882a593Smuzhiyun                                          xwl_window,
2711*4882a593Smuzhiyun                                          sprite,
2712*4882a593Smuzhiyun                                          x, y);
2713*4882a593Smuzhiyun     xwl_pointer_warp_emulator_set_fake_pos(warp_emulator, x, y);
2714*4882a593Smuzhiyun }
2715*4882a593Smuzhiyun 
2716*4882a593Smuzhiyun static void
xwl_pointer_warp_emulator_handle_motion(struct xwl_pointer_warp_emulator * warp_emulator,double dx,double dy,double dx_unaccel,double dy_unaccel)2717*4882a593Smuzhiyun xwl_pointer_warp_emulator_handle_motion(struct xwl_pointer_warp_emulator *warp_emulator,
2718*4882a593Smuzhiyun                                         double dx,
2719*4882a593Smuzhiyun                                         double dy,
2720*4882a593Smuzhiyun                                         double dx_unaccel,
2721*4882a593Smuzhiyun                                         double dy_unaccel)
2722*4882a593Smuzhiyun {
2723*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = warp_emulator->xwl_seat;
2724*4882a593Smuzhiyun     ValuatorMask mask;
2725*4882a593Smuzhiyun     WindowPtr window;
2726*4882a593Smuzhiyun     int x, y;
2727*4882a593Smuzhiyun 
2728*4882a593Smuzhiyun     valuator_mask_zero(&mask);
2729*4882a593Smuzhiyun     valuator_mask_set_unaccelerated(&mask, 0, dx, dx_unaccel);
2730*4882a593Smuzhiyun     valuator_mask_set_unaccelerated(&mask, 1, dy, dy_unaccel);
2731*4882a593Smuzhiyun 
2732*4882a593Smuzhiyun     QueuePointerEvents(xwl_seat->relative_pointer, MotionNotify, 0,
2733*4882a593Smuzhiyun                        POINTER_RELATIVE, &mask);
2734*4882a593Smuzhiyun 
2735*4882a593Smuzhiyun     window = xwl_seat->focus_window->window;
2736*4882a593Smuzhiyun     miPointerGetPosition(xwl_seat->pointer, &x, &y);
2737*4882a593Smuzhiyun 
2738*4882a593Smuzhiyun     if (xwl_pointer_warp_emulator_is_locked(warp_emulator) &&
2739*4882a593Smuzhiyun         xwl_seat->cursor_confinement_window != warp_emulator->locked_window &&
2740*4882a593Smuzhiyun         (x < window->drawable.x ||
2741*4882a593Smuzhiyun          y < window->drawable.y ||
2742*4882a593Smuzhiyun          x >= (window->drawable.x + window->drawable.width) ||
2743*4882a593Smuzhiyun          y >= (window->drawable.y + window->drawable.height)))
2744*4882a593Smuzhiyun         xwl_seat_destroy_pointer_warp_emulator(xwl_seat);
2745*4882a593Smuzhiyun     else
2746*4882a593Smuzhiyun         xwl_pointer_warp_emulator_set_fake_pos(warp_emulator, x, y);
2747*4882a593Smuzhiyun }
2748*4882a593Smuzhiyun 
2749*4882a593Smuzhiyun static struct xwl_pointer_warp_emulator *
xwl_pointer_warp_emulator_create(struct xwl_seat * xwl_seat)2750*4882a593Smuzhiyun xwl_pointer_warp_emulator_create(struct xwl_seat *xwl_seat)
2751*4882a593Smuzhiyun {
2752*4882a593Smuzhiyun     struct xwl_pointer_warp_emulator *warp_emulator;
2753*4882a593Smuzhiyun 
2754*4882a593Smuzhiyun     warp_emulator = calloc(1, sizeof *warp_emulator);
2755*4882a593Smuzhiyun     if (!warp_emulator) {
2756*4882a593Smuzhiyun         ErrorF("%s: ENOMEM\n", __func__);
2757*4882a593Smuzhiyun         return NULL;
2758*4882a593Smuzhiyun     }
2759*4882a593Smuzhiyun 
2760*4882a593Smuzhiyun     warp_emulator->xwl_seat = xwl_seat;
2761*4882a593Smuzhiyun 
2762*4882a593Smuzhiyun     return warp_emulator;
2763*4882a593Smuzhiyun }
2764*4882a593Smuzhiyun 
2765*4882a593Smuzhiyun static void
xwl_pointer_warp_emulator_destroy(struct xwl_pointer_warp_emulator * warp_emulator)2766*4882a593Smuzhiyun xwl_pointer_warp_emulator_destroy(struct xwl_pointer_warp_emulator *warp_emulator)
2767*4882a593Smuzhiyun {
2768*4882a593Smuzhiyun     if (warp_emulator->locked_pointer)
2769*4882a593Smuzhiyun         zwp_locked_pointer_v1_destroy(warp_emulator->locked_pointer);
2770*4882a593Smuzhiyun     free(warp_emulator);
2771*4882a593Smuzhiyun }
2772*4882a593Smuzhiyun 
2773*4882a593Smuzhiyun static void
xwl_seat_create_pointer_warp_emulator(struct xwl_seat * xwl_seat)2774*4882a593Smuzhiyun xwl_seat_create_pointer_warp_emulator(struct xwl_seat *xwl_seat)
2775*4882a593Smuzhiyun {
2776*4882a593Smuzhiyun     if (xwl_seat->confined_pointer)
2777*4882a593Smuzhiyun         xwl_seat_destroy_confined_pointer(xwl_seat);
2778*4882a593Smuzhiyun 
2779*4882a593Smuzhiyun     xwl_seat->pointer_warp_emulator =
2780*4882a593Smuzhiyun         xwl_pointer_warp_emulator_create(xwl_seat);
2781*4882a593Smuzhiyun }
2782*4882a593Smuzhiyun 
2783*4882a593Smuzhiyun static Bool
xwl_seat_can_emulate_pointer_warp(struct xwl_seat * xwl_seat)2784*4882a593Smuzhiyun xwl_seat_can_emulate_pointer_warp(struct xwl_seat *xwl_seat)
2785*4882a593Smuzhiyun {
2786*4882a593Smuzhiyun     struct xwl_screen *xwl_screen;
2787*4882a593Smuzhiyun 
2788*4882a593Smuzhiyun     if (!xwl_seat)
2789*4882a593Smuzhiyun         return FALSE;
2790*4882a593Smuzhiyun 
2791*4882a593Smuzhiyun     if (!xwl_seat->pointer)
2792*4882a593Smuzhiyun         return FALSE;
2793*4882a593Smuzhiyun 
2794*4882a593Smuzhiyun     xwl_screen = xwl_seat->xwl_screen;
2795*4882a593Smuzhiyun 
2796*4882a593Smuzhiyun     if (!xwl_screen->relative_pointer_manager)
2797*4882a593Smuzhiyun         return FALSE;
2798*4882a593Smuzhiyun 
2799*4882a593Smuzhiyun     if (!xwl_screen->pointer_constraints)
2800*4882a593Smuzhiyun         return FALSE;
2801*4882a593Smuzhiyun 
2802*4882a593Smuzhiyun     return TRUE;
2803*4882a593Smuzhiyun }
2804*4882a593Smuzhiyun 
2805*4882a593Smuzhiyun void
xwl_seat_emulate_pointer_warp(struct xwl_seat * xwl_seat,struct xwl_window * xwl_window,SpritePtr sprite,int x,int y)2806*4882a593Smuzhiyun xwl_seat_emulate_pointer_warp(struct xwl_seat *xwl_seat,
2807*4882a593Smuzhiyun                               struct xwl_window *xwl_window,
2808*4882a593Smuzhiyun                               SpritePtr sprite,
2809*4882a593Smuzhiyun                               int x, int y)
2810*4882a593Smuzhiyun {
2811*4882a593Smuzhiyun     if (!xwl_seat_can_emulate_pointer_warp(xwl_seat))
2812*4882a593Smuzhiyun         return;
2813*4882a593Smuzhiyun 
2814*4882a593Smuzhiyun     if (xwl_seat->x_cursor != NULL)
2815*4882a593Smuzhiyun         return;
2816*4882a593Smuzhiyun 
2817*4882a593Smuzhiyun     if (!xwl_seat->pointer_warp_emulator)
2818*4882a593Smuzhiyun         xwl_seat_create_pointer_warp_emulator(xwl_seat);
2819*4882a593Smuzhiyun 
2820*4882a593Smuzhiyun     if (!xwl_seat->pointer_warp_emulator)
2821*4882a593Smuzhiyun         return;
2822*4882a593Smuzhiyun 
2823*4882a593Smuzhiyun     xwl_pointer_warp_emulator_warp(xwl_seat->pointer_warp_emulator,
2824*4882a593Smuzhiyun                                    xwl_window,
2825*4882a593Smuzhiyun                                    sprite,
2826*4882a593Smuzhiyun                                    x, y);
2827*4882a593Smuzhiyun }
2828*4882a593Smuzhiyun 
2829*4882a593Smuzhiyun static Bool
xwl_seat_maybe_lock_on_hidden_cursor(struct xwl_seat * xwl_seat)2830*4882a593Smuzhiyun xwl_seat_maybe_lock_on_hidden_cursor(struct xwl_seat *xwl_seat)
2831*4882a593Smuzhiyun {
2832*4882a593Smuzhiyun     /* Some clients use hidden cursor+confineTo+relative motion
2833*4882a593Smuzhiyun      * to implement infinite panning (eg. 3D views), lock the
2834*4882a593Smuzhiyun      * pointer for so the relative pointer is used.
2835*4882a593Smuzhiyun      */
2836*4882a593Smuzhiyun     if (xwl_seat->x_cursor ||
2837*4882a593Smuzhiyun         !xwl_seat->cursor_confinement_window)
2838*4882a593Smuzhiyun         return FALSE;
2839*4882a593Smuzhiyun 
2840*4882a593Smuzhiyun     if (!xwl_seat->focus_window)
2841*4882a593Smuzhiyun         return FALSE;
2842*4882a593Smuzhiyun 
2843*4882a593Smuzhiyun     if (xwl_seat->confined_pointer)
2844*4882a593Smuzhiyun         xwl_seat_destroy_confined_pointer(xwl_seat);
2845*4882a593Smuzhiyun 
2846*4882a593Smuzhiyun     xwl_seat_create_pointer_warp_emulator(xwl_seat);
2847*4882a593Smuzhiyun     xwl_pointer_warp_emulator_lock(xwl_seat->pointer_warp_emulator);
2848*4882a593Smuzhiyun     return TRUE;
2849*4882a593Smuzhiyun }
2850*4882a593Smuzhiyun 
2851*4882a593Smuzhiyun void
xwl_seat_cursor_visibility_changed(struct xwl_seat * xwl_seat)2852*4882a593Smuzhiyun xwl_seat_cursor_visibility_changed(struct xwl_seat *xwl_seat)
2853*4882a593Smuzhiyun {
2854*4882a593Smuzhiyun     if (xwl_seat->pointer_warp_emulator && xwl_seat->x_cursor != NULL) {
2855*4882a593Smuzhiyun         xwl_seat_destroy_pointer_warp_emulator(xwl_seat);
2856*4882a593Smuzhiyun     } else if (!xwl_seat->x_cursor && xwl_seat->cursor_confinement_window) {
2857*4882a593Smuzhiyun         /* If the cursor goes hidden as is confined, lock it for
2858*4882a593Smuzhiyun          * relative motion to work. */
2859*4882a593Smuzhiyun         xwl_seat_maybe_lock_on_hidden_cursor(xwl_seat);
2860*4882a593Smuzhiyun     }
2861*4882a593Smuzhiyun }
2862*4882a593Smuzhiyun 
2863*4882a593Smuzhiyun void
xwl_seat_destroy_pointer_warp_emulator(struct xwl_seat * xwl_seat)2864*4882a593Smuzhiyun xwl_seat_destroy_pointer_warp_emulator(struct xwl_seat *xwl_seat)
2865*4882a593Smuzhiyun {
2866*4882a593Smuzhiyun     if (!xwl_seat->pointer_warp_emulator)
2867*4882a593Smuzhiyun         return;
2868*4882a593Smuzhiyun 
2869*4882a593Smuzhiyun     xwl_pointer_warp_emulator_destroy(xwl_seat->pointer_warp_emulator);
2870*4882a593Smuzhiyun     xwl_seat->pointer_warp_emulator = NULL;
2871*4882a593Smuzhiyun 
2872*4882a593Smuzhiyun     if (xwl_seat->cursor_confinement_window) {
2873*4882a593Smuzhiyun         xwl_seat_confine_pointer(xwl_seat,
2874*4882a593Smuzhiyun                                  xwl_seat->cursor_confinement_window);
2875*4882a593Smuzhiyun     }
2876*4882a593Smuzhiyun }
2877*4882a593Smuzhiyun 
2878*4882a593Smuzhiyun void
xwl_seat_confine_pointer(struct xwl_seat * xwl_seat,struct xwl_window * xwl_window)2879*4882a593Smuzhiyun xwl_seat_confine_pointer(struct xwl_seat *xwl_seat,
2880*4882a593Smuzhiyun                          struct xwl_window *xwl_window)
2881*4882a593Smuzhiyun {
2882*4882a593Smuzhiyun     struct zwp_pointer_constraints_v1 *pointer_constraints =
2883*4882a593Smuzhiyun         xwl_seat->xwl_screen->pointer_constraints;
2884*4882a593Smuzhiyun 
2885*4882a593Smuzhiyun     if (!pointer_constraints)
2886*4882a593Smuzhiyun         return;
2887*4882a593Smuzhiyun 
2888*4882a593Smuzhiyun     if (!xwl_seat->wl_pointer)
2889*4882a593Smuzhiyun         return;
2890*4882a593Smuzhiyun 
2891*4882a593Smuzhiyun     if (xwl_seat->cursor_confinement_window == xwl_window &&
2892*4882a593Smuzhiyun         xwl_seat->confined_pointer)
2893*4882a593Smuzhiyun         return;
2894*4882a593Smuzhiyun 
2895*4882a593Smuzhiyun     xwl_seat_unconfine_pointer(xwl_seat);
2896*4882a593Smuzhiyun 
2897*4882a593Smuzhiyun     xwl_seat->cursor_confinement_window = xwl_window;
2898*4882a593Smuzhiyun 
2899*4882a593Smuzhiyun     if (xwl_seat->pointer_warp_emulator)
2900*4882a593Smuzhiyun         return;
2901*4882a593Smuzhiyun 
2902*4882a593Smuzhiyun     if (xwl_seat_maybe_lock_on_hidden_cursor(xwl_seat))
2903*4882a593Smuzhiyun         return;
2904*4882a593Smuzhiyun 
2905*4882a593Smuzhiyun     xwl_seat->confined_pointer =
2906*4882a593Smuzhiyun         zwp_pointer_constraints_v1_confine_pointer(pointer_constraints,
2907*4882a593Smuzhiyun                                                    xwl_window->surface,
2908*4882a593Smuzhiyun                                                    xwl_seat->wl_pointer,
2909*4882a593Smuzhiyun                                                    NULL,
2910*4882a593Smuzhiyun                                                    ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT);
2911*4882a593Smuzhiyun }
2912*4882a593Smuzhiyun 
2913*4882a593Smuzhiyun static void
xwl_seat_destroy_confined_pointer(struct xwl_seat * xwl_seat)2914*4882a593Smuzhiyun xwl_seat_destroy_confined_pointer(struct xwl_seat *xwl_seat)
2915*4882a593Smuzhiyun {
2916*4882a593Smuzhiyun     zwp_confined_pointer_v1_destroy(xwl_seat->confined_pointer);
2917*4882a593Smuzhiyun     xwl_seat->confined_pointer = NULL;
2918*4882a593Smuzhiyun }
2919*4882a593Smuzhiyun 
2920*4882a593Smuzhiyun void
xwl_seat_unconfine_pointer(struct xwl_seat * xwl_seat)2921*4882a593Smuzhiyun xwl_seat_unconfine_pointer(struct xwl_seat *xwl_seat)
2922*4882a593Smuzhiyun {
2923*4882a593Smuzhiyun     xwl_seat->cursor_confinement_window = NULL;
2924*4882a593Smuzhiyun 
2925*4882a593Smuzhiyun     if (xwl_seat->confined_pointer)
2926*4882a593Smuzhiyun         xwl_seat_destroy_confined_pointer(xwl_seat);
2927*4882a593Smuzhiyun }
2928*4882a593Smuzhiyun 
2929*4882a593Smuzhiyun void
InitInput(int argc,char * argv[])2930*4882a593Smuzhiyun InitInput(int argc, char *argv[])
2931*4882a593Smuzhiyun {
2932*4882a593Smuzhiyun     ScreenPtr pScreen = screenInfo.screens[0];
2933*4882a593Smuzhiyun     struct xwl_screen *xwl_screen = xwl_screen_get(pScreen);
2934*4882a593Smuzhiyun 
2935*4882a593Smuzhiyun     if (!dixRegisterPrivateKey(&xwl_tablet_private_key, PRIVATE_DEVICE, 0)) {
2936*4882a593Smuzhiyun         ErrorF("Failed to register private key\n");
2937*4882a593Smuzhiyun         return;
2938*4882a593Smuzhiyun     }
2939*4882a593Smuzhiyun 
2940*4882a593Smuzhiyun     mieqInit();
2941*4882a593Smuzhiyun 
2942*4882a593Smuzhiyun     xwl_screen->input_registry = wl_display_get_registry(xwl_screen->display);
2943*4882a593Smuzhiyun     wl_registry_add_listener(xwl_screen->input_registry, &input_listener,
2944*4882a593Smuzhiyun                              xwl_screen);
2945*4882a593Smuzhiyun 
2946*4882a593Smuzhiyun     xwl_screen->XYToWindow = pScreen->XYToWindow;
2947*4882a593Smuzhiyun     pScreen->XYToWindow = xwl_xy_to_window;
2948*4882a593Smuzhiyun 
2949*4882a593Smuzhiyun     xwl_screen_roundtrip(xwl_screen);
2950*4882a593Smuzhiyun }
2951*4882a593Smuzhiyun 
2952*4882a593Smuzhiyun void
CloseInput(void)2953*4882a593Smuzhiyun CloseInput(void)
2954*4882a593Smuzhiyun {
2955*4882a593Smuzhiyun     mieqFini();
2956*4882a593Smuzhiyun }
2957