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