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