summaryrefslogtreecommitdiff
path: root/wayland-kms-auth.c
blob: 679c6a558c8cae60339a2c3c92ef1f2d98a32541 (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
/*
 * Copyright © 2013 Renesas Solutions Corp.
 * Copyright © 2011 Kristian Høgsberg
 * Copyright © 2011 Benjamin Franzke
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *    Takanari Hayama <taki@igel.co.jp>
 *
 * Based on wayland-drm.c by the following authors:
 *    Kristian Høgsberg <krh@bitplanet.net>
 *    Benjamin Franzke <benjaminfranzke@googlemail.com>
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <unistd.h>
#include <errno.h>

#include <xf86drm.h>
#include <wayland-client.h>
#include "wayland-kms-auth.h"
#include "wayland-kms-client-protocol.h"

#if defined(DEBUG)
#	define WLKMS_DEBUG(s, x...) { printf(s, ##x); }
#else
#	define WLKMS_DEBUG(s, x...) { }
#endif

struct kms_auth {
	struct wl_display *wl_display;	/* wl_display facing my server */
	struct wl_event_queue *wl_queue;
	struct wl_registry *wl_registry;
	struct wl_kms *wl_kms;
	int authenticated;
};


/*
 * Sync with the server
 */

static void wayland_sync_callback(void *data, struct wl_callback *callback, uint32_t serial)
{
	int *done = data;
	*done = 1;
	WLKMS_DEBUG("%s: %s: %d\n", __FILE__, __func__, __LINE__);
	wl_callback_destroy(callback);
}

static const struct wl_callback_listener wayland_sync_listener = {
	.done = wayland_sync_callback
};

/*
 * sync with the server
 */
int wayland_sync(struct kms_auth *auth)
{
	struct wl_callback *callback;
	int ret = 0, done = 0;

	WLKMS_DEBUG("%s: %s: %d\n", __FILE__, __func__, __LINE__);
	callback = wl_display_sync(auth->wl_display);
	wl_callback_add_listener(callback, &wayland_sync_listener, &done);
	wl_proxy_set_queue((struct wl_proxy*)callback, auth->wl_queue);
	while (ret >= 0 && !done) {
		ret = wl_display_dispatch_queue(auth->wl_display, auth->wl_queue);
	}

	if (!done) {
		wl_callback_destroy(callback);
	}

	return ret;
}

/*
 * For the nested authentication
 */

static void wayland_kms_handle_authenticated(void *data, struct wl_kms *kms)
{
	struct kms_auth *auth = data;
	WLKMS_DEBUG("%s: %s: %d: authenticated.\n", __FILE__, __func__, __LINE__);
	auth->authenticated = 1;
}

static void wayland_kms_handle_format(void *data, struct wl_kms *kms, uint32_t format)
{
}

static void wayland_kms_handle_device(void *data, struct wl_kms *kms, const char *device)
{
}

static const struct wl_kms_listener wayland_kms_listener = {
	.authenticated = wayland_kms_handle_authenticated,
	.format = wayland_kms_handle_format,
	.device = wayland_kms_handle_device
};

/*
 * registry routines to the server global objects
 */

static void wayland_registry_handle_global(void *data, struct wl_registry *registry,
		                                           uint32_t name, const char *interface, uint32_t version)
{
	struct kms_auth *auth = data;

	WLKMS_DEBUG("%s: %s: %d\n", __FILE__, __func__, __LINE__);

	/*
	 * we need to connect to the wl_kms objects
	 */
	if (!strcmp(interface, "wl_kms")) {
		auth->wl_kms = wl_registry_bind(registry, name, &wl_kms_interface, version);
		wl_kms_add_listener(auth->wl_kms, &wayland_kms_listener, auth);
	}
}

static void wayland_registry_handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
{
}

static const struct wl_registry_listener wayland_registry_listener = {
	.global = wayland_registry_handle_global,
	.global_remove = wayland_registry_handle_global_remove,
};

int
kms_auth_request(struct kms_auth *auth, uint32_t magic)
{
	auth->authenticated = 0;
	wl_kms_authenticate(auth->wl_kms, magic);

	if (wayland_sync(auth) < 0 || !auth->authenticated)
		return -1;

	return 0;
}

struct kms_auth*
kms_auth_init(struct wl_display *display)
{
	struct kms_auth *auth;

	if (!(auth = calloc(1, sizeof(struct kms_auth))))
		return NULL;

	auth->wl_display = display;

	auth->wl_queue = wl_display_create_queue(auth->wl_display);
	auth->wl_registry = wl_display_get_registry(auth->wl_display);
	wl_proxy_set_queue((struct wl_proxy*)auth->wl_registry, auth->wl_queue);
	wl_registry_add_listener(auth->wl_registry, &wayland_registry_listener, auth);

	if (wayland_sync(auth) < 0) {
		free(auth);
		return NULL;
	}

	return auth;
}