summaryrefslogtreecommitdiff
path: root/hw/xfree86/xwayland/xwayland-input.c
blob: ea63607c5c4f0a9a3463da4731c45e5108cdea65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
 * Copyright © 2008 Kristian Høgsberg
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of the
 * copyright holders not be used in advertising or publicity
 * pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied
 * warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

#ifdef HAVE_XORG_CONFIG_H
#include "xorg-config.h"
#endif

#include <stdint.h>
#include <unistd.h>
#include <linux/input.h>
#include <wayland-util.h>
#include <wayland-client.h>
#include <X11/extensions/compositeproto.h>
#include <xserver-properties.h>

#include <compositeext.h>
#include <selection.h>
#include <extinit.h>
#include <exevents.h>
#include <input.h>
#include <inputstr.h>
#include <exevents.h>
#include <xkbsrv.h>
#include <xf86Xinput.h>
#include <xf86Crtc.h>
#include <xf86str.h>
#include <windowstr.h>
#include <xf86Priv.h>
#include <mipointrst.h>

#include "xwayland.h"
#include "xwayland-private.h"

/*
 * TODO:
 *  - lose X kb focus when wayland surface loses it
 *  - active grabs, grab owner crack
 */

static void
xwl_pointer_control(DeviceIntPtr device, PtrCtrl *ctrl)
{
	/* Nothing to do, dix handles all settings */
}

static int
xwl_pointer_proc(DeviceIntPtr device, int what)
{
#define NBUTTONS 10
#define NAXES 2
    BYTE map[NBUTTONS + 1];
    int i = 0;
    Atom btn_labels[NBUTTONS] = {0};
    Atom axes_labels[NAXES] = {0};

    switch (what) {
    case DEVICE_INIT:
	device->public.on = FALSE;

        for (i = 1; i <= NBUTTONS; i++)
            map[i] = i;

        btn_labels[0] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_LEFT);
        btn_labels[1] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_MIDDLE);
        btn_labels[2] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_RIGHT);
        btn_labels[3] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_WHEEL_UP);
        btn_labels[4] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_WHEEL_DOWN);
        btn_labels[5] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_HWHEEL_LEFT);
        btn_labels[6] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_HWHEEL_RIGHT);
        /* don't know about the rest */

        axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_X);
        axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_Y);

        if (!InitValuatorClassDeviceStruct(device, 2, btn_labels,
                                           GetMotionHistorySize(), Absolute))
            return BadValue;

        /* Valuators */
        InitValuatorAxisStruct(device, 0, axes_labels[0],
                               0, 0xFFFF, 10000, 0, 10000, Absolute);
        InitValuatorAxisStruct(device, 1, axes_labels[1],
                               0, 0xFFFF, 10000, 0, 10000, Absolute);

        if (!InitPtrFeedbackClassDeviceStruct(device, xwl_pointer_control))
            return BadValue;

        if (!InitButtonClassDeviceStruct(device, 3, btn_labels, map))
            return BadValue;

    case DEVICE_ON:
	device->public.on = TRUE;
        return Success;

    case DEVICE_OFF:
    case DEVICE_CLOSE:
	device->public.on = FALSE;
        return Success;
    }

    return BadMatch;

#undef NBUTTONS
#undef NAXES
}

static void
xwl_keyboard_control(DeviceIntPtr device, KeybdCtrl *ctrl)
{
    /* FIXME: Set keyboard leds based on CAPSFLAG etc being set in
     * ctrl->leds */
}

static int
xwl_keyboard_proc(DeviceIntPtr device, int what)
{
    XkbRMLVOSet rmlvo;

    switch (what) {
    case DEVICE_INIT:
	device->public.on = FALSE;

        rmlvo.rules = "evdev";
        rmlvo.model = "evdev";
        rmlvo.layout = "us";
        rmlvo.variant = NULL;
        rmlvo.options = NULL;

        if (!InitKeyboardDeviceStruct(device, &rmlvo, NULL, xwl_keyboard_control))
            return BadValue;

        return Success;
    case DEVICE_ON:
	device->public.on = TRUE;
        return Success;

    case DEVICE_OFF:
    case DEVICE_CLOSE:
	device->public.on = FALSE;
        return Success;
    }

    return BadMatch;
}

static void
xwl_keyboard_uninit(InputDriverPtr drv, InputInfoPtr pInfo, int flags)
{
}

static int
xwl_keyboard_init(InputDriverPtr drv, InputInfoPtr pInfo, int flags)
{
    pInfo->type_name = "xwayland-keyboard";
    pInfo->device_control = xwl_keyboard_proc;
    pInfo->read_input = NULL;
    pInfo->control_proc = NULL;
    pInfo->switch_mode = NULL;
    pInfo->fd = -1;

    return Success;
}

