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 | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 34 | |
| 35 | #define EGL_EGLEXT_PROTOTYPES 1 |
| 36 | #define GL_GLEXT_PROTOTYPES 1 |
| 37 | #include <GL/gl.h> |
| 38 | #include <EGL/egl.h> |
| 39 | #include <EGL/eglext.h> |
| 40 | #include <cairo-gl.h> |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 41 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 42 | #include <linux/input.h> |
Kristian Høgsberg | 3c38fa0 | 2009-02-23 22:30:29 -0500 | [diff] [blame] | 43 | #include "wayland-util.h" |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 44 | #include "wayland-client.h" |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 45 | #include "wayland-glib.h" |
Kristian Høgsberg | f88ae45 | 2010-06-05 10:17:55 -0400 | [diff] [blame] | 46 | #include "../cairo-util.h" |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 47 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 48 | #include "window.h" |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 49 | |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 50 | struct display { |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 51 | struct wl_display *display; |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 52 | struct wl_compositor *compositor; |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 53 | struct wl_output *output; |
| 54 | struct wl_input_device *input_device; |
| 55 | struct rectangle screen_allocation; |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 56 | EGLDisplay dpy; |
| 57 | EGLContext ctx; |
Janusz Lewandowski | d923e9d | 2010-01-31 03:01:26 +0100 | [diff] [blame] | 58 | cairo_device_t *device; |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 59 | int fd; |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 60 | GMainLoop *loop; |
| 61 | GSource *source; |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame^] | 62 | struct wl_list window_list; |
| 63 | char *device_name; |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | struct window { |
| 67 | struct display *display; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 68 | struct wl_surface *surface; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 69 | const char *title; |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 70 | struct rectangle allocation, saved_allocation; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 71 | int minimum_width, minimum_height; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 72 | int margin; |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 73 | int drag_x, drag_y; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 74 | int state; |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 75 | int fullscreen; |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 76 | int decoration; |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 77 | struct wl_input_device *grab_device; |
Kristian Høgsberg | 3c248cc | 2009-02-22 23:01:35 -0500 | [diff] [blame] | 78 | struct wl_input_device *keyboard_device; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 79 | uint32_t name; |
Kristian Høgsberg | 5544491 | 2009-02-21 14:31:09 -0500 | [diff] [blame] | 80 | uint32_t modifiers; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 81 | |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 82 | EGLImageKHR *image; |
Kristian Høgsberg | 6a1b201 | 2009-12-16 14:43:37 -0500 | [diff] [blame] | 83 | cairo_surface_t *cairo_surface, *pending_surface; |
Kristian Høgsberg | a341fa0 | 2010-01-24 18:10:15 -0500 | [diff] [blame] | 84 | int new_surface; |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 85 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 86 | window_resize_handler_t resize_handler; |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 87 | window_key_handler_t key_handler; |
Kristian Høgsberg | 3c248cc | 2009-02-22 23:01:35 -0500 | [diff] [blame] | 88 | window_keyboard_focus_handler_t keyboard_focus_handler; |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame^] | 89 | window_acknowledge_handler_t acknowledge_handler; |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 90 | window_frame_handler_t frame_handler; |
| 91 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 92 | void *user_data; |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame^] | 93 | struct wl_list link; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 94 | }; |
| 95 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 96 | static void |
| 97 | rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius) |
| 98 | { |
| 99 | cairo_move_to(cr, x0, y0 + radius); |
| 100 | cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2); |
| 101 | cairo_line_to(cr, x1 - radius, y0); |
| 102 | cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI); |
| 103 | cairo_line_to(cr, x1, y1 - radius); |
| 104 | cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2); |
| 105 | cairo_line_to(cr, x0 + radius, y1); |
| 106 | cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI); |
| 107 | cairo_close_path(cr); |
| 108 | } |
| 109 | |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 110 | static const cairo_user_data_key_t surface_data_key; |
| 111 | struct surface_data { |
| 112 | EGLImageKHR image; |
| 113 | GLuint texture; |
| 114 | EGLDisplay dpy; |
| 115 | }; |
| 116 | |
| 117 | static void |
| 118 | surface_data_destroy(void *p) |
| 119 | { |
| 120 | struct surface_data *data = p; |
| 121 | |
| 122 | glDeleteTextures(1, &data->texture); |
| 123 | eglDestroyImageKHR(data->dpy, data->image); |
| 124 | } |
| 125 | |
| 126 | cairo_surface_t * |
| 127 | window_create_surface(struct window *window, |
| 128 | struct rectangle *rectangle) |
| 129 | { |
| 130 | struct surface_data *data; |
| 131 | EGLDisplay dpy = window->display->dpy; |
| 132 | cairo_surface_t *surface; |
| 133 | |
| 134 | EGLint image_attribs[] = { |
| 135 | EGL_WIDTH, 0, |
| 136 | EGL_HEIGHT, 0, |
| 137 | EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA, |
| 138 | EGL_IMAGE_USE_MESA, EGL_IMAGE_USE_SCANOUT_MESA, |
| 139 | EGL_NONE |
| 140 | }; |
| 141 | |
| 142 | data = malloc(sizeof *data); |
| 143 | image_attribs[1] = rectangle->width; |
| 144 | image_attribs[3] = rectangle->height; |
| 145 | data->image = eglCreateDRMImageMESA(dpy, image_attribs); |
| 146 | glGenTextures(1, &data->texture); |
| 147 | data->dpy = dpy; |
| 148 | glBindTexture(GL_TEXTURE_2D, data->texture); |
| 149 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image); |
| 150 | |
| 151 | surface = cairo_gl_surface_create_for_texture(window->display->device, |
| 152 | CAIRO_CONTENT_COLOR_ALPHA, |
| 153 | data->texture, |
| 154 | rectangle->width, |
| 155 | rectangle->height); |
| 156 | |
| 157 | cairo_surface_set_user_data (surface, &surface_data_key, |
| 158 | data, surface_data_destroy); |
| 159 | |
| 160 | return surface; |
| 161 | } |
| 162 | |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 163 | static void |
Kristian Høgsberg | 6a1b201 | 2009-12-16 14:43:37 -0500 | [diff] [blame] | 164 | window_attach_surface(struct window *window) |
| 165 | { |
| 166 | struct wl_visual *visual; |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 167 | struct surface_data *data; |
| 168 | EGLint name, stride; |
Kristian Høgsberg | 6a1b201 | 2009-12-16 14:43:37 -0500 | [diff] [blame] | 169 | |
Kristian Høgsberg | 2aac302 | 2009-12-21 10:04:53 -0500 | [diff] [blame] | 170 | if (window->pending_surface != NULL) |
| 171 | return; |
| 172 | |
| 173 | window->pending_surface = |
| 174 | cairo_surface_reference(window->cairo_surface); |
| 175 | |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 176 | data = cairo_surface_get_user_data (window->cairo_surface, &surface_data_key); |
| 177 | eglExportDRMImageMESA(window->display->dpy, data->image, &name, NULL, &stride); |
| 178 | |
Kristian Høgsberg | 6a1b201 | 2009-12-16 14:43:37 -0500 | [diff] [blame] | 179 | visual = wl_display_get_premultiplied_argb_visual(window->display->display); |
| 180 | wl_surface_attach(window->surface, |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 181 | name, |
Kristian Høgsberg | 6a1b201 | 2009-12-16 14:43:37 -0500 | [diff] [blame] | 182 | window->allocation.width, |
| 183 | window->allocation.height, |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 184 | stride, |
Kristian Høgsberg | 6a1b201 | 2009-12-16 14:43:37 -0500 | [diff] [blame] | 185 | visual); |
| 186 | |
| 187 | wl_surface_map(window->surface, |
| 188 | window->allocation.x - window->margin, |
| 189 | window->allocation.y - window->margin, |
| 190 | window->allocation.width, |
| 191 | window->allocation.height); |
| 192 | } |
| 193 | |
Kristian Høgsberg | a341fa0 | 2010-01-24 18:10:15 -0500 | [diff] [blame] | 194 | void |
| 195 | window_commit(struct window *window, uint32_t key) |
| 196 | { |
| 197 | if (window->new_surface) { |
| 198 | window_attach_surface(window); |
| 199 | window->new_surface = 0; |
| 200 | } |
| 201 | |
| 202 | wl_compositor_commit(window->display->compositor, key); |
| 203 | } |
| 204 | |
Kristian Høgsberg | 6a1b201 | 2009-12-16 14:43:37 -0500 | [diff] [blame] | 205 | static void |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 206 | window_draw_decorations(struct window *window) |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 207 | { |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 208 | cairo_t *cr; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 209 | int border = 2, radius = 5; |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 210 | cairo_text_extents_t extents; |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 211 | cairo_pattern_t *gradient, *outline, *bright, *dim; |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 212 | int width, height; |
Kristian Høgsberg | 7d7b5db | 2009-09-21 13:43:46 -0400 | [diff] [blame] | 213 | int shadow_dx = 4, shadow_dy = 4; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 214 | |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 215 | window->cairo_surface = |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 216 | window_create_surface(window, &window->allocation); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 217 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 218 | outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1); |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 219 | bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 220 | dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4); |
| 221 | |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 222 | cr = cairo_create(window->cairo_surface); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 223 | |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 224 | width = window->allocation.width - window->margin * 2; |
| 225 | height = window->allocation.height - window->margin * 2; |
| 226 | |
Kristian Høgsberg | 7d7b5db | 2009-09-21 13:43:46 -0400 | [diff] [blame] | 227 | cairo_translate(cr, window->margin + shadow_dx, |
| 228 | window->margin + shadow_dy); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 229 | cairo_set_line_width (cr, border); |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 230 | cairo_set_source_rgba(cr, 0, 0, 0, 0.7); |
Kristian Høgsberg | 7d7b5db | 2009-09-21 13:43:46 -0400 | [diff] [blame] | 231 | rounded_rect(cr, -1, -1, width + 1, height + 1, radius); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 232 | cairo_fill(cr); |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 233 | |
Kristian Høgsberg | f88ae45 | 2010-06-05 10:17:55 -0400 | [diff] [blame] | 234 | #define SLOW_BUT_PWETTY_not_right_now |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 235 | #ifdef SLOW_BUT_PWETTY |
| 236 | /* FIXME: Aw, pretty drop shadows now have to fallback to sw. |
| 237 | * Ideally we should have convolution filters in cairo, but we |
| 238 | * can also fallback to compositing the shadow image a bunch |
| 239 | * of times according to the blur kernel. */ |
| 240 | { |
| 241 | cairo_surface_t *map; |
| 242 | |
| 243 | map = cairo_drm_surface_map(window->cairo_surface); |
Kristian Høgsberg | 0acc6c4 | 2009-03-05 07:49:42 -0500 | [diff] [blame] | 244 | blur_surface(map, 32); |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 245 | cairo_drm_surface_unmap(window->cairo_surface, map); |
| 246 | } |
| 247 | #endif |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 248 | |
Kristian Høgsberg | 7d7b5db | 2009-09-21 13:43:46 -0400 | [diff] [blame] | 249 | cairo_translate(cr, -shadow_dx, -shadow_dy); |
| 250 | if (window->keyboard_device) { |
| 251 | rounded_rect(cr, 0, 0, width, height, radius); |
| 252 | gradient = cairo_pattern_create_linear (0, 0, 0, 100); |
| 253 | cairo_pattern_add_color_stop_rgb(gradient, 0, 0.6, 0.6, 0.6); |
| 254 | cairo_pattern_add_color_stop_rgb(gradient, 1, 0.8, 0.8, 0.8); |
| 255 | cairo_set_source(cr, gradient); |
| 256 | cairo_fill(cr); |
| 257 | cairo_pattern_destroy(gradient); |
| 258 | } else { |
| 259 | rounded_rect(cr, 0, 0, width, height, radius); |
| 260 | cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1); |
| 261 | cairo_fill(cr); |
| 262 | } |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 263 | |
| 264 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 265 | cairo_move_to(cr, 10, 50); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 266 | cairo_line_to(cr, width - 10, 50); |
| 267 | cairo_line_to(cr, width - 10, height - 10); |
| 268 | cairo_line_to(cr, 10, height - 10); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 269 | cairo_close_path(cr); |
| 270 | cairo_set_source(cr, dim); |
| 271 | cairo_stroke(cr); |
| 272 | |
| 273 | cairo_move_to(cr, 11, 51); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 274 | cairo_line_to(cr, width - 10, 51); |
| 275 | cairo_line_to(cr, width - 10, height - 10); |
| 276 | cairo_line_to(cr, 11, height - 10); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 277 | cairo_close_path(cr); |
| 278 | cairo_set_source(cr, bright); |
| 279 | cairo_stroke(cr); |
| 280 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 281 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 282 | cairo_set_font_size(cr, 14); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 283 | cairo_text_extents(cr, window->title, &extents); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 284 | cairo_move_to(cr, (width - extents.width) / 2, 10 - extents.y_bearing); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 285 | cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); |
| 286 | cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND); |
| 287 | cairo_set_line_width (cr, 4); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 288 | cairo_text_path(cr, window->title); |
Kristian Høgsberg | 7d7b5db | 2009-09-21 13:43:46 -0400 | [diff] [blame] | 289 | if (window->keyboard_device) { |
| 290 | cairo_set_source_rgb(cr, 0.56, 0.56, 0.56); |
| 291 | cairo_stroke_preserve(cr); |
| 292 | cairo_set_source_rgb(cr, 1, 1, 1); |
| 293 | cairo_fill(cr); |
| 294 | } else { |
| 295 | cairo_set_source_rgb(cr, 0.8, 0.8, 0.8); |
| 296 | cairo_fill(cr); |
| 297 | } |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 298 | cairo_destroy(cr); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | static void |
| 302 | window_draw_fullscreen(struct window *window) |
| 303 | { |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 304 | window->cairo_surface = |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 305 | window_create_surface(window, &window->allocation); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | void |
| 309 | window_draw(struct window *window) |
| 310 | { |
Kristian Høgsberg | 2aac302 | 2009-12-21 10:04:53 -0500 | [diff] [blame] | 311 | if (window->cairo_surface != NULL) |
| 312 | cairo_surface_destroy(window->cairo_surface); |
| 313 | |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 314 | if (window->fullscreen || !window->decoration) |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 315 | window_draw_fullscreen(window); |
| 316 | else |
| 317 | window_draw_decorations(window); |
Kristian Høgsberg | a341fa0 | 2010-01-24 18:10:15 -0500 | [diff] [blame] | 318 | |
| 319 | window->new_surface = 1; |
Kristian Høgsberg | 44f36e3 | 2008-11-26 12:57:31 -0500 | [diff] [blame] | 320 | } |
| 321 | |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 322 | cairo_surface_t * |
| 323 | window_get_surface(struct window *window) |
| 324 | { |
| 325 | return window->cairo_surface; |
| 326 | } |
| 327 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 328 | enum window_state { |
| 329 | WINDOW_STABLE, |
| 330 | WINDOW_MOVING, |
| 331 | WINDOW_RESIZING_UPPER_LEFT, |
| 332 | WINDOW_RESIZING_UPPER_RIGHT, |
| 333 | WINDOW_RESIZING_LOWER_LEFT, |
| 334 | WINDOW_RESIZING_LOWER_RIGHT |
| 335 | }; |
| 336 | |
| 337 | enum location { |
| 338 | LOCATION_INTERIOR, |
| 339 | LOCATION_UPPER_LEFT, |
| 340 | LOCATION_UPPER_RIGHT, |
| 341 | LOCATION_LOWER_LEFT, |
| 342 | LOCATION_LOWER_RIGHT, |
| 343 | LOCATION_OUTSIDE |
| 344 | }; |
| 345 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 346 | static void |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 347 | window_handle_motion(void *data, struct wl_input_device *input_device, |
| 348 | 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] | 349 | { |
| 350 | struct window *window = data; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 351 | |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 352 | switch (window->state) { |
| 353 | case WINDOW_MOVING: |
| 354 | if (window->fullscreen) |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 355 | break; |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 356 | if (window->grab_device != input_device) |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 357 | break; |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 358 | window->allocation.x = window->drag_x + x; |
| 359 | window->allocation.y = window->drag_y + y; |
| 360 | wl_surface_map(window->surface, |
| 361 | window->allocation.x - window->margin, |
| 362 | window->allocation.y - window->margin, |
| 363 | window->allocation.width, |
| 364 | window->allocation.height); |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 365 | wl_compositor_commit(window->display->compositor, 1); |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 366 | break; |
| 367 | case WINDOW_RESIZING_LOWER_RIGHT: |
| 368 | if (window->fullscreen) |
| 369 | break; |
| 370 | if (window->grab_device != input_device) |
| 371 | break; |
| 372 | window->allocation.width = window->drag_x + x; |
| 373 | window->allocation.height = window->drag_y + y; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 374 | |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 375 | if (window->resize_handler) |
| 376 | (*window->resize_handler)(window, |
| 377 | window->user_data); |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 378 | |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 379 | break; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 383 | static void window_handle_button(void *data, struct wl_input_device *input_device, |
| 384 | uint32_t button, uint32_t state, |
| 385 | int32_t x, int32_t y, int32_t sx, int32_t sy) |
| 386 | { |
| 387 | struct window *window = data; |
| 388 | int32_t left = window->allocation.x; |
| 389 | int32_t right = window->allocation.x + |
| 390 | window->allocation.width - window->margin * 2; |
| 391 | int32_t top = window->allocation.y; |
| 392 | int32_t bottom = window->allocation.y + |
| 393 | window->allocation.height - window->margin * 2; |
| 394 | int grip_size = 16, location; |
| 395 | |
| 396 | if (right - grip_size <= x && x < right && |
| 397 | bottom - grip_size <= y && y < bottom) { |
| 398 | location = LOCATION_LOWER_RIGHT; |
| 399 | } else if (left <= x && x < right && top <= y && y < bottom) { |
| 400 | location = LOCATION_INTERIOR; |
| 401 | } else { |
| 402 | location = LOCATION_OUTSIDE; |
| 403 | } |
| 404 | |
| 405 | if (button == BTN_LEFT && state == 1) { |
| 406 | switch (location) { |
| 407 | case LOCATION_INTERIOR: |
| 408 | window->drag_x = window->allocation.x - x; |
| 409 | window->drag_y = window->allocation.y - y; |
| 410 | window->state = WINDOW_MOVING; |
| 411 | window->grab_device = input_device; |
| 412 | break; |
| 413 | case LOCATION_LOWER_RIGHT: |
| 414 | window->drag_x = window->allocation.width - x; |
| 415 | window->drag_y = window->allocation.height - y; |
| 416 | window->state = WINDOW_RESIZING_LOWER_RIGHT; |
| 417 | window->grab_device = input_device; |
| 418 | break; |
| 419 | default: |
| 420 | window->state = WINDOW_STABLE; |
| 421 | break; |
| 422 | } |
| 423 | } else if (button == BTN_LEFT && |
| 424 | state == 0 && window->grab_device == input_device) { |
| 425 | window->state = WINDOW_STABLE; |
| 426 | } |
| 427 | } |
| 428 | |
Kristian Høgsberg | 5544491 | 2009-02-21 14:31:09 -0500 | [diff] [blame] | 429 | |
| 430 | struct key { |
| 431 | uint32_t code[4]; |
| 432 | } evdev_keymap[] = { |
| 433 | { { 0, 0 } }, /* 0 */ |
| 434 | { { 0x1b, 0x1b } }, |
| 435 | { { '1', '!' } }, |
| 436 | { { '2', '@' } }, |
| 437 | { { '3', '#' } }, |
| 438 | { { '4', '$' } }, |
| 439 | { { '5', '%' } }, |
| 440 | { { '6', '^' } }, |
| 441 | { { '7', '&' } }, |
| 442 | { { '8', '*' } }, |
| 443 | { { '9', '(' } }, |
| 444 | { { '0', ')' } }, |
| 445 | { { '-', '_' } }, |
| 446 | { { '=', '+' } }, |
| 447 | { { '\b', '\b' } }, |
| 448 | { { '\t', '\t' } }, |
| 449 | |
| 450 | { { 'q', 'Q', 0x11 } }, /* 16 */ |
| 451 | { { 'w', 'W', 0x17 } }, |
| 452 | { { 'e', 'E', 0x05 } }, |
| 453 | { { 'r', 'R', 0x12 } }, |
| 454 | { { 't', 'T', 0x14 } }, |
| 455 | { { 'y', 'Y', 0x19 } }, |
| 456 | { { 'u', 'U', 0x15 } }, |
| 457 | { { 'i', 'I', 0x09 } }, |
| 458 | { { 'o', 'O', 0x0f } }, |
| 459 | { { 'p', 'P', 0x10 } }, |
| 460 | { { '[', '{', 0x1b } }, |
| 461 | { { ']', '}', 0x1d } }, |
| 462 | { { '\n', '\n' } }, |
| 463 | { { 0, 0 } }, |
| 464 | { { 'a', 'A', 0x01} }, |
| 465 | { { 's', 'S', 0x13 } }, |
| 466 | |
| 467 | { { 'd', 'D', 0x04 } }, /* 32 */ |
| 468 | { { 'f', 'F', 0x06 } }, |
| 469 | { { 'g', 'G', 0x07 } }, |
| 470 | { { 'h', 'H', 0x08 } }, |
| 471 | { { 'j', 'J', 0x0a } }, |
| 472 | { { 'k', 'K', 0x0b } }, |
| 473 | { { 'l', 'L', 0x0c } }, |
| 474 | { { ';', ':' } }, |
| 475 | { { '\'', '"' } }, |
| 476 | { { '`', '~' } }, |
| 477 | { { 0, 0 } }, |
| 478 | { { '\\', '|', 0x1c } }, |
| 479 | { { 'z', 'Z', 0x1a } }, |
| 480 | { { 'x', 'X', 0x18 } }, |
| 481 | { { 'c', 'C', 0x03 } }, |
| 482 | { { 'v', 'V', 0x16 } }, |
| 483 | |
| 484 | { { 'b', 'B', 0x02 } }, /* 48 */ |
| 485 | { { 'n', 'N', 0x0e } }, |
| 486 | { { 'm', 'M', 0x0d } }, |
| 487 | { { ',', '<' } }, |
| 488 | { { '.', '>' } }, |
| 489 | { { '/', '?' } }, |
| 490 | { { 0, 0 } }, |
| 491 | { { '*', '*' } }, |
| 492 | { { 0, 0 } }, |
| 493 | { { ' ', ' ' } }, |
| 494 | { { 0, 0 } } |
| 495 | |
| 496 | /* 59 */ |
| 497 | }; |
| 498 | |
| 499 | #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0]) |
| 500 | |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 501 | static void |
Kristian Høgsberg | 99f090d | 2009-02-23 22:37:14 -0500 | [diff] [blame] | 502 | window_update_modifiers(struct window *window, uint32_t key, uint32_t state) |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 503 | { |
Kristian Høgsberg | 5544491 | 2009-02-21 14:31:09 -0500 | [diff] [blame] | 504 | uint32_t mod = 0; |
Kristian Høgsberg | 3c248cc | 2009-02-22 23:01:35 -0500 | [diff] [blame] | 505 | |
Kristian Høgsberg | 5544491 | 2009-02-21 14:31:09 -0500 | [diff] [blame] | 506 | switch (key) { |
| 507 | case KEY_LEFTSHIFT: |
| 508 | case KEY_RIGHTSHIFT: |
| 509 | mod = WINDOW_MODIFIER_SHIFT; |
| 510 | break; |
| 511 | case KEY_LEFTCTRL: |
| 512 | case KEY_RIGHTCTRL: |
| 513 | mod = WINDOW_MODIFIER_CONTROL; |
| 514 | break; |
| 515 | case KEY_LEFTALT: |
| 516 | case KEY_RIGHTALT: |
| 517 | mod = WINDOW_MODIFIER_ALT; |
| 518 | break; |
Kristian Høgsberg | 5544491 | 2009-02-21 14:31:09 -0500 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | if (state) |
| 522 | window->modifiers |= mod; |
| 523 | else |
| 524 | window->modifiers &= ~mod; |
Kristian Høgsberg | 99f090d | 2009-02-23 22:37:14 -0500 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | static void |
| 528 | window_handle_key(void *data, struct wl_input_device *input_device, |
| 529 | uint32_t key, uint32_t state) |
| 530 | { |
| 531 | struct window *window = data; |
| 532 | uint32_t unicode = 0; |
| 533 | |
| 534 | if (window->keyboard_device != input_device) |
| 535 | return; |
| 536 | |
| 537 | window_update_modifiers(window, key, state); |
| 538 | |
| 539 | if (key < ARRAY_LENGTH(evdev_keymap)) { |
| 540 | if (window->modifiers & WINDOW_MODIFIER_CONTROL) |
| 541 | unicode = evdev_keymap[key].code[2]; |
| 542 | else if (window->modifiers & WINDOW_MODIFIER_SHIFT) |
| 543 | unicode = evdev_keymap[key].code[1]; |
| 544 | else |
| 545 | unicode = evdev_keymap[key].code[0]; |
| 546 | } |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 547 | |
| 548 | if (window->key_handler) |
Kristian Høgsberg | 5544491 | 2009-02-21 14:31:09 -0500 | [diff] [blame] | 549 | (*window->key_handler)(window, key, unicode, |
| 550 | state, window->modifiers, window->user_data); |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 551 | } |
| 552 | |
Kristian Høgsberg | db6c2f3 | 2009-02-22 21:51:24 -0500 | [diff] [blame] | 553 | static void |
| 554 | window_handle_pointer_focus(void *data, |
| 555 | struct wl_input_device *input_device, |
| 556 | struct wl_surface *surface) |
| 557 | { |
| 558 | } |
| 559 | |
| 560 | static void |
| 561 | window_handle_keyboard_focus(void *data, |
| 562 | struct wl_input_device *input_device, |
Kristian Høgsberg | 3c38fa0 | 2009-02-23 22:30:29 -0500 | [diff] [blame] | 563 | struct wl_surface *surface, |
| 564 | struct wl_array *keys) |
Kristian Høgsberg | db6c2f3 | 2009-02-22 21:51:24 -0500 | [diff] [blame] | 565 | { |
Kristian Høgsberg | 3c248cc | 2009-02-22 23:01:35 -0500 | [diff] [blame] | 566 | struct window *window = data; |
Kristian Høgsberg | 99f090d | 2009-02-23 22:37:14 -0500 | [diff] [blame] | 567 | uint32_t *k, *end; |
Kristian Høgsberg | 3c248cc | 2009-02-22 23:01:35 -0500 | [diff] [blame] | 568 | |
| 569 | if (window->keyboard_device == input_device && surface != window->surface) |
| 570 | window->keyboard_device = NULL; |
| 571 | else if (window->keyboard_device == NULL && surface == window->surface) |
| 572 | window->keyboard_device = input_device; |
| 573 | else |
| 574 | return; |
| 575 | |
Kristian Høgsberg | 99f090d | 2009-02-23 22:37:14 -0500 | [diff] [blame] | 576 | if (window->keyboard_device) { |
| 577 | end = keys->data + keys->size; |
| 578 | for (k = keys->data; k < end; k++) |
| 579 | window_update_modifiers(window, *k, 1); |
| 580 | } else { |
| 581 | window->modifiers = 0; |
| 582 | } |
| 583 | |
Kristian Høgsberg | 3c248cc | 2009-02-22 23:01:35 -0500 | [diff] [blame] | 584 | if (window->keyboard_focus_handler) |
| 585 | (*window->keyboard_focus_handler)(window, |
| 586 | window->keyboard_device, |
| 587 | window->user_data); |
Kristian Høgsberg | db6c2f3 | 2009-02-22 21:51:24 -0500 | [diff] [blame] | 588 | } |
| 589 | |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 590 | static const struct wl_input_device_listener input_device_listener = { |
| 591 | window_handle_motion, |
| 592 | window_handle_button, |
| 593 | window_handle_key, |
Kristian Høgsberg | db6c2f3 | 2009-02-22 21:51:24 -0500 | [diff] [blame] | 594 | window_handle_pointer_focus, |
| 595 | window_handle_keyboard_focus, |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 596 | }; |
| 597 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 598 | void |
| 599 | window_get_child_rectangle(struct window *window, |
| 600 | struct rectangle *rectangle) |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 601 | { |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 602 | if (window->fullscreen && !window->decoration) { |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 603 | *rectangle = window->allocation; |
| 604 | } else { |
| 605 | rectangle->x = window->margin + 10; |
| 606 | rectangle->y = window->margin + 50; |
| 607 | rectangle->width = window->allocation.width - 20 - window->margin * 2; |
| 608 | rectangle->height = window->allocation.height - 60 - window->margin * 2; |
| 609 | } |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | void |
Kristian Høgsberg | 2210676 | 2008-12-08 13:50:07 -0500 | [diff] [blame] | 613 | window_set_child_size(struct window *window, |
| 614 | struct rectangle *rectangle) |
| 615 | { |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 616 | if (!window->fullscreen) { |
| 617 | window->allocation.width = rectangle->width + 20 + window->margin * 2; |
| 618 | window->allocation.height = rectangle->height + 60 + window->margin * 2; |
| 619 | } |
Kristian Høgsberg | 2210676 | 2008-12-08 13:50:07 -0500 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | void |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 623 | window_copy_image(struct window *window, |
| 624 | struct rectangle *rectangle, EGLImageKHR image) |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 625 | { |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 626 | /* set image as read buffer, copy pixels or something... */ |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | void |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 630 | window_copy_surface(struct window *window, |
| 631 | struct rectangle *rectangle, |
| 632 | cairo_surface_t *surface) |
| 633 | { |
Kristian Høgsberg | 2aac302 | 2009-12-21 10:04:53 -0500 | [diff] [blame] | 634 | cairo_t *cr; |
| 635 | |
| 636 | cr = cairo_create (window->cairo_surface); |
| 637 | |
| 638 | cairo_set_source_surface (cr, |
| 639 | surface, |
| 640 | rectangle->x, rectangle->y); |
| 641 | |
| 642 | cairo_paint (cr); |
| 643 | cairo_destroy (cr); |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | void |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 647 | window_set_fullscreen(struct window *window, int fullscreen) |
| 648 | { |
| 649 | window->fullscreen = fullscreen; |
| 650 | if (window->fullscreen) { |
| 651 | window->saved_allocation = window->allocation; |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 652 | window->allocation = window->display->screen_allocation; |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 653 | } else { |
| 654 | window->allocation = window->saved_allocation; |
| 655 | } |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | void |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 659 | window_set_decoration(struct window *window, int decoration) |
| 660 | { |
| 661 | window->decoration = decoration; |
| 662 | } |
| 663 | |
| 664 | void |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 665 | window_set_resize_handler(struct window *window, |
| 666 | window_resize_handler_t handler, void *data) |
| 667 | { |
| 668 | window->resize_handler = handler; |
| 669 | window->user_data = data; |
| 670 | } |
| 671 | |
| 672 | void |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 673 | window_set_key_handler(struct window *window, |
| 674 | window_key_handler_t handler, void *data) |
| 675 | { |
| 676 | window->key_handler = handler; |
| 677 | window->user_data = data; |
| 678 | } |
| 679 | |
Kristian Høgsberg | 3c248cc | 2009-02-22 23:01:35 -0500 | [diff] [blame] | 680 | void |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame^] | 681 | window_set_acknowledge_handler(struct window *window, |
| 682 | window_acknowledge_handler_t handler, void *data) |
| 683 | { |
| 684 | window->acknowledge_handler = handler; |
| 685 | window->user_data = data; |
| 686 | } |
| 687 | |
| 688 | void |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 689 | window_set_frame_handler(struct window *window, |
| 690 | window_frame_handler_t handler, void *data) |
| 691 | { |
| 692 | window->frame_handler = handler; |
| 693 | window->user_data = data; |
| 694 | } |
| 695 | |
| 696 | void |
Kristian Høgsberg | 3c248cc | 2009-02-22 23:01:35 -0500 | [diff] [blame] | 697 | window_set_keyboard_focus_handler(struct window *window, |
| 698 | window_keyboard_focus_handler_t handler, void *data) |
| 699 | { |
| 700 | window->keyboard_focus_handler = handler; |
| 701 | window->user_data = data; |
| 702 | } |
| 703 | |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 704 | void |
| 705 | window_move(struct window *window, int32_t x, int32_t y) |
| 706 | { |
| 707 | window->allocation.x = x; |
| 708 | window->allocation.y = y; |
| 709 | |
| 710 | wl_surface_map(window->surface, |
| 711 | window->allocation.x - window->margin, |
| 712 | window->allocation.y - window->margin, |
| 713 | window->allocation.width, |
| 714 | window->allocation.height); |
| 715 | } |
| 716 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 717 | struct window * |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 718 | window_create(struct display *display, const char *title, |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 719 | int32_t x, int32_t y, int32_t width, int32_t height) |
| 720 | { |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 721 | struct window *window; |
| 722 | |
| 723 | window = malloc(sizeof *window); |
| 724 | if (window == NULL) |
| 725 | return NULL; |
| 726 | |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 727 | memset(window, 0, sizeof *window); |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 728 | window->display = display; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 729 | window->title = strdup(title); |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 730 | window->surface = wl_compositor_create_surface(display->compositor); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 731 | window->allocation.x = x; |
| 732 | window->allocation.y = y; |
| 733 | window->allocation.width = width; |
| 734 | window->allocation.height = height; |
| 735 | window->saved_allocation = window->allocation; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 736 | window->margin = 16; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 737 | window->state = WINDOW_STABLE; |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 738 | window->decoration = 1; |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 739 | |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame^] | 740 | wl_list_insert(display->window_list.prev, &window->link); |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 741 | |
| 742 | wl_input_device_add_listener(display->input_device, |
| 743 | &input_device_listener, window); |
| 744 | |
| 745 | return window; |
| 746 | } |
| 747 | |
| 748 | static void |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame^] | 749 | display_handle_device(void *data, |
| 750 | struct wl_compositor *compositor, |
| 751 | const char *device) |
| 752 | { |
| 753 | struct display *d = data; |
| 754 | |
| 755 | d->device_name = strdup(device); |
| 756 | } |
| 757 | |
| 758 | static void |
| 759 | display_handle_acknowledge(void *data, |
| 760 | struct wl_compositor *compositor, |
| 761 | uint32_t key, uint32_t frame) |
| 762 | { |
| 763 | struct display *d = data; |
| 764 | struct window *window; |
| 765 | cairo_surface_t *pending; |
| 766 | |
| 767 | /* The acknowledge event means that the server processed our |
| 768 | * last commit request and we can now safely free the old |
| 769 | * window buffer if we resized and render the next frame into |
| 770 | * our back buffer.. */ |
| 771 | wl_list_for_each(window, &d->window_list, link) { |
| 772 | pending = window->pending_surface; |
| 773 | window->pending_surface = NULL; |
| 774 | if (pending != window->cairo_surface) |
| 775 | window_attach_surface(window); |
| 776 | cairo_surface_destroy(pending); |
| 777 | if (window->acknowledge_handler) |
| 778 | (*window->acknowledge_handler)(window, key, frame, window->user_data); |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | static void |
| 783 | display_handle_frame(void *data, |
| 784 | struct wl_compositor *compositor, |
| 785 | uint32_t frame, uint32_t timestamp) |
| 786 | { |
| 787 | struct display *d = data; |
| 788 | struct window *window; |
| 789 | |
| 790 | wl_list_for_each(window, &d->window_list, link) { |
| 791 | if (window->frame_handler) |
| 792 | (*window->frame_handler)(window, frame, |
| 793 | timestamp, window->user_data); |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | static const struct wl_compositor_listener compositor_listener = { |
| 798 | display_handle_device, |
| 799 | display_handle_acknowledge, |
| 800 | display_handle_frame, |
| 801 | }; |
| 802 | |
| 803 | static void |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 804 | display_handle_geometry(void *data, |
| 805 | struct wl_output *output, |
| 806 | int32_t width, int32_t height) |
| 807 | { |
| 808 | struct display *display = data; |
| 809 | |
| 810 | display->screen_allocation.x = 0; |
| 811 | display->screen_allocation.y = 0; |
| 812 | display->screen_allocation.width = width; |
| 813 | display->screen_allocation.height = height; |
| 814 | } |
| 815 | |
| 816 | static const struct wl_output_listener output_listener = { |
| 817 | display_handle_geometry, |
| 818 | }; |
| 819 | |
| 820 | static void |
| 821 | display_handle_global(struct wl_display *display, |
| 822 | struct wl_object *object, void *data) |
| 823 | { |
| 824 | struct display *d = data; |
| 825 | |
| 826 | if (wl_object_implements(object, "compositor", 1)) { |
| 827 | d->compositor = (struct wl_compositor *) object; |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame^] | 828 | wl_compositor_add_listener(d->compositor, &compositor_listener, d); |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 829 | } else if (wl_object_implements(object, "output", 1)) { |
| 830 | d->output = (struct wl_output *) object; |
| 831 | wl_output_add_listener(d->output, &output_listener, d); |
| 832 | } else if (wl_object_implements(object, "input_device", 1)) { |
| 833 | d->input_device =(struct wl_input_device *) object; |
| 834 | } |
| 835 | } |
| 836 | |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 837 | static const char socket_name[] = "\0wayland"; |
| 838 | |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 839 | struct display * |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 840 | display_create(int *argc, char **argv[], const GOptionEntry *option_entries) |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 841 | { |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 842 | PFNEGLGETTYPEDDISPLAYMESA get_typed_display_mesa; |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 843 | struct display *d; |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 844 | EGLint major, minor, count; |
| 845 | EGLConfig config; |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 846 | int fd; |
| 847 | GOptionContext *context; |
| 848 | GError *error; |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 849 | |
| 850 | static const EGLint config_attribs[] = { |
| 851 | EGL_SURFACE_TYPE, 0, |
| 852 | EGL_NO_SURFACE_CAPABLE_MESA, EGL_OPENGL_BIT, |
| 853 | EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, |
| 854 | EGL_NONE |
| 855 | }; |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 856 | |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame^] | 857 | g_type_init(); |
| 858 | |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 859 | context = g_option_context_new(NULL); |
| 860 | if (option_entries) { |
| 861 | g_option_context_add_main_entries(context, option_entries, "Wayland View"); |
| 862 | if (!g_option_context_parse(context, argc, argv, &error)) { |
| 863 | fprintf(stderr, "option parsing failed: %s\n", error->message); |
| 864 | exit(EXIT_FAILURE); |
| 865 | } |
| 866 | } |
| 867 | |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 868 | d = malloc(sizeof *d); |
| 869 | if (d == NULL) |
| 870 | return NULL; |
| 871 | |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame^] | 872 | d->display = wl_display_create(socket_name, sizeof socket_name); |
| 873 | if (d->display == NULL) { |
| 874 | fprintf(stderr, "failed to create display: %m\n"); |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 875 | return NULL; |
| 876 | } |
| 877 | |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame^] | 878 | /* Set up listener so we'll catch all events. */ |
| 879 | wl_display_add_global_listener(d->display, |
| 880 | display_handle_global, d); |
| 881 | |
| 882 | /* Process connection events. */ |
| 883 | wl_display_iterate(d->display, WL_DISPLAY_READABLE); |
| 884 | |
| 885 | fd = open(d->device_name, O_RDWR); |
| 886 | if (fd < 0) { |
| 887 | fprintf(stderr, "drm open failed: %m\n"); |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 888 | return NULL; |
| 889 | } |
| 890 | |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 891 | get_typed_display_mesa = |
| 892 | (PFNEGLGETTYPEDDISPLAYMESA) eglGetProcAddress("eglGetTypedDisplayMESA"); |
| 893 | if (get_typed_display_mesa == NULL) { |
| 894 | fprintf(stderr, "eglGetDisplayMESA() not found\n"); |
| 895 | return NULL; |
| 896 | } |
| 897 | |
| 898 | d->dpy = get_typed_display_mesa(EGL_DRM_DISPLAY_TYPE_MESA, |
| 899 | (void *) fd); |
| 900 | if (!eglInitialize(d->dpy, &major, &minor)) { |
| 901 | fprintf(stderr, "failed to initialize display\n"); |
| 902 | return NULL; |
| 903 | } |
| 904 | |
| 905 | if (!eglChooseConfig(d->dpy, config_attribs, &config, 1, &count) || |
| 906 | count == 0) { |
| 907 | fprintf(stderr, "eglChooseConfig() failed\n"); |
| 908 | return NULL; |
| 909 | } |
| 910 | |
| 911 | eglBindAPI(EGL_OPENGL_API); |
| 912 | |
| 913 | d->ctx = eglCreateContext(d->dpy, config, EGL_NO_CONTEXT, NULL); |
| 914 | if (d->ctx == NULL) { |
| 915 | fprintf(stderr, "failed to create context\n"); |
| 916 | return NULL; |
| 917 | } |
| 918 | |
| 919 | if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) { |
| 920 | fprintf(stderr, "faile to make context current\n"); |
| 921 | return NULL; |
| 922 | } |
| 923 | |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 924 | d->device = cairo_egl_device_create(d->dpy, d->ctx); |
Kristian Høgsberg | 2644910 | 2009-05-28 20:23:31 -0400 | [diff] [blame] | 925 | if (d->device == NULL) { |
| 926 | fprintf(stderr, "failed to get cairo drm device\n"); |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 927 | return NULL; |
| 928 | } |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 929 | |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 930 | d->loop = g_main_loop_new(NULL, FALSE); |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame^] | 931 | d->source = wl_glib_source_new(d->display); |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 932 | g_source_attach(d->source, NULL); |
| 933 | |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame^] | 934 | wl_list_init(&d->window_list); |
| 935 | |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 936 | return d; |
| 937 | } |
| 938 | |
| 939 | struct wl_compositor * |
| 940 | display_get_compositor(struct display *display) |
| 941 | { |
| 942 | return display->compositor; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 943 | } |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 944 | |
| 945 | EGLDisplay |
| 946 | display_get_egl_display(struct display *d) |
| 947 | { |
| 948 | return d->dpy; |
| 949 | } |
| 950 | |
| 951 | void |
| 952 | display_run(struct display *d) |
| 953 | { |
| 954 | g_main_loop_run(d->loop); |
| 955 | } |