Put code in src/ and include/

This commit is contained in:
Johan Malm 2020-05-27 14:29:05 +01:00
parent d9a083960b
commit d28465dfc3
12 changed files with 39 additions and 49 deletions

74
src/debug/dbg.c Normal file
View file

@ -0,0 +1,74 @@
#include "labwc.h"
static void show_one_xdg_view(struct view *view)
{
fprintf(stderr, "XDG ");
switch (view->xdg_surface->role) {
case WLR_XDG_SURFACE_ROLE_NONE:
fprintf(stderr, "- ");
break;
case WLR_XDG_SURFACE_ROLE_TOPLEVEL:
fprintf(stderr, "0 ");
break;
case WLR_XDG_SURFACE_ROLE_POPUP:
fprintf(stderr, "? ");
break;
}
fprintf(stderr, " %p %s", (void *)view,
view->xdg_surface->toplevel->app_id);
fprintf(stderr, " {%d, %d, %d, %d}\n", view->xdg_surface->geometry.x,
view->xdg_surface->geometry.y,
view->xdg_surface->geometry.height,
view->xdg_surface->geometry.width);
}
static void show_one_xwl_view(struct view *view)
{
fprintf(stderr, "XWL ");
if (!view->been_mapped) {
fprintf(stderr, "- ");
} else {
fprintf(stderr, "%d ", xwl_nr_parents(view));
}
fprintf(stderr, " %d ",
wl_list_length(&view->xwayland_surface->children));
if (view->mapped) {
fprintf(stderr, "Y");
} else {
fprintf(stderr, "-");
}
fprintf(stderr, " %p %s {%d,%d,%d,%d}\n", (void *)view,
view->xwayland_surface->class, view->xwayland_surface->x,
view->xwayland_surface->y, view->xwayland_surface->width,
view->xwayland_surface->height);
/*
* Other variables to consider printing:
*
* view->mapped,
* view->been_mapped,
* view->xwayland_surface->override_redirect,
* wlr_xwayland_or_surface_wants_focus(view->xwayland_surface));
* view->xwayland_surface->saved_width,
* view->xwayland_surface->saved_height);
* view->xwayland_surface->surface->sx,
* view->xwayland_surface->surface->sy);
*/
}
void dbg_show_one_view(struct view *view)
{
if (view->type == LAB_XDG_SHELL_VIEW)
show_one_xdg_view(view);
else if (view->type == LAB_XWAYLAND_VIEW)
show_one_xwl_view(view);
}
void dbg_show_views(struct server *server)
{
struct view *view;
fprintf(stderr, "---\n");
fprintf(stderr, "TYPE NR_PNT NR_CLD MAPPED VIEW-POINTER NAME\n");
wl_list_for_each_reverse (view, &server->views, link)
dbg_show_one_view(view);
}

3
src/debug/meson.build Normal file
View file

@ -0,0 +1,3 @@
labwc_sources += files(
'dbg.c',
)

49
src/deco.c Normal file
View file

@ -0,0 +1,49 @@
#include "labwc.h"
struct wlr_box deco_max_extents(struct view *view)
{
struct wlr_box box = {
.x = view->x - XWL_WINDOW_BORDER,
.y = view->y - XWL_TITLEBAR_HEIGHT - XWL_WINDOW_BORDER,
.width = view->surface->current.width + 2 * XWL_WINDOW_BORDER,
.height = view->surface->current.height + XWL_TITLEBAR_HEIGHT +
2 * XWL_WINDOW_BORDER,
};
return box;
}
struct wlr_box deco_box(struct view *view, enum deco_part deco_part)
{
struct wlr_box box = { .x = 0, .y = 0, .width = 0, .height = 0 };
if (!view || !view->surface)
return box;
switch (deco_part) {
case LAB_DECO_PART_TOP:
box.x = view->x - XWL_WINDOW_BORDER;
box.y = view->y - XWL_TITLEBAR_HEIGHT - XWL_WINDOW_BORDER;
box.width =
view->surface->current.width + 2 * XWL_WINDOW_BORDER;
box.height = XWL_TITLEBAR_HEIGHT + XWL_WINDOW_BORDER;
break;
case LAB_DECO_PART_LEFT:
box.x = view->x - XWL_WINDOW_BORDER;
box.y = view->y;
box.width = XWL_WINDOW_BORDER;
box.height = view->surface->current.height;
break;
default:
break;
}
return box;
}
enum deco_part deco_at(struct view *view, double lx, double ly)
{
enum deco_part deco_part;
for (deco_part = 0; deco_part < LAB_DECO_NONE; ++deco_part) {
struct wlr_box box = deco_box(view, deco_part);
if (wlr_box_contains_point(&box, lx, ly))
return deco_part;
}
return LAB_DECO_NONE;
}

294
src/main.c Normal file
View file

