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 | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 58 | struct wlsc_matrix { |
| 59 | GLdouble d[16]; |
| 60 | }; |
| 61 | |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 62 | struct wl_visual { |
| 63 | struct wl_object base; |
| 64 | }; |
| 65 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 66 | struct wlsc_output { |
Kristian Høgsberg | ee02ca6 | 2008-12-21 23:37:12 -0500 | [diff] [blame] | 67 | struct wl_object base; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 68 | struct wl_list link; |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 69 | struct wlsc_compositor *compositor; |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 70 | struct wlsc_surface *background; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 71 | EGLSurface surface; |
| 72 | int32_t x, y, width, height, stride; |
| 73 | |
| 74 | struct drm_mode_modeinfo *mode; |
| 75 | uint32_t fb_id; |
| 76 | uint32_t crtc_id; |
| 77 | uint32_t connector_id; |
Kristian Høgsberg | ee02ca6 | 2008-12-21 23:37:12 -0500 | [diff] [blame] | 78 | }; |
| 79 | |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 80 | struct wlsc_input_device { |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 81 | struct wl_object base; |
| 82 | int32_t x, y; |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 83 | struct wlsc_compositor *ec; |
| 84 | struct wlsc_surface *pointer_surface; |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 85 | struct wl_list link; |
Kristian Høgsberg | 29573bc | 2008-12-11 23:27:27 -0500 | [diff] [blame] | 86 | |
| 87 | int grab; |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 88 | struct wlsc_surface *grab_surface; |
| 89 | struct wlsc_surface *focus_surface; |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 90 | }; |
| 91 | |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 92 | struct wlsc_compositor { |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 93 | struct wl_compositor base; |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 94 | struct wl_visual argb_visual, premultiplied_argb_visual, rgb_visual; |
| 95 | |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 96 | EGLDisplay display; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 97 | EGLContext context; |
Kristian Høgsberg | 2d9cd1e | 2008-11-03 08:09:34 -0500 | [diff] [blame] | 98 | EGLConfig config; |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 99 | struct wl_display *wl_display; |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 100 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 101 | struct wl_list output_list; |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 102 | struct wl_list input_device_list; |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 103 | struct wl_list surface_list; |
| 104 | |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 105 | struct wl_event_source *term_signal_source; |
| 106 | |
| 107 | /* tty handling state */ |
| 108 | int tty_fd; |
Ray Strode | e96dcb8 | 2008-12-20 02:00:49 -0500 | [diff] [blame] | 109 | uint32_t vt_active : 1; |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 110 | |
| 111 | struct termios terminal_attributes; |
| 112 | struct wl_event_source *tty_input_source; |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 113 | struct wl_event_source *enter_vt_source; |
| 114 | struct wl_event_source *leave_vt_source; |
| 115 | |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 116 | struct udev *udev; |
| 117 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 118 | /* Repaint state. */ |
| 119 | struct wl_event_source *timer_source; |
| 120 | int repaint_needed; |
| 121 | int repaint_on_timeout; |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 122 | struct timespec previous_swap; |
| 123 | uint32_t current_frame; |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 124 | |
| 125 | uint32_t meta_state; |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 126 | struct wl_list animate_list; |
| 127 | struct wlsc_surface *primary; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 128 | }; |
| 129 | |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 130 | #define META_DOWN 256 |
| 131 | |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 132 | struct wlsc_animate { |
| 133 | struct wl_list link; |
| 134 | void (*animate)(struct wlsc_animate *animate, |
| 135 | struct wlsc_compositor *compositor, |
| 136 | uint32_t frame, uint32_t msecs); |
| 137 | }; |
| 138 | |
| 139 | struct wlsc_vector { |
| 140 | GLdouble x, y, z; |
| 141 | }; |
| 142 | |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 143 | struct wlsc_surface { |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 144 | struct wl_surface base; |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 145 | struct wlsc_compositor *compositor; |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 146 | struct wl_visual *visual; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 147 | GLuint texture; |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 148 | struct wl_map map; |
Kristian Høgsberg | 2d9cd1e | 2008-11-03 08:09:34 -0500 | [diff] [blame] | 149 | EGLSurface surface; |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 150 | int width, height; |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 151 | struct wl_list link; |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 152 | struct wlsc_matrix matrix; |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 153 | |
| 154 | struct wlsc_vector target, current, previous; |
| 155 | GLdouble target_angle, current_angle, previous_angle; |
| 156 | struct wlsc_animate animate; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 157 | }; |
| 158 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 159 | static const char *option_background = "background.jpg"; |
| 160 | |
| 161 | static const GOptionEntry option_entries[] = { |
| 162 | { "background", 'b', 0, G_OPTION_ARG_STRING, |
| 163 | &option_background, "Background image" }, |
| 164 | { NULL } |
| 165 | }; |
| 166 | |
| 167 | |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 168 | struct screenshooter { |
| 169 | struct wl_object base; |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 170 | struct wlsc_compositor *ec; |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 171 | }; |
| 172 | |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 173 | struct screenshooter_interface { |
| 174 | void (*shoot)(struct wl_client *client, struct screenshooter *shooter); |
| 175 | }; |
| 176 | |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 177 | static void |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 178 | screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter) |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 179 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 180 | struct wlsc_compositor *ec = shooter->ec; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 181 | struct wlsc_output *output; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 182 | char buffer[256]; |
Kristian Høgsberg | c0b4432 | 2009-01-26 22:54:40 -0500 | [diff] [blame] | 183 | GdkPixbuf *pixbuf, *normal; |
Kristian Høgsberg | 0ea4710 | 2008-12-14 15:53:13 -0500 | [diff] [blame] | 184 | GError *error = NULL; |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 185 | unsigned char *data; |
| 186 | int i, j; |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 187 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 188 | i = 0; |
| 189 | output = container_of(ec->output_list.next, struct wlsc_output, link); |
| 190 | while (&output->link != &ec->output_list) { |
| 191 | snprintf(buffer, sizeof buffer, "wayland-screenshot-%d.png", i++); |
Kristian Høgsberg | c0b4432 | 2009-01-26 22:54:40 -0500 | [diff] [blame] | 192 | data = malloc(output->width * output->height * 4); |
| 193 | if (data == NULL) { |
| 194 | fprintf(stderr, "couldn't allocate image buffer\n"); |
| 195 | continue; |
| 196 | } |
| 197 | |
| 198 | glReadBuffer(GL_FRONT); |
| 199 | glPixelStorei(GL_PACK_ALIGNMENT, 1); |
| 200 | glReadPixels(0, 0, output->width, output->height, |
| 201 | GL_RGBA, GL_UNSIGNED_BYTE, data); |
| 202 | |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 203 | /* FIXME: We should just use a RGB visual for the frontbuffer. */ |
| 204 | for (j = 3; j < output->width * output->height * 4; j += 4) |
| 205 | data[j] = 0xff; |
| 206 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 207 | pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE, |
Kristian Høgsberg | c0b4432 | 2009-01-26 22:54:40 -0500 | [diff] [blame] | 208 | 8, output->width, output->height, output->width * 4, |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 209 | NULL, NULL); |
Kristian Høgsberg | c0b4432 | 2009-01-26 22:54:40 -0500 | [diff] [blame] | 210 | normal = gdk_pixbuf_flip(pixbuf, FALSE); |
| 211 | gdk_pixbuf_save(normal, buffer, "png", &error, NULL); |
| 212 | gdk_pixbuf_unref(normal); |
| 213 | gdk_pixbuf_unref(pixbuf); |
| 214 | free(data); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 215 | |
| 216 | output = container_of(output->link.next, |
| 217 | struct wlsc_output, link); |
| 218 | } |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 219 | } |
| 220 | |
Kristian Høgsberg | fb6d68d | 2008-12-21 21:54:51 -0500 | [diff] [blame] | 221 | static const struct wl_message screenshooter_methods[] = { |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 222 | { "shoot", "", NULL } |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | static const struct wl_interface screenshooter_interface = { |
| 226 | "screenshooter", 1, |
| 227 | ARRAY_LENGTH(screenshooter_methods), |
| 228 | screenshooter_methods, |
| 229 | }; |
| 230 | |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 231 | struct screenshooter_interface screenshooter_implementation = { |
| 232 | screenshooter_shoot |
| 233 | }; |
| 234 | |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 235 | static struct screenshooter * |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 236 | screenshooter_create(struct wlsc_compositor *ec) |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 237 | { |
| 238 | struct screenshooter *shooter; |
| 239 | |
| 240 | shooter = malloc(sizeof *shooter); |
| 241 | if (shooter == NULL) |
| 242 | return NULL; |
| 243 | |
| 244 | shooter->base.interface = &screenshooter_interface; |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 245 | shooter->base.implementation = (void(**)(void)) &screenshooter_implementation; |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 246 | shooter->ec = ec; |
| 247 | |
| 248 | return shooter; |
| 249 | }; |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 250 | |
| 251 | static void |
| 252 | wlsc_matrix_init(struct wlsc_matrix *matrix) |
| 253 | { |
| 254 | static const struct wlsc_matrix identity = { |
| 255 | { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 } |
| 256 | }; |
| 257 | |
| 258 | memcpy(matrix, &identity, sizeof identity); |
| 259 | } |
| 260 | |
| 261 | static void |
| 262 | wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n) |
| 263 | { |
| 264 | struct wlsc_matrix tmp; |
| 265 | const GLdouble *row, *column; |
| 266 | div_t d; |
| 267 | int i, j; |
| 268 | |
| 269 | for (i = 0; i < 16; i++) { |
| 270 | tmp.d[i] = 0; |
| 271 | d = div(i, 4); |
| 272 | row = m->d + d.quot * 4; |
| 273 | column = n->d + d.rem; |
| 274 | for (j = 0; j < 4; j++) |
| 275 | tmp.d[i] += row[j] * column[j * 4]; |
| 276 | } |
| 277 | memcpy(m, &tmp, sizeof tmp); |
| 278 | } |
| 279 | |
| 280 | static void |
| 281 | wlsc_matrix_translate(struct wlsc_matrix *matrix, GLdouble x, GLdouble y, GLdouble z) |
| 282 | { |
| 283 | struct wlsc_matrix translate = { |
| 284 | { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 } |
| 285 | }; |
| 286 | |
| 287 | wlsc_matrix_multiply(matrix, &translate); |
| 288 | } |
| 289 | |
| 290 | static void |
| 291 | wlsc_matrix_scale(struct wlsc_matrix *matrix, GLdouble x, GLdouble y, GLdouble z) |
| 292 | { |
| 293 | struct wlsc_matrix scale = { |
| 294 | { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 } |
| 295 | }; |
| 296 | |
| 297 | wlsc_matrix_multiply(matrix, &scale); |
| 298 | } |
| 299 | |
| 300 | static void |
| 301 | wlsc_matrix_rotate(struct wlsc_matrix *matrix, |
| 302 | GLdouble angle, GLdouble x, GLdouble y, GLdouble z) |
| 303 | { |
| 304 | GLdouble c = cos(angle); |
| 305 | GLdouble s = sin(angle); |
| 306 | struct wlsc_matrix rotate = { |
| 307 | { x * x * (1 - c) + c, y * x * (1 - c) + z * s, x * z * (1 - c) - y * s, 0, |
| 308 | x * y * (1 - c) - z * s, y * y * (1 - c) + c, y * z * (1 - c) - x * s, 0, |
| 309 | x * z * (1 - c) + y * x, y * z * (1 - c) - x * s, z * z * (1 - c) + c, 0, |
| 310 | 0, 0, 0, 1 } |
| 311 | }; |
| 312 | |
| 313 | wlsc_matrix_multiply(matrix, &rotate); |
| 314 | } |
| 315 | |
| 316 | static void |
| 317 | wlsc_surface_update_matrix(struct wlsc_surface *es) |
| 318 | { |
| 319 | GLdouble tx, ty; |
| 320 | |
| 321 | tx = es->map.x + es->map.width / 2; |
| 322 | ty = es->map.y + es->map.height / 2; |
| 323 | |
| 324 | wlsc_matrix_init(&es->matrix); |
| 325 | wlsc_matrix_translate(&es->matrix, -tx, -ty, 0); |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 326 | wlsc_matrix_rotate(&es->matrix, es->current_angle, 0, 1, 0); |
| 327 | wlsc_matrix_translate(&es->matrix, tx + es->current.x, |
| 328 | ty + es->current.y, es->current.z); |
| 329 | } |
| 330 | |
| 331 | static void |
| 332 | wlsc_surface_init(struct wlsc_surface *surface, |
| 333 | struct wlsc_compositor *compositor, struct wl_visual *visual, |
| 334 | int32_t x, int32_t y, int32_t width, int32_t height) |
| 335 | { |
| 336 | glGenTextures(1, &surface->texture); |
| 337 | surface->compositor = compositor; |
| 338 | surface->map.x = x; |
| 339 | surface->map.y = y; |
| 340 | surface->map.width = width; |
| 341 | surface->map.height = height; |
| 342 | surface->surface = EGL_NO_SURFACE; |
| 343 | surface->visual = visual; |
| 344 | surface->current.x = 0; |
| 345 | surface->current.y = 0; |
| 346 | surface->current.z = 0; |
| 347 | surface->current_angle = 0; |
| 348 | surface->target.x = 0; |
| 349 | surface->target.y = 0; |
| 350 | surface->target.z = 0; |
| 351 | surface->target_angle = 0; |
| 352 | surface->previous_angle = 0; |
| 353 | |
| 354 | wlsc_surface_update_matrix(surface); |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 355 | } |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 356 | |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 357 | static struct wlsc_surface * |
| 358 | wlsc_surface_create_from_cairo_surface(struct wlsc_compositor *ec, |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 359 | cairo_surface_t *surface, |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 360 | int x, int y, int width, int height) |
| 361 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 362 | struct wlsc_surface *es; |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 363 | int stride; |
| 364 | void *data; |
| 365 | |
| 366 | stride = cairo_image_surface_get_stride(surface); |
| 367 | data = cairo_image_surface_get_data(surface); |
| 368 | |
| 369 | es = malloc(sizeof *es); |
| 370 | if (es == NULL) |
| 371 | return NULL; |
| 372 | |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 373 | wlsc_surface_init(es, ec, &ec->premultiplied_argb_visual, |
| 374 | x, y, width, height); |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 375 | glBindTexture(GL_TEXTURE_2D, es->texture); |
| 376 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
Kristian Høgsberg | 98fed0f | 2008-12-09 13:35:35 -0500 | [diff] [blame] | 377 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT); |
| 378 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 379 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 380 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, |
| 381 | GL_BGRA, GL_UNSIGNED_BYTE, data); |
| 382 | |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 383 | return es; |
| 384 | } |
| 385 | |
| 386 | static void |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 387 | wlsc_surface_destroy(struct wlsc_surface *es, struct wlsc_compositor *ec) |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 388 | { |
| 389 | glDeleteTextures(1, &es->texture); |
| 390 | if (es->surface != EGL_NO_SURFACE) |
| 391 | eglDestroySurface(ec->display, es->surface); |
| 392 | free(es); |
| 393 | } |
| 394 | |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 395 | static void |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 396 | pointer_path(cairo_t *cr, int x, int y) |
| 397 | { |
| 398 | const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10; |
| 399 | const int width = 16, height = 16; |
| 400 | |
| 401 | cairo_move_to(cr, x, y); |
| 402 | cairo_line_to(cr, x + tx, y + ty); |
| 403 | cairo_line_to(cr, x + dx, y + dy); |
| 404 | cairo_line_to(cr, x + width - end, y + height); |
| 405 | cairo_line_to(cr, x + width, y + height - end); |
| 406 | cairo_line_to(cr, x + dy, y + dx); |
| 407 | cairo_line_to(cr, x + ty, y + tx); |
| 408 | cairo_close_path(cr); |
| 409 | } |
| 410 | |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 411 | static struct wlsc_surface * |
| 412 | 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] | 413 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 414 | struct wlsc_surface *es; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 415 | const int hotspot_x = 16, hotspot_y = 16; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 416 | cairo_surface_t *surface; |
| 417 | cairo_t *cr; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 418 | |
| 419 | surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, |
| 420 | width, height); |
| 421 | |
| 422 | cr = cairo_create(surface); |
| 423 | pointer_path(cr, hotspot_x + 5, hotspot_y + 4); |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 424 | cairo_set_line_width(cr, 2); |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 425 | cairo_set_source_rgb(cr, 0, 0, 0); |
| 426 | cairo_stroke_preserve(cr); |
| 427 | cairo_fill(cr); |
| 428 | blur_surface(surface, width); |
| 429 | |
| 430 | pointer_path(cr, hotspot_x, hotspot_y); |
| 431 | cairo_stroke_preserve(cr); |
| 432 | cairo_set_source_rgb(cr, 1, 1, 1); |
| 433 | cairo_fill(cr); |
| 434 | cairo_destroy(cr); |
| 435 | |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 436 | es = wlsc_surface_create_from_cairo_surface(ec, |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 437 | surface, |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 438 | x - hotspot_x, |
| 439 | y - hotspot_y, |
| 440 | width, height); |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 441 | |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 442 | cairo_surface_destroy(surface); |
| 443 | |
Kristian Høgsberg | 5487982 | 2008-11-23 17:07:32 -0500 | [diff] [blame] | 444 | return es; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 445 | } |
| 446 | |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 447 | static struct wlsc_surface * |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 448 | background_create(struct wlsc_output *output, const char *filename) |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 449 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 450 | struct wlsc_surface *background; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 451 | GdkPixbuf *pixbuf; |
| 452 | GError *error = NULL; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 453 | void *data; |
Ray Strode | 18fd33c | 2008-12-18 21:05:20 -0500 | [diff] [blame] | 454 | GLenum format; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 455 | |
| 456 | background = malloc(sizeof *background); |
| 457 | if (background == NULL) |
| 458 | return NULL; |
| 459 | |
| 460 | g_type_init(); |
| 461 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 462 | pixbuf = gdk_pixbuf_new_from_file_at_scale(filename, |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 463 | output->width, |
| 464 | output->height, |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 465 | FALSE, &error); |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 466 | if (error != NULL) { |
| 467 | free(background); |
| 468 | return NULL; |
| 469 | } |
| 470 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 471 | data = gdk_pixbuf_get_pixels(pixbuf); |
| 472 | |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 473 | wlsc_surface_init(background, output->compositor, |
| 474 | &output->compositor->rgb_visual, |
| 475 | output->x, output->y, output->width, output->height); |
| 476 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 477 | glBindTexture(GL_TEXTURE_2D, background->texture); |
| 478 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
Kristian Høgsberg | 98fed0f | 2008-12-09 13:35:35 -0500 | [diff] [blame] | 479 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT); |
Kristian Høgsberg | fdec236 | 2001-01-01 22:23:51 -0500 | [diff] [blame] | 480 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 481 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
Ray Strode | 18fd33c | 2008-12-18 21:05:20 -0500 | [diff] [blame] | 482 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 483 | if (gdk_pixbuf_get_has_alpha(pixbuf)) |
Ray Strode | 18fd33c | 2008-12-18 21:05:20 -0500 | [diff] [blame] | 484 | format = GL_RGBA; |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 485 | else |
Ray Strode | 18fd33c | 2008-12-18 21:05:20 -0500 | [diff] [blame] | 486 | format = GL_RGB; |
Ray Strode | 18fd33c | 2008-12-18 21:05:20 -0500 | [diff] [blame] | 487 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 488 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, |
| 489 | output->width, output->height, 0, |
Ray Strode | 18fd33c | 2008-12-18 21:05:20 -0500 | [diff] [blame] | 490 | format, GL_UNSIGNED_BYTE, data); |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 491 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 492 | return background; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | static void |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 496 | wlsc_surface_draw(struct wlsc_surface *es) |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 497 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 498 | struct wlsc_compositor *ec = es->compositor; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 499 | GLint vertices[12]; |
| 500 | GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 }; |
| 501 | GLuint indices[4] = { 0, 1, 2, 3 }; |
| 502 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 503 | vertices[0] = es->map.x; |
| 504 | vertices[1] = es->map.y; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 505 | vertices[2] = 0; |
| 506 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 507 | vertices[3] = es->map.x; |
| 508 | vertices[4] = es->map.y + es->map.height; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 509 | vertices[5] = 0; |
| 510 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 511 | vertices[6] = es->map.x + es->map.width; |
| 512 | vertices[7] = es->map.y; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 513 | vertices[8] = 0; |
| 514 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 515 | vertices[9] = es->map.x + es->map.width; |
| 516 | vertices[10] = es->map.y + es->map.height; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 517 | vertices[11] = 0; |
| 518 | |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 519 | if (es->visual == &ec->argb_visual) { |
| 520 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 521 | glEnable(GL_BLEND); |
| 522 | } else if (es->visual == &ec->premultiplied_argb_visual) { |
| 523 | glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); |
| 524 | glEnable(GL_BLEND); |
| 525 | } else { |
| 526 | glDisable(GL_BLEND); |
| 527 | } |
| 528 | |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 529 | glPushMatrix(); |
| 530 | glMultMatrixd(es->matrix.d); |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 531 | glBindTexture(GL_TEXTURE_2D, es->texture); |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 532 | glEnable(GL_TEXTURE_2D); |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 533 | glEnableClientState(GL_VERTEX_ARRAY); |
| 534 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 535 | glVertexPointer(3, GL_INT, 0, vertices); |
| 536 | glTexCoordPointer(2, GL_INT, 0, tex_coords); |
| 537 | glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices); |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 538 | glPopMatrix(); |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | static void |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 542 | wlsc_vector_add(struct wlsc_vector *v1, struct wlsc_vector *v2) |
| 543 | { |
| 544 | v1->x += v2->x; |
| 545 | v1->y += v2->y; |
| 546 | v1->z += v2->z; |
| 547 | } |
| 548 | |
| 549 | static void |
| 550 | wlsc_vector_subtract(struct wlsc_vector *v1, struct wlsc_vector *v2) |
| 551 | { |
| 552 | v1->x -= v2->x; |
| 553 | v1->y -= v2->y; |
| 554 | v1->z -= v2->z; |
| 555 | } |
| 556 | |
| 557 | static void |
| 558 | wlsc_vector_scalar(struct wlsc_vector *v1, GLdouble s) |
| 559 | { |
| 560 | v1->x *= s; |
| 561 | v1->y *= s; |
| 562 | v1->z *= s; |
| 563 | } |
| 564 | |
| 565 | static void |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 566 | repaint_output(struct wlsc_output *output) |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 567 | { |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 568 | struct wlsc_compositor *ec = output->compositor; |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 569 | struct wlsc_surface *es; |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 570 | struct wlsc_input_device *eid; |
Kristian Høgsberg | fdec236 | 2001-01-01 22:23:51 -0500 | [diff] [blame] | 571 | double s = 3000; |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 572 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 573 | if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) { |
| 574 | fprintf(stderr, "failed to make context current\n"); |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 575 | return; |
| 576 | } |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 577 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 578 | glViewport(0, 0, output->width, output->height); |
| 579 | glMatrixMode(GL_PROJECTION); |
| 580 | glLoadIdentity(); |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 581 | glFrustum(-output->width / s, output->width / s, |
| 582 | output->height / s, -output->height / s, 1, 2 * s); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 583 | glMatrixMode(GL_MODELVIEW); |
Kristian Høgsberg | fdec236 | 2001-01-01 22:23:51 -0500 | [diff] [blame] | 584 | glLoadIdentity(); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 585 | glClearColor(0, 0, 0.2, 1); |
| 586 | |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 587 | glTranslatef(-output->width / 2, -output->height / 2, -s / 2); |
Kristian Høgsberg | fdec236 | 2001-01-01 22:23:51 -0500 | [diff] [blame] | 588 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 589 | if (output->background) |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 590 | wlsc_surface_draw(output->background); |
Kristian Høgsberg | 5b7f832 | 2008-12-18 12:08:19 -0500 | [diff] [blame] | 591 | else |
| 592 | glClear(GL_COLOR_BUFFER_BIT); |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 593 | |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 594 | es = container_of(ec->surface_list.next, |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 595 | struct wlsc_surface, link); |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 596 | while (&es->link != &ec->surface_list) { |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 597 | wlsc_surface_draw(es); |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 598 | es = container_of(es->link.next, |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 599 | struct wlsc_surface, link); |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 600 | } |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 601 | |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 602 | eid = container_of(ec->input_device_list.next, |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 603 | struct wlsc_input_device, link); |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 604 | while (&eid->link != &ec->input_device_list) { |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 605 | wlsc_surface_draw(eid->pointer_surface); |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 606 | |
| 607 | eid = container_of(eid->link.next, |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 608 | struct wlsc_input_device, link); |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 609 | } |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 610 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 611 | eglSwapBuffers(ec->display, output->surface); |
| 612 | } |
| 613 | |
| 614 | static void |
| 615 | repaint(void *data) |
| 616 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 617 | struct wlsc_compositor *ec = data; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 618 | struct wlsc_output *output; |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 619 | struct wlsc_animate *animate, *next; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 620 | struct timespec ts; |
| 621 | uint32_t msecs; |
| 622 | |
| 623 | if (!ec->repaint_needed) { |
| 624 | ec->repaint_on_timeout = 0; |
| 625 | return; |
| 626 | } |
| 627 | |
| 628 | output = container_of(ec->output_list.next, struct wlsc_output, link); |
| 629 | while (&output->link != &ec->output_list) { |
| 630 | repaint_output(output); |
| 631 | output = container_of(output->link.next, |
| 632 | struct wlsc_output, link); |
| 633 | } |
| 634 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 635 | ec->repaint_needed = 0; |
Kristian Høgsberg | 9af92b3 | 2008-11-24 01:12:46 -0500 | [diff] [blame] | 636 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 637 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 638 | msecs = ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000); |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 639 | wl_display_post_frame(ec->wl_display, &ec->base, |
| 640 | ec->current_frame, msecs); |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 641 | |
Kristian Høgsberg | 4a29890 | 2008-11-28 18:35:25 -0500 | [diff] [blame] | 642 | wl_event_source_timer_update(ec->timer_source, 10); |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 643 | ec->repaint_on_timeout = 1; |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 644 | |
| 645 | animate = container_of(ec->animate_list.next, struct wlsc_animate, link); |
| 646 | while (&animate->link != &ec->animate_list) { |
| 647 | next = container_of(animate->link.next, |
| 648 | struct wlsc_animate, link); |
| 649 | animate->animate(animate, ec, ec->current_frame, msecs); |
| 650 | animate = next; |
| 651 | } |
| 652 | |
| 653 | ec->current_frame++; |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 654 | } |
| 655 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 656 | static void |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 657 | schedule_repaint(struct wlsc_compositor *ec) |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 658 | { |
| 659 | struct wl_event_loop *loop; |
| 660 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 661 | ec->repaint_needed = 1; |
| 662 | if (!ec->repaint_on_timeout) { |
| 663 | loop = wl_display_get_event_loop(ec->wl_display); |
Kristian Høgsberg | 4a29890 | 2008-11-28 18:35:25 -0500 | [diff] [blame] | 664 | wl_event_loop_add_idle(loop, repaint, ec); |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 665 | } |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 666 | } |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 667 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 668 | static void |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 669 | surface_destroy(struct wl_client *client, |
| 670 | struct wl_surface *surface) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 671 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 672 | struct wlsc_surface *es = (struct wlsc_surface *) surface; |
| 673 | struct wlsc_compositor *ec = es->compositor; |
Kristian Høgsberg | 2d9cd1e | 2008-11-03 08:09:34 -0500 | [diff] [blame] | 674 | |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 675 | wl_list_remove(&es->link); |
Kristian Høgsberg | bb8cd93 | 2009-02-10 19:48:47 -0500 | [diff] [blame^] | 676 | wl_list_remove(&es->animate.link); |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 677 | wlsc_surface_destroy(es, ec); |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 678 | |
| 679 | schedule_repaint(ec); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 680 | } |
| 681 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 682 | static void |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 683 | surface_attach(struct wl_client *client, |
| 684 | struct wl_surface *surface, uint32_t name, |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 685 | uint32_t width, uint32_t height, uint32_t stride, |
| 686 | struct wl_object *visual) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 687 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 688 | struct wlsc_surface *es = (struct wlsc_surface *) surface; |
| 689 | struct wlsc_compositor *ec = es->compositor; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 690 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 691 | if (es->surface != EGL_NO_SURFACE) |
| 692 | eglDestroySurface(ec->display, es->surface); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 693 | |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 694 | es->width = width; |
| 695 | es->height = height; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 696 | es->surface = eglCreateSurfaceForName(ec->display, ec->config, |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 697 | name, width, height, stride, NULL); |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 698 | if (visual == &ec->argb_visual.base) |
| 699 | es->visual = &ec->argb_visual; |
| 700 | else if (visual == &ec->premultiplied_argb_visual.base) |
| 701 | es->visual = &ec->premultiplied_argb_visual; |
Kristian Høgsberg | e10b828 | 2008-12-18 19:58:44 -0500 | [diff] [blame] | 702 | else if (visual == &ec->rgb_visual.base) |
| 703 | es->visual = &ec->rgb_visual; |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 704 | else |
| 705 | /* FIXME: Smack client with an exception event */; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 706 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 707 | glBindTexture(GL_TEXTURE_2D, es->texture); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 708 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
Kristian Høgsberg | 98fed0f | 2008-12-09 13:35:35 -0500 | [diff] [blame] | 709 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT); |
| 710 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 711 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 712 | eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D); |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 713 | } |
| 714 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 715 | static void |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 716 | surface_map(struct wl_client *client, |
| 717 | struct wl_surface *surface, |
| 718 | 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] | 719 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 720 | struct wlsc_surface *es = (struct wlsc_surface *) surface; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 721 | |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 722 | es->map.x = x; |
| 723 | es->map.y = y; |
| 724 | es->map.width = width; |
| 725 | es->map.height = height; |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 726 | wlsc_surface_update_matrix(es); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 727 | } |
| 728 | |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 729 | static void |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 730 | surface_copy(struct wl_client *client, |
| 731 | struct wl_surface *surface, |
| 732 | int32_t dst_x, int32_t dst_y, |
| 733 | uint32_t name, uint32_t stride, |
| 734 | 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] | 735 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 736 | struct wlsc_surface *es = (struct wlsc_surface *) surface; |
| 737 | struct wlsc_compositor *ec = es->compositor; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 738 | EGLSurface src; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 739 | |
| 740 | /* FIXME: glCopyPixels should work, but then we'll have to |
| 741 | * call eglMakeCurrent to set up the src and dest surfaces |
| 742 | * first. This seems cheaper, but maybe there's a better way |
| 743 | * to accomplish this. */ |
| 744 | |
| 745 | src = eglCreateSurfaceForName(ec->display, ec->config, |
| 746 | name, x + width, y + height, stride, NULL); |
| 747 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 748 | 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] | 749 | src, GL_FRONT_LEFT, x, y, width, height); |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 750 | eglDestroySurface(ec->display, src); |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | static void |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 754 | surface_damage(struct wl_client *client, |
| 755 | struct wl_surface *surface, |
| 756 | 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] | 757 | { |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 758 | /* FIXME: This need to take a damage region, of course. */ |
| 759 | } |
| 760 | |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 761 | const static struct wl_surface_interface surface_interface = { |
| 762 | surface_destroy, |
| 763 | surface_attach, |
| 764 | surface_map, |
| 765 | surface_copy, |
| 766 | surface_damage |
| 767 | }; |
| 768 | |
| 769 | static void |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 770 | animate_surface(struct wlsc_animate *animate, |
| 771 | struct wlsc_compositor *compositor, |
| 772 | uint32_t frame, uint32_t msecs) |
| 773 | { |
| 774 | struct wlsc_surface *s; |
| 775 | double angle_force, angle; |
| 776 | struct wlsc_vector force, tmp; |
| 777 | double step = 0.3; |
| 778 | double friction = 1; |
| 779 | double spring = 0.2; |
| 780 | |
| 781 | s = container_of(animate, struct wlsc_surface, animate); |
| 782 | |
| 783 | angle = s->current_angle; |
| 784 | angle_force = (s->target_angle - angle) * spring + |
| 785 | (s->previous_angle - angle) * friction; |
| 786 | |
| 787 | s->current_angle = angle + (angle - s->previous_angle) + angle_force * step; |
| 788 | s->previous_angle = angle; |
| 789 | |
| 790 | force = s->target; |
| 791 | wlsc_vector_subtract(&force, &s->current); |
| 792 | wlsc_vector_scalar(&force, spring); |
| 793 | tmp = s->previous; |
| 794 | wlsc_vector_subtract(&tmp, &s->current); |
| 795 | wlsc_vector_scalar(&tmp, friction); |
| 796 | wlsc_vector_add(&force, &tmp); |
| 797 | |
| 798 | wlsc_vector_scalar(&force, step); |
| 799 | wlsc_vector_add(&force, &s->current); |
| 800 | wlsc_vector_subtract(&force, &s->previous); |
| 801 | s->previous = s->current; |
| 802 | wlsc_vector_add(&s->current, &force); |
| 803 | |
| 804 | wlsc_surface_update_matrix(s); |
| 805 | |
| 806 | tmp = s->current; |
| 807 | wlsc_vector_subtract(&tmp, &s->target); |
Kristian Høgsberg | bb8cd93 | 2009-02-10 19:48:47 -0500 | [diff] [blame^] | 808 | if (tmp.x * tmp.x + tmp.y * tmp.y + tmp.z * tmp.z > 0.001) { |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 809 | schedule_repaint(compositor); |
Kristian Høgsberg | bb8cd93 | 2009-02-10 19:48:47 -0500 | [diff] [blame^] | 810 | } else { |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 811 | wl_list_remove(&s->animate.link); |
Kristian Høgsberg | bb8cd93 | 2009-02-10 19:48:47 -0500 | [diff] [blame^] | 812 | wl_list_init(&s->animate.link); |
| 813 | } |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | static void |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 817 | compositor_create_surface(struct wl_client *client, |
| 818 | struct wl_compositor *compositor, uint32_t id) |
| 819 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 820 | struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor; |
| 821 | struct wlsc_surface *es; |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 822 | |
| 823 | es = malloc(sizeof *es); |
| 824 | if (es == NULL) |
| 825 | /* FIXME: Send OOM event. */ |
| 826 | return; |
| 827 | |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 828 | wlsc_surface_init(es, ec, NULL, 0, 0, 0, 0); |
| 829 | es->animate.animate = animate_surface; |
| 830 | wl_list_init(&es->animate.link); |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 831 | |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 832 | wl_list_insert(ec->surface_list.prev, &es->link); |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 833 | wl_client_add_surface(client, &es->base, |
| 834 | &surface_interface, id); |
| 835 | } |
| 836 | |
| 837 | static void |
| 838 | compositor_commit(struct wl_client *client, |
| 839 | struct wl_compositor *compositor, uint32_t key) |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 840 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 841 | struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor; |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 842 | |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 843 | schedule_repaint(ec); |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 844 | wl_client_send_acknowledge(client, compositor, key, ec->current_frame); |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 845 | } |
| 846 | |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 847 | const static struct wl_compositor_interface compositor_interface = { |
| 848 | compositor_create_surface, |
| 849 | compositor_commit |
| 850 | }; |
| 851 | |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 852 | static struct wlsc_surface * |
Kristian Høgsberg | 7e972a5 | 2008-12-21 17:26:00 -0500 | [diff] [blame] | 853 | 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] | 854 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 855 | struct wlsc_compositor *ec = device->ec; |
| 856 | struct wlsc_surface *es; |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 857 | |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 858 | if (device->grab > 0) |
| 859 | return device->grab_surface; |
| 860 | |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 861 | es = container_of(ec->surface_list.prev, |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 862 | struct wlsc_surface, link); |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 863 | while (&es->link != &ec->surface_list) { |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 864 | if (es->map.x <= device->x && |
| 865 | device->x < es->map.x + es->map.width && |
| 866 | es->map.y <= device->y && |
Kristian Høgsberg | 6c9c8f8 | 2009-02-10 18:29:24 -0500 | [diff] [blame] | 867 | device->y < es->map.y + es->map.height) { |
| 868 | /* Transform to surface coordinates. */ |
| 869 | *sx = (device->x - es->map.x) * es->width / es->map.width; |
| 870 | *sy = (device->y - es->map.y) * es->height / es->map.height; |
| 871 | |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 872 | return es; |
Kristian Høgsberg | 6c9c8f8 | 2009-02-10 18:29:24 -0500 | [diff] [blame] | 873 | } |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 874 | |
| 875 | es = container_of(es->link.prev, |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 876 | struct wlsc_surface, link); |
Kristian Høgsberg | 7e972a5 | 2008-12-21 17:26:00 -0500 | [diff] [blame] | 877 | |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | return NULL; |
| 881 | } |
| 882 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 883 | void |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 884 | notify_motion(struct wlsc_input_device *device, int x, int y) |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 885 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 886 | struct wlsc_surface *es; |
| 887 | struct wlsc_compositor *ec = device->ec; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 888 | struct wlsc_output *output; |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 889 | const int hotspot_x = 16, hotspot_y = 16; |
| 890 | int32_t sx, sy; |
| 891 | |
Ray Strode | e96dcb8 | 2008-12-20 02:00:49 -0500 | [diff] [blame] | 892 | if (!ec->vt_active) |
| 893 | return; |
| 894 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 895 | /* FIXME: We need some multi head love here. */ |
| 896 | output = container_of(ec->output_list.next, struct wlsc_output, link); |
| 897 | if (x < output->x) |
Ray Strode | 90e701d | 2008-12-18 23:05:43 -0500 | [diff] [blame] | 898 | x = 0; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 899 | if (y < output->y) |
Ray Strode | 90e701d | 2008-12-18 23:05:43 -0500 | [diff] [blame] | 900 | y = 0; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 901 | if (x >= output->x + output->width) |
| 902 | x = output->x + output->width - 1; |
| 903 | if (y >= output->y + output->height) |
| 904 | y = output->y + output->height - 1; |
Ray Strode | 90e701d | 2008-12-18 23:05:43 -0500 | [diff] [blame] | 905 | |
Kristian Høgsberg | e3ef3e5 | 2008-12-21 19:30:01 -0500 | [diff] [blame] | 906 | device->x = x; |
| 907 | device->y = y; |
Kristian Høgsberg | 7e972a5 | 2008-12-21 17:26:00 -0500 | [diff] [blame] | 908 | es = pick_surface(device, &sx, &sy); |
| 909 | if (es) |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 910 | wl_surface_post_event(&es->base, &device->base, |
Kristian Høgsberg | 5a75c90 | 2008-12-10 13:16:50 -0500 | [diff] [blame] | 911 | WL_INPUT_MOTION, x, y, sx, sy); |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 912 | |
Kristian Høgsberg | 29573bc | 2008-12-11 23:27:27 -0500 | [diff] [blame] | 913 | device->pointer_surface->map.x = x - hotspot_x; |
| 914 | device->pointer_surface->map.y = y - hotspot_y; |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 915 | |
| 916 | schedule_repaint(device->ec); |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 917 | } |
| 918 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 919 | void |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 920 | notify_button(struct wlsc_input_device *device, |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 921 | int32_t button, int32_t state) |
Kristian Høgsberg | eac149a | 2008-12-10 00:24:18 -0500 | [diff] [blame] | 922 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 923 | struct wlsc_surface *es; |
| 924 | struct wlsc_compositor *ec = device->ec; |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 925 | int32_t sx, sy; |
Kristian Høgsberg | eac149a | 2008-12-10 00:24:18 -0500 | [diff] [blame] | 926 | |
Ray Strode | e96dcb8 | 2008-12-20 02:00:49 -0500 | [diff] [blame] | 927 | if (!ec->vt_active) |
| 928 | return; |
| 929 | |
Kristian Høgsberg | 7e972a5 | 2008-12-21 17:26:00 -0500 | [diff] [blame] | 930 | es = pick_surface(device, &sx, &sy); |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 931 | if (es) { |
| 932 | wl_list_remove(&es->link); |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 933 | wl_list_insert(device->ec->surface_list.prev, &es->link); |
Kristian Høgsberg | 5a75c90 | 2008-12-10 13:16:50 -0500 | [diff] [blame] | 934 | |
Kristian Høgsberg | 29573bc | 2008-12-11 23:27:27 -0500 | [diff] [blame] | 935 | if (state) { |
Kristian Høgsberg | a7700c8 | 2008-12-12 13:48:30 -0500 | [diff] [blame] | 936 | /* FIXME: We need callbacks when the surfaces |
| 937 | * we reference here go away. */ |
Kristian Høgsberg | 29573bc | 2008-12-11 23:27:27 -0500 | [diff] [blame] | 938 | device->grab++; |
| 939 | device->grab_surface = es; |
Kristian Høgsberg | a7700c8 | 2008-12-12 13:48:30 -0500 | [diff] [blame] | 940 | device->focus_surface = es; |
Kristian Høgsberg | 29573bc | 2008-12-11 23:27:27 -0500 | [diff] [blame] | 941 | } else { |
| 942 | device->grab--; |
| 943 | } |
| 944 | |
Kristian Høgsberg | 5a75c90 | 2008-12-10 13:16:50 -0500 | [diff] [blame] | 945 | /* FIXME: Swallow click on raise? */ |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 946 | wl_surface_post_event(&es->base, &device->base, |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 947 | WL_INPUT_BUTTON, button, state, |
| 948 | device->x, device->y, sx, sy); |
Kristian Høgsberg | eac149a | 2008-12-10 00:24:18 -0500 | [diff] [blame] | 949 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 950 | schedule_repaint(device->ec); |
| 951 | } |
Kristian Høgsberg | eac149a | 2008-12-10 00:24:18 -0500 | [diff] [blame] | 952 | } |
| 953 | |
Kristian Høgsberg | ab909ae | 2001-01-01 22:24:24 -0500 | [diff] [blame] | 954 | static void on_term_signal(int signal_number, void *data); |
| 955 | |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 956 | static void |
| 957 | update_surface_targets(struct wlsc_compositor *compositor, int primary) |
| 958 | { |
| 959 | struct wlsc_surface *s; |
| 960 | int i; |
| 961 | |
| 962 | i = 0; |
| 963 | s = container_of(compositor->surface_list.next, |
| 964 | struct wlsc_surface, link); |
| 965 | while (&s->link != &compositor->surface_list) { |
| 966 | if (i < primary) { |
| 967 | s->target.x = -400 + 500 * (i - primary); |
| 968 | s->target.y = 0; |
| 969 | s->target.z = -1500; |
| 970 | s->target_angle = M_PI / 4; |
| 971 | } else if (i == primary) { |
| 972 | s->target.x = 0; |
| 973 | s->target.y = 0; |
| 974 | s->target.z = -1000; |
| 975 | s->target_angle = 0; |
| 976 | compositor->primary = s; |
| 977 | } else { |
| 978 | s->target.x = 400 + 500 * (i - primary); |
| 979 | s->target.y = 0; |
| 980 | s->target.z = -1500; |
| 981 | s->target_angle = -M_PI / 4; |
| 982 | } |
| 983 | wl_list_remove(&s->animate.link); |
| 984 | wl_list_insert(compositor->animate_list.prev, &s->animate.link); |
| 985 | s = container_of(s->link.next, |
| 986 | struct wlsc_surface, link); |
| 987 | i++; |
| 988 | } |
| 989 | |
| 990 | schedule_repaint(compositor); |
| 991 | } |
| 992 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 993 | void |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 994 | notify_key(struct wlsc_input_device *device, |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 995 | uint32_t key, uint32_t state) |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 996 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 997 | struct wlsc_compositor *ec = device->ec; |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 998 | struct wlsc_surface *s; |
Ray Strode | e96dcb8 | 2008-12-20 02:00:49 -0500 | [diff] [blame] | 999 | |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 1000 | switch (key | ec->meta_state) { |
| 1001 | case KEY_EJECTCD | META_DOWN: |
Kristian Høgsberg | ab909ae | 2001-01-01 22:24:24 -0500 | [diff] [blame] | 1002 | on_term_signal(SIGTERM, ec); |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 1003 | return; |
| 1004 | |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 1005 | case KEY_1 | META_DOWN: |
| 1006 | case KEY_2 | META_DOWN: |
| 1007 | case KEY_3 | META_DOWN: |
| 1008 | case KEY_4 | META_DOWN: |
| 1009 | case KEY_5 | META_DOWN: |
| 1010 | update_surface_targets(ec, key - KEY_1); |
| 1011 | if (device->grab == 0 && s != NULL) |
| 1012 | device->focus_surface = ec->primary; |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 1013 | return; |
| 1014 | |
| 1015 | case KEY_LEFTMETA: |
| 1016 | case KEY_RIGHTMETA: |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 1017 | case KEY_LEFTMETA | META_DOWN: |
| 1018 | case KEY_RIGHTMETA | META_DOWN: |
| 1019 | ec->meta_state = state ? META_DOWN : 0; |
Kristian Høgsberg | 1febe01 | 2009-02-10 18:30:14 -0500 | [diff] [blame] | 1020 | if (state == 0 && ec->primary != NULL) { |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 1021 | ec->primary->target.z = 0; |
| 1022 | wl_list_remove(&ec->primary->animate.link); |
| 1023 | wl_list_insert(&ec->animate_list, |
| 1024 | &ec->primary->animate.link); |
| 1025 | schedule_repaint(ec); |
| 1026 | } |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 1027 | return; |
| 1028 | } |
Kristian Høgsberg | ab909ae | 2001-01-01 22:24:24 -0500 | [diff] [blame] | 1029 | |
Ray Strode | e96dcb8 | 2008-12-20 02:00:49 -0500 | [diff] [blame] | 1030 | if (!ec->vt_active) |
| 1031 | return; |
| 1032 | |
Kristian Høgsberg | 2c0e56b | 2008-12-19 13:54:40 -0500 | [diff] [blame] | 1033 | if (device->focus_surface != NULL) |
| 1034 | wl_surface_post_event(&device->focus_surface->base, |
| 1035 | &device->base, |
| 1036 | WL_INPUT_KEY, key, state); |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 1037 | } |
| 1038 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 1039 | struct evdev_input_device * |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 1040 | evdev_input_device_create(struct wlsc_input_device *device, |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 1041 | struct wl_display *display, const char *path); |
| 1042 | |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 1043 | static struct wlsc_input_device * |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1044 | create_input_device(struct wlsc_compositor *ec) |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 1045 | { |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 1046 | struct wlsc_input_device *device; |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 1047 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 1048 | device = malloc(sizeof *device); |
| 1049 | if (device == NULL) |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 1050 | return NULL; |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 1051 | |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 1052 | memset(device, 0, sizeof *device); |
Kristian Høgsberg | fe831a7 | 2008-12-21 21:50:23 -0500 | [diff] [blame] | 1053 | device->base.interface = &wl_input_device_interface; |
Kristian Høgsberg | b3131d9 | 2008-12-24 19:30:25 -0500 | [diff] [blame] | 1054 | device->base.implementation = NULL; |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 1055 | wl_display_add_object(ec->wl_display, &device->base); |
Kristian Høgsberg | b3131d9 | 2008-12-24 19:30:25 -0500 | [diff] [blame] | 1056 | wl_display_add_global(ec->wl_display, &device->base, NULL); |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 1057 | device->x = 100; |
| 1058 | device->y = 100; |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 1059 | device->ec = ec; |
| 1060 | |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 1061 | wl_list_insert(ec->input_device_list.prev, &device->link); |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 1062 | |
| 1063 | return device; |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | void |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 1067 | 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] | 1068 | { |
| 1069 | *x = device->x; |
| 1070 | *y = device->y; |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 1071 | } |
| 1072 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1073 | static void |
| 1074 | post_output_geometry(struct wl_client *client, struct wl_object *global) |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1075 | { |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1076 | struct wlsc_output *output = |
| 1077 | container_of(global, struct wlsc_output, base); |
| 1078 | |
| 1079 | wl_client_post_event(client, global, |
| 1080 | WL_OUTPUT_GEOMETRY, |
| 1081 | output->width, output->height); |
| 1082 | } |
| 1083 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1084 | static int |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1085 | init_egl(struct wlsc_compositor *ec, struct udev_device *device) |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1086 | { |
| 1087 | static const EGLint config_attribs[] = { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1088 | EGL_DEPTH_SIZE, 0, |
| 1089 | EGL_STENCIL_SIZE, 0, |
| 1090 | EGL_CONFIG_CAVEAT, EGL_NONE, |
| 1091 | EGL_RED_SIZE, 8, |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1092 | EGL_NONE |
| 1093 | }; |
| 1094 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1095 | EGLint major, minor; |
| 1096 | |
Kristian Høgsberg | aa68fe3 | 2009-01-15 12:50:21 -0500 | [diff] [blame] | 1097 | ec->display = eglCreateDisplayNative(device); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1098 | if (ec->display == NULL) { |
| 1099 | fprintf(stderr, "failed to create display\n"); |
| 1100 | return -1; |
| 1101 | } |
| 1102 | |
| 1103 | if (!eglInitialize(ec->display, &major, &minor)) { |
| 1104 | fprintf(stderr, "failed to initialize display\n"); |
| 1105 | return -1; |
| 1106 | } |
| 1107 | |
| 1108 | if (!eglChooseConfig(ec->display, config_attribs, &ec->config, 1, NULL)) |
| 1109 | return -1; |
| 1110 | |
| 1111 | ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL); |
| 1112 | if (ec->context == NULL) { |
| 1113 | fprintf(stderr, "failed to create context\n"); |
| 1114 | return -1; |
| 1115 | } |
| 1116 | return 0; |
| 1117 | } |
| 1118 | |
| 1119 | static int |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1120 | create_output(struct wlsc_compositor *ec, struct udev_device *device) |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1121 | { |
| 1122 | const static EGLint surface_attribs[] = { |
| 1123 | EGL_RENDER_BUFFER, EGL_BACK_BUFFER, |
| 1124 | EGL_NONE |
| 1125 | }; |
| 1126 | |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1127 | drmModeConnector *connector; |
| 1128 | drmModeRes *resources; |
| 1129 | drmModeEncoder *encoder; |
| 1130 | struct drm_mode_modeinfo *mode; |
| 1131 | struct drm_i915_gem_create create; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1132 | struct drm_gem_flink flink; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1133 | struct wlsc_output *output; |
Kristian Høgsberg | 2c875bd | 2008-12-19 10:34:02 -0500 | [diff] [blame] | 1134 | int i, ret, fd; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1135 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1136 | if (ec->display == NULL && init_egl(ec, device) < 0) { |
| 1137 | fprintf(stderr, "failed to initialize egl\n"); |
| 1138 | return -1; |
| 1139 | } |
| 1140 | |
| 1141 | output = malloc(sizeof *output); |
| 1142 | if (output == NULL) |
| 1143 | return -1; |
| 1144 | |
Kristian Høgsberg | 2c875bd | 2008-12-19 10:34:02 -0500 | [diff] [blame] | 1145 | fd = eglGetDisplayFD(ec->display); |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1146 | resources = drmModeGetResources(fd); |
| 1147 | if (!resources) { |
| 1148 | fprintf(stderr, "drmModeGetResources failed\n"); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1149 | return -1; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | for (i = 0; i < resources->count_connectors; i++) { |
| 1153 | connector = drmModeGetConnector(fd, resources->connectors[i]); |
| 1154 | if (connector == NULL) |
| 1155 | continue; |
| 1156 | |
| 1157 | if (connector->connection == DRM_MODE_CONNECTED && |
| 1158 | connector->count_modes > 0) |
| 1159 | break; |
| 1160 | |
| 1161 | drmModeFreeConnector(connector); |
| 1162 | } |
| 1163 | |
| 1164 | if (i == resources->count_connectors) { |
| 1165 | fprintf(stderr, "No currently active connector found.\n"); |
| 1166 | return -1; |
| 1167 | } |
| 1168 | |
| 1169 | mode = &connector->modes[0]; |
| 1170 | |
| 1171 | for (i = 0; i < resources->count_encoders; i++) { |
| 1172 | encoder = drmModeGetEncoder(fd, resources->encoders[i]); |
| 1173 | |
| 1174 | if (encoder == NULL) |
| 1175 | continue; |
| 1176 | |
| 1177 | if (encoder->encoder_id == connector->encoder_id) |
| 1178 | break; |
| 1179 | |
| 1180 | drmModeFreeEncoder(encoder); |
| 1181 | } |
| 1182 | |
| 1183 | /* Mode size at 32 bpp */ |
| 1184 | create.size = mode->hdisplay * mode->vdisplay * 4; |
| 1185 | if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) { |
| 1186 | fprintf(stderr, "gem create failed: %m\n"); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1187 | return -1; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1188 | } |
| 1189 | |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1190 | ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay, |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1191 | 32, 32, mode->hdisplay * 4, create.handle, &output->fb_id); |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1192 | if (ret) { |
| 1193 | fprintf(stderr, "failed to add fb: %m\n"); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1194 | return -1; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1195 | } |
| 1196 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1197 | ret = drmModeSetCrtc(fd, encoder->crtc_id, output->fb_id, 0, 0, |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1198 | &connector->connector_id, 1, mode); |
| 1199 | if (ret) { |
| 1200 | fprintf(stderr, "failed to set mode: %m\n"); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1201 | return -1; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | flink.handle = create.handle; |
| 1205 | if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) { |
| 1206 | fprintf(stderr, "gem flink failed: %m\n"); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1207 | return -1; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1208 | } |
| 1209 | |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 1210 | output->compositor = ec; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1211 | output->crtc_id = encoder->crtc_id; |
| 1212 | output->connector_id = connector->connector_id; |
| 1213 | output->mode = mode; |
Kristian Høgsberg | e68d24c | 2009-01-15 11:44:35 -0500 | [diff] [blame] | 1214 | output->x = 0; |
| 1215 | output->y = 0; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1216 | output->width = mode->hdisplay; |
| 1217 | output->height = mode->vdisplay; |
| 1218 | output->stride = mode->hdisplay * 4; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1219 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1220 | output->surface = eglCreateSurfaceForName(ec->display, ec->config, |
| 1221 | flink.name, |
| 1222 | output->width, output->height, |
| 1223 | output->stride, |
| 1224 | surface_attribs); |
| 1225 | if (output->surface == NULL) { |
| 1226 | fprintf(stderr, "failed to create surface\n"); |
| 1227 | return -1; |
| 1228 | } |
| 1229 | |
| 1230 | output->base.interface = &wl_output_interface; |
| 1231 | wl_display_add_object(ec->wl_display, &output->base); |
| 1232 | wl_display_add_global(ec->wl_display, &output->base, post_output_geometry); |
| 1233 | wl_list_insert(ec->output_list.prev, &output->link); |
| 1234 | |
| 1235 | if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) { |
| 1236 | fprintf(stderr, "failed to make context current\n"); |
| 1237 | return -1; |
| 1238 | } |
| 1239 | |
| 1240 | output->background = background_create(output, option_background); |
| 1241 | |
| 1242 | return 0; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1243 | } |
| 1244 | |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 1245 | static const struct wl_interface visual_interface = { |
| 1246 | "visual", 1, |
| 1247 | }; |
| 1248 | |
| 1249 | static void |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1250 | add_visuals(struct wlsc_compositor *ec) |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 1251 | { |
| 1252 | ec->argb_visual.base.interface = &visual_interface; |
| 1253 | ec->argb_visual.base.implementation = NULL; |
| 1254 | wl_display_add_object(ec->wl_display, &ec->argb_visual.base); |
Kristian Høgsberg | ee02ca6 | 2008-12-21 23:37:12 -0500 | [diff] [blame] | 1255 | 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] | 1256 | |
| 1257 | ec->premultiplied_argb_visual.base.interface = &visual_interface; |
| 1258 | ec->premultiplied_argb_visual.base.implementation = NULL; |
| 1259 | wl_display_add_object(ec->wl_display, |
| 1260 | &ec->premultiplied_argb_visual.base); |
| 1261 | wl_display_add_global(ec->wl_display, |
Kristian Høgsberg | ee02ca6 | 2008-12-21 23:37:12 -0500 | [diff] [blame] | 1262 | &ec->premultiplied_argb_visual.base, NULL); |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 1263 | |
| 1264 | ec->rgb_visual.base.interface = &visual_interface; |
| 1265 | ec->rgb_visual.base.implementation = NULL; |
| 1266 | wl_display_add_object(ec->wl_display, &ec->rgb_visual.base); |
Kristian Høgsberg | ee02ca6 | 2008-12-21 23:37:12 -0500 | [diff] [blame] | 1267 | wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL); |
| 1268 | } |
| 1269 | |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 1270 | static void on_enter_vt(int signal_number, void *data) |
| 1271 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1272 | struct wlsc_compositor *ec = data; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1273 | struct wlsc_output *output; |
Kristian Høgsberg | 2c875bd | 2008-12-19 10:34:02 -0500 | [diff] [blame] | 1274 | int ret, fd; |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 1275 | |
Kristian Høgsberg | 38ccd3a | 2008-12-19 10:15:35 -0500 | [diff] [blame] | 1276 | ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ); |
Ray Strode | e96dcb8 | 2008-12-20 02:00:49 -0500 | [diff] [blame] | 1277 | ec->vt_active = TRUE; |
Kristian Høgsberg | 38ccd3a | 2008-12-19 10:15:35 -0500 | [diff] [blame] | 1278 | |
Kristian Høgsberg | 2c875bd | 2008-12-19 10:34:02 -0500 | [diff] [blame] | 1279 | fd = eglGetDisplayFD(ec->display); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1280 | output = container_of(ec->output_list.next, struct wlsc_output, link); |
| 1281 | while (&output->link != &ec->output_list) { |
| 1282 | ret = drmModeSetCrtc(fd, output->crtc_id, output->fb_id, 0, 0, |
| 1283 | &output->connector_id, 1, output->mode); |
| 1284 | if (ret) |
| 1285 | fprintf(stderr, "failed to set mode for connector %d: %m\n", |
| 1286 | output->connector_id); |
| 1287 | |
| 1288 | output = container_of(output->link.next, |
| 1289 | struct wlsc_output, link); |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 1290 | } |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | static void on_leave_vt(int signal_number, void *data) |
| 1294 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1295 | struct wlsc_compositor *ec = data; |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 1296 | |
| 1297 | ioctl (ec->tty_fd, VT_RELDISP, 1); |
Ray Strode | e96dcb8 | 2008-12-20 02:00:49 -0500 | [diff] [blame] | 1298 | ec->vt_active = FALSE; |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 1299 | } |
| 1300 | |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 1301 | static void |
| 1302 | on_tty_input(int fd, uint32_t mask, void *data) |
| 1303 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1304 | struct wlsc_compositor *ec = data; |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 1305 | |
| 1306 | /* Ignore input to tty. We get keyboard events from evdev |
| 1307 | */ |
| 1308 | tcflush(ec->tty_fd, TCIFLUSH); |
| 1309 | } |
| 1310 | |
| 1311 | static void on_term_signal(int signal_number, void *data) |
| 1312 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1313 | struct wlsc_compositor *ec = data; |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 1314 | |
| 1315 | if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0) |
| 1316 | fprintf(stderr, "could not restore terminal to canonical mode\n"); |
| 1317 | |
| 1318 | exit(0); |
| 1319 | } |
| 1320 | |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1321 | 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] | 1322 | { |
| 1323 | struct termios raw_attributes; |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1324 | struct vt_mode mode = { 0 }; |
| 1325 | |
| 1326 | ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY); |
| 1327 | if (ec->tty_fd <= 0) { |
| 1328 | fprintf(stderr, "failed to open active tty: %m\n"); |
| 1329 | return -1; |
| 1330 | } |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 1331 | |
| 1332 | if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) { |
| 1333 | fprintf(stderr, "could not get terminal attributes: %m\n"); |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1334 | return -1; |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 1335 | } |
| 1336 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1337 | /* Ignore control characters and disable echo */ |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 1338 | raw_attributes = ec->terminal_attributes; |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1339 | cfmakeraw(&raw_attributes); |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 1340 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1341 | /* Fix up line endings to be normal (cfmakeraw hoses them) */ |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 1342 | raw_attributes.c_oflag |= OPOST | OCRNL; |
| 1343 | |
| 1344 | if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0) |
| 1345 | fprintf(stderr, "could not put terminal into raw mode: %m\n"); |
| 1346 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1347 | ec->term_signal_source = |
| 1348 | wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec); |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 1349 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1350 | ec->tty_input_source = |
| 1351 | wl_event_loop_add_fd(loop, ec->tty_fd, |
| 1352 | WL_EVENT_READABLE, on_tty_input, ec); |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 1353 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1354 | ec->vt_active = TRUE; |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 1355 | mode.mode = VT_PROCESS; |
| 1356 | mode.relsig = SIGUSR1; |
| 1357 | mode.acqsig = SIGUSR2; |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1358 | if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) { |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 1359 | fprintf(stderr, "failed to take control of vt handling\n"); |
| 1360 | } |
| 1361 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1362 | ec->leave_vt_source = |
| 1363 | wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec); |
| 1364 | ec->enter_vt_source = |
| 1365 | wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec); |
Kristian Høgsberg | fe831a7 | 2008-12-21 21:50:23 -0500 | [diff] [blame] | 1366 | |
| 1367 | return 0; |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 1368 | } |
| 1369 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1370 | static int |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1371 | init_libudev(struct wlsc_compositor *ec) |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 1372 | { |
| 1373 | struct udev_enumerate *e; |
| 1374 | struct udev_list_entry *entry; |
| 1375 | struct udev_device *device; |
Kristian Høgsberg | f13eb14 | 2009-01-26 21:38:14 -0500 | [diff] [blame] | 1376 | const char *path; |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 1377 | struct wlsc_input_device *input_device; |
| 1378 | |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 1379 | ec->udev = udev_new(); |
| 1380 | if (ec->udev == NULL) { |
| 1381 | fprintf(stderr, "failed to initialize udev context\n"); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1382 | return -1; |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | input_device = create_input_device(ec); |
| 1386 | |
| 1387 | e = udev_enumerate_new(ec->udev); |
Kristian Høgsberg | f13eb14 | 2009-01-26 21:38:14 -0500 | [diff] [blame] | 1388 | udev_enumerate_add_match_subsystem(e, "input"); |
| 1389 | udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1"); |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 1390 | udev_enumerate_scan_devices(e); |
| 1391 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
| 1392 | path = udev_list_entry_get_name(entry); |
| 1393 | device = udev_device_new_from_syspath(ec->udev, path); |
Kristian Høgsberg | f13eb14 | 2009-01-26 21:38:14 -0500 | [diff] [blame] | 1394 | evdev_input_device_create(input_device, ec->wl_display, |
| 1395 | udev_device_get_devnode(device)); |
| 1396 | } |
| 1397 | udev_enumerate_unref(e); |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 1398 | |
Kristian Høgsberg | f13eb14 | 2009-01-26 21:38:14 -0500 | [diff] [blame] | 1399 | e = udev_enumerate_new(ec->udev); |
| 1400 | udev_enumerate_add_match_subsystem(e, "drm"); |
| 1401 | udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1"); |
| 1402 | udev_enumerate_scan_devices(e); |
| 1403 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
| 1404 | path = udev_list_entry_get_name(entry); |
| 1405 | device = udev_device_new_from_syspath(ec->udev, path); |
| 1406 | if (create_output(ec, device) < 0) { |
| 1407 | fprintf(stderr, "failed to create output for %s\n", path); |
| 1408 | return -1; |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 1409 | } |
| 1410 | } |
| 1411 | udev_enumerate_unref(e); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1412 | |
| 1413 | /* Create the pointer surface now that we have a current EGL context. */ |
| 1414 | input_device->pointer_surface = |
| 1415 | pointer_create(ec, input_device->x, input_device->y, 64, 64); |
| 1416 | |
| 1417 | return 0; |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 1418 | } |
| 1419 | |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1420 | static struct wlsc_compositor * |
| 1421 | wlsc_compositor_create(struct wl_display *display) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1422 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1423 | struct wlsc_compositor *ec; |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 1424 | struct screenshooter *shooter; |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 1425 | struct wl_event_loop *loop; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1426 | |
| 1427 | ec = malloc(sizeof *ec); |
| 1428 | if (ec == NULL) |
| 1429 | return NULL; |
| 1430 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1431 | memset(ec, 0, sizeof *ec); |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 1432 | ec->wl_display = display; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1433 | |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 1434 | wl_display_set_compositor(display, &ec->base, &compositor_interface); |
Kristian Høgsberg | ee02ca6 | 2008-12-21 23:37:12 -0500 | [diff] [blame] | 1435 | |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 1436 | add_visuals(ec); |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 1437 | |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 1438 | wl_list_init(&ec->surface_list); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1439 | wl_list_init(&ec->input_device_list); |
| 1440 | wl_list_init(&ec->output_list); |
| 1441 | if (init_libudev(ec) < 0) { |
| 1442 | fprintf(stderr, "failed to initialize devices\n"); |
| 1443 | return NULL; |
| 1444 | } |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 1445 | |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 1446 | shooter = screenshooter_create(ec); |
| 1447 | wl_display_add_object(display, &shooter->base); |
Kristian Høgsberg | ee02ca6 | 2008-12-21 23:37:12 -0500 | [diff] [blame] | 1448 | wl_display_add_global(display, &shooter->base, NULL); |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 1449 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 1450 | loop = wl_display_get_event_loop(ec->wl_display); |
Ray Strode | e96dcb8 | 2008-12-20 02:00:49 -0500 | [diff] [blame] | 1451 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1452 | setup_tty(ec, loop); |
| 1453 | |
Kristian Høgsberg | 4a29890 | 2008-11-28 18:35:25 -0500 | [diff] [blame] | 1454 | ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec); |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 1455 | schedule_repaint(ec); |
| 1456 | |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame] | 1457 | wl_list_init(&ec->animate_list); |
| 1458 | |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1459 | return ec; |
| 1460 | } |
| 1461 | |
| 1462 | /* The plan here is to generate a random anonymous socket name and |
| 1463 | * advertise that through a service on the session dbus. |
| 1464 | */ |
| 1465 | static const char socket_name[] = "\0wayland"; |
| 1466 | |
| 1467 | int main(int argc, char *argv[]) |
| 1468 | { |
| 1469 | struct wl_display *display; |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1470 | struct wlsc_compositor *ec; |
Kristian Høgsberg | d653126 | 2008-12-12 11:06:18 -0500 | [diff] [blame] | 1471 | GError *error = NULL; |
| 1472 | GOptionContext *context; |
| 1473 | |
| 1474 | context = g_option_context_new(NULL); |
| 1475 | g_option_context_add_main_entries(context, option_entries, "Wayland"); |
| 1476 | if (!g_option_context_parse(context, &argc, &argv, &error)) { |
| 1477 | fprintf(stderr, "option parsing failed: %s\n", error->message); |
| 1478 | exit(EXIT_FAILURE); |
| 1479 | } |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1480 | |
| 1481 | display = wl_display_create(); |
| 1482 | |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1483 | ec = wlsc_compositor_create(display); |
Kristian Høgsberg | 841883b | 2008-12-05 11:19:56 -0500 | [diff] [blame] | 1484 | if (ec == NULL) { |
| 1485 | fprintf(stderr, "failed to create compositor\n"); |
| 1486 | exit(EXIT_FAILURE); |
| 1487 | } |
| 1488 | |
Kristian Høgsberg | dc0f355 | 2008-12-07 15:22:22 -0500 | [diff] [blame] | 1489 | if (wl_display_add_socket(display, socket_name, sizeof socket_name)) { |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1490 | fprintf(stderr, "failed to add socket: %m\n"); |
| 1491 | exit(EXIT_FAILURE); |
| 1492 | } |
| 1493 | |
| 1494 | wl_display_run(display); |
| 1495 | |
| 1496 | return 0; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1497 | } |