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