Add README for the adventurous, allow evdev override from getenv().
[profile/ivi/wayland.git] / background.c
1 #include <stdint.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <fcntl.h>
5 #include <i915_drm.h>
6 #include <sys/ioctl.h>
7 #include <sys/poll.h>
8 #include <gdk-pixbuf/gdk-pixbuf.h>
9
10 #include "wayland-client.h"
11
12 static const char gem_device[] = "/dev/dri/card0";
13 static const char socket_name[] = "\0wayland";
14
15 static uint8_t *convert_to_argb(GdkPixbuf *pixbuf, int *new_stride)
16 {
17         uint8_t *data, *row, *src;
18         uint32_t *pixel;
19         int width, height, stride, channels, i, j;
20
21         width = gdk_pixbuf_get_width(pixbuf);
22         height = gdk_pixbuf_get_height(pixbuf);
23         stride = gdk_pixbuf_get_rowstride(pixbuf);
24         row = gdk_pixbuf_get_pixels(pixbuf);
25         channels = gdk_pixbuf_get_n_channels(pixbuf);
26
27         data = malloc(height * width * 4);
28         if (data == NULL) {
29                 fprintf(stderr, "out of memory\n");
30                 return NULL;
31         }
32
33         for (i = 0; i < height; i++) {
34                 for (j = 0; j < width; j++) {
35                         src = &row[i * stride + j * channels];
36                         pixel = (uint32_t *) &data[i * width * 4 + j * 4];
37                         *pixel = 0xff000000 | (src[0] << 16) | (src[1] << 8) | src[2];
38                 }
39         }
40
41         *new_stride = width * 4;
42
43         return data;
44 }
45
46 static uint32_t name_pixbuf(int fd, GdkPixbuf *pixbuf)
47 {
48         struct drm_i915_gem_create create;
49         struct drm_gem_flink flink;
50         struct drm_i915_gem_pwrite pwrite;
51         int32_t width, height, stride;
52         void *data;
53
54         width = gdk_pixbuf_get_width(pixbuf);
55         height = gdk_pixbuf_get_height(pixbuf);
56         data = convert_to_argb(pixbuf, &stride);
57
58         memset(&create, 0, sizeof(create));
59         create.size = height * stride;
60
61         if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
62                 fprintf(stderr, "gem create failed: %m\n");
63                 return 0;
64         }
65
66         pwrite.handle = create.handle;
67         pwrite.offset = 0;
68         pwrite.size = height * stride;
69         pwrite.data_ptr = (uint64_t) (uintptr_t) data;
70         if (ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite) < 0) {
71                 fprintf(stderr, "gem pwrite failed: %m\n");
72                 return 0;
73         }
74
75         flink.handle = create.handle;
76         if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
77                 fprintf(stderr, "gem flink failed: %m\n");
78                 return 0;
79         }
80
81 #if 0
82         /* We need to hold on to the handle until the server has received
83          * the attach request... we probably need a confirmation event.
84          * I guess the breadcrumb idea will suffice. */
85         struct drm_gem_close close;
86         close.handle = create.handle;
87         if (ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close) < 0) {
88                 fprintf(stderr, "gem close failed: %m\n");
89                 return 0;
90         }
91 #endif
92
93         return flink.name;
94 }
95
96 static int
97 connection_update(struct wl_connection *connection,
98                   uint32_t mask, void *data)
99 {
100         struct pollfd *p = data;
101
102         p->events = 0;
103         if (mask & WL_CONNECTION_READABLE)
104                 p->events |= POLLIN;
105         if (mask & WL_CONNECTION_WRITABLE)
106                 p->events |= POLLOUT;
107
108         return 0;
109 }
110
111 int main(int argc, char *argv[])
112 {
113         GdkPixbuf *image;
114         GError *error = NULL;
115         struct wl_display *display;
116         struct wl_surface *surface;
117         int fd, width, height, stride;
118         uint32_t name, mask;
119         struct pollfd p[1];
120
121         fd = open(gem_device, O_RDWR);
122         if (fd < 0) {
123                 fprintf(stderr, "drm open failed: %m\n");
124                 return -1;
125         }
126
127         display = wl_display_create(socket_name,
128                                     connection_update, &p[0]);
129         if (display == NULL) {
130                 fprintf(stderr, "failed to create display: %m\n");
131                 return -1;
132         }
133         p[0].fd = wl_display_get_fd(display);
134
135         surface = wl_display_create_surface(display);
136
137         g_type_init();
138         image = gdk_pixbuf_new_from_file (argv[1], &error);
139
140         name = name_pixbuf(fd, image);
141
142         width = gdk_pixbuf_get_width(image);
143         height = gdk_pixbuf_get_height(image);
144         stride = gdk_pixbuf_get_rowstride(image);
145
146         printf("width %d, height %d\n", width, height);
147
148         wl_surface_attach(surface, name, width, height, width * 4);
149
150         wl_surface_map(surface, 0, 0, width, height);
151
152         while (1) {
153                 poll(p, 1, -1);
154                 mask = 0;
155                 if (p[0].revents & POLLIN)
156                         mask |= WL_CONNECTION_READABLE;
157                 if (p[0].revents & POLLOUT)
158                         mask |= WL_CONNECTION_WRITABLE;
159                 wl_display_iterate(display, mask);
160         }
161
162         return 0;
163 }