summaryrefslogtreecommitdiff
path: root/src/wayland.c
blob: e53b06a0f4671780c1e6b2265bb2e20ba2c973e4 (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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/*
 * 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_CONFIG_H
#include "config.h"
#endif

#include <linux/input.h>

#include <extinit.h>
#include <input.h>
#include <inputstr.h>
#include <exevents.h>
#include <xkbsrv.h>
#include <xf86Xinput.h>
#include "i830.h"
#include <wayland-client.h>
#include <X11/extensions/compositeproto.h>
#include <compositeext.h>

static int wayland_window_private_key_index;
static DevPrivateKey wayland_window_private_key = &wayland_window_private_key_index;

struct xwl_window_private {
	struct xwl_context *context;
	struct wl_surface *surface;
	struct wl_visual *visual;
	WindowPtr window;
};

struct xwl_context {
	ScrnInfoPtr pScrn;
	int fd;
	struct wl_display *display;
	struct wl_compositor *compositor;
	struct wl_surface *surface;
	uint32_t mask;
	int rootless;

	/* FIXME: Hack. */
	int32_t width, height;

	DeviceIntPtr pointer;
	DeviceIntPtr keyboard;

	RealizeWindowProcPtr	RealizeWindow;
	UnrealizeWindowProcPtr	UnrealizeWindow;
};

struct xwl_output {
	struct wl_output *output;
	struct xwl_context *context;
	struct wl_surface *surface;
	int32_t x, y, width, height;
	xf86Monitor monitor;	
};

/* OMG the horror of the xf86 input wrapper... Fortunately, we don't
 * have to use that. */

static int
wayland_input_proc(DeviceIntPtr device, int event)
{
	switch (event) {
	case DEVICE_INIT:
		break;
	case DEVICE_ON:
		break;
	case DEVICE_OFF:
		break;
	case DEVICE_CLOSE:
		break;
	}

	return Success;
}

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

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

static int
wayland_input_init_pointer(struct xwl_context *context)
{
	DeviceIntPtr device;
	int min_x = 0, min_y = 0, max_x = context->width, max_y = context->height;
	static unsigned char map[] = { 1, 2, 3 };
	CARD32 atom;
	char *name = "wayland";
	Atom labels[3];

        device = AddInputDevice(serverClient, wayland_input_proc, TRUE);
	context->pointer = device;

        atom = MakeAtom(name, strlen(name), TRUE);
        AssignTypeAndName(device, atom, name);

        device->coreEvents = TRUE;
        device->type = SLAVE;
        device->spriteInfo->spriteOwner = FALSE;

	labels[0] = MakeAtom("x", 1, TRUE);
	labels[1] = MakeAtom("y", 1, TRUE);

	if (!InitValuatorClassDeviceStruct(device, 2, labels,
					   GetMotionHistorySize(), Absolute))
		return !Success;

	/* Valuators */
	InitValuatorAxisStruct(device, labels[0], 0, min_x, max_x, 10000, 0, 10000);
	InitValuatorAxisStruct(device, labels[1], 1, min_y, max_y, 10000, 0, 10000);

	if (!InitPtrFeedbackClassDeviceStruct(device, wayland_input_ptr_ctrl_proc))
		return !Success;

	/* FIXME: count number of actual buttons */
	labels[0] = MakeAtom("left", 4, TRUE);
	labels[1] = MakeAtom("middle", 6, TRUE);
	labels[2] = MakeAtom("right", 5, TRUE);
	if (!InitButtonClassDeviceStruct(device, 3, labels, map))
		return !Success;

	return Success;
}

