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 | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <string.h> |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 21 | #include <stdbool.h> |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <stdint.h> |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 24 | #include <stdarg.h> |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 25 | #include <termios.h> |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 26 | #include <i915_drm.h> |
| 27 | #include <sys/ioctl.h> |
| 28 | #include <sys/mman.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <unistd.h> |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 31 | #include <signal.h> |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 32 | #include <cairo.h> |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 33 | #include <gdk-pixbuf/gdk-pixbuf.h> |
| 34 | #include <glib.h> |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 35 | #include <sys/poll.h> |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 36 | #include <png.h> |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 37 | #include <math.h> |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 38 | #include <linux/input.h> |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 39 | #include <linux/vt.h> |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 40 | #include <xf86drmMode.h> |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 41 | #include <time.h> |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 42 | #include <fnmatch.h> |
| 43 | #include <dirent.h> |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 44 | |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 45 | #define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE |
| 46 | #include <libudev.h> |
| 47 | |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 48 | #include <GL/gl.h> |
| 49 | #include <eagle.h> |
| 50 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 51 | #include "wayland.h" |
Kristian Høgsberg | fe831a7 | 2008-12-21 21:50:23 -0500 | [diff] [blame] | 52 | #include "wayland-protocol.h" |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 53 | #include "cairo-util.h" |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 54 | #include "wayland-system-compositor.h" |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 55 | |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 56 | #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0]) |
| 57 | |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 58 | struct wl_visual { |
| 59 | struct wl_object base; |
| 60 | }; |
| 61 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 62 | struct wlsc_output { |
Kristian Høgsberg | ee02ca6 | 2008-12-21 23:37:12 -0500 | [diff] [blame] | 63 | struct wl_object base; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 64 | struct wl_list link; |
| 65 | struct egl_compositor *ec; |
| 66 | struct egl_surface *background; |
| 67 | EGLSurface surface; |
| 68 | int32_t x, y, width, height, stride; |
| 69 | |
| 70 | struct drm_mode_modeinfo *mode; |
| 71 | uint32_t fb_id; |
| 72 | uint32_t crtc_id; |
| 73 | uint32_t connector_id; |
Kristian Høgsberg | ee02ca6 | 2008-12-21 23:37:12 -0500 | [diff] [blame] | 74 | }; |
| 75 | |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 76 | struct wlsc_input_device { |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 77 | struct wl_object base; |
| 78 | int32_t x, y; |
| 79 | struct egl_compositor *ec; |
Kristian Høgsberg | 29573bc | 2008-12-11 23:27:27 -0500 | [diff] [blame] | 80 | struct egl_surface *pointer_surface; |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 81 | struct wl_list link; |
Kristian Høgsberg | 29573bc | 2008-12-11 23:27:27 -0500 | [diff] [blame] | 82 | |
| 83 | int grab; |
| 84 | struct egl_surface *grab_surface; |
Kristian Høgsberg | a7700c8 | 2008-12-12 13:48:30 -0500 | [diff] [blame] | 85 | struct egl_surface *focus_surface; |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 86 | }; |
| 87 | |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 88 | struct egl_compositor { |
| 89 | struct wl_compositor base; |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 90 | struct wl_visual argb_visual, premultiplied_argb_visual, rgb_visual; |
| 91 | |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 92 | EGLDisplay display; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 93 | EGLContext context; |
Kristian Høgsberg | 2d9cd1e | 2008-11-03 08:09:34 -0500 | [diff] [blame] | 94 | EGLConfig config; |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 95 | struct wl_display *wl_display; |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 96 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 97 | struct wl_list output_list; |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 98 | struct wl_list input_device_list; |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 99 | struct wl_list surface_list; |
| 100 | |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 101 | struct wl_event_source *term_signal_source; |
| 102 | |
| 103 | /* tty handling state */ |
| 104 | int tty_fd; |
Ray Strode | e96dcb8 | 2008-12-20 02:00:49 -0500 | [diff] [blame] | 105 | uint32_t vt_active : 1; |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 106 | |
| 107 | struct termios terminal_attributes; |
| 108 | struct wl_event_source *tty_input_source; |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 109 | struct wl_event_source *enter_vt_source; |
| 110 | struct wl_event_source *leave_vt_source; |
| 111 | |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 112 | struct udev *udev; |
| 113 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 114 | /* Repaint state. */ |
| 115 | struct wl_event_source *timer_source; |
| 116 | int repaint_needed; |
| 117 | int repaint_on_timeout; |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 118 | struct timespec previous_swap; |
| 119 | uint32_t current_frame; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 120 | }; |
| 121 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 122 | struct egl_surface { |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 123 | struct wl_surface base; |
| 124 | struct egl_compositor *compositor; |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 125 | struct wl_visual *visual; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 126 | GLuint texture; |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 127 | struct wl_map map; |
Kristian Høgsberg | 2d9cd1e | 2008-11-03 08:09:34 -0500 | [diff] [blame] | 128 | EGLSurface surface; |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 129 | int width, height; |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 130 | struct wl_list link; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 131 | }; |
| 132 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 133 | static const char *option_background = "background.jpg"; |
| 134 | |
| 135 | static const GOptionEntry option_entries[] = { |
| 136 | { "background", 'b', 0, G_OPTION_ARG_STRING, |
| 137 | &option_background, "Background image" }, |
| 138 | { NULL } |
| 139 | }; |
| 140 | |
| 141 | |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 142 | struct screenshooter { |
| 143 | struct wl_object base; |
| 144 | struct egl_compositor *ec; |
| 145 | }; |
| 146 | |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 147 | struct screenshooter_interface { |
| 148 | void (*shoot)(struct wl_client *client, struct screenshooter *shooter); |
| 149 | }; |
| 150 | |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 151 | static void |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 152 | screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter) |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 153 | { |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 154 | struct egl_compositor *ec = shooter->ec; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 155 | struct wlsc_output *output; |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 156 | GLuint stride; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 157 | char buffer[256]; |
Kristian Høgsberg | 0ea4710 | 2008-12-14 15:53:13 -0500 | [diff] [blame] | 158 | GdkPixbuf *pixbuf; |
| 159 | GError *error = NULL; |
| 160 | void *data; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 161 | int i; |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 162 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 163 | i = 0; |
| 164 | output = container_of(ec->output_list.next, struct wlsc_output, link); |
| 165 | while (&output->link != &ec->output_list) { |
| 166 | snprintf(buffer, sizeof buffer, "wayland-screenshot-%d.png", i++); |
| 167 | data = eglReadBuffer(ec->display, output->surface, GL_FRONT_LEFT, &stride); |
| 168 | pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE, |
| 169 | 8, output->width, output->height, stride, |
| 170 | NULL, NULL); |
| 171 | gdk_pixbuf_save(pixbuf, buffer, "png", &error, NULL); |
| 172 | |
| 173 | output = container_of(output->link.next, |
| 174 | struct wlsc_output, link); |
| 175 | } |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 176 | } |
| 177 | |
Kristian Høgsberg | fb6d68d | 2008-12-21 21:54:51 -0500 | [diff] [blame] | 178 | static const struct wl_message screenshooter_methods[] = { |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 179 | { "shoot", "", NULL } |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | static const struct wl_interface screenshooter_interface = { |
| 183 | "screenshooter", 1, |
| 184 | ARRAY_LENGTH(screenshooter_methods), |
| 185 | screenshooter_methods, |
| 186 | }; |
| 187 | |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 188 | struct screenshooter_interface screenshooter_implementation = { |
| 189 | screenshooter_shoot |
| 190 | }; |
| 191 | |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 192 | static struct screenshooter * |
| 193 | screenshooter_create(struct egl_compositor *ec) |
| 194 | { |
| 195 | struct screenshooter *shooter; |
| 196 | |
| 197 | shooter = malloc(sizeof *shooter); |
| 198 | if (shooter == NULL) |
| 199 | return NULL; |
| 200 | |
| 201 | shooter->base.interface = &screenshooter_interface; |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 202 | shooter->base.implementation = (void(**)(void)) &screenshooter_implementation; |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 203 | shooter->ec = ec; |
| 204 | |
| 205 | return shooter; |
| 206 | }; |
| 207 | |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 208 | static struct egl_surface * |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 209 | egl_surface_create_from_cairo_surface(struct egl_compositor *ec, |
| 210 | cairo_surface_t *surface, |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 211 | int x, int y, int width, int height) |
| 212 | { |
| 213 | struct egl_surface *es; |
| 214 | int stride; |
| 215 | void *data; |
| 216 | |
| 217 | stride = cairo_image_surface_get_stride(surface); |
| 218 | data = cairo_image_surface_get_data(surface); |
| 219 | |
| 220 | es = malloc(sizeof *es); |
| 221 | if (es == NULL) |
| 222 | return NULL; |
| 223 | |
| 224 | glGenTextures(1, &es->texture); |
| 225 | glBindTexture(GL_TEXTURE_2D, es->texture); |
| 226 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
Kristian Høgsberg | 98fed0f | 2008-12-09 13:35:35 -0500 | [diff] [blame] | 227 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT); |
| 228 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 229 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 230 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, |
| 231 | GL_BGRA, GL_UNSIGNED_BYTE, data); |
| 232 | |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 233 | es->compositor = ec; |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 234 | es->map.x = x; |
| 235 | es->map.y = y; |
| 236 | es->map.width = width; |
| 237 | es->map.height = height; |
| 238 | es->surface = EGL_NO_SURFACE; |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 239 | es->visual = &ec->premultiplied_argb_visual; |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 240 | |
| 241 | return es; |
| 242 | } |
| 243 | |
| 244 | static void |
| 245 | egl_surface_destroy(struct egl_surface *es, struct egl_compositor *ec) |
| 246 | { |
| 247 | glDeleteTextures(1, &es->texture); |
| 248 | if (es->surface != EGL_NO_SURFACE) |
| 249 | eglDestroySurface(ec->display, es->surface); |
| 250 | free(es); |
| 251 | } |
| 252 | |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 253 | static void |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 254 | pointer_path(cairo_t *cr, int x, int y) |
| 255 | { |
| 256 | const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10; |
| 257 | const int width = 16, height = 16; |
| 258 | |
| 259 | cairo_move_to(cr, x, y); |
| 260 | cairo_line_to(cr, x + tx, y + ty); |
| 261 | cairo_line_to(cr, x + dx, y + dy); |
| 262 | cairo_line_to(cr, x + width - end, y + height); |
| 263 | cairo_line_to(cr, x + width, y + height - end); |
| 264 | cairo_line_to(cr, x + dy, y + dx); |
| 265 | cairo_line_to(cr, x + ty, y + tx); |
| 266 | cairo_close_path(cr); |
| 267 | } |
| 268 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 269 | static struct egl_surface * |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 270 | pointer_create(struct egl_compositor *ec, int x, int y, int width, int height) |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 271 | { |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 272 | struct egl_surface *es; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 273 | const int hotspot_x = 16, hotspot_y = 16; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 274 | cairo_surface_t *surface; |
| 275 | cairo_t *cr; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 276 | |
| 277 | surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, |
| 278 | width, height); |
| 279 | |
| 280 | cr = cairo_create(surface); |
| 281 | pointer_path(cr, hotspot_x + 5, hotspot_y + 4); |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 282 | cairo_set_line_width(cr, 2); |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 283 | cairo_set_source_rgb(cr, 0, 0, 0); |
| 284 | cairo_stroke_preserve(cr); |
| 285 | cairo_fill(cr); |
| 286 | blur_surface(surface, width); |
| 287 | |
| 288 | pointer_path(cr, hotspot_x, hotspot_y); |
| 289 | cairo_stroke_preserve(cr); |
| 290 | cairo_set_source_rgb(cr, 1, 1, 1); |
| 291 | cairo_fill(cr); |
| 292 | cairo_destroy(cr); |
| 293 | |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 294 | es = egl_surface_create_from_cairo_surface(ec, |
| 295 | surface, |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 296 | x - hotspot_x, |
| 297 | y - hotspot_y, |
| 298 | width, height); |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 299 | |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 300 | cairo_surface_destroy(surface); |
| 301 | |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 302 | return es; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | static struct egl_surface * |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 306 | background_create(struct wlsc_output *output, const char *filename) |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 307 | { |
| 308 | struct egl_surface *background; |
| 309 | GdkPixbuf *pixbuf; |
| 310 | GError *error = NULL; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 311 | void *data; |
Ray Strode | 18fd33c | 2008-12-18 21:05:20 -0500 | [diff] [blame] | 312 | GLenum format; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 313 | |
| 314 | background = malloc(sizeof *background); |
| 315 | if (background == NULL) |
| 316 | return NULL; |
| 317 | |
| 318 | g_type_init(); |
| 319 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 320 | pixbuf = gdk_pixbuf_new_from_file_at_scale(filename, |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 321 | output->width, |
| 322 | output->height, |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 323 | FALSE, &error); |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 324 | if (error != NULL) { |
| 325 | free(background); |
| 326 | return NULL; |
| 327 | } |
| 328 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 329 | data = gdk_pixbuf_get_pixels(pixbuf); |
| 330 | |
| 331 | glGenTextures(1, &background->texture); |
| 332 | glBindTexture(GL_TEXTURE_2D, background->texture); |
| 333 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
Kristian Høgsberg | 98fed0f | 2008-12-09 13:35:35 -0500 | [diff] [blame] | 334 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT); |
| 335 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 336 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
Ray Strode | 18fd33c | 2008-12-18 21:05:20 -0500 | [diff] [blame] | 337 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 338 | if (gdk_pixbuf_get_has_alpha(pixbuf)) |
Ray Strode | 18fd33c | 2008-12-18 21:05:20 -0500 | [diff] [blame] | 339 | format = GL_RGBA; |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 340 | else |
Ray Strode | 18fd33c | 2008-12-18 21:05:20 -0500 | [diff] [blame] | 341 | format = GL_RGB; |
Ray Strode | 18fd33c | 2008-12-18 21:05:20 -0500 | [diff] [blame] | 342 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 343 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, |
| 344 | output->width, output->height, 0, |
Ray Strode | 18fd33c | 2008-12-18 21:05:20 -0500 | [diff] [blame] | 345 | format, GL_UNSIGNED_BYTE, data); |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 346 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 347 | background->compositor = output->ec; |
| 348 | background->map.x = output->x; |
| 349 | background->map.y = output->y; |
| 350 | background->map.width = output->width; |
| 351 | background->map.height = output->height; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 352 | background->surface = EGL_NO_SURFACE; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 353 | background->visual = &output->ec->rgb_visual; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 354 | |
| 355 | return background; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | static void |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 359 | draw_surface(struct egl_surface *es) |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 360 | { |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 361 | struct egl_compositor *ec = es->compositor; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 362 | GLint vertices[12]; |
| 363 | GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 }; |
| 364 | GLuint indices[4] = { 0, 1, 2, 3 }; |
| 365 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 366 | vertices[0] = es->map.x; |
| 367 | vertices[1] = es->map.y; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 368 | vertices[2] = 0; |
| 369 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 370 | vertices[3] = es->map.x; |
| 371 | vertices[4] = es->map.y + es->map.height; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 372 | vertices[5] = 0; |
| 373 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 374 | vertices[6] = es->map.x + es->map.width; |
| 375 | vertices[7] = es->map.y; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 376 | vertices[8] = 0; |
| 377 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 378 | vertices[9] = es->map.x + es->map.width; |
| 379 | vertices[10] = es->map.y + es->map.height; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 380 | vertices[11] = 0; |
| 381 | |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 382 | if (es->visual == &ec->argb_visual) { |
| 383 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 384 | glEnable(GL_BLEND); |
| 385 | } else if (es->visual == &ec->premultiplied_argb_visual) { |
| 386 | glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); |
| 387 | glEnable(GL_BLEND); |
| 388 | } else { |
| 389 | glDisable(GL_BLEND); |
| 390 | } |
| 391 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 392 | glBindTexture(GL_TEXTURE_2D, es->texture); |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 393 | glEnable(GL_TEXTURE_2D); |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 394 | glEnableClientState(GL_VERTEX_ARRAY); |
| 395 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 396 | glVertexPointer(3, GL_INT, 0, vertices); |
| 397 | glTexCoordPointer(2, GL_INT, 0, tex_coords); |
| 398 | glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices); |
| 399 | } |
| 400 | |
| 401 | static void |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 402 | repaint_output(struct wlsc_output *output) |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 403 | { |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 404 | struct egl_compositor *ec = output->ec; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 405 | struct egl_surface *es; |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 406 | struct wlsc_input_device *eid; |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 407 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 408 | if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) { |
| 409 | fprintf(stderr, "failed to make context current\n"); |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 410 | return; |
| 411 | } |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 412 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 413 | glViewport(0, 0, output->width, output->height); |
| 414 | glMatrixMode(GL_PROJECTION); |
| 415 | glLoadIdentity(); |
| 416 | glOrtho(0, output->width, output->height, 0, 0, 1000.0); |
| 417 | glMatrixMode(GL_MODELVIEW); |
| 418 | glClearColor(0, 0, 0.2, 1); |
| 419 | |
| 420 | if (output->background) |
| 421 | draw_surface(output->background); |
Kristian Høgsberg | 5b7f832 | 2008-12-18 12:08:19 -0500 | [diff] [blame] | 422 | else |
| 423 | glClear(GL_COLOR_BUFFER_BIT); |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 424 | |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 425 | es = container_of(ec->surface_list.next, |
| 426 | struct egl_surface, link); |
| 427 | while (&es->link != &ec->surface_list) { |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 428 | draw_surface(es); |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 429 | |
| 430 | es = container_of(es->link.next, |
| 431 | struct egl_surface, link); |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 432 | } |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 433 | |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 434 | eid = container_of(ec->input_device_list.next, |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 435 | struct wlsc_input_device, link); |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 436 | while (&eid->link != &ec->input_device_list) { |
| 437 | draw_surface(eid->pointer_surface); |
| 438 | |
| 439 | eid = container_of(eid->link.next, |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 440 | struct wlsc_input_device, link); |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 441 | } |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 442 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 443 | eglSwapBuffers(ec->display, output->surface); |
| 444 | } |
| 445 | |
| 446 | static void |
| 447 | repaint(void *data) |
| 448 | { |
| 449 | struct egl_compositor *ec = data; |
| 450 | struct wlsc_output *output; |
| 451 | struct timespec ts; |
| 452 | uint32_t msecs; |
| 453 | |
| 454 | if (!ec->repaint_needed) { |
| 455 | ec->repaint_on_timeout = 0; |
| 456 | return; |
| 457 | } |
| 458 | |
| 459 | output = container_of(ec->output_list.next, struct wlsc_output, link); |
| 460 | while (&output->link != &ec->output_list) { |
| 461 | repaint_output(output); |
| 462 | output = container_of(output->link.next, |
| 463 | struct wlsc_output, link); |
| 464 | } |
| 465 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 466 | ec->repaint_needed = 0; |
Kristian Høgsberg | 9af92b3 | 2008-11-24 01:12:46 -0500 | [diff] [blame] | 467 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 468 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 469 | msecs = ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000); |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 470 | wl_display_post_frame(ec->wl_display, &ec->base, |
| 471 | ec->current_frame, msecs); |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 472 | ec->current_frame++; |
| 473 | |
Kristian Høgsberg | 4a29890 | 2008-11-28 18:35:25 -0500 | [diff] [blame] | 474 | wl_event_source_timer_update(ec->timer_source, 10); |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 475 | ec->repaint_on_timeout = 1; |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 476 | } |
| 477 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 478 | static void |
| 479 | schedule_repaint(struct egl_compositor *ec) |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 480 | { |
| 481 | struct wl_event_loop *loop; |
| 482 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 483 | ec->repaint_needed = 1; |
| 484 | if (!ec->repaint_on_timeout) { |
| 485 | loop = wl_display_get_event_loop(ec->wl_display); |
Kristian Høgsberg | 4a29890 | 2008-11-28 18:35:25 -0500 | [diff] [blame] | 486 | wl_event_loop_add_idle(loop, repaint, ec); |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 487 | } |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 488 | } |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 489 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 490 | static void |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 491 | surface_destroy(struct wl_client *client, |
| 492 | struct wl_surface *surface) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 493 | { |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 494 | struct egl_surface *es = (struct egl_surface *) surface; |
| 495 | struct egl_compositor *ec = es->compositor; |
Kristian Høgsberg | 2d9cd1e | 2008-11-03 08:09:34 -0500 | [diff] [blame] | 496 | |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 497 | wl_list_remove(&es->link); |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 498 | egl_surface_destroy(es, ec); |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 499 | |
| 500 | schedule_repaint(ec); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 501 | } |
| 502 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 503 | static void |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 504 | surface_attach(struct wl_client *client, |
| 505 | struct wl_surface *surface, uint32_t name, |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 506 | uint32_t width, uint32_t height, uint32_t stride, |
| 507 | struct wl_object *visual) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 508 | { |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 509 | struct egl_surface *es = (struct egl_surface *) surface; |
| 510 | struct egl_compositor *ec = es->compositor; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 511 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 512 | if (es->surface != EGL_NO_SURFACE) |
| 513 | eglDestroySurface(ec->display, es->surface); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 514 | |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 515 | es->width = width; |
| 516 | es->height = height; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 517 | es->surface = eglCreateSurfaceForName(ec->display, ec->config, |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 518 | name, width, height, stride, NULL); |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 519 | if (visual == &ec->argb_visual.base) |
| 520 | es->visual = &ec->argb_visual; |
| 521 | else if (visual == &ec->premultiplied_argb_visual.base) |
| 522 | es->visual = &ec->premultiplied_argb_visual; |
Kristian Høgsberg | e10b828 | 2008-12-18 19:58:44 -0500 | [diff] [blame] | 523 | else if (visual == &ec->rgb_visual.base) |
| 524 | es->visual = &ec->rgb_visual; |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 525 | else |
| 526 | /* FIXME: Smack client with an exception event */; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 527 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 528 | glBindTexture(GL_TEXTURE_2D, es->texture); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 529 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
Kristian Høgsberg | 98fed0f | 2008-12-09 13:35:35 -0500 | [diff] [blame] | 530 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT); |
| 531 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 532 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 533 | eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D); |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 534 | } |
| 535 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 536 | static void |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 537 | surface_map(struct wl_client *client, |
| 538 | struct wl_surface *surface, |
| 539 | int32_t x, int32_t y, int32_t width, int32_t height) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 540 | { |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 541 | struct egl_surface *es = (struct egl_surface *) surface; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 542 | |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 543 | es->map.x = x; |
| 544 | es->map.y = y; |
| 545 | es->map.width = width; |
| 546 | es->map.height = height; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 547 | } |
| 548 | |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 549 | static void |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 550 | surface_copy(struct wl_client *client, |
| 551 | struct wl_surface *surface, |
| 552 | int32_t dst_x, int32_t dst_y, |
| 553 | uint32_t name, uint32_t stride, |
| 554 | 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] | 555 | { |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 556 | struct egl_surface *es = (struct egl_surface *) surface; |
| 557 | struct egl_compositor *ec = es->compositor; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 558 | EGLSurface src; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 559 | |
| 560 | /* FIXME: glCopyPixels should work, but then we'll have to |
| 561 | * call eglMakeCurrent to set up the src and dest surfaces |
| 562 | * first. This seems cheaper, but maybe there's a better way |
| 563 | * to accomplish this. */ |
| 564 | |
| 565 | src = eglCreateSurfaceForName(ec->display, ec->config, |
| 566 | name, x + width, y + height, stride, NULL); |
| 567 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 568 | eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y, |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 569 | src, GL_FRONT_LEFT, x, y, width, height); |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 570 | eglDestroySurface(ec->display, src); |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | static void |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 574 | surface_damage(struct wl_client *client, |
| 575 | struct wl_surface *surface, |
| 576 | 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] | 577 | { |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 578 | /* FIXME: This need to take a damage region, of course. */ |
| 579 | } |
| 580 | |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 581 | const static struct wl_surface_interface surface_interface = { |
| 582 | surface_destroy, |
| 583 | surface_attach, |
| 584 | surface_map, |
| 585 | surface_copy, |
| 586 | surface_damage |
| 587 | }; |
| 588 | |
| 589 | static void |
| 590 | compositor_create_surface(struct wl_client *client, |
| 591 | struct wl_compositor *compositor, uint32_t id) |
| 592 | { |
| 593 | struct egl_compositor *ec = (struct egl_compositor *) compositor; |
| 594 | struct egl_surface *es; |
| 595 | |
| 596 | es = malloc(sizeof *es); |
| 597 | if (es == NULL) |
| 598 | /* FIXME: Send OOM event. */ |
| 599 | return; |
| 600 | |
| 601 | es->compositor = ec; |
| 602 | es->surface = EGL_NO_SURFACE; |
| 603 | wl_list_insert(ec->surface_list.prev, &es->link); |
| 604 | glGenTextures(1, &es->texture); |
| 605 | wl_client_add_surface(client, &es->base, |
| 606 | &surface_interface, id); |
| 607 | } |
| 608 | |
| 609 | static void |
| 610 | compositor_commit(struct wl_client *client, |
| 611 | struct wl_compositor *compositor, uint32_t key) |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 612 | { |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 613 | struct egl_compositor *ec = (struct egl_compositor *) compositor; |
| 614 | |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 615 | schedule_repaint(ec); |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 616 | wl_client_send_acknowledge(client, compositor, key, ec->current_frame); |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 617 | } |
| 618 | |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 619 | const static struct wl_compositor_interface compositor_interface = { |
| 620 | compositor_create_surface, |
| 621 | compositor_commit |
| 622 | }; |
| 623 | |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 624 | static struct egl_surface * |
Kristian Høgsberg | 7e972a5 | 2008-12-21 17:26:00 -0500 | [diff] [blame] | 625 | pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy) |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 626 | { |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 627 | struct egl_compositor *ec = device->ec; |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 628 | struct egl_surface *es; |
| 629 | |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 630 | if (device->grab > 0) |
| 631 | return device->grab_surface; |
| 632 | |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 633 | es = container_of(ec->surface_list.prev, |
| 634 | struct egl_surface, link); |
| 635 | while (&es->link != &ec->surface_list) { |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 636 | if (es->map.x <= device->x && |
| 637 | device->x < es->map.x + es->map.width && |
| 638 | es->map.y <= device->y && |
| 639 | device->y < es->map.y + es->map.height) |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 640 | return es; |
| 641 | |
| 642 | es = container_of(es->link.prev, |
| 643 | struct egl_surface, link); |
Kristian Høgsberg | 7e972a5 | 2008-12-21 17:26:00 -0500 | [diff] [blame] | 644 | |
| 645 | /* Transform to surface coordinates. */ |
Kristian Høgsberg | e3ef3e5 | 2008-12-21 19:30:01 -0500 | [diff] [blame] | 646 | *sx = (device->x - es->map.x) * es->width / es->map.width; |
| 647 | *sy = (device->y - es->map.y) * es->height / es->map.height; |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | return NULL; |
| 651 | } |
| 652 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 653 | void |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 654 | notify_motion(struct wlsc_input_device *device, int x, int y) |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 655 | { |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 656 | struct egl_surface *es; |
Ray Strode | 90e701d | 2008-12-18 23:05:43 -0500 | [diff] [blame] | 657 | struct egl_compositor *ec = device->ec; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 658 | struct wlsc_output *output; |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 659 | const int hotspot_x = 16, hotspot_y = 16; |
| 660 | int32_t sx, sy; |
| 661 | |
Ray Strode | e96dcb8 | 2008-12-20 02:00:49 -0500 | [diff] [blame] | 662 | if (!ec->vt_active) |
| 663 | return; |
| 664 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 665 | /* FIXME: We need some multi head love here. */ |
| 666 | output = container_of(ec->output_list.next, struct wlsc_output, link); |
| 667 | if (x < output->x) |
Ray Strode | 90e701d | 2008-12-18 23:05:43 -0500 | [diff] [blame] | 668 | x = 0; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 669 | if (y < output->y) |
Ray Strode | 90e701d | 2008-12-18 23:05:43 -0500 | [diff] [blame] | 670 | y = 0; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 671 | if (x >= output->x + output->width) |
| 672 | x = output->x + output->width - 1; |
| 673 | if (y >= output->y + output->height) |
| 674 | y = output->y + output->height - 1; |
Ray Strode | 90e701d | 2008-12-18 23:05:43 -0500 | [diff] [blame] | 675 | |
Kristian Høgsberg | e3ef3e5 | 2008-12-21 19:30:01 -0500 | [diff] [blame] | 676 | device->x = x; |
| 677 | device->y = y; |
Kristian Høgsberg | 7e972a5 | 2008-12-21 17:26:00 -0500 | [diff] [blame] | 678 | es = pick_surface(device, &sx, &sy); |
| 679 | if (es) |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 680 | wl_surface_post_event(&es->base, &device->base, |
Kristian Høgsberg | 5a75c90 | 2008-12-10 13:16:50 -0500 | [diff] [blame] | 681 | WL_INPUT_MOTION, x, y, sx, sy); |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 682 | |
Kristian Høgsberg | 29573bc | 2008-12-11 23:27:27 -0500 | [diff] [blame] | 683 | device->pointer_surface->map.x = x - hotspot_x; |
| 684 | device->pointer_surface->map.y = y - hotspot_y; |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 685 | |
| 686 | schedule_repaint(device->ec); |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 687 | } |
| 688 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 689 | void |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 690 | notify_button(struct wlsc_input_device *device, |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 691 | int32_t button, int32_t state) |
Kristian Høgsberg | eac149a | 2008-12-10 00:24:18 -0500 | [diff] [blame] | 692 | { |
Kristian Høgsberg | eac149a | 2008-12-10 00:24:18 -0500 | [diff] [blame] | 693 | struct egl_surface *es; |
Ray Strode | e96dcb8 | 2008-12-20 02:00:49 -0500 | [diff] [blame] | 694 | struct egl_compositor *ec = device->ec; |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 695 | int32_t sx, sy; |
Kristian Høgsberg | eac149a | 2008-12-10 00:24:18 -0500 | [diff] [blame] | 696 | |
Ray Strode | e96dcb8 | 2008-12-20 02:00:49 -0500 | [diff] [blame] | 697 | if (!ec->vt_active) |
| 698 | return; |
| 699 | |
Kristian Høgsberg | 7e972a5 | 2008-12-21 17:26:00 -0500 | [diff] [blame] | 700 | es = pick_surface(device, &sx, &sy); |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 701 | if (es) { |
| 702 | wl_list_remove(&es->link); |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 703 | wl_list_insert(device->ec->surface_list.prev, &es->link); |
Kristian Høgsberg | 5a75c90 | 2008-12-10 13:16:50 -0500 | [diff] [blame] | 704 | |
Kristian Høgsberg | 29573bc | 2008-12-11 23:27:27 -0500 | [diff] [blame] | 705 | if (state) { |
Kristian Høgsberg | a7700c8 | 2008-12-12 13:48:30 -0500 | [diff] [blame] | 706 | /* FIXME: We need callbacks when the surfaces |
| 707 | * we reference here go away. */ |
Kristian Høgsberg | 29573bc | 2008-12-11 23:27:27 -0500 | [diff] [blame] | 708 | device->grab++; |
| 709 | device->grab_surface = es; |
Kristian Høgsberg | a7700c8 | 2008-12-12 13:48:30 -0500 | [diff] [blame] | 710 | device->focus_surface = es; |
Kristian Høgsberg | 29573bc | 2008-12-11 23:27:27 -0500 | [diff] [blame] | 711 | } else { |
| 712 | device->grab--; |
| 713 | } |
| 714 | |
Kristian Høgsberg | 5a75c90 | 2008-12-10 13:16:50 -0500 | [diff] [blame] | 715 | /* FIXME: Swallow click on raise? */ |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 716 | wl_surface_post_event(&es->base, &device->base, |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 717 | WL_INPUT_BUTTON, button, state, |
| 718 | device->x, device->y, sx, sy); |
Kristian Høgsberg | eac149a | 2008-12-10 00:24:18 -0500 | [diff] [blame] | 719 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 720 | schedule_repaint(device->ec); |
| 721 | } |
Kristian Høgsberg | eac149a | 2008-12-10 00:24:18 -0500 | [diff] [blame] | 722 | } |
| 723 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 724 | void |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 725 | notify_key(struct wlsc_input_device *device, |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 726 | uint32_t key, uint32_t state) |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 727 | { |
Ray Strode | e96dcb8 | 2008-12-20 02:00:49 -0500 | [diff] [blame] | 728 | struct egl_compositor *ec = device->ec; |
| 729 | |
| 730 | if (!ec->vt_active) |
| 731 | return; |
| 732 | |
Kristian Høgsberg | 2c0e56b | 2008-12-19 13:54:40 -0500 | [diff] [blame] | 733 | if (device->focus_surface != NULL) |
| 734 | wl_surface_post_event(&device->focus_surface->base, |
| 735 | &device->base, |
| 736 | WL_INPUT_KEY, key, state); |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 737 | } |
| 738 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 739 | struct evdev_input_device * |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 740 | evdev_input_device_create(struct wlsc_input_device *device, |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 741 | struct wl_display *display, const char *path); |
| 742 | |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 743 | static struct wlsc_input_device * |
| 744 | create_input_device(struct egl_compositor *ec) |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 745 | { |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 746 | struct wlsc_input_device *device; |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 747 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 748 | device = malloc(sizeof *device); |
| 749 | if (device == NULL) |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 750 | return NULL; |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 751 | |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 752 | memset(device, 0, sizeof *device); |
Kristian Høgsberg | fe831a7 | 2008-12-21 21:50:23 -0500 | [diff] [blame] | 753 | device->base.interface = &wl_input_device_interface; |
Kristian Høgsberg | b3131d9 | 2008-12-24 19:30:25 -0500 | [diff] [blame] | 754 | device->base.implementation = NULL; |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 755 | wl_display_add_object(ec->wl_display, &device->base); |
Kristian Høgsberg | b3131d9 | 2008-12-24 19:30:25 -0500 | [diff] [blame] | 756 | wl_display_add_global(ec->wl_display, &device->base, NULL); |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 757 | device->x = 100; |
| 758 | device->y = 100; |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 759 | device->ec = ec; |
| 760 | |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 761 | wl_list_insert(ec->input_device_list.prev, &device->link); |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 762 | |
| 763 | return device; |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | void |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 767 | wlsc_device_get_position(struct wlsc_input_device *device, int32_t *x, int32_t *y) |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 768 | { |
| 769 | *x = device->x; |
| 770 | *y = device->y; |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 771 | } |
| 772 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 773 | static void |
| 774 | post_output_geometry(struct wl_client *client, struct wl_object *global) |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 775 | { |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 776 | struct wlsc_output *output = |
| 777 | container_of(global, struct wlsc_output, base); |
| 778 | |
| 779 | wl_client_post_event(client, global, |
| 780 | WL_OUTPUT_GEOMETRY, |
| 781 | output->width, output->height); |
| 782 | } |
| 783 | |
| 784 | static const char * |
| 785 | get_udev_property(struct udev_device *device, const char *name) |
| 786 | { |
| 787 | struct udev_list_entry *entry; |
| 788 | |
| 789 | udev_list_entry_foreach(entry, udev_device_get_properties_list_entry(device)) |
| 790 | if (strcmp(udev_list_entry_get_name(entry), name) == 0) |
| 791 | return udev_list_entry_get_value(entry); |
| 792 | |
| 793 | return NULL; |
| 794 | } |
| 795 | |
| 796 | struct dri_driver_entry { |
| 797 | uint32_t vendor_id; |
| 798 | uint32_t chip_id; |
| 799 | const char *driver; |
| 800 | }; |
| 801 | |
| 802 | static const struct dri_driver_entry driver_map[] = { |
| 803 | /* FIXME: We need to extract this table from the dri drivers |
| 804 | * and store it on disk. For now, map my i965 to i965, |
| 805 | * anything else intel to i915 and that's that. */ |
| 806 | |
| 807 | { 0x8086, 0x2a02, "i965" }, |
| 808 | { 0x8086, ~0, "i915" }, |
| 809 | { 0, } |
| 810 | }; |
| 811 | |
| 812 | static const char * |
| 813 | get_driver_for_device(struct udev_device *device) |
| 814 | { |
| 815 | struct udev_device *parent; |
| 816 | const char *pci_id; |
| 817 | uint32_t vendor_id, chip_id; |
| 818 | int i; |
| 819 | |
| 820 | parent = udev_device_get_parent(device); |
| 821 | pci_id = get_udev_property(parent, "PCI_ID"); |
| 822 | if (sscanf(pci_id, "%x:%x", &vendor_id, &chip_id) != 2) |
| 823 | return NULL; |
| 824 | |
| 825 | for (i = 0; i < ARRAY_LENGTH(driver_map); i++) { |
| 826 | if (driver_map[i].vendor_id == vendor_id && |
| 827 | (driver_map[i].chip_id == ~0 || driver_map[i].chip_id == chip_id)) |
| 828 | return driver_map[i].driver; |
| 829 | } |
| 830 | |
| 831 | return NULL; |
| 832 | } |
| 833 | |
| 834 | static int |
| 835 | init_egl(struct egl_compositor *ec, struct udev_device *device) |
| 836 | { |
| 837 | static const EGLint config_attribs[] = { |
| 838 | EGL_DEPTH_SIZE, 0, |
| 839 | EGL_STENCIL_SIZE, 0, |
| 840 | EGL_CONFIG_CAVEAT, EGL_NONE, |
| 841 | EGL_NONE |
| 842 | }; |
| 843 | |
| 844 | const char *path, *driver; |
| 845 | EGLint major, minor; |
| 846 | |
| 847 | path = udev_device_get_devnode(device); |
| 848 | driver = get_driver_for_device(device); |
| 849 | if (driver == NULL) { |
| 850 | fprintf(stderr, "didn't find driver for %s\n", |
| 851 | udev_device_get_devpath(device)); |
| 852 | return -1; |
| 853 | } |
| 854 | |
| 855 | ec->display = eglCreateDisplayNative(path, driver); |
| 856 | if (ec->display == NULL) { |
| 857 | fprintf(stderr, "failed to create display\n"); |
| 858 | return -1; |
| 859 | } |
| 860 | |
| 861 | if (!eglInitialize(ec->display, &major, &minor)) { |
| 862 | fprintf(stderr, "failed to initialize display\n"); |
| 863 | return -1; |
| 864 | } |
| 865 | |
| 866 | if (!eglChooseConfig(ec->display, config_attribs, &ec->config, 1, NULL)) |
| 867 | return -1; |
| 868 | |
| 869 | ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL); |
| 870 | if (ec->context == NULL) { |
| 871 | fprintf(stderr, "failed to create context\n"); |
| 872 | return -1; |
| 873 | } |
| 874 | return 0; |
| 875 | } |
| 876 | |
| 877 | static int |
| 878 | create_output(struct egl_compositor *ec, struct udev_device *device) |
| 879 | { |
| 880 | const static EGLint surface_attribs[] = { |
| 881 | EGL_RENDER_BUFFER, EGL_BACK_BUFFER, |
| 882 | EGL_NONE |
| 883 | }; |
| 884 | |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 885 | drmModeConnector *connector; |
| 886 | drmModeRes *resources; |
| 887 | drmModeEncoder *encoder; |
| 888 | struct drm_mode_modeinfo *mode; |
| 889 | struct drm_i915_gem_create create; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 890 | struct drm_gem_flink flink; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 891 | struct wlsc_output *output; |
Kristian Høgsberg | 2c875bd | 2008-12-19 10:34:02 -0500 | [diff] [blame] | 892 | int i, ret, fd; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 893 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 894 | if (ec->display == NULL && init_egl(ec, device) < 0) { |
| 895 | fprintf(stderr, "failed to initialize egl\n"); |
| 896 | return -1; |
| 897 | } |
| 898 | |
| 899 | output = malloc(sizeof *output); |
| 900 | if (output == NULL) |
| 901 | return -1; |
| 902 | |
Kristian Høgsberg | 2c875bd | 2008-12-19 10:34:02 -0500 | [diff] [blame] | 903 | fd = eglGetDisplayFD(ec->display); |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 904 | resources = drmModeGetResources(fd); |
| 905 | if (!resources) { |
| 906 | fprintf(stderr, "drmModeGetResources failed\n"); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 907 | return -1; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | for (i = 0; i < resources->count_connectors; i++) { |
| 911 | connector = drmModeGetConnector(fd, resources->connectors[i]); |
| 912 | if (connector == NULL) |
| 913 | continue; |
| 914 | |
| 915 | if (connector->connection == DRM_MODE_CONNECTED && |
| 916 | connector->count_modes > 0) |
| 917 | break; |
| 918 | |
| 919 | drmModeFreeConnector(connector); |
| 920 | } |
| 921 | |
| 922 | if (i == resources->count_connectors) { |
| 923 | fprintf(stderr, "No currently active connector found.\n"); |
| 924 | return -1; |
| 925 | } |
| 926 | |
| 927 | mode = &connector->modes[0]; |
| 928 | |
| 929 | for (i = 0; i < resources->count_encoders; i++) { |
| 930 | encoder = drmModeGetEncoder(fd, resources->encoders[i]); |
| 931 | |
| 932 | if (encoder == NULL) |
| 933 | continue; |
| 934 | |
| 935 | if (encoder->encoder_id == connector->encoder_id) |
| 936 | break; |
| 937 | |
| 938 | drmModeFreeEncoder(encoder); |
| 939 | } |
| 940 | |
| 941 | /* Mode size at 32 bpp */ |
| 942 | create.size = mode->hdisplay * mode->vdisplay * 4; |
| 943 | if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) { |
| 944 | fprintf(stderr, "gem create failed: %m\n"); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 945 | return -1; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 946 | } |
| 947 | |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 948 | ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay, |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 949 | 32, 32, mode->hdisplay * 4, create.handle, &output->fb_id); |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 950 | if (ret) { |
| 951 | fprintf(stderr, "failed to add fb: %m\n"); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 952 | return -1; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 953 | } |
| 954 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 955 | ret = drmModeSetCrtc(fd, encoder->crtc_id, output->fb_id, 0, 0, |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 956 | &connector->connector_id, 1, mode); |
| 957 | if (ret) { |
| 958 | fprintf(stderr, "failed to set mode: %m\n"); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 959 | return -1; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | flink.handle = create.handle; |
| 963 | if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) { |
| 964 | fprintf(stderr, "gem flink failed: %m\n"); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 965 | return -1; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 966 | } |
| 967 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 968 | output->ec = ec; |
| 969 | output->crtc_id = encoder->crtc_id; |
| 970 | output->connector_id = connector->connector_id; |
| 971 | output->mode = mode; |
| 972 | output->width = mode->hdisplay; |
| 973 | output->height = mode->vdisplay; |
| 974 | output->stride = mode->hdisplay * 4; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 975 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 976 | output->surface = eglCreateSurfaceForName(ec->display, ec->config, |
| 977 | flink.name, |
| 978 | output->width, output->height, |
| 979 | output->stride, |
| 980 | surface_attribs); |
| 981 | if (output->surface == NULL) { |
| 982 | fprintf(stderr, "failed to create surface\n"); |
| 983 | return -1; |
| 984 | } |
| 985 | |
| 986 | output->base.interface = &wl_output_interface; |
| 987 | wl_display_add_object(ec->wl_display, &output->base); |
| 988 | wl_display_add_global(ec->wl_display, &output->base, post_output_geometry); |
| 989 | wl_list_insert(ec->output_list.prev, &output->link); |
| 990 | |
| 991 | if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) { |
| 992 | fprintf(stderr, "failed to make context current\n"); |
| 993 | return -1; |
| 994 | } |
| 995 | |
| 996 | output->background = background_create(output, option_background); |
| 997 | |
| 998 | return 0; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 999 | } |
| 1000 | |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 1001 | static const struct wl_interface visual_interface = { |
| 1002 | "visual", 1, |
| 1003 | }; |
| 1004 | |
| 1005 | static void |
| 1006 | add_visuals(struct egl_compositor *ec) |
| 1007 | { |
| 1008 | ec->argb_visual.base.interface = &visual_interface; |
| 1009 | ec->argb_visual.base.implementation = NULL; |
| 1010 | wl_display_add_object(ec->wl_display, &ec->argb_visual.base); |
Kristian Høgsberg | ee02ca6 | 2008-12-21 23:37:12 -0500 | [diff] [blame] | 1011 | wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL); |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 1012 | |
| 1013 | ec->premultiplied_argb_visual.base.interface = &visual_interface; |
| 1014 | ec->premultiplied_argb_visual.base.implementation = NULL; |
| 1015 | wl_display_add_object(ec->wl_display, |
| 1016 | &ec->premultiplied_argb_visual.base); |
| 1017 | wl_display_add_global(ec->wl_display, |
Kristian Høgsberg | ee02ca6 | 2008-12-21 23:37:12 -0500 | [diff] [blame] | 1018 | &ec->premultiplied_argb_visual.base, NULL); |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 1019 | |
| 1020 | ec->rgb_visual.base.interface = &visual_interface; |
| 1021 | ec->rgb_visual.base.implementation = NULL; |
| 1022 | wl_display_add_object(ec->wl_display, &ec->rgb_visual.base); |
Kristian Høgsberg | ee02ca6 | 2008-12-21 23:37:12 -0500 | [diff] [blame] | 1023 | wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL); |
| 1024 | } |
| 1025 | |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 1026 | static void on_enter_vt(int signal_number, void *data) |
| 1027 | { |
| 1028 | struct egl_compositor *ec = data; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 1029 | struct wlsc_output *output; |
Kristian Høgsberg | 2c875bd | 2008-12-19 10:34:02 -0500 | [diff] [blame] | 1030 | int ret, fd; |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 1031 | |
Kristian Høgsberg | 38ccd3a | 2008-12-19 10:15:35 -0500 | [diff] [blame] | 1032 | ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ); |
Ray Strode | e96dcb8 | 2008-12-20 02:00:49 -0500 | [diff] [blame] | 1033 | ec->vt_active = TRUE; |
Kristian Høgsberg | 38ccd3a | 2008-12-19 10:15:35 -0500 | [diff] [blame] | 1034 | |
Kristian Høgsberg | 2c875bd | 2008-12-19 10:34:02 -0500 | [diff] [blame] | 1035 | fd = eglGetDisplayFD(ec->display); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 1036 | output = container_of(ec->output_list.next, struct wlsc_output, link); |
| 1037 | while (&output->link != &ec->output_list) { |
| 1038 | ret = drmModeSetCrtc(fd, output->crtc_id, output->fb_id, 0, 0, |
| 1039 | &output->connector_id, 1, output->mode); |
| 1040 | if (ret) |
| 1041 | fprintf(stderr, "failed to set mode for connector %d: %m\n", |
| 1042 | output->connector_id); |
| 1043 | |
| 1044 | output = container_of(output->link.next, |
| 1045 | struct wlsc_output, link); |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 1046 | } |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | static void on_leave_vt(int signal_number, void *data) |
| 1050 | { |
| 1051 | struct egl_compositor *ec = data; |
| 1052 | |
| 1053 | ioctl (ec->tty_fd, VT_RELDISP, 1); |
Ray Strode | e96dcb8 | 2008-12-20 02:00:49 -0500 | [diff] [blame] | 1054 | ec->vt_active = FALSE; |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 1055 | } |
| 1056 | |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 1057 | static void |
| 1058 | on_tty_input(int fd, uint32_t mask, void *data) |
| 1059 | { |
| 1060 | struct egl_compositor *ec = data; |
| 1061 | |
| 1062 | /* Ignore input to tty. We get keyboard events from evdev |
| 1063 | */ |
| 1064 | tcflush(ec->tty_fd, TCIFLUSH); |
| 1065 | } |
| 1066 | |
| 1067 | static void on_term_signal(int signal_number, void *data) |
| 1068 | { |
| 1069 | struct egl_compositor *ec = data; |
| 1070 | |
| 1071 | if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0) |
| 1072 | fprintf(stderr, "could not restore terminal to canonical mode\n"); |
| 1073 | |
| 1074 | exit(0); |
| 1075 | } |
| 1076 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1077 | static int setup_tty(struct egl_compositor *ec, struct wl_event_loop *loop) |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 1078 | { |
| 1079 | struct termios raw_attributes; |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1080 | struct vt_mode mode = { 0 }; |
| 1081 | |
| 1082 | ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY); |
| 1083 | if (ec->tty_fd <= 0) { |
| 1084 | fprintf(stderr, "failed to open active tty: %m\n"); |
| 1085 | return -1; |
| 1086 | } |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 1087 | |
| 1088 | if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) { |
| 1089 | fprintf(stderr, "could not get terminal attributes: %m\n"); |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1090 | return -1; |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 1091 | } |
| 1092 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1093 | /* Ignore control characters and disable echo */ |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 1094 | raw_attributes = ec->terminal_attributes; |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1095 | cfmakeraw(&raw_attributes); |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 1096 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1097 | /* Fix up line endings to be normal (cfmakeraw hoses them) */ |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 1098 | raw_attributes.c_oflag |= OPOST | OCRNL; |
| 1099 | |
| 1100 | if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0) |
| 1101 | fprintf(stderr, "could not put terminal into raw mode: %m\n"); |
| 1102 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1103 | ec->term_signal_source = |
| 1104 | wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec); |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 1105 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1106 | ec->tty_input_source = |
| 1107 | wl_event_loop_add_fd(loop, ec->tty_fd, |
| 1108 | WL_EVENT_READABLE, on_tty_input, ec); |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 1109 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1110 | ec->vt_active = TRUE; |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 1111 | mode.mode = VT_PROCESS; |
| 1112 | mode.relsig = SIGUSR1; |
| 1113 | mode.acqsig = SIGUSR2; |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1114 | if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) { |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 1115 | fprintf(stderr, "failed to take control of vt handling\n"); |
| 1116 | } |
| 1117 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1118 | ec->leave_vt_source = |
| 1119 | wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec); |
| 1120 | ec->enter_vt_source = |
| 1121 | wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec); |
Kristian Høgsberg | fe831a7 | 2008-12-21 21:50:23 -0500 | [diff] [blame] | 1122 | |
| 1123 | return 0; |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 1124 | } |
| 1125 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 1126 | static int |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 1127 | init_libudev(struct egl_compositor *ec) |
| 1128 | { |
| 1129 | struct udev_enumerate *e; |
| 1130 | struct udev_list_entry *entry; |
| 1131 | struct udev_device *device; |
| 1132 | const char *path, *seat; |
| 1133 | struct wlsc_input_device *input_device; |
| 1134 | |
| 1135 | /* FIXME: Newer (version 135+) udev has two new features that |
| 1136 | * make all this much easier: 1) we can enumerate by a |
| 1137 | * specific property. This lets us directly iterate through |
| 1138 | * the devices we care about. 2) We can attach properties to |
| 1139 | * sysfs nodes without a device file, which lets us configure |
| 1140 | * which connectors belong to a seat instead of tagging the |
| 1141 | * overall drm node. I don't want to update my system udev, |
| 1142 | * so I'm going to stick with this until the new version is in |
| 1143 | * rawhide. */ |
| 1144 | |
| 1145 | ec->udev = udev_new(); |
| 1146 | if (ec->udev == NULL) { |
| 1147 | fprintf(stderr, "failed to initialize udev context\n"); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 1148 | return -1; |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | input_device = create_input_device(ec); |
| 1152 | |
| 1153 | e = udev_enumerate_new(ec->udev); |
| 1154 | udev_enumerate_scan_devices(e); |
| 1155 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
| 1156 | path = udev_list_entry_get_name(entry); |
| 1157 | device = udev_device_new_from_syspath(ec->udev, path); |
| 1158 | |
| 1159 | /* FIXME: Should the property namespace be CK for console kit? */ |
| 1160 | seat = get_udev_property(device, "WAYLAND_SEAT"); |
| 1161 | if (!seat || strcmp(seat, "1") != 0) |
| 1162 | continue; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 1163 | |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 1164 | if (strcmp(udev_device_get_subsystem(device), "input") == 0) { |
| 1165 | evdev_input_device_create(input_device, ec->wl_display, |
| 1166 | udev_device_get_devnode(device)); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 1167 | } else if (strcmp(udev_device_get_subsystem(device), "drm") == 0) { |
| 1168 | if (create_output(ec, device) < 0) { |
| 1169 | fprintf(stderr, "failed to create output for %s\n", path); |
| 1170 | return -1; |
| 1171 | } |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 1172 | } |
| 1173 | } |
| 1174 | udev_enumerate_unref(e); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 1175 | |
| 1176 | /* Create the pointer surface now that we have a current EGL context. */ |
| 1177 | input_device->pointer_surface = |
| 1178 | pointer_create(ec, input_device->x, input_device->y, 64, 64); |
| 1179 | |
| 1180 | return 0; |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 1181 | } |
| 1182 | |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1183 | static struct egl_compositor * |
| 1184 | egl_compositor_create(struct wl_display *display) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1185 | { |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1186 | struct egl_compositor *ec; |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 1187 | struct screenshooter *shooter; |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 1188 | struct wl_event_loop *loop; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1189 | |
| 1190 | ec = malloc(sizeof *ec); |
| 1191 | if (ec == NULL) |
| 1192 | return NULL; |
| 1193 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 1194 | memset(ec, 0, sizeof *ec); |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 1195 | ec->wl_display = display; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1196 | |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 1197 | wl_display_set_compositor(display, &ec->base, &compositor_interface); |
Kristian Høgsberg | ee02ca6 | 2008-12-21 23:37:12 -0500 | [diff] [blame] | 1198 | |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 1199 | add_visuals(ec); |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 1200 | |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 1201 | wl_list_init(&ec->surface_list); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame^] | 1202 | wl_list_init(&ec->input_device_list); |
| 1203 | wl_list_init(&ec->output_list); |
| 1204 | if (init_libudev(ec) < 0) { |
| 1205 | fprintf(stderr, "failed to initialize devices\n"); |
| 1206 | return NULL; |
| 1207 | } |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 1208 | |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 1209 | shooter = screenshooter_create(ec); |
| 1210 | wl_display_add_object(display, &shooter->base); |
Kristian Høgsberg | ee02ca6 | 2008-12-21 23:37:12 -0500 | [diff] [blame] | 1211 | wl_display_add_global(display, &shooter->base, NULL); |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 1212 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 1213 | loop = wl_display_get_event_loop(ec->wl_display); |
Ray Strode | e96dcb8 | 2008-12-20 02:00:49 -0500 | [diff] [blame] | 1214 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1215 | setup_tty(ec, loop); |
| 1216 | |
Kristian Høgsberg | 4a29890 | 2008-11-28 18:35:25 -0500 | [diff] [blame] | 1217 | ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec); |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 1218 | schedule_repaint(ec); |
| 1219 | |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1220 | return ec; |
| 1221 | } |
| 1222 | |
| 1223 | /* The plan here is to generate a random anonymous socket name and |
| 1224 | * advertise that through a service on the session dbus. |
| 1225 | */ |
| 1226 | static const char socket_name[] = "\0wayland"; |
| 1227 | |
| 1228 | int main(int argc, char *argv[]) |
| 1229 | { |
| 1230 | struct wl_display *display; |
| 1231 | struct egl_compositor *ec; |
Kristian Høgsberg | d653126 | 2008-12-12 11:06:18 -0500 | [diff] [blame] | 1232 | GError *error = NULL; |
| 1233 | GOptionContext *context; |
| 1234 | |
| 1235 | context = g_option_context_new(NULL); |
| 1236 | g_option_context_add_main_entries(context, option_entries, "Wayland"); |
| 1237 | if (!g_option_context_parse(context, &argc, &argv, &error)) { |
| 1238 | fprintf(stderr, "option parsing failed: %s\n", error->message); |
| 1239 | exit(EXIT_FAILURE); |
| 1240 | } |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1241 | |
| 1242 | display = wl_display_create(); |
| 1243 | |
| 1244 | ec = egl_compositor_create(display); |
Kristian Høgsberg | 841883b | 2008-12-05 11:19:56 -0500 | [diff] [blame] | 1245 | if (ec == NULL) { |
| 1246 | fprintf(stderr, "failed to create compositor\n"); |
| 1247 | exit(EXIT_FAILURE); |
| 1248 | } |
| 1249 | |
Kristian Høgsberg | dc0f355 | 2008-12-07 15:22:22 -0500 | [diff] [blame] | 1250 | if (wl_display_add_socket(display, socket_name, sizeof socket_name)) { |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1251 | fprintf(stderr, "failed to add socket: %m\n"); |
| 1252 | exit(EXIT_FAILURE); |
| 1253 | } |
| 1254 | |
| 1255 | wl_display_run(display); |
| 1256 | |
| 1257 | return 0; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1258 | } |