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