blob: 826d9c10337269ceb6984e3ad457f0d98a5c1d91 [file] [log] [blame]
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -05001#include <stdint.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <i915_drm.h>
6#include <sys/ioctl.h>
7#include <fcntl.h>
8#include <unistd.h>
9#include <math.h>
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -050010#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050011#include <glib.h>
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -050012
13#include "wayland-client.h"
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050014#include "wayland-glib.h"
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -050015
16static const char gem_device[] = "/dev/dri/card0";
17static const char socket_name[] = "\0wayland";
18
19static uint32_t name_cairo_surface(int fd, cairo_surface_t *surface)
20{
21 struct drm_i915_gem_create create;
22 struct drm_gem_flink flink;
23 struct drm_i915_gem_pwrite pwrite;
24 int32_t width, height, stride;
Kristian Høgsberg1b2f4392008-11-02 15:50:55 -050025 void *data;
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -050026
27 width = cairo_image_surface_get_width(surface);
28 height = cairo_image_surface_get_height(surface);
29 stride = cairo_image_surface_get_stride(surface);
Kristian Høgsberg1b2f4392008-11-02 15:50:55 -050030 data = cairo_image_surface_get_data(surface);
31
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -050032 memset(&create, 0, sizeof(create));
33 create.size = height * stride;
34
35 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
36 fprintf(stderr, "gem create failed: %m\n");
37 return 0;
38 }
39
40 pwrite.handle = create.handle;
41 pwrite.offset = 0;
42 pwrite.size = height * stride;
Kristian Høgsberg1b2f4392008-11-02 15:50:55 -050043 pwrite.data_ptr = (uint64_t) (uintptr_t) data;
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -050044 if (ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite) < 0) {
45 fprintf(stderr, "gem pwrite failed: %m\n");
46 return 0;
47 }
48
49 flink.handle = create.handle;
50 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
51 fprintf(stderr, "gem flink failed: %m\n");
52 return 0;
53 }
54
55#if 0
56 /* We need to hold on to the handle until the server has received
57 * the attach request... we probably need a confirmation event.
58 * I guess the breadcrumb idea will suffice. */
59 struct drm_gem_close close;
60 close.handle = create.handle;
61 if (ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close) < 0) {
62 fprintf(stderr, "gem close failed: %m\n");
63 return 0;
64 }
65#endif
66
67 return flink.name;
68}
69
70static void *
71draw_pointer(int width, int height)
72{
Kristian Høgsberg30749422008-11-08 16:01:59 -050073 const int d = 2, end = 6, tx = 2, ty = 7, dx = 3, dy = 5;
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -050074 cairo_surface_t *surface;
75 cairo_t *cr;
76
77 surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
78 width, height);
79
80 cr = cairo_create(surface);
81 cairo_set_line_width (cr, d);
82 cairo_move_to(cr, d, d);
83 cairo_line_to(cr, d + tx, d + ty);
84 cairo_line_to(cr, d + dx, d + dy);
85 cairo_line_to(cr, width - end, height - d);
86 cairo_line_to(cr, width - d, height - end);
87 cairo_line_to(cr, d + dy, d + dx);
88 cairo_line_to(cr, d + ty, d + tx);
89 cairo_close_path(cr);
Kristian Høgsberg30749422008-11-08 16:01:59 -050090 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -050091 cairo_stroke_preserve(cr);
Kristian Høgsberg30749422008-11-08 16:01:59 -050092 cairo_set_source_rgb(cr, 1, 1, 1);
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -050093 cairo_fill(cr);
94
95 cairo_destroy(cr);
96
97 return surface;
98}
99
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -0500100struct pointer {
101 int width, height;
102 struct wl_surface *surface;
103};
104
Kristian Høgsberg30749422008-11-08 16:01:59 -0500105static void
106event_handler(struct wl_display *display,
107 uint32_t opcode,
108 uint32_t arg1, uint32_t arg2, void *data)
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -0500109{
110 struct pointer *pointer = data;
111
Kristian Høgsberg2a20d832008-11-02 17:22:39 -0500112 if (opcode == 0)
113 wl_surface_map(pointer->surface, arg1, arg2, pointer->width, pointer->height);
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -0500114}
115
116int main(int argc, char *argv[])
117{
118 struct wl_display *display;
119 struct pointer pointer;
120 int fd;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500121 uint32_t name;
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -0500122 cairo_surface_t *s;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500123 GMainLoop *loop;
124 GSource *source;
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -0500125
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -0500126 fd = open(gem_device, O_RDWR);
127 if (fd < 0) {
128 fprintf(stderr, "drm open failed: %m\n");
129 return -1;
130 }
131
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500132 display = wl_display_create(socket_name);
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -0500133 if (display == NULL) {
134 fprintf(stderr, "failed to create display: %m\n");
135 return -1;
136 }
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500137
138 loop = g_main_loop_new(NULL, FALSE);
139 source = wayland_source_new(display);
140 g_source_attach(source, NULL);
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -0500141
142 pointer.width = 16;
143 pointer.height = 16;
144 pointer.surface = wl_display_create_surface(display);
145
146 s = draw_pointer(pointer.width, pointer.height);
147 name = name_cairo_surface(fd, s);
148
149 wl_surface_attach(pointer.surface, name,
150 pointer.width, pointer.height,
151 cairo_image_surface_get_stride(s));
Kristian Høgsberg2a20d832008-11-02 17:22:39 -0500152 wl_surface_map(pointer.surface, 512, 384, pointer.width, pointer.height);
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -0500153
154 wl_display_set_event_handler(display, event_handler, &pointer);
155
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500156 g_main_loop_run(loop);
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -0500157
158 return 0;
159}