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