Kristian Høgsberg | ffd710e | 2008-12-02 15:15:01 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008 Kristian Høgsberg |
| 3 | * |
| 4 | * Permission to use, copy, modify, distribute, and sell this software and its |
| 5 | * documentation for any purpose is hereby granted without fee, provided that |
| 6 | * the above copyright notice appear in all copies and that both that copyright |
| 7 | * notice and this permission notice appear in supporting documentation, and |
| 8 | * that the name of the copyright holders not be used in advertising or |
| 9 | * publicity pertaining to distribution of the software without specific, |
| 10 | * written prior permission. The copyright holders make no representations |
| 11 | * about the suitability of this software for any purpose. It is provided "as |
| 12 | * is" without express or implied warranty. |
| 13 | * |
| 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 15 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 16 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 17 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 18 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 19 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 20 | * OF THIS SOFTWARE. |
| 21 | */ |
| 22 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 23 | #include <stdint.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 27 | #include <fcntl.h> |
| 28 | #include <unistd.h> |
| 29 | #include <math.h> |
| 30 | #include <time.h> |
| 31 | #include <cairo.h> |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 32 | #include <glib.h> |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame] | 33 | #include <glib-object.h> |
Kristian Høgsberg | da275dd | 2010-08-16 17:47:07 -0400 | [diff] [blame^] | 34 | #include <gdk-pixbuf/gdk-pixbuf.h> |
Kristian Høgsberg | 640609a | 2010-08-09 22:11:47 -0400 | [diff] [blame] | 35 | #include <xf86drm.h> |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 36 | |
| 37 | #define EGL_EGLEXT_PROTOTYPES 1 |
| 38 | #define GL_GLEXT_PROTOTYPES 1 |
| 39 | #include <GL/gl.h> |
| 40 | #include <EGL/egl.h> |
| 41 | #include <EGL/eglext.h> |
| 42 | #include <cairo-gl.h> |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 43 | |
Kristian Høgsberg | 94adf6c | 2010-06-25 16:50:05 -0400 | [diff] [blame] | 44 | #include <X11/extensions/XKBcommon.h> |
| 45 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 46 | #include <linux/input.h> |
Kristian Høgsberg | 3c38fa0 | 2009-02-23 22:30:29 -0500 | [diff] [blame] | 47 | #include "wayland-util.h" |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 48 | #include "wayland-client.h" |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 49 | #include "wayland-glib.h" |
Kristian Høgsberg | b91cd10 | 2010-08-16 16:17:42 -0400 | [diff] [blame] | 50 | #include "cairo-util.h" |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 51 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 52 | #include "window.h" |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 53 | |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 54 | struct display { |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 55 | struct wl_display *display; |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 56 | struct wl_compositor *compositor; |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 57 | struct wl_shell *shell; |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 58 | struct wl_drm *drm; |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 59 | struct wl_output *output; |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 60 | struct rectangle screen_allocation; |
Kristian Høgsberg | 640609a | 2010-08-09 22:11:47 -0400 | [diff] [blame] | 61 | int authenticated; |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 62 | EGLDisplay dpy; |
| 63 | EGLContext ctx; |
Janusz Lewandowski | d923e9d | 2010-01-31 03:01:26 +0100 | [diff] [blame] | 64 | cairo_device_t *device; |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 65 | int fd; |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 66 | GMainLoop *loop; |
| 67 | GSource *source; |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame] | 68 | struct wl_list window_list; |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 69 | struct wl_list input_list; |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame] | 70 | char *device_name; |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 71 | cairo_surface_t *active_frame, *inactive_frame, *shadow; |
Kristian Høgsberg | 3e6e7e6 | 2010-07-02 15:12:02 -0400 | [diff] [blame] | 72 | struct xkb_desc *xkb; |
Kristian Høgsberg | da275dd | 2010-08-16 17:47:07 -0400 | [diff] [blame^] | 73 | EGLImageKHR *pointer_images; |
| 74 | struct wl_buffer **pointer_buffers; |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | struct window { |
| 78 | struct display *display; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 79 | struct wl_surface *surface; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 80 | const char *title; |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 81 | struct rectangle allocation, saved_allocation, pending_allocation; |
| 82 | int resize_edges; |
Kristian Høgsberg | 80d746f | 2010-06-14 23:52:50 -0400 | [diff] [blame] | 83 | int redraw_scheduled; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 84 | int minimum_width, minimum_height; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 85 | int margin; |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 86 | int fullscreen; |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 87 | int decoration; |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 88 | struct input *grab_device; |
| 89 | struct input *keyboard_device; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 90 | uint32_t name; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 91 | |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 92 | EGLImageKHR *image; |
Kristian Høgsberg | 6a1b201 | 2009-12-16 14:43:37 -0500 | [diff] [blame] | 93 | cairo_surface_t *cairo_surface, *pending_surface; |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 94 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 95 | window_resize_handler_t resize_handler; |
Kristian Høgsberg | 80d746f | 2010-06-14 23:52:50 -0400 | [diff] [blame] | 96 | window_redraw_handler_t redraw_handler; |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 97 | window_key_handler_t key_handler; |
Kristian Høgsberg | 3c248cc | 2009-02-22 23:01:35 -0500 | [diff] [blame] | 98 | window_keyboard_focus_handler_t keyboard_focus_handler; |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame] | 99 | window_acknowledge_handler_t acknowledge_handler; |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 100 | window_frame_handler_t frame_handler; |
| 101 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 102 | void *user_data; |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame] | 103 | struct wl_list link; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 104 | }; |
| 105 | |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 106 | struct input { |
| 107 | struct display *display; |
| 108 | struct wl_input_device *input_device; |
| 109 | struct window *pointer_focus; |
| 110 | struct window *keyboard_focus; |
| 111 | uint32_t modifiers; |
| 112 | int32_t x, y, sx, sy; |
| 113 | struct wl_list link; |
| 114 | }; |
| 115 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 116 | static void |
| 117 | rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius) |
| 118 | { |
| 119 | cairo_move_to(cr, x0, y0 + radius); |
| 120 | cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2); |
| 121 | cairo_line_to(cr, x1 - radius, y0); |
| 122 | cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI); |
| 123 | cairo_line_to(cr, x1, y1 - radius); |
| 124 | cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2); |
| 125 | cairo_line_to(cr, x0 + radius, y1); |
| 126 | cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI); |
| 127 | cairo_close_path(cr); |
| 128 | } |
| 129 | |
Kristian Høgsberg | da275dd | 2010-08-16 17:47:07 -0400 | [diff] [blame^] | 130 | static int |
| 131 | texture_from_png(const char *filename, int width, int height) |
| 132 | { |
| 133 | GdkPixbuf *pixbuf; |
| 134 | GError *error = NULL; |
| 135 | void *data; |
| 136 | GLenum format; |
| 137 | |
| 138 | pixbuf = gdk_pixbuf_new_from_file_at_scale(filename, |
| 139 | width, height, |
| 140 | FALSE, &error); |
| 141 | if (error != NULL) |
| 142 | return -1; |
| 143 | |
| 144 | data = gdk_pixbuf_get_pixels(pixbuf); |
| 145 | |
| 146 | if (gdk_pixbuf_get_has_alpha(pixbuf)) |
| 147 | format = GL_RGBA; |
| 148 | else |
| 149 | format = GL_RGB; |
| 150 | |
| 151 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, |
| 152 | width, height, 0, format, GL_UNSIGNED_BYTE, data); |
| 153 | |
| 154 | gdk_pixbuf_unref(pixbuf); |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | static const struct { |
| 160 | const char *filename; |
| 161 | int hotspot_x, hotspot_y; |
| 162 | } pointer_images[] = { |
| 163 | { DATADIR "/wayland/bottom_left_corner.png", 6, 30 }, |
| 164 | { DATADIR "/wayland/bottom_right_corner.png", 28, 28 }, |
| 165 | { DATADIR "/wayland/bottom_side.png", 16, 20 }, |
| 166 | { DATADIR "/wayland/grabbing.png", 20, 17 }, |
| 167 | { DATADIR "/wayland/left_ptr.png", 10, 5 }, |
| 168 | { DATADIR "/wayland/left_side.png", 10, 20 }, |
| 169 | { DATADIR "/wayland/right_side.png", 30, 19 }, |
| 170 | { DATADIR "/wayland/top_left_corner.png", 8, 8 }, |
| 171 | { DATADIR "/wayland/top_right_corner.png", 26, 8 }, |
| 172 | { DATADIR "/wayland/top_side.png", 18, 8 }, |
| 173 | { DATADIR "/wayland/xterm.png", 15, 15 } |
| 174 | }; |
| 175 | |
| 176 | static void |
| 177 | create_pointer_images(struct display *d) |
| 178 | { |
| 179 | int i, count; |
| 180 | GLuint texture; |
| 181 | const int width = 32, height = 32; |
| 182 | EGLint name, stride; |
| 183 | struct wl_visual *visual; |
| 184 | |
| 185 | EGLint image_attribs[] = { |
| 186 | EGL_WIDTH, 0, |
| 187 | EGL_HEIGHT, 0, |
| 188 | EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA, |
| 189 | EGL_IMAGE_USE_MESA, EGL_IMAGE_USE_SCANOUT_MESA, |
| 190 | EGL_NONE |
| 191 | }; |
| 192 | |
| 193 | glGenTextures(1, &texture); |
| 194 | glBindTexture(GL_TEXTURE_2D, texture); |
| 195 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 196 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 197 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 198 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 199 | |
| 200 | visual = wl_display_get_argb_visual(d->display); |
| 201 | |
| 202 | image_attribs[1] = width; |
| 203 | image_attribs[3] = height; |
| 204 | count = ARRAY_LENGTH(pointer_images); |
| 205 | d->pointer_images = malloc(count * sizeof *d->pointer_images); |
| 206 | d->pointer_buffers = malloc(count * sizeof *d->pointer_buffers); |
| 207 | for (i = 0; i < count; i++) { |
| 208 | d->pointer_images[i] = |
| 209 | eglCreateDRMImageMESA(d->dpy, image_attribs); |
| 210 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, |
| 211 | d->pointer_images[i]); |
| 212 | eglExportDRMImageMESA(d->dpy, |
| 213 | d->pointer_images[i], |
| 214 | &name, NULL, &stride); |
| 215 | texture_from_png(pointer_images[i].filename, width, height); |
| 216 | |
| 217 | d->pointer_buffers[i] = |
| 218 | wl_drm_create_buffer(d->drm, name, |
| 219 | width, height, stride, visual); |
| 220 | } |
| 221 | |
| 222 | } |
| 223 | |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 224 | static const cairo_user_data_key_t surface_data_key; |
| 225 | struct surface_data { |
| 226 | EGLImageKHR image; |
| 227 | GLuint texture; |
| 228 | EGLDisplay dpy; |
| 229 | }; |
| 230 | |
| 231 | static void |
| 232 | surface_data_destroy(void *p) |
| 233 | { |
| 234 | struct surface_data *data = p; |
| 235 | |
| 236 | glDeleteTextures(1, &data->texture); |
| 237 | eglDestroyImageKHR(data->dpy, data->image); |
| 238 | } |
| 239 | |
| 240 | cairo_surface_t * |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 241 | display_create_surface(struct display *display, |
| 242 | struct rectangle *rectangle) |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 243 | { |
| 244 | struct surface_data *data; |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 245 | EGLDisplay dpy = display->dpy; |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 246 | cairo_surface_t *surface; |
| 247 | |
| 248 | EGLint image_attribs[] = { |
| 249 | EGL_WIDTH, 0, |
| 250 | EGL_HEIGHT, 0, |
| 251 | EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA, |
| 252 | EGL_IMAGE_USE_MESA, EGL_IMAGE_USE_SCANOUT_MESA, |
| 253 | EGL_NONE |
| 254 | }; |
| 255 | |
| 256 | data = malloc(sizeof *data); |
| 257 | image_attribs[1] = rectangle->width; |
| 258 | image_attribs[3] = rectangle->height; |
| 259 | data->image = eglCreateDRMImageMESA(dpy, image_attribs); |
| 260 | glGenTextures(1, &data->texture); |
| 261 | data->dpy = dpy; |
| 262 | glBindTexture(GL_TEXTURE_2D, data->texture); |
| 263 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image); |
| 264 | |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 265 | surface = cairo_gl_surface_create_for_texture(display->device, |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 266 | CAIRO_CONTENT_COLOR_ALPHA, |
| 267 | data->texture, |
| 268 | rectangle->width, |
| 269 | rectangle->height); |
| 270 | |
| 271 | cairo_surface_set_user_data (surface, &surface_data_key, |
| 272 | data, surface_data_destroy); |
| 273 | |
| 274 | return surface; |
| 275 | } |
| 276 | |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 277 | static void |
Kristian Høgsberg | 6a1b201 | 2009-12-16 14:43:37 -0500 | [diff] [blame] | 278 | window_attach_surface(struct window *window) |
| 279 | { |
| 280 | struct wl_visual *visual; |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 281 | struct display *display = window->display; |
| 282 | struct wl_buffer *buffer; |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 283 | struct surface_data *data; |
| 284 | EGLint name, stride; |
Kristian Høgsberg | 6a1b201 | 2009-12-16 14:43:37 -0500 | [diff] [blame] | 285 | |
Kristian Høgsberg | 2aac302 | 2009-12-21 10:04:53 -0500 | [diff] [blame] | 286 | if (window->pending_surface != NULL) |
| 287 | return; |
| 288 | |
Kristian Høgsberg | 0953162 | 2010-06-14 23:22:15 -0400 | [diff] [blame] | 289 | window->pending_surface = window->cairo_surface; |
| 290 | window->cairo_surface = NULL; |
Kristian Høgsberg | 2aac302 | 2009-12-21 10:04:53 -0500 | [diff] [blame] | 291 | |
Kristian Høgsberg | 0953162 | 2010-06-14 23:22:15 -0400 | [diff] [blame] | 292 | data = cairo_surface_get_user_data (window->pending_surface, |
| 293 | &surface_data_key); |
| 294 | eglExportDRMImageMESA(window->display->dpy, |
| 295 | data->image, &name, NULL, &stride); |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 296 | |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 297 | visual = wl_display_get_premultiplied_argb_visual(display->display); |
| 298 | buffer = wl_drm_create_buffer(display->drm, |
| 299 | name, |
| 300 | window->allocation.width, |
| 301 | window->allocation.height, |
| 302 | stride, |
| 303 | visual); |
| 304 | |
| 305 | wl_surface_attach(window->surface, buffer); |
| 306 | wl_buffer_destroy(buffer); |
Kristian Høgsberg | 6a1b201 | 2009-12-16 14:43:37 -0500 | [diff] [blame] | 307 | |
| 308 | wl_surface_map(window->surface, |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 309 | window->allocation.x, |
| 310 | window->allocation.y, |
| 311 | window->allocation.width, |
| 312 | window->allocation.height); |
Kristian Høgsberg | 0953162 | 2010-06-14 23:22:15 -0400 | [diff] [blame] | 313 | |
| 314 | wl_compositor_commit(window->display->compositor, 0); |
Kristian Høgsberg | 6a1b201 | 2009-12-16 14:43:37 -0500 | [diff] [blame] | 315 | } |
| 316 | |
Kristian Høgsberg | a341fa0 | 2010-01-24 18:10:15 -0500 | [diff] [blame] | 317 | void |
| 318 | window_commit(struct window *window, uint32_t key) |
| 319 | { |
Kristian Høgsberg | 0953162 | 2010-06-14 23:22:15 -0400 | [diff] [blame] | 320 | if (window->cairo_surface) { |
Kristian Høgsberg | a341fa0 | 2010-01-24 18:10:15 -0500 | [diff] [blame] | 321 | window_attach_surface(window); |
Kristian Høgsberg | 0953162 | 2010-06-14 23:22:15 -0400 | [diff] [blame] | 322 | } else { |
| 323 | wl_compositor_commit(window->display->compositor, key); |
Kristian Høgsberg | a341fa0 | 2010-01-24 18:10:15 -0500 | [diff] [blame] | 324 | } |
Kristian Høgsberg | a341fa0 | 2010-01-24 18:10:15 -0500 | [diff] [blame] | 325 | } |
| 326 | |
Kristian Høgsberg | 6a1b201 | 2009-12-16 14:43:37 -0500 | [diff] [blame] | 327 | static void |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 328 | window_draw_decorations(struct window *window) |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 329 | { |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 330 | cairo_t *cr; |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 331 | cairo_text_extents_t extents; |
Kristian Høgsberg | 2d6b7c1 | 2010-06-25 16:51:57 -0400 | [diff] [blame] | 332 | cairo_pattern_t *outline, *bright, *dim; |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 333 | int width, height; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 334 | |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 335 | window->cairo_surface = |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 336 | display_create_surface(window->display, &window->allocation); |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 337 | width = window->allocation.width; |
| 338 | height = window->allocation.height; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 339 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 340 | outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1); |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 341 | bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 342 | dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4); |
| 343 | |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 344 | cr = cairo_create(window->cairo_surface); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 345 | |
Kristian Høgsberg | 0953162 | 2010-06-14 23:22:15 -0400 | [diff] [blame] | 346 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 347 | cairo_set_source_rgba(cr, 0, 0, 0, 0); |
Kristian Høgsberg | 0953162 | 2010-06-14 23:22:15 -0400 | [diff] [blame] | 348 | cairo_paint(cr); |
| 349 | |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 350 | cairo_set_source_rgba(cr, 0, 0, 0, 0.6); |
| 351 | tile_mask(cr, window->display->shadow, 3, 3, width, height, 32); |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 352 | |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 353 | if (window->keyboard_device) |
| 354 | tile_source(cr, window->display->active_frame, |
| 355 | 0, 0, width, height, 96); |
| 356 | else |
| 357 | tile_source(cr, window->display->inactive_frame, |
| 358 | 0, 0, width, height, 96); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 359 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 360 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 361 | cairo_set_font_size(cr, 14); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 362 | cairo_text_extents(cr, window->title, &extents); |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 363 | cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 364 | cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); |
| 365 | cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND); |
| 366 | cairo_set_line_width (cr, 4); |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 367 | if (window->keyboard_device) |
| 368 | cairo_set_source_rgb(cr, 0, 0, 0); |
| 369 | else |
Kristian Høgsberg | 7d7b5db | 2009-09-21 13:43:46 -0400 | [diff] [blame] | 370 | cairo_set_source_rgb(cr, 0.8, 0.8, 0.8); |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 371 | cairo_show_text(cr, window->title); |
| 372 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 373 | cairo_destroy(cr); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | static void |
| 377 | window_draw_fullscreen(struct window *window) |
| 378 | { |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 379 | window->cairo_surface = |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 380 | display_create_surface(window->display, &window->allocation); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | void |
| 384 | window_draw(struct window *window) |
| 385 | { |
Kristian Høgsberg | 2aac302 | 2009-12-21 10:04:53 -0500 | [diff] [blame] | 386 | if (window->cairo_surface != NULL) |
| 387 | cairo_surface_destroy(window->cairo_surface); |
| 388 | |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 389 | if (window->fullscreen || !window->decoration) |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 390 | window_draw_fullscreen(window); |
| 391 | else |
| 392 | window_draw_decorations(window); |
Kristian Høgsberg | 44f36e3 | 2008-11-26 12:57:31 -0500 | [diff] [blame] | 393 | } |
| 394 | |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 395 | cairo_surface_t * |
| 396 | window_get_surface(struct window *window) |
| 397 | { |
| 398 | return window->cairo_surface; |
| 399 | } |
| 400 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 401 | enum window_state { |
Kristian Høgsberg | bf6ceda | 2010-06-14 20:25:06 -0400 | [diff] [blame] | 402 | WINDOW_MOVING = 0, |
| 403 | WINDOW_RESIZING_TOP = 1, |
| 404 | WINDOW_RESIZING_BOTTOM = 2, |
| 405 | WINDOW_RESIZING_LEFT = 4, |
| 406 | WINDOW_RESIZING_TOP_LEFT = 5, |
| 407 | WINDOW_RESIZING_BOTTOM_LEFT = 6, |
| 408 | WINDOW_RESIZING_RIGHT = 8, |
| 409 | WINDOW_RESIZING_TOP_RIGHT = 9, |
| 410 | WINDOW_RESIZING_BOTTOM_RIGHT = 10, |
| 411 | WINDOW_RESIZING_MASK = 15, |
| 412 | WINDOW_STABLE = 16, |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 413 | }; |
| 414 | |
Kristian Høgsberg | da275dd | 2010-08-16 17:47:07 -0400 | [diff] [blame^] | 415 | enum pointer_type { |
| 416 | POINTER_BOTTOM_LEFT, |
| 417 | POINTER_BOTTOM_RIGHT, |
| 418 | POINTER_BOTTOM, |
| 419 | POINTER_DRAGGING, |
| 420 | POINTER_LEFT_PTR, |
| 421 | POINTER_LEFT, |
| 422 | POINTER_RIGHT, |
| 423 | POINTER_TOP_LEFT, |
| 424 | POINTER_TOP_RIGHT, |
| 425 | POINTER_TOP, |
| 426 | POINTER_IBEAM, |
| 427 | }; |
| 428 | |
| 429 | static int |
| 430 | get_pointer_location(struct window *window, int32_t x, int32_t y) |
| 431 | { |
| 432 | int vlocation, hlocation; |
| 433 | const int grip_size = 8; |
| 434 | |
| 435 | if (x < window->margin) |
| 436 | hlocation = WINDOW_STABLE; |
| 437 | else if (window->margin <= x && x < window->margin + grip_size) |
| 438 | hlocation = WINDOW_RESIZING_LEFT; |
| 439 | else if (x < window->allocation.width - window->margin - grip_size) |
| 440 | hlocation = WINDOW_MOVING; |
| 441 | else if (x < window->allocation.width - window->margin) |
| 442 | hlocation = WINDOW_RESIZING_RIGHT; |
| 443 | else |
| 444 | hlocation = WINDOW_STABLE; |
| 445 | |
| 446 | if (y < window->margin) |
| 447 | vlocation = WINDOW_STABLE; |
| 448 | else if (window->margin <= y && y < window->margin + grip_size) |
| 449 | vlocation = WINDOW_RESIZING_TOP; |
| 450 | else if (y < window->allocation.height - window->margin - grip_size) |
| 451 | vlocation = WINDOW_MOVING; |
| 452 | else if (y < window->allocation.height - window->margin) |
| 453 | vlocation = WINDOW_RESIZING_BOTTOM; |
| 454 | else |
| 455 | vlocation = WINDOW_STABLE; |
| 456 | |
| 457 | return vlocation | hlocation; |
| 458 | } |
| 459 | |
| 460 | static void |
| 461 | set_pointer_image(struct input *input) |
| 462 | { |
| 463 | struct display *display = input->display; |
| 464 | int pointer, location; |
| 465 | |
| 466 | location = get_pointer_location(input->pointer_focus, |
| 467 | input->sx, input->sy); |
| 468 | switch (location) { |
| 469 | case WINDOW_RESIZING_TOP: |
| 470 | pointer = POINTER_TOP; |
| 471 | break; |
| 472 | case WINDOW_RESIZING_BOTTOM: |
| 473 | pointer = POINTER_BOTTOM; |
| 474 | break; |
| 475 | case WINDOW_RESIZING_LEFT: |
| 476 | pointer = POINTER_LEFT; |
| 477 | break; |
| 478 | case WINDOW_RESIZING_RIGHT: |
| 479 | pointer = POINTER_RIGHT; |
| 480 | break; |
| 481 | case WINDOW_RESIZING_TOP_LEFT: |
| 482 | pointer = POINTER_TOP_LEFT; |
| 483 | break; |
| 484 | case WINDOW_RESIZING_TOP_RIGHT: |
| 485 | pointer = POINTER_TOP_RIGHT; |
| 486 | break; |
| 487 | case WINDOW_RESIZING_BOTTOM_LEFT: |
| 488 | pointer = POINTER_BOTTOM_LEFT; |
| 489 | break; |
| 490 | case WINDOW_RESIZING_BOTTOM_RIGHT: |
| 491 | pointer = POINTER_BOTTOM_RIGHT; |
| 492 | break; |
| 493 | case WINDOW_STABLE: |
| 494 | wl_input_device_attach(input->input_device, |
| 495 | NULL, 0, 0); |
| 496 | return; |
| 497 | default: |
| 498 | /* FIXME: We should ask the application here. */ |
| 499 | pointer = POINTER_IBEAM; |
| 500 | break; |
| 501 | } |
| 502 | |
| 503 | wl_input_device_attach(input->input_device, |
| 504 | display->pointer_buffers[pointer], |
| 505 | pointer_images[pointer].hotspot_x, |
| 506 | pointer_images[pointer].hotspot_y); |
| 507 | } |
| 508 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 509 | static void |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 510 | window_handle_motion(void *data, struct wl_input_device *input_device, |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 511 | uint32_t time, |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 512 | int32_t x, int32_t y, int32_t sx, int32_t sy) |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 513 | { |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 514 | struct input *input = data; |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 515 | |
| 516 | input->x = x; |
| 517 | input->y = y; |
| 518 | input->sx = sx; |
| 519 | input->sy = sy; |
Kristian Høgsberg | da275dd | 2010-08-16 17:47:07 -0400 | [diff] [blame^] | 520 | |
| 521 | set_pointer_image(input); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 522 | } |
| 523 | |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 524 | static void window_handle_button(void *data, struct wl_input_device *input_device, |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 525 | uint32_t time, uint32_t button, uint32_t state) |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 526 | { |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 527 | struct input *input = data; |
| 528 | struct window *window = input->pointer_focus; |
Kristian Høgsberg | da275dd | 2010-08-16 17:47:07 -0400 | [diff] [blame^] | 529 | int location; |
Kristian Høgsberg | bf6ceda | 2010-06-14 20:25:06 -0400 | [diff] [blame] | 530 | |
Kristian Høgsberg | da275dd | 2010-08-16 17:47:07 -0400 | [diff] [blame^] | 531 | location = get_pointer_location(window, input->sx, input->sy); |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 532 | |
| 533 | if (button == BTN_LEFT && state == 1) { |
Kristian Høgsberg | da275dd | 2010-08-16 17:47:07 -0400 | [diff] [blame^] | 534 | switch (location) { |
Kristian Høgsberg | bf6ceda | 2010-06-14 20:25:06 -0400 | [diff] [blame] | 535 | case WINDOW_MOVING: |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 536 | wl_shell_move(window->display->shell, |
| 537 | window->surface, input_device, time); |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 538 | break; |
Kristian Høgsberg | bf6ceda | 2010-06-14 20:25:06 -0400 | [diff] [blame] | 539 | case WINDOW_RESIZING_TOP: |
| 540 | case WINDOW_RESIZING_BOTTOM: |
| 541 | case WINDOW_RESIZING_LEFT: |
| 542 | case WINDOW_RESIZING_RIGHT: |
| 543 | case WINDOW_RESIZING_TOP_LEFT: |
| 544 | case WINDOW_RESIZING_TOP_RIGHT: |
| 545 | case WINDOW_RESIZING_BOTTOM_LEFT: |
| 546 | case WINDOW_RESIZING_BOTTOM_RIGHT: |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 547 | wl_shell_resize(window->display->shell, |
| 548 | window->surface, input_device, time, |
Kristian Høgsberg | da275dd | 2010-08-16 17:47:07 -0400 | [diff] [blame^] | 549 | location); |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 550 | break; |
| 551 | } |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | |
Kristian Høgsberg | 99f090d | 2009-02-23 22:37:14 -0500 | [diff] [blame] | 555 | static void |
| 556 | window_handle_key(void *data, struct wl_input_device *input_device, |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 557 | uint32_t time, uint32_t key, uint32_t state) |
Kristian Høgsberg | 99f090d | 2009-02-23 22:37:14 -0500 | [diff] [blame] | 558 | { |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 559 | struct input *input = data; |
| 560 | struct window *window = input->keyboard_focus; |
Kristian Høgsberg | 94adf6c | 2010-06-25 16:50:05 -0400 | [diff] [blame] | 561 | struct display *d = window->display; |
| 562 | uint32_t code, sym, level; |
Kristian Høgsberg | 99f090d | 2009-02-23 22:37:14 -0500 | [diff] [blame] | 563 | |
Kristian Høgsberg | 94adf6c | 2010-06-25 16:50:05 -0400 | [diff] [blame] | 564 | code = key + d->xkb->min_key_code; |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 565 | if (window->keyboard_device != input) |
Kristian Høgsberg | 99f090d | 2009-02-23 22:37:14 -0500 | [diff] [blame] | 566 | return; |
| 567 | |
Kristian Høgsberg | 94adf6c | 2010-06-25 16:50:05 -0400 | [diff] [blame] | 568 | level = 0; |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 569 | if (input->modifiers & WINDOW_MODIFIER_SHIFT && |
Kristian Høgsberg | 94adf6c | 2010-06-25 16:50:05 -0400 | [diff] [blame] | 570 | XkbKeyGroupWidth(d->xkb, code, 0) > 1) |
| 571 | level = 1; |
Kristian Høgsberg | 99f090d | 2009-02-23 22:37:14 -0500 | [diff] [blame] | 572 | |
Kristian Høgsberg | 94adf6c | 2010-06-25 16:50:05 -0400 | [diff] [blame] | 573 | sym = XkbKeySymEntry(d->xkb, code, level, 0); |
| 574 | |
| 575 | if (state) |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 576 | input->modifiers |= d->xkb->map->modmap[code]; |
Kristian Høgsberg | 94adf6c | 2010-06-25 16:50:05 -0400 | [diff] [blame] | 577 | else |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 578 | input->modifiers &= ~d->xkb->map->modmap[code]; |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 579 | |
| 580 | if (window->key_handler) |
Kristian Høgsberg | 94adf6c | 2010-06-25 16:50:05 -0400 | [diff] [blame] | 581 | (*window->key_handler)(window, key, sym, state, |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 582 | input->modifiers, window->user_data); |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 583 | } |
| 584 | |
Kristian Høgsberg | db6c2f3 | 2009-02-22 21:51:24 -0500 | [diff] [blame] | 585 | static void |
| 586 | window_handle_pointer_focus(void *data, |
| 587 | struct wl_input_device *input_device, |
Kristian Høgsberg | 6d70202 | 2010-08-06 15:12:22 -0400 | [diff] [blame] | 588 | uint32_t time, struct wl_surface *surface, |
| 589 | int32_t x, int32_t y, int32_t sx, int32_t sy) |
Kristian Høgsberg | db6c2f3 | 2009-02-22 21:51:24 -0500 | [diff] [blame] | 590 | { |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 591 | struct input *input = data; |
| 592 | |
Kristian Høgsberg | da275dd | 2010-08-16 17:47:07 -0400 | [diff] [blame^] | 593 | if (surface) { |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 594 | input->pointer_focus = wl_surface_get_user_data(surface); |
Kristian Høgsberg | da275dd | 2010-08-16 17:47:07 -0400 | [diff] [blame^] | 595 | set_pointer_image(input); |
| 596 | } else { |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 597 | input->pointer_focus = NULL; |
Kristian Høgsberg | da275dd | 2010-08-16 17:47:07 -0400 | [diff] [blame^] | 598 | } |
Kristian Høgsberg | db6c2f3 | 2009-02-22 21:51:24 -0500 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | static void |
| 602 | window_handle_keyboard_focus(void *data, |
| 603 | struct wl_input_device *input_device, |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 604 | uint32_t time, |
Kristian Høgsberg | 3c38fa0 | 2009-02-23 22:30:29 -0500 | [diff] [blame] | 605 | struct wl_surface *surface, |
| 606 | struct wl_array *keys) |
Kristian Høgsberg | db6c2f3 | 2009-02-22 21:51:24 -0500 | [diff] [blame] | 607 | { |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 608 | struct input *input = data; |
| 609 | struct window *window = input->keyboard_focus; |
| 610 | struct display *d = input->display; |
Kristian Høgsberg | 99f090d | 2009-02-23 22:37:14 -0500 | [diff] [blame] | 611 | uint32_t *k, *end; |
Kristian Høgsberg | 3c248cc | 2009-02-22 23:01:35 -0500 | [diff] [blame] | 612 | |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 613 | window = input->keyboard_focus; |
| 614 | if (window) { |
Kristian Høgsberg | 3c248cc | 2009-02-22 23:01:35 -0500 | [diff] [blame] | 615 | window->keyboard_device = NULL; |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 616 | if (window->keyboard_focus_handler) |
| 617 | (*window->keyboard_focus_handler)(window, NULL, |
| 618 | window->user_data); |
Kristian Høgsberg | 99f090d | 2009-02-23 22:37:14 -0500 | [diff] [blame] | 619 | } |
| 620 | |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 621 | if (surface) |
| 622 | input->keyboard_focus = wl_surface_get_user_data(surface); |
| 623 | else |
| 624 | input->keyboard_focus = NULL; |
| 625 | |
| 626 | end = keys->data + keys->size; |
| 627 | for (k = keys->data; k < end; k++) |
| 628 | input->modifiers |= d->xkb->map->modmap[*k]; |
| 629 | |
| 630 | window = input->keyboard_focus; |
| 631 | if (window) { |
| 632 | window->keyboard_device = input; |
| 633 | if (window->keyboard_focus_handler) |
| 634 | (*window->keyboard_focus_handler)(window, |
| 635 | window->keyboard_device, |
| 636 | window->user_data); |
| 637 | } |
Kristian Høgsberg | db6c2f3 | 2009-02-22 21:51:24 -0500 | [diff] [blame] | 638 | } |
| 639 | |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 640 | static const struct wl_input_device_listener input_device_listener = { |
| 641 | window_handle_motion, |
| 642 | window_handle_button, |
| 643 | window_handle_key, |
Kristian Høgsberg | db6c2f3 | 2009-02-22 21:51:24 -0500 | [diff] [blame] | 644 | window_handle_pointer_focus, |
| 645 | window_handle_keyboard_focus, |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 646 | }; |
| 647 | |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 648 | static void |
| 649 | handle_configure(void *data, struct wl_shell *shell, |
| 650 | uint32_t time, uint32_t edges, |
| 651 | struct wl_surface *surface, |
| 652 | int32_t x, int32_t y, int32_t width, int32_t height) |
| 653 | { |
| 654 | struct window *window = wl_surface_get_user_data(surface); |
| 655 | |
| 656 | window->resize_edges = edges; |
| 657 | window->pending_allocation.x = x; |
| 658 | window->pending_allocation.y = y; |
| 659 | window->pending_allocation.width = width; |
| 660 | window->pending_allocation.height = height; |
| 661 | |
| 662 | if (!(edges & 15)) |
| 663 | return; |
| 664 | |
| 665 | if (window->resize_handler) |
| 666 | (*window->resize_handler)(window, |
| 667 | window->user_data); |
| 668 | else if (window->redraw_handler) |
| 669 | window_schedule_redraw(window); |
| 670 | } |
| 671 | |
| 672 | static const struct wl_shell_listener shell_listener = { |
| 673 | handle_configure, |
| 674 | }; |
| 675 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 676 | void |
| 677 | window_get_child_rectangle(struct window *window, |
| 678 | struct rectangle *rectangle) |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 679 | { |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 680 | if (window->fullscreen && !window->decoration) { |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 681 | *rectangle = window->allocation; |
| 682 | } else { |
| 683 | rectangle->x = window->margin + 10; |
| 684 | rectangle->y = window->margin + 50; |
| 685 | rectangle->width = window->allocation.width - 20 - window->margin * 2; |
| 686 | rectangle->height = window->allocation.height - 60 - window->margin * 2; |
| 687 | } |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | void |
Kristian Høgsberg | 2210676 | 2008-12-08 13:50:07 -0500 | [diff] [blame] | 691 | window_set_child_size(struct window *window, |
| 692 | struct rectangle *rectangle) |
| 693 | { |
Kristian Høgsberg | bf6ceda | 2010-06-14 20:25:06 -0400 | [diff] [blame] | 694 | int32_t width, height; |
| 695 | |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 696 | if (!window->fullscreen) { |
Kristian Høgsberg | bf6ceda | 2010-06-14 20:25:06 -0400 | [diff] [blame] | 697 | width = rectangle->width + 20 + window->margin * 2; |
| 698 | height = rectangle->height + 60 + window->margin * 2; |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 699 | |
| 700 | if (window->resize_edges & WINDOW_RESIZING_LEFT) |
| 701 | window->allocation.x += |
| 702 | window->allocation.width - width; |
| 703 | if (window->resize_edges & WINDOW_RESIZING_TOP) |
| 704 | window->allocation.y += |
| 705 | window->allocation.height - height; |
| 706 | |
Kristian Høgsberg | bf6ceda | 2010-06-14 20:25:06 -0400 | [diff] [blame] | 707 | window->allocation.width = width; |
| 708 | window->allocation.height = height; |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 709 | } |
Kristian Høgsberg | 2210676 | 2008-12-08 13:50:07 -0500 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | void |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 713 | window_copy_image(struct window *window, |
| 714 | struct rectangle *rectangle, EGLImageKHR image) |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 715 | { |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 716 | /* set image as read buffer, copy pixels or something... */ |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | void |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 720 | window_copy_surface(struct window *window, |
| 721 | struct rectangle *rectangle, |
| 722 | cairo_surface_t *surface) |
| 723 | { |
Kristian Høgsberg | 2aac302 | 2009-12-21 10:04:53 -0500 | [diff] [blame] | 724 | cairo_t *cr; |
| 725 | |
| 726 | cr = cairo_create (window->cairo_surface); |
| 727 | |
| 728 | cairo_set_source_surface (cr, |
| 729 | surface, |
| 730 | rectangle->x, rectangle->y); |
| 731 | |
| 732 | cairo_paint (cr); |
| 733 | cairo_destroy (cr); |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 734 | } |
| 735 | |
Kristian Høgsberg | 80d746f | 2010-06-14 23:52:50 -0400 | [diff] [blame] | 736 | static gboolean |
| 737 | idle_redraw(void *data) |
| 738 | { |
| 739 | struct window *window = data; |
| 740 | |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 741 | if (window->resize_edges) |
| 742 | window->allocation = window->pending_allocation; |
| 743 | |
Kristian Høgsberg | 80d746f | 2010-06-14 23:52:50 -0400 | [diff] [blame] | 744 | window->redraw_handler(window, window->user_data); |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 745 | |
Kristian Høgsberg | 80d746f | 2010-06-14 23:52:50 -0400 | [diff] [blame] | 746 | window->redraw_scheduled = 0; |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 747 | window->resize_edges = 0; |
Kristian Høgsberg | 80d746f | 2010-06-14 23:52:50 -0400 | [diff] [blame] | 748 | |
| 749 | return FALSE; |
| 750 | } |
| 751 | |
| 752 | void |
| 753 | window_schedule_redraw(struct window *window) |
| 754 | { |
| 755 | if (!window->redraw_scheduled) { |
| 756 | g_idle_add(idle_redraw, window); |
| 757 | window->redraw_scheduled = 1; |
| 758 | } |
| 759 | } |
| 760 | |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 761 | void |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 762 | window_set_fullscreen(struct window *window, int fullscreen) |
| 763 | { |
| 764 | window->fullscreen = fullscreen; |
| 765 | if (window->fullscreen) { |
| 766 | window->saved_allocation = window->allocation; |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 767 | window->allocation = window->display->screen_allocation; |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 768 | } else { |
| 769 | window->allocation = window->saved_allocation; |
| 770 | } |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | void |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 774 | window_set_decoration(struct window *window, int decoration) |
| 775 | { |
| 776 | window->decoration = decoration; |
| 777 | } |
| 778 | |
| 779 | void |
Kristian Høgsberg | c8c3734 | 2010-06-25 11:19:22 -0400 | [diff] [blame] | 780 | window_set_user_data(struct window *window, void *data) |
| 781 | { |
| 782 | window->user_data = data; |
| 783 | } |
| 784 | |
| 785 | void |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 786 | window_set_resize_handler(struct window *window, |
Kristian Høgsberg | c8c3734 | 2010-06-25 11:19:22 -0400 | [diff] [blame] | 787 | window_resize_handler_t handler) |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 788 | { |
| 789 | window->resize_handler = handler; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | void |
Kristian Høgsberg | 80d746f | 2010-06-14 23:52:50 -0400 | [diff] [blame] | 793 | window_set_redraw_handler(struct window *window, |
Kristian Høgsberg | c8c3734 | 2010-06-25 11:19:22 -0400 | [diff] [blame] | 794 | window_redraw_handler_t handler) |
Kristian Høgsberg | 80d746f | 2010-06-14 23:52:50 -0400 | [diff] [blame] | 795 | { |
| 796 | window->redraw_handler = handler; |
Kristian Høgsberg | 80d746f | 2010-06-14 23:52:50 -0400 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | void |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 800 | window_set_key_handler(struct window *window, |
Kristian Høgsberg | c8c3734 | 2010-06-25 11:19:22 -0400 | [diff] [blame] | 801 | window_key_handler_t handler) |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 802 | { |
| 803 | window->key_handler = handler; |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 804 | } |
| 805 | |
Kristian Høgsberg | 3c248cc | 2009-02-22 23:01:35 -0500 | [diff] [blame] | 806 | void |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame] | 807 | window_set_acknowledge_handler(struct window *window, |
Kristian Høgsberg | c8c3734 | 2010-06-25 11:19:22 -0400 | [diff] [blame] | 808 | window_acknowledge_handler_t handler) |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame] | 809 | { |
| 810 | window->acknowledge_handler = handler; |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | void |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 814 | window_set_frame_handler(struct window *window, |
Kristian Høgsberg | c8c3734 | 2010-06-25 11:19:22 -0400 | [diff] [blame] | 815 | window_frame_handler_t handler) |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 816 | { |
| 817 | window->frame_handler = handler; |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | void |
Kristian Høgsberg | 3c248cc | 2009-02-22 23:01:35 -0500 | [diff] [blame] | 821 | window_set_keyboard_focus_handler(struct window *window, |
Kristian Høgsberg | c8c3734 | 2010-06-25 11:19:22 -0400 | [diff] [blame] | 822 | window_keyboard_focus_handler_t handler) |
Kristian Høgsberg | 3c248cc | 2009-02-22 23:01:35 -0500 | [diff] [blame] | 823 | { |
| 824 | window->keyboard_focus_handler = handler; |
Kristian Høgsberg | 3c248cc | 2009-02-22 23:01:35 -0500 | [diff] [blame] | 825 | } |
| 826 | |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 827 | void |
| 828 | window_move(struct window *window, int32_t x, int32_t y) |
| 829 | { |
| 830 | window->allocation.x = x; |
| 831 | window->allocation.y = y; |
| 832 | |
| 833 | wl_surface_map(window->surface, |
| 834 | window->allocation.x - window->margin, |
| 835 | window->allocation.y - window->margin, |
| 836 | window->allocation.width, |
| 837 | window->allocation.height); |
| 838 | } |
| 839 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 840 | struct window * |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 841 | window_create(struct display *display, const char *title, |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 842 | int32_t x, int32_t y, int32_t width, int32_t height) |
| 843 | { |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 844 | struct window *window; |
| 845 | |
| 846 | window = malloc(sizeof *window); |
| 847 | if (window == NULL) |
| 848 | return NULL; |
| 849 | |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 850 | memset(window, 0, sizeof *window); |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 851 | window->display = display; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 852 | window->title = strdup(title); |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 853 | window->surface = wl_compositor_create_surface(display->compositor); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 854 | window->allocation.x = x; |
| 855 | window->allocation.y = y; |
| 856 | window->allocation.width = width; |
| 857 | window->allocation.height = height; |
| 858 | window->saved_allocation = window->allocation; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 859 | window->margin = 16; |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 860 | window->decoration = 1; |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 861 | |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 862 | wl_surface_set_user_data(window->surface, window); |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame] | 863 | wl_list_insert(display->window_list.prev, &window->link); |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 864 | |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 865 | return window; |
| 866 | } |
| 867 | |
| 868 | static void |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 869 | drm_handle_device(void *data, struct wl_drm *drm, const char *device) |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame] | 870 | { |
| 871 | struct display *d = data; |
| 872 | |
| 873 | d->device_name = strdup(device); |
| 874 | } |
| 875 | |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 876 | static void drm_handle_authenticated(void *data, struct wl_drm *drm) |
| 877 | { |
Kristian Høgsberg | 640609a | 2010-08-09 22:11:47 -0400 | [diff] [blame] | 878 | struct display *d = data; |
| 879 | |
| 880 | d->authenticated = 1; |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | static const struct wl_drm_listener drm_listener = { |
| 884 | drm_handle_device, |
| 885 | drm_handle_authenticated |
| 886 | }; |
| 887 | |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame] | 888 | static void |
| 889 | display_handle_acknowledge(void *data, |
| 890 | struct wl_compositor *compositor, |
| 891 | uint32_t key, uint32_t frame) |
| 892 | { |
| 893 | struct display *d = data; |
| 894 | struct window *window; |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame] | 895 | |
| 896 | /* The acknowledge event means that the server processed our |
| 897 | * last commit request and we can now safely free the old |
| 898 | * window buffer if we resized and render the next frame into |
| 899 | * our back buffer.. */ |
| 900 | wl_list_for_each(window, &d->window_list, link) { |
Kristian Høgsberg | 0953162 | 2010-06-14 23:22:15 -0400 | [diff] [blame] | 901 | cairo_surface_destroy(window->pending_surface); |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame] | 902 | window->pending_surface = NULL; |
Kristian Høgsberg | 0953162 | 2010-06-14 23:22:15 -0400 | [diff] [blame] | 903 | if (window->cairo_surface) |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame] | 904 | window_attach_surface(window); |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame] | 905 | if (window->acknowledge_handler) |
| 906 | (*window->acknowledge_handler)(window, key, frame, window->user_data); |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | static void |
| 911 | display_handle_frame(void *data, |
| 912 | struct wl_compositor *compositor, |
| 913 | uint32_t frame, uint32_t timestamp) |
| 914 | { |
| 915 | struct display *d = data; |
| 916 | struct window *window; |
| 917 | |
| 918 | wl_list_for_each(window, &d->window_list, link) { |
| 919 | if (window->frame_handler) |
| 920 | (*window->frame_handler)(window, frame, |
| 921 | timestamp, window->user_data); |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | static const struct wl_compositor_listener compositor_listener = { |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame] | 926 | display_handle_acknowledge, |
| 927 | display_handle_frame, |
| 928 | }; |
| 929 | |
| 930 | static void |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 931 | display_handle_geometry(void *data, |
| 932 | struct wl_output *output, |
| 933 | int32_t width, int32_t height) |
| 934 | { |
| 935 | struct display *display = data; |
| 936 | |
| 937 | display->screen_allocation.x = 0; |
| 938 | display->screen_allocation.y = 0; |
| 939 | display->screen_allocation.width = width; |
| 940 | display->screen_allocation.height = height; |
| 941 | } |
| 942 | |
| 943 | static const struct wl_output_listener output_listener = { |
| 944 | display_handle_geometry, |
| 945 | }; |
| 946 | |
| 947 | static void |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame] | 948 | display_add_input(struct display *d, uint32_t id) |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 949 | { |
| 950 | struct input *input; |
| 951 | |
| 952 | input = malloc(sizeof *input); |
| 953 | if (input == NULL) |
| 954 | return; |
| 955 | |
| 956 | memset(input, 0, sizeof *input); |
| 957 | input->display = d; |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame] | 958 | input->input_device = wl_input_device_create(d->display, id); |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 959 | input->pointer_focus = NULL; |
| 960 | input->keyboard_focus = NULL; |
| 961 | wl_list_insert(d->input_list.prev, &input->link); |
| 962 | |
| 963 | wl_input_device_add_listener(input->input_device, |
| 964 | &input_device_listener, input); |
| 965 | } |
| 966 | |
| 967 | static void |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame] | 968 | display_handle_global(struct wl_display *display, uint32_t id, |
| 969 | const char *interface, uint32_t version, void *data) |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 970 | { |
| 971 | struct display *d = data; |
| 972 | |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame] | 973 | if (strcmp(interface, "compositor") == 0) { |
| 974 | d->compositor = wl_compositor_create(display, id); |
| 975 | wl_compositor_add_listener(d->compositor, |
| 976 | &compositor_listener, d); |
| 977 | } else if (strcmp(interface, "output") == 0) { |
| 978 | d->output = wl_output_create(display, id); |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 979 | wl_output_add_listener(d->output, &output_listener, d); |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame] | 980 | } else if (strcmp(interface, "input_device") == 0) { |
| 981 | display_add_input(d, id); |
| 982 | } else if (strcmp(interface, "shell") == 0) { |
| 983 | d->shell = wl_shell_create(display, id); |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 984 | wl_shell_add_listener(d->shell, &shell_listener, d); |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame] | 985 | } else if (strcmp(interface, "drm") == 0) { |
| 986 | d->drm = wl_drm_create(display, id); |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 987 | wl_drm_add_listener(d->drm, &drm_listener, d); |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 988 | } |
| 989 | } |
| 990 | |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 991 | static const char socket_name[] = "\0wayland"; |
| 992 | |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 993 | static void |
| 994 | display_render_frame(struct display *d) |
| 995 | { |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 996 | int radius = 8; |
| 997 | cairo_t *cr; |
| 998 | |
| 999 | d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128); |
| 1000 | cr = cairo_create(d->shadow); |
| 1001 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 1002 | cairo_set_source_rgba(cr, 0, 0, 0, 1); |
| 1003 | rounded_rect(cr, 16, 16, 112, 112, radius); |
| 1004 | cairo_fill(cr); |
| 1005 | cairo_destroy(cr); |
| 1006 | blur_surface(d->shadow, 64); |
| 1007 | |
| 1008 | d->active_frame = |
| 1009 | cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128); |
| 1010 | cr = cairo_create(d->active_frame); |
| 1011 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 1012 | cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1); |
| 1013 | rounded_rect(cr, 16, 16, 112, 112, radius); |
| 1014 | cairo_fill(cr); |
| 1015 | cairo_destroy(cr); |
| 1016 | |
| 1017 | d->inactive_frame = |
| 1018 | cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128); |
| 1019 | cr = cairo_create(d->inactive_frame); |
| 1020 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 1021 | cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1); |
| 1022 | rounded_rect(cr, 16, 16, 112, 112, radius); |
| 1023 | cairo_fill(cr); |
| 1024 | cairo_destroy(cr); |
| 1025 | } |
| 1026 | |
Kristian Høgsberg | 94adf6c | 2010-06-25 16:50:05 -0400 | [diff] [blame] | 1027 | static void |
| 1028 | init_xkb(struct display *d) |
| 1029 | { |
Kristian Høgsberg | 3e6e7e6 | 2010-07-02 15:12:02 -0400 | [diff] [blame] | 1030 | struct xkb_rule_names names; |
Kristian Høgsberg | 94adf6c | 2010-06-25 16:50:05 -0400 | [diff] [blame] | 1031 | |
Kristian Høgsberg | 3e6e7e6 | 2010-07-02 15:12:02 -0400 | [diff] [blame] | 1032 | names.rules = "evdev"; |
| 1033 | names.model = "pc105"; |
| 1034 | names.layout = "us"; |
| 1035 | names.variant = ""; |
| 1036 | names.options = ""; |
Kristian Høgsberg | 94adf6c | 2010-06-25 16:50:05 -0400 | [diff] [blame] | 1037 | |
Kristian Høgsberg | 3e6e7e6 | 2010-07-02 15:12:02 -0400 | [diff] [blame] | 1038 | d->xkb = xkb_compile_keymap_from_rules(&names); |
Kristian Høgsberg | 94adf6c | 2010-06-25 16:50:05 -0400 | [diff] [blame] | 1039 | if (!d->xkb) { |
| 1040 | fprintf(stderr, "Failed to compile keymap\n"); |
| 1041 | exit(1); |
| 1042 | } |
| 1043 | } |
| 1044 | |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 1045 | struct display * |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 1046 | display_create(int *argc, char **argv[], const GOptionEntry *option_entries) |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 1047 | { |
| 1048 | struct display *d; |
Kristian Høgsberg | 379b678 | 2010-07-28 22:52:28 -0400 | [diff] [blame] | 1049 | EGLint major, minor; |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 1050 | int fd; |
| 1051 | GOptionContext *context; |
| 1052 | GError *error; |
Kristian Høgsberg | 640609a | 2010-08-09 22:11:47 -0400 | [diff] [blame] | 1053 | drm_magic_t magic; |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 1054 | |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame] | 1055 | g_type_init(); |
| 1056 | |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 1057 | context = g_option_context_new(NULL); |
| 1058 | if (option_entries) { |
| 1059 | g_option_context_add_main_entries(context, option_entries, "Wayland View"); |
| 1060 | if (!g_option_context_parse(context, argc, argv, &error)) { |
| 1061 | fprintf(stderr, "option parsing failed: %s\n", error->message); |
| 1062 | exit(EXIT_FAILURE); |
| 1063 | } |
| 1064 | } |
| 1065 | |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 1066 | d = malloc(sizeof *d); |
| 1067 | if (d == NULL) |
| 1068 | return NULL; |
| 1069 | |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame] | 1070 | d->display = wl_display_create(socket_name, sizeof socket_name); |
| 1071 | if (d->display == NULL) { |
| 1072 | fprintf(stderr, "failed to create display: %m\n"); |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 1073 | return NULL; |
| 1074 | } |
| 1075 | |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 1076 | wl_list_init(&d->input_list); |
| 1077 | |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame] | 1078 | /* Set up listener so we'll catch all events. */ |
| 1079 | wl_display_add_global_listener(d->display, |
| 1080 | display_handle_global, d); |
| 1081 | |
| 1082 | /* Process connection events. */ |
| 1083 | wl_display_iterate(d->display, WL_DISPLAY_READABLE); |
| 1084 | |
| 1085 | fd = open(d->device_name, O_RDWR); |
| 1086 | if (fd < 0) { |
| 1087 | fprintf(stderr, "drm open failed: %m\n"); |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 1088 | return NULL; |
| 1089 | } |
| 1090 | |
Kristian Høgsberg | 640609a | 2010-08-09 22:11:47 -0400 | [diff] [blame] | 1091 | if (drmGetMagic(fd, &magic)) { |
| 1092 | fprintf(stderr, "DRI2: failed to get drm magic"); |
| 1093 | return NULL; |
| 1094 | } |
| 1095 | |
| 1096 | /* Wait for authenticated event */ |
| 1097 | wl_drm_authenticate(d->drm, magic); |
| 1098 | wl_display_iterate(d->display, WL_DISPLAY_WRITABLE); |
| 1099 | while (!d->authenticated) |
| 1100 | wl_display_iterate(d->display, WL_DISPLAY_READABLE); |
| 1101 | |
Kristian Høgsberg | f252d6a | 2010-07-08 20:15:10 -0400 | [diff] [blame] | 1102 | d->dpy = eglGetDRMDisplayMESA(fd); |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 1103 | if (!eglInitialize(d->dpy, &major, &minor)) { |
| 1104 | fprintf(stderr, "failed to initialize display\n"); |
| 1105 | return NULL; |
| 1106 | } |
| 1107 | |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 1108 | eglBindAPI(EGL_OPENGL_API); |
| 1109 | |
Kristian Høgsberg | 379b678 | 2010-07-28 22:52:28 -0400 | [diff] [blame] | 1110 | d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL); |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 1111 | if (d->ctx == NULL) { |
| 1112 | fprintf(stderr, "failed to create context\n"); |
| 1113 | return NULL; |
| 1114 | } |
| 1115 | |
| 1116 | if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) { |
| 1117 | fprintf(stderr, "faile to make context current\n"); |
| 1118 | return NULL; |
| 1119 | } |
| 1120 | |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 1121 | d->device = cairo_egl_device_create(d->dpy, d->ctx); |
Kristian Høgsberg | 2644910 | 2009-05-28 20:23:31 -0400 | [diff] [blame] | 1122 | if (d->device == NULL) { |
| 1123 | fprintf(stderr, "failed to get cairo drm device\n"); |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 1124 | return NULL; |
| 1125 | } |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 1126 | |
Kristian Høgsberg | da275dd | 2010-08-16 17:47:07 -0400 | [diff] [blame^] | 1127 | create_pointer_images(d); |
| 1128 | |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 1129 | display_render_frame(d); |
| 1130 | |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 1131 | d->loop = g_main_loop_new(NULL, FALSE); |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame] | 1132 | d->source = wl_glib_source_new(d->display); |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 1133 | g_source_attach(d->source, NULL); |
| 1134 | |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame] | 1135 | wl_list_init(&d->window_list); |
| 1136 | |
Kristian Høgsberg | 94adf6c | 2010-06-25 16:50:05 -0400 | [diff] [blame] | 1137 | init_xkb(d); |
| 1138 | |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 1139 | return d; |
| 1140 | } |
| 1141 | |
| 1142 | struct wl_compositor * |
| 1143 | display_get_compositor(struct display *display) |
| 1144 | { |
| 1145 | return display->compositor; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 1146 | } |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 1147 | |
| 1148 | EGLDisplay |
| 1149 | display_get_egl_display(struct display *d) |
| 1150 | { |
| 1151 | return d->dpy; |
| 1152 | } |
| 1153 | |
| 1154 | void |
| 1155 | display_run(struct display *d) |
| 1156 | { |
| 1157 | g_main_loop_run(d->loop); |
| 1158 | } |