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