static int
wayland_input_init_keyboard(struct xwl_context *context)
{
	DeviceIntPtr device;
	CARD32 atom;
	char *name = "wayland";
	KeySymsRec key_syms;
	XkbRMLVOSet rmlvo;

        device = AddInputDevice(serverClient, wayland_input_proc, TRUE);
	context->keyboard = device;

        atom = MakeAtom(name, strlen(name), TRUE);
        AssignTypeAndName(device, atom, name);

        device->coreEvents = TRUE;
        device->type = SLAVE;
        device->spriteInfo->spriteOwner = FALSE;

	key_syms.map        = malloc(4 * 248 * sizeof(KeySym));
	key_syms.mapWidth   = 4;
	key_syms.minKeyCode = 8;
	key_syms.maxKeyCode = 254;

	rmlvo.rules = "evdev";
	rmlvo.model = "evdev";
	rmlvo.layout = "us";
	rmlvo.variant = NULL;
	rmlvo.options = NULL;
        if (!InitKeyboardDeviceStruct(device, &rmlvo,
				      NULL, wayland_input_kbd_ctrl))
            return !Success;

        RegisterOtherDevice(device);
	ActivateDevice(device, FALSE);

	return Success;
}

static int
source_update(uint32_t mask, void *data)
{
	struct xwl_context *context = data;

	context->mask = mask;

	return 0;
}

static void
wakeup_handler(pointer data, int err, pointer read_mask)
{
	struct xwl_context *context = data;

	if (err >= 0 && FD_ISSET(context->fd, (fd_set *) read_mask)) {
		wl_display_iterate(context->display,
				   WL_DISPLAY_READABLE);
	}
}

static void
block_handler(pointer data, struct timeval **tv, pointer read_mask)
{
	struct xwl_context *context = data;

	/* The X servers "main loop" doesn't let us select for
	 * writable, so let's just do a blocking write here. */

	while (context->mask & WL_DISPLAY_WRITABLE)
		wl_display_iterate(context->display,
				   WL_DISPLAY_WRITABLE);
}

static void
handle_motion(void *data, struct wl_input_device *input_device,
	      int32_t x, int32_t y, int32_t sx, int32_t sy)
{
	struct xwl_context *context = data;
	
	xf86PostMotionEvent(context->pointer, TRUE, 0, 2, x, y);
}

