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