2 * Copyright © 2008 Kristian Høgsberg
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.
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.
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.
27 #include <sys/ioctl.h>
33 #include <gdk-pixbuf/gdk-pixbuf.h>
38 #include <linux/input.h>
40 #include <xf86drmMode.h>
45 #define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
52 #include "wayland-protocol.h"
53 #include "cairo-util.h"
54 #include "wayland-system-compositor.h"
56 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
59 struct wl_object base;
63 struct wl_object base;
65 struct egl_compositor *ec;
66 struct egl_surface *background;
68 int32_t x, y, width, height, stride;
70 struct drm_mode_modeinfo *mode;
73 uint32_t connector_id;
76 struct wlsc_input_device {
77 struct wl_object base;
79 struct egl_compositor *ec;
80 struct egl_surface *pointer_surface;
84 struct egl_surface *grab_surface;
85 struct egl_surface *focus_surface;
88 struct egl_compositor {
89 struct wl_compositor base;
90 struct wl_visual argb_visual, premultiplied_argb_visual, rgb_visual;
95 struct wl_display *wl_display;
97 struct wl_list output_list;
98 struct wl_list input_device_list;
99 struct wl_list surface_list;
101 struct wl_event_source *term_signal_source;
103 /* tty handling state */
105 uint32_t vt_active : 1;
107 struct termios terminal_attributes;
108 struct wl_event_source *tty_input_source;
109 struct wl_event_source *enter_vt_source;
110 struct wl_event_source *leave_vt_source;
115 struct wl_event_source *timer_source;
117 int repaint_on_timeout;
118 struct timespec previous_swap;
119 uint32_t current_frame;
123 struct wl_surface base;
124 struct egl_compositor *compositor;
125 struct wl_visual *visual;
133 static const char *option_background = "background.jpg";
135 static const GOptionEntry option_entries[] = {
136 { "background", 'b', 0, G_OPTION_ARG_STRING,
137 &option_background, "Background image" },
142 struct screenshooter {
143 struct wl_object base;
144 struct egl_compositor *ec;
147 struct screenshooter_interface {
148 void (*shoot)(struct wl_client *client, struct screenshooter *shooter);
152 screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
154 struct egl_compositor *ec = shooter->ec;
155 struct wlsc_output *output;
159 GError *error = NULL;
164 output = container_of(ec->output_list.next, struct wlsc_output, link);
165 while (&output->link != &ec->output_list) {
166 snprintf(buffer, sizeof buffer, "wayland-screenshot-%d.png", i++);
167 data = eglReadBuffer(ec->display, output->surface, GL_FRONT_LEFT, &stride);
168 pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
169 8, output->width, output->height, stride,
171 gdk_pixbuf_save(pixbuf, buffer, "png", &error, NULL);
173 output = container_of(output->link.next,
174 struct wlsc_output, link);
178 static const struct wl_message screenshooter_methods[] = {
179 { "shoot", "", NULL }
182 static const struct wl_interface screenshooter_interface = {
184 ARRAY_LENGTH(screenshooter_methods),
185 screenshooter_methods,
188 struct screenshooter_interface screenshooter_implementation = {
192 static struct screenshooter *
193 screenshooter_create(struct egl_compositor *ec)
195 struct screenshooter *shooter;
197 shooter = malloc(sizeof *shooter);
201 shooter->base.interface = &screenshooter_interface;
202 shooter->base.implementation = (void(**)(void)) &screenshooter_implementation;
208 static struct egl_surface *
209 egl_surface_create_from_cairo_surface(struct egl_compositor *ec,
210 cairo_surface_t *surface,
211 int x, int y, int width, int height)
213 struct egl_surface *es;
217 stride = cairo_image_surface_get_stride(surface);
218 data = cairo_image_surface_get_data(surface);
220 es = malloc(sizeof *es);
224 glGenTextures(1, &es->texture);
225 glBindTexture(GL_TEXTURE_2D, es->texture);
226 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
227 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
228 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
229 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
230 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
231 GL_BGRA, GL_UNSIGNED_BYTE, data);
236 es->map.width = width;
237 es->map.height = height;
238 es->surface = EGL_NO_SURFACE;
239 es->visual = &ec->premultiplied_argb_visual;
245 egl_surface_destroy(struct egl_surface *es, struct egl_compositor *ec)
247 glDeleteTextures(1, &es->texture);
248 if (es->surface != EGL_NO_SURFACE)
249 eglDestroySurface(ec->display, es->surface);
254 pointer_path(cairo_t *cr, int x, int y)
256 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
257 const int width = 16, height = 16;
259 cairo_move_to(cr, x, y);
260 cairo_line_to(cr, x + tx, y + ty);
261 cairo_line_to(cr, x + dx, y + dy);
262 cairo_line_to(cr, x + width - end, y + height);
263 cairo_line_to(cr, x + width, y + height - end);
264 cairo_line_to(cr, x + dy, y + dx);
265 cairo_line_to(cr, x + ty, y + tx);
266 cairo_close_path(cr);
269 static struct egl_surface *
270 pointer_create(struct egl_compositor *ec, int x, int y, int width, int height)
272 struct egl_surface *es;
273 const int hotspot_x = 16, hotspot_y = 16;
274 cairo_surface_t *surface;
277 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
280 cr = cairo_create(surface);
281 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
282 cairo_set_line_width(cr, 2);
283 cairo_set_source_rgb(cr, 0, 0, 0);
284 cairo_stroke_preserve(cr);
286 blur_surface(surface, width);
288 pointer_path(cr, hotspot_x, hotspot_y);
289 cairo_stroke_preserve(cr);
290 cairo_set_source_rgb(cr, 1, 1, 1);
294 es = egl_surface_create_from_cairo_surface(ec,
300 cairo_surface_destroy(surface);
305 static struct egl_surface *
306 background_create(struct wlsc_output *output, const char *filename)
308 struct egl_surface *background;
310 GError *error = NULL;
314 background = malloc(sizeof *background);
315 if (background == NULL)
320 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
329 data = gdk_pixbuf_get_pixels(pixbuf);
331 glGenTextures(1, &background->texture);
332 glBindTexture(GL_TEXTURE_2D, background->texture);
333 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
334 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
335 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
336 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
338 if (gdk_pixbuf_get_has_alpha(pixbuf))
343 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
344 output->width, output->height, 0,
345 format, GL_UNSIGNED_BYTE, data);
347 background->compositor = output->ec;
348 background->map.x = output->x;
349 background->map.y = output->y;
350 background->map.width = output->width;
351 background->map.height = output->height;
352 background->surface = EGL_NO_SURFACE;
353 background->visual = &output->ec->rgb_visual;
359 draw_surface(struct egl_surface *es)
361 struct egl_compositor *ec = es->compositor;
363 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
364 GLuint indices[4] = { 0, 1, 2, 3 };
366 vertices[0] = es->map.x;
367 vertices[1] = es->map.y;
370 vertices[3] = es->map.x;
371 vertices[4] = es->map.y + es->map.height;
374 vertices[6] = es->map.x + es->map.width;
375 vertices[7] = es->map.y;
378 vertices[9] = es->map.x + es->map.width;
379 vertices[10] = es->map.y + es->map.height;
382 if (es->visual == &ec->argb_visual) {
383 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
385 } else if (es->visual == &ec->premultiplied_argb_visual) {
386 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
392 glBindTexture(GL_TEXTURE_2D, es->texture);
393 glEnable(GL_TEXTURE_2D);
394 glEnableClientState(GL_VERTEX_ARRAY);
395 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
396 glVertexPointer(3, GL_INT, 0, vertices);
397 glTexCoordPointer(2, GL_INT, 0, tex_coords);
398 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
402 repaint_output(struct wlsc_output *output)
404 struct egl_compositor *ec = output->ec;
405 struct egl_surface *es;
406 struct wlsc_input_device *eid;
408 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
409 fprintf(stderr, "failed to make context current\n");
413 glViewport(0, 0, output->width, output->height);
414 glMatrixMode(GL_PROJECTION);
416 glOrtho(0, output->width, output->height, 0, 0, 1000.0);
417 glMatrixMode(GL_MODELVIEW);
418 glClearColor(0, 0, 0.2, 1);
420 if (output->background)
421 draw_surface(output->background);
423 glClear(GL_COLOR_BUFFER_BIT);
425 es = container_of(ec->surface_list.next,
426 struct egl_surface, link);
427 while (&es->link != &ec->surface_list) {
430 es = container_of(es->link.next,
431 struct egl_surface, link);
434 eid = container_of(ec->input_device_list.next,
435 struct wlsc_input_device, link);
436 while (&eid->link != &ec->input_device_list) {
437 draw_surface(eid->pointer_surface);
439 eid = container_of(eid->link.next,
440 struct wlsc_input_device, link);
443 eglSwapBuffers(ec->display, output->surface);
449 struct egl_compositor *ec = data;
450 struct wlsc_output *output;
454 if (!ec->repaint_needed) {
455 ec->repaint_on_timeout = 0;
459 output = container_of(ec->output_list.next, struct wlsc_output, link);
460 while (&output->link != &ec->output_list) {
461 repaint_output(output);
462 output = container_of(output->link.next,
463 struct wlsc_output, link);
466 ec->repaint_needed = 0;
468 clock_gettime(CLOCK_MONOTONIC, &ts);
469 msecs = ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000);
470 wl_display_post_frame(ec->wl_display, &ec->base,
471 ec->current_frame, msecs);
474 wl_event_source_timer_update(ec->timer_source, 10);
475 ec->repaint_on_timeout = 1;
479 schedule_repaint(struct egl_compositor *ec)
481 struct wl_event_loop *loop;
483 ec->repaint_needed = 1;
484 if (!ec->repaint_on_timeout) {
485 loop = wl_display_get_event_loop(ec->wl_display);
486 wl_event_loop_add_idle(loop, repaint, ec);
491 surface_destroy(struct wl_client *client,
492 struct wl_surface *surface)
494 struct egl_surface *es = (struct egl_surface *) surface;
495 struct egl_compositor *ec = es->compositor;
497 wl_list_remove(&es->link);
498 egl_surface_destroy(es, ec);
500 schedule_repaint(ec);
504 surface_attach(struct wl_client *client,
505 struct wl_surface *surface, uint32_t name,
506 uint32_t width, uint32_t height, uint32_t stride,
507 struct wl_object *visual)
509 struct egl_surface *es = (struct egl_surface *) surface;
510 struct egl_compositor *ec = es->compositor;
512 if (es->surface != EGL_NO_SURFACE)
513 eglDestroySurface(ec->display, es->surface);
517 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
518 name, width, height, stride, NULL);
519 if (visual == &ec->argb_visual.base)
520 es->visual = &ec->argb_visual;
521 else if (visual == &ec->premultiplied_argb_visual.base)
522 es->visual = &ec->premultiplied_argb_visual;
523 else if (visual == &ec->rgb_visual.base)
524 es->visual = &ec->rgb_visual;
526 /* FIXME: Smack client with an exception event */;
528 glBindTexture(GL_TEXTURE_2D, es->texture);
529 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
530 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
531 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
532 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
533 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
537 surface_map(struct wl_client *client,
538 struct wl_surface *surface,
539 int32_t x, int32_t y, int32_t width, int32_t height)
541 struct egl_surface *es = (struct egl_surface *) surface;
545 es->map.width = width;
546 es->map.height = height;
550 surface_copy(struct wl_client *client,
551 struct wl_surface *surface,
552 int32_t dst_x, int32_t dst_y,
553 uint32_t name, uint32_t stride,
554 int32_t x, int32_t y, int32_t width, int32_t height)
556 struct egl_surface *es = (struct egl_surface *) surface;
557 struct egl_compositor *ec = es->compositor;
560 /* FIXME: glCopyPixels should work, but then we'll have to
561 * call eglMakeCurrent to set up the src and dest surfaces
562 * first. This seems cheaper, but maybe there's a better way
563 * to accomplish this. */
565 src = eglCreateSurfaceForName(ec->display, ec->config,
566 name, x + width, y + height, stride, NULL);
568 eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y,
569 src, GL_FRONT_LEFT, x, y, width, height);
570 eglDestroySurface(ec->display, src);
574 surface_damage(struct wl_client *client,
575 struct wl_surface *surface,
576 int32_t x, int32_t y, int32_t width, int32_t height)
578 /* FIXME: This need to take a damage region, of course. */
581 const static struct wl_surface_interface surface_interface = {
590 compositor_create_surface(struct wl_client *client,
591 struct wl_compositor *compositor, uint32_t id)
593 struct egl_compositor *ec = (struct egl_compositor *) compositor;
594 struct egl_surface *es;
596 es = malloc(sizeof *es);
598 /* FIXME: Send OOM event. */
602 es->surface = EGL_NO_SURFACE;
603 wl_list_insert(ec->surface_list.prev, &es->link);
604 glGenTextures(1, &es->texture);
605 wl_client_add_surface(client, &es->base,
606 &surface_interface, id);
610 compositor_commit(struct wl_client *client,
611 struct wl_compositor *compositor, uint32_t key)
613 struct egl_compositor *ec = (struct egl_compositor *) compositor;
615 schedule_repaint(ec);
616 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
619 const static struct wl_compositor_interface compositor_interface = {
620 compositor_create_surface,
624 static struct egl_surface *
625 pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
627 struct egl_compositor *ec = device->ec;
628 struct egl_surface *es;
630 if (device->grab > 0)
631 return device->grab_surface;
633 es = container_of(ec->surface_list.prev,
634 struct egl_surface, link);
635 while (&es->link != &ec->surface_list) {
636 if (es->map.x <= device->x &&
637 device->x < es->map.x + es->map.width &&
638 es->map.y <= device->y &&
639 device->y < es->map.y + es->map.height)
642 es = container_of(es->link.prev,
643 struct egl_surface, link);
645 /* Transform to surface coordinates. */
646 *sx = (device->x - es->map.x) * es->width / es->map.width;
647 *sy = (device->y - es->map.y) * es->height / es->map.height;
654 notify_motion(struct wlsc_input_device *device, int x, int y)
656 struct egl_surface *es;
657 struct egl_compositor *ec = device->ec;
658 struct wlsc_output *output;
659 const int hotspot_x = 16, hotspot_y = 16;
665 /* FIXME: We need some multi head love here. */
666 output = container_of(ec->output_list.next, struct wlsc_output, link);
671 if (x >= output->x + output->width)
672 x = output->x + output->width - 1;
673 if (y >= output->y + output->height)
674 y = output->y + output->height - 1;
678 es = pick_surface(device, &sx, &sy);
680 wl_surface_post_event(&es->base, &device->base,
681 WL_INPUT_MOTION, x, y, sx, sy);
683 device->pointer_surface->map.x = x - hotspot_x;
684 device->pointer_surface->map.y = y - hotspot_y;
686 schedule_repaint(device->ec);
690 notify_button(struct wlsc_input_device *device,
691 int32_t button, int32_t state)
693 struct egl_surface *es;
694 struct egl_compositor *ec = device->ec;
700 es = pick_surface(device, &sx, &sy);
702 wl_list_remove(&es->link);
703 wl_list_insert(device->ec->surface_list.prev, &es->link);
706 /* FIXME: We need callbacks when the surfaces
707 * we reference here go away. */
709 device->grab_surface = es;
710 device->focus_surface = es;
715 /* FIXME: Swallow click on raise? */
716 wl_surface_post_event(&es->base, &device->base,
717 WL_INPUT_BUTTON, button, state,
718 device->x, device->y, sx, sy);
720 schedule_repaint(device->ec);
725 notify_key(struct wlsc_input_device *device,
726 uint32_t key, uint32_t state)
728 struct egl_compositor *ec = device->ec;
733 if (device->focus_surface != NULL)
734 wl_surface_post_event(&device->focus_surface->base,
736 WL_INPUT_KEY, key, state);
739 struct evdev_input_device *
740 evdev_input_device_create(struct wlsc_input_device *device,
741 struct wl_display *display, const char *path);
743 static struct wlsc_input_device *
744 create_input_device(struct egl_compositor *ec)
746 struct wlsc_input_device *device;
748 device = malloc(sizeof *device);
752 memset(device, 0, sizeof *device);
753 device->base.interface = &wl_input_device_interface;
754 device->base.implementation = NULL;
755 wl_display_add_object(ec->wl_display, &device->base);
756 wl_display_add_global(ec->wl_display, &device->base, NULL);
761 wl_list_insert(ec->input_device_list.prev, &device->link);
767 wlsc_device_get_position(struct wlsc_input_device *device, int32_t *x, int32_t *y)
774 post_output_geometry(struct wl_client *client, struct wl_object *global)
776 struct wlsc_output *output =
777 container_of(global, struct wlsc_output, base);
779 wl_client_post_event(client, global,
781 output->width, output->height);
785 get_udev_property(struct udev_device *device, const char *name)
787 struct udev_list_entry *entry;
789 udev_list_entry_foreach(entry, udev_device_get_properties_list_entry(device))
790 if (strcmp(udev_list_entry_get_name(entry), name) == 0)
791 return udev_list_entry_get_value(entry);
797 init_egl(struct egl_compositor *ec, struct udev_device *device)
799 static const EGLint config_attribs[] = {
802 EGL_CONFIG_CAVEAT, EGL_NONE,
808 ec->display = eglCreateDisplayNative(device);
809 if (ec->display == NULL) {
810 fprintf(stderr, "failed to create display\n");
814 if (!eglInitialize(ec->display, &major, &minor)) {
815 fprintf(stderr, "failed to initialize display\n");
819 if (!eglChooseConfig(ec->display, config_attribs, &ec->config, 1, NULL))
822 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
823 if (ec->context == NULL) {
824 fprintf(stderr, "failed to create context\n");
831 create_output(struct egl_compositor *ec, struct udev_device *device)
833 const static EGLint surface_attribs[] = {
834 EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
838 drmModeConnector *connector;
839 drmModeRes *resources;
840 drmModeEncoder *encoder;
841 struct drm_mode_modeinfo *mode;
842 struct drm_i915_gem_create create;
843 struct drm_gem_flink flink;
844 struct wlsc_output *output;
847 if (ec->display == NULL && init_egl(ec, device) < 0) {
848 fprintf(stderr, "failed to initialize egl\n");
852 output = malloc(sizeof *output);
856 fd = eglGetDisplayFD(ec->display);
857 resources = drmModeGetResources(fd);
859 fprintf(stderr, "drmModeGetResources failed\n");
863 for (i = 0; i < resources->count_connectors; i++) {
864 connector = drmModeGetConnector(fd, resources->connectors[i]);
865 if (connector == NULL)
868 if (connector->connection == DRM_MODE_CONNECTED &&
869 connector->count_modes > 0)
872 drmModeFreeConnector(connector);
875 if (i == resources->count_connectors) {
876 fprintf(stderr, "No currently active connector found.\n");
880 mode = &connector->modes[0];
882 for (i = 0; i < resources->count_encoders; i++) {
883 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
888 if (encoder->encoder_id == connector->encoder_id)
891 drmModeFreeEncoder(encoder);
894 /* Mode size at 32 bpp */
895 create.size = mode->hdisplay * mode->vdisplay * 4;
896 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
897 fprintf(stderr, "gem create failed: %m\n");
901 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
902 32, 32, mode->hdisplay * 4, create.handle, &output->fb_id);
904 fprintf(stderr, "failed to add fb: %m\n");
908 ret = drmModeSetCrtc(fd, encoder->crtc_id, output->fb_id, 0, 0,
909 &connector->connector_id, 1, mode);
911 fprintf(stderr, "failed to set mode: %m\n");
915 flink.handle = create.handle;
916 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
917 fprintf(stderr, "gem flink failed: %m\n");
922 output->crtc_id = encoder->crtc_id;
923 output->connector_id = connector->connector_id;
927 output->width = mode->hdisplay;
928 output->height = mode->vdisplay;
929 output->stride = mode->hdisplay * 4;
931 output->surface = eglCreateSurfaceForName(ec->display, ec->config,
933 output->width, output->height,
936 if (output->surface == NULL) {
937 fprintf(stderr, "failed to create surface\n");
941 output->base.interface = &wl_output_interface;
942 wl_display_add_object(ec->wl_display, &output->base);
943 wl_display_add_global(ec->wl_display, &output->base, post_output_geometry);
944 wl_list_insert(ec->output_list.prev, &output->link);
946 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
947 fprintf(stderr, "failed to make context current\n");
951 output->background = background_create(output, option_background);
956 static const struct wl_interface visual_interface = {
961 add_visuals(struct egl_compositor *ec)
963 ec->argb_visual.base.interface = &visual_interface;
964 ec->argb_visual.base.implementation = NULL;
965 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
966 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
968 ec->premultiplied_argb_visual.base.interface = &visual_interface;
969 ec->premultiplied_argb_visual.base.implementation = NULL;
970 wl_display_add_object(ec->wl_display,
971 &ec->premultiplied_argb_visual.base);
972 wl_display_add_global(ec->wl_display,
973 &ec->premultiplied_argb_visual.base, NULL);
975 ec->rgb_visual.base.interface = &visual_interface;
976 ec->rgb_visual.base.implementation = NULL;
977 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
978 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
981 static void on_enter_vt(int signal_number, void *data)
983 struct egl_compositor *ec = data;
984 struct wlsc_output *output;
987 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
988 ec->vt_active = TRUE;
990 fd = eglGetDisplayFD(ec->display);
991 output = container_of(ec->output_list.next, struct wlsc_output, link);
992 while (&output->link != &ec->output_list) {
993 ret = drmModeSetCrtc(fd, output->crtc_id, output->fb_id, 0, 0,
994 &output->connector_id, 1, output->mode);
996 fprintf(stderr, "failed to set mode for connector %d: %m\n",
997 output->connector_id);
999 output = container_of(output->link.next,
1000 struct wlsc_output, link);
1004 static void on_leave_vt(int signal_number, void *data)
1006 struct egl_compositor *ec = data;
1008 ioctl (ec->tty_fd, VT_RELDISP, 1);
1009 ec->vt_active = FALSE;
1013 on_tty_input(int fd, uint32_t mask, void *data)
1015 struct egl_compositor *ec = data;
1017 /* Ignore input to tty. We get keyboard events from evdev
1019 tcflush(ec->tty_fd, TCIFLUSH);
1022 static void on_term_signal(int signal_number, void *data)
1024 struct egl_compositor *ec = data;
1026 if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0)
1027 fprintf(stderr, "could not restore terminal to canonical mode\n");
1032 static int setup_tty(struct egl_compositor *ec, struct wl_event_loop *loop)
1034 struct termios raw_attributes;
1035 struct vt_mode mode = { 0 };
1037 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
1038 if (ec->tty_fd <= 0) {
1039 fprintf(stderr, "failed to open active tty: %m\n");
1043 if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
1044 fprintf(stderr, "could not get terminal attributes: %m\n");
1048 /* Ignore control characters and disable echo */
1049 raw_attributes = ec->terminal_attributes;
1050 cfmakeraw(&raw_attributes);
1052 /* Fix up line endings to be normal (cfmakeraw hoses them) */
1053 raw_attributes.c_oflag |= OPOST | OCRNL;
1055 if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
1056 fprintf(stderr, "could not put terminal into raw mode: %m\n");
1058 ec->term_signal_source =
1059 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
1061 ec->tty_input_source =
1062 wl_event_loop_add_fd(loop, ec->tty_fd,
1063 WL_EVENT_READABLE, on_tty_input, ec);
1065 ec->vt_active = TRUE;
1066 mode.mode = VT_PROCESS;
1067 mode.relsig = SIGUSR1;
1068 mode.acqsig = SIGUSR2;
1069 if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) {
1070 fprintf(stderr, "failed to take control of vt handling\n");
1073 ec->leave_vt_source =
1074 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec);
1075 ec->enter_vt_source =
1076 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec);
1082 init_libudev(struct egl_compositor *ec)
1084 struct udev_enumerate *e;
1085 struct udev_list_entry *entry;
1086 struct udev_device *device;
1087 const char *path, *seat;
1088 struct wlsc_input_device *input_device;
1090 /* FIXME: Newer (version 135+) udev has two new features that
1091 * make all this much easier: 1) we can enumerate by a
1092 * specific property. This lets us directly iterate through
1093 * the devices we care about. 2) We can attach properties to
1094 * sysfs nodes without a device file, which lets us configure
1095 * which connectors belong to a seat instead of tagging the
1096 * overall drm node. I don't want to update my system udev,
1097 * so I'm going to stick with this until the new version is in
1100 ec->udev = udev_new();
1101 if (ec->udev == NULL) {
1102 fprintf(stderr, "failed to initialize udev context\n");
1106 input_device = create_input_device(ec);
1108 e = udev_enumerate_new(ec->udev);
1109 udev_enumerate_scan_devices(e);
1110 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1111 path = udev_list_entry_get_name(entry);
1112 device = udev_device_new_from_syspath(ec->udev, path);
1114 /* FIXME: Should the property namespace be CK for console kit? */
1115 seat = get_udev_property(device, "WAYLAND_SEAT");
1116 if (!seat || strcmp(seat, "1") != 0)
1119 if (strcmp(udev_device_get_subsystem(device), "input") == 0) {
1120 evdev_input_device_create(input_device, ec->wl_display,
1121 udev_device_get_devnode(device));
1122 } else if (strcmp(udev_device_get_subsystem(device), "drm") == 0) {
1123 if (create_output(ec, device) < 0) {
1124 fprintf(stderr, "failed to create output for %s\n", path);
1129 udev_enumerate_unref(e);
1131 /* Create the pointer surface now that we have a current EGL context. */
1132 input_device->pointer_surface =
1133 pointer_create(ec, input_device->x, input_device->y, 64, 64);
1138 static struct egl_compositor *
1139 egl_compositor_create(struct wl_display *display)
1141 struct egl_compositor *ec;
1142 struct screenshooter *shooter;
1143 struct wl_event_loop *loop;
1145 ec = malloc(sizeof *ec);
1149 memset(ec, 0, sizeof *ec);
1150 ec->wl_display = display;
1152 wl_display_set_compositor(display, &ec->base, &compositor_interface);
1156 wl_list_init(&ec->surface_list);
1157 wl_list_init(&ec->input_device_list);
1158 wl_list_init(&ec->output_list);
1159 if (init_libudev(ec) < 0) {
1160 fprintf(stderr, "failed to initialize devices\n");
1164 shooter = screenshooter_create(ec);
1165 wl_display_add_object(display, &shooter->base);
1166 wl_display_add_global(display, &shooter->base, NULL);
1168 loop = wl_display_get_event_loop(ec->wl_display);
1170 setup_tty(ec, loop);
1172 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
1173 schedule_repaint(ec);
1178 /* The plan here is to generate a random anonymous socket name and
1179 * advertise that through a service on the session dbus.
1181 static const char socket_name[] = "\0wayland";
1183 int main(int argc, char *argv[])
1185 struct wl_display *display;
1186 struct egl_compositor *ec;
1187 GError *error = NULL;
1188 GOptionContext *context;
1190 context = g_option_context_new(NULL);
1191 g_option_context_add_main_entries(context, option_entries, "Wayland");
1192 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1193 fprintf(stderr, "option parsing failed: %s\n", error->message);
1197 display = wl_display_create();
1199 ec = egl_compositor_create(display);
1201 fprintf(stderr, "failed to create compositor\n");
1205 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
1206 fprintf(stderr, "failed to add socket: %m\n");
1210 wl_display_run(display);