Kristian Høgsberg | ffd710e | 2008-12-02 15:15:01 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008 Kristian Høgsberg |
| 3 | * |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
Kristian Høgsberg | ffd710e | 2008-12-02 15:15:01 -0500 | [diff] [blame] | 8 | * |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software Foundation, |
| 16 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
Kristian Høgsberg | ffd710e | 2008-12-02 15:15:01 -0500 | [diff] [blame] | 17 | */ |
| 18 | |
Kristian Høgsberg | 06bc264 | 2010-12-01 09:50:16 -0500 | [diff] [blame] | 19 | #define _GNU_SOURCE |
| 20 | |
Kristian Høgsberg | a941022 | 2011-01-14 17:22:35 -0500 | [diff] [blame] | 21 | #include "config.h" |
| 22 | |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <stdint.h> |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 27 | #include <limits.h> |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 28 | #include <stdarg.h> |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 29 | #include <sys/ioctl.h> |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 30 | #include <fcntl.h> |
| 31 | #include <unistd.h> |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 32 | #include <gdk-pixbuf/gdk-pixbuf.h> |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 33 | #include <math.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 34 | #include <linux/input.h> |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 35 | |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 36 | #include "wayland-server.h" |
Kristian Høgsberg | 8286302 | 2010-06-04 21:52:02 -0400 | [diff] [blame] | 37 | #include "compositor.h" |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 38 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 39 | /* The plan here is to generate a random anonymous socket name and |
| 40 | * advertise that through a service on the session dbus. |
| 41 | */ |
Kristian Høgsberg | 2bb3ebe | 2010-12-01 15:36:20 -0500 | [diff] [blame] | 42 | static const char *option_socket_name = NULL; |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 43 | static const char *option_background = "background.jpg"; |
| 44 | static const char *option_geometry = "1024x640"; |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 45 | static int option_idle_time = 5; |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 46 | static int option_connector = 0; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 47 | |
| 48 | static const GOptionEntry option_entries[] = { |
| 49 | { "background", 'b', 0, G_OPTION_ARG_STRING, |
| 50 | &option_background, "Background image" }, |
Kristian Høgsberg | f5878fa | 2009-09-18 17:02:41 -0400 | [diff] [blame] | 51 | { "connector", 'c', 0, G_OPTION_ARG_INT, |
| 52 | &option_connector, "KMS connector" }, |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 53 | { "geometry", 'g', 0, G_OPTION_ARG_STRING, |
| 54 | &option_geometry, "Geometry" }, |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 55 | { "socket", 's', 0, G_OPTION_ARG_STRING, |
| 56 | &option_socket_name, "Socket Name" }, |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 57 | { "idle-time", 'i', 0, G_OPTION_ARG_INT, |
| 58 | &option_idle_time, "Screensaver idle time" }, |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 59 | { NULL } |
| 60 | }; |
| 61 | |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 62 | static void |
| 63 | wlsc_matrix_init(struct wlsc_matrix *matrix) |
| 64 | { |
| 65 | static const struct wlsc_matrix identity = { |
| 66 | { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 } |
| 67 | }; |
| 68 | |
| 69 | memcpy(matrix, &identity, sizeof identity); |
| 70 | } |
| 71 | |
| 72 | static void |
| 73 | wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n) |
| 74 | { |
| 75 | struct wlsc_matrix tmp; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 76 | const GLfloat *row, *column; |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 77 | div_t d; |
| 78 | int i, j; |
| 79 | |
| 80 | for (i = 0; i < 16; i++) { |
| 81 | tmp.d[i] = 0; |
| 82 | d = div(i, 4); |
| 83 | row = m->d + d.quot * 4; |
| 84 | column = n->d + d.rem; |
| 85 | for (j = 0; j < 4; j++) |
| 86 | tmp.d[i] += row[j] * column[j * 4]; |
| 87 | } |
| 88 | memcpy(m, &tmp, sizeof tmp); |
| 89 | } |
| 90 | |
| 91 | static void |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 92 | wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z) |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 93 | { |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 94 | struct wlsc_matrix translate = { |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 95 | { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 } |
| 96 | }; |
| 97 | |
| 98 | wlsc_matrix_multiply(matrix, &translate); |
| 99 | } |
| 100 | |
| 101 | static void |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 102 | wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z) |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 103 | { |
| 104 | struct wlsc_matrix scale = { |
| 105 | { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 } |
| 106 | }; |
| 107 | |
| 108 | wlsc_matrix_multiply(matrix, &scale); |
| 109 | } |
| 110 | |
| 111 | static void |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 112 | wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v) |
| 113 | { |
| 114 | int i, j; |
Kristian Høgsberg | 0b8646b | 2010-06-08 15:29:14 -0400 | [diff] [blame] | 115 | struct wlsc_vector t; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 116 | |
| 117 | for (i = 0; i < 4; i++) { |
Kristian Høgsberg | 0b8646b | 2010-06-08 15:29:14 -0400 | [diff] [blame] | 118 | t.f[i] = 0; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 119 | for (j = 0; j < 4; j++) |
Kristian Høgsberg | 0b8646b | 2010-06-08 15:29:14 -0400 | [diff] [blame] | 120 | t.f[i] += v->f[j] * matrix->d[i + j * 4]; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 121 | } |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 122 | |
Kristian Høgsberg | 0b8646b | 2010-06-08 15:29:14 -0400 | [diff] [blame] | 123 | *v = t; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 124 | } |
| 125 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 126 | struct wlsc_surface * |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 127 | wlsc_surface_create(struct wlsc_compositor *compositor, |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 128 | int32_t x, int32_t y, int32_t width, int32_t height) |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 129 | { |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 130 | struct wlsc_surface *surface; |
| 131 | |
| 132 | surface = malloc(sizeof *surface); |
| 133 | if (surface == NULL) |
| 134 | return NULL; |
| 135 | |
Kristian Høgsberg | c551bd2 | 2010-12-06 16:43:16 -0500 | [diff] [blame] | 136 | wl_list_init(&surface->surface.destroy_listener_list); |
Kristian Høgsberg | f6b1471 | 2011-01-06 15:32:14 -0500 | [diff] [blame] | 137 | wl_list_init(&surface->link); |
Benjamin Franzke | bab41fb | 2011-03-07 18:04:59 +0100 | [diff] [blame] | 138 | wl_list_init(&surface->buffer_link); |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 139 | surface->map_type = WLSC_SURFACE_MAP_UNMAPPED; |
Kristian Høgsberg | c551bd2 | 2010-12-06 16:43:16 -0500 | [diff] [blame] | 140 | |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 141 | glGenTextures(1, &surface->texture); |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 142 | glBindTexture(GL_TEXTURE_2D, surface->texture); |
| 143 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 144 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 145 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 146 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 147 | |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 148 | surface->compositor = compositor; |
Kristian Høgsberg | c5d6be9 | 2011-01-14 16:22:37 -0500 | [diff] [blame] | 149 | surface->visual = NULL; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 150 | surface->image = EGL_NO_IMAGE_KHR; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 151 | surface->saved_texture = 0; |
Benjamin Franzke | bab41fb | 2011-03-07 18:04:59 +0100 | [diff] [blame] | 152 | surface->buffer = NULL; |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 153 | surface->x = x; |
| 154 | surface->y = y; |
| 155 | surface->width = width; |
| 156 | surface->height = height; |
Kristian Høgsberg | 8da19ac | 2009-03-17 16:12:51 -0400 | [diff] [blame] | 157 | wlsc_matrix_init(&surface->matrix); |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 158 | wlsc_matrix_scale(&surface->matrix, width, height, 1); |
| 159 | wlsc_matrix_translate(&surface->matrix, x, y, 0); |
| 160 | |
| 161 | wlsc_matrix_init(&surface->matrix_inv); |
| 162 | wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0); |
| 163 | wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1); |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 164 | |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 165 | return surface; |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 166 | } |
| 167 | |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 168 | void |
| 169 | wlsc_surface_damage_rectangle(struct wlsc_surface *surface, |
| 170 | int32_t x, int32_t y, |
| 171 | int32_t width, int32_t height) |
| 172 | { |
| 173 | struct wlsc_compositor *compositor = surface->compositor; |
| 174 | |
| 175 | pixman_region32_union_rect(&compositor->damage_region, |
| 176 | &compositor->damage_region, |
| 177 | surface->x + x, surface->y + y, |
| 178 | width, height); |
| 179 | wlsc_compositor_schedule_repaint(compositor); |
| 180 | } |
| 181 | |
| 182 | void |
| 183 | wlsc_surface_damage(struct wlsc_surface *surface) |
| 184 | { |
| 185 | wlsc_surface_damage_rectangle(surface, 0, 0, |
| 186 | surface->width, surface->height); |
| 187 | } |
| 188 | |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 189 | uint32_t |
Kristian Høgsberg | 7132a9a | 2010-12-06 21:41:10 -0500 | [diff] [blame] | 190 | get_time(void) |
| 191 | { |
| 192 | struct timeval tv; |
| 193 | |
| 194 | gettimeofday(&tv, NULL); |
| 195 | |
| 196 | return tv.tv_sec * 1000 + tv.tv_usec / 1000; |
| 197 | } |
| 198 | |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 199 | static void |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 200 | destroy_surface(struct wl_resource *resource, struct wl_client *client) |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 201 | { |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 202 | struct wlsc_surface *surface = |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 203 | container_of(resource, struct wlsc_surface, surface.resource); |
Kristian Høgsberg | c551bd2 | 2010-12-06 16:43:16 -0500 | [diff] [blame] | 204 | struct wl_listener *l, *next; |
Kristian Høgsberg | 4685fa3 | 2010-12-06 21:38:50 -0500 | [diff] [blame] | 205 | uint32_t time; |
Kristian Høgsberg | 4fa4873 | 2009-03-10 23:17:00 -0400 | [diff] [blame] | 206 | |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 207 | wlsc_surface_damage(surface); |
| 208 | |
Kristian Høgsberg | 3f8f39c | 2009-09-18 17:05:13 -0400 | [diff] [blame] | 209 | wl_list_remove(&surface->link); |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 210 | if (surface->saved_texture == 0) |
| 211 | glDeleteTextures(1, &surface->texture); |
| 212 | else |
| 213 | glDeleteTextures(1, &surface->saved_texture); |
| 214 | |
Kristian Høgsberg | 3f8f39c | 2009-09-18 17:05:13 -0400 | [diff] [blame] | 215 | |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 216 | if (surface->image != EGL_NO_IMAGE_KHR) |
| 217 | eglDestroyImageKHR(surface->compositor->display, |
| 218 | surface->image); |
| 219 | |
Benjamin Franzke | bab41fb | 2011-03-07 18:04:59 +0100 | [diff] [blame] | 220 | wl_list_remove(&surface->buffer_link); |
| 221 | |
Kristian Høgsberg | 7132a9a | 2010-12-06 21:41:10 -0500 | [diff] [blame] | 222 | time = get_time(); |
Kristian Høgsberg | c551bd2 | 2010-12-06 16:43:16 -0500 | [diff] [blame] | 223 | wl_list_for_each_safe(l, next, |
| 224 | &surface->surface.destroy_listener_list, link) |
Kristian Høgsberg | 4685fa3 | 2010-12-06 21:38:50 -0500 | [diff] [blame] | 225 | l->func(l, &surface->surface, time); |
Kristian Høgsberg | 4fa4873 | 2009-03-10 23:17:00 -0400 | [diff] [blame] | 226 | |
Kristian Høgsberg | 4fa4873 | 2009-03-10 23:17:00 -0400 | [diff] [blame] | 227 | free(surface); |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 228 | } |
| 229 | |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 230 | uint32_t * |
| 231 | wlsc_load_image(const char *filename, int width, int height) |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 232 | { |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 233 | GdkPixbuf *pixbuf; |
| 234 | GError *error = NULL; |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 235 | int stride, i, n_channels; |
| 236 | unsigned char *pixels, *end, *argb_pixels, *s, *d; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 237 | |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 238 | pixbuf = gdk_pixbuf_new_from_file_at_scale(filename, |
| 239 | width, height, |
| 240 | FALSE, &error); |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 241 | if (error != NULL) { |
| 242 | fprintf(stderr, "failed to load image: %s\n", error->message); |
| 243 | g_error_free(error); |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 244 | return NULL; |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 245 | } |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 246 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 247 | stride = gdk_pixbuf_get_rowstride(pixbuf); |
| 248 | pixels = gdk_pixbuf_get_pixels(pixbuf); |
| 249 | n_channels = gdk_pixbuf_get_n_channels(pixbuf); |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 250 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 251 | argb_pixels = malloc (height * width * 4); |
| 252 | if (argb_pixels == NULL) { |
Darxus@chaosreigns.com | c4df99c | 2011-01-25 15:00:56 -0500 | [diff] [blame] | 253 | g_object_unref(pixbuf); |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 254 | return NULL; |
| 255 | } |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 256 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 257 | if (n_channels == 4) { |
| 258 | for (i = 0; i < height; i++) { |
| 259 | s = pixels + i * stride; |
| 260 | end = s + width * 4; |
| 261 | d = argb_pixels + i * width * 4; |
| 262 | while (s < end) { |
| 263 | unsigned int t; |
| 264 | |
| 265 | #define MULT(_d,c,a,t) \ |
| 266 | do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0) |
| 267 | |
| 268 | MULT(d[0], s[2], s[3], t); |
| 269 | MULT(d[1], s[1], s[3], t); |
| 270 | MULT(d[2], s[0], s[3], t); |
| 271 | d[3] = s[3]; |
| 272 | s += 4; |
| 273 | d += 4; |
| 274 | } |
| 275 | } |
| 276 | } else if (n_channels == 3) { |
| 277 | for (i = 0; i < height; i++) { |
| 278 | s = pixels + i * stride; |
| 279 | end = s + width * 3; |
| 280 | d = argb_pixels + i * width * 4; |
| 281 | while (s < end) { |
| 282 | d[0] = s[2]; |
| 283 | d[1] = s[1]; |
| 284 | d[2] = s[0]; |
| 285 | d[3] = 0xff; |
| 286 | s += 3; |
| 287 | d += 4; |
| 288 | } |
| 289 | } |
| 290 | } |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 291 | |
Darxus@chaosreigns.com | c4df99c | 2011-01-25 15:00:56 -0500 | [diff] [blame] | 292 | g_object_unref(pixbuf); |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 293 | |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 294 | return (uint32_t *) argb_pixels; |
| 295 | } |
| 296 | |
Benjamin Franzke | faa0a9d | 2011-02-21 16:24:53 +0100 | [diff] [blame] | 297 | static void |
| 298 | wlsc_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface) |
| 299 | { |
| 300 | struct wlsc_surface *es = (struct wlsc_surface *) surface; |
| 301 | struct wlsc_compositor *ec = es->compositor; |
Benjamin Franzke | 315b3dc | 2011-03-08 11:32:57 +0100 | [diff] [blame] | 302 | struct wl_list *surfaces_attached_to; |
Benjamin Franzke | faa0a9d | 2011-02-21 16:24:53 +0100 | [diff] [blame] | 303 | |
Benjamin Franzke | 315b3dc | 2011-03-08 11:32:57 +0100 | [diff] [blame] | 304 | if (es->saved_texture != 0) |
| 305 | es->texture = es->saved_texture; |
| 306 | |
| 307 | glBindTexture(GL_TEXTURE_2D, es->texture); |
| 308 | |
| 309 | if (wl_buffer_is_shm(buffer)) { |
| 310 | /* Unbind any EGLImage texture that may be bound, so we don't |
| 311 | * overwrite it.*/ |
| 312 | glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, |
| 313 | 0, 0, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL); |
| 314 | glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, |
| 315 | buffer->width, buffer->height, 0, |
| 316 | GL_BGRA_EXT, GL_UNSIGNED_BYTE, |
| 317 | wl_shm_buffer_get_data(buffer)); |
| 318 | es->visual = buffer->visual; |
| 319 | |
| 320 | surfaces_attached_to = buffer->user_data; |
| 321 | |
| 322 | if (es->buffer) |
| 323 | wl_list_remove(&es->buffer_link); |
| 324 | wl_list_insert(surfaces_attached_to, &es->buffer_link); |
Benjamin Franzke | faa0a9d | 2011-02-21 16:24:53 +0100 | [diff] [blame] | 325 | } else { |
Kristian Høgsberg | df2f197 | 2011-04-21 23:48:13 -0400 | [diff] [blame] | 326 | es->image = eglCreateImageKHR(ec->display, NULL, |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 327 | EGL_WAYLAND_BUFFER_WL, |
| 328 | buffer, NULL); |
Benjamin Franzke | faa0a9d | 2011-02-21 16:24:53 +0100 | [diff] [blame] | 329 | |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 330 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, es->image); |
Benjamin Franzke | faa0a9d | 2011-02-21 16:24:53 +0100 | [diff] [blame] | 331 | es->visual = buffer->visual; |
Benjamin Franzke | faa0a9d | 2011-02-21 16:24:53 +0100 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 335 | static void |
| 336 | wlsc_sprite_attach(struct wlsc_sprite *sprite, struct wl_surface *surface) |
| 337 | { |
| 338 | struct wlsc_surface *es = (struct wlsc_surface *) surface; |
| 339 | |
| 340 | es->image = sprite->image; |
| 341 | if (sprite->image != EGL_NO_IMAGE_KHR) { |
| 342 | glBindTexture(GL_TEXTURE_2D, es->texture); |
| 343 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, es->image); |
| 344 | } else { |
| 345 | if (es->saved_texture == 0) |
| 346 | es->saved_texture = es->texture; |
| 347 | es->texture = sprite->texture; |
| 348 | } |
| 349 | |
| 350 | es->visual = sprite->visual; |
| 351 | } |
| 352 | |
| 353 | enum sprite_usage { |
| 354 | SPRITE_USE_CURSOR = (1 << 0), |
| 355 | }; |
| 356 | |
| 357 | static struct wlsc_sprite * |
| 358 | create_sprite_from_png(struct wlsc_compositor *ec, |
| 359 | const char *filename, int width, int height, |
| 360 | uint32_t usage) |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 361 | { |
| 362 | uint32_t *pixels; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 363 | struct wlsc_sprite *sprite; |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 364 | |
| 365 | pixels = wlsc_load_image(filename, width, height); |
Tim Wiederhake | ac5c5e7 | 2011-01-27 01:32:36 +0100 | [diff] [blame] | 366 | if(pixels == NULL) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 367 | return NULL; |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 368 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 369 | sprite = malloc(sizeof *sprite); |
| 370 | if (sprite == NULL) { |
| 371 | free(pixels); |
| 372 | return NULL; |
| 373 | } |
| 374 | |
| 375 | sprite->visual = &ec->compositor.premultiplied_argb_visual; |
| 376 | sprite->width = width; |
| 377 | sprite->height = height; |
| 378 | sprite->image = EGL_NO_IMAGE_KHR; |
| 379 | |
| 380 | if (usage & SPRITE_USE_CURSOR && ec->create_cursor_image != NULL) |
| 381 | sprite->image = ec->create_cursor_image(ec, width, height); |
| 382 | |
| 383 | glGenTextures(1, &sprite->texture); |
| 384 | glBindTexture(GL_TEXTURE_2D, sprite->texture); |
| 385 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 386 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 387 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 388 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 389 | |
| 390 | if (sprite->image != EGL_NO_IMAGE_KHR) { |
| 391 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, sprite->image); |
| 392 | glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, |
| 393 | GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels); |
| 394 | } else { |
| 395 | glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, width, height, 0, |
| 396 | GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels); |
| 397 | } |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 398 | |
Kristian Høgsberg | f58d8ca | 2011-01-26 14:37:07 -0500 | [diff] [blame] | 399 | free(pixels); |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 400 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 401 | return sprite; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 402 | } |
| 403 | |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 404 | static const struct { |
| 405 | const char *filename; |
| 406 | int hotspot_x, hotspot_y; |
| 407 | } pointer_images[] = { |
Kristian Høgsberg | 4219a40 | 2010-08-16 16:43:03 -0400 | [diff] [blame] | 408 | { DATADIR "/wayland/bottom_left_corner.png", 6, 30 }, |
| 409 | { DATADIR "/wayland/bottom_right_corner.png", 28, 28 }, |
| 410 | { DATADIR "/wayland/bottom_side.png", 16, 20 }, |
| 411 | { DATADIR "/wayland/grabbing.png", 20, 17 }, |
| 412 | { DATADIR "/wayland/left_ptr.png", 10, 5 }, |
| 413 | { DATADIR "/wayland/left_side.png", 10, 20 }, |
| 414 | { DATADIR "/wayland/right_side.png", 30, 19 }, |
| 415 | { DATADIR "/wayland/top_left_corner.png", 8, 8 }, |
| 416 | { DATADIR "/wayland/top_right_corner.png", 26, 8 }, |
| 417 | { DATADIR "/wayland/top_side.png", 18, 8 }, |
| 418 | { DATADIR "/wayland/xterm.png", 15, 15 } |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 419 | }; |
| 420 | |
| 421 | static void |
| 422 | create_pointer_images(struct wlsc_compositor *ec) |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 423 | { |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 424 | int i, count; |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 425 | const int width = 32, height = 32; |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 426 | |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 427 | count = ARRAY_LENGTH(pointer_images); |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 428 | ec->pointer_sprites = malloc(count * sizeof *ec->pointer_sprites); |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 429 | for (i = 0; i < count; i++) { |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 430 | ec->pointer_sprites[i] = |
| 431 | create_sprite_from_png(ec, |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 432 | pointer_images[i].filename, |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 433 | width, height, |
| 434 | SPRITE_USE_CURSOR); |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 435 | } |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 436 | } |
| 437 | |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 438 | static struct wlsc_surface * |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 439 | background_create(struct wlsc_output *output, const char *filename) |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 440 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 441 | struct wlsc_surface *background; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 442 | struct wlsc_sprite *sprite; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 443 | |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 444 | background = wlsc_surface_create(output->compositor, |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 445 | output->x, output->y, |
| 446 | output->width, output->height); |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 447 | if (background == NULL) |
| 448 | return NULL; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 449 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 450 | sprite = create_sprite_from_png(output->compositor, filename, |
| 451 | output->width, output->height, 0); |
| 452 | if (sprite == NULL) { |
Benjamin Franzke | d3b023e | 2011-01-15 12:34:49 +0100 | [diff] [blame] | 453 | free(background); |
| 454 | return NULL; |
| 455 | } |
Benjamin Franzke | faa0a9d | 2011-02-21 16:24:53 +0100 | [diff] [blame] | 456 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 457 | wlsc_sprite_attach(sprite, &background->surface); |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 458 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 459 | return background; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | static void |
Kristian Høgsberg | 525e4c0 | 2011-02-14 10:39:54 -0500 | [diff] [blame] | 463 | wlsc_surface_draw(struct wlsc_surface *es, |
| 464 | struct wlsc_output *output, pixman_region32_t *clip) |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 465 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 466 | struct wlsc_compositor *ec = es->compositor; |
Kristian Høgsberg | 525e4c0 | 2011-02-14 10:39:54 -0500 | [diff] [blame] | 467 | GLfloat *v, inv_width, inv_height; |
| 468 | unsigned int *p; |
| 469 | pixman_region32_t repaint; |
| 470 | pixman_box32_t *rectangles; |
| 471 | int i, n; |
| 472 | |
| 473 | pixman_region32_init_rect(&repaint, |
| 474 | es->x, es->y, es->width, es->height); |
| 475 | pixman_region32_intersect(&repaint, &repaint, clip); |
| 476 | if (!pixman_region32_not_empty(&repaint)) |
| 477 | return; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 478 | |
Kristian Høgsberg | c5c510e | 2010-12-08 15:12:58 -0500 | [diff] [blame] | 479 | if (es->visual == &ec->compositor.argb_visual) { |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 480 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 481 | glEnable(GL_BLEND); |
Kristian Høgsberg | c5c510e | 2010-12-08 15:12:58 -0500 | [diff] [blame] | 482 | } else if (es->visual == &ec->compositor.premultiplied_argb_visual) { |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 483 | glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); |
| 484 | glEnable(GL_BLEND); |
| 485 | } else { |
| 486 | glDisable(GL_BLEND); |
| 487 | } |
| 488 | |
Kristian Høgsberg | 525e4c0 | 2011-02-14 10:39:54 -0500 | [diff] [blame] | 489 | rectangles = pixman_region32_rectangles(&repaint, &n); |
| 490 | v = wl_array_add(&ec->vertices, n * 16 * sizeof *v); |
| 491 | p = wl_array_add(&ec->indices, n * 6 * sizeof *p); |
| 492 | inv_width = 1.0 / es->width; |
| 493 | inv_height = 1.0 / es->height; |
| 494 | for (i = 0; i < n; i++, v += 16, p += 6) { |
| 495 | v[ 0] = rectangles[i].x1; |
| 496 | v[ 1] = rectangles[i].y1; |
| 497 | v[ 2] = (GLfloat) (rectangles[i].x1 - es->x) * inv_width; |
| 498 | v[ 3] = (GLfloat) (rectangles[i].y1 - es->y) * inv_height; |
Kristian Høgsberg | fa4e2a7 | 2011-02-13 13:44:55 -0500 | [diff] [blame] | 499 | |
Kristian Høgsberg | 525e4c0 | 2011-02-14 10:39:54 -0500 | [diff] [blame] | 500 | v[ 4] = rectangles[i].x1; |
| 501 | v[ 5] = rectangles[i].y2; |
| 502 | v[ 6] = v[ 2]; |
| 503 | v[ 7] = (GLfloat) (rectangles[i].y2 - es->y) * inv_height; |
Kristian Høgsberg | fa4e2a7 | 2011-02-13 13:44:55 -0500 | [diff] [blame] | 504 | |
Kristian Høgsberg | 525e4c0 | 2011-02-14 10:39:54 -0500 | [diff] [blame] | 505 | v[ 8] = rectangles[i].x2; |
| 506 | v[ 9] = rectangles[i].y1; |
| 507 | v[10] = (GLfloat) (rectangles[i].x2 - es->x) * inv_width; |
| 508 | v[11] = v[ 3]; |
Kristian Høgsberg | fa4e2a7 | 2011-02-13 13:44:55 -0500 | [diff] [blame] | 509 | |
Kristian Høgsberg | 525e4c0 | 2011-02-14 10:39:54 -0500 | [diff] [blame] | 510 | v[12] = rectangles[i].x2; |
| 511 | v[13] = rectangles[i].y2; |
| 512 | v[14] = v[10]; |
| 513 | v[15] = v[ 7]; |
| 514 | |
| 515 | p[0] = i * 4 + 0; |
| 516 | p[1] = i * 4 + 1; |
| 517 | p[2] = i * 4 + 2; |
| 518 | p[3] = i * 4 + 2; |
| 519 | p[4] = i * 4 + 1; |
| 520 | p[5] = i * 4 + 3; |
| 521 | } |
Kristian Høgsberg | fa4e2a7 | 2011-02-13 13:44:55 -0500 | [diff] [blame] | 522 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 523 | glBindTexture(GL_TEXTURE_2D, es->texture); |
Kristian Høgsberg | 525e4c0 | 2011-02-14 10:39:54 -0500 | [diff] [blame] | 524 | v = ec->vertices.data; |
| 525 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]); |
| 526 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]); |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 527 | glEnableVertexAttribArray(0); |
| 528 | glEnableVertexAttribArray(1); |
Kristian Høgsberg | 525e4c0 | 2011-02-14 10:39:54 -0500 | [diff] [blame] | 529 | glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data); |
| 530 | |
| 531 | ec->vertices.size = 0; |
| 532 | ec->indices.size = 0; |
| 533 | pixman_region32_fini(&repaint); |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | static void |
Kristian Høgsberg | 8da19ac | 2009-03-17 16:12:51 -0400 | [diff] [blame] | 537 | wlsc_surface_raise(struct wlsc_surface *surface) |
| 538 | { |
| 539 | struct wlsc_compositor *compositor = surface->compositor; |
| 540 | |
| 541 | wl_list_remove(&surface->link); |
Kristian Høgsberg | 747638b | 2010-07-12 17:06:06 -0400 | [diff] [blame] | 542 | wl_list_insert(&compositor->surface_list, &surface->link); |
Kristian Høgsberg | 8da19ac | 2009-03-17 16:12:51 -0400 | [diff] [blame] | 543 | } |
| 544 | |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 545 | void |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 546 | wlsc_surface_update_matrix(struct wlsc_surface *es) |
| 547 | { |
| 548 | wlsc_matrix_init(&es->matrix); |
| 549 | wlsc_matrix_scale(&es->matrix, es->width, es->height, 1); |
| 550 | wlsc_matrix_translate(&es->matrix, es->x, es->y, 0); |
| 551 | |
| 552 | wlsc_matrix_init(&es->matrix_inv); |
| 553 | wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0); |
| 554 | wlsc_matrix_scale(&es->matrix_inv, |
| 555 | 1.0 / es->width, 1.0 / es->height, 1); |
| 556 | } |
| 557 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 558 | void |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 559 | wlsc_compositor_damage_all(struct wlsc_compositor *compositor) |
| 560 | { |
| 561 | struct wlsc_output *output; |
| 562 | |
| 563 | wl_list_for_each(output, &compositor->output_list, link) |
| 564 | wlsc_output_damage(output); |
| 565 | } |
| 566 | |
| 567 | void |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 568 | wlsc_output_finish_frame(struct wlsc_output *output, int msecs) |
Kristian Høgsberg | 01f941b | 2009-05-27 17:47:15 -0400 | [diff] [blame] | 569 | { |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 570 | struct wlsc_compositor *compositor = output->compositor; |
| 571 | struct wlsc_surface *es; |
| 572 | |
| 573 | wl_list_for_each(es, &compositor->surface_list, link) { |
| 574 | if (es->output == output) { |
| 575 | wl_display_post_frame(compositor->wl_display, |
| 576 | &es->surface, msecs); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | output->finished = 1; |
| 581 | |
Kristian Høgsberg | 5d312db | 2009-09-12 16:57:02 -0400 | [diff] [blame] | 582 | wl_event_source_timer_update(compositor->timer_source, 5); |
Kristian Høgsberg | f30c67e | 2011-02-06 12:58:44 -0500 | [diff] [blame] | 583 | compositor->repaint_on_timeout = 1; |
Kristian Høgsberg | 01f941b | 2009-05-27 17:47:15 -0400 | [diff] [blame] | 584 | } |
| 585 | |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 586 | void |
| 587 | wlsc_output_damage(struct wlsc_output *output) |
| 588 | { |
| 589 | struct wlsc_compositor *compositor = output->compositor; |
| 590 | |
| 591 | pixman_region32_union_rect(&compositor->damage_region, |
| 592 | &compositor->damage_region, |
| 593 | output->x, output->y, |
| 594 | output->width, output->height); |
| 595 | wlsc_compositor_schedule_repaint(compositor); |
| 596 | } |
| 597 | |
Kristian Høgsberg | 01f941b | 2009-05-27 17:47:15 -0400 | [diff] [blame] | 598 | static void |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 599 | wlsc_output_repaint(struct wlsc_output *output) |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 600 | { |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 601 | struct wlsc_compositor *ec = output->compositor; |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 602 | struct wlsc_surface *es; |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 603 | struct wlsc_input_device *eid; |
Kristian Høgsberg | cfc6d27 | 2011-04-11 13:34:24 -0400 | [diff] [blame] | 604 | pixman_region32_t new_damage, total_damage, repaint; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 605 | int using_hardware_cursor = 1; |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 606 | |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 607 | output->prepare_render(output); |
| 608 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 609 | glViewport(0, 0, output->width, output->height); |
Kristian Høgsberg | fdec236 | 2001-01-01 22:23:51 -0500 | [diff] [blame] | 610 | |
Kristian Høgsberg | 7ffc448 | 2011-04-22 14:23:51 -0400 | [diff] [blame^] | 611 | glUseProgram(ec->texture_shader.program); |
| 612 | glUniformMatrix4fv(ec->texture_shader.proj_uniform, |
| 613 | 1, GL_FALSE, output->matrix.d); |
| 614 | glUniform1i(ec->texture_shader.tex_uniform, 0); |
Kristian Høgsberg | fa4e2a7 | 2011-02-13 13:44:55 -0500 | [diff] [blame] | 615 | |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 616 | pixman_region32_init(&new_damage); |
| 617 | pixman_region32_init(&total_damage); |
| 618 | pixman_region32_intersect_rect(&new_damage, |
| 619 | &ec->damage_region, |
| 620 | output->x, output->y, |
| 621 | output->width, output->height); |
| 622 | pixman_region32_subtract(&ec->damage_region, |
| 623 | &ec->damage_region, &new_damage); |
| 624 | pixman_region32_union(&total_damage, &new_damage, |
| 625 | &output->previous_damage_region); |
| 626 | pixman_region32_copy(&output->previous_damage_region, &new_damage); |
| 627 | |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 628 | if (ec->state == WLSC_COMPOSITOR_SLEEPING) { |
| 629 | glClear(GL_COLOR_BUFFER_BIT); |
| 630 | return; |
| 631 | } |
| 632 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 633 | if (ec->focus) |
| 634 | if (output->set_hardware_cursor(output, ec->input_device) < 0) |
| 635 | using_hardware_cursor = 0; |
| 636 | |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 637 | es = container_of(ec->surface_list.next, struct wlsc_surface, link); |
| 638 | if (es->map_type == WLSC_SURFACE_MAP_FULLSCREEN && |
| 639 | es->fullscreen_output == output) { |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 640 | if (es->visual == &ec->compositor.rgb_visual && |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 641 | using_hardware_cursor) { |
| 642 | if (output->prepare_scanout_surface(output, es) == 0) { |
| 643 | /* We're drawing nothing now, |
| 644 | * draw the damaged regions later. */ |
| 645 | pixman_region32_union(&ec->damage_region, |
| 646 | &ec->damage_region, |
| 647 | &total_damage); |
| 648 | return; |
| 649 | } |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 650 | } |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 651 | |
| 652 | if (es->width < output->width || |
| 653 | es->height < output->height) |
| 654 | glClear(GL_COLOR_BUFFER_BIT); |
| 655 | wlsc_surface_draw(es, output, &total_damage); |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 656 | } else { |
Kristian Høgsberg | cfc6d27 | 2011-04-11 13:34:24 -0400 | [diff] [blame] | 657 | wl_list_for_each(es, &ec->surface_list, link) { |
| 658 | if (es->visual != &ec->compositor.rgb_visual) |
| 659 | continue; |
| 660 | |
| 661 | pixman_region32_init_rect(&repaint, |
| 662 | es->x, es->y, |
| 663 | es->width, es->height); |
| 664 | wlsc_surface_draw(es, output, &total_damage); |
| 665 | pixman_region32_subtract(&total_damage, |
| 666 | &total_damage, &repaint); |
| 667 | } |
| 668 | |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 669 | if (output->background) |
Kristian Høgsberg | 525e4c0 | 2011-02-14 10:39:54 -0500 | [diff] [blame] | 670 | wlsc_surface_draw(output->background, |
| 671 | output, &total_damage); |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 672 | else |
| 673 | glClear(GL_COLOR_BUFFER_BIT); |
| 674 | |
Kristian Høgsberg | c9824dd | 2011-02-06 16:54:59 -0500 | [diff] [blame] | 675 | wl_list_for_each_reverse(es, &ec->surface_list, link) { |
Kristian Høgsberg | 547cadf | 2011-04-12 22:23:30 -0400 | [diff] [blame] | 676 | if (ec->overlay == es) |
Kristian Høgsberg | c9824dd | 2011-02-06 16:54:59 -0500 | [diff] [blame] | 677 | continue; |
| 678 | |
Kristian Høgsberg | cfc6d27 | 2011-04-11 13:34:24 -0400 | [diff] [blame] | 679 | if (es->visual == &ec->compositor.rgb_visual) { |
| 680 | pixman_region32_union_rect(&total_damage, |
| 681 | &total_damage, |
| 682 | es->x, es->y, |
| 683 | es->width, es->height); |
| 684 | continue; |
| 685 | } |
| 686 | |
Kristian Høgsberg | 525e4c0 | 2011-02-14 10:39:54 -0500 | [diff] [blame] | 687 | wlsc_surface_draw(es, output, &total_damage); |
Kristian Høgsberg | c9824dd | 2011-02-06 16:54:59 -0500 | [diff] [blame] | 688 | } |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 689 | } |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 690 | |
Kristian Høgsberg | 547cadf | 2011-04-12 22:23:30 -0400 | [diff] [blame] | 691 | if (ec->overlay) |
| 692 | wlsc_surface_draw(ec->overlay, output, &total_damage); |
Kristian Høgsberg | c9824dd | 2011-02-06 16:54:59 -0500 | [diff] [blame] | 693 | |
Kristian Høgsberg | 86e0989 | 2010-07-07 09:51:11 -0400 | [diff] [blame] | 694 | if (ec->focus) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 695 | wl_list_for_each(eid, &ec->input_device_list, link) { |
| 696 | if (&eid->input_device != ec->input_device || |
| 697 | !using_hardware_cursor) |
| 698 | wlsc_surface_draw(eid->sprite, output, |
| 699 | &total_damage); |
| 700 | } |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 701 | } |
| 702 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 703 | static int |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 704 | repaint(void *data) |
| 705 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 706 | struct wlsc_compositor *ec = data; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 707 | struct wlsc_output *output; |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 708 | int repainted_all_outputs = 1; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 709 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 710 | wl_list_for_each(output, &ec->output_list, link) { |
| 711 | if (!output->repaint_needed) |
| 712 | continue; |
| 713 | |
| 714 | if (!output->finished) { |
| 715 | repainted_all_outputs = 0; |
| 716 | continue; |
| 717 | } |
| 718 | |
| 719 | wlsc_output_repaint(output); |
| 720 | output->finished = 0; |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 721 | output->repaint_needed = 0; |
Kristian Høgsberg | 11e2828 | 2011-04-11 16:47:50 -0400 | [diff] [blame] | 722 | output->present(output); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 723 | } |
| 724 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 725 | if (repainted_all_outputs) |
| 726 | ec->repaint_on_timeout = 0; |
| 727 | else |
| 728 | wl_event_source_timer_update(ec->timer_source, 1); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 729 | |
| 730 | return 1; |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 731 | } |
| 732 | |
Kristian Høgsberg | 86e0989 | 2010-07-07 09:51:11 -0400 | [diff] [blame] | 733 | void |
Kristian Høgsberg | 8da19ac | 2009-03-17 16:12:51 -0400 | [diff] [blame] | 734 | wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor) |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 735 | { |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 736 | struct wlsc_output *output; |
| 737 | |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 738 | if (compositor->state == WLSC_COMPOSITOR_SLEEPING) |
| 739 | return; |
| 740 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 741 | wl_list_for_each(output, &compositor->output_list, link) |
| 742 | output->repaint_needed = 1; |
| 743 | |
Kristian Høgsberg | b0a167c | 2009-08-14 11:15:18 -0400 | [diff] [blame] | 744 | if (compositor->repaint_on_timeout) |
| 745 | return; |
| 746 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 747 | wl_event_source_timer_update(compositor->timer_source, 1); |
| 748 | compositor->repaint_on_timeout = 1; |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 749 | } |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 750 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 751 | static void |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 752 | surface_destroy(struct wl_client *client, |
| 753 | struct wl_surface *surface) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 754 | { |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 755 | wl_resource_destroy(&surface->resource, client); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 756 | } |
| 757 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 758 | void |
| 759 | wlsc_surface_assign_output(struct wlsc_surface *es) |
| 760 | { |
| 761 | struct wlsc_compositor *ec = es->compositor; |
| 762 | struct wlsc_output *output; |
| 763 | |
| 764 | struct wlsc_output *tmp = es->output; |
| 765 | es->output = NULL; |
| 766 | |
| 767 | wl_list_for_each(output, &ec->output_list, link) { |
| 768 | if (output->x < es->x && es->x < output->x + output->width && |
| 769 | output->y < es->y && es->y < output->y + output->height) { |
| 770 | if (output != tmp) |
| 771 | printf("assiging surface %p to output %p\n", |
| 772 | es, output); |
| 773 | es->output = output; |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | if (es->output == NULL) { |
| 778 | printf("no output found\n"); |
| 779 | es->output = container_of(ec->output_list.next, |
| 780 | struct wlsc_output, link); |
| 781 | } |
| 782 | } |
| 783 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 784 | static void |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 785 | surface_attach(struct wl_client *client, |
Kristian Høgsberg | 82da52b | 2010-12-17 09:53:12 -0500 | [diff] [blame] | 786 | struct wl_surface *surface, struct wl_buffer *buffer, |
| 787 | int32_t x, int32_t y) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 788 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 789 | struct wlsc_surface *es = (struct wlsc_surface *) surface; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 790 | |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 791 | /* FIXME: This damages the entire old surface, but we should |
| 792 | * really just damage the part that's no longer covered by the |
| 793 | * surface. Anything covered by the new surface will be |
| 794 | * damaged by the client. */ |
| 795 | wlsc_surface_damage(es); |
| 796 | |
Benjamin Franzke | faa0a9d | 2011-02-21 16:24:53 +0100 | [diff] [blame] | 797 | wlsc_buffer_attach(buffer, surface); |
| 798 | |
Kristian Høgsberg | 3d5bae0 | 2010-10-06 21:17:40 -0400 | [diff] [blame] | 799 | es->buffer = buffer; |
Kristian Høgsberg | 82da52b | 2010-12-17 09:53:12 -0500 | [diff] [blame] | 800 | es->x += x; |
| 801 | es->y += y; |
| 802 | es->width = buffer->width; |
| 803 | es->height = buffer->height; |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 804 | if (x != 0 || y != 0) |
| 805 | wlsc_surface_assign_output(es); |
Kristian Høgsberg | 82da52b | 2010-12-17 09:53:12 -0500 | [diff] [blame] | 806 | wlsc_surface_update_matrix(es); |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 807 | } |
| 808 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 809 | static void |
Kristian Høgsberg | 82da52b | 2010-12-17 09:53:12 -0500 | [diff] [blame] | 810 | surface_map_toplevel(struct wl_client *client, |
| 811 | struct wl_surface *surface) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 812 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 813 | struct wlsc_surface *es = (struct wlsc_surface *) surface; |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 814 | struct wlsc_compositor *ec = es->compositor; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 815 | |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 816 | switch (es->map_type) { |
| 817 | case WLSC_SURFACE_MAP_UNMAPPED: |
| 818 | es->x = 10 + random() % 400; |
| 819 | es->y = 10 + random() % 400; |
| 820 | wlsc_surface_update_matrix(es); |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 821 | /* assign to first output */ |
| 822 | es->output = container_of(ec->output_list.next, |
| 823 | struct wlsc_output, link); |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 824 | wl_list_insert(&es->compositor->surface_list, &es->link); |
| 825 | break; |
| 826 | case WLSC_SURFACE_MAP_TOPLEVEL: |
Kristian Høgsberg | 8f66a57 | 2011-01-07 08:38:56 -0500 | [diff] [blame] | 827 | return; |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 828 | case WLSC_SURFACE_MAP_FULLSCREEN: |
| 829 | es->fullscreen_output = NULL; |
| 830 | es->x = es->saved_x; |
| 831 | es->y = es->saved_y; |
| 832 | wlsc_surface_update_matrix(es); |
| 833 | break; |
| 834 | default: |
| 835 | break; |
| 836 | } |
Kristian Høgsberg | 8f66a57 | 2011-01-07 08:38:56 -0500 | [diff] [blame] | 837 | |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 838 | wlsc_surface_damage(es); |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 839 | es->map_type = WLSC_SURFACE_MAP_TOPLEVEL; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 840 | } |
| 841 | |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 842 | static void |
Kristian Høgsberg | 8dc378f | 2011-01-21 18:02:24 -0500 | [diff] [blame] | 843 | surface_map_transient(struct wl_client *client, |
| 844 | struct wl_surface *surface, struct wl_surface *parent, |
| 845 | int x, int y, uint32_t flags) |
| 846 | { |
| 847 | struct wlsc_surface *es = (struct wlsc_surface *) surface; |
| 848 | struct wlsc_surface *pes = (struct wlsc_surface *) parent; |
| 849 | |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 850 | switch (es->map_type) { |
| 851 | case WLSC_SURFACE_MAP_UNMAPPED: |
| 852 | wl_list_insert(&es->compositor->surface_list, &es->link); |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 853 | /* assign to parents output */ |
| 854 | es->output = pes->output; |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 855 | break; |
| 856 | case WLSC_SURFACE_MAP_FULLSCREEN: |
| 857 | es->fullscreen_output = NULL; |
| 858 | break; |
| 859 | default: |
| 860 | break; |
| 861 | } |
Kristian Høgsberg | 8dc378f | 2011-01-21 18:02:24 -0500 | [diff] [blame] | 862 | |
| 863 | es->x = pes->x + x; |
| 864 | es->y = pes->y + y; |
| 865 | |
| 866 | wlsc_surface_update_matrix(es); |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 867 | wlsc_surface_damage(es); |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 868 | es->map_type = WLSC_SURFACE_MAP_TRANSIENT; |
| 869 | } |
| 870 | |
| 871 | static void |
| 872 | surface_map_fullscreen(struct wl_client *client, struct wl_surface *surface) |
| 873 | { |
| 874 | struct wlsc_surface *es = (struct wlsc_surface *) surface; |
| 875 | struct wlsc_output *output; |
| 876 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 877 | /* FIXME: Fullscreen on first output */ |
| 878 | /* FIXME: Handle output going away */ |
| 879 | output = container_of(es->compositor->output_list.next, |
| 880 | struct wlsc_output, link); |
| 881 | |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 882 | switch (es->map_type) { |
| 883 | case WLSC_SURFACE_MAP_UNMAPPED: |
| 884 | es->x = 10 + random() % 400; |
| 885 | es->y = 10 + random() % 400; |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 886 | /* assign to first output */ |
| 887 | es->output = output; |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 888 | wl_list_insert(&es->compositor->surface_list, &es->link); |
| 889 | break; |
| 890 | case WLSC_SURFACE_MAP_FULLSCREEN: |
| 891 | return; |
| 892 | default: |
| 893 | break; |
| 894 | } |
| 895 | |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 896 | es->saved_x = es->x; |
| 897 | es->saved_y = es->y; |
| 898 | es->x = (output->width - es->width) / 2; |
| 899 | es->y = (output->height - es->height) / 2; |
| 900 | es->fullscreen_output = output; |
| 901 | wlsc_surface_update_matrix(es); |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 902 | wlsc_surface_damage(es); |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 903 | es->map_type = WLSC_SURFACE_MAP_FULLSCREEN; |
Kristian Høgsberg | 8dc378f | 2011-01-21 18:02:24 -0500 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | static void |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 907 | surface_damage(struct wl_client *client, |
| 908 | struct wl_surface *surface, |
| 909 | int32_t x, int32_t y, int32_t width, int32_t height) |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 910 | { |
Kristian Høgsberg | 9d69f8e | 2010-09-03 14:46:38 -0400 | [diff] [blame] | 911 | struct wlsc_surface *es = (struct wlsc_surface *) surface; |
| 912 | |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 913 | wlsc_surface_damage_rectangle(es, x, y, width, height); |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 914 | } |
| 915 | |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 916 | const static struct wl_surface_interface surface_interface = { |
| 917 | surface_destroy, |
| 918 | surface_attach, |
Kristian Høgsberg | 82da52b | 2010-12-17 09:53:12 -0500 | [diff] [blame] | 919 | surface_map_toplevel, |
Kristian Høgsberg | 8dc378f | 2011-01-21 18:02:24 -0500 | [diff] [blame] | 920 | surface_map_transient, |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 921 | surface_map_fullscreen, |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 922 | surface_damage |
| 923 | }; |
| 924 | |
| 925 | static void |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 926 | wlsc_input_device_attach(struct wlsc_input_device *device, |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 927 | int x, int y, int width, int height) |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 928 | { |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 929 | wlsc_surface_damage(device->sprite); |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 930 | |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 931 | device->hotspot_x = x; |
| 932 | device->hotspot_y = y; |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 933 | |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 934 | device->sprite->x = device->input_device.x - device->hotspot_x; |
| 935 | device->sprite->y = device->input_device.y - device->hotspot_y; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 936 | device->sprite->width = width; |
| 937 | device->sprite->height = height; |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 938 | wlsc_surface_update_matrix(device->sprite); |
| 939 | |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 940 | wlsc_surface_damage(device->sprite); |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 941 | } |
| 942 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 943 | static void |
| 944 | wlsc_input_device_attach_buffer(struct wlsc_input_device *device, |
| 945 | struct wl_buffer *buffer, int x, int y) |
| 946 | { |
| 947 | wlsc_buffer_attach(buffer, &device->sprite->surface); |
| 948 | wlsc_input_device_attach(device, x, y, buffer->width, buffer->height); |
| 949 | } |
| 950 | |
| 951 | static void |
| 952 | wlsc_input_device_attach_sprite(struct wlsc_input_device *device, |
| 953 | struct wlsc_sprite *sprite, int x, int y) |
| 954 | { |
| 955 | wlsc_sprite_attach(sprite, &device->sprite->surface); |
| 956 | wlsc_input_device_attach(device, x, y, sprite->width, sprite->height); |
| 957 | } |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 958 | |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 959 | void |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 960 | wlsc_input_device_set_pointer_image(struct wlsc_input_device *device, |
| 961 | enum wlsc_pointer_type type) |
| 962 | { |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 963 | struct wlsc_compositor *compositor = |
| 964 | (struct wlsc_compositor *) device->input_device.compositor; |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 965 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 966 | wlsc_input_device_attach_sprite(device, |
| 967 | compositor->pointer_sprites[type], |
| 968 | pointer_images[type].hotspot_x, |
| 969 | pointer_images[type].hotspot_y); |
Kristian Høgsberg | eef08fb | 2010-08-17 21:23:10 -0400 | [diff] [blame] | 970 | } |
| 971 | |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 972 | static void |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 973 | compositor_create_surface(struct wl_client *client, |
| 974 | struct wl_compositor *compositor, uint32_t id) |
| 975 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 976 | struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor; |
Kristian Høgsberg | 8da19ac | 2009-03-17 16:12:51 -0400 | [diff] [blame] | 977 | struct wlsc_surface *surface; |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 978 | |
Kristian Høgsberg | c5d6be9 | 2011-01-14 16:22:37 -0500 | [diff] [blame] | 979 | surface = wlsc_surface_create(ec, 0, 0, 0, 0); |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 980 | if (surface == NULL) { |
Kristian Høgsberg | 13b8ae4 | 2010-09-02 20:55:16 -0400 | [diff] [blame] | 981 | wl_client_post_no_memory(client); |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 982 | return; |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 983 | } |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 984 | |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 985 | surface->surface.resource.destroy = destroy_surface; |
Kristian Høgsberg | f66d0f4 | 2010-09-02 20:27:16 -0400 | [diff] [blame] | 986 | |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 987 | surface->surface.resource.object.id = id; |
| 988 | surface->surface.resource.object.interface = &wl_surface_interface; |
| 989 | surface->surface.resource.object.implementation = |
Kristian Høgsberg | f66d0f4 | 2010-09-02 20:27:16 -0400 | [diff] [blame] | 990 | (void (**)(void)) &surface_interface; |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 991 | surface->surface.client = client; |
Kristian Høgsberg | f66d0f4 | 2010-09-02 20:27:16 -0400 | [diff] [blame] | 992 | |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 993 | wl_client_add_resource(client, &surface->surface.resource); |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 994 | } |
| 995 | |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 996 | const static struct wl_compositor_interface compositor_interface = { |
| 997 | compositor_create_surface, |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 998 | }; |
| 999 | |
Kristian Høgsberg | 7b6907f | 2009-02-14 17:47:55 -0500 | [diff] [blame] | 1000 | static void |
| 1001 | wlsc_surface_transform(struct wlsc_surface *surface, |
| 1002 | int32_t x, int32_t y, int32_t *sx, int32_t *sy) |
| 1003 | { |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1004 | struct wlsc_vector v = { { x, y, 0, 1 } }; |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1005 | |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1006 | wlsc_matrix_transform(&surface->matrix_inv, &v); |
Kristian Høgsberg | 0b8646b | 2010-06-08 15:29:14 -0400 | [diff] [blame] | 1007 | *sx = v.f[0] * surface->width; |
| 1008 | *sy = v.f[1] * surface->height; |
Kristian Høgsberg | 7b6907f | 2009-02-14 17:47:55 -0500 | [diff] [blame] | 1009 | } |
| 1010 | |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 1011 | struct wlsc_surface * |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1012 | pick_surface(struct wl_input_device *device, int32_t *sx, int32_t *sy) |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 1013 | { |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1014 | struct wlsc_compositor *ec = |
| 1015 | (struct wlsc_compositor *) device->compositor; |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1016 | struct wlsc_surface *es; |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 1017 | |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1018 | wl_list_for_each(es, &ec->surface_list, link) { |
| 1019 | wlsc_surface_transform(es, device->x, device->y, sx, sy); |
| 1020 | if (0 <= *sx && *sx < es->width && |
| 1021 | 0 <= *sy && *sy < es->height) |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 1022 | return es; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1023 | } |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 1024 | |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 1025 | return NULL; |
| 1026 | } |
| 1027 | |
Kristian Høgsberg | 8321e69 | 2010-12-07 17:06:15 -0500 | [diff] [blame] | 1028 | |
| 1029 | static void |
| 1030 | motion_grab_motion(struct wl_grab *grab, |
| 1031 | uint32_t time, int32_t x, int32_t y) |
| 1032 | { |
| 1033 | struct wlsc_input_device *device = |
| 1034 | (struct wlsc_input_device *) grab->input_device; |
| 1035 | struct wlsc_surface *es = |
| 1036 | (struct wlsc_surface *) device->input_device.pointer_focus; |
| 1037 | int32_t sx, sy; |
| 1038 | |
| 1039 | wlsc_surface_transform(es, x, y, &sx, &sy); |
| 1040 | wl_client_post_event(es->surface.client, |
| 1041 | &device->input_device.object, |
| 1042 | WL_INPUT_DEVICE_MOTION, |
| 1043 | time, x, y, sx, sy); |
| 1044 | } |
| 1045 | |
| 1046 | static void |
Kristian Høgsberg | b3fc757 | 2010-12-08 11:07:57 -0500 | [diff] [blame] | 1047 | motion_grab_button(struct wl_grab *grab, |
| 1048 | uint32_t time, int32_t button, int32_t state) |
| 1049 | { |
| 1050 | wl_client_post_event(grab->input_device->pointer_focus->client, |
| 1051 | &grab->input_device->object, |
| 1052 | WL_INPUT_DEVICE_BUTTON, |
| 1053 | time, button, state); |
| 1054 | } |
| 1055 | |
| 1056 | static void |
Kristian Høgsberg | 8321e69 | 2010-12-07 17:06:15 -0500 | [diff] [blame] | 1057 | motion_grab_end(struct wl_grab *grab, uint32_t time) |
| 1058 | { |
| 1059 | } |
| 1060 | |
| 1061 | static const struct wl_grab_interface motion_grab_interface = { |
| 1062 | motion_grab_motion, |
Kristian Høgsberg | b3fc757 | 2010-12-08 11:07:57 -0500 | [diff] [blame] | 1063 | motion_grab_button, |
Kristian Høgsberg | 8321e69 | 2010-12-07 17:06:15 -0500 | [diff] [blame] | 1064 | motion_grab_end |
| 1065 | }; |
| 1066 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 1067 | void |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 1068 | wlsc_compositor_wake(struct wlsc_compositor *compositor) |
| 1069 | { |
| 1070 | if (compositor->idle_inhibit) |
| 1071 | return; |
| 1072 | |
| 1073 | if (compositor->state == WLSC_COMPOSITOR_SLEEPING) { |
| 1074 | compositor->state = WLSC_COMPOSITOR_ACTIVE; |
| 1075 | wlsc_compositor_damage_all(compositor); |
| 1076 | } |
| 1077 | |
| 1078 | wl_event_source_timer_update(compositor->idle_source, |
| 1079 | option_idle_time * 1000); |
| 1080 | } |
| 1081 | |
| 1082 | static void |
| 1083 | wlsc_compositor_idle_inhibit(struct wlsc_compositor *compositor) |
| 1084 | { |
| 1085 | wlsc_compositor_wake(compositor); |
| 1086 | compositor->idle_inhibit++; |
| 1087 | } |
| 1088 | |
| 1089 | static void |
| 1090 | wlsc_compositor_idle_release(struct wlsc_compositor *compositor) |
| 1091 | { |
| 1092 | compositor->idle_inhibit--; |
| 1093 | wlsc_compositor_wake(compositor); |
| 1094 | } |
| 1095 | |
| 1096 | static int |
| 1097 | idle_handler(void *data) |
| 1098 | { |
| 1099 | struct wlsc_compositor *compositor = data; |
| 1100 | |
| 1101 | if (compositor->idle_inhibit) |
| 1102 | return 1; |
| 1103 | |
| 1104 | wlsc_compositor_damage_all(compositor); |
| 1105 | compositor->state = WLSC_COMPOSITOR_SLEEPING; |
| 1106 | |
| 1107 | return 1; |
| 1108 | } |
| 1109 | |
| 1110 | void |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1111 | notify_motion(struct wl_input_device *device, uint32_t time, int x, int y) |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 1112 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1113 | struct wlsc_surface *es; |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1114 | struct wlsc_compositor *ec = |
| 1115 | (struct wlsc_compositor *) device->compositor; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1116 | struct wlsc_output *output; |
Kristian Høgsberg | 6d65d5f | 2010-12-07 13:30:18 -0500 | [diff] [blame] | 1117 | const struct wl_grab_interface *interface; |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1118 | struct wlsc_input_device *wd = (struct wlsc_input_device *) device; |
Kristian Høgsberg | fc9c28a | 2010-12-07 13:04:43 -0500 | [diff] [blame] | 1119 | int32_t sx, sy; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1120 | int x_valid = 0, y_valid = 0; |
| 1121 | int min_x = INT_MAX, min_y = INT_MAX, max_x = INT_MIN, max_y = INT_MIN; |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 1122 | |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 1123 | wlsc_compositor_wake(ec); |
| 1124 | |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1125 | wl_list_for_each(output, &ec->output_list, link) { |
| 1126 | if (output->x <= x && x <= output->x + output->width) |
| 1127 | x_valid = 1; |
| 1128 | |
| 1129 | if (output->y <= y && y <= output->y + output->height) |
| 1130 | y_valid = 1; |
| 1131 | |
| 1132 | /* FIXME: calculate this only on output addition/deletion */ |
| 1133 | if (output->x < min_x) |
| 1134 | min_x = output->x; |
| 1135 | if (output->y < min_y) |
| 1136 | min_y = output->y; |
| 1137 | |
| 1138 | if (output->x + output->width > max_x) |
| 1139 | max_x = output->x + output->width; |
| 1140 | if (output->y + output->height > max_y) |
| 1141 | max_y = output->y + output->height; |
| 1142 | } |
| 1143 | |
| 1144 | if (!x_valid) { |
| 1145 | if (x < min_x) |
| 1146 | x = min_x; |
| 1147 | else if (x >= max_x) |
| 1148 | x = max_x; |
| 1149 | } |
| 1150 | if (!y_valid) { |
| 1151 | if (y < min_y) |
| 1152 | y = min_y; |
| 1153 | else if (y >= max_y) |
| 1154 | y = max_y; |
| 1155 | } |
Ray Strode | 90e701d | 2008-12-18 23:05:43 -0500 | [diff] [blame] | 1156 | |
Kristian Høgsberg | e3ef3e5 | 2008-12-21 19:30:01 -0500 | [diff] [blame] | 1157 | device->x = x; |
| 1158 | device->y = y; |
Kristian Høgsberg | db6c2f3 | 2009-02-22 21:51:24 -0500 | [diff] [blame] | 1159 | |
Kristian Høgsberg | 8321e69 | 2010-12-07 17:06:15 -0500 | [diff] [blame] | 1160 | if (device->grab) { |
| 1161 | interface = device->grab->interface; |
| 1162 | interface->motion(device->grab, time, x, y); |
| 1163 | } else { |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 1164 | es = pick_surface(device, &sx, &sy); |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1165 | wl_input_device_set_pointer_focus(device, |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1166 | &es->surface, |
Kristian Høgsberg | 2643707 | 2010-12-01 10:17:47 -0500 | [diff] [blame] | 1167 | time, x, y, sx, sy); |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 1168 | if (es) |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1169 | wl_client_post_event(es->surface.client, |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1170 | &device->object, |
Kristian Høgsberg | 50038e4 | 2010-09-07 21:08:59 -0400 | [diff] [blame] | 1171 | WL_INPUT_DEVICE_MOTION, |
| 1172 | time, x, y, sx, sy); |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 1173 | } |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 1174 | |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 1175 | wlsc_surface_damage(wd->sprite); |
| 1176 | |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1177 | wd->sprite->x = device->x - wd->hotspot_x; |
| 1178 | wd->sprite->y = device->y - wd->hotspot_y; |
| 1179 | wlsc_surface_update_matrix(wd->sprite); |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 1180 | |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 1181 | wlsc_surface_damage(wd->sprite); |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 1182 | } |
| 1183 | |
Kristian Høgsberg | 30021d7 | 2011-04-12 17:42:30 -0400 | [diff] [blame] | 1184 | void |
Kristian Høgsberg | c9824dd | 2011-02-06 16:54:59 -0500 | [diff] [blame] | 1185 | wlsc_surface_activate(struct wlsc_surface *surface, |
| 1186 | struct wlsc_input_device *device, uint32_t time) |
| 1187 | { |
| 1188 | wlsc_surface_raise(surface); |
| 1189 | if (device->selection) |
| 1190 | wlsc_selection_set_focus(device->selection, |
| 1191 | &surface->surface, time); |
| 1192 | |
| 1193 | wl_input_device_set_keyboard_focus(&device->input_device, |
| 1194 | &surface->surface, |
| 1195 | time); |
| 1196 | } |
| 1197 | |
Kristian Høgsberg | a8ec863 | 2011-04-12 17:22:49 -0400 | [diff] [blame] | 1198 | struct wlsc_binding { |
| 1199 | uint32_t key; |
| 1200 | uint32_t button; |
| 1201 | uint32_t modifier; |
| 1202 | wlsc_binding_handler_t handler; |
| 1203 | void *data; |
| 1204 | struct wl_list link; |
| 1205 | }; |
| 1206 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 1207 | void |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1208 | notify_button(struct wl_input_device *device, |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 1209 | uint32_t time, int32_t button, int32_t state) |
Kristian Høgsberg | eac149a | 2008-12-10 00:24:18 -0500 | [diff] [blame] | 1210 | { |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1211 | struct wlsc_input_device *wd = (struct wlsc_input_device *) device; |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1212 | struct wlsc_compositor *compositor = |
| 1213 | (struct wlsc_compositor *) device->compositor; |
Kristian Høgsberg | a8ec863 | 2011-04-12 17:22:49 -0400 | [diff] [blame] | 1214 | struct wlsc_binding *b; |
| 1215 | struct wlsc_surface *surface = |
| 1216 | (struct wlsc_surface *) device->pointer_focus; |
Kristian Høgsberg | ea08115 | 2010-12-07 08:59:51 -0500 | [diff] [blame] | 1217 | |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 1218 | if (state) |
| 1219 | wlsc_compositor_idle_inhibit(compositor); |
| 1220 | else |
| 1221 | wlsc_compositor_idle_release(compositor); |
| 1222 | |
Kristian Høgsberg | dd4046a | 2011-01-21 17:00:09 -0500 | [diff] [blame] | 1223 | if (state && surface && device->grab == NULL) { |
Kristian Høgsberg | c9824dd | 2011-02-06 16:54:59 -0500 | [diff] [blame] | 1224 | wlsc_surface_activate(surface, wd, time); |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1225 | wl_input_device_start_grab(device, |
| 1226 | &device->motion_grab, |
| 1227 | button, time); |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 1228 | } |
Kristian Høgsberg | dff2e3c | 2010-12-07 09:02:09 -0500 | [diff] [blame] | 1229 | |
Kristian Høgsberg | a8ec863 | 2011-04-12 17:22:49 -0400 | [diff] [blame] | 1230 | wl_list_for_each(b, &compositor->binding_list, link) { |
| 1231 | if (b->button == button && |
| 1232 | b->modifier == wd->modifier_state && state) { |
| 1233 | b->handler(&wd->input_device, |
| 1234 | time, 0, button, state, b->data); |
| 1235 | break; |
| 1236 | } |
Benjamin Franzke | 5a2218a | 2011-02-01 16:30:31 +0100 | [diff] [blame] | 1237 | } |
Kristian Høgsberg | b3fc757 | 2010-12-08 11:07:57 -0500 | [diff] [blame] | 1238 | |
Kristian Høgsberg | dd4046a | 2011-01-21 17:00:09 -0500 | [diff] [blame] | 1239 | if (device->grab) |
| 1240 | device->grab->interface->button(device->grab, time, |
| 1241 | button, state); |
Kristian Høgsberg | dff2e3c | 2010-12-07 09:02:09 -0500 | [diff] [blame] | 1242 | |
Kristian Høgsberg | 8321e69 | 2010-12-07 17:06:15 -0500 | [diff] [blame] | 1243 | if (!state && device->grab && device->grab_button == button) |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1244 | wl_input_device_end_grab(device, time); |
Kristian Høgsberg | eac149a | 2008-12-10 00:24:18 -0500 | [diff] [blame] | 1245 | } |
| 1246 | |
Kristian Høgsberg | c9824dd | 2011-02-06 16:54:59 -0500 | [diff] [blame] | 1247 | static void |
Kristian Høgsberg | a8ec863 | 2011-04-12 17:22:49 -0400 | [diff] [blame] | 1248 | terminate_binding(struct wl_input_device *device, uint32_t time, |
| 1249 | uint32_t key, uint32_t button, uint32_t state, void *data) |
Kristian Høgsberg | 3555d09 | 2011-04-11 13:58:13 -0400 | [diff] [blame] | 1250 | { |
| 1251 | struct wlsc_compositor *compositor = data; |
| 1252 | |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 1253 | if (state) |
| 1254 | wl_display_terminate(compositor->wl_display); |
Kristian Høgsberg | 3555d09 | 2011-04-11 13:58:13 -0400 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | struct wlsc_binding * |
| 1258 | wlsc_compositor_add_binding(struct wlsc_compositor *compositor, |
Kristian Høgsberg | a8ec863 | 2011-04-12 17:22:49 -0400 | [diff] [blame] | 1259 | uint32_t key, uint32_t button, uint32_t modifier, |
Kristian Høgsberg | 3555d09 | 2011-04-11 13:58:13 -0400 | [diff] [blame] | 1260 | wlsc_binding_handler_t handler, void *data) |
| 1261 | { |
| 1262 | struct wlsc_binding *binding; |
| 1263 | |
| 1264 | binding = malloc(sizeof *binding); |
| 1265 | if (binding == NULL) |
| 1266 | return NULL; |
| 1267 | |
| 1268 | binding->key = key; |
Kristian Høgsberg | a8ec863 | 2011-04-12 17:22:49 -0400 | [diff] [blame] | 1269 | binding->button = button; |
Kristian Høgsberg | 3555d09 | 2011-04-11 13:58:13 -0400 | [diff] [blame] | 1270 | binding->modifier = modifier; |
| 1271 | binding->handler = handler; |
| 1272 | binding->data = data; |
| 1273 | wl_list_insert(compositor->binding_list.prev, &binding->link); |
| 1274 | |
| 1275 | return binding; |
| 1276 | } |
| 1277 | |
| 1278 | void |
| 1279 | wlsc_binding_destroy(struct wlsc_binding *binding) |
| 1280 | { |
| 1281 | wl_list_remove(&binding->link); |
| 1282 | free(binding); |
| 1283 | } |
| 1284 | |
Kristian Høgsberg | f512d07 | 2011-04-12 17:16:00 -0400 | [diff] [blame] | 1285 | static void |
| 1286 | update_modifier_state(struct wlsc_input_device *device, |
| 1287 | uint32_t key, uint32_t state) |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 1288 | { |
Kristian Høgsberg | 2cbedd1 | 2009-09-18 17:29:49 -0400 | [diff] [blame] | 1289 | uint32_t modifier; |
Kristian Høgsberg | ab909ae | 2001-01-01 22:24:24 -0500 | [diff] [blame] | 1290 | |
Kristian Høgsberg | 2cbedd1 | 2009-09-18 17:29:49 -0400 | [diff] [blame] | 1291 | switch (key) { |
| 1292 | case KEY_LEFTCTRL: |
| 1293 | case KEY_RIGHTCTRL: |
| 1294 | modifier = MODIFIER_CTRL; |
| 1295 | break; |
| 1296 | |
| 1297 | case KEY_LEFTALT: |
| 1298 | case KEY_RIGHTALT: |
| 1299 | modifier = MODIFIER_ALT; |
| 1300 | break; |
| 1301 | |
Kristian Høgsberg | 5b75f1b | 2010-08-04 23:21:41 -0400 | [diff] [blame] | 1302 | case KEY_LEFTMETA: |
| 1303 | case KEY_RIGHTMETA: |
| 1304 | modifier = MODIFIER_SUPER; |
| 1305 | break; |
| 1306 | |
Kristian Høgsberg | 2cbedd1 | 2009-09-18 17:29:49 -0400 | [diff] [blame] | 1307 | default: |
| 1308 | modifier = 0; |
| 1309 | break; |
| 1310 | } |
| 1311 | |
| 1312 | if (state) |
Kristian Høgsberg | f512d07 | 2011-04-12 17:16:00 -0400 | [diff] [blame] | 1313 | device->modifier_state |= modifier; |
Kristian Høgsberg | 2cbedd1 | 2009-09-18 17:29:49 -0400 | [diff] [blame] | 1314 | else |
Kristian Høgsberg | f512d07 | 2011-04-12 17:16:00 -0400 | [diff] [blame] | 1315 | device->modifier_state &= ~modifier; |
| 1316 | } |
Kristian Høgsberg | 2cbedd1 | 2009-09-18 17:29:49 -0400 | [diff] [blame] | 1317 | |
Kristian Høgsberg | f512d07 | 2011-04-12 17:16:00 -0400 | [diff] [blame] | 1318 | void |
| 1319 | notify_key(struct wl_input_device *device, |
| 1320 | uint32_t time, uint32_t key, uint32_t state) |
| 1321 | { |
| 1322 | struct wlsc_input_device *wd = (struct wlsc_input_device *) device; |
| 1323 | struct wlsc_compositor *compositor = |
| 1324 | (struct wlsc_compositor *) device->compositor; |
| 1325 | uint32_t *k, *end; |
| 1326 | struct wlsc_binding *b; |
| 1327 | |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 1328 | if (state) |
| 1329 | wlsc_compositor_idle_inhibit(compositor); |
| 1330 | else |
| 1331 | wlsc_compositor_idle_release(compositor); |
| 1332 | |
Kristian Høgsberg | f512d07 | 2011-04-12 17:16:00 -0400 | [diff] [blame] | 1333 | wl_list_for_each(b, &compositor->binding_list, link) { |
| 1334 | if (b->key == key && |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 1335 | b->modifier == wd->modifier_state) { |
Kristian Høgsberg | a8ec863 | 2011-04-12 17:22:49 -0400 | [diff] [blame] | 1336 | b->handler(&wd->input_device, |
| 1337 | time, key, 0, state, b->data); |
Kristian Høgsberg | f512d07 | 2011-04-12 17:16:00 -0400 | [diff] [blame] | 1338 | break; |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | update_modifier_state(wd, key, state); |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1343 | end = device->keys.data + device->keys.size; |
| 1344 | for (k = device->keys.data; k < end; k++) { |
Kristian Høgsberg | 3c38fa0 | 2009-02-23 22:30:29 -0500 | [diff] [blame] | 1345 | if (*k == key) |
| 1346 | *k = *--end; |
| 1347 | } |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1348 | device->keys.size = (void *) end - device->keys.data; |
Kristian Høgsberg | 3c38fa0 | 2009-02-23 22:30:29 -0500 | [diff] [blame] | 1349 | if (state) { |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1350 | k = wl_array_add(&device->keys, sizeof *k); |
Kristian Høgsberg | 3c38fa0 | 2009-02-23 22:30:29 -0500 | [diff] [blame] | 1351 | *k = key; |
| 1352 | } |
Ray Strode | e96dcb8 | 2008-12-20 02:00:49 -0500 | [diff] [blame] | 1353 | |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1354 | if (device->keyboard_focus != NULL) |
| 1355 | wl_client_post_event(device->keyboard_focus->client, |
| 1356 | &device->object, |
Kristian Høgsberg | 50038e4 | 2010-09-07 21:08:59 -0400 | [diff] [blame] | 1357 | WL_INPUT_DEVICE_KEY, time, key, state); |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 1358 | } |
| 1359 | |
Kristian Høgsberg | 93331ff | 2011-01-26 20:35:07 -0500 | [diff] [blame] | 1360 | void |
| 1361 | notify_pointer_focus(struct wl_input_device *device, |
| 1362 | uint32_t time, struct wlsc_output *output, |
| 1363 | int32_t x, int32_t y) |
| 1364 | { |
| 1365 | struct wlsc_input_device *wd = (struct wlsc_input_device *) device; |
| 1366 | struct wlsc_compositor *compositor = |
| 1367 | (struct wlsc_compositor *) device->compositor; |
| 1368 | struct wlsc_surface *es; |
| 1369 | int32_t sx, sy; |
| 1370 | |
| 1371 | if (output) { |
| 1372 | device->x = x; |
| 1373 | device->y = y; |
| 1374 | es = pick_surface(device, &sx, &sy); |
| 1375 | wl_input_device_set_pointer_focus(device, |
| 1376 | &es->surface, |
| 1377 | time, x, y, sx, sy); |
| 1378 | |
| 1379 | compositor->focus = 1; |
| 1380 | |
| 1381 | wd->sprite->x = device->x - wd->hotspot_x; |
| 1382 | wd->sprite->y = device->y - wd->hotspot_y; |
| 1383 | wlsc_surface_update_matrix(wd->sprite); |
| 1384 | } else { |
| 1385 | wl_input_device_set_pointer_focus(device, NULL, |
| 1386 | time, 0, 0, 0, 0); |
| 1387 | compositor->focus = 0; |
| 1388 | } |
| 1389 | |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 1390 | wlsc_surface_damage(wd->sprite); |
Kristian Høgsberg | 93331ff | 2011-01-26 20:35:07 -0500 | [diff] [blame] | 1391 | } |
| 1392 | |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 1393 | void |
Kristian Høgsberg | f992b2f | 2011-01-28 15:53:07 -0500 | [diff] [blame] | 1394 | notify_keyboard_focus(struct wl_input_device *device, |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 1395 | uint32_t time, struct wlsc_output *output, |
| 1396 | struct wl_array *keys) |
| 1397 | { |
Kristian Høgsberg | f992b2f | 2011-01-28 15:53:07 -0500 | [diff] [blame] | 1398 | struct wlsc_input_device *wd = |
| 1399 | (struct wlsc_input_device *) device; |
| 1400 | struct wlsc_compositor *compositor = |
| 1401 | (struct wlsc_compositor *) device->compositor; |
| 1402 | struct wlsc_surface *es; |
Kristian Høgsberg | f512d07 | 2011-04-12 17:16:00 -0400 | [diff] [blame] | 1403 | uint32_t *k, *end; |
Kristian Høgsberg | f992b2f | 2011-01-28 15:53:07 -0500 | [diff] [blame] | 1404 | |
| 1405 | if (!wl_list_empty(&compositor->surface_list)) |
| 1406 | es = container_of(compositor->surface_list.next, |
| 1407 | struct wlsc_surface, link); |
| 1408 | else |
| 1409 | es = NULL; |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 1410 | |
| 1411 | if (output) { |
Kristian Høgsberg | f992b2f | 2011-01-28 15:53:07 -0500 | [diff] [blame] | 1412 | wl_array_copy(&wd->input_device.keys, keys); |
Kristian Høgsberg | f512d07 | 2011-04-12 17:16:00 -0400 | [diff] [blame] | 1413 | wd->modifier_state = 0; |
| 1414 | end = device->keys.data + device->keys.size; |
| 1415 | for (k = device->keys.data; k < end; k++) { |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 1416 | wlsc_compositor_idle_inhibit(compositor); |
Kristian Høgsberg | f512d07 | 2011-04-12 17:16:00 -0400 | [diff] [blame] | 1417 | update_modifier_state(wd, *k, 1); |
| 1418 | } |
| 1419 | |
Kristian Høgsberg | f992b2f | 2011-01-28 15:53:07 -0500 | [diff] [blame] | 1420 | wl_input_device_set_keyboard_focus(&wd->input_device, |
| 1421 | &es->surface, time); |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 1422 | } else { |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 1423 | end = device->keys.data + device->keys.size; |
| 1424 | for (k = device->keys.data; k < end; k++) |
| 1425 | wlsc_compositor_idle_release(compositor); |
| 1426 | |
Kristian Høgsberg | f512d07 | 2011-04-12 17:16:00 -0400 | [diff] [blame] | 1427 | wd->modifier_state = 0; |
Kristian Høgsberg | f992b2f | 2011-01-28 15:53:07 -0500 | [diff] [blame] | 1428 | wl_input_device_set_keyboard_focus(&wd->input_device, |
Kristian Høgsberg | 3ba4858 | 2011-01-27 11:57:19 -0500 | [diff] [blame] | 1429 | NULL, time); |
| 1430 | } |
| 1431 | } |
| 1432 | |
| 1433 | |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 1434 | static void |
Kristian Høgsberg | ab1862d | 2010-12-09 11:29:40 -0500 | [diff] [blame] | 1435 | input_device_attach(struct wl_client *client, |
| 1436 | struct wl_input_device *device_base, |
| 1437 | uint32_t time, |
| 1438 | struct wl_buffer *buffer, int32_t x, int32_t y) |
| 1439 | { |
| 1440 | struct wlsc_input_device *device = |
| 1441 | (struct wlsc_input_device *) device_base; |
| 1442 | |
| 1443 | if (time < device->input_device.pointer_focus_time) |
| 1444 | return; |
| 1445 | if (device->input_device.pointer_focus == NULL) |
| 1446 | return; |
Kristian Høgsberg | ab1862d | 2010-12-09 11:29:40 -0500 | [diff] [blame] | 1447 | if (device->input_device.pointer_focus->client != client) |
| 1448 | return; |
| 1449 | |
| 1450 | if (buffer == NULL) { |
| 1451 | wlsc_input_device_set_pointer_image(device, |
| 1452 | WLSC_POINTER_LEFT_PTR); |
| 1453 | return; |
| 1454 | } |
| 1455 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 1456 | wlsc_input_device_attach_buffer(device, buffer, x, y); |
Kristian Høgsberg | ab1862d | 2010-12-09 11:29:40 -0500 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | const static struct wl_input_device_interface input_device_interface = { |
| 1460 | input_device_attach, |
| 1461 | }; |
| 1462 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1463 | void |
| 1464 | wlsc_input_device_init(struct wlsc_input_device *device, |
| 1465 | struct wlsc_compositor *ec) |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 1466 | { |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1467 | wl_input_device_init(&device->input_device, &ec->compositor); |
Kristian Høgsberg | 02ef1c1 | 2010-12-06 21:35:19 -0500 | [diff] [blame] | 1468 | |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1469 | device->input_device.object.interface = &wl_input_device_interface; |
| 1470 | device->input_device.object.implementation = |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 1471 | (void (**)(void)) &input_device_interface; |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1472 | wl_display_add_object(ec->wl_display, &device->input_device.object); |
| 1473 | wl_display_add_global(ec->wl_display, &device->input_device.object, NULL); |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 1474 | |
Kristian Høgsberg | c5d6be9 | 2011-01-14 16:22:37 -0500 | [diff] [blame] | 1475 | device->sprite = wlsc_surface_create(ec, |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1476 | device->input_device.x, |
| 1477 | device->input_device.y, 32, 32); |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 1478 | device->hotspot_x = 16; |
| 1479 | device->hotspot_y = 16; |
Kristian Høgsberg | a8ec863 | 2011-04-12 17:22:49 -0400 | [diff] [blame] | 1480 | device->modifier_state = 0; |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 1481 | |
Kristian Høgsberg | 9c3e8d7 | 2010-12-08 09:48:52 -0500 | [diff] [blame] | 1482 | device->input_device.motion_grab.interface = &motion_grab_interface; |
Kristian Høgsberg | 8321e69 | 2010-12-07 17:06:15 -0500 | [diff] [blame] | 1483 | |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 1484 | wl_list_insert(ec->input_device_list.prev, &device->link); |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 1485 | |
| 1486 | wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR); |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 1487 | } |
| 1488 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1489 | static void |
Kristian Høgsberg | 91342c6 | 2011-04-14 14:44:58 -0400 | [diff] [blame] | 1490 | wlsc_output_post_geometry(struct wl_client *client, |
| 1491 | struct wl_object *global, uint32_t version) |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1492 | { |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1493 | struct wlsc_output *output = |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1494 | container_of(global, struct wlsc_output, object); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1495 | |
| 1496 | wl_client_post_event(client, global, |
| 1497 | WL_OUTPUT_GEOMETRY, |
Kristian Høgsberg | f8fc08f | 2010-12-01 20:10:10 -0500 | [diff] [blame] | 1498 | output->x, output->y, |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1499 | output->width, output->height); |
| 1500 | } |
| 1501 | |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1502 | static const char vertex_shader[] = |
| 1503 | "uniform mat4 proj;\n" |
Kristian Høgsberg | fa4e2a7 | 2011-02-13 13:44:55 -0500 | [diff] [blame] | 1504 | "attribute vec2 position;\n" |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1505 | "attribute vec2 texcoord;\n" |
| 1506 | "varying vec2 v_texcoord;\n" |
| 1507 | "void main()\n" |
| 1508 | "{\n" |
Kristian Høgsberg | fa4e2a7 | 2011-02-13 13:44:55 -0500 | [diff] [blame] | 1509 | " gl_Position = proj * vec4(position, 0.0, 1.0);\n" |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1510 | " v_texcoord = texcoord;\n" |
| 1511 | "}\n"; |
| 1512 | |
Kristian Høgsberg | 7ffc448 | 2011-04-22 14:23:51 -0400 | [diff] [blame^] | 1513 | static const char texture_fragment_shader[] = |
Kristian Høgsberg | dfce71d | 2010-12-07 20:19:10 -0500 | [diff] [blame] | 1514 | "precision mediump float;\n" |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1515 | "varying vec2 v_texcoord;\n" |
| 1516 | "uniform sampler2D tex;\n" |
| 1517 | "void main()\n" |
| 1518 | "{\n" |
| 1519 | " gl_FragColor = texture2D(tex, v_texcoord)\n;" |
| 1520 | "}\n"; |
| 1521 | |
Kristian Høgsberg | a946821 | 2010-06-14 21:03:11 -0400 | [diff] [blame] | 1522 | static int |
Kristian Høgsberg | 7ffc448 | 2011-04-22 14:23:51 -0400 | [diff] [blame^] | 1523 | compile_shader(GLenum type, const char *source) |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1524 | { |
Kristian Høgsberg | 7ffc448 | 2011-04-22 14:23:51 -0400 | [diff] [blame^] | 1525 | GLuint s; |
Kristian Høgsberg | 67a21bd | 2010-06-25 18:58:24 -0400 | [diff] [blame] | 1526 | char msg[512]; |
Kristian Høgsberg | 67a21bd | 2010-06-25 18:58:24 -0400 | [diff] [blame] | 1527 | GLint status; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1528 | |
Kristian Høgsberg | 7ffc448 | 2011-04-22 14:23:51 -0400 | [diff] [blame^] | 1529 | s = glCreateShader(type); |
| 1530 | glShaderSource(s, 1, &source, NULL); |
| 1531 | glCompileShader(s); |
| 1532 | glGetShaderiv(s, GL_COMPILE_STATUS, &status); |
Kristian Høgsberg | 67a21bd | 2010-06-25 18:58:24 -0400 | [diff] [blame] | 1533 | if (!status) { |
Kristian Høgsberg | 7ffc448 | 2011-04-22 14:23:51 -0400 | [diff] [blame^] | 1534 | glGetShaderInfoLog(s, sizeof msg, NULL, msg); |
| 1535 | fprintf(stderr, "shader info: %s\n", msg); |
| 1536 | return GL_NONE; |
Kristian Høgsberg | 67a21bd | 2010-06-25 18:58:24 -0400 | [diff] [blame] | 1537 | } |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1538 | |
Kristian Høgsberg | 7ffc448 | 2011-04-22 14:23:51 -0400 | [diff] [blame^] | 1539 | return s; |
| 1540 | } |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1541 | |
Kristian Høgsberg | 7ffc448 | 2011-04-22 14:23:51 -0400 | [diff] [blame^] | 1542 | static int |
| 1543 | wlsc_shader_init(struct wlsc_shader *shader, |
| 1544 | const char *vertex_source, const char *fragment_source) |
| 1545 | { |
| 1546 | char msg[512]; |
| 1547 | GLint status; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1548 | |
Kristian Høgsberg | 7ffc448 | 2011-04-22 14:23:51 -0400 | [diff] [blame^] | 1549 | shader->vertex_shader = |
| 1550 | compile_shader(GL_VERTEX_SHADER, vertex_source); |
| 1551 | shader->fragment_shader = |
| 1552 | compile_shader(GL_FRAGMENT_SHADER, fragment_source); |
| 1553 | |
| 1554 | shader->program = glCreateProgram(); |
| 1555 | glAttachShader(shader->program, shader->vertex_shader); |
| 1556 | glAttachShader(shader->program, shader->fragment_shader); |
| 1557 | glBindAttribLocation(shader->program, 0, "position"); |
| 1558 | glBindAttribLocation(shader->program, 1, "texcoord"); |
| 1559 | |
| 1560 | glLinkProgram(shader->program); |
| 1561 | glGetProgramiv(shader->program, GL_LINK_STATUS, &status); |
Kristian Høgsberg | 67a21bd | 2010-06-25 18:58:24 -0400 | [diff] [blame] | 1562 | if (!status) { |
Kristian Høgsberg | 7ffc448 | 2011-04-22 14:23:51 -0400 | [diff] [blame^] | 1563 | glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg); |
Kristian Høgsberg | 67a21bd | 2010-06-25 18:58:24 -0400 | [diff] [blame] | 1564 | fprintf(stderr, "link info: %s\n", msg); |
| 1565 | return -1; |
| 1566 | } |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1567 | |
Kristian Høgsberg | 7ffc448 | 2011-04-22 14:23:51 -0400 | [diff] [blame^] | 1568 | shader->proj_uniform = glGetUniformLocation(shader->program, "proj"); |
| 1569 | shader->tex_uniform = glGetUniformLocation(shader->program, "tex"); |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1570 | |
Kristian Høgsberg | 67a21bd | 2010-06-25 18:58:24 -0400 | [diff] [blame] | 1571 | return 0; |
Kristian Høgsberg | 27803c6 | 2010-06-06 22:23:21 -0400 | [diff] [blame] | 1572 | } |
| 1573 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1574 | void |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1575 | wlsc_output_destroy(struct wlsc_output *output) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1576 | { |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1577 | destroy_surface(&output->background->surface.resource, NULL); |
| 1578 | } |
| 1579 | |
| 1580 | void |
| 1581 | wlsc_output_move(struct wlsc_output *output, int x, int y) |
| 1582 | { |
| 1583 | struct wlsc_compositor *c = output->compositor; |
Benjamin Franzke | 1b765ff | 2011-02-18 16:51:37 +0100 | [diff] [blame] | 1584 | int flip; |
| 1585 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1586 | output->x = x; |
| 1587 | output->y = y; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1588 | |
Benjamin Franzke | 264b3f9 | 2011-03-16 13:48:42 +0100 | [diff] [blame] | 1589 | if (output->background) { |
| 1590 | output->background->x = x; |
| 1591 | output->background->y = y; |
| 1592 | wlsc_surface_update_matrix(output->background); |
| 1593 | } |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1594 | |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 1595 | pixman_region32_init(&output->previous_damage_region); |
| 1596 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1597 | wlsc_matrix_init(&output->matrix); |
| 1598 | wlsc_matrix_translate(&output->matrix, |
| 1599 | -output->x - output->width / 2.0, |
| 1600 | -output->y - output->height / 2.0, 0); |
Benjamin Franzke | 1b765ff | 2011-02-18 16:51:37 +0100 | [diff] [blame] | 1601 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1602 | flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1603 | wlsc_matrix_scale(&output->matrix, |
Benjamin Franzke | 1b765ff | 2011-02-18 16:51:37 +0100 | [diff] [blame] | 1604 | 2.0 / output->width, |
| 1605 | flip * 2.0 / output->height, 1); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1606 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1607 | pixman_region32_union_rect(&c->damage_region, |
| 1608 | &c->damage_region, |
| 1609 | x, y, output->width, output->height); |
| 1610 | } |
| 1611 | |
| 1612 | void |
| 1613 | wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c, |
| 1614 | int x, int y, int width, int height, uint32_t flags) |
| 1615 | { |
| 1616 | output->compositor = c; |
| 1617 | output->x = x; |
| 1618 | output->y = y; |
| 1619 | output->width = width; |
| 1620 | output->height = height; |
| 1621 | |
| 1622 | output->background = |
| 1623 | background_create(output, option_background); |
| 1624 | |
| 1625 | output->flags = flags; |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 1626 | output->finished = 1; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1627 | wlsc_output_move(output, x, y); |
| 1628 | |
Kristian Høgsberg | b313b02 | 2010-12-01 17:07:41 -0500 | [diff] [blame] | 1629 | output->object.interface = &wl_output_interface; |
| 1630 | wl_display_add_object(c->wl_display, &output->object); |
| 1631 | wl_display_add_global(c->wl_display, &output->object, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1632 | wlsc_output_post_geometry); |
| 1633 | } |
| 1634 | |
Benjamin Franzke | 315b3dc | 2011-03-08 11:32:57 +0100 | [diff] [blame] | 1635 | static void |
| 1636 | shm_buffer_created(struct wl_buffer *buffer) |
| 1637 | { |
| 1638 | struct wl_list *surfaces_attached_to; |
| 1639 | |
| 1640 | surfaces_attached_to = malloc(sizeof *surfaces_attached_to); |
| 1641 | if (!surfaces_attached_to) { |
| 1642 | buffer->user_data = NULL; |
| 1643 | return; |
| 1644 | } |
| 1645 | |
| 1646 | wl_list_init(surfaces_attached_to); |
| 1647 | |
| 1648 | buffer->user_data = surfaces_attached_to; |
| 1649 | } |
| 1650 | |
| 1651 | static void |
| 1652 | shm_buffer_damaged(struct wl_buffer *buffer, |
| 1653 | int32_t x, int32_t y, int32_t width, int32_t height) |
| 1654 | { |
| 1655 | struct wl_list *surfaces_attached_to = buffer->user_data; |
| 1656 | struct wlsc_surface *es; |
| 1657 | |
| 1658 | wl_list_for_each(es, surfaces_attached_to, buffer_link) { |
| 1659 | glBindTexture(GL_TEXTURE_2D, es->texture); |
| 1660 | glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, |
| 1661 | buffer->width, buffer->height, 0, |
| 1662 | GL_BGRA_EXT, GL_UNSIGNED_BYTE, |
| 1663 | wl_shm_buffer_get_data(buffer)); |
| 1664 | /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't |
| 1665 | * support any unpack attributes except GL_UNPACK_ALIGNMENT. */ |
| 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | static void |
| 1670 | shm_buffer_destroyed(struct wl_buffer *buffer) |
| 1671 | { |
| 1672 | struct wl_list *surfaces_attached_to = buffer->user_data; |
| 1673 | struct wlsc_surface *es, *next; |
| 1674 | |
| 1675 | wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) { |
| 1676 | es->buffer = NULL; |
| 1677 | wl_list_remove(&es->buffer_link); |
| 1678 | } |
| 1679 | |
| 1680 | free(surfaces_attached_to); |
| 1681 | } |
| 1682 | |
| 1683 | const static struct wl_shm_callbacks shm_callbacks = { |
| 1684 | shm_buffer_created, |
| 1685 | shm_buffer_damaged, |
| 1686 | shm_buffer_destroyed |
| 1687 | }; |
| 1688 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1689 | int |
| 1690 | wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display) |
| 1691 | { |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 1692 | struct wl_event_loop *loop; |
Kristian Høgsberg | d711d0c | 2011-01-14 17:28:21 -0500 | [diff] [blame] | 1693 | const char *extensions; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1694 | |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 1695 | ec->wl_display = display; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1696 | |
Kristian Høgsberg | c5c510e | 2010-12-08 15:12:58 -0500 | [diff] [blame] | 1697 | wl_compositor_init(&ec->compositor, &compositor_interface, display); |
Kristian Høgsberg | ee02ca6 | 2008-12-21 23:37:12 -0500 | [diff] [blame] | 1698 | |
Benjamin Franzke | 315b3dc | 2011-03-08 11:32:57 +0100 | [diff] [blame] | 1699 | ec->shm = wl_shm_init(display, &shm_callbacks); |
Benjamin Franzke | faa0a9d | 2011-02-21 16:24:53 +0100 | [diff] [blame] | 1700 | eglBindWaylandDisplayWL(ec->display, ec->wl_display); |
Kristian Høgsberg | 3d5bae0 | 2010-10-06 21:17:40 -0400 | [diff] [blame] | 1701 | |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 1702 | wl_list_init(&ec->surface_list); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1703 | wl_list_init(&ec->input_device_list); |
| 1704 | wl_list_init(&ec->output_list); |
Kristian Høgsberg | 3555d09 | 2011-04-11 13:58:13 -0400 | [diff] [blame] | 1705 | wl_list_init(&ec->binding_list); |
| 1706 | |
Kristian Høgsberg | 0793756 | 2011-04-12 17:25:42 -0400 | [diff] [blame] | 1707 | wlsc_shell_init(ec); |
Kristian Høgsberg | 30021d7 | 2011-04-12 17:42:30 -0400 | [diff] [blame] | 1708 | wlsc_switcher_init(ec); |
Kristian Høgsberg | 0793756 | 2011-04-12 17:25:42 -0400 | [diff] [blame] | 1709 | |
Kristian Høgsberg | a8ec863 | 2011-04-12 17:22:49 -0400 | [diff] [blame] | 1710 | wlsc_compositor_add_binding(ec, KEY_BACKSPACE, 0, |
Kristian Høgsberg | 3555d09 | 2011-04-11 13:58:13 -0400 | [diff] [blame] | 1711 | MODIFIER_CTRL | MODIFIER_ALT, |
| 1712 | terminate_binding, ec); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1713 | |
Kristian Høgsberg | 1db21f1 | 2010-08-16 16:08:12 -0400 | [diff] [blame] | 1714 | create_pointer_images(ec); |
| 1715 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1716 | screenshooter_create(ec); |
| 1717 | |
Kristian Høgsberg | d711d0c | 2011-01-14 17:28:21 -0500 | [diff] [blame] | 1718 | extensions = (const char *) glGetString(GL_EXTENSIONS); |
| 1719 | if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) { |
| 1720 | fprintf(stderr, |
| 1721 | "GL_EXT_texture_format_BGRA8888 not available\n"); |
| 1722 | return -1; |
| 1723 | } |
| 1724 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1725 | glActiveTexture(GL_TEXTURE0); |
Kristian Høgsberg | 7ffc448 | 2011-04-22 14:23:51 -0400 | [diff] [blame^] | 1726 | |
| 1727 | if (wlsc_shader_init(&ec->texture_shader, |
| 1728 | vertex_shader, texture_fragment_shader) < 0) |
Kristian Høgsberg | a946821 | 2010-06-14 21:03:11 -0400 | [diff] [blame] | 1729 | return -1; |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 1730 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 1731 | loop = wl_display_get_event_loop(ec->wl_display); |
Kristian Høgsberg | e10a5d9 | 2011-04-22 14:01:18 -0400 | [diff] [blame] | 1732 | ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec); |
| 1733 | wl_event_source_timer_update(ec->idle_source, option_idle_time * 1000); |
| 1734 | |
Kristian Høgsberg | 4a29890 | 2008-11-28 18:35:25 -0500 | [diff] [blame] | 1735 | ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec); |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame] | 1736 | pixman_region32_init(&ec->damage_region); |
Kristian Høgsberg | 8da19ac | 2009-03-17 16:12:51 -0400 | [diff] [blame] | 1737 | wlsc_compositor_schedule_repaint(ec); |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 1738 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1739 | return 0; |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1740 | } |
| 1741 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 1742 | static int on_term_signal(int signal_number, void *data) |
Kristian Høgsberg | 50dc698 | 2010-12-01 16:43:56 -0500 | [diff] [blame] | 1743 | { |
| 1744 | struct wlsc_compositor *ec = data; |
| 1745 | |
| 1746 | wl_display_terminate(ec->wl_display); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 1747 | |
| 1748 | return 1; |
Kristian Høgsberg | 50dc698 | 2010-12-01 16:43:56 -0500 | [diff] [blame] | 1749 | } |
| 1750 | |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1751 | int main(int argc, char *argv[]) |
| 1752 | { |
| 1753 | struct wl_display *display; |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1754 | struct wlsc_compositor *ec; |
Kristian Høgsberg | 50dc698 | 2010-12-01 16:43:56 -0500 | [diff] [blame] | 1755 | struct wl_event_loop *loop; |
Kristian Høgsberg | d653126 | 2008-12-12 11:06:18 -0500 | [diff] [blame] | 1756 | GError *error = NULL; |
| 1757 | GOptionContext *context; |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 1758 | int width, height; |
Kristian Høgsberg | d653126 | 2008-12-12 11:06:18 -0500 | [diff] [blame] | 1759 | |
Kristian Høgsberg | 77fb167 | 2010-08-16 10:38:29 -0400 | [diff] [blame] | 1760 | g_type_init(); /* GdkPixbuf needs this, it seems. */ |
| 1761 | |
Kristian Høgsberg | d653126 | 2008-12-12 11:06:18 -0500 | [diff] [blame] | 1762 | context = g_option_context_new(NULL); |
| 1763 | g_option_context_add_main_entries(context, option_entries, "Wayland"); |
| 1764 | if (!g_option_context_parse(context, &argc, &argv, &error)) { |
| 1765 | fprintf(stderr, "option parsing failed: %s\n", error->message); |
| 1766 | exit(EXIT_FAILURE); |
| 1767 | } |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 1768 | if (sscanf(option_geometry, "%dx%d", &width, &height) != 2) { |
| 1769 | fprintf(stderr, "invalid geometry option: %s \n", |
| 1770 | option_geometry); |
| 1771 | exit(EXIT_FAILURE); |
| 1772 | } |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1773 | |
| 1774 | display = wl_display_create(); |
| 1775 | |
Kristian Høgsberg | a941022 | 2011-01-14 17:22:35 -0500 | [diff] [blame] | 1776 | ec = NULL; |
| 1777 | |
| 1778 | #if BUILD_WAYLAND_COMPOSITOR |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1779 | if (getenv("WAYLAND_DISPLAY")) |
| 1780 | ec = wayland_compositor_create(display, width, height); |
Kristian Høgsberg | a941022 | 2011-01-14 17:22:35 -0500 | [diff] [blame] | 1781 | #endif |
| 1782 | |
| 1783 | #if BUILD_X11_COMPOSITOR |
| 1784 | if (ec == NULL && getenv("DISPLAY")) |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 1785 | ec = x11_compositor_create(display, width, height); |
Kristian Høgsberg | a941022 | 2011-01-14 17:22:35 -0500 | [diff] [blame] | 1786 | #endif |
| 1787 | |
Benjamin Franzke | 5d00709 | 2011-04-04 00:30:25 +0200 | [diff] [blame] | 1788 | #if BUILD_OPENWFD_COMPOSITOR |
| 1789 | if (ec == NULL && getenv("OPENWFD")) |
| 1790 | ec = wfd_compositor_create(display, option_connector); |
| 1791 | #endif |
| 1792 | |
Kristian Høgsberg | a941022 | 2011-01-14 17:22:35 -0500 | [diff] [blame] | 1793 | #if BUILD_DRM_COMPOSITOR |
| 1794 | if (ec == NULL) |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 1795 | ec = drm_compositor_create(display, option_connector); |
Kristian Høgsberg | a941022 | 2011-01-14 17:22:35 -0500 | [diff] [blame] | 1796 | #endif |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1797 | |
Kristian Høgsberg | 841883b | 2008-12-05 11:19:56 -0500 | [diff] [blame] | 1798 | if (ec == NULL) { |
| 1799 | fprintf(stderr, "failed to create compositor\n"); |
| 1800 | exit(EXIT_FAILURE); |
| 1801 | } |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 1802 | |
Kristian Høgsberg | 2bb3ebe | 2010-12-01 15:36:20 -0500 | [diff] [blame] | 1803 | if (wl_display_add_socket(display, option_socket_name)) { |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1804 | fprintf(stderr, "failed to add socket: %m\n"); |
| 1805 | exit(EXIT_FAILURE); |
| 1806 | } |
| 1807 | |
Kristian Høgsberg | 50dc698 | 2010-12-01 16:43:56 -0500 | [diff] [blame] | 1808 | loop = wl_display_get_event_loop(ec->wl_display); |
| 1809 | wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec); |
| 1810 | wl_event_loop_add_signal(loop, SIGINT, on_term_signal, ec); |
| 1811 | |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1812 | wl_display_run(display); |
| 1813 | |
Benjamin Franzke | faa0a9d | 2011-02-21 16:24:53 +0100 | [diff] [blame] | 1814 | eglUnbindWaylandDisplayWL(ec->display, display); |
Kristian Høgsberg | 2bb3ebe | 2010-12-01 15:36:20 -0500 | [diff] [blame] | 1815 | wl_display_destroy(display); |
| 1816 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1817 | ec->destroy(ec); |
| 1818 | |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1819 | return 0; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1820 | } |