_X_EXPORT InputDriverRec xwl_keyboard_driver = {
    1,
    "xwayland-keyboard",
    NULL,
    xwl_keyboard_init,
    xwl_keyboard_uninit,
    NULL,
};

static void
xwl_pointer_uninit(InputDriverPtr drv, InputInfoPtr pInfo, int flags)
{
}

static int
xwl_pointer_init(InputDriverPtr drv, InputInfoPtr pInfo, int flags)
{
    pInfo->type_name = "xwayland-pointer";
    pInfo->device_control = xwl_pointer_proc;
    pInfo->read_input = NULL;
    pInfo->control_proc = NULL;
    pInfo->switch_mode = NULL;
    pInfo->fd = -1;

    return Success;
}

_X_EXPORT InputDriverRec xwl_pointer_driver = {
    1,
    "xwayland-pointer",
    NULL,
    xwl_pointer_init,
    xwl_pointer_uninit,
    NULL,
};

void
xwl_input_teardown(pointer p)
{
}

pointer
xwl_input_setup(pointer module, pointer opts, int *errmaj, int *errmin)
{
    xf86AddInputDriver(&xwl_keyboard_driver, module, 0);
    xf86AddInputDriver(&xwl_pointer_driver, module, 0);

    return module;
}

static DeviceIntPtr
device_added(struct xwl_input_device *xwl_input_device, const char *driver)
{
    DeviceIntPtr dev = NULL;
    InputInfoPtr pInfo;
    int rc;

    pInfo = xf86AllocateInput();
    if (!pInfo)
        return NULL;

    pInfo->driver = xstrdup(driver);

    if (asprintf(&pInfo->name, "%s:%d",
		 pInfo->driver, xwl_input_device->id) == -1) {
	free(pInfo);
	return NULL;
    }

    pInfo->private = xwl_input_device;

    rc = xf86NewInputDevice(pInfo, &dev, 1);
    if (rc != Success) {
	free(pInfo);
	return NULL;
    }

    LogMessage(X_INFO, "config/xwayland: Adding input device %s\n",
	       pInfo->name);

    return dev;
}

static void
input_device_handle_motion(void *data, struct wl_input_device *input_device,
			   uint32_t time,
			   int32_t x, int32_t y, int32_t sx, int32_t sy)
{
    struct xwl_input_device *xwl_input_device = data;
    struct xwl_screen *xwl_screen = xwl_input_device->xwl_screen;
    int32_t dx, dy, lx, ly;

    xwl_input_device->time = time;

    if (!xwl_input_device->focus_window)
	return ;

    dx = xwl_input_device->focus_window->window->drawable.x;
    dy = xwl_input_device->focus_window->window->drawable.y;

    lx = xf86ScaleAxis(sx + dx, 0xFFFF, 0, xwl_screen->scrninfo->virtualX, 0);
    ly = xf86ScaleAxis(sy + dy, 0xFFFF, 0, xwl_screen->scrninfo->virtualY, 0);

    xf86PostMotionEvent(xwl_input_device->pointer,
			TRUE, 0, 2, lx, ly);
}

static void
input_device_handle_button(void *data, struct wl_input_device *input_device,
			   uint32_t time, uint32_t button, uint32_t state)
{
    struct xwl_input_device *xwl_input_device = data;
    int index;

    xwl_input_device->time = time;

    switch (button) {
    case BTN_MIDDLE:
	index = 2;
	break;
    case BTN_RIGHT:
	index = 3;
	break;
    default:
	index = button - BTN_LEFT + 1;
	break;
    }

    xf86PostButtonEvent(xwl_input_device->pointer,
			TRUE, index, state, 0, 0);
}

static void
input_device_handle_key(void *data, struct wl_input_device *input_device,
			uint32_t time, uint32_t key, uint32_t state)
{
    struct xwl_input_device *xwl_input_device = data;
    uint32_t modifier;

    xwl_input_device->time = time;

    switch (key) {
    case KEY_LEFTMETA:
    case KEY_RIGHTMETA:
	modifier = MODIFIER_META;
	break;
    default:
	modifier = 0;
	break;
    }

    if (state)
	xwl_input_device->modifiers |= modifier;
    else
	xwl_input_device->modifiers &= ~modifier;

    xf86PostKeyboardEvent(xwl_input_device->keyboard, key + 8, state);
}

