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