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 | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 676 | wlsc_surface_destroy(es, ec); |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 677 | |
| 678 | schedule_repaint(ec); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 679 | } |
| 680 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 681 | static void |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 682 | surface_attach(struct wl_client *client, |
| 683 | struct wl_surface *surface, uint32_t name, |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 684 | uint32_t width, uint32_t height, uint32_t stride, |
| 685 | struct wl_object *visual) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 686 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 687 | struct wlsc_surface *es = (struct wlsc_surface *) surface; |
| 688 | struct wlsc_compositor *ec = es->compositor; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 689 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 690 | if (es->surface != EGL_NO_SURFACE) |
| 691 | eglDestroySurface(ec->display, es->surface); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 692 | |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 693 | es->width = width; |
| 694 | es->height = height; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 695 | es->surface = eglCreateSurfaceForName(ec->display, ec->config, |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 696 | name, width, height, stride, NULL); |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 697 | if (visual == &ec->argb_visual.base) |
| 698 | es->visual = &ec->argb_visual; |
| 699 | else if (visual == &ec->premultiplied_argb_visual.base) |
| 700 | es->visual = &ec->premultiplied_argb_visual; |
Kristian Høgsberg | e10b828 | 2008-12-18 19:58:44 -0500 | [diff] [blame] | 701 | else if (visual == &ec->rgb_visual.base) |
| 702 | es->visual = &ec->rgb_visual; |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 703 | else |
| 704 | /* FIXME: Smack client with an exception event */; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 705 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 706 | glBindTexture(GL_TEXTURE_2D, es->texture); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 707 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
Kristian Høgsberg | 98fed0f | 2008-12-09 13:35:35 -0500 | [diff] [blame] | 708 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT); |
| 709 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 710 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 711 | eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D); |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 712 | } |
| 713 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 714 | static void |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 715 | surface_map(struct wl_client *client, |
| 716 | struct wl_surface *surface, |
| 717 | 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] | 718 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 719 | struct wlsc_surface *es = (struct wlsc_surface *) surface; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 720 | |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 721 | es->map.x = x; |
| 722 | es->map.y = y; |
| 723 | es->map.width = width; |
| 724 | es->map.height = height; |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 725 | wlsc_surface_update_matrix(es); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 726 | } |
| 727 | |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 728 | static void |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 729 | surface_copy(struct wl_client *client, |
| 730 | struct wl_surface *surface, |
| 731 | int32_t dst_x, int32_t dst_y, |
| 732 | uint32_t name, uint32_t stride, |
| 733 | 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] | 734 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 735 | struct wlsc_surface *es = (struct wlsc_surface *) surface; |
| 736 | struct wlsc_compositor *ec = es->compositor; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 737 | EGLSurface src; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 738 | |
| 739 | /* FIXME: glCopyPixels should work, but then we'll have to |
| 740 | * call eglMakeCurrent to set up the src and dest surfaces |
| 741 | * first. This seems cheaper, but maybe there's a better way |
| 742 | * to accomplish this. */ |
| 743 | |
| 744 | src = eglCreateSurfaceForName(ec->display, ec->config, |
| 745 | name, x + width, y + height, stride, NULL); |
| 746 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame] | 747 | 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] | 748 | src, GL_FRONT_LEFT, x, y, width, height); |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 749 | eglDestroySurface(ec->display, src); |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | static void |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 753 | surface_damage(struct wl_client *client, |
| 754 | struct wl_surface *surface, |
| 755 | 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] | 756 | { |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 757 | /* FIXME: This need to take a damage region, of course. */ |
| 758 | } |
| 759 | |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 760 | const static struct wl_surface_interface surface_interface = { |
| 761 | surface_destroy, |
| 762 | surface_attach, |
| 763 | surface_map, |
| 764 | surface_copy, |
| 765 | surface_damage |
| 766 | }; |
| 767 | |
| 768 | static void |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame^] | 769 | animate_surface(struct wlsc_animate *animate, |
| 770 | struct wlsc_compositor *compositor, |
| 771 | uint32_t frame, uint32_t msecs) |
| 772 | { |
| 773 | struct wlsc_surface *s; |
| 774 | double angle_force, angle; |
| 775 | struct wlsc_vector force, tmp; |
| 776 | double step = 0.3; |
| 777 | double friction = 1; |
| 778 | double spring = 0.2; |
| 779 | |
| 780 | s = container_of(animate, struct wlsc_surface, animate); |
| 781 | |
| 782 | angle = s->current_angle; |
| 783 | angle_force = (s->target_angle - angle) * spring + |
| 784 | (s->previous_angle - angle) * friction; |
| 785 | |
| 786 | s->current_angle = angle + (angle - s->previous_angle) + angle_force * step; |
| 787 | s->previous_angle = angle; |
| 788 | |
| 789 | force = s->target; |
| 790 | wlsc_vector_subtract(&force, &s->current); |
| 791 | wlsc_vector_scalar(&force, spring); |
| 792 | tmp = s->previous; |
| 793 | wlsc_vector_subtract(&tmp, &s->current); |
| 794 | wlsc_vector_scalar(&tmp, friction); |
| 795 | wlsc_vector_add(&force, &tmp); |
| 796 | |
| 797 | wlsc_vector_scalar(&force, step); |
| 798 | wlsc_vector_add(&force, &s->current); |
| 799 | wlsc_vector_subtract(&force, &s->previous); |
| 800 | s->previous = s->current; |
| 801 | wlsc_vector_add(&s->current, &force); |
| 802 | |
| 803 | wlsc_surface_update_matrix(s); |
| 804 | |
| 805 | tmp = s->current; |
| 806 | wlsc_vector_subtract(&tmp, &s->target); |
| 807 | if (tmp.x * tmp.x + tmp.y * tmp.y + tmp.z * tmp.z > 0.001) |
| 808 | schedule_repaint(compositor); |
| 809 | else |
| 810 | wl_list_remove(&s->animate.link); |
| 811 | } |
| 812 | |
| 813 | static void |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 814 | compositor_create_surface(struct wl_client *client, |
| 815 | struct wl_compositor *compositor, uint32_t id) |
| 816 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 817 | struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor; |
| 818 | struct wlsc_surface *es; |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 819 | |
| 820 | es = malloc(sizeof *es); |
| 821 | if (es == NULL) |
| 822 | /* FIXME: Send OOM event. */ |
| 823 | return; |
| 824 | |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame^] | 825 | wlsc_surface_init(es, ec, NULL, 0, 0, 0, 0); |
| 826 | es->animate.animate = animate_surface; |
| 827 | wl_list_init(&es->animate.link); |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 828 | |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 829 | wl_list_insert(ec->surface_list.prev, &es->link); |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 830 | wl_client_add_surface(client, &es->base, |
| 831 | &surface_interface, id); |
| 832 | } |
| 833 | |
| 834 | static void |
| 835 | compositor_commit(struct wl_client *client, |
| 836 | struct wl_compositor *compositor, uint32_t key) |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 837 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 838 | struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor; |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 839 | |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 840 | schedule_repaint(ec); |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 841 | wl_client_send_acknowledge(client, compositor, key, ec->current_frame); |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 842 | } |
| 843 | |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 844 | const static struct wl_compositor_interface compositor_interface = { |
| 845 | compositor_create_surface, |
| 846 | compositor_commit |
| 847 | }; |
| 848 | |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 849 | static struct wlsc_surface * |
Kristian Høgsberg | 7e972a5 | 2008-12-21 17:26:00 -0500 | [diff] [blame] | 850 | 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] | 851 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 852 | struct wlsc_compositor *ec = device->ec; |
| 853 | struct wlsc_surface *es; |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 854 | |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 855 | if (device->grab > 0) |
| 856 | return device->grab_surface; |
| 857 | |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 858 | es = container_of(ec->surface_list.prev, |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 859 | struct wlsc_surface, link); |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 860 | while (&es->link != &ec->surface_list) { |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 861 | if (es->map.x <= device->x && |
| 862 | device->x < es->map.x + es->map.width && |
| 863 | es->map.y <= device->y && |
| 864 | device->y < es->map.y + es->map.height) |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 865 | return es; |
| 866 | |
| 867 | es = container_of(es->link.prev, |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 868 | struct wlsc_surface, link); |
Kristian Høgsberg | 7e972a5 | 2008-12-21 17:26:00 -0500 | [diff] [blame] | 869 | |
| 870 | /* Transform to surface coordinates. */ |
Kristian Høgsberg | e3ef3e5 | 2008-12-21 19:30:01 -0500 | [diff] [blame] | 871 | *sx = (device->x - es->map.x) * es->width / es->map.width; |
| 872 | *sy = (device->y - es->map.y) * es->height / es->map.height; |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | return NULL; |
| 876 | } |
| 877 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 878 | void |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 879 | notify_motion(struct wlsc_input_device *device, int x, int y) |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 880 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 881 | struct wlsc_surface *es; |
| 882 | struct wlsc_compositor *ec = device->ec; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 883 | struct wlsc_output *output; |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 884 | const int hotspot_x = 16, hotspot_y = 16; |
| 885 | int32_t sx, sy; |
| 886 | |
Ray Strode | e96dcb8 | 2008-12-20 02:00:49 -0500 | [diff] [blame] | 887 | if (!ec->vt_active) |
| 888 | return; |
| 889 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 890 | /* FIXME: We need some multi head love here. */ |
| 891 | output = container_of(ec->output_list.next, struct wlsc_output, link); |
| 892 | if (x < output->x) |
Ray Strode | 90e701d | 2008-12-18 23:05:43 -0500 | [diff] [blame] | 893 | x = 0; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 894 | if (y < output->y) |
Ray Strode | 90e701d | 2008-12-18 23:05:43 -0500 | [diff] [blame] | 895 | y = 0; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 896 | if (x >= output->x + output->width) |
| 897 | x = output->x + output->width - 1; |
| 898 | if (y >= output->y + output->height) |
| 899 | y = output->y + output->height - 1; |
Ray Strode | 90e701d | 2008-12-18 23:05:43 -0500 | [diff] [blame] | 900 | |
Kristian Høgsberg | e3ef3e5 | 2008-12-21 19:30:01 -0500 | [diff] [blame] | 901 | device->x = x; |
| 902 | device->y = y; |
Kristian Høgsberg | 7e972a5 | 2008-12-21 17:26:00 -0500 | [diff] [blame] | 903 | es = pick_surface(device, &sx, &sy); |
| 904 | if (es) |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 905 | wl_surface_post_event(&es->base, &device->base, |
Kristian Høgsberg | 5a75c90 | 2008-12-10 13:16:50 -0500 | [diff] [blame] | 906 | WL_INPUT_MOTION, x, y, sx, sy); |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 907 | |
Kristian Høgsberg | 29573bc | 2008-12-11 23:27:27 -0500 | [diff] [blame] | 908 | device->pointer_surface->map.x = x - hotspot_x; |
| 909 | device->pointer_surface->map.y = y - hotspot_y; |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 910 | |
| 911 | schedule_repaint(device->ec); |
Kristian Høgsberg | 715a081 | 2008-12-10 10:42:04 -0500 | [diff] [blame] | 912 | } |
| 913 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 914 | void |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 915 | notify_button(struct wlsc_input_device *device, |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 916 | int32_t button, int32_t state) |
Kristian Høgsberg | eac149a | 2008-12-10 00:24:18 -0500 | [diff] [blame] | 917 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 918 | struct wlsc_surface *es; |
| 919 | struct wlsc_compositor *ec = device->ec; |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 920 | int32_t sx, sy; |
Kristian Høgsberg | eac149a | 2008-12-10 00:24:18 -0500 | [diff] [blame] | 921 | |
Ray Strode | e96dcb8 | 2008-12-20 02:00:49 -0500 | [diff] [blame] | 922 | if (!ec->vt_active) |
| 923 | return; |
| 924 | |
Kristian Høgsberg | 7e972a5 | 2008-12-21 17:26:00 -0500 | [diff] [blame] | 925 | es = pick_surface(device, &sx, &sy); |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 926 | if (es) { |
| 927 | wl_list_remove(&es->link); |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 928 | wl_list_insert(device->ec->surface_list.prev, &es->link); |
Kristian Høgsberg | 5a75c90 | 2008-12-10 13:16:50 -0500 | [diff] [blame] | 929 | |
Kristian Høgsberg | 29573bc | 2008-12-11 23:27:27 -0500 | [diff] [blame] | 930 | if (state) { |
Kristian Høgsberg | a7700c8 | 2008-12-12 13:48:30 -0500 | [diff] [blame] | 931 | /* FIXME: We need callbacks when the surfaces |
| 932 | * we reference here go away. */ |
Kristian Høgsberg | 29573bc | 2008-12-11 23:27:27 -0500 | [diff] [blame] | 933 | device->grab++; |
| 934 | device->grab_surface = es; |
Kristian Høgsberg | a7700c8 | 2008-12-12 13:48:30 -0500 | [diff] [blame] | 935 | device->focus_surface = es; |
Kristian Høgsberg | 29573bc | 2008-12-11 23:27:27 -0500 | [diff] [blame] | 936 | } else { |
| 937 | device->grab--; |
| 938 | } |
| 939 | |
Kristian Høgsberg | 5a75c90 | 2008-12-10 13:16:50 -0500 | [diff] [blame] | 940 | /* FIXME: Swallow click on raise? */ |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 941 | wl_surface_post_event(&es->base, &device->base, |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 942 | WL_INPUT_BUTTON, button, state, |
| 943 | device->x, device->y, sx, sy); |
Kristian Høgsberg | eac149a | 2008-12-10 00:24:18 -0500 | [diff] [blame] | 944 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 945 | schedule_repaint(device->ec); |
| 946 | } |
Kristian Høgsberg | eac149a | 2008-12-10 00:24:18 -0500 | [diff] [blame] | 947 | } |
| 948 | |
Kristian Høgsberg | ab909ae | 2001-01-01 22:24:24 -0500 | [diff] [blame] | 949 | static void on_term_signal(int signal_number, void *data); |
| 950 | |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame^] | 951 | static void |
| 952 | update_surface_targets(struct wlsc_compositor *compositor, int primary) |
| 953 | { |
| 954 | struct wlsc_surface *s; |
| 955 | int i; |
| 956 | |
| 957 | i = 0; |
| 958 | s = container_of(compositor->surface_list.next, |
| 959 | struct wlsc_surface, link); |
| 960 | while (&s->link != &compositor->surface_list) { |
| 961 | if (i < primary) { |
| 962 | s->target.x = -400 + 500 * (i - primary); |
| 963 | s->target.y = 0; |
| 964 | s->target.z = -1500; |
| 965 | s->target_angle = M_PI / 4; |
| 966 | } else if (i == primary) { |
| 967 | s->target.x = 0; |
| 968 | s->target.y = 0; |
| 969 | s->target.z = -1000; |
| 970 | s->target_angle = 0; |
| 971 | compositor->primary = s; |
| 972 | } else { |
| 973 | s->target.x = 400 + 500 * (i - primary); |
| 974 | s->target.y = 0; |
| 975 | s->target.z = -1500; |
| 976 | s->target_angle = -M_PI / 4; |
| 977 | } |
| 978 | wl_list_remove(&s->animate.link); |
| 979 | wl_list_insert(compositor->animate_list.prev, &s->animate.link); |
| 980 | s = container_of(s->link.next, |
| 981 | struct wlsc_surface, link); |
| 982 | i++; |
| 983 | } |
| 984 | |
| 985 | schedule_repaint(compositor); |
| 986 | } |
| 987 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 988 | void |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 989 | notify_key(struct wlsc_input_device *device, |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 990 | uint32_t key, uint32_t state) |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 991 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 992 | struct wlsc_compositor *ec = device->ec; |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame^] | 993 | struct wlsc_surface *s; |
Ray Strode | e96dcb8 | 2008-12-20 02:00:49 -0500 | [diff] [blame] | 994 | |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 995 | switch (key | ec->meta_state) { |
| 996 | case KEY_EJECTCD | META_DOWN: |
Kristian Høgsberg | ab909ae | 2001-01-01 22:24:24 -0500 | [diff] [blame] | 997 | on_term_signal(SIGTERM, ec); |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 998 | return; |
| 999 | |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame^] | 1000 | case KEY_1 | META_DOWN: |
| 1001 | case KEY_2 | META_DOWN: |
| 1002 | case KEY_3 | META_DOWN: |
| 1003 | case KEY_4 | META_DOWN: |
| 1004 | case KEY_5 | META_DOWN: |
| 1005 | update_surface_targets(ec, key - KEY_1); |
| 1006 | if (device->grab == 0 && s != NULL) |
| 1007 | device->focus_surface = ec->primary; |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 1008 | return; |
| 1009 | |
| 1010 | case KEY_LEFTMETA: |
| 1011 | case KEY_RIGHTMETA: |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 1012 | case KEY_LEFTMETA | META_DOWN: |
| 1013 | case KEY_RIGHTMETA | META_DOWN: |
| 1014 | ec->meta_state = state ? META_DOWN : 0; |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame^] | 1015 | if (state == 0) { |
| 1016 | ec->primary->target.z = 0; |
| 1017 | wl_list_remove(&ec->primary->animate.link); |
| 1018 | wl_list_insert(&ec->animate_list, |
| 1019 | &ec->primary->animate.link); |
| 1020 | schedule_repaint(ec); |
| 1021 | } |
Kristian Høgsberg | 5c8c328 | 2009-02-09 15:17:46 -0500 | [diff] [blame] | 1022 | return; |
| 1023 | } |
Kristian Høgsberg | ab909ae | 2001-01-01 22:24:24 -0500 | [diff] [blame] | 1024 | |
Ray Strode | e96dcb8 | 2008-12-20 02:00:49 -0500 | [diff] [blame] | 1025 | if (!ec->vt_active) |
| 1026 | return; |
| 1027 | |
Kristian Høgsberg | 2c0e56b | 2008-12-19 13:54:40 -0500 | [diff] [blame] | 1028 | if (device->focus_surface != NULL) |
| 1029 | wl_surface_post_event(&device->focus_surface->base, |
| 1030 | &device->base, |
| 1031 | WL_INPUT_KEY, key, state); |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 1032 | } |
| 1033 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 1034 | struct evdev_input_device * |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 1035 | evdev_input_device_create(struct wlsc_input_device *device, |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 1036 | struct wl_display *display, const char *path); |
| 1037 | |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 1038 | static struct wlsc_input_device * |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1039 | create_input_device(struct wlsc_compositor *ec) |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 1040 | { |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 1041 | struct wlsc_input_device *device; |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 1042 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 1043 | device = malloc(sizeof *device); |
| 1044 | if (device == NULL) |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 1045 | return NULL; |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 1046 | |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 1047 | memset(device, 0, sizeof *device); |
Kristian Høgsberg | fe831a7 | 2008-12-21 21:50:23 -0500 | [diff] [blame] | 1048 | device->base.interface = &wl_input_device_interface; |
Kristian Høgsberg | b3131d9 | 2008-12-24 19:30:25 -0500 | [diff] [blame] | 1049 | device->base.implementation = NULL; |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 1050 | wl_display_add_object(ec->wl_display, &device->base); |
Kristian Høgsberg | b3131d9 | 2008-12-24 19:30:25 -0500 | [diff] [blame] | 1051 | wl_display_add_global(ec->wl_display, &device->base, NULL); |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 1052 | device->x = 100; |
| 1053 | device->y = 100; |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 1054 | device->ec = ec; |
| 1055 | |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 1056 | wl_list_insert(ec->input_device_list.prev, &device->link); |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 1057 | |
| 1058 | return device; |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | void |
Kristian Høgsberg | 82f6e8a | 2008-12-19 13:47:53 -0500 | [diff] [blame] | 1062 | 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] | 1063 | { |
| 1064 | *x = device->x; |
| 1065 | *y = device->y; |
Kristian Høgsberg | cddc0ad | 2008-11-24 00:31:49 -0500 | [diff] [blame] | 1066 | } |
| 1067 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1068 | static void |
| 1069 | post_output_geometry(struct wl_client *client, struct wl_object *global) |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1070 | { |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1071 | struct wlsc_output *output = |
| 1072 | container_of(global, struct wlsc_output, base); |
| 1073 | |
| 1074 | wl_client_post_event(client, global, |
| 1075 | WL_OUTPUT_GEOMETRY, |
| 1076 | output->width, output->height); |
| 1077 | } |
| 1078 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1079 | static int |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1080 | init_egl(struct wlsc_compositor *ec, struct udev_device *device) |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1081 | { |
| 1082 | static const EGLint config_attribs[] = { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1083 | EGL_DEPTH_SIZE, 0, |
| 1084 | EGL_STENCIL_SIZE, 0, |
| 1085 | EGL_CONFIG_CAVEAT, EGL_NONE, |
| 1086 | EGL_RED_SIZE, 8, |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1087 | EGL_NONE |
| 1088 | }; |
| 1089 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1090 | EGLint major, minor; |
| 1091 | |
Kristian Høgsberg | aa68fe3 | 2009-01-15 12:50:21 -0500 | [diff] [blame] | 1092 | ec->display = eglCreateDisplayNative(device); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1093 | if (ec->display == NULL) { |
| 1094 | fprintf(stderr, "failed to create display\n"); |
| 1095 | return -1; |
| 1096 | } |
| 1097 | |
| 1098 | if (!eglInitialize(ec->display, &major, &minor)) { |
| 1099 | fprintf(stderr, "failed to initialize display\n"); |
| 1100 | return -1; |
| 1101 | } |
| 1102 | |
| 1103 | if (!eglChooseConfig(ec->display, config_attribs, &ec->config, 1, NULL)) |
| 1104 | return -1; |
| 1105 | |
| 1106 | ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL); |
| 1107 | if (ec->context == NULL) { |
| 1108 | fprintf(stderr, "failed to create context\n"); |
| 1109 | return -1; |
| 1110 | } |
| 1111 | return 0; |
| 1112 | } |
| 1113 | |
| 1114 | static int |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1115 | create_output(struct wlsc_compositor *ec, struct udev_device *device) |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1116 | { |
| 1117 | const static EGLint surface_attribs[] = { |
| 1118 | EGL_RENDER_BUFFER, EGL_BACK_BUFFER, |
| 1119 | EGL_NONE |
| 1120 | }; |
| 1121 | |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1122 | drmModeConnector *connector; |
| 1123 | drmModeRes *resources; |
| 1124 | drmModeEncoder *encoder; |
| 1125 | struct drm_mode_modeinfo *mode; |
| 1126 | struct drm_i915_gem_create create; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1127 | struct drm_gem_flink flink; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1128 | struct wlsc_output *output; |
Kristian Høgsberg | 2c875bd | 2008-12-19 10:34:02 -0500 | [diff] [blame] | 1129 | int i, ret, fd; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1130 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1131 | if (ec->display == NULL && init_egl(ec, device) < 0) { |
| 1132 | fprintf(stderr, "failed to initialize egl\n"); |
| 1133 | return -1; |
| 1134 | } |
| 1135 | |
| 1136 | output = malloc(sizeof *output); |
| 1137 | if (output == NULL) |
| 1138 | return -1; |
| 1139 | |
Kristian Høgsberg | 2c875bd | 2008-12-19 10:34:02 -0500 | [diff] [blame] | 1140 | fd = eglGetDisplayFD(ec->display); |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1141 | resources = drmModeGetResources(fd); |
| 1142 | if (!resources) { |
| 1143 | fprintf(stderr, "drmModeGetResources failed\n"); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1144 | return -1; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | for (i = 0; i < resources->count_connectors; i++) { |
| 1148 | connector = drmModeGetConnector(fd, resources->connectors[i]); |
| 1149 | if (connector == NULL) |
| 1150 | continue; |
| 1151 | |
| 1152 | if (connector->connection == DRM_MODE_CONNECTED && |
| 1153 | connector->count_modes > 0) |
| 1154 | break; |
| 1155 | |
| 1156 | drmModeFreeConnector(connector); |
| 1157 | } |
| 1158 | |
| 1159 | if (i == resources->count_connectors) { |
| 1160 | fprintf(stderr, "No currently active connector found.\n"); |
| 1161 | return -1; |
| 1162 | } |
| 1163 | |
| 1164 | mode = &connector->modes[0]; |
| 1165 | |
| 1166 | for (i = 0; i < resources->count_encoders; i++) { |
| 1167 | encoder = drmModeGetEncoder(fd, resources->encoders[i]); |
| 1168 | |
| 1169 | if (encoder == NULL) |
| 1170 | continue; |
| 1171 | |
| 1172 | if (encoder->encoder_id == connector->encoder_id) |
| 1173 | break; |
| 1174 | |
| 1175 | drmModeFreeEncoder(encoder); |
| 1176 | } |
| 1177 | |
| 1178 | /* Mode size at 32 bpp */ |
| 1179 | create.size = mode->hdisplay * mode->vdisplay * 4; |
| 1180 | if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) { |
| 1181 | fprintf(stderr, "gem create failed: %m\n"); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1182 | return -1; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1183 | } |
| 1184 | |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1185 | ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay, |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1186 | 32, 32, mode->hdisplay * 4, create.handle, &output->fb_id); |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1187 | if (ret) { |
| 1188 | fprintf(stderr, "failed to add fb: %m\n"); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1189 | return -1; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1190 | } |
| 1191 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1192 | ret = drmModeSetCrtc(fd, encoder->crtc_id, output->fb_id, 0, 0, |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1193 | &connector->connector_id, 1, mode); |
| 1194 | if (ret) { |
| 1195 | fprintf(stderr, "failed to set mode: %m\n"); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1196 | return -1; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | flink.handle = create.handle; |
| 1200 | if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) { |
| 1201 | fprintf(stderr, "gem flink failed: %m\n"); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1202 | return -1; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1203 | } |
| 1204 | |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame^] | 1205 | output->compositor = ec; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1206 | output->crtc_id = encoder->crtc_id; |
| 1207 | output->connector_id = connector->connector_id; |
| 1208 | output->mode = mode; |
Kristian Høgsberg | e68d24c | 2009-01-15 11:44:35 -0500 | [diff] [blame] | 1209 | output->x = 0; |
| 1210 | output->y = 0; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1211 | output->width = mode->hdisplay; |
| 1212 | output->height = mode->vdisplay; |
| 1213 | output->stride = mode->hdisplay * 4; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1214 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1215 | output->surface = eglCreateSurfaceForName(ec->display, ec->config, |
| 1216 | flink.name, |
| 1217 | output->width, output->height, |
| 1218 | output->stride, |
| 1219 | surface_attribs); |
| 1220 | if (output->surface == NULL) { |
| 1221 | fprintf(stderr, "failed to create surface\n"); |
| 1222 | return -1; |
| 1223 | } |
| 1224 | |
| 1225 | output->base.interface = &wl_output_interface; |
| 1226 | wl_display_add_object(ec->wl_display, &output->base); |
| 1227 | wl_display_add_global(ec->wl_display, &output->base, post_output_geometry); |
| 1228 | wl_list_insert(ec->output_list.prev, &output->link); |
| 1229 | |
| 1230 | if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) { |
| 1231 | fprintf(stderr, "failed to make context current\n"); |
| 1232 | return -1; |
| 1233 | } |
| 1234 | |
| 1235 | output->background = background_create(output, option_background); |
| 1236 | |
| 1237 | return 0; |
Kristian Høgsberg | bf9541f | 2008-11-25 12:10:09 -0500 | [diff] [blame] | 1238 | } |
| 1239 | |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 1240 | static const struct wl_interface visual_interface = { |
| 1241 | "visual", 1, |
| 1242 | }; |
| 1243 | |
| 1244 | static void |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1245 | add_visuals(struct wlsc_compositor *ec) |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 1246 | { |
| 1247 | ec->argb_visual.base.interface = &visual_interface; |
| 1248 | ec->argb_visual.base.implementation = NULL; |
| 1249 | wl_display_add_object(ec->wl_display, &ec->argb_visual.base); |
Kristian Høgsberg | ee02ca6 | 2008-12-21 23:37:12 -0500 | [diff] [blame] | 1250 | 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] | 1251 | |
| 1252 | ec->premultiplied_argb_visual.base.interface = &visual_interface; |
| 1253 | ec->premultiplied_argb_visual.base.implementation = NULL; |
| 1254 | wl_display_add_object(ec->wl_display, |
| 1255 | &ec->premultiplied_argb_visual.base); |
| 1256 | wl_display_add_global(ec->wl_display, |
Kristian Høgsberg | ee02ca6 | 2008-12-21 23:37:12 -0500 | [diff] [blame] | 1257 | &ec->premultiplied_argb_visual.base, NULL); |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 1258 | |
| 1259 | ec->rgb_visual.base.interface = &visual_interface; |
| 1260 | ec->rgb_visual.base.implementation = NULL; |
| 1261 | wl_display_add_object(ec->wl_display, &ec->rgb_visual.base); |
Kristian Høgsberg | ee02ca6 | 2008-12-21 23:37:12 -0500 | [diff] [blame] | 1262 | wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL); |
| 1263 | } |
| 1264 | |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 1265 | static void on_enter_vt(int signal_number, void *data) |
| 1266 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1267 | struct wlsc_compositor *ec = data; |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1268 | struct wlsc_output *output; |
Kristian Høgsberg | 2c875bd | 2008-12-19 10:34:02 -0500 | [diff] [blame] | 1269 | int ret, fd; |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 1270 | |
Kristian Høgsberg | 38ccd3a | 2008-12-19 10:15:35 -0500 | [diff] [blame] | 1271 | ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ); |
Ray Strode | e96dcb8 | 2008-12-20 02:00:49 -0500 | [diff] [blame] | 1272 | ec->vt_active = TRUE; |
Kristian Høgsberg | 38ccd3a | 2008-12-19 10:15:35 -0500 | [diff] [blame] | 1273 | |
Kristian Høgsberg | 2c875bd | 2008-12-19 10:34:02 -0500 | [diff] [blame] | 1274 | fd = eglGetDisplayFD(ec->display); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1275 | output = container_of(ec->output_list.next, struct wlsc_output, link); |
| 1276 | while (&output->link != &ec->output_list) { |
| 1277 | ret = drmModeSetCrtc(fd, output->crtc_id, output->fb_id, 0, 0, |
| 1278 | &output->connector_id, 1, output->mode); |
| 1279 | if (ret) |
| 1280 | fprintf(stderr, "failed to set mode for connector %d: %m\n", |
| 1281 | output->connector_id); |
| 1282 | |
| 1283 | output = container_of(output->link.next, |
| 1284 | struct wlsc_output, link); |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 1285 | } |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | static void on_leave_vt(int signal_number, void *data) |
| 1289 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1290 | struct wlsc_compositor *ec = data; |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 1291 | |
| 1292 | ioctl (ec->tty_fd, VT_RELDISP, 1); |
Ray Strode | e96dcb8 | 2008-12-20 02:00:49 -0500 | [diff] [blame] | 1293 | ec->vt_active = FALSE; |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 1294 | } |
| 1295 | |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 1296 | static void |
| 1297 | on_tty_input(int fd, uint32_t mask, void *data) |
| 1298 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1299 | struct wlsc_compositor *ec = data; |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 1300 | |
| 1301 | /* Ignore input to tty. We get keyboard events from evdev |
| 1302 | */ |
| 1303 | tcflush(ec->tty_fd, TCIFLUSH); |
| 1304 | } |
| 1305 | |
| 1306 | static void on_term_signal(int signal_number, void *data) |
| 1307 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1308 | struct wlsc_compositor *ec = data; |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 1309 | |
| 1310 | if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0) |
| 1311 | fprintf(stderr, "could not restore terminal to canonical mode\n"); |
| 1312 | |
| 1313 | exit(0); |
| 1314 | } |
| 1315 | |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1316 | 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] | 1317 | { |
| 1318 | struct termios raw_attributes; |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1319 | struct vt_mode mode = { 0 }; |
| 1320 | |
| 1321 | ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY); |
| 1322 | if (ec->tty_fd <= 0) { |
| 1323 | fprintf(stderr, "failed to open active tty: %m\n"); |
| 1324 | return -1; |
| 1325 | } |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 1326 | |
| 1327 | if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) { |
| 1328 | fprintf(stderr, "could not get terminal attributes: %m\n"); |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1329 | return -1; |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 1330 | } |
| 1331 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1332 | /* Ignore control characters and disable echo */ |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 1333 | raw_attributes = ec->terminal_attributes; |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1334 | cfmakeraw(&raw_attributes); |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 1335 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1336 | /* Fix up line endings to be normal (cfmakeraw hoses them) */ |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 1337 | raw_attributes.c_oflag |= OPOST | OCRNL; |
| 1338 | |
| 1339 | if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0) |
| 1340 | fprintf(stderr, "could not put terminal into raw mode: %m\n"); |
| 1341 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1342 | ec->term_signal_source = |
| 1343 | wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec); |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 1344 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1345 | ec->tty_input_source = |
| 1346 | wl_event_loop_add_fd(loop, ec->tty_fd, |
| 1347 | WL_EVENT_READABLE, on_tty_input, ec); |
Ray Strode | 966aa11 | 2008-12-19 14:28:02 -0500 | [diff] [blame] | 1348 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1349 | ec->vt_active = TRUE; |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 1350 | mode.mode = VT_PROCESS; |
| 1351 | mode.relsig = SIGUSR1; |
| 1352 | mode.acqsig = SIGUSR2; |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1353 | if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) { |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 1354 | fprintf(stderr, "failed to take control of vt handling\n"); |
| 1355 | } |
| 1356 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1357 | ec->leave_vt_source = |
| 1358 | wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec); |
| 1359 | ec->enter_vt_source = |
| 1360 | wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec); |
Kristian Høgsberg | fe831a7 | 2008-12-21 21:50:23 -0500 | [diff] [blame] | 1361 | |
| 1362 | return 0; |
Ray Strode | 19ad6a9 | 2008-12-19 01:45:41 -0500 | [diff] [blame] | 1363 | } |
| 1364 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1365 | static int |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1366 | init_libudev(struct wlsc_compositor *ec) |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 1367 | { |
| 1368 | struct udev_enumerate *e; |
| 1369 | struct udev_list_entry *entry; |
| 1370 | struct udev_device *device; |
Kristian Høgsberg | f13eb14 | 2009-01-26 21:38:14 -0500 | [diff] [blame] | 1371 | const char *path; |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 1372 | struct wlsc_input_device *input_device; |
| 1373 | |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 1374 | ec->udev = udev_new(); |
| 1375 | if (ec->udev == NULL) { |
| 1376 | fprintf(stderr, "failed to initialize udev context\n"); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1377 | return -1; |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | input_device = create_input_device(ec); |
| 1381 | |
| 1382 | e = udev_enumerate_new(ec->udev); |
Kristian Høgsberg | f13eb14 | 2009-01-26 21:38:14 -0500 | [diff] [blame] | 1383 | udev_enumerate_add_match_subsystem(e, "input"); |
| 1384 | udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1"); |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 1385 | udev_enumerate_scan_devices(e); |
| 1386 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
| 1387 | path = udev_list_entry_get_name(entry); |
| 1388 | device = udev_device_new_from_syspath(ec->udev, path); |
Kristian Høgsberg | f13eb14 | 2009-01-26 21:38:14 -0500 | [diff] [blame] | 1389 | evdev_input_device_create(input_device, ec->wl_display, |
| 1390 | udev_device_get_devnode(device)); |
| 1391 | } |
| 1392 | udev_enumerate_unref(e); |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 1393 | |
Kristian Høgsberg | f13eb14 | 2009-01-26 21:38:14 -0500 | [diff] [blame] | 1394 | e = udev_enumerate_new(ec->udev); |
| 1395 | udev_enumerate_add_match_subsystem(e, "drm"); |
| 1396 | udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1"); |
| 1397 | udev_enumerate_scan_devices(e); |
| 1398 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
| 1399 | path = udev_list_entry_get_name(entry); |
| 1400 | device = udev_device_new_from_syspath(ec->udev, path); |
| 1401 | if (create_output(ec, device) < 0) { |
| 1402 | fprintf(stderr, "failed to create output for %s\n", path); |
| 1403 | return -1; |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 1404 | } |
| 1405 | } |
| 1406 | udev_enumerate_unref(e); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1407 | |
| 1408 | /* Create the pointer surface now that we have a current EGL context. */ |
| 1409 | input_device->pointer_surface = |
| 1410 | pointer_create(ec, input_device->x, input_device->y, 64, 64); |
| 1411 | |
| 1412 | return 0; |
Kristian Høgsberg | 890bc05 | 2008-12-30 14:31:33 -0500 | [diff] [blame] | 1413 | } |
| 1414 | |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1415 | static struct wlsc_compositor * |
| 1416 | wlsc_compositor_create(struct wl_display *display) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1417 | { |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1418 | struct wlsc_compositor *ec; |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 1419 | struct screenshooter *shooter; |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 1420 | struct wl_event_loop *loop; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1421 | |
| 1422 | ec = malloc(sizeof *ec); |
| 1423 | if (ec == NULL) |
| 1424 | return NULL; |
| 1425 | |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1426 | memset(ec, 0, sizeof *ec); |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 1427 | ec->wl_display = display; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1428 | |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 1429 | wl_display_set_compositor(display, &ec->base, &compositor_interface); |
Kristian Høgsberg | ee02ca6 | 2008-12-21 23:37:12 -0500 | [diff] [blame] | 1430 | |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 1431 | add_visuals(ec); |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 1432 | |
Kristian Høgsberg | 201a904 | 2008-12-10 00:40:50 -0500 | [diff] [blame] | 1433 | wl_list_init(&ec->surface_list); |
Kristian Høgsberg | 81ce09a | 2008-12-31 16:18:42 -0500 | [diff] [blame] | 1434 | wl_list_init(&ec->input_device_list); |
| 1435 | wl_list_init(&ec->output_list); |
| 1436 | if (init_libudev(ec) < 0) { |
| 1437 | fprintf(stderr, "failed to initialize devices\n"); |
| 1438 | return NULL; |
| 1439 | } |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 1440 | |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 1441 | shooter = screenshooter_create(ec); |
| 1442 | wl_display_add_object(display, &shooter->base); |
Kristian Høgsberg | ee02ca6 | 2008-12-21 23:37:12 -0500 | [diff] [blame] | 1443 | wl_display_add_global(display, &shooter->base, NULL); |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 1444 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 1445 | loop = wl_display_get_event_loop(ec->wl_display); |
Ray Strode | e96dcb8 | 2008-12-20 02:00:49 -0500 | [diff] [blame] | 1446 | |
Kristian Høgsberg | 0ab2624 | 2008-12-21 19:33:09 -0500 | [diff] [blame] | 1447 | setup_tty(ec, loop); |
| 1448 | |
Kristian Høgsberg | 4a29890 | 2008-11-28 18:35:25 -0500 | [diff] [blame] | 1449 | ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec); |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 1450 | schedule_repaint(ec); |
| 1451 | |
Kristian Høgsberg | 1a208d5 | 2009-02-10 14:20:26 -0500 | [diff] [blame^] | 1452 | wl_list_init(&ec->animate_list); |
| 1453 | |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1454 | return ec; |
| 1455 | } |
| 1456 | |
| 1457 | /* The plan here is to generate a random anonymous socket name and |
| 1458 | * advertise that through a service on the session dbus. |
| 1459 | */ |
| 1460 | static const char socket_name[] = "\0wayland"; |
| 1461 | |
| 1462 | int main(int argc, char *argv[]) |
| 1463 | { |
| 1464 | struct wl_display *display; |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1465 | struct wlsc_compositor *ec; |
Kristian Høgsberg | d653126 | 2008-12-12 11:06:18 -0500 | [diff] [blame] | 1466 | GError *error = NULL; |
| 1467 | GOptionContext *context; |
| 1468 | |
| 1469 | context = g_option_context_new(NULL); |
| 1470 | g_option_context_add_main_entries(context, option_entries, "Wayland"); |
| 1471 | if (!g_option_context_parse(context, &argc, &argv, &error)) { |
| 1472 | fprintf(stderr, "option parsing failed: %s\n", error->message); |
| 1473 | exit(EXIT_FAILURE); |
| 1474 | } |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1475 | |
| 1476 | display = wl_display_create(); |
| 1477 | |
Kristian Høgsberg | 8e43862 | 2009-01-26 23:07:00 -0500 | [diff] [blame] | 1478 | ec = wlsc_compositor_create(display); |
Kristian Høgsberg | 841883b | 2008-12-05 11:19:56 -0500 | [diff] [blame] | 1479 | if (ec == NULL) { |
| 1480 | fprintf(stderr, "failed to create compositor\n"); |
| 1481 | exit(EXIT_FAILURE); |
| 1482 | } |
| 1483 | |
Kristian Høgsberg | dc0f355 | 2008-12-07 15:22:22 -0500 | [diff] [blame] | 1484 | if (wl_display_add_socket(display, socket_name, sizeof socket_name)) { |
Kristian Høgsberg | 122912c | 2008-12-05 11:13:50 -0500 | [diff] [blame] | 1485 | fprintf(stderr, "failed to add socket: %m\n"); |
| 1486 | exit(EXIT_FAILURE); |
| 1487 | } |
| 1488 | |
| 1489 | wl_display_run(display); |
| 1490 | |
| 1491 | return 0; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1492 | } |