static void
input_device_handle_pointer_enter(void *data,
				  struct wl_input_device *input_device,
				  uint32_t time,
				  struct wl_surface *surface,
				  int32_t x, int32_t y, int32_t sx, int32_t sy)

{
    struct xwl_input_device *xwl_input_device = data;

    xwl_input_device->time = time;

    if (surface)
	xwl_input_device->focus_window = wl_surface_get_user_data(surface);
    else
	xwl_input_device->focus_window = NULL;

    if (xwl_input_device->focus_window)
	SetDeviceRedirectWindow(xwl_input_device->pointer,
				xwl_input_device->focus_window->window);
    else
	SetDeviceRedirectWindow(xwl_input_device->pointer,
				PointerRootWin);
}

static void
input_device_handle_keyboard_enter(void *data,
				   struct wl_input_device *input_device,
				   uint32_t time,
				   struct wl_surface *surface,
				   struct wl_array *keys)
{
    struct xwl_input_device *xwl_input_device = data;
    uint32_t *k, *end;

    xwl_input_device->time = time;

    xwl_input_device->modifiers = 0;
    end = (uint32_t *) ((char *) keys->data + keys->size);
    for (k = keys->data; k < end; k++) {
	switch (*k) {
	case KEY_LEFTMETA:
	case KEY_RIGHTMETA:
	    xwl_input_device->modifiers |= MODIFIER_META;
	    break;
	}
    }
}

static void
input_device_handle_axis(void *data, struct wl_input_device *input_device,
                         uint32_t time, uint32_t axis, int32_t value)
{
}

static void
input_device_handle_pointer_leave(void *data,
                                  struct wl_input_device *input_device,
                                  uint32_t time, struct wl_surface *surface)
{
}

static void
input_device_handle_keyboard_leave(void *data,
                                   struct wl_input_device *input_device,
                                   uint32_t time,
                                   struct wl_surface *surface)
{
}

static void
input_device_handle_touch_down(void *data,
                               struct wl_input_device *wl_input_device,
                               uint32_t time, struct wl_surface *surface,
                               int32_t id, int32_t x, int32_t y)
{
}

static void
input_device_handle_touch_up(void *data,
                             struct wl_input_device *wl_input_device,
                             uint32_t time, int32_t id)
{
}

static void
input_device_handle_touch_motion(void *data,
                                 struct wl_input_device *wl_input_device,
                                 uint32_t time,
                                 int32_t id, int32_t x, int32_t y)
{
}

static void
input_device_handle_touch_frame(void *data,
				struct wl_input_device *wl_input_device)
{
}

static void
input_device_handle_touch_cancel(void *data,
				 struct wl_input_device *wl_input_device)
{
}

static const struct wl_input_device_listener input_device_listener = {
    input_device_handle_motion,
    input_device_handle_button,
    input_device_handle_axis,
    input_device_handle_key,
    input_device_handle_pointer_enter,
    input_device_handle_pointer_leave,
    input_device_handle_keyboard_enter,
    input_device_handle_keyboard_leave,
    input_device_handle_touch_down,
    input_device_handle_touch_up,
    input_device_handle_touch_motion,
    input_device_handle_touch_frame,
    input_device_handle_touch_cancel,
};

static void
create_input_device(struct xwl_screen *xwl_screen, uint32_t id,
		    uint32_t version)
{
    struct xwl_input_device *xwl_input_device;

    xwl_input_device = calloc(sizeof *xwl_input_device, 1);
    if (xwl_input_device == NULL) {
	ErrorF("create_input ENOMEM");
	return ;
    }

    xwl_input_device->xwl_screen = xwl_screen;
    xorg_list_add(&xwl_input_device->link, &xwl_screen->input_device_list);

    xwl_input_device->pointer =
	device_added(xwl_input_device, "xwayland-pointer");
    xwl_input_device->keyboard =
	device_added(xwl_input_device, "xwayland-keyboard");

    xwl_input_device->input_device =
        wl_display_bind(xwl_screen->display, id, &wl_input_device_interface);
    xwl_input_device->id = id;

    wl_input_device_add_listener(xwl_input_device->input_device,
				 &input_device_listener,
				 xwl_input_device);
}

static void
input_handler(struct wl_display *display,
	      uint32_t id,
	      const char *interface,
	      uint32_t version,
	      void *data)
{
    struct xwl_screen *xwl_screen = data;

    if (strcmp (interface, "wl_input_device") == 0) {
        create_input_device(xwl_screen, id, 1);
    }
}

void
xwl_input_init(struct xwl_screen *xwl_screen)
{
    xwl_screen->input_listener =
	wl_display_add_global_listener(xwl_screen->display,
				       input_handler, xwl_screen);
}