@ -0,0 +1,294 @@
#include "labwc.h"
#include <wlr/types/wlr_export_dmabuf_v1.h>
#include <wlr/types/wlr_screencopy_v1.h>
#include <wlr/types/wlr_data_control_v1.h>
#include <wlr/types/wlr_gamma_control_v1.h>
#include <wlr/types/wlr_primary_selection_v1.h>
int main(int argc, char *argv[])
{
struct server server = { 0 };
char *startup_cmd = NULL;
wlr_log_init(WLR_ERROR, NULL);
int c;
while ((c = getopt(argc, argv, "s:h")) != -1) {
switch (c) {
case 's':
startup_cmd = optarg;
break;
default:
printf("Usage: %s [-s startup command]\n", argv[0]);
return 0;
}
}
if (optind < argc) {
printf("Usage: %s [-s startup command]\n", argv[0]);
return 0;
}
/* Wayland requires XDG_RUNTIME_DIR to be set */
if (!getenv("XDG_RUNTIME_DIR")) {
wlr_log(WLR_ERROR, "XDG_RUNTIME_DIR is not set");
return 1;
}
/*
* The Wayland display is managed by libwayland. It handles accepting
* clients from the Unix socket, manging Wayland globals, and so on.
*/
server.wl_display = wl_display_create();
if (!server.wl_display) {
wlr_log(WLR_ERROR, "cannot allocate a wayland display");
return 1;
}
/* TODO: Catch signals. Maybe SIGHUP for reconfigure */
/*
* The backend is a wlroots feature which abstracts the underlying
* input and output hardware. the autocreate option will choose the
* most suitable backend based on the current environment, such as
* opening an x11 window if an x11 server is running. the null
* argument here optionally allows you to pass in a custom renderer if
* wlr_renderer doesn't meet your needs. the backend uses the
* renderer, for example, to fall back to software cursors if the
* backend does not support hardware cursors (some older gpus don't).
*/
server.backend = wlr_backend_autocreate(server.wl_display, NULL);
if (!server.backend) {
wlr_log(WLR_ERROR, "unable to create the wlroots backend");
return 1;
}
/*
* If we don't provide a renderer, autocreate makes a GLES2 renderer
* for us. The renderer is responsible for defining the various pixel
* formats it supports for shared memory, this configures that for
* clients.
*/
server.renderer = wlr_backend_get_renderer(server.backend);
wlr_renderer_init_wl_display(server.renderer, server.wl_display);
wl_list_init(&server.views);
wl_list_init(&server.outputs);
/*
* Create an output layout, which a wlroots utility for working with
* an arrangement of screens in a physical layout.
*/
server.output_layout = wlr_output_layout_create();
if (!server.output_layout) {
wlr_log(WLR_ERROR, "unable to create output layout");
return 1;
}
/*
* Create some hands-off wlroots interfaces. The compositor is
* necessary for clients to allocate surfaces and the data device
* manager handles the clipboard. Each of these wlroots interfaces has
* room for you to dig your fingers in and play with their behavior if
* you want.
*/
server.compositor =
wlr_compositor_create(server.wl_display, server.renderer);
if (!server.compositor) {
wlr_log(WLR_ERROR, "unable to create the wlroots compositor");
return 1;
}
struct wlr_data_device_manager *device_manager = NULL;
device_manager = wlr_data_device_manager_create(server.wl_display);
if (!device_manager) {
wlr_log(WLR_ERROR, "unable to create data device manager");
return 1;
}
/*
* Configure a listener to be notified when new outputs are available
* on the backend.
*/
server.new_output.notify = server_new_output;
wl_signal_add(&server.backend->events.new_output, &server.new_output);
/*
* Configures a seat, which is a single "seat" at which a user sits
* and operates the computer. This conceptually includes up to one
* keyboard, pointer, touch, and drawing tablet device. We also rig up
* a listener to let us know when new input devices are available on
* the backend.
*/
server.seat = wlr_seat_create(server.wl_display, "seat0");
if (!server.seat) {
wlr_log(WLR_ERROR, "cannot allocate seat0");
return 1;
}
server.cursor = wlr_cursor_create();
if (!server.cursor) {
wlr_log(WLR_ERROR, "unable to create cursor");
return 1;
}
wlr_cursor_attach_output_layout(server.cursor, server.output_layout);
// This is done below
// server.cursor_mgr = wlr_xcursor_manager_create(NULL, XCURSOR_SIZE);
// if (!server.cursor_mgr) {
// wlr_log(WLR_ERROR, "cannot create xcursor manager");
// return 1;
//}
server.cursor_motion.notify = server_cursor_motion;
wl_signal_add(&server.cursor->events.motion, &server.cursor_motion);
server.cursor_motion_absolute.notify = server_cursor_motion_absolute;
wl_signal_add(&server.cursor->events.motion_absolute,
&server.cursor_motion_absolute);
server.cursor_button.notify = server_cursor_button;
wl_signal_add(&server.cursor->events.button, &server.cursor_button);
server.cursor_axis.notify = server_cursor_axis;
wl_signal_add(&server.cursor->events.axis, &server.cursor_axis);
server.cursor_frame.notify = server_cursor_frame;
wl_signal_add(&server.cursor->events.frame, &server.cursor_frame);
wl_list_init(&server.keyboards);
server.new_input.notify = server_new_input;
wl_signal_add(&server.backend->events.new_input, &server.new_input);
server.request_cursor.notify = seat_request_cursor;
wl_signal_add(&server.seat->events.request_set_cursor,
&server.request_cursor);
server.request_set_selection.notify = seat_request_set_selection;
wl_signal_add(&server.seat->events.request_set_selection,
&server.request_set_selection);
/* Init xdg-shell */
server.xdg_shell = wlr_xdg_shell_create(server.wl_display);
if (!server.xdg_shell) {
wlr_log(WLR_ERROR, "unable to create the XDG shell interface");
return 1;
}
server.new_xdg_surface.notify = xdg_surface_new;
wl_signal_add(&server.xdg_shell->events.new_surface,
&server.new_xdg_surface);
/* Disable CSD */
struct wlr_xdg_decoration_manager_v1 *xdg_deco_mgr = NULL;
xdg_deco_mgr = wlr_xdg_decoration_manager_v1_create(server.wl_display);
if (!xdg_deco_mgr) {
wlr_log(WLR_ERROR, "unable to create the XDG deco manager");
return 1;
}
wl_signal_add(&xdg_deco_mgr->events.new_toplevel_decoration,
&server.xdg_toplevel_decoration);
server.xdg_toplevel_decoration.notify = xdg_toplevel_decoration;
struct wlr_server_decoration_manager *deco_mgr = NULL;
deco_mgr = wlr_server_decoration_manager_create(server.wl_display);
if (!deco_mgr) {
wlr_log(WLR_ERROR, "unable to create the server deco manager");
return 1;
}
wlr_server_decoration_manager_set_default_mode(
deco_mgr, LAB_DISABLE_CSD ?
WLR_SERVER_DECORATION_MANAGER_MODE_SERVER :
WLR_SERVER_DECORATION_MANAGER_MODE_CLIENT);
/* FIXME: Check return values */
wlr_export_dmabuf_manager_v1_create(server.wl_display);
wlr_screencopy_manager_v1_create(server.wl_display);
wlr_data_control_manager_v1_create(server.wl_display);
wlr_gamma_control_manager_v1_create(server.wl_display);
wlr_primary_selection_v1_device_manager_create(server.wl_display);
/* Init xwayland */
server.xwayland = wlr_xwayland_create(server.wl_display,
server.compositor, false);
if (!server.xwayland) {
wlr_log(WLR_ERROR, "cannot create xwayland server");
return 1;
}
server.new_xwayland_surface.notify = xwl_surface_new;
wl_signal_add(&server.xwayland->events.new_surface,
&server.new_xwayland_surface);
server.cursor_mgr =
wlr_xcursor_manager_create(XCURSOR_DEFAULT, XCURSOR_SIZE);
if (!server.cursor_mgr) {
wlr_log(WLR_ERROR, "cannot create xwayland xcursor manager");
return 1;
}
if (setenv("DISPLAY", server.xwayland->display_name, true) < 0)
wlr_log_errno(WLR_ERROR, "unable to set DISPLAY for xwayland");
else
wlr_log(WLR_DEBUG, "xwayland is running on display %s",
server.xwayland->display_name);
if (wlr_xcursor_manager_load(server.cursor_mgr, 1))
wlr_log(WLR_ERROR, "cannot load xwayland xcursor theme");
struct wlr_xcursor *xcursor;
xcursor = wlr_xcursor_manager_get_xcursor(server.cursor_mgr,
XCURSOR_DEFAULT, 1);
if (xcursor) {
struct wlr_xcursor_image *image = xcursor->images[0];
wlr_xwayland_set_cursor(server.xwayland, image->buffer,
image->width * 4, image->width,
image->height, image->hotspot_x,
image->hotspot_y);
}
/* Add a Unix socket to the Wayland display. */
const char *socket = wl_display_add_socket_auto(server.wl_display);
if (!socket) {
wlr_log_errno(WLR_ERROR, "unable to open wayland socket");
return 1;
}
/*
* Start the backend. This will enumerate outputs and inputs, become
* the DRM master, etc
*/
if (!wlr_backend_start(server.backend)) {
wlr_log(WLR_ERROR, "unable to start the wlroots backend");
return 1;
}
setenv("WAYLAND_DISPLAY", socket, true);
if (setenv("WAYLAND_DISPLAY", socket, true) < 0)
wlr_log_errno(WLR_ERROR, "unable to set WAYLAND_DISPLAY");
else
wlr_log(WLR_DEBUG, "WAYLAND_DISPLAY=%s", socket);
wl_display_init_shm(server.wl_display);
wlr_xwayland_set_seat(server.xwayland, server.seat);
if (startup_cmd) {
if (fork() == 0) {
execl("/bin/sh", "/bin/sh", "-c", startup_cmd,
(void *)NULL);
}
}
wl_display_run(server.wl_display);
struct output *_output, *_output_tmp;
wl_list_for_each_safe (_output, _output_tmp, &server.outputs, link) {
wl_list_remove(&_output->link);
free(_output);
}
struct output *k, *k_tmp;
wl_list_for_each_safe (k, k_tmp, &server.keyboards, link) {
wl_list_remove(&k->link);
free(k);
}
wlr_cursor_destroy(server.cursor);
wlr_output_layout_destroy(server.output_layout);
wlr_xwayland_destroy(server.xwayland);
wlr_xcursor_manager_destroy(server.cursor_mgr);
wl_display_destroy_clients(server.wl_display);
wl_display_destroy(server.wl_display);
return 0;
}

