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