static void
handle_button(void *data, struct wl_input_device *input_device,
	      uint32_t button, uint32_t state,
	      int32_t x, int32_t y, int32_t sx, int32_t sy)
{
	struct xwl_context *context = data;
	int index;

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

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

static void
handle_key(void *data, struct wl_input_device *input_device,
	   uint32_t key, uint32_t state)
{
	struct xwl_context *context = data;

	xf86PostKeyboardEvent(context->pointer, key + 8, state);
}

static void
handle_pointer_focus(void *data,
		     struct wl_input_device *input_device,
		     struct wl_surface *surface)
{
}

static void
handle_keyboard_focus(void *data,
		      struct wl_input_device *input_device,
		      struct wl_surface *surface,
		      struct wl_array *keys)
{
	/* FIXME: Restore X modifiers here. */
}

static const struct wl_input_device_listener input_device_listener = {
	handle_motion,
	handle_button,
	handle_key,
	handle_pointer_focus,
	handle_keyboard_focus,
};

static void
display_handle_geometry(void *data,
			struct wl_output *output,
			int32_t width, int32_t height)
{
	struct xwl_output *xwl_output = data;

	xwl_output->x = 0;
	xwl_output->y = 0;
	xwl_output->width = width;
	xwl_output->height = height;

	xwl_output->context->width = width;
	xwl_output->context->height = height;
}

static const struct wl_output_listener output_listener = {
	display_handle_geometry,
};

static void
crtc_dpms(xf86CrtcPtr drmmode_crtc, int mode)
{
}

static Bool
crtc_set_mode_major(xf86CrtcPtr crtc, DisplayModePtr mode,
		    Rotation rotation, int x, int y)
{
	ScrnInfoPtr pScrn = crtc->scrn;
	I830Ptr pI830 = I830PTR(pScrn);
	struct xwl_context *context = pI830->wayland_data;
	uint32_t name;
	struct wl_visual *visual;

	if (context->rootless)
		return TRUE;

	visual = wl_display_get_rgb_visual(context->display);

	if (dri_bo_flink(pI830->front_buffer->bo, &name) != 0)
		/* failed to name buffer */
		return FALSE;

	wl_surface_attach(context->surface, name,
			  mode->HDisplay, mode->VDisplay,
			  pScrn->displayWidth * pI830->cpp, visual);
	wl_surface_map(context->surface, 0, 0,
		       mode->HDisplay, mode->VDisplay);
	wl_compositor_commit(context->compositor, 0);

	return TRUE;
}

static void
crtc_set_cursor_colors (xf86CrtcPtr crtc, int bg, int fg)
{
}

static void
crtc_set_cursor_position (xf86CrtcPtr crtc, int x, int y)
{
}

static void
crtc_show_cursor (xf86CrtcPtr crtc)
{
}

static void
crtc_hide_cursor (xf86CrtcPtr crtc)
{
}

static void
crtc_load_cursor_argb (xf86CrtcPtr crtc, CARD32 *image)
{
}

static PixmapPtr
crtc_shadow_create(xf86CrtcPtr crtc, void *data, int width, int height)
{
	return NULL;
}

static void *
crtc_shadow_allocate(xf86CrtcPtr crtc, int width, int height)
{
	return NULL;
}

static void
crtc_shadow_destroy(xf86CrtcPtr crtc, PixmapPtr rotate_pixmap, void *data)
{
}

static const xf86CrtcFuncsRec crtc_funcs = {
	.dpms                = crtc_dpms,
	.set_mode_major      = crtc_set_mode_major,
	.set_cursor_colors   = crtc_set_cursor_colors,
	.set_cursor_position = crtc_set_cursor_position,
	.show_cursor         = crtc_show_cursor,
	.hide_cursor         = crtc_hide_cursor,
	.load_cursor_argb    = crtc_load_cursor_argb,
	.shadow_create       = crtc_shadow_create,
	.shadow_allocate     = crtc_shadow_allocate,
	.shadow_destroy      = crtc_shadow_destroy,
	.destroy = NULL, /* XXX */
};

static void
output_dpms(xf86OutputPtr output, int mode)
{
	return;
}

static xf86OutputStatus
output_detect(xf86OutputPtr output)
{
	return XF86OutputStatusConnected;
}

static Bool
output_mode_valid(xf86OutputPtr output, DisplayModePtr pModes)
{
	return MODE_OK;
}

static DisplayModePtr
output_get_modes(xf86OutputPtr xf86output)
{
	struct xwl_output *output = xf86output->driver_private;
	struct monitor_ranges *ranges;
	DisplayModePtr modes;

	modes = xf86CVTMode(output->width, output->height, 60, TRUE, FALSE);
	output->monitor.det_mon[0].type = DS_RANGES;
	ranges = &output->monitor.det_mon[0].section.ranges;
	ranges->min_h = modes->HSync - 10;
	ranges->max_h = modes->HSync + 10;
	ranges->min_v = modes->VRefresh - 10;
	ranges->max_v = modes->VRefresh + 10;
	ranges->max_clock = modes->Clock + 100;
	output->monitor.det_mon[1].type = DT;
	output->monitor.det_mon[2].type = DT;
	output->monitor.det_mon[3].type = DT;

	xf86output->MonInfo = &output->monitor;

	return modes;
}

static void
output_destroy(xf86OutputPtr xf86output)
{
	struct xwl_output *output = xf86output->driver_private;

	free(output);
}

static const xf86OutputFuncsRec output_funcs = {
	.dpms	    = output_dpms,
	.detect	    = output_detect,
	.mode_valid = output_mode_valid,
	.get_modes  = output_get_modes,
	.destroy    = output_destroy
};

static void
create_output(struct xwl_context *context, struct wl_object *object)
{
	struct xwl_output *output;
	xf86OutputPtr xf86output;
	xf86CrtcPtr xf86crtc;

	output = malloc(sizeof *output);
	output->context = context;
	output->output = (struct wl_output *) object;

	output->surface = wl_compositor_create_surface(context->compositor);
	if (output->surface == NULL) {
		ErrorF("wl_display_create_surface failed\n");
		return;
	}
	wl_output_add_listener(output->output, &output_listener, output);
	context->surface = output->surface;

	xf86output = xf86OutputCreate(context->pScrn, &output_funcs, "WAYLAND-1");
	xf86output->driver_private = output;
	xf86output->mm_width = 300;
	xf86output->mm_height = 240;
	xf86output->subpixel_order = SubPixelHorizontalRGB;
	xf86output->possible_crtcs = 1;
	xf86output->possible_clones = 1;

	xf86crtc = xf86CrtcCreate(context->pScrn, &crtc_funcs);
	xf86crtc->driver_private = output;

	return;

}

static void
global_handler(struct wl_display *display,
	       struct wl_object *object, void *data)
{
	struct xwl_context *context = data;

	if (wl_object_implements(object, "compositor", 1)) { 
		context->compositor = (struct wl_compositor *) object;
	} else if (wl_object_implements(object, "output", 1)) {
		create_output(context, object);
	} else if (wl_object_implements(object, "input_device", 1)) {
		struct wl_input_device *device =
			(struct wl_input_device *) object;
		wl_input_device_add_listener(device,
					     &input_device_listener, context);
	}
}

static Bool
resize (ScrnInfoPtr scrn, int width, int height)
{
	if (scrn->virtualX == width && scrn->virtualY == height)
		return TRUE;

	scrn->virtualX = width;
	scrn->virtualY = height;

	return TRUE;
}

static const xf86CrtcConfigFuncsRec config_funcs = {
	resize
};

static const char socket_name[] = "\0wayland";

static Bool
wayland_realize_window (WindowPtr pWin)
{
	ScreenPtr pScreen = pWin->drawable.pScreen;
	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
	I830Ptr pI830 = I830PTR(pScrn);
	struct xwl_context *context = pI830->wayland_data;
	Bool ret = TRUE;
	PixmapPtr pPixmap;
	struct xwl_window_private *priv;
	drm_intel_bo *bo;
	uint32_t name;

	pScreen->RealizeWindow = context->RealizeWindow;

	if (!(*pScreen->RealizeWindow) (pWin))
		ret = FALSE;

	context->RealizeWindow = pScreen->RealizeWindow;
	pScreen->RealizeWindow = wayland_realize_window;

	pPixmap = (*pScreen->GetWindowPixmap)(pWin);

	if (!context->rootless)
		return;

	/* Redirect child windows of root window.  We're better off
	 * doing this at init time, but need to find the right time
	 * after the root window has been created. */
	if (pWin->parent == NULL) {
		CompositeRedirectSubwindows(pWin, CompositeRedirectManual);
		return ret;
	}

	if (dixLookupPrivate(&pWin->devPrivates, wayland_window_private_key)) {
		ErrorF("already realized window %p\n", pWin);
		return ret;
	}

	/* FIXME: This doesn't deal with reparenting.  Reparenting a
	 * toplevel window into another will have to destroy a wayland
	 * surface. */

	if ((*pScreen->GetWindowPixmap)(pWin->parent) == pPixmap)
		return ret;

	/* If not the root window, create a wayland surface using the
	 * backing pixmap. */

	priv = malloc(sizeof *priv);
	priv->context = context;
	priv->surface = wl_compositor_create_surface(context->compositor);
	if (priv->surface == NULL) {
		ErrorF("wl_display_create_surface failed\n");
		return ret;
	}
	priv->window = pWin;
	priv->visual = wl_display_get_rgb_visual(context->display);

	bo = i830_get_pixmap_bo (pPixmap);
	if (drm_intel_bo_flink(bo, &name) != 0) {
		ErrorF("failed to name buffer\n");
	}

	wl_surface_attach(priv->surface, name,
			  pPixmap->drawable.width,
			  pPixmap->drawable.height,
			  pPixmap->devKind, priv->visual);
	wl_surface_map(priv->surface, 50, 50,
		       pPixmap->drawable.width,
		       pPixmap->drawable.height);
	wl_compositor_commit(context->compositor, 0);

	/* FIXME: Set private in CreateWindow, probably. */
	dixSetPrivate(&pWin->devPrivates, wayland_window_private_key, priv);

	ErrorF("created wayland surface for window %p, pixmap %p, %dx%d, stride %d, name %d\n",
	       pWin, pPixmap, pPixmap->drawable.width, pPixmap->drawable.height,
	       pPixmap->devKind, name);

	return ret;
}

static Bool
wayland_unrealize_window (WindowPtr pWin)
{
	ScreenPtr pScreen = pWin->drawable.pScreen;
	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
	I830Ptr pI830 = I830PTR(pScrn);
	struct xwl_context *context = pI830->wayland_data;
	struct xwl_window_private *priv;
	Bool ret = TRUE;

	pScreen->UnrealizeWindow = context->UnrealizeWindow;
	if (!(*pScreen->UnrealizeWindow)(pWin))
		ret = FALSE;

	context->UnrealizeWindow = pScreen->UnrealizeWindow;
	pScreen->UnrealizeWindow = wayland_unrealize_window;

	priv = dixLookupPrivate(&pWin->devPrivates, wayland_window_private_key);
	if (priv) {
		wl_surface_destroy(priv->surface);
		free(priv);
		dixSetPrivate(&pWin->devPrivates, wayland_window_private_key, NULL);
	}

	return ret;
}

void
wayland_init_rootless(ScrnInfoPtr pScrn)
{
	ScreenPtr pScreen = screenInfo.screens[pScrn->scrnIndex];
	I830Ptr pI830 = I830PTR(pScrn);
	struct xwl_context *context = pI830->wayland_data;

	context->RealizeWindow = pScreen->RealizeWindow;
	pScreen->RealizeWindow = wayland_realize_window;
	
	context->UnrealizeWindow = pScreen->UnrealizeWindow;
	pScreen->UnrealizeWindow = wayland_unrealize_window;
}

Bool
wayland_pre_init(ScrnInfoPtr pScrn)
{
	struct xwl_context *context;
	I830Ptr pI830 = I830PTR(pScrn);
	
	pI830->have_gem = TRUE;
	i830_init_bufmgr(pScrn);

	context = malloc(sizeof *context);
	if (context == NULL) {
		ErrorF("malloc failed\n");
		return FALSE;
	}

	memset(context, 0, sizeof *context);
	context->rootless = 1;
	context->pScrn = pScrn;
	context->display = wl_display_create(socket_name, sizeof socket_name);
	if (context->display == NULL) {
		ErrorF("wl_display_create failed\n");
		return FALSE;
	}

	xf86CrtcConfigInit(pScrn, &config_funcs);

	xf86CrtcSetSizeRange(pScrn, 320, 200, 8192, 8192);

	/* Set up listener so we'll catch all events. */
	wl_display_add_global_listener(context->display,
				       global_handler, context);

	/* Process connection events. */
	wl_display_iterate(context->display, WL_DISPLAY_READABLE);

	context->fd = wl_display_get_fd(context->display,
					source_update, context);

	pI830->wayland_data = context;
	AddGeneralSocket(context->fd);
	RegisterBlockAndWakeupHandlers(block_handler, wakeup_handler, context);

	xf86InitialConfiguration(pScrn, TRUE);

	wayland_input_init_pointer(context);
	wayland_input_init_keyboard(context);

	return TRUE;
}

void wayland_post_damage(I830Ptr pI830)
{
	struct xwl_context *context = pI830->wayland_data;

	/* FIXME: Need a list of surfaces with damage. */
	wl_surface_damage(context->surface, 0, 0, 10, 10);
	wl_compositor_commit(context->compositor, 0);

	while (context->mask & WL_DISPLAY_WRITABLE)
		wl_display_iterate(context->display,
				   WL_DISPLAY_WRITABLE);
}