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