summaryrefslogtreecommitdiff
path: root/hw/xfree86/xwayland/xwayland-client.c
blob: 859cf626c9bb17397c1217dfa59bfb2746774509 (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
/*
 * Copyright © 2010 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 <unistd.h>
#include <fcntl.h>

#include <wayland-util.h>
#include <wayland-client.h>

#include <xf86Crtc.h>

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

static void
compositor_handle_visual(void *data,
			 struct wl_compositor *compositor,
			 uint32_t id, uint32_t token)
{
    struct xwl_screen *xwl_screen = data;

    switch (token) {
    case WL_COMPOSITOR_VISUAL_ARGB32:
	xwl_screen->argb_visual =
	    wl_visual_create(xwl_screen->display, id, 1);
	break;
    case WL_COMPOSITOR_VISUAL_PREMULTIPLIED_ARGB32:
	xwl_screen->premultiplied_argb_visual =
	    wl_visual_create(xwl_screen->display, id, 1);
	break;
    case WL_COMPOSITOR_VISUAL_XRGB32:
	xwl_screen->rgb_visual =
	    wl_visual_create(xwl_screen->display, id, 1);
	break;
    }
}

static const struct wl_compositor_listener compositor_listener = {
    compositor_handle_visual,
};

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

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

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

static const struct wl_output_listener output_listener = {
    display_handle_geometry,
};

static void
create_output(struct xwl_screen *xwl_screen, uint32_t id,
	      uint32_t version)
{
    struct xwl_output *xwl_output;

    xwl_output = xwl_output_create(xwl_screen);

    xwl_output->output = wl_output_create (xwl_screen->display, id, 1);
    wl_output_add_listener(xwl_output->output,
			   &output_listener, xwl_output);
}

static void
global_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_compositor") == 0) {
	xwl_screen->compositor =
	    wl_compositor_create (xwl_screen->display, id, 1);
	wl_compositor_add_listener(xwl_screen->compositor,
				   &compositor_listener, xwl_screen);
    } else if (strcmp (interface, "wl_shm") == 0) {
        xwl_screen->shm = wl_shm_create (xwl_screen->display, id, 1);
    } else if (strcmp (interface, "wl_output") == 0) {
        create_output(xwl_screen, id, 1);
    }
}

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

    xwl_screen->mask = mask;

    return 0;
}

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

    if (err >= 0 && FD_ISSET(xwl_screen->wayland_fd, (fd_set *) read_mask))
	wl_display_iterate(xwl_screen->display, WL_DISPLAY_READABLE);
}

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

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

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

static void
sync_callback(void *data)
{
	int *done = data;

	*done = 1;
}

void
xwl_force_roundtrip(struct xwl_screen *xwl_screen)
{
    int done = 0;

    wl_display_sync_callback(xwl_screen->display, sync_callback, &done);
    wl_display_iterate(xwl_screen->display, WL_DISPLAY_WRITABLE);
    while (!done)
	    wl_display_iterate(xwl_screen->display, WL_DISPLAY_READABLE);
}

int
wayland_screen_init(struct xwl_screen *xwl_screen)
{
    AddGeneralSocket(xwl_screen->wayland_fd);
    RegisterBlockAndWakeupHandlers(block_handler, wakeup_handler, xwl_screen);
    return Success;
}

int
wayland_screen_pre_init(struct xwl_screen *xwl_screen, int use_drm)
{
    int ret;

    xwl_screen->display = wl_display_connect(NULL);
    if (xwl_screen->display == NULL) {
	ErrorF("wl_display_create failed\n");
	return BadAlloc;
    }

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

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

    xwl_screen->wayland_fd =
	wl_display_get_fd(xwl_screen->display, source_update, xwl_screen);

#ifdef WITH_LIBDRM
    if (use_drm)
	ret = xwl_drm_pre_init(xwl_screen);
    if (use_drm && ret != Success)
	return ret;
#endif

    if (!xwl_screen->premultiplied_argb_visual || !xwl_screen->rgb_visual)
	    xwl_force_roundtrip(xwl_screen);
    if (!xwl_screen->premultiplied_argb_visual || !xwl_screen->rgb_visual)
	    return BadMatch;

    return Success;
}

int
wayland_screen_close(struct xwl_screen *xwl_screen)
{
    struct xwl_input_device *xwl_input_device, *itmp;
    struct xwl_window *xwl_window, *wtmp;

    if (xwl_screen->input_listener)
	wl_display_remove_global_listener(xwl_screen->display,
					  xwl_screen->input_listener);


    list_for_each_entry_safe(xwl_input_device, itmp,
			     &xwl_screen->input_device_list, link) {
	wl_input_device_destroy(xwl_input_device->input_device);
	free(xwl_input_device);
    }
    list_for_each_entry_safe(xwl_window, wtmp,
			     &xwl_screen->window_list, link) {
	wl_buffer_destroy(xwl_window->buffer);
	wl_surface_destroy(xwl_window->surface);
	free(xwl_window);
    }

    xwl_screen->input_listener = NULL;
    list_init(&xwl_screen->input_device_list);
    list_init(&xwl_screen->damage_window_list);
    list_init(&xwl_screen->window_list);
    xwl_screen->root_x = 0;
    xwl_screen->root_y = 0;

    xwl_force_roundtrip(xwl_screen);
    return Success;
}