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