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
|
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <ctype.h>
#include <sys/poll.h>
#include "connection.h"
#include "wayland-client.h"
static const char socket_name[] = "\0wayland";
struct wl_buffer {
char data[4096];
int head, tail;
};
struct wl_proxy {
struct wl_display *display;
uint32_t id;
};
struct wl_display {
struct wl_proxy proxy;
struct wl_connection *connection;
int fd;
uint32_t id;
};
struct wl_surface {
struct wl_proxy proxy;
};
struct wl_display *
wl_display_create(const char *address,
wl_connection_update_func_t update, void *data)
{
struct wl_display *display;
struct sockaddr_un name;
socklen_t size;
char buffer[256];
uint32_t id, length;
display = malloc(sizeof *display);
if (display == NULL)
return NULL;
memset(display, 0, sizeof *display);
display->fd = socket(PF_LOCAL, SOCK_STREAM, 0);
if (display->fd < 0) {
free(display);
return NULL;
}
name.sun_family = AF_LOCAL;
memcpy(name.sun_path, address, strlen(address + 1) + 2);
size = offsetof (struct sockaddr_un, sun_path) + sizeof socket_name;
if (connect(display->fd, (struct sockaddr *) &name, size) < 0) {
close(display->fd);
free(display);
return NULL;
}
/* FIXME: We'll need a protocol for getting a new range, I
* guess... */
read(display->fd, &display->id, sizeof display->id);
/* FIXME: actually discover advertised objects here. */
read(display->fd, &id, sizeof id);
read(display->fd, &length, sizeof length);
read(display->fd, buffer, (length + 3) & ~3);
display->proxy.display = display;
display->proxy.id = id;
display->connection = wl_connection_create(display->fd,
update, data);
return display;
}
void
wl_display_destroy(struct wl_display *display)
{
wl_connection_destroy(display->connection);
close(display->fd);
free(display);
}
int
wl_display_get_fd(struct wl_display *display)
{
return display->fd;
}
static void
handle_event(struct wl_connection *connection)
{
uint32_t p[2], opcode, size;
wl_connection_copy(connection, p, sizeof p);
opcode = p[1] & 0xffff;
size = p[1] >> 16;
printf("signal from object %d, opcode %d, size %d\n",
p[0], opcode, size);
wl_connection_consume(connection, sizeof p);
}
void
wl_display_iterate(struct wl_display *display, uint32_t mask)
{
uint32_t p[2], opcode, size;
int len;
len = wl_connection_data(display->connection, mask);
while (len > 0) {
if (len < sizeof p)
break;
wl_connection_copy(display->connection, p, sizeof p);
opcode = p[1] & 0xffff;
size = p[1] >> 16;
if (len < size)
break;
handle_event(display->connection);
}
if (len < 0) {
fprintf(stderr, "read error: %m\n");
exit(EXIT_FAILURE);
}
}
#define WL_DISPLAY_CREATE_SURFACE 0
struct wl_surface *
wl_display_create_surface(struct wl_display *display)
{
struct wl_surface *surface;
uint32_t request[3];
surface = malloc(sizeof *surface);
if (surface == NULL)
return NULL;
surface->proxy.id = display->id++;
surface->proxy.display = display;
request[0] = display->proxy.id;
request[1] = WL_DISPLAY_CREATE_SURFACE | ((sizeof request) << 16);
request[2] = surface->proxy.id;
wl_connection_write(display->connection, request, sizeof request);
return surface;
}
#define WL_SURFACE_DESTROY 0
#define WL_SURFACE_ATTACH 1
#define WL_SURFACE_MAP 2
void wl_surface_destroy(struct wl_surface *surface)
{
uint32_t request[2];
request[0] = surface->proxy.id;
request[1] = WL_SURFACE_DESTROY | ((sizeof request) << 16);
wl_connection_write(surface->proxy.display->connection,
request, sizeof request);
}
void wl_surface_attach(struct wl_surface *surface,
uint32_t name, int width, int height, int stride)
{
uint32_t request[6];
request[0] = surface->proxy.id;
request[1] = WL_SURFACE_ATTACH | ((sizeof request) << 16);
request[2] = name;
request[3] = width;
request[4] = height;
request[5] = stride;
wl_connection_write(surface->proxy.display->connection,
request, sizeof request);
}
void wl_surface_map(struct wl_surface *surface,
int32_t x, int32_t y, int32_t width, int32_t height)
{
uint32_t request[6];
request[0] = surface->proxy.id;
request[1] = WL_SURFACE_MAP | ((sizeof request) << 16);
request[2] = x;
request[3] = y;
request[4] = width;
request[5] = height;
wl_connection_write(surface->proxy.display->connection,
request, sizeof request);
}
|