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