2008-12-02 23:15:01 +03:00
|
|
|
/*
|
|
|
|
* Copyright © 2008 Kristian Høgsberg
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, distribute, and sell this software and its
|
|
|
|
* documentation for any purpose is hereby granted without fee, provided that
|
|
|
|
* the above copyright notice appear in all copies and that both that copyright
|
|
|
|
* notice and this permission notice appear in supporting documentation, and
|
|
|
|
* that the name of the copyright holders not be used in advertising or
|
|
|
|
* publicity pertaining to distribution of the software without specific,
|
|
|
|
* written prior permission. The copyright holders make no representations
|
|
|
|
* about the suitability of this software for any purpose. It is provided "as
|
|
|
|
* is" without express or implied warranty.
|
|
|
|
*
|
|
|
|
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
|
|
|
|
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
|
|
|
|
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
|
|
|
|
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
|
|
|
|
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
|
|
|
|
* OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2008-11-03 02:51:48 +03:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <cairo.h>
|
2008-11-07 23:54:48 +03:00
|
|
|
#include <glib.h>
|
2010-06-09 04:34:11 +04:00
|
|
|
#include <glib-object.h>
|
2010-06-08 22:08:30 +04:00
|
|
|
|
|
|
|
#define EGL_EGLEXT_PROTOTYPES 1
|
|
|
|
#define GL_GLEXT_PROTOTYPES 1
|
|
|
|
#include <GL/gl.h>
|
|
|
|
#include <EGL/egl.h>
|
|
|
|
#include <EGL/eglext.h>
|
|
|
|
#include <cairo-gl.h>
|
2008-11-03 02:51:48 +03:00
|
|
|
|
2008-12-12 07:18:45 +03:00
|
|
|
#include <linux/input.h>
|
2009-02-24 06:30:29 +03:00
|
|
|
#include "wayland-util.h"
|
2008-11-03 02:51:48 +03:00
|
|
|
#include "wayland-client.h"
|
2008-11-07 23:54:48 +03:00
|
|
|
#include "wayland-glib.h"
|
2010-06-05 18:17:55 +04:00
|
|
|
#include "../cairo-util.h"
|
2008-11-09 06:46:30 +03:00
|
|
|
|
2008-12-08 03:59:11 +03:00
|
|
|
#include "window.h"
|
2008-11-03 23:31:30 +03:00
|
|
|
|
2009-01-27 07:42:46 +03:00
|
|
|
struct display {
|
2008-11-26 06:40:39 +03:00
|
|
|
struct wl_display *display;
|
2008-12-16 04:35:24 +03:00
|
|
|
struct wl_compositor *compositor;
|
2009-01-27 07:42:46 +03:00
|
|
|
struct wl_output *output;
|
|
|
|
struct wl_input_device *input_device;
|
|
|
|
struct rectangle screen_allocation;
|
2010-06-08 22:08:30 +04:00
|
|
|
EGLDisplay dpy;
|
|
|
|
EGLContext ctx;
|
2010-01-31 05:01:26 +03:00
|
|
|
cairo_device_t *device;
|
2009-01-27 07:42:46 +03:00
|
|
|
int fd;
|
2010-06-08 22:59:44 +04:00
|
|
|
GMainLoop *loop;
|
|
|
|
GSource *source;
|
2010-06-09 04:34:11 +04:00
|
|
|
struct wl_list window_list;
|
|
|
|
char *device_name;
|
2009-01-27 07:42:46 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct window {
|
|
|
|
struct display *display;
|
2008-11-03 02:51:48 +03:00
|
|
|
struct wl_surface *surface;
|
2008-12-08 03:59:11 +03:00
|
|
|
const char *title;
|
2009-01-27 07:42:46 +03:00
|
|
|
struct rectangle allocation, saved_allocation;
|
2008-12-08 03:59:11 +03:00
|
|
|
int minimum_width, minimum_height;
|
2008-11-26 06:40:39 +03:00
|
|
|
int margin;
|
2008-12-12 20:00:02 +03:00
|
|
|
int drag_x, drag_y;
|
2008-11-03 02:51:48 +03:00
|
|
|
int state;
|
2008-12-22 20:14:50 +03:00
|
|
|
int fullscreen;
|
2010-06-08 22:08:30 +04:00
|
|
|
int decoration;
|
2008-12-30 19:03:33 +03:00
|
|
|
struct wl_input_device *grab_device;
|
2009-02-23 07:01:35 +03:00
|
|
|
struct wl_input_device *keyboard_device;
|
2008-11-03 02:51:48 +03:00
|
|
|
uint32_t name;
|
2009-02-21 22:31:09 +03:00
|
|
|
uint32_t modifiers;
|
2008-11-08 23:06:01 +03:00
|
|
|
|
2010-06-08 22:08:30 +04:00
|
|
|
EGLImageKHR *image;
|
2009-12-16 22:43:37 +03:00
|
|
|
cairo_surface_t *cairo_surface, *pending_surface;
|
2010-01-25 02:10:15 +03:00
|
|
|
int new_surface;
|
2008-12-08 03:59:11 +03:00
|
|
|
|
|
|
|
window_resize_handler_t resize_handler;
|
2008-12-08 08:01:36 +03:00
|
|
|
window_key_handler_t key_handler;
|
2009-02-23 07:01:35 +03:00
|
|
|
window_keyboard_focus_handler_t keyboard_focus_handler;
|
2010-06-09 04:34:11 +04:00
|
|
|
window_acknowledge_handler_t acknowledge_handler;
|
2010-06-08 22:08:30 +04:00
|
|
|
window_frame_handler_t frame_handler;
|
|
|
|
|
2008-12-08 03:59:11 +03:00
|
|
|
void *user_data;
|
2010-06-09 04:34:11 +04:00
|
|
|
struct wl_list link;
|
2008-11-03 02:51:48 +03:00
|
|
|
};
|
|
|
|
|
2008-11-09 02:53:37 +03:00
|
|
|
static void
|
|
|
|
rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
|
|
|
|
{
|
|
|
|
cairo_move_to(cr, x0, y0 + radius);
|
|
|
|
cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
|
|
|
|
cairo_line_to(cr, x1 - radius, y0);
|
|
|
|
cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
|
|
|
|
cairo_line_to(cr, x1, y1 - radius);
|
|
|
|
cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
|
|
|
|
cairo_line_to(cr, x0 + radius, y1);
|
|
|
|
cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
|
|
|
|
cairo_close_path(cr);
|
|
|
|
}
|
|
|
|
|
2010-06-08 22:08:30 +04:00
|
|
|
static const cairo_user_data_key_t surface_data_key;
|
|
|
|
struct surface_data {
|
|
|
|
EGLImageKHR image;
|
|
|
|
GLuint texture;
|
|
|
|
EGLDisplay dpy;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
surface_data_destroy(void *p)
|
|
|
|
{
|
|
|
|
struct surface_data *data = p;
|
|
|
|
|
|
|
|
glDeleteTextures(1, &data->texture);
|
|
|
|
eglDestroyImageKHR(data->dpy, data->image);
|
|
|
|
}
|
|
|
|
|
|
|
|
cairo_surface_t *
|
|
|
|
window_create_surface(struct window *window,
|
|
|
|
struct rectangle *rectangle)
|
|
|
|
{
|
|
|
|
struct surface_data *data;
|
|
|
|
EGLDisplay dpy = window->display->dpy;
|
|
|
|
cairo_surface_t *surface;
|
|
|
|
|
|
|
|
EGLint image_attribs[] = {
|
|
|
|
EGL_WIDTH, 0,
|
|
|
|
EGL_HEIGHT, 0,
|
|
|
|
EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA,
|
|
|
|
EGL_IMAGE_USE_MESA, EGL_IMAGE_USE_SCANOUT_MESA,
|
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
data = malloc(sizeof *data);
|
|
|
|
image_attribs[1] = rectangle->width;
|
|
|
|
image_attribs[3] = rectangle->height;
|
|
|
|
data->image = eglCreateDRMImageMESA(dpy, image_attribs);
|
|
|
|
glGenTextures(1, &data->texture);
|
|
|
|
data->dpy = dpy;
|
|
|
|
glBindTexture(GL_TEXTURE_2D, data->texture);
|
|
|
|
glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
|
|
|
|
|
|
|
|
surface = cairo_gl_surface_create_for_texture(window->display->device,
|
|
|
|
CAIRO_CONTENT_COLOR_ALPHA,
|
|
|
|
data->texture,
|
|
|
|
rectangle->width,
|
|
|
|
rectangle->height);
|
|
|
|
|
|
|
|
cairo_surface_set_user_data (surface, &surface_data_key,
|
|
|
|
data, surface_data_destroy);
|
|
|
|
|
|
|
|
return surface;
|
|
|
|
}
|
|
|
|
|
2009-12-16 22:43:37 +03:00
|
|
|
static void
|
|
|
|
window_attach_surface(struct window *window)
|
|
|
|
{
|
|
|
|
struct wl_visual *visual;
|
2010-06-08 22:08:30 +04:00
|
|
|
struct surface_data *data;
|
|
|
|
EGLint name, stride;
|
2009-12-16 22:43:37 +03:00
|
|
|
|
2009-12-21 18:04:53 +03:00
|
|
|
if (window->pending_surface != NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
window->pending_surface =
|
|
|
|
cairo_surface_reference(window->cairo_surface);
|
|
|
|
|
2010-06-08 22:08:30 +04:00
|
|
|
data = cairo_surface_get_user_data (window->cairo_surface, &surface_data_key);
|
|
|
|
eglExportDRMImageMESA(window->display->dpy, data->image, &name, NULL, &stride);
|
|
|
|
|
2009-12-16 22:43:37 +03:00
|
|
|
visual = wl_display_get_premultiplied_argb_visual(window->display->display);
|
|
|
|
wl_surface_attach(window->surface,
|
2010-06-08 22:08:30 +04:00
|
|
|
name,
|
2009-12-16 22:43:37 +03:00
|
|
|
window->allocation.width,
|
|
|
|
window->allocation.height,
|
2010-06-08 22:08:30 +04:00
|
|
|
stride,
|
2009-12-16 22:43:37 +03:00
|
|
|
visual);
|
|
|
|
|
|
|
|
wl_surface_map(window->surface,
|
|
|
|
window->allocation.x - window->margin,
|
|
|
|
window->allocation.y - window->margin,
|
|
|
|
window->allocation.width,
|
|
|
|
window->allocation.height);
|
|
|
|
}
|
|
|
|
|
2010-01-25 02:10:15 +03:00
|
|
|
void
|
|
|
|
window_commit(struct window *window, uint32_t key)
|
|
|
|
{
|
|
|
|
if (window->new_surface) {
|
|
|
|
window_attach_surface(window);
|
|
|
|
window->new_surface = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
wl_compositor_commit(window->display->compositor, key);
|
|
|
|
}
|
|
|
|
|
2008-12-22 20:14:50 +03:00
|
|
|
static void
|
|
|
|
window_draw_decorations(struct window *window)
|
2008-11-03 02:51:48 +03:00
|
|
|
{
|
|
|
|
cairo_t *cr;
|
2008-11-26 06:40:39 +03:00
|
|
|
int border = 2, radius = 5;
|
2008-11-03 14:59:52 +03:00
|
|
|
cairo_text_extents_t extents;
|
2008-11-09 02:53:37 +03:00
|
|
|
cairo_pattern_t *gradient, *outline, *bright, *dim;
|
2008-12-22 20:14:50 +03:00
|
|
|
int width, height;
|
2009-09-21 21:43:46 +04:00
|
|
|
int shadow_dx = 4, shadow_dy = 4;
|
2008-11-03 02:51:48 +03:00
|
|
|
|
2009-01-15 19:37:43 +03:00
|
|
|
window->cairo_surface =
|
2010-06-08 22:08:30 +04:00
|
|
|
window_create_surface(window, &window->allocation);
|
2008-11-03 02:51:48 +03:00
|
|
|
|
2008-11-09 02:53:37 +03:00
|
|
|
outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
|
2008-11-19 08:49:39 +03:00
|
|
|
bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
|
2008-11-09 02:53:37 +03:00
|
|
|
dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
|
|
|
|
|
2009-01-15 19:37:43 +03:00
|
|
|
cr = cairo_create(window->cairo_surface);
|
2008-11-09 06:46:30 +03:00
|
|
|
|
2008-12-22 20:14:50 +03:00
|
|
|
width = window->allocation.width - window->margin * 2;
|
|
|
|
height = window->allocation.height - window->margin * 2;
|
|
|
|
|
2009-09-21 21:43:46 +04:00
|
|
|
cairo_translate(cr, window->margin + shadow_dx,
|
|
|
|
window->margin + shadow_dy);
|
2008-11-09 06:46:30 +03:00
|
|
|
cairo_set_line_width (cr, border);
|
2008-11-19 08:49:39 +03:00
|
|
|
cairo_set_source_rgba(cr, 0, 0, 0, 0.7);
|
2009-09-21 21:43:46 +04:00
|
|
|
rounded_rect(cr, -1, -1, width + 1, height + 1, radius);
|
2008-11-09 06:46:30 +03:00
|
|
|
cairo_fill(cr);
|
2009-01-15 19:37:43 +03:00
|
|
|
|
2010-06-05 18:17:55 +04:00
|
|
|
#define SLOW_BUT_PWETTY_not_right_now
|
2009-01-15 19:37:43 +03:00
|
|
|
#ifdef SLOW_BUT_PWETTY
|
|
|
|
/* FIXME: Aw, pretty drop shadows now have to fallback to sw.
|
|
|
|
* Ideally we should have convolution filters in cairo, but we
|
|
|
|
* can also fallback to compositing the shadow image a bunch
|
|
|
|
* of times according to the blur kernel. */
|
|
|
|
{
|
|
|
|
cairo_surface_t *map;
|
|
|
|
|
|
|
|
map = cairo_drm_surface_map(window->cairo_surface);
|
2009-03-05 15:49:42 +03:00
|
|
|
blur_surface(map, 32);
|
2009-01-15 19:37:43 +03:00
|
|
|
cairo_drm_surface_unmap(window->cairo_surface, map);
|
|
|
|
}
|
|
|
|
#endif
|
2008-11-09 06:46:30 +03:00
|
|
|
|
2009-09-21 21:43:46 +04:00
|
|
|
cairo_translate(cr, -shadow_dx, -shadow_dy);
|
|
|
|
if (window->keyboard_device) {
|
|
|
|
rounded_rect(cr, 0, 0, width, height, radius);
|
|
|
|
gradient = cairo_pattern_create_linear (0, 0, 0, 100);
|
|
|
|
cairo_pattern_add_color_stop_rgb(gradient, 0, 0.6, 0.6, 0.6);
|
|
|
|
cairo_pattern_add_color_stop_rgb(gradient, 1, 0.8, 0.8, 0.8);
|
|
|
|
cairo_set_source(cr, gradient);
|
|
|
|
cairo_fill(cr);
|
|
|
|
cairo_pattern_destroy(gradient);
|
|
|
|
} else {
|
|
|
|
rounded_rect(cr, 0, 0, width, height, radius);
|
|
|
|
cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
|
|
|
|
cairo_fill(cr);
|
|
|
|
}
|
2008-11-09 02:53:37 +03:00
|
|
|
|
|
|
|
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
|
|
|
|
cairo_move_to(cr, 10, 50);
|
2008-12-22 20:14:50 +03:00
|
|
|
cairo_line_to(cr, width - 10, 50);
|
|
|
|
cairo_line_to(cr, width - 10, height - 10);
|
|
|
|
cairo_line_to(cr, 10, height - 10);
|
2008-11-03 02:51:48 +03:00
|
|
|
cairo_close_path(cr);
|
2008-11-09 02:53:37 +03:00
|
|
|
cairo_set_source(cr, dim);
|
|
|
|
cairo_stroke(cr);
|
|
|
|
|
|
|
|
cairo_move_to(cr, 11, 51);
|
2008-12-22 20:14:50 +03:00
|
|
|
cairo_line_to(cr, width - 10, 51);
|
|
|
|
cairo_line_to(cr, width - 10, height - 10);
|
|
|
|
cairo_line_to(cr, 11, height - 10);
|
2008-11-09 02:53:37 +03:00
|
|
|
cairo_close_path(cr);
|
|
|
|
cairo_set_source(cr, bright);
|
2008-11-03 02:51:48 +03:00
|
|
|
cairo_stroke(cr);
|
2008-11-03 14:59:52 +03:00
|
|
|
|
2008-11-09 02:53:37 +03:00
|
|
|
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
|
|
|
|
cairo_set_font_size(cr, 14);
|
2008-12-08 03:59:11 +03:00
|
|
|
cairo_text_extents(cr, window->title, &extents);
|
2008-12-22 20:14:50 +03:00
|
|
|
cairo_move_to(cr, (width - extents.width) / 2, 10 - extents.y_bearing);
|
2008-11-09 02:53:37 +03:00
|
|
|
cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
|
|
|
|
cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
|
|
|
|
cairo_set_line_width (cr, 4);
|
2008-12-08 03:59:11 +03:00
|
|
|
cairo_text_path(cr, window->title);
|
2009-09-21 21:43:46 +04:00
|
|
|
if (window->keyboard_device) {
|
|
|
|
cairo_set_source_rgb(cr, 0.56, 0.56, 0.56);
|
|
|
|
cairo_stroke_preserve(cr);
|
|
|
|
cairo_set_source_rgb(cr, 1, 1, 1);
|
|
|
|
cairo_fill(cr);
|
|
|
|
} else {
|
|
|
|
cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
|
|
|
|
cairo_fill(cr);
|
|
|
|
}
|
2008-11-03 02:51:48 +03:00
|
|
|
cairo_destroy(cr);
|
2008-12-22 20:14:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
window_draw_fullscreen(struct window *window)
|
|
|
|
{
|
2009-01-15 19:37:43 +03:00
|
|
|
window->cairo_surface =
|
2010-06-08 22:08:30 +04:00
|
|
|
window_create_surface(window, &window->allocation);
|
2008-12-22 20:14:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
window_draw(struct window *window)
|
|
|
|
{
|
2009-12-21 18:04:53 +03:00
|
|
|
if (window->cairo_surface != NULL)
|
|
|
|
cairo_surface_destroy(window->cairo_surface);
|
|
|
|
|
2010-06-08 22:08:30 +04:00
|
|
|
if (window->fullscreen || !window->decoration)
|
2008-12-22 20:14:50 +03:00
|
|
|
window_draw_fullscreen(window);
|
|
|
|
else
|
|
|
|
window_draw_decorations(window);
|
2010-01-25 02:10:15 +03:00
|
|
|
|
|
|
|
window->new_surface = 1;
|
2008-11-26 20:57:31 +03:00
|
|
|
}
|
|
|
|
|
2010-06-08 22:08:30 +04:00
|
|
|
cairo_surface_t *
|
|
|
|
window_get_surface(struct window *window)
|
|
|
|
{
|
|
|
|
return window->cairo_surface;
|
|
|
|
}
|
|
|
|
|
2008-11-03 02:51:48 +03:00
|
|
|
enum window_state {
|
2010-06-15 04:25:06 +04:00
|
|
|
WINDOW_MOVING = 0,
|
|
|
|
WINDOW_RESIZING_TOP = 1,
|
|
|
|
WINDOW_RESIZING_BOTTOM = 2,
|
|
|
|
WINDOW_RESIZING_LEFT = 4,
|
|
|
|
WINDOW_RESIZING_TOP_LEFT = 5,
|
|
|
|
WINDOW_RESIZING_BOTTOM_LEFT = 6,
|
|
|
|
WINDOW_RESIZING_RIGHT = 8,
|
|
|
|
WINDOW_RESIZING_TOP_RIGHT = 9,
|
|
|
|
WINDOW_RESIZING_BOTTOM_RIGHT = 10,
|
|
|
|
WINDOW_RESIZING_MASK = 15,
|
|
|
|
WINDOW_STABLE = 16,
|
2008-11-03 02:51:48 +03:00
|
|
|
};
|
|
|
|
|
2008-11-09 02:53:37 +03:00
|
|
|
static void
|
2008-12-30 19:03:33 +03:00
|
|
|
window_handle_motion(void *data, struct wl_input_device *input_device,
|
|
|
|
int32_t x, int32_t y, int32_t sx, int32_t sy)
|
2008-11-03 02:51:48 +03:00
|
|
|
{
|
|
|
|
struct window *window = data;
|
2008-12-08 20:59:37 +03:00
|
|
|
|
2008-12-30 19:03:33 +03:00
|
|
|
switch (window->state) {
|
|
|
|
case WINDOW_MOVING:
|
|
|
|
if (window->fullscreen)
|
2008-11-03 02:51:48 +03:00
|
|
|
break;
|
2008-12-30 19:03:33 +03:00
|
|
|
if (window->grab_device != input_device)
|
|
|
|
break;
|
|
|
|
window->allocation.x = window->drag_x + x;
|
|
|
|
window->allocation.y = window->drag_y + y;
|
|
|
|
wl_surface_map(window->surface,
|
|
|
|
window->allocation.x - window->margin,
|
|
|
|
window->allocation.y - window->margin,
|
|
|
|
window->allocation.width,
|
|
|
|
window->allocation.height);
|
2009-01-27 07:42:46 +03:00
|
|
|
wl_compositor_commit(window->display->compositor, 1);
|
2008-12-30 19:03:33 +03:00
|
|
|
break;
|
2010-06-15 04:25:06 +04:00
|
|
|
|
|
|
|
case WINDOW_RESIZING_TOP:
|
|
|
|
case WINDOW_RESIZING_BOTTOM:
|
|
|
|
case WINDOW_RESIZING_LEFT:
|
|
|
|
case WINDOW_RESIZING_RIGHT:
|
|
|
|
case WINDOW_RESIZING_TOP_LEFT:
|
|
|
|
case WINDOW_RESIZING_TOP_RIGHT:
|
|
|
|
case WINDOW_RESIZING_BOTTOM_LEFT:
|
|
|
|
case WINDOW_RESIZING_BOTTOM_RIGHT:
|
2008-12-30 19:03:33 +03:00
|
|
|
if (window->fullscreen)
|
|
|
|
break;
|
|
|
|
if (window->grab_device != input_device)
|
|
|
|
break;
|
2010-06-15 04:25:06 +04:00
|
|
|
if (window->state & WINDOW_RESIZING_LEFT) {
|
|
|
|
window->allocation.x = x - window->drag_x + window->saved_allocation.x;
|
|
|
|
window->allocation.width = window->drag_x - x + window->saved_allocation.width;
|
|
|
|
}
|
|
|
|
if (window->state & WINDOW_RESIZING_RIGHT)
|
|
|
|
window->allocation.width = x - window->drag_x + window->saved_allocation.width;
|
|
|
|
if (window->state & WINDOW_RESIZING_TOP) {
|
|
|
|
window->allocation.y = y - window->drag_y + window->saved_allocation.y;
|
|
|
|
window->allocation.height = window->drag_y - y + window->saved_allocation.height;
|
|
|
|
}
|
|
|
|
if (window->state & WINDOW_RESIZING_BOTTOM)
|
|
|
|
window->allocation.height = y - window->drag_y + window->saved_allocation.height;
|
2008-12-30 19:03:33 +03:00
|
|
|
|
|
|
|
if (window->resize_handler)
|
|
|
|
(*window->resize_handler)(window,
|
|
|
|
window->user_data);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void window_handle_button(void *data, struct wl_input_device *input_device,
|
|
|
|
uint32_t button, uint32_t state,
|
|
|
|
int32_t x, int32_t y, int32_t sx, int32_t sy)
|
|
|
|
{
|
|
|
|
struct window *window = data;
|
2010-06-15 04:25:06 +04:00
|
|
|
int grip_size = 8, vlocation, hlocation;
|
|
|
|
|
|
|
|
if (window->margin <= sx && sx < window->margin + grip_size)
|
|
|
|
hlocation = WINDOW_RESIZING_LEFT;
|
|
|
|
else if (sx < window->allocation.width - window->margin - grip_size)
|
|
|
|
hlocation = WINDOW_MOVING;
|
|
|
|
else if (sx < window->allocation.width - window->margin)
|
|
|
|
hlocation = WINDOW_RESIZING_RIGHT;
|
|
|
|
else
|
|
|
|
hlocation = WINDOW_STABLE;
|
|
|
|
|
|
|
|
if (window->margin <= sy && sy < window->margin + grip_size)
|
|
|
|
vlocation = WINDOW_RESIZING_TOP;
|
|
|
|
else if (sy < window->allocation.height - window->margin - grip_size)
|
|
|
|
vlocation = WINDOW_MOVING;
|
|
|
|
else if (sy < window->allocation.height - window->margin)
|
|
|
|
vlocation = WINDOW_RESIZING_BOTTOM;
|
|
|
|
else
|
|
|
|
vlocation = WINDOW_STABLE;
|
2008-11-03 02:51:48 +03:00
|
|
|
|
2008-12-30 19:03:33 +03:00
|
|
|
if (button == BTN_LEFT && state == 1) {
|
2010-06-15 04:25:06 +04:00
|
|
|
switch (hlocation | vlocation) {
|
|
|
|
case WINDOW_MOVING:
|
2008-12-30 19:03:33 +03:00
|
|
|
window->drag_x = window->allocation.x - x;
|
|
|
|
window->drag_y = window->allocation.y - y;
|
|
|
|
window->state = WINDOW_MOVING;
|
|
|
|
window->grab_device = input_device;
|
|
|
|
break;
|
2010-06-15 04:25:06 +04:00
|
|
|
case WINDOW_RESIZING_TOP:
|
|
|
|
case WINDOW_RESIZING_BOTTOM:
|
|
|
|
case WINDOW_RESIZING_LEFT:
|
|
|
|
case WINDOW_RESIZING_RIGHT:
|
|
|
|
case WINDOW_RESIZING_TOP_LEFT:
|
|
|
|
case WINDOW_RESIZING_TOP_RIGHT:
|
|
|
|
case WINDOW_RESIZING_BOTTOM_LEFT:
|
|
|
|
case WINDOW_RESIZING_BOTTOM_RIGHT:
|
|
|
|
window->drag_x = x;
|
|
|
|
window->drag_y = y;
|
|
|
|
window->saved_allocation = window->allocation;
|
|
|
|
window->state = hlocation | vlocation;
|
2008-12-30 19:03:33 +03:00
|
|
|
window->grab_device = input_device;
|
|
|
|
break;
|
|
|
|
default:
|
2008-11-03 02:51:48 +03:00
|
|
|
window->state = WINDOW_STABLE;
|
2008-12-30 19:03:33 +03:00
|
|
|
break;
|
2008-11-03 02:51:48 +03:00
|
|
|
}
|
2008-12-30 19:03:33 +03:00
|
|
|
} else if (button == BTN_LEFT &&
|
|
|
|
state == 0 && window->grab_device == input_device) {
|
|
|
|
window->state = WINDOW_STABLE;
|
2008-11-03 02:51:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-21 22:31:09 +03:00
|
|
|
|
|
|
|
struct key {
|
|
|
|
uint32_t code[4];
|
|
|
|
} evdev_keymap[] = {
|
|
|
|
{ { 0, 0 } }, /* 0 */
|
|
|
|
{ { 0x1b, 0x1b } },
|
|
|
|
{ { '1', '!' } },
|
|
|
|
{ { '2', '@' } },
|
|
|
|
{ { '3', '#' } },
|
|
|
|
{ { '4', '$' } },
|
|
|
|
{ { '5', '%' } },
|
|
|
|
{ { '6', '^' } },
|
|
|
|
{ { '7', '&' } },
|
|
|
|
{ { '8', '*' } },
|
|
|
|
{ { '9', '(' } },
|
|
|
|
{ { '0', ')' } },
|
|
|
|
{ { '-', '_' } },
|
|
|
|
{ { '=', '+' } },
|
|
|
|
{ { '\b', '\b' } },
|
|
|
|
{ { '\t', '\t' } },
|
|
|
|
|
|
|
|
{ { 'q', 'Q', 0x11 } }, /* 16 */
|
|
|
|
{ { 'w', 'W', 0x17 } },
|
|
|
|
{ { 'e', 'E', 0x05 } },
|
|
|
|
{ { 'r', 'R', 0x12 } },
|
|
|
|
{ { 't', 'T', 0x14 } },
|
|
|
|
{ { 'y', 'Y', 0x19 } },
|
|
|
|
{ { 'u', 'U', 0x15 } },
|
|
|
|
{ { 'i', 'I', 0x09 } },
|
|
|
|
{ { 'o', 'O', 0x0f } },
|
|
|
|
{ { 'p', 'P', 0x10 } },
|
|
|
|
{ { '[', '{', 0x1b } },
|
|
|
|
{ { ']', '}', 0x1d } },
|
|
|
|
{ { '\n', '\n' } },
|
|
|
|
{ { 0, 0 } },
|
|
|
|
{ { 'a', 'A', 0x01} },
|
|
|
|
{ { 's', 'S', 0x13 } },
|
|
|
|
|
|
|
|
{ { 'd', 'D', 0x04 } }, /* 32 */
|
|
|
|
{ { 'f', 'F', 0x06 } },
|
|
|
|
{ { 'g', 'G', 0x07 } },
|
|
|
|
{ { 'h', 'H', 0x08 } },
|
|
|
|
{ { 'j', 'J', 0x0a } },
|
|
|
|
{ { 'k', 'K', 0x0b } },
|
|
|
|
{ { 'l', 'L', 0x0c } },
|
|
|
|
{ { ';', ':' } },
|
|
|
|
{ { '\'', '"' } },
|
|
|
|
{ { '`', '~' } },
|
|
|
|
{ { 0, 0 } },
|
|
|
|
{ { '\\', '|', 0x1c } },
|
|
|
|
{ { 'z', 'Z', 0x1a } },
|
|
|
|
{ { 'x', 'X', 0x18 } },
|
|
|
|
{ { 'c', 'C', 0x03 } },
|
|
|
|
{ { 'v', 'V', 0x16 } },
|
|
|
|
|
|
|
|
{ { 'b', 'B', 0x02 } }, /* 48 */
|
|
|
|
{ { 'n', 'N', 0x0e } },
|
|
|
|
{ { 'm', 'M', 0x0d } },
|
|
|
|
{ { ',', '<' } },
|
|
|
|
{ { '.', '>' } },
|
|
|
|
{ { '/', '?' } },
|
|
|
|
{ { 0, 0 } },
|
|
|
|
{ { '*', '*' } },
|
|
|
|
{ { 0, 0 } },
|
|
|
|
{ { ' ', ' ' } },
|
|
|
|
{ { 0, 0 } }
|
|
|
|
|
|
|
|
/* 59 */
|
|
|
|
};
|
|
|
|
|
|
|
|
#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
|
|
|
|
|
2008-12-30 19:03:33 +03:00
|
|
|
static void
|
2009-02-24 06:37:14 +03:00
|
|
|
window_update_modifiers(struct window *window, uint32_t key, uint32_t state)
|
2008-12-30 19:03:33 +03:00
|
|
|
{
|
2009-02-21 22:31:09 +03:00
|
|
|
uint32_t mod = 0;
|
2009-02-23 07:01:35 +03:00
|
|
|
|
2009-02-21 22:31:09 +03:00
|
|
|
switch (key) {
|
|
|
|
case KEY_LEFTSHIFT:
|
|
|
|
case KEY_RIGHTSHIFT:
|
|
|
|
mod = WINDOW_MODIFIER_SHIFT;
|
|
|
|
break;
|
|
|
|
case KEY_LEFTCTRL:
|
|
|
|
case KEY_RIGHTCTRL:
|
|
|
|
mod = WINDOW_MODIFIER_CONTROL;
|
|
|
|
break;
|
|
|
|
case KEY_LEFTALT:
|
|
|
|
case KEY_RIGHTALT:
|
|
|
|
mod = WINDOW_MODIFIER_ALT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state)
|
|
|
|
window->modifiers |= mod;
|
|
|
|
else
|
|
|
|
window->modifiers &= ~mod;
|
2009-02-24 06:37:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
window_handle_key(void *data, struct wl_input_device *input_device,
|
|
|
|
uint32_t key, uint32_t state)
|
|
|
|
{
|
|
|
|
struct window *window = data;
|
|
|
|
uint32_t unicode = 0;
|
|
|
|
|
|
|
|
if (window->keyboard_device != input_device)
|
|
|
|
return;
|
|
|
|
|
|
|
|
window_update_modifiers(window, key, state);
|
|
|
|
|
|
|
|
if (key < ARRAY_LENGTH(evdev_keymap)) {
|
|
|
|
if (window->modifiers & WINDOW_MODIFIER_CONTROL)
|
|
|
|
unicode = evdev_keymap[key].code[2];
|
|
|
|
else if (window->modifiers & WINDOW_MODIFIER_SHIFT)
|
|
|
|
unicode = evdev_keymap[key].code[1];
|
|
|
|
else
|
|
|
|
unicode = evdev_keymap[key].code[0];
|
|
|
|
}
|
2008-12-30 19:03:33 +03:00
|
|
|
|
|
|
|
if (window->key_handler)
|
2009-02-21 22:31:09 +03:00
|
|
|
(*window->key_handler)(window, key, unicode,
|
|
|
|
state, window->modifiers, window->user_data);
|
2008-12-30 19:03:33 +03:00
|
|
|
}
|
|
|
|
|
2009-02-23 05:51:24 +03:00
|
|
|
static void
|
|
|
|
window_handle_pointer_focus(void *data,
|
|
|
|
struct wl_input_device *input_device,
|
|
|
|
struct wl_surface *surface)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
window_handle_keyboard_focus(void *data,
|
|
|
|
struct wl_input_device *input_device,
|
2009-02-24 06:30:29 +03:00
|
|
|
struct wl_surface *surface,
|
|
|
|
struct wl_array *keys)
|
2009-02-23 05:51:24 +03:00
|
|
|
{
|
2009-02-23 07:01:35 +03:00
|
|
|
struct window *window = data;
|
2009-02-24 06:37:14 +03:00
|
|
|
uint32_t *k, *end;
|
2009-02-23 07:01:35 +03:00
|
|
|
|
|
|
|
if (window->keyboard_device == input_device && surface != window->surface)
|
|
|
|
window->keyboard_device = NULL;
|
|
|
|
else if (window->keyboard_device == NULL && surface == window->surface)
|
|
|
|
window->keyboard_device = input_device;
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
|
2009-02-24 06:37:14 +03:00
|
|
|
if (window->keyboard_device) {
|
|
|
|
end = keys->data + keys->size;
|
|
|
|
for (k = keys->data; k < end; k++)
|
|
|
|
window_update_modifiers(window, *k, 1);
|
|
|
|
} else {
|
|
|
|
window->modifiers = 0;
|
|
|
|
}
|
|
|
|
|
2009-02-23 07:01:35 +03:00
|
|
|
if (window->keyboard_focus_handler)
|
|
|
|
(*window->keyboard_focus_handler)(window,
|
|
|
|
window->keyboard_device,
|
|
|
|
window->user_data);
|
2009-02-23 05:51:24 +03:00
|
|
|
}
|
|
|
|
|
2008-12-30 19:03:33 +03:00
|
|
|
static const struct wl_input_device_listener input_device_listener = {
|
|
|
|
window_handle_motion,
|
|
|
|
window_handle_button,
|
|
|
|
window_handle_key,
|
2009-02-23 05:51:24 +03:00
|
|
|
window_handle_pointer_focus,
|
|
|
|
window_handle_keyboard_focus,
|
2008-12-30 19:03:33 +03:00
|
|
|
};
|
|
|
|
|
2008-12-08 03:59:11 +03:00
|
|
|
void
|
|
|
|
window_get_child_rectangle(struct window *window,
|
|
|
|
struct rectangle *rectangle)
|
|
|
|
{
|
2010-06-08 22:08:30 +04:00
|
|
|
if (window->fullscreen && !window->decoration) {
|
2008-12-22 20:14:50 +03:00
|
|
|
*rectangle = window->allocation;
|
|
|
|
} else {
|
|
|
|
rectangle->x = window->margin + 10;
|
|
|
|
rectangle->y = window->margin + 50;
|
|
|
|
rectangle->width = window->allocation.width - 20 - window->margin * 2;
|
|
|
|
rectangle->height = window->allocation.height - 60 - window->margin * 2;
|
|
|
|
}
|
2008-12-08 03:59:11 +03:00
|
|
|
}
|
|
|
|
|
2008-12-08 21:50:07 +03:00
|
|
|
void
|
|
|
|
window_set_child_size(struct window *window,
|
|
|
|
struct rectangle *rectangle)
|
|
|
|
{
|
2010-06-15 04:25:06 +04:00
|
|
|
int32_t width, height;
|
|
|
|
|
2008-12-22 20:14:50 +03:00
|
|
|
if (!window->fullscreen) {
|
2010-06-15 04:25:06 +04:00
|
|
|
width = rectangle->width + 20 + window->margin * 2;
|
|
|
|
height = rectangle->height + 60 + window->margin * 2;
|
|
|
|
if (window->state & WINDOW_RESIZING_LEFT)
|
|
|
|
window->allocation.x -=
|
|
|
|
width - window->allocation.width;
|
|
|
|
if (window->state & WINDOW_RESIZING_TOP)
|
|
|
|
window->allocation.y -=
|
|
|
|
height - window->allocation.height;
|
|
|
|
window->allocation.width = width;
|
|
|
|
window->allocation.height = height;
|
2008-12-22 20:14:50 +03:00
|
|
|
}
|
2008-12-08 21:50:07 +03:00
|
|
|
}
|
|
|
|
|
2008-12-08 03:59:11 +03:00
|
|
|
void
|
2010-06-08 22:08:30 +04:00
|
|
|
window_copy_image(struct window *window,
|
|
|
|
struct rectangle *rectangle, EGLImageKHR image)
|
2008-12-08 03:59:11 +03:00
|
|
|
{
|
2010-06-08 22:08:30 +04:00
|
|
|
/* set image as read buffer, copy pixels or something... */
|
2008-12-22 20:14:50 +03:00
|
|
|
}
|
|
|
|
|
2009-01-15 19:37:43 +03:00
|
|
|
void
|
|
|
|
window_copy_surface(struct window *window,
|
|
|
|
struct rectangle *rectangle,
|
|
|
|
cairo_surface_t *surface)
|
|
|
|
{
|
2009-12-21 18:04:53 +03:00
|
|
|
cairo_t *cr;
|
|
|
|
|
|
|
|
cr = cairo_create (window->cairo_surface);
|
|
|
|
|
|
|
|
cairo_set_source_surface (cr,
|
|
|
|
surface,
|
|
|
|
rectangle->x, rectangle->y);
|
|
|
|
|
|
|
|
cairo_paint (cr);
|
|
|
|
cairo_destroy (cr);
|
2009-01-15 19:37:43 +03:00
|
|
|
}
|
|
|
|
|
2008-12-22 20:14:50 +03:00
|
|
|
void
|
|
|
|
window_set_fullscreen(struct window *window, int fullscreen)
|
|
|
|
{
|
|
|
|
window->fullscreen = fullscreen;
|
|
|
|
if (window->fullscreen) {
|
|
|
|
window->saved_allocation = window->allocation;
|
2009-01-27 07:42:46 +03:00
|
|
|
window->allocation = window->display->screen_allocation;
|
2008-12-22 20:14:50 +03:00
|
|
|
} else {
|
|
|
|
window->allocation = window->saved_allocation;
|
|
|
|
}
|
2008-12-08 03:59:11 +03:00
|
|
|
}
|
|
|
|
|
2010-06-08 22:08:30 +04:00
|
|
|
void
|
|
|
|
window_set_decoration(struct window *window, int decoration)
|
|
|
|
{
|
|
|
|
window->decoration = decoration;
|
|
|
|
}
|
|
|
|
|
2008-12-08 03:59:11 +03:00
|
|
|
void
|
|
|
|
window_set_resize_handler(struct window *window,
|
|
|
|
window_resize_handler_t handler, void *data)
|
|
|
|
{
|
|
|
|
window->resize_handler = handler;
|
|
|
|
window->user_data = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-12-30 19:03:33 +03:00
|
|
|
window_set_key_handler(struct window *window,
|
|
|
|
window_key_handler_t handler, void *data)
|
2008-12-08 03:59:11 +03:00
|
|
|
{
|
2008-12-30 19:03:33 +03:00
|
|
|
window->key_handler = handler;
|
2008-12-08 03:59:11 +03:00
|
|
|
window->user_data = data;
|
|
|
|
}
|
|
|
|
|
2010-06-09 04:34:11 +04:00
|
|
|
void
|
|
|
|
window_set_acknowledge_handler(struct window *window,
|
|
|
|
window_acknowledge_handler_t handler, void *data)
|
|
|
|
{
|
|
|
|
window->acknowledge_handler = handler;
|
|
|
|
window->user_data = data;
|
|
|
|
}
|
|
|
|
|
2010-06-08 22:08:30 +04:00
|
|
|
void
|
|
|
|
window_set_frame_handler(struct window *window,
|
|
|
|
window_frame_handler_t handler, void *data)
|
|
|
|
{
|
|
|
|
window->frame_handler = handler;
|
|
|
|
window->user_data = data;
|
|
|
|
}
|
|
|
|
|
2009-02-23 07:01:35 +03:00
|
|
|
void
|
|
|
|
window_set_keyboard_focus_handler(struct window *window,
|
|
|
|
window_keyboard_focus_handler_t handler, void *data)
|
|
|
|
{
|
|
|
|
window->keyboard_focus_handler = handler;
|
|
|
|
window->user_data = data;
|
|
|
|
}
|
|
|
|
|
2010-06-08 22:08:30 +04:00
|
|
|
void
|
|
|
|
window_move(struct window *window, int32_t x, int32_t y)
|
|
|
|
{
|
|
|
|
window->allocation.x = x;
|
|
|
|
window->allocation.y = y;
|
|
|
|
|
|
|
|
wl_surface_map(window->surface,
|
|
|
|
window->allocation.x - window->margin,
|
|
|
|
window->allocation.y - window->margin,
|
|
|
|
window->allocation.width,
|
|
|
|
window->allocation.height);
|
|
|
|
}
|
|
|
|
|
2009-01-27 07:42:46 +03:00
|
|
|
struct window *
|
|
|
|
window_create(struct display *display, const char *title,
|
|
|
|
int32_t x, int32_t y, int32_t width, int32_t height)
|
|
|
|
{
|
|
|
|
struct window *window;
|
|
|
|
|
|
|
|
window = malloc(sizeof *window);
|
|
|
|
if (window == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memset(window, 0, sizeof *window);
|
|
|
|
window->display = display;
|
|
|
|
window->title = strdup(title);
|
|
|
|
window->surface = wl_compositor_create_surface(display->compositor);
|
|
|
|
window->allocation.x = x;
|
|
|
|
window->allocation.y = y;
|
|
|
|
window->allocation.width = width;
|
|
|
|
window->allocation.height = height;
|
|
|
|
window->saved_allocation = window->allocation;
|
|
|
|
window->margin = 16;
|
|
|
|
window->state = WINDOW_STABLE;
|
2010-06-08 22:59:44 +04:00
|
|
|
window->decoration = 1;
|
2009-01-27 07:42:46 +03:00
|
|
|
|
2010-06-09 04:34:11 +04:00
|
|
|
wl_list_insert(display->window_list.prev, &window->link);
|
2009-01-27 07:42:46 +03:00
|
|
|
|
|
|
|
wl_input_device_add_listener(display->input_device,
|
|
|
|
&input_device_listener, window);
|
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|
|
|
|
|
2010-06-09 04:34:11 +04:00
|
|
|
static void
|
|
|
|
display_handle_device(void *data,
|
|
|
|
struct wl_compositor *compositor,
|
|
|
|
const char *device)
|
|
|
|
{
|
|
|
|
struct display *d = data;
|
|
|
|
|
|
|
|
d->device_name = strdup(device);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
display_handle_acknowledge(void *data,
|
|
|
|
struct wl_compositor *compositor,
|
|
|
|
uint32_t key, uint32_t frame)
|
|
|
|
{
|
|
|
|
struct display *d = data;
|
|
|
|
struct window *window;
|
|
|
|
cairo_surface_t *pending;
|
|
|
|
|
|
|
|
/* The acknowledge event means that the server processed our
|
|
|
|
* last commit request and we can now safely free the old
|
|
|
|
* window buffer if we resized and render the next frame into
|
|
|
|
* our back buffer.. */
|
|
|
|
wl_list_for_each(window, &d->window_list, link) {
|
|
|
|
pending = window->pending_surface;
|
|
|
|
window->pending_surface = NULL;
|
|
|
|
if (pending != window->cairo_surface)
|
|
|
|
window_attach_surface(window);
|
|
|
|
cairo_surface_destroy(pending);
|
|
|
|
if (window->acknowledge_handler)
|
|
|
|
(*window->acknowledge_handler)(window, key, frame, window->user_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
display_handle_frame(void *data,
|
|
|
|
struct wl_compositor *compositor,
|
|
|
|
uint32_t frame, uint32_t timestamp)
|
|
|
|
{
|
|
|
|
struct display *d = data;
|
|
|
|
struct window *window;
|
|
|
|
|
|
|
|
wl_list_for_each(window, &d->window_list, link) {
|
|
|
|
if (window->frame_handler)
|
|
|
|
(*window->frame_handler)(window, frame,
|
|
|
|
timestamp, window->user_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wl_compositor_listener compositor_listener = {
|
|
|
|
display_handle_device,
|
|
|
|
display_handle_acknowledge,
|
|
|
|
display_handle_frame,
|
|
|
|
};
|
|
|
|
|
2008-12-30 19:03:33 +03:00
|
|
|
static void
|
2009-01-27 07:42:46 +03:00
|
|
|
display_handle_geometry(void *data,
|
|
|
|
struct wl_output *output,
|
|
|
|
int32_t width, int32_t height)
|
2008-12-08 03:59:11 +03:00
|
|
|
{
|
2009-01-27 07:42:46 +03:00
|
|
|
struct display *display = data;
|
2008-12-30 19:03:33 +03:00
|
|
|
|
2009-01-27 07:42:46 +03:00
|
|
|
display->screen_allocation.x = 0;
|
|
|
|
display->screen_allocation.y = 0;
|
|
|
|
display->screen_allocation.width = width;
|
|
|
|
display->screen_allocation.height = height;
|
2008-12-08 03:59:11 +03:00
|
|
|
}
|
|
|
|
|
2008-12-30 19:03:33 +03:00
|
|
|
static const struct wl_output_listener output_listener = {
|
2009-01-27 07:42:46 +03:00
|
|
|
display_handle_geometry,
|
2008-12-30 19:03:33 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2009-01-27 07:42:46 +03:00
|
|
|
display_handle_global(struct wl_display *display,
|
2008-12-30 19:03:33 +03:00
|
|
|
struct wl_object *object, void *data)
|
2008-12-08 08:01:36 +03:00
|
|
|
{
|
2009-01-27 07:42:46 +03:00
|
|
|
struct display *d = data;
|
2008-12-30 19:03:33 +03:00
|
|
|
|
|
|
|
if (wl_object_implements(object, "compositor", 1)) {
|
2009-01-27 07:42:46 +03:00
|
|
|
d->compositor = (struct wl_compositor *) object;
|
2010-06-09 04:34:11 +04:00
|
|
|
wl_compositor_add_listener(d->compositor, &compositor_listener, d);
|
2008-12-30 19:03:33 +03:00
|
|
|
} else if (wl_object_implements(object, "output", 1)) {
|
2009-01-27 07:42:46 +03:00
|
|
|
d->output = (struct wl_output *) object;
|
|
|
|
wl_output_add_listener(d->output, &output_listener, d);
|
2008-12-30 19:03:33 +03:00
|
|
|
} else if (wl_object_implements(object, "input_device", 1)) {
|
2009-01-27 07:42:46 +03:00
|
|
|
d->input_device =(struct wl_input_device *) object;
|
2008-12-30 19:03:33 +03:00
|
|
|
}
|
2008-12-08 08:01:36 +03:00
|
|
|
}
|
|
|
|
|
2010-06-08 22:59:44 +04:00
|
|
|
static const char socket_name[] = "\0wayland";
|
|
|
|
|
2009-01-27 07:42:46 +03:00
|
|
|
struct display *
|
2010-06-08 22:59:44 +04:00
|
|
|
display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
|
2008-11-03 23:31:30 +03:00
|
|
|
{
|
2010-06-08 22:08:30 +04:00
|
|
|
PFNEGLGETTYPEDDISPLAYMESA get_typed_display_mesa;
|
2009-01-27 07:42:46 +03:00
|
|
|
struct display *d;
|
2010-06-08 22:08:30 +04:00
|
|
|
EGLint major, minor, count;
|
|
|
|
EGLConfig config;
|
2010-06-08 22:59:44 +04:00
|
|
|
int fd;
|
|
|
|
GOptionContext *context;
|
|
|
|
GError *error;
|
2010-06-08 22:08:30 +04:00
|
|
|
|
|
|
|
static const EGLint config_attribs[] = {
|
|
|
|
EGL_SURFACE_TYPE, 0,
|
|
|
|
EGL_NO_SURFACE_CAPABLE_MESA, EGL_OPENGL_BIT,
|
|
|
|
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
|
|
|
|
EGL_NONE
|
|
|
|
};
|
2008-11-07 23:54:48 +03:00
|
|
|
|
2010-06-09 04:34:11 +04:00
|
|
|
g_type_init();
|
|
|
|
|
2010-06-08 22:59:44 +04:00
|
|
|
context = g_option_context_new(NULL);
|
|
|
|
if (option_entries) {
|
|
|
|
g_option_context_add_main_entries(context, option_entries, "Wayland View");
|
|
|
|
if (!g_option_context_parse(context, argc, argv, &error)) {
|
|
|
|
fprintf(stderr, "option parsing failed: %s\n", error->message);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-27 07:42:46 +03:00
|
|
|
d = malloc(sizeof *d);
|
|
|
|
if (d == NULL)
|
2008-11-07 23:54:48 +03:00
|
|
|
return NULL;
|
|
|
|
|
2010-06-09 04:34:11 +04:00
|
|
|
d->display = wl_display_create(socket_name, sizeof socket_name);
|
|
|
|
if (d->display == NULL) {
|
|
|
|
fprintf(stderr, "failed to create display: %m\n");
|
2010-06-08 22:59:44 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-06-09 04:34:11 +04:00
|
|
|
/* Set up listener so we'll catch all events. */
|
|
|
|
wl_display_add_global_listener(d->display,
|
|
|
|
display_handle_global, d);
|
|
|
|
|
|
|
|
/* Process connection events. */
|
|
|
|
wl_display_iterate(d->display, WL_DISPLAY_READABLE);
|
|
|
|
|
|
|
|
fd = open(d->device_name, O_RDWR);
|
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "drm open failed: %m\n");
|
2010-06-08 22:59:44 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-06-08 22:08:30 +04:00
|
|
|
get_typed_display_mesa =
|
|
|
|
(PFNEGLGETTYPEDDISPLAYMESA) eglGetProcAddress("eglGetTypedDisplayMESA");
|
|
|
|
if (get_typed_display_mesa == NULL) {
|
|
|
|
fprintf(stderr, "eglGetDisplayMESA() not found\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->dpy = get_typed_display_mesa(EGL_DRM_DISPLAY_TYPE_MESA,
|
|
|
|
(void *) fd);
|
|
|
|
if (!eglInitialize(d->dpy, &major, &minor)) {
|
|
|
|
fprintf(stderr, "failed to initialize display\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!eglChooseConfig(d->dpy, config_attribs, &config, 1, &count) ||
|
|
|
|
count == 0) {
|
|
|
|
fprintf(stderr, "eglChooseConfig() failed\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
eglBindAPI(EGL_OPENGL_API);
|
|
|
|
|
|
|
|
d->ctx = eglCreateContext(d->dpy, config, EGL_NO_CONTEXT, NULL);
|
|
|
|
if (d->ctx == NULL) {
|
|
|
|
fprintf(stderr, "failed to create context\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
|
|
|
|
fprintf(stderr, "faile to make context current\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->device = cairo_egl_device_create(d->dpy, d->ctx);
|
2009-05-29 04:23:31 +04:00
|
|
|
if (d->device == NULL) {
|
|
|
|
fprintf(stderr, "failed to get cairo drm device\n");
|
2009-01-15 19:37:43 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-11-07 23:54:48 +03:00
|
|
|
|
2010-06-08 22:59:44 +04:00
|
|
|
d->loop = g_main_loop_new(NULL, FALSE);
|
2010-06-09 04:34:11 +04:00
|
|
|
d->source = wl_glib_source_new(d->display);
|
2010-06-08 22:59:44 +04:00
|
|
|
g_source_attach(d->source, NULL);
|
|
|
|
|
2010-06-09 04:34:11 +04:00
|
|
|
wl_list_init(&d->window_list);
|
|
|
|
|
2009-01-27 07:42:46 +03:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct wl_compositor *
|
|
|
|
display_get_compositor(struct display *display)
|
|
|
|
{
|
|
|
|
return display->compositor;
|
2008-11-03 02:51:48 +03:00
|
|
|
}
|
2010-06-08 22:59:44 +04:00
|
|
|
|
|
|
|
EGLDisplay
|
|
|
|
display_get_egl_display(struct display *d)
|
|
|
|
{
|
|
|
|
return d->dpy;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
display_run(struct display *d)
|
|
|
|
{
|
|
|
|
g_main_loop_run(d->loop);
|
|
|
|
}
|