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
|
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <i915_drm.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <fcntl.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#include <cairo.h>
#include "wayland-client.h"
static const char gem_device[] = "/dev/dri/card0";
static const char socket_name[] = "\0wayland";
static uint32_t name_cairo_surface(int fd, cairo_surface_t *surface)
{
struct drm_i915_gem_create create;
struct drm_gem_flink flink;
struct drm_i915_gem_pwrite pwrite;
int32_t width, height, stride;
void *data;
width = cairo_image_surface_get_width(surface);
height = cairo_image_surface_get_height(surface);
stride = cairo_image_surface_get_stride(surface);
data = cairo_image_surface_get_data(surface);
memset(&create, 0, sizeof(create));
create.size = height * stride;
if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
fprintf(stderr, "gem create failed: %m\n");
return 0;
}
pwrite.handle = create.handle;
pwrite.offset = 0;
pwrite.size = height * stride;
pwrite.data_ptr = (uint64_t) (uintptr_t) data;
if (ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite) < 0) {
fprintf(stderr, "gem pwrite failed: %m\n");
return 0;
}
flink.handle = create.handle;
if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
fprintf(stderr, "gem flink failed: %m\n");
return 0;
}
#if 0
/* We need to hold on to the handle until the server has received
* the attach request... we probably need a confirmation event.
* I guess the breadcrumb idea will suffice. */
struct drm_gem_close close;
close.handle = create.handle;
if (ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close) < 0) {
fprintf(stderr, "gem close failed: %m\n");
return 0;
}
#endif
return flink.name;
}
static void *
draw_pointer(int width, int height)
{
const int d = 2, end = 4, tx = 2, ty = 6, dx = 4, dy = 4;
cairo_surface_t *surface;
cairo_t *cr;
surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
width, height);
cr = cairo_create(surface);
cairo_set_line_width (cr, d);
cairo_move_to(cr, d, d);
cairo_line_to(cr, d + tx, d + ty);
cairo_line_to(cr, d + dx, d + dy);
cairo_line_to(cr, width - end, height - d);
cairo_line_to(cr, width - d, height - end);
cairo_line_to(cr, d + dy, d + dx);
cairo_line_to(cr, d + ty, d + tx);
cairo_close_path(cr);
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_stroke_preserve(cr);
cairo_set_source_rgb(cr, 1, 0, 0);
cairo_fill(cr);
cairo_destroy(cr);
return surface;
}
static int
connection_update(struct wl_connection *connection,
uint32_t mask, void *data)
{
struct pollfd *p = data;
p->events = 0;
if (mask & WL_CONNECTION_READABLE)
p->events |= POLLIN;
if (mask & WL_CONNECTION_WRITABLE)
p->events |= POLLOUT;
return 0;
}
struct pointer {
int width, height;
struct wl_surface *surface;
};
void event_handler(struct wl_display *display,
uint32_t opcode,
uint32_t arg1, uint32_t arg2, void *data)
{
struct pointer *pointer = data;
if (opcode == 0)
wl_surface_map(pointer->surface, arg1, arg2, pointer->width, pointer->height);
}
int main(int argc, char *argv[])
{
struct wl_display *display;
struct pointer pointer;
int fd;
uint32_t name, mask;
cairo_surface_t *s;
struct pollfd p[1];
fd = open(gem_device, O_RDWR);
if (fd < 0) {
fprintf(stderr, "drm open failed: %m\n");
return -1;
}
display = wl_display_create(socket_name,
connection_update, &p[0]);
if (display == NULL) {
fprintf(stderr, "failed to create display: %m\n");
return -1;
}
p[0].fd = wl_display_get_fd(display);
pointer.width = 16;
pointer.height = 16;
pointer.surface = wl_display_create_surface(display);
s = draw_pointer(pointer.width, pointer.height);
name = name_cairo_surface(fd, s);
wl_surface_attach(pointer.surface, name,
pointer.width, pointer.height,
cairo_image_surface_get_stride(s));
wl_surface_map(pointer.surface, 512, 384, pointer.width, pointer.height);
wl_display_set_event_handler(display, event_handler, &pointer);
while (1) {
poll(p, 1, -1);
mask = 0;
if (p[0].revents & POLLIN)
mask |= WL_CONNECTION_READABLE;
if (p[0].revents & POLLOUT)
mask |= WL_CONNECTION_WRITABLE;
wl_display_iterate(display, mask);
}
return 0;
}
|