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