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