11
src/meson.build Normal file
View file

@ -0,0 +1,11 @@
labwc_sources = files(
'main.c',
'server.c',
'output.c',
'view.c',
'xdg.c',
'xwl.c',
'deco.c',
)
subdir('debug')

186
src/output.c Normal file
View file

@ -0,0 +1,186 @@
#include "labwc.h"
struct render_data {
struct wlr_output *output;
struct wlr_renderer *renderer;
struct view *view;
struct timespec *when;
};
static void render_cycle_box(struct output *output)
{
if (!output->server->cycle_view)
return;
struct view *view;
wl_list_for_each_reverse (view, &output->server->views, link) {
if (view != output->server->cycle_view)
continue;
struct wlr_box box;
if ((view->type == LAB_XWAYLAND_VIEW) || LAB_DISABLE_CSD) {
box = deco_max_extents(view);
} else {
box = view_get_surface_geometry(view);
box.x += view->x;
box.y += view->y;
}
float cycle_color[] = { 0.0, 0.0, 0.0, 0.2 };
wlr_render_rect(output->server->renderer, &box, cycle_color,
output->wlr_output->transform_matrix);
return;
}
}
static void render_decorations(struct wlr_output *output, struct view *view)
{
if (!view_want_deco(view))
return;
struct wlr_box box = deco_max_extents(view);
float matrix[9];
wlr_matrix_project_box(matrix, &box, WL_OUTPUT_TRANSFORM_NORMAL, 0,
output->transform_matrix);
float color[] = { 0.2, 0.2, 0.7, 0.9 };
wlr_render_quad_with_matrix(view->server->renderer, color, matrix);
box = deco_box(view, LAB_DECO_PART_TOP);
float color2[] = { 0.7, 0.2, 0.2, 0.9 };
wlr_render_rect(view->server->renderer, &box, color2,
output->transform_matrix);
}
static void render_surface(struct wlr_surface *surface, int sx, int sy,
void *data)
{
/* This function is called for every surface that needs to be rendered.
*/
struct render_data *rdata = data;
struct view *view = rdata->view;
struct wlr_output *output = rdata->output;
/* We first obtain a wlr_texture, which is a GPU resource. wlroots
* automatically handles negotiating these with the client. The
* underlying resource could be an opaque handle passed from the client,
* or the client could have sent a pixel buffer which we copied to the
* GPU, or a few other means. You don't have to worry about this,
* wlroots takes care of it. */
struct wlr_texture *texture = wlr_surface_get_texture(surface);
if (texture == NULL) {
return;
}
/* The view has a position in layout coordinates. If you have two
* displays, one next to the other, both 1080p, a view on the rightmost
* display might have layout coordinates of 2000,100. We need to
* translate that to output-local coordinates, or (2000 - 1920). */
double ox = 0, oy = 0;
wlr_output_layout_output_coords(view->server->output_layout, output,
&ox, &oy);
ox += view->x + sx;
oy += view->y + sy;
/* We also have to apply the scale factor for HiDPI outputs. This is
* only part of the puzzle, TinyWL does not fully support HiDPI. */
struct wlr_box box = {
.x = ox * output->scale,
.y = oy * output->scale,
.width = surface->current.width * output->scale,
.height = surface->current.height * output->scale,
};
/*
* Those familiar with OpenGL are also familiar with the role of
* matricies in graphics programming. We need to prepare a matrix to
* render the view with. wlr_matrix_project_box is a helper which takes
* a box with a desired x, y coordinates, width and height, and an
* output geometry, then prepares an orthographic projection and
* multiplies the necessary transforms to produce a
* model-view-projection matrix.
*
* Naturally you can do this any way you like, for example to make a 3D
* compositor.
*/
float matrix[9];
enum wl_output_transform transform =
wlr_output_transform_invert(surface->current.transform);
wlr_matrix_project_box(matrix, &box, transform, 0,
output->transform_matrix);
/* This takes our matrix, the texture, and an alpha, and performs the
* actual rendering on the GPU. */
wlr_render_texture_with_matrix(rdata->renderer, texture, matrix, 1);
/* This lets the client know that we've displayed that frame and it can
* prepare another one now if it likes. */
wlr_surface_send_frame_done(surface, rdata->when);
}
void output_frame(struct wl_listener *listener, void *data)
{
/* This function is called every time an output is ready to display a
* frame, generally at the output's refresh rate (e.g. 60Hz). */
struct output *output = wl_container_of(listener, output, frame);
struct wlr_renderer *renderer = output->server->renderer;
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
/* wlr_output_attach_render makes the OpenGL context current. */
if (!wlr_output_attach_render(output->wlr_output, NULL)) {
return;
}
/* The "effective" resolution can change if you rotate your outputs. */
int width, height;
wlr_output_effective_resolution(output->wlr_output, &width, &height);
/* Begin the renderer (calls glViewport and some other GL sanity checks)
*/
wlr_renderer_begin(renderer, width, height);
float color[4] = { 0.3, 0.3, 0.3, 1.0 };
wlr_renderer_clear(renderer, color);
/* Each subsequent window we render is rendered on top of the last.
* Because our view list is ordered front-to-back, we iterate over it
* backwards. */
struct view *view;
wl_list_for_each_reverse (view, &output->server->views, link) {
if (!view->mapped) {
/* An unmapped view should not be rendered. */
continue;
}
struct render_data rdata = {
.output = output->wlr_output,
.view = view,
.renderer = renderer,
.when = &now,
};
render_decorations(output->wlr_output, view);
/* This calls our render_surface function for each surface among
* the xdg_surface's toplevel and popups. */
if (view->type == LAB_XDG_SHELL_VIEW) {
wlr_xdg_surface_for_each_surface(
view->xdg_surface, render_surface, &rdata);
} else if (view->type == LAB_XWAYLAND_VIEW) {
render_surface(view->xwayland_surface->surface, 0, 0,
&rdata);
}
}
/* If in cycle (alt-tab) mode, highlight selected view */
render_cycle_box(output);
/* Hardware cursors are rendered by the GPU on a separate plane, and can
* be moved around without re-rendering what's beneath them - which is
* more efficient. However, not all hardware supports hardware cursors.
* For this reason, wlroots provides a software fallback, which we ask
* it to render here. wlr_cursor handles configuring hardware vs
* software cursors for you,
* and this function is a no-op when hardware cursors are in use. */
wlr_output_render_software_cursors(output->wlr_output, NULL);
/* Conclude rendering and swap the buffers, showing the final frame
* on-screen. */
wlr_renderer_end(renderer);
wlr_output_commit(output->wlr_output);
}

483
src/server.c Normal file
View file

@ -0,0 +1,483 @@
#include "labwc.h"
#define MIN_VIEW_WIDTH (100)
#define MIN_VIEW_HEIGHT (60)
void begin_interactive(struct view *view, enum cursor_mode mode, uint32_t edges)
{
/*
* This function sets up an interactive move or resize operation, where
* the compositor stops propegating pointer events to clients and
* instead consumes them itself, to move or resize windows.
*/
struct server *server = view->server;
server->grabbed_view = view;
server->cursor_mode = mode;
/* Remember view and cursor positions at start of move/resize */
server->grab_x = server->cursor->x;
server->grab_y = server->cursor->y;
server->grab_box = view_geometry(view);
server->resize_edges = edges;
}
static void keyboard_handle_modifiers(struct wl_listener *listener, void *data)
{
/*
* This event is raised when a modifier key, such as shift or alt, is
* pressed. We simply communicate this to the client.
*/
struct keyboard *keyboard =
wl_container_of(listener, keyboard, modifiers);
/*
* A seat can only have one keyboard, but this is a limitation of the
* Wayland protocol - not wlroots. We assign all connected keyboards to
* the same seat. You can swap out the underlying wlr_keyboard like
* this and wlr_seat handles this transparently.
*/
wlr_seat_set_keyboard(keyboard->server->seat, keyboard->device);
/* Send modifiers to the client. */
wlr_seat_keyboard_notify_modifiers(
keyboard->server->seat, &keyboard->device->keyboard->modifiers);
}
static bool handle_keybinding(struct server *server, xkb_keysym_t sym)
{
/*
* Here we handle compositor keybindings. This is when the compositor is
* processing keys, rather than passing them on to the client for its
* own processing.
*
* This function assumes Alt is held down.
*/
switch (sym) {
case XKB_KEY_Escape:
wl_display_terminate(server->wl_display);
break;
case XKB_KEY_F1:
case XKB_KEY_F2:
server->cycle_view = next_toplevel(view_front_toplevel(server));
fprintf(stderr, "cycle_view=%p\n", (void *)server->cycle_view);
break;
case XKB_KEY_F3:
if (fork() == 0) {
execl("/bin/dmenu_run", "/bin/dmenu_run", (void *)NULL);
}
break;
case XKB_KEY_F6:
begin_interactive(view_front_toplevel(server),
TINYWL_CURSOR_MOVE, 0);
break;
case XKB_KEY_F12:
dbg_show_views(server);
break;
default:
return false;
}
return true;
}
static void keyboard_handle_key(struct wl_listener *listener, void *data)
{
/* This event is raised when a key is pressed or released. */
struct keyboard *keyboard = wl_container_of(listener, keyboard, key);
struct server *server = keyboard->server;
struct wlr_event_keyboard_key *event = data;
struct wlr_seat *seat = server->seat;
/* Translate libinput keycode -> xkbcommon */
uint32_t keycode = event->keycode + 8;
/* Get a list of keysyms based on the keymap for this keyboard */
const xkb_keysym_t *syms;
int nsyms = xkb_state_key_get_syms(
keyboard->device->keyboard->xkb_state, keycode, &syms);
bool handled = false;
uint32_t modifiers =
wlr_keyboard_get_modifiers(keyboard->device->keyboard);
if (server->cycle_view) {
if ((syms[0] == XKB_KEY_Alt_L) &&
event->state == WLR_KEY_RELEASED) {
/* end cycle */
view_focus(server->cycle_view);
server->cycle_view = NULL;
} else if (event->state == WLR_KEY_PRESSED) {
/* cycle to next */
server->cycle_view = next_toplevel(server->cycle_view);
fprintf(stderr, "cycle_view=%p\n",
(void *)server->cycle_view);
return;
}
}
/* Handle compositor key bindings */
if ((modifiers & WLR_MODIFIER_ALT) && event->state == WLR_KEY_PRESSED) {
/* If alt is held down and this button was _pressed_, we attempt
* to process it as a compositor keybinding. */
for (int i = 0; i < nsyms; i++) {
handled = handle_keybinding(server, syms[i]);
}
}
if (!handled) {
/* Otherwise, we pass it along to the client. */
wlr_seat_set_keyboard(seat, keyboard->device);
wlr_seat_keyboard_notify_key(seat, event->time_msec,
event->keycode, event->state);
}
}
static void server_new_keyboard(struct server *server,
struct wlr_input_device *device)
{
struct keyboard *keyboard = calloc(1, sizeof(struct keyboard));
keyboard->server = server;
keyboard->device = device;
/*
* We need to prepare an XKB keymap and assign it to the keyboard. This
* assumes the defaults (e.g. layout = "us").
*/
struct xkb_rule_names rules = { 0 };
struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
struct xkb_keymap *keymap = xkb_map_new_from_names(
context, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS);
wlr_keyboard_set_keymap(device->keyboard, keymap);
xkb_keymap_unref(keymap);
xkb_context_unref(context);
wlr_keyboard_set_repeat_info(device->keyboard, 25, 600);
/* Here we set up listeners for keyboard events. */
keyboard->modifiers.notify = keyboard_handle_modifiers;
wl_signal_add(&device->keyboard->events.modifiers,
&keyboard->modifiers);
keyboard->key.notify = keyboard_handle_key;
wl_signal_add(&device->keyboard->events.key, &keyboard->key);
wlr_seat_set_keyboard(server->seat, device);
/* And add the keyboard to our list of keyboards */
wl_list_insert(&server->keyboards, &keyboard->link);
}
static void server_new_pointer(struct server *server,
struct wlr_input_device *device)
{
/* TODO: Configure libinput on device to set tap, acceleration, etc */
wlr_cursor_attach_input_device(server->cursor, device);
}
void server_new_input(struct wl_listener *listener, void *data)
{
/*
* This event is raised by the backend when a new input device becomes
* available.
*/
struct server *server = wl_container_of(listener, server, new_input);
struct wlr_input_device *device = data;
switch (device->type) {
case WLR_INPUT_DEVICE_KEYBOARD:
server_new_keyboard(server, device);
break;
case WLR_INPUT_DEVICE_POINTER:
server_new_pointer(server, device);
break;
default:
break;
}
/*
* We need to let the wlr_seat know what our capabilities are, which is
* communiciated to the client.
*/
uint32_t caps = WL_SEAT_CAPABILITY_POINTER;
if (!wl_list_empty(&server->keyboards)) {
caps |= WL_SEAT_CAPABILITY_KEYBOARD;
}
wlr_seat_set_capabilities(server->seat, caps);
}
void seat_request_cursor(struct wl_listener *listener, void *data)
{
struct server *server =
wl_container_of(listener, server, request_cursor);
/*
* This event is rasied by the seat when a client provides a cursor
* image
*/
struct wlr_seat_pointer_request_set_cursor_event *event = data;
struct wlr_seat_client *focused_client =
server->seat->pointer_state.focused_client;
/* This can be sent by any client, so we check to make sure this one is
* actually has pointer focus first. */
if (focused_client == event->seat_client) {
/* Once we've vetted the client, we can tell the cursor to use
* the provided surface as the cursor image. It will set the
* hardware cursor on the output that it's currently on and
* continue to do so as the cursor moves between outputs. */
wlr_cursor_set_surface(server->cursor, event->surface,
event->hotspot_x, event->hotspot_y);
}
}
void seat_request_set_selection(struct wl_listener *listener, void *data)
{
struct server *server =
wl_container_of(listener, server, request_set_selection);
struct wlr_seat_request_set_selection_event *event = data;
wlr_seat_set_selection(server->seat, event->source, event->serial);
}
static void process_cursor_move(struct server *server, uint32_t time)
{
/* Move the grabbed view to the new position. */
double dx = server->cursor->x - server->grab_x;
double dy = server->cursor->y - server->grab_y;
server->grabbed_view->x = server->grab_box.x + dx;
server->grabbed_view->y = server->grab_box.y + dy;
if (server->grabbed_view->type != LAB_XWAYLAND_VIEW)
return;
struct view *view = server->grabbed_view;
wlr_xwayland_surface_configure(view->xwayland_surface, view->x, view->y,
view->xwayland_surface->width,
view->xwayland_surface->height);
}
static void process_cursor_resize(struct server *server, uint32_t time)
{
/*
* TODO: Wait for the client to prepare a buffer at the new size, then
* commit any movement that was prepared.
*/
double dx = server->cursor->x - server->grab_x;
double dy = server->cursor->y - server->grab_y;
struct view *view = server->grabbed_view;
struct wlr_box new_view_geo = view_geometry(view);
if (server->resize_edges & WLR_EDGE_TOP) {
new_view_geo.y = server->grab_box.y + dy;
new_view_geo.height = server->grab_box.height - dy;
} else if (server->resize_edges & WLR_EDGE_BOTTOM) {
new_view_geo.height = server->grab_box.height + dy;
}
if (server->resize_edges & WLR_EDGE_LEFT) {
new_view_geo.x = server->grab_box.x + dx;
new_view_geo.width = server->grab_box.width - dx;
} else if (server->resize_edges & WLR_EDGE_RIGHT) {
new_view_geo.width = server->grab_box.width + dx;
}
if ((new_view_geo.height < MIN_VIEW_HEIGHT) ||
(new_view_geo.width < MIN_VIEW_WIDTH))
return;
/* Move */
view->x = new_view_geo.x;
view->y = new_view_geo.y;
/* Resize */
view_resize(view, new_view_geo);
}
static void process_cursor_motion(struct server *server, uint32_t time)
{
/* If the mode is non-passthrough, delegate to those functions. */
if (server->cursor_mode == TINYWL_CURSOR_MOVE) {
process_cursor_move(server, time);
return;
} else if (server->cursor_mode == TINYWL_CURSOR_RESIZE) {
process_cursor_resize(server, time);
return;
}
/* Otherwise, find the view under the pointer and send the event along.
*/
double sx, sy;
struct wlr_seat *seat = server->seat;
struct wlr_surface *surface = NULL;
int view_area;
struct view *view = view_at(server, server->cursor->x,
server->cursor->y, &surface, &sx, &sy,
&view_area);
if (!view) {
/* If there's no view under the cursor, set the cursor image to
* a default. This is what makes the cursor image appear when
* you move it around the screen, not over any views. */
wlr_xcursor_manager_set_cursor_image(
server->cursor_mgr, "left_ptr", server->cursor);
}
switch (view_area) {
case LAB_DECO_PART_TOP:
wlr_xcursor_manager_set_cursor_image(
server->cursor_mgr, "left_ptr", server->cursor);
break;
case LAB_DECO_PART_LEFT:
wlr_xcursor_manager_set_cursor_image(
server->cursor_mgr, "left_side", server->cursor);
break;
}
if (surface) {
bool focus_changed = seat->pointer_state.focused_surface !=
surface;
/*
* "Enter" the surface if necessary. This lets the client know
* that the cursor has entered one of its surfaces.
*
* Note that this gives the surface "pointer focus", which is
* distinct from keyboard focus. You get pointer focus by moving
* the pointer over a window.
*/
wlr_seat_pointer_notify_enter(seat, surface, sx, sy);
if (!focus_changed) {
/* The enter event contains coordinates, so we only need
* to notify on motion if the focus did not change. */
wlr_seat_pointer_notify_motion(seat, time, sx, sy);
}
} else {
/* Clear pointer focus so future button events and such are not
* sent to the last client to have the cursor over it. */
wlr_seat_pointer_clear_focus(seat);
}
}
void server_cursor_motion(struct wl_listener *listener, void *data)
{
/* This event is forwarded by the cursor when a pointer emits a
* _relative_ pointer motion event (i.e. a delta) */
struct server *server =
wl_container_of(listener, server, cursor_motion);
struct wlr_event_pointer_motion *event = data;
/* The cursor doesn't move unless we tell it to. The cursor
* automatically handles constraining the motion to the output layout,
* as well as any special configuration applied for the specific input
* device which generated the event. You can pass NULL for the device if
* you want to move the cursor around without any input. */
wlr_cursor_move(server->cursor, event->device, event->delta_x,
event->delta_y);
process_cursor_motion(server, event->time_msec);
}
void server_cursor_motion_absolute(struct wl_listener *listener, void *data)
{
/* This event is forwarded by the cursor when a pointer emits an
* _absolute_ motion event, from 0..1 on each axis. This happens, for
* example, when wlroots is running under a Wayland window rather than
* KMS+DRM, and you move the mouse over the window. You could enter the
* window from any edge, so we have to warp the mouse there. There is
* also some hardware which emits these events. */
struct server *server =
wl_container_of(listener, server, cursor_motion_absolute);
struct wlr_event_pointer_motion_absolute *event = data;
wlr_cursor_warp_absolute(server->cursor, event->device, event->x,
event->y);
process_cursor_motion(server, event->time_msec);
}
void server_cursor_button(struct wl_listener *listener, void *data)
{
/* This event is forwarded by the cursor when a pointer emits a button
* event. */
struct server *server =
wl_container_of(listener, server, cursor_button);
struct wlr_event_pointer_button *event = data;
/* Notify the client with pointer focus that a button press has occurred
*/
wlr_seat_pointer_notify_button(server->seat, event->time_msec,
event->button, event->state);
double sx, sy;
struct wlr_surface *surface;
int view_area;
struct view *view = view_at(server, server->cursor->x,
server->cursor->y, &surface, &sx, &sy,
&view_area);
if (event->state == WLR_BUTTON_RELEASED) {
/* If you released any buttons, we exit interactive move/resize
* mode. */
server->cursor_mode = TINYWL_CURSOR_PASSTHROUGH;
} else {
/* Focus that client if the button was _pressed_ */
view_focus(view);
switch (view_area) {
case LAB_DECO_PART_TOP:
begin_interactive(view, TINYWL_CURSOR_MOVE, 0);
break;
case LAB_DECO_PART_LEFT:
begin_interactive(view, TINYWL_CURSOR_RESIZE,
WLR_EDGE_LEFT);
break;
}
}
}
void server_cursor_axis(struct wl_listener *listener, void *data)
{
/* This event is forwarded by the cursor when a pointer emits an axis
* event, for example when you move the scroll wheel. */
struct server *server = wl_container_of(listener, server, cursor_axis);
struct wlr_event_pointer_axis *event = data;
/* Notify the client with pointer focus of the axis event. */
wlr_seat_pointer_notify_axis(server->seat, event->time_msec,
event->orientation, event->delta,
event->delta_discrete, event->source);
}
void server_cursor_frame(struct wl_listener *listener, void *data)
{
/* This event is forwarded by the cursor when a pointer emits an frame
* event. Frame events are sent after regular pointer events to group
* multiple events together. For instance, two axis events may happen at
* the
* same time, in which case a frame event won't be sent in between. */
struct server *server = wl_container_of(listener, server, cursor_frame);
/* Notify the client with pointer focus of the frame event. */
wlr_seat_pointer_notify_frame(server->seat);
}
void server_new_output(struct wl_listener *listener, void *data)
{
/* This event is rasied by the backend when a new output (aka a display
* or monitor) becomes available. */
struct server *server = wl_container_of(listener, server, new_output);
struct wlr_output *wlr_output = data;
/*
* Some backends don't have modes. DRM+KMS does, and we need to set a
* mode before we can use the output. The mode is a tuple of (width,
* height, refresh rate), and each monitor supports only a specific set
* of modes. We just pick the monitor's preferred mode.
* TODO: support user configuration
*/
if (!wl_list_empty(&wlr_output->modes)) {
struct wlr_output_mode *mode =
wlr_output_preferred_mode(wlr_output);
wlr_output_set_mode(wlr_output, mode);
wlr_output_enable(wlr_output, true);
if (!wlr_output_commit(wlr_output)) {
return;
}
}
/* Allocates and configures our state for this output */
struct output *output = calloc(1, sizeof(struct output));
output->wlr_output = wlr_output;
output->server = server;
/* Sets up a listener for the frame notify event. */
output->frame.notify = output_frame;
wl_signal_add(&wlr_output->events.frame, &output->frame);
wl_list_insert(&server->outputs, &output->link);
/* Adds this to the output layout. The add_auto function arranges
* outputs from left-to-right in the order they appear. A more
* sophisticated compositor would let the user configure the arrangement
* of outputs in the layout.
*
* The output layout utility automatically adds a wl_output global to
* the display, which Wayland clients can see to find out information
* about the output (such as DPI, scale factor, manufacturer, etc).
*/
wlr_output_layout_add_auto(server->output_layout, wlr_output);
}

220
src/view.c Normal file
View file

@ -0,0 +1,220 @@
#include "labwc.h"
struct wlr_box view_get_surface_geometry(struct view *view)
{
struct wlr_box box = { 0 };
switch (view->type) {
case LAB_XDG_SHELL_VIEW:
wlr_xdg_surface_get_geometry(view->xdg_surface, &box);
break;
case LAB_XWAYLAND_VIEW:
box.width = view->xwayland_surface->width;
box.height = view->xwayland_surface->height;
break;
}
return box;
}
/* Get geometry relative to screen */
struct wlr_box view_geometry(struct view *view)
{
struct wlr_box b = view_get_surface_geometry(view);
/* Add XDG view invisible border if it exists */
b.width += 2 * b.x;
b.height += 2 * b.y;
/* Make co-ordinates relative to screen */
b.x = view->x;
b.y = view->y;
return b;
}
void view_resize(struct view *view, struct wlr_box geo)
{
struct wlr_box border = view_get_surface_geometry(view);
switch (view->type) {
case LAB_XDG_SHELL_VIEW:
wlr_xdg_toplevel_set_size(view->xdg_surface,
geo.width - 2 * border.x,
geo.height - 2 * border.y);
break;
case LAB_XWAYLAND_VIEW:
wlr_xwayland_surface_configure(view->xwayland_surface, view->x,
view->y, geo.width, geo.height);
break;
default:
break;
}
}
static bool is_toplevel(struct view *view)
{
switch (view->type) {
case LAB_XDG_SHELL_VIEW:
return view->xdg_surface->role == WLR_XDG_SURFACE_ROLE_TOPLEVEL;
case LAB_XWAYLAND_VIEW:
return xwl_nr_parents(view) > 0 ? false : true;
}
return false;
}
bool view_want_deco(struct view *view)
{
if (view->type != LAB_XWAYLAND_VIEW)
return false;
if (!is_toplevel(view))
return false;
if (view->xwayland_surface->override_redirect)
return false;
if (view->xwayland_surface->decorations !=
WLR_XWAYLAND_SURFACE_DECORATIONS_ALL)
return false;
return true;
}
static void move_to_front(struct view *view)
{
wl_list_remove(&view->link);
wl_list_insert(&view->server->views, &view->link);
}
/* Activate/deactivate toplevel surface */
static void set_activated(struct wlr_surface *surface, bool activated)
{
if (!surface)
return;
if (wlr_surface_is_xdg_surface(surface)) {
struct wlr_xdg_surface *previous;
previous = wlr_xdg_surface_from_wlr_surface(surface);
wlr_xdg_toplevel_set_activated(previous, activated);
} else {
struct wlr_xwayland_surface *previous;
previous = wlr_xwayland_surface_from_wlr_surface(surface);
wlr_xwayland_surface_activate(previous, activated);
}
}
void view_focus(struct view *view)
{
/* Note: this function only deals with keyboard focus. */
if (!view || !view->surface)
return;
struct server *server = view->server;
struct wlr_seat *seat = server->seat;
struct wlr_surface *prev_surface;
prev_surface = seat->keyboard_state.focused_surface;
if (prev_surface == view->surface) {
/* Don't re-focus an already focused surface. */
return;
}
if (view->type == LAB_XWAYLAND_VIEW) {
/* Don't focus on menus, etc */
move_to_front(view);
if (!wlr_xwayland_or_surface_wants_focus(
view->xwayland_surface)) {
return;
}
}
if (prev_surface)
set_activated(seat->keyboard_state.focused_surface, false);
struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(seat);
move_to_front(view);
set_activated(view->surface, true);
/*
* Tell the seat to have the keyboard enter this surface. wlroots will
* keep track of this and automatically send key events to the
* appropriate clients without additional work on your part.
*/
wlr_seat_keyboard_notify_enter(seat, view->surface, keyboard->keycodes,
keyboard->num_keycodes,
&keyboard->modifiers);
}
struct view *view_front_toplevel(struct server *server)
{
struct view *view;
wl_list_for_each (view, &server->views, link) {
if (!view->been_mapped)
continue;
if (is_toplevel(view))
return view;
}
return NULL;
}
struct view *next_toplevel(struct view *current)
{
/* FIXME: write nr_toplevels() */
struct view *view = current;
do {
view = wl_container_of(view->link.next, view, link);
} while (!view->been_mapped || !is_toplevel(view));
return view;
}
static bool _view_at(struct view *view, double lx, double ly,
struct wlr_surface **surface, double *sx, double *sy)
{
/*
* XDG toplevels may have nested surfaces, such as popup windows for
* context menus or tooltips. This function tests if any of those are
* underneath the coordinates lx and ly (in output Layout Coordinates).
* If so, it sets the surface pointer to that wlr_surface and the sx and
* sy coordinates to the coordinates relative to that surface's top-left
* corner.
*/
double view_sx = lx - view->x;
double view_sy = ly - view->y;
double _sx, _sy;
struct wlr_surface *_surface = NULL;
switch (view->type) {
case LAB_XDG_SHELL_VIEW:
_surface = wlr_xdg_surface_surface_at(
view->xdg_surface, view_sx, view_sy, &_sx, &_sy);
break;
case LAB_XWAYLAND_VIEW:
if (!view->xwayland_surface->surface)
return false;
_surface =
wlr_surface_surface_at(view->xwayland_surface->surface,
view_sx, view_sy, &_sx, &_sy);
break;
}
if (_surface) {
*sx = _sx;
*sy = _sy;
*surface = _surface;
return true;
}
return false;
}
struct view *view_at(struct server *server, double lx, double ly,
struct wlr_surface **surface, double *sx, double *sy,
int *view_area)
{
/*
* This iterates over all of our surfaces and attempts to find one under
* the cursor. It relies on server->views being ordered from
* top-to-bottom.
*/
struct view *view;
wl_list_for_each (view, &server->views, link) {
if (_view_at(view, lx, ly, surface, sx, sy))
return view;
if (!view_want_deco(view))
continue;
if (deco_at(view, lx, ly) == LAB_DECO_PART_TOP) {
*view_area = LAB_DECO_PART_TOP;
return view;
}
if (deco_at(view, lx, ly) == LAB_DECO_PART_LEFT) {
*view_area = LAB_DECO_PART_LEFT;
return view;
}
}
return NULL;
}

129
src/xdg.c Normal file
View file

@ -0,0 +1,129 @@
#include "labwc.h"
struct xdg_deco {
struct wlr_xdg_toplevel_decoration_v1 *wlr_decoration;
struct server *server;
struct wl_listener destroy;
struct wl_listener request_mode;
};
static void xdg_deco_destroy(struct wl_listener *listener, void *data)
{
struct xdg_deco *xdg_deco =
wl_container_of(listener, xdg_deco, destroy);
wl_list_remove(&xdg_deco->destroy.link);
wl_list_remove(&xdg_deco->request_mode.link);
free(xdg_deco);
}
static void xdg_deco_request_mode(struct wl_listener *listener, void *data)
{
struct xdg_deco *xdg_deco;
xdg_deco = wl_container_of(listener, xdg_deco, request_mode);
enum wlr_xdg_toplevel_decoration_v1_mode mode;
if (LAB_DISABLE_CSD)
mode = WLR_XDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE;
else
mode = WLR_XDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE;
wlr_xdg_toplevel_decoration_v1_set_mode(xdg_deco->wlr_decoration, mode);
}
void xdg_toplevel_decoration(struct wl_listener *listener, void *data)
{
struct server *server =
wl_container_of(listener, server, xdg_toplevel_decoration);
struct wlr_xdg_toplevel_decoration_v1 *wlr_decoration = data;
struct xdg_deco *xdg_deco = calloc(1, sizeof(struct xdg_deco));
if (!xdg_deco)
return;
xdg_deco->wlr_decoration = wlr_decoration;
xdg_deco->server = server;
xdg_deco->destroy.notify = xdg_deco_destroy;
wl_signal_add(&wlr_decoration->events.destroy, &xdg_deco->destroy);
xdg_deco->request_mode.notify = xdg_deco_request_mode;
wl_signal_add(&wlr_decoration->events.request_mode,
&xdg_deco->request_mode);
xdg_deco_request_mode(&xdg_deco->request_mode, wlr_decoration);
}
void xdg_surface_map(struct wl_listener *listener, void *data)
{
/* Called when the surface is mapped, or ready to display on-screen. */
struct view *view = wl_container_of(listener, view, map);
view->mapped = true;
view->been_mapped = true;
view->surface = view->xdg_surface->surface;
view_focus(view);
}
void xdg_surface_unmap(struct wl_listener *listener, void *data)
{
struct view *view = wl_container_of(listener, view, unmap);
view->mapped = false;
view_focus(next_toplevel(view));
}
void xdg_surface_destroy(struct wl_listener *listener, void *data)
{
struct view *view = wl_container_of(listener, view, destroy);
wl_list_remove(&view->link);
free(view);
}
void xdg_toplevel_request_move(struct wl_listener *listener, void *data)
{
/* This event is raised when a client would like to begin an interactive
* move, typically because the user clicked on their client-side
* decorations. Note that a more sophisticated compositor should check
* the provied serial against a list of button press serials sent to
* this
* client, to prevent the client from requesting this whenever they
* want. */
struct view *view = wl_container_of(listener, view, request_move);
begin_interactive(view, TINYWL_CURSOR_MOVE, 0);
}
void xdg_toplevel_request_resize(struct wl_listener *listener, void *data)
{
/* This event is raised when a client would like to begin an interactive
* resize, typically because the user clicked on their client-side
* decorations. Note that a more sophisticated compositor should check
* the provied serial against a list of button press serials sent to
* this
* client, to prevent the client from requesting this whenever they
* want. */
struct wlr_xdg_toplevel_resize_event *event = data;
struct view *view = wl_container_of(listener, view, request_resize);
begin_interactive(view, TINYWL_CURSOR_RESIZE, event->edges);
}
void xdg_surface_new(struct wl_listener *listener, void *data)
{
struct server *server =
wl_container_of(listener, server, new_xdg_surface);
struct wlr_xdg_surface *xdg_surface = data;
if (xdg_surface->role != WLR_XDG_SURFACE_ROLE_TOPLEVEL) {
return;
}
struct view *view = calloc(1, sizeof(struct view));
view->server = server;
view->type = LAB_XDG_SHELL_VIEW;
view->xdg_surface = xdg_surface;
view->map.notify = xdg_surface_map;
wl_signal_add(&xdg_surface->events.map, &view->map);
view->unmap.notify = xdg_surface_unmap;
wl_signal_add(&xdg_surface->events.unmap, &view->unmap);
view->destroy.notify = xdg_surface_destroy;
wl_signal_add(&xdg_surface->events.destroy, &view->destroy);
struct wlr_xdg_toplevel *toplevel = xdg_surface->toplevel;
view->request_move.notify = xdg_toplevel_request_move;
wl_signal_add(&toplevel->events.request_move, &view->request_move);
view->request_resize.notify = xdg_toplevel_request_resize;
wl_signal_add(&toplevel->events.request_resize, &view->request_resize);
wl_list_insert(&server->views, &view->link);
}

98
src/xwl.c Normal file
View file

@ -0,0 +1,98 @@
#include "labwc.h"
int xwl_nr_parents(struct view *view)
{
struct wlr_xwayland_surface *s = view->xwayland_surface;
int i = 0;
if (!s) {
fprintf(stderr, "warn: (%s) no xwayland surface\n", __func__);
return -1;
}
while (s->parent) {
s = s->parent;
++i;
}
return i;
}
static void position(struct view *view)
{
struct wlr_box box;
if (!view_want_deco(view))
return;
if (view->x || view->y)
return;
box = deco_box(view, LAB_DECO_PART_TOP);
view->y = box.height;
box = deco_box(view, LAB_DECO_PART_LEFT);
view->x = box.width;
wlr_xwayland_surface_configure(view->xwayland_surface, view->x, view->y,
view->xwayland_surface->width,
view->xwayland_surface->height);
}
void xwl_surface_map(struct wl_listener *listener, void *data)
{
struct view *view = wl_container_of(listener, view, map);
view->mapped = true;
view->x = view->xwayland_surface->x;
view->y = view->xwayland_surface->y;
view->surface = view->xwayland_surface->surface;
if (!view->been_mapped)
position(view);
view->been_mapped = true;
view_focus(view);
}
void xwl_surface_unmap(struct wl_listener *listener, void *data)
{
struct view *view = wl_container_of(listener, view, unmap);
view->mapped = false;
/*
* Note that if 'view' is not a toplevel view, the 'front' toplevel view
* will be focussed on; but if 'view' is a toplevel view, the 'next'
* will be focussed on.
*/
view_focus(next_toplevel(view));
}
void xwl_surface_destroy(struct wl_listener *listener, void *data)
{
struct view *view = wl_container_of(listener, view, destroy);
wl_list_remove(&view->link);
free(view);
}
void xwl_surface_configure(struct wl_listener *listener, void *data)
{
struct view *view = wl_container_of(listener, view, request_configure);
struct wlr_xwayland_surface_configure_event *event = data;
wlr_xwayland_surface_configure(view->xwayland_surface, event->x,
event->y, event->width, event->height);
}
void xwl_surface_new(struct wl_listener *listener, void *data)
{
struct server *server =
wl_container_of(listener, server, new_xwayland_surface);
struct wlr_xwayland_surface *xwayland_surface = data;
wlr_xwayland_surface_ping(xwayland_surface);
struct view *view = calloc(1, sizeof(struct view));
view->server = server;
view->type = LAB_XWAYLAND_VIEW;
view->xwayland_surface = xwayland_surface;
view->map.notify = xwl_surface_map;
wl_signal_add(&xwayland_surface->events.map, &view->map);
view->unmap.notify = xwl_surface_unmap;
wl_signal_add(&xwayland_surface->events.unmap, &view->unmap);
view->destroy.notify = xwl_surface_destroy;
wl_signal_add(&xwayland_surface->events.destroy, &view->destroy);
view->request_configure.notify = xwl_surface_configure;
wl_signal_add(&xwayland_surface->events.request_configure,
&view->request_configure);
wl_list_insert(&server->views, &view->link);
}