blob: e80a00ebc51f7af926187d6f9c8512e185507d86 [file] [log] [blame]
Kristian Høgsberg558949b2011-12-21 22:54:49 -05001/*
2 * Copyright © 2011 Benjamin Franzke
3 * Copyright © 2011 Intel Corporation
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 * OF THIS SOFTWARE.
22 */
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <stdbool.h>
28#include <assert.h>
29#include <unistd.h>
30#include <sys/mman.h>
31
32#include <wayland-client.h>
33#include <wayland-egl.h>
34
35struct touch {
36 struct wl_display *display;
37 struct wl_compositor *compositor;
38 struct wl_shell *shell;
39 struct wl_shm *shm;
40 struct wl_input_device *input_device;
41 struct wl_surface *surface;
42 struct wl_shell_surface *shell_surface;
43 struct wl_buffer *buffer;
44 int has_argb;
45 uint32_t mask;
46 int width, height;
47 void *data;
48};
49
50static void
51create_shm_buffer(struct touch *touch)
52{
53 char filename[] = "/tmp/wayland-shm-XXXXXX";
54 int fd, size, stride;
55
56 fd = mkstemp(filename);
57 if (fd < 0) {
58 fprintf(stderr, "open %s failed: %m\n", filename);
59 exit(1);
60 }
61 stride = touch->width * 4;
62 size = stride * touch->height;
63 if (ftruncate(fd, size) < 0) {
64 fprintf(stderr, "ftruncate failed: %m\n");
65 close(fd);
66 exit(1);
67 }
68
69 touch->data =
70 mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
71 unlink(filename);
72
73 if (touch->data == MAP_FAILED) {
74 fprintf(stderr, "mmap failed: %m\n");
75 close(fd);
76 exit(1);
77 }
78
79 touch->buffer =
80 wl_shm_create_buffer(touch->shm, fd,
81 touch->width, touch->height, stride,
82 WL_SHM_FORMAT_PREMULTIPLIED_ARGB32);
83
84 close(fd);
85}
86
87static void
88shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
89{
90 struct touch *touch = data;
91
92 if (format == WL_SHM_FORMAT_PREMULTIPLIED_ARGB32)
93 touch->has_argb = 1;
94}
95
96struct wl_shm_listener shm_listenter = {
97 shm_format
98};
99
100
101static void
102input_device_handle_motion(void *data, struct wl_input_device *input_device,
103 uint32_t time,
104 int32_t x, int32_t y, int32_t sx, int32_t sy)
105{
106}
107
108static void
109input_device_handle_button(void *data,
110 struct wl_input_device *input_device,
111 uint32_t time, uint32_t button, uint32_t state)
112{
113}
114
115static void
116input_device_handle_key(void *data, struct wl_input_device *input_device,
117 uint32_t time, uint32_t key, uint32_t state)
118{
119}
120
121static void
122input_device_handle_pointer_focus(void *data,
123 struct wl_input_device *input_device,
124 uint32_t time, struct wl_surface *surface,
125 int32_t x, int32_t y, int32_t sx, int32_t sy)
126{
127}
128
129static void
130input_device_handle_keyboard_focus(void *data,
131 struct wl_input_device *input_device,
132 uint32_t time,
133 struct wl_surface *surface,
134 struct wl_array *keys)
135{
136}
137
138static void
139touch_paint(struct touch *touch, int32_t x, int32_t y, int32_t id)
140{
141 uint32_t *p, c;
142 static const uint32_t colors[] = {
143 0xffff0000,
144 0xffffff00,
145 0xff0000ff,
146 0xffff00ff,
147 };
148
149 if (id < ARRAY_LENGTH(colors))
150 c = colors[id];
151 else
152 c = 0xffffffff;
153
154 if (x < 1 || touch->width - 1 < x ||
155 y < 1 || touch->height - 1 < y)
156 return;
157
158 p = (uint32_t *) touch->data + (x - 1) + (y -1 ) * touch->width;
159 p[1] = c;
160 p += touch->width;
161 p[0] = c;
162 p[1] = c;
163 p[2] = c;
164 p += touch->width;
165 p[1] = c;
166
167 wl_buffer_damage(touch->buffer, 0, 0, touch->width, touch->height);
168 wl_surface_damage(touch->surface,
169 0, 0, touch->width, touch->height);
170}
171
172static void
173input_device_handle_touch_down(void *data,
174 struct wl_input_device *wl_input_device,
175 uint32_t time, struct wl_surface *surface,
176 int32_t id, int32_t x, int32_t y)
177{
178 struct touch *touch = data;
179
180 touch_paint(touch, x, y, id);
181}
182
183static void
184input_device_handle_touch_up(void *data,
185 struct wl_input_device *wl_input_device,
186 uint32_t time, int32_t id)
187{
188}
189
190static void
191input_device_handle_touch_motion(void *data,
192 struct wl_input_device *wl_input_device,
193 uint32_t time,
194 int32_t id, int32_t x, int32_t y)
195{
196 struct touch *touch = data;
197
198 touch_paint(touch, x, y, id);
199}
200
201static void
202input_device_handle_touch_frame(void *data,
203 struct wl_input_device *wl_input_device)
204{
205}
206
207static void
208input_device_handle_touch_cancel(void *data,
209 struct wl_input_device *wl_input_device)
210{
211}
212
213static const struct wl_input_device_listener input_device_listener = {
214 input_device_handle_motion,
215 input_device_handle_button,
216 input_device_handle_key,
217 input_device_handle_pointer_focus,
218 input_device_handle_keyboard_focus,
219 input_device_handle_touch_down,
220 input_device_handle_touch_up,
221 input_device_handle_touch_motion,
222 input_device_handle_touch_frame,
223 input_device_handle_touch_cancel,
224};
225
226static void
227handle_global(struct wl_display *display, uint32_t id,
228 const char *interface, uint32_t version, void *data)
229{
230 struct touch *touch = data;
231
232 if (strcmp(interface, "wl_compositor") == 0) {
233 touch->compositor =
234 wl_display_bind(display, id, &wl_compositor_interface);
235 } else if (strcmp(interface, "wl_shell") == 0) {
236 touch->shell =
237 wl_display_bind(display, id, &wl_shell_interface);
238 } else if (strcmp(interface, "wl_shm") == 0) {
239 touch->shm = wl_display_bind(display, id, &wl_shm_interface);
240 wl_shm_add_listener(touch->shm, &shm_listenter, touch);
241 } else if (strcmp(interface, "wl_input_device") == 0) {
242 touch->input_device =
243 wl_display_bind(display, id,
244 &wl_input_device_interface);
245 wl_input_device_add_listener(touch->input_device,
246 &input_device_listener, touch);
247 }
248}
249
250static int
251event_mask_update(uint32_t mask, void *data)
252{
253 struct touch *touch = data;
254
255 touch->mask = mask;
256
257 return 0;
258}
259
260static struct touch *
261touch_create(int width, int height)
262{
263 struct touch *touch;
264
265 touch = malloc(sizeof *touch);
266 touch->display = wl_display_connect(NULL);
267 assert(touch->display);
268
269 touch->has_argb = 0;
270 wl_display_add_global_listener(touch->display, handle_global, touch);
271 wl_display_iterate(touch->display, WL_DISPLAY_READABLE);
272 wl_display_roundtrip(touch->display);
273
274 if (!touch->has_argb) {
275 fprintf(stderr, "WL_SHM_FORMAT_ARGB32 not available\n");
276 exit(1);
277 }
278
279 wl_display_get_fd(touch->display, event_mask_update, touch);
280
281 touch->width = width;
282 touch->height = height;
283 touch->surface = wl_compositor_create_surface(touch->compositor);
284 touch->shell_surface = wl_shell_get_shell_surface(touch->shell,
285 touch->surface);
286 create_shm_buffer(touch);
287
288 wl_shell_surface_set_toplevel(touch->shell_surface);
289 wl_surface_set_user_data(touch->surface, touch);
290
291 memset(touch->data, 64, width * height * 4);
292 wl_buffer_damage(touch->buffer, 0, 0, width, height);
293 wl_surface_attach(touch->surface, touch->buffer, 0, 0);
294 wl_surface_damage(touch->surface, 0, 0, width, height);
295
296 return touch;
297}
298
299int
300main(int argc, char **argv)
301{
302 struct touch *touch;
303
304 touch = touch_create(600, 500);
305
306 while (true)
307 wl_display_iterate(touch->display, touch->mask);
308
309 return 0;
310}