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