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