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