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