blob: e5c3ef9ee64d52d2ad4fc803cfef5058984cead2 [file] [log] [blame]
/*
* Copyright © 2010-2012 Intel Corporation
* Copyright © 2011-2012 Collabora, Ltd.
* Copyright © 2013 Raspberry Pi Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "config.h"
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <linux/input.h>
#include <assert.h>
#include <signal.h>
#include <math.h>
#include <sys/types.h>
#include "shell.h"
#include "compositor/weston.h"
#include "weston-desktop-shell-server-protocol.h"
#include "shared/config-parser.h"
#include "shared/helpers.h"
#include "shared/timespec-util.h"
#include "libweston-desktop/libweston-desktop.h"
#define DEFAULT_NUM_WORKSPACES 1
#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
struct focus_state {
struct desktop_shell *shell;
struct weston_seat *seat;
struct workspace *ws;
struct weston_surface *keyboard_focus;
struct wl_list link;
struct wl_listener seat_destroy_listener;
struct wl_listener surface_destroy_listener;
};
/*
* Surface stacking and ordering.
*
* This is handled using several linked lists of surfaces, organised into
* ‘layers’. The layers are ordered, and each of the surfaces in one layer are
* above all of the surfaces in the layer below. The set of layers is static and
* in the following order (top-most first):
* • Lock layer (only ever displayed on its own)
* • Cursor layer
* • Input panel layer
* • Fullscreen layer
* • Panel layer
* • Workspace layers
* • Background layer
*
* The list of layers may be manipulated to remove whole layers of surfaces from
* display. For example, when locking the screen, all layers except the lock
* layer are removed.
*
* A surface’s layer is modified on configuring the surface, in
* set_surface_type() (which is only called when the surface’s type change is
* _committed_). If a surface’s type changes (e.g. when making a window
* fullscreen) its layer changes too.
*
* In order to allow popup and transient surfaces to be correctly stacked above
* their parent surfaces, each surface tracks both its parent surface, and a
* linked list of its children. When a surface’s layer is updated, so are the
* layers of its children. Note that child surfaces are *not* the same as
* subsurfaces — child/parent surfaces are purely for maintaining stacking
* order.
*
* The children_link list of siblings of a surface (i.e. those surfaces which
* have the same parent) only contains weston_surfaces which have a
* shell_surface. Stacking is not implemented for non-shell_surface
* weston_surfaces. This means that the following implication does *not* hold:
* (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
*/
struct shell_surface {
struct wl_signal destroy_signal;
struct weston_desktop_surface *desktop_surface;
struct weston_view *view;
int32_t last_width, last_height;
struct desktop_shell *shell;
int32_t saved_x, saved_y;
bool saved_position_valid;
bool saved_rotation_valid;
int unresponsive, grabbed;
uint32_t resize_edges;
struct {
struct weston_transform transform;
struct weston_matrix rotation;
} rotation;
struct {
struct weston_transform transform; /* matrix from x, y */
struct weston_view *black_view;
} fullscreen;
struct weston_transform workspace_transform;
struct weston_output *fullscreen_output;
struct weston_output *output;
struct wl_listener output_destroy_listener;
struct surface_state {
bool fullscreen;
bool maximized;
bool lowered;
} state;
struct {
bool is_set;
int32_t x;
int32_t y;
} xwayland;
int focus_count;
bool destroying;
};
struct shell_grab {
struct weston_pointer_grab grab;
struct shell_surface *shsurf;
struct wl_listener shsurf_destroy_listener;
};
struct shell_touch_grab {
struct weston_touch_grab grab;
struct shell_surface *shsurf;
struct wl_listener shsurf_destroy_listener;
struct weston_touch *touch;
};
struct weston_move_grab {
struct shell_grab base;
wl_fixed_t dx, dy;
bool client_initiated;
};
struct weston_touch_move_grab {
struct shell_touch_grab base;
int active;
wl_fixed_t dx, dy;
};
struct rotate_grab {
struct shell_grab base;
struct weston_matrix rotation;
struct {
float x;
float y;
} center;
};
struct shell_seat {
struct weston_seat *seat;
struct wl_listener seat_destroy_listener;
struct weston_surface *focused_surface;
struct wl_listener caps_changed_listener;
struct wl_listener pointer_focus_listener;
struct wl_listener keyboard_focus_listener;
};
static struct desktop_shell *
shell_surface_get_shell(struct shell_surface *shsurf);
static void
set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
static void
surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
static void
shell_fade_startup(struct desktop_shell *shell);
static void
shell_fade(struct desktop_shell *shell, enum fade_type type);
static struct shell_seat *
get_shell_seat(struct weston_seat *seat);
static void
get_output_panel_size(struct desktop_shell *shell,
struct weston_output *output,
int *width, int *height);
static void
shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
static int
shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
{
const char *t, *c;
struct weston_desktop_surface *desktop_surface =
weston_surface_get_desktop_surface(surface);
t = weston_desktop_surface_get_title(desktop_surface);
c = weston_desktop_surface_get_app_id(desktop_surface);
return snprintf(buf, len, "%s window%s%s%s%s%s",
"top-level",
t ? " '" : "", t ?: "", t ? "'" : "",
c ? " of " : "", c ?: "");
}
static void
destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
{
struct shell_grab *grab;
grab = container_of(listener, struct shell_grab,
shsurf_destroy_listener);
grab->shsurf = NULL;
}
struct weston_view *
get_default_view(struct weston_surface *surface)
{
struct shell_surface *shsurf;
struct weston_view *view;
if (!surface || wl_list_empty(&surface->views))
return NULL;
shsurf = get_shell_surface(surface);
if (shsurf)
return shsurf->view;
wl_list_for_each(view, &surface->views, surface_link)
if (weston_view_is_mapped(view))
return view;
return container_of(surface->views.next, struct weston_view, surface_link);
}
static void
shell_grab_start(struct shell_grab *grab,
const struct weston_pointer_grab_interface *interface,
struct shell_surface *shsurf,
struct weston_pointer *pointer,
enum weston_desktop_shell_cursor cursor)
{
struct desktop_shell *shell = shsurf->shell;
weston_seat_break_desktop_grabs(pointer->seat);
grab->grab.interface = interface;
grab->shsurf = shsurf;
grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
wl_signal_add(&shsurf->destroy_signal,
&grab->shsurf_destroy_listener);
shsurf->grabbed = 1;
weston_pointer_start_grab(pointer, &grab->grab);
if (shell->child.desktop_shell) {
weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
cursor);
weston_pointer_set_focus(pointer,
get_default_view(shell->grab_surface),
wl_fixed_from_int(0),
wl_fixed_from_int(0));
}
}
static void
get_panel_size(struct desktop_shell *shell,
struct weston_view *view,
int *width,
int *height)
{
float x1, y1;
float x2, y2;
weston_view_to_global_float(view, 0, 0, &x1, &y1);
weston_view_to_global_float(view,
view->surface->width,
view->surface->height,
&x2, &y2);
*width = (int)(x2 - x1);
*height = (int)(y2 - y1);
}
static void
get_output_panel_size(struct desktop_shell *shell,
struct weston_output *output,
int *width,
int *height)
{
struct weston_view *view;
*width = 0;
*height = 0;
if (!output)
return;
wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
if (view->surface->output == output) {
get_panel_size(shell, view, width, height);
return;
}
}
/* the correct view wasn't found */
}
static void
get_output_work_area(struct desktop_shell *shell,
struct weston_output *output,
pixman_rectangle32_t *area)
{
int32_t panel_width = 0, panel_height = 0;
if (!output) {
area->x = 0;
area->y = 0;
area->width = 0;
area->height = 0;
return;
}
area->x = output->x;
area->y = output->y;
get_output_panel_size(shell, output, &panel_width, &panel_height);
switch (shell->panel_position) {
case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
default:
area->y += panel_height;
/* fallthrough */
case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
area->width = output->width;
area->height = output->height - panel_height;
break;
case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
area->x += panel_width;
/* fallthrough */
case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
area->width = output->width - panel_width;
area->height = output->height;
break;
}
}
static void
shell_grab_end(struct shell_grab *grab)
{
if (grab->shsurf) {
wl_list_remove(&grab->shsurf_destroy_listener.link);
grab->shsurf->grabbed = 0;
if (grab->shsurf->resize_edges) {
grab->shsurf->resize_edges = 0;
}
}
weston_pointer_end_grab(grab->grab.pointer);
}
static void
shell_touch_grab_start(struct shell_touch_grab *grab,
const struct weston_touch_grab_interface *interface,
struct shell_surface *shsurf,
struct weston_touch *touch)
{
struct desktop_shell *shell = shsurf->shell;
weston_seat_break_desktop_grabs(touch->seat);
grab->grab.interface = interface;
grab->shsurf = shsurf;
grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
wl_signal_add(&shsurf->destroy_signal,
&grab->shsurf_destroy_listener);
grab->touch = touch;
shsurf->grabbed = 1;
weston_touch_start_grab(touch, &grab->grab);
if (shell->child.desktop_shell)
weston_touch_set_focus(touch,
get_default_view(shell->grab_surface));
}
static void
shell_touch_grab_end(struct shell_touch_grab *grab)
{
if (grab->shsurf) {
wl_list_remove(&grab->shsurf_destroy_listener.link);
grab->shsurf->grabbed = 0;
}
weston_touch_end_grab(grab->touch);
}
static void
center_on_output(struct weston_view *view,
struct weston_output *output);
static enum weston_keyboard_modifier
get_modifier(char *modifier)
{
if (!modifier)
return MODIFIER_SUPER;
if (!strcmp("ctrl", modifier))
return MODIFIER_CTRL;
else if (!strcmp("alt", modifier))
return MODIFIER_ALT;
else if (!strcmp("super", modifier))
return MODIFIER_SUPER;
else if (!strcmp("none", modifier))
return 0;
else
return MODIFIER_SUPER;
}
static enum animation_type
get_animation_type(char *animation)
{
if (!animation)
return ANIMATION_NONE;
if (!strcmp("zoom", animation))
return ANIMATION_ZOOM;
else if (!strcmp("fade", animation))
return ANIMATION_FADE;
else if (!strcmp("dim-layer", animation))
return ANIMATION_DIM_LAYER;
else
return ANIMATION_NONE;
}
static void
shell_configuration(struct desktop_shell *shell)
{
struct weston_config_section *section;
char *s, *client;
int allow_zap;
section = weston_config_get_section(wet_get_config(shell->compositor),
"shell", NULL, NULL);
client = wet_get_binary_path(WESTON_SHELL_CLIENT);
weston_config_section_get_string(section, "client", &s, client);
free(client);
shell->client = s;
weston_config_section_get_bool(section,
"allow-zap", &allow_zap, true);
shell->allow_zap = allow_zap;
weston_config_section_get_string(section,
"binding-modifier", &s, "super");
shell->binding_modifier = get_modifier(s);
free(s);
weston_config_section_get_string(section,
"exposay-modifier", &s, "none");
shell->exposay_modifier = get_modifier(s);
free(s);
weston_config_section_get_string(section, "animation", &s, "none");
shell->win_animation_type = get_animation_type(s);
free(s);
weston_config_section_get_string(section, "close-animation", &s, "fade");
shell->win_close_animation_type = get_animation_type(s);
free(s);
weston_config_section_get_string(section,
"startup-animation", &s, "fade");
shell->startup_animation_type = get_animation_type(s);
free(s);
if (shell->startup_animation_type == ANIMATION_ZOOM)
shell->startup_animation_type = ANIMATION_NONE;
weston_config_section_get_string(section, "focus-animation", &s, "none");
shell->focus_animation_type = get_animation_type(s);
free(s);
weston_config_section_get_uint(section, "num-workspaces",
&shell->workspaces.num,
DEFAULT_NUM_WORKSPACES);
}
struct weston_output *
get_default_output(struct weston_compositor *compositor)
{
if (wl_list_empty(&compositor->output_list))
return NULL;
return container_of(compositor->output_list.next,
struct weston_output, link);
}
static int
focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
{
return snprintf(buf, len, "focus highlight effect for output %s",
surface->output->name);
}
/* no-op func for checking focus surface */
static void
focus_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
{
}
static struct focus_surface *
get_focus_surface(struct weston_surface *surface)
{
if (surface->committed == focus_surface_committed)
return surface->committed_private;
else
return NULL;
}
static bool
is_focus_surface (struct weston_surface *es)
{
return (es->committed == focus_surface_committed);
}
static bool
is_focus_view (struct weston_view *view)
{
return is_focus_surface (view->surface);
}
static struct focus_surface *
create_focus_surface(struct weston_compositor *ec,
struct weston_output *output)
{
struct focus_surface *fsurf = NULL;
struct weston_surface *surface = NULL;
fsurf = malloc(sizeof *fsurf);
if (!fsurf)
return NULL;
fsurf->surface = weston_surface_create(ec);
surface = fsurf->surface;
if (surface == NULL) {
free(fsurf);
return NULL;
}
surface->committed = focus_surface_committed;
surface->output = output;
surface->is_mapped = true;
surface->committed_private = fsurf;
weston_surface_set_label_func(surface, focus_surface_get_label);
fsurf->view = weston_view_create(surface);
if (fsurf->view == NULL) {
weston_surface_destroy(surface);
free(fsurf);
return NULL;
}
weston_view_set_output(fsurf->view, output);
fsurf->view->is_mapped = true;
weston_surface_set_size(surface, output->width, output->height);
weston_view_set_position(fsurf->view, output->x, output->y);
weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
pixman_region32_fini(&surface->opaque);
pixman_region32_init_rect(&surface->opaque, output->x, output->y,
output->width, output->height);
pixman_region32_fini(&surface->input);
pixman_region32_init(&surface->input);
wl_list_init(&fsurf->workspace_transform.link);
return fsurf;
}
static void
focus_surface_destroy(struct focus_surface *fsurf)
{
weston_surface_destroy(fsurf->surface);
free(fsurf);
}
static void
focus_animation_done(struct weston_view_animation *animation, void *data)
{
struct workspace *ws = data;
ws->focus_animation = NULL;
}
static void
focus_state_destroy(struct focus_state *state)
{
wl_list_remove(&state->seat_destroy_listener.link);
wl_list_remove(&state->surface_destroy_listener.link);
free(state);
}
static void
focus_state_seat_destroy(struct wl_listener *listener, void *data)
{
struct focus_state *state = container_of(listener,
struct focus_state,
seat_destroy_listener);
wl_list_remove(&state->link);
focus_state_destroy(state);
}
static void
focus_state_surface_destroy(struct wl_listener *listener, void *data)
{
struct focus_state *state = container_of(listener,
struct focus_state,
surface_destroy_listener);
struct weston_surface *main_surface;
struct weston_view *next;
struct weston_view *view;
main_surface = weston_surface_get_main_surface(state->keyboard_focus);
next = NULL;
wl_list_for_each(view,
&state->ws->layer.view_list.link, layer_link.link) {
if (view->surface == main_surface)
continue;
if (is_focus_view(view))
continue;
if (!get_shell_surface(view->surface))
continue;
next = view;
break;
}
/* if the focus was a sub-surface, activate its main surface */
if (main_surface != state->keyboard_focus)
next = get_default_view(main_surface);
if (next) {
state->keyboard_focus = NULL;
activate(state->shell, next, state->seat,
WESTON_ACTIVATE_FLAG_CONFIGURE);
} else {
if (state->shell->focus_animation_type == ANIMATION_DIM_LAYER) {
if (state->ws->focus_animation)
weston_view_animation_destroy(state->ws->focus_animation);
state->ws->focus_animation = weston_fade_run(
state->ws->fsurf_front->view,
state->ws->fsurf_front->view->alpha, 0.0, 300,
focus_animation_done, state->ws);
}
wl_list_remove(&state->link);
focus_state_destroy(state);
}
}
static struct focus_state *
focus_state_create(struct desktop_shell *shell, struct weston_seat *seat,
struct workspace *ws)
{
struct focus_state *state;
state = malloc(sizeof *state);
if (state == NULL)
return NULL;
state->shell = shell;
state->keyboard_focus = NULL;
state->ws = ws;
state->seat = seat;
wl_list_insert(&ws->focus_list, &state->link);
state->seat_destroy_listener.notify = focus_state_seat_destroy;
state->surface_destroy_listener.notify = focus_state_surface_destroy;
wl_signal_add(&seat->destroy_signal,
&state->seat_destroy_listener);
wl_list_init(&state->surface_destroy_listener.link);
return state;
}
static struct focus_state *
ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
{
struct workspace *ws = get_current_workspace(shell);
struct focus_state *state;
wl_list_for_each(state, &ws->focus_list, link)
if (state->seat == seat)
break;
if (&state->link == &ws->focus_list)
state = focus_state_create(shell, seat, ws);
return state;
}
static void
focus_state_set_focus(struct focus_state *state,
struct weston_surface *surface)
{
if (state->keyboard_focus) {
wl_list_remove(&state->surface_destroy_listener.link);
wl_list_init(&state->surface_destroy_listener.link);
}
state->keyboard_focus = surface;
if (surface)
wl_signal_add(&surface->destroy_signal,
&state->surface_destroy_listener);
}
static void
restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
{
struct focus_state *state, *next;
struct weston_surface *surface;
struct wl_list pending_seat_list;
struct weston_seat *seat, *next_seat;
/* Temporarily steal the list of seats so that we can keep
* track of the seats we've already processed */
wl_list_init(&pending_seat_list);
wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
wl_list_init(&shell->compositor->seat_list);
wl_list_for_each_safe(state, next, &ws->focus_list, link) {
struct weston_keyboard *keyboard =
weston_seat_get_keyboard(state->seat);
wl_list_remove(&state->seat->link);
wl_list_insert(&shell->compositor->seat_list,
&state->seat->link);
if (!keyboard)
continue;
surface = state->keyboard_focus;
weston_keyboard_set_focus(keyboard, surface);
}
/* For any remaining seats that we don't have a focus state
* for we'll reset the keyboard focus to NULL */
wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
struct weston_keyboard *keyboard =
weston_seat_get_keyboard(seat);
wl_list_insert(&shell->compositor->seat_list, &seat->link);
if (!keyboard)
continue;
weston_keyboard_set_focus(keyboard, NULL);
}
}
static void
replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
struct weston_seat *seat)
{
struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
struct focus_state *state;
wl_list_for_each(state, &ws->focus_list, link) {
if (state->seat == seat) {
focus_state_set_focus(state, keyboard->focus);
return;
}
}
}
static void
drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
struct weston_surface *surface)
{
struct focus_state *state;
wl_list_for_each(state, &ws->focus_list, link)
if (state->keyboard_focus == surface)
focus_state_set_focus(state, NULL);
}
static void
animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
struct weston_view *from, struct weston_view *to)
{
struct weston_output *output;
bool focus_surface_created = false;
/* FIXME: Only support dim animation using two layers */
if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
return;
output = get_default_output(shell->compositor);
if (ws->fsurf_front == NULL && (from || to)) {
ws->fsurf_front = create_focus_surface(shell->compositor, output);
if (ws->fsurf_front == NULL)
return;
ws->fsurf_front->view->alpha = 0.0;
ws->fsurf_back = create_focus_surface(shell->compositor, output);
if (ws->fsurf_back == NULL) {
focus_surface_destroy(ws->fsurf_front);
return;
}
ws->fsurf_back->view->alpha = 0.0;
focus_surface_created = true;
} else {
weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
}
if (ws->focus_animation) {
weston_view_animation_destroy(ws->focus_animation);
ws->focus_animation = NULL;
}
if (to)
weston_layer_entry_insert(&to->layer_link,
&ws->fsurf_front->view->layer_link);
else if (from)
weston_layer_entry_insert(&ws->layer.view_list,
&ws->fsurf_front->view->layer_link);
if (focus_surface_created) {
ws->focus_animation = weston_fade_run(
ws->fsurf_front->view,
ws->fsurf_front->view->alpha, 0.4, 300,
focus_animation_done, ws);
} else if (from) {
weston_layer_entry_insert(&from->layer_link,
&ws->fsurf_back->view->layer_link);
ws->focus_animation = weston_stable_fade_run(
ws->fsurf_front->view, 0.0,
ws->fsurf_back->view, 0.4,
focus_animation_done, ws);
} else if (to) {
weston_layer_entry_insert(&ws->layer.view_list,
&ws->fsurf_back->view->layer_link);
ws->focus_animation = weston_stable_fade_run(
ws->fsurf_front->view, 0.0,
ws->fsurf_back->view, 0.4,
focus_animation_done, ws);
}
}
static void
workspace_destroy(struct workspace *ws)
{
struct focus_state *state, *next;
wl_list_for_each_safe(state, next, &ws->focus_list, link)
focus_state_destroy(state);
if (ws->fsurf_front)
focus_surface_destroy(ws->fsurf_front);
if (ws->fsurf_back)
focus_surface_destroy(ws->fsurf_back);
free(ws);
}
static void
seat_destroyed(struct wl_listener *listener, void *data)
{
struct weston_seat *seat = data;
struct focus_state *state, *next;
struct workspace *ws = container_of(listener,
struct workspace,
seat_destroyed_listener);
wl_list_for_each_safe(state, next, &ws->focus_list, link)
if (state->seat == seat)
wl_list_remove(&state->link);
}
static struct workspace *
workspace_create(struct desktop_shell *shell)
{
struct workspace *ws = malloc(sizeof *ws);
if (ws == NULL)
return NULL;
weston_layer_init(&ws->layer, shell->compositor);
wl_list_init(&ws->focus_list);
wl_list_init(&ws->seat_destroyed_listener.link);
ws->seat_destroyed_listener.notify = seat_destroyed;
ws->fsurf_front = NULL;
ws->fsurf_back = NULL;
ws->focus_animation = NULL;
return ws;
}
static int
workspace_is_empty(struct workspace *ws)
{
return wl_list_empty(&ws->layer.view_list.link);
}
static struct workspace *
get_workspace(struct desktop_shell *shell, unsigned int index)
{
struct workspace **pws = shell->workspaces.array.data;
assert(index < shell->workspaces.num);
pws += index;
return *pws;
}
struct workspace *
get_current_workspace(struct desktop_shell *shell)
{
return get_workspace(shell, shell->workspaces.current);
}
static void
activate_workspace(struct desktop_shell *shell, unsigned int index)
{
struct workspace *ws;
ws = get_workspace(shell, index);
weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
shell->workspaces.current = index;
}
static unsigned int
get_output_height(struct weston_output *output)
{
return abs(output->region.extents.y1 - output->region.extents.y2);
}
static void
view_translate(struct workspace *ws, struct weston_view *view, double d)
{
struct weston_transform *transform;
if (is_focus_view(view)) {
struct focus_surface *fsurf = get_focus_surface(view->surface);
transform = &fsurf->workspace_transform;
} else {
struct shell_surface *shsurf = get_shell_surface(view->surface);
transform = &shsurf->workspace_transform;
}
if (wl_list_empty(&transform->link))
wl_list_insert(view->geometry.transformation_list.prev,
&transform->link);
weston_matrix_init(&transform->matrix);
weston_matrix_translate(&transform->matrix,
0.0, d, 0.0);
weston_view_geometry_dirty(view);
}
static void
workspace_translate_out(struct workspace *ws, double fraction)
{
struct weston_view *view;
unsigned int height;
double d;
wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
height = get_output_height(view->surface->output);
d = height * fraction;
view_translate(ws, view, d);
}
}
static void
workspace_translate_in(struct workspace *ws, double fraction)
{
struct weston_view *view;
unsigned int height;
double d;
wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
height = get_output_height(view->surface->output);
if (fraction > 0)
d = -(height - height * fraction);
else
d = height + height * fraction;
view_translate(ws, view, d);
}
}
static void
reverse_workspace_change_animation(struct desktop_shell *shell,
unsigned int index,
struct workspace *from,
struct workspace *to)
{
shell->workspaces.current = index;
shell->workspaces.anim_to = to;
shell->workspaces.anim_from = from;
shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
shell->workspaces.anim_timestamp = (struct timespec) { 0 };
weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
weston_compositor_schedule_repaint(shell->compositor);
}
static void
workspace_deactivate_transforms(struct workspace *ws)
{
struct weston_view *view;
struct weston_transform *transform;
wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
if (is_focus_view(view)) {
struct focus_surface *fsurf = get_focus_surface(view->surface);
transform = &fsurf->workspace_transform;
} else {
struct shell_surface *shsurf = get_shell_surface(view->surface);
transform = &shsurf->workspace_transform;
}
if (!wl_list_empty(&transform->link)) {
wl_list_remove(&transform->link);
wl_list_init(&transform->link);
}
weston_view_geometry_dirty(view);
}
}
static void
finish_workspace_change_animation(struct desktop_shell *shell,
struct workspace *from,
struct workspace *to)
{
struct weston_view *view;
weston_compositor_schedule_repaint(shell->compositor);
/* Views that extend past the bottom of the output are still
* visible after the workspace animation ends but before its layer
* is hidden. In that case, we need to damage below those views so
* that the screen is properly repainted. */
wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
weston_view_damage_below(view);
wl_list_remove(&shell->workspaces.animation.link);
workspace_deactivate_transforms(from);
workspace_deactivate_transforms(to);
shell->workspaces.anim_to = NULL;
weston_layer_unset_position(&shell->workspaces.anim_from->layer);
}
static void
animate_workspace_change_frame(struct weston_animation *animation,
struct weston_output *output,
const struct timespec *time)
{
struct desktop_shell *shell =
container_of(animation, struct desktop_shell,
workspaces.animation);
struct workspace *from = shell->workspaces.anim_from;
struct workspace *to = shell->workspaces.anim_to;
int64_t t;
double x, y;
if (workspace_is_empty(from) && workspace_is_empty(to)) {
finish_workspace_change_animation(shell, from, to);
return;
}
if (timespec_is_zero(&shell->workspaces.anim_timestamp)) {
if (shell->workspaces.anim_current == 0.0)
shell->workspaces.anim_timestamp = *time;
else
timespec_add_msec(&shell->workspaces.anim_timestamp,
time,
/* Invers of movement function 'y' below. */
-(asin(1.0 - shell->workspaces.anim_current) *
DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
M_2_PI));
}
t = timespec_sub_to_msec(time, &shell->workspaces.anim_timestamp);
/*
* x = [0, π/2]
* y(x) = sin(x)
*/
x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
y = sin(x);
if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
weston_compositor_schedule_repaint(shell->compositor);
workspace_translate_out(from, shell->workspaces.anim_dir * y);
workspace_translate_in(to, shell->workspaces.anim_dir * y);
shell->workspaces.anim_current = y;
weston_compositor_schedule_repaint(shell->compositor);
}
else
finish_workspace_change_animation(shell, from, to);
}
static void
animate_workspace_change(struct desktop_shell *shell,
unsigned int index,
struct workspace *from,
struct workspace *to)
{
struct weston_output *output;
int dir;
if (index > shell->workspaces.current)
dir = -1;
else
dir = 1;
shell->workspaces.current = index;
shell->workspaces.anim_dir = dir;
shell->workspaces.anim_from = from;
shell->workspaces.anim_to = to;
shell->workspaces.anim_current = 0.0;
shell->workspaces.anim_timestamp = (struct timespec) { 0 };
output = container_of(shell->compositor->output_list.next,
struct weston_output, link);
wl_list_insert(&output->animation_list,
&shell->workspaces.animation.link);
weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
workspace_translate_in(to, 0);
restore_focus_state(shell, to);
weston_compositor_schedule_repaint(shell->compositor);
}
static void
update_workspace(struct desktop_shell *shell, unsigned int index,
struct workspace *from, struct workspace *to)
{
shell->workspaces.current = index;
weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
weston_layer_unset_position(&from->layer);
}
static void
change_workspace(struct desktop_shell *shell, unsigned int index)
{
struct workspace *from;
struct workspace *to;
struct focus_state *state;
if (index == shell->workspaces.current)
return;
/* Don't change workspace when there is any fullscreen surfaces. */
if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
return;
from = get_current_workspace(shell);
to = get_workspace(shell, index);
if (shell->workspaces.anim_from == to &&
shell->workspaces.anim_to == from) {
restore_focus_state(shell, to);
reverse_workspace_change_animation(shell, index, from, to);
return;
}
if (shell->workspaces.anim_to != NULL)
finish_workspace_change_animation(shell,
shell->workspaces.anim_from,
shell->workspaces.anim_to);
restore_focus_state(shell, to);
if (shell->focus_animation_type != ANIMATION_NONE) {
wl_list_for_each(state, &from->focus_list, link)
if (state->keyboard_focus)
animate_focus_change(shell, from,
get_default_view(state->keyboard_focus), NULL);
wl_list_for_each(state, &to->focus_list, link)
if (state->keyboard_focus)
animate_focus_change(shell, to,
NULL, get_default_view(state->keyboard_focus));
}
if (workspace_is_empty(to) && workspace_is_empty(from))
update_workspace(shell, index, from, to);
else
animate_workspace_change(shell, index, from, to);
}
static bool
workspace_has_only(struct workspace *ws, struct weston_surface *surface)
{
struct wl_list *list = &ws->layer.view_list.link;
struct wl_list *e;
if (wl_list_empty(list))
return false;
e = list->next;
if (e->next != list)
return false;
return container_of(e, struct weston_view, layer_link.link)->surface == surface;
}
static void
surface_keyboard_focus_lost(struct weston_surface *surface)
{
struct weston_compositor *compositor = surface->compositor;
struct weston_seat *seat;
struct weston_surface *focus;
wl_list_for_each(seat, &compositor->seat_list, link) {
struct weston_keyboard *keyboard =
weston_seat_get_keyboard(seat);
if (!keyboard)
continue;
focus = weston_surface_get_main_surface(keyboard->focus);
if (focus == surface)
weston_keyboard_set_focus(keyboard, NULL);
}
}
static void
take_surface_to_workspace_by_seat(struct desktop_shell *shell,
struct weston_seat *seat,
unsigned int index)
{
struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
struct weston_surface *surface;
struct weston_view *view;
struct shell_surface *shsurf;
struct workspace *from;
struct workspace *to;
struct focus_state *state;
surface = weston_surface_get_main_surface(keyboard->focus);
view = get_default_view(surface);
if (view == NULL ||
index == shell->workspaces.current ||
is_focus_view(view))
return;
from = get_current_workspace(shell);
to = get_workspace(shell, index);
weston_layer_entry_remove(&view->layer_link);
weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
shsurf = get_shell_surface(surface);
if (shsurf != NULL)
shell_surface_update_child_surface_layers(shsurf);
replace_focus_state(shell, to, seat);
drop_focus_state(shell, from, surface);
if (shell->workspaces.anim_from == to &&
shell->workspaces.anim_to == from) {
reverse_workspace_change_animation(shell, index, from, to);
return;
}
if (shell->workspaces.anim_to != NULL)
finish_workspace_change_animation(shell,
shell->workspaces.anim_from,
shell->workspaces.anim_to);
if (workspace_is_empty(from) &&
workspace_has_only(to, surface))
update_workspace(shell, index, from, to);
else {
if (shsurf != NULL &&
wl_list_empty(&shsurf->workspace_transform.link))
wl_list_insert(&shell->workspaces.anim_sticky_list,
&shsurf->workspace_transform.link);
animate_workspace_change(shell, index, from, to);
}
state = ensure_focus_state(shell, seat);
if (state != NULL)
focus_state_set_focus(state, surface);
}
static void
touch_move_grab_down(struct weston_touch_grab *grab,
const struct timespec *time,
int touch_id, wl_fixed_t x, wl_fixed_t y)
{
}
static void
touch_move_grab_up(struct weston_touch_grab *grab, const struct timespec *time,
int touch_id)
{
struct weston_touch_move_grab *move =
(struct weston_touch_move_grab *) container_of(
grab, struct shell_touch_grab, grab);
if (touch_id == 0)
move->active = 0;
if (grab->touch->num_tp == 0) {
shell_touch_grab_end(&move->base);
free(move);
}
}
static void
touch_move_grab_motion(struct weston_touch_grab *grab,
const struct timespec *time, int touch_id,
wl_fixed_t x, wl_fixed_t y)
{
struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
struct shell_surface *shsurf = move->base.shsurf;
struct weston_surface *es;
int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
if (!shsurf || !move->active)
return;
es = weston_desktop_surface_get_surface(shsurf->desktop_surface);
weston_view_set_position(shsurf->view, dx, dy);
weston_compositor_schedule_repaint(es->compositor);
}
static void
touch_move_grab_frame(struct weston_touch_grab *grab)
{
}
static void
touch_move_grab_cancel(struct weston_touch_grab *grab)
{
struct weston_touch_move_grab *move =
(struct weston_touch_move_grab *) container_of(
grab, struct shell_touch_grab, grab);
shell_touch_grab_end(&move->base);
free(move);
}
static const struct weston_touch_grab_interface touch_move_grab_interface = {
touch_move_grab_down,
touch_move_grab_up,
touch_move_grab_motion,
touch_move_grab_frame,
touch_move_grab_cancel,
};
static int
surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
{
struct weston_touch_move_grab *move;
if (!shsurf)
return -1;
if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
weston_desktop_surface_get_maximized(shsurf->desktop_surface))
return 0;
move = malloc(sizeof *move);
if (!move)
return -1;
move->active = 1;
move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
touch->grab_x;
move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
touch->grab_y;
shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
touch);
return 0;
}
static void
noop_grab_focus(struct weston_pointer_grab *grab)
{
}
static void
noop_grab_axis(struct weston_pointer_grab *grab,
const struct timespec *time,
struct weston_pointer_axis_event *event)
{
}
static void
noop_grab_axis_source(struct weston_pointer_grab *grab,
uint32_t source)
{
}
static void
noop_grab_frame(struct weston_pointer_grab *grab)
{
}
static void
constrain_position(struct weston_move_grab *move, int *cx, int *cy)
{
struct shell_surface *shsurf = move->base.shsurf;
struct weston_surface *surface =
weston_desktop_surface_get_surface(shsurf->desktop_surface);
struct weston_pointer *pointer = move->base.grab.pointer;
int x, y, bottom;
const int safety = 50;
pixman_rectangle32_t area;
struct weston_geometry geometry;
x = wl_fixed_to_int(pointer->x + move->dx);
y = wl_fixed_to_int(pointer->y + move->dy);
if (shsurf->shell->panel_position ==
WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
get_output_work_area(shsurf->shell, surface->output, &area);
geometry =
weston_desktop_surface_get_geometry(shsurf->desktop_surface);
bottom = y + geometry.height + geometry.y;
if (bottom - safety < area.y)
y = area.y + safety - geometry.height
- geometry.y;
if (move->client_initiated &&
y + geometry.y < area.y)
y = area.y - geometry.y;
}
*cx = x;
*cy = y;
}
static void
move_grab_motion(struct weston_pointer_grab *grab,
const struct timespec *time,
struct weston_pointer_motion_event *event)
{
struct weston_move_grab *move = (struct weston_move_grab *) grab;
struct weston_pointer *pointer = grab->pointer;
struct shell_surface *shsurf = move->base.shsurf;
struct weston_surface *surface;
int cx, cy;
weston_pointer_move(pointer, event);
if (!shsurf)
return;
surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
constrain_position(move, &cx, &cy);
weston_view_set_position(shsurf->view, cx, cy);
weston_compositor_schedule_repaint(surface->compositor);
}
static void
move_grab_button(struct weston_pointer_grab *grab,
const struct timespec *time, uint32_t button, uint32_t state_w)
{
struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
grab);
struct weston_pointer *pointer = grab->pointer;
enum wl_pointer_button_state state = state_w;
if (pointer->button_count == 0 &&
state == WL_POINTER_BUTTON_STATE_RELEASED) {
shell_grab_end(shell_grab);
free(grab);
}
}
static void
move_grab_cancel(struct weston_pointer_grab *grab)
{
struct shell_grab *shell_grab =
container_of(grab, struct shell_grab, grab);
shell_grab_end(shell_grab);
free(grab);
}
static const struct weston_pointer_grab_interface move_grab_interface = {
noop_grab_focus,
move_grab_motion,
move_grab_button,
noop_grab_axis,
noop_grab_axis_source,
noop_grab_frame,
move_grab_cancel,
};
static int
surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
bool client_initiated)
{
struct weston_move_grab *move;
if (!shsurf)
return -1;
if (shsurf->grabbed ||
weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
weston_desktop_surface_get_maximized(shsurf->desktop_surface))
return 0;
move = malloc(sizeof *move);
if (!move)
return -1;
move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
pointer->grab_x;
move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
pointer->grab_y;
move->client_initiated = client_initiated;
shell_grab_start(&move->base, &move_grab_interface, shsurf,
pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
return 0;
}
struct weston_resize_grab {
struct shell_grab base;
uint32_t edges;
int32_t width, height;
};
static void
resize_grab_motion(struct weston_pointer_grab *grab,
const struct timespec *time,
struct weston_pointer_motion_event *event)
{
struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
struct weston_pointer *pointer = grab->pointer;
struct shell_surface *shsurf = resize->base.shsurf;
int32_t width, height;
struct weston_size min_size, max_size;
wl_fixed_t from_x, from_y;
wl_fixed_t to_x, to_y;
weston_pointer_move(pointer, event);
if (!shsurf)
return;
weston_view_from_global_fixed(shsurf->view,
pointer->grab_x, pointer->grab_y,
&from_x, &from_y);
weston_view_from_global_fixed(shsurf->view,
pointer->x, pointer->y, &to_x, &to_y);
width = resize->width;
if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
width += wl_fixed_to_int(from_x - to_x);
} else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
width += wl_fixed_to_int(to_x - from_x);
}
height = resize->height;
if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
height += wl_fixed_to_int(from_y - to_y);
} else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
height += wl_fixed_to_int(to_y - from_y);
}
max_size = weston_desktop_surface_get_max_size(shsurf->desktop_surface);
min_size = weston_desktop_surface_get_min_size(shsurf->desktop_surface);
min_size.width = MAX(1, min_size.width);
min_size.height = MAX(1, min_size.height);
if (width < min_size.width)
width = min_size.width;
else if (max_size.width > 0 && width > max_size.width)
width = max_size.width;
if (height < min_size.height)
height = min_size.height;
else if (max_size.width > 0 && width > max_size.width)
width = max_size.width;
weston_desktop_surface_set_size(shsurf->desktop_surface, width, height);
}
static void
resize_grab_button(struct weston_pointer_grab *grab,
const struct timespec *time,
uint32_t button, uint32_t state_w)
{
struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
struct weston_pointer *pointer = grab->pointer;
enum wl_pointer_button_state state = state_w;
struct weston_desktop_surface *desktop_surface =
resize->base.shsurf->desktop_surface;
if (pointer->button_count == 0 &&
state == WL_POINTER_BUTTON_STATE_RELEASED) {
weston_desktop_surface_set_resizing(desktop_surface, false);
shell_grab_end(&resize->base);
free(grab);
}
}
static void
resize_grab_cancel(struct weston_pointer_grab *grab)
{
struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
struct weston_desktop_surface *desktop_surface =
resize->base.shsurf->desktop_surface;
weston_desktop_surface_set_resizing(desktop_surface, false);
shell_grab_end(&resize->base);
free(grab);
}
static const struct weston_pointer_grab_interface resize_grab_interface = {
noop_grab_focus,
resize_grab_motion,
resize_grab_button,
noop_grab_axis,
noop_grab_axis_source,
noop_grab_frame,
resize_grab_cancel,
};
/*
* Returns the bounding box of a surface and all its sub-surfaces,
* in surface-local coordinates. */
static void
surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
int32_t *y, int32_t *w, int32_t *h) {
pixman_region32_t region;
pixman_box32_t *box;
struct weston_subsurface *subsurface;
pixman_region32_init_rect(&region, 0, 0,
surface->width,
surface->height);
wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
pixman_region32_union_rect(&region, &region,
subsurface->position.x,
subsurface->position.y,
subsurface->surface->width,
subsurface->surface->height);
}
box = pixman_region32_extents(&region);
if (x)
*x = box->x1;
if (y)
*y = box->y1;
if (w)
*w = box->x2 - box->x1;
if (h)
*h = box->y2 - box->y1;
pixman_region32_fini(&region);
}
static int
surface_resize(struct shell_surface *shsurf,
struct weston_pointer *pointer, uint32_t edges)
{
struct weston_resize_grab *resize;
const unsigned resize_topbottom =
WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
const unsigned resize_leftright =
WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
const unsigned resize_any = resize_topbottom | resize_leftright;
struct weston_geometry geometry;
if (shsurf->grabbed ||
weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
weston_desktop_surface_get_maximized(shsurf->desktop_surface))
return 0;
/* Check for invalid edge combinations. */
if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
(edges & resize_topbottom) == resize_topbottom ||
(edges & resize_leftright) == resize_leftright)
return 0;
resize = malloc(sizeof *resize);
if (!resize)
return -1;
resize->edges = edges;
geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
resize->width = geometry.width;
resize->height = geometry.height;
shsurf->resize_edges = edges;
weston_desktop_surface_set_resizing(shsurf->desktop_surface, true);
shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
pointer, edges);
return 0;
}
static void
busy_cursor_grab_focus(struct weston_pointer_grab *base)
{
struct shell_grab *grab = (struct shell_grab *) base;
struct weston_pointer *pointer = base->pointer;
struct weston_desktop_surface *desktop_surface;
struct weston_view *view;
wl_fixed_t sx, sy;
view = weston_compositor_pick_view(pointer->seat->compositor,
pointer->x, pointer->y,
&sx, &sy);
desktop_surface = weston_surface_get_desktop_surface(view->surface);
if (!grab->shsurf || grab->shsurf->desktop_surface != desktop_surface) {
shell_grab_end(grab);
free(grab);
}
}
static void
busy_cursor_grab_motion(struct weston_pointer_grab *grab,
const struct timespec *time,
struct weston_pointer_motion_event *event)
{
weston_pointer_move(grab->pointer, event);
}
static void
busy_cursor_grab_button(struct weston_pointer_grab *base,
const struct timespec *time,
uint32_t button, uint32_t state)
{
struct shell_grab *grab = (struct shell_grab *) base;
struct shell_surface *shsurf = grab->shsurf;
struct weston_pointer *pointer = grab->grab.pointer;
struct weston_seat *seat = pointer->seat;
if (shsurf && button == BTN_LEFT && state) {
activate(shsurf->shell, shsurf->view, seat,
WESTON_ACTIVATE_FLAG_CONFIGURE);
surface_move(shsurf, pointer, false);
} else if (shsurf && button == BTN_RIGHT && state) {
activate(shsurf->shell, shsurf->view, seat,
WESTON_ACTIVATE_FLAG_CONFIGURE);
surface_rotate(shsurf, pointer);
}
}
static void
busy_cursor_grab_cancel(struct weston_pointer_grab *base)
{
struct shell_grab *grab = (struct shell_grab *) base;
shell_grab_end(grab);
free(grab);
}
static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
busy_cursor_grab_focus,
busy_cursor_grab_motion,
busy_cursor_grab_button,
noop_grab_axis,
noop_grab_axis_source,
noop_grab_frame,
busy_cursor_grab_cancel,
};
static void
handle_pointer_focus(struct wl_listener *listener, void *data)
{
struct weston_pointer *pointer = data;
struct weston_view *view = pointer->focus;
struct shell_surface *shsurf;
struct weston_desktop_client *client;
if (!view)
return;
shsurf = get_shell_surface(view->surface);
if (!shsurf)
return;
client = weston_desktop_surface_get_client(shsurf->desktop_surface);
if (shsurf->unresponsive)
set_busy_cursor(shsurf, pointer);
else
weston_desktop_client_ping(client);
}
static void
shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
{
if (--shsurf->focus_count == 0)
weston_desktop_surface_set_activated(shsurf->desktop_surface, false);
}
static void
shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
{
if (shsurf->focus_count++ == 0)
weston_desktop_surface_set_activated(shsurf->desktop_surface, true);
}
static void
handle_keyboard_focus(struct wl_listener *listener, void *data)
{
struct weston_keyboard *keyboard = data;
struct shell_seat *seat = get_shell_seat(keyboard->seat);
if (seat->focused_surface) {
struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
if (shsurf)
shell_surface_lose_keyboard_focus(shsurf);
}
seat->focused_surface = weston_surface_get_main_surface(keyboard->focus);
if (seat->focused_surface) {
struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
if (shsurf)
shell_surface_gain_keyboard_focus(shsurf);
}
}
/* The surface will be inserted into the list immediately after the link
* returned by this function (i.e. will be stacked immediately above the
* returned link). */
static struct weston_layer_entry *
shell_surface_calculate_layer_link (struct shell_surface *shsurf)
{
struct workspace *ws;
if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
!shsurf->state.lowered) {
return &shsurf->shell->fullscreen_layer.view_list;
}
/* Move the surface to a normal workspace layer so that surfaces
* which were previously fullscreen or transient are no longer
* rendered on top. */
ws = get_current_workspace(shsurf->shell);
return &ws->layer.view_list;
}
static void
shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
{
weston_desktop_surface_propagate_layer(shsurf->desktop_surface);
}
/* Update the surface’s layer. Mark both the old and new views as having dirty
* geometry to ensure the changes are redrawn.
*
* If any child surfaces exist and are mapped, ensure they’re in the same layer
* as this surface. */
static void
shell_surface_update_layer(struct shell_surface *shsurf)
{
struct weston_surface *surface =
weston_desktop_surface_get_surface(shsurf->desktop_surface);
struct weston_layer_entry *new_layer_link;
new_layer_link = shell_surface_calculate_layer_link(shsurf);
if (new_layer_link == NULL)
return;
if (new_layer_link == &shsurf->view->layer_link)
return;
weston_view_geometry_dirty(shsurf->view);
weston_layer_entry_remove(&shsurf->view->layer_link);
weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
weston_view_geometry_dirty(shsurf->view);
weston_surface_damage(surface);
shell_surface_update_child_surface_layers(shsurf);
}
static void
notify_output_destroy(struct wl_listener *listener, void *data)
{
struct shell_surface *shsurf =
container_of(listener,
struct shell_surface, output_destroy_listener);
shsurf->output = NULL;
shsurf->output_destroy_listener.notify = NULL;
}
static void
shell_surface_set_output(struct shell_surface *shsurf,
struct weston_output *output)
{
struct weston_surface *es =
weston_desktop_surface_get_surface(shsurf->desktop_surface);
/* get the default output, if the client set it as NULL
check whether the output is available */
if (output)
shsurf->output = output;
else if (es->output)
shsurf->output = es->output;
else
shsurf->output = get_default_output(es->compositor);
if (shsurf->output_destroy_listener.notify) {
wl_list_remove(&shsurf->output_destroy_listener.link);
shsurf->output_destroy_listener.notify = NULL;
}
if (!shsurf->output)
return;
shsurf->output_destroy_listener.notify = notify_output_destroy;
wl_signal_add(&shsurf->output->destroy_signal,
&shsurf->output_destroy_listener);
}
static void
weston_view_set_initial_position(struct weston_view *view,
struct desktop_shell *shell);
static void
unset_fullscreen(struct shell_surface *shsurf)
{
/* Unset the fullscreen output, driver configuration and transforms. */
wl_list_remove(&shsurf->fullscreen.transform.link);
wl_list_init(&shsurf->fullscreen.transform.link);
if (shsurf->fullscreen.black_view)
weston_surface_destroy(shsurf->fullscreen.black_view->surface);
shsurf->fullscreen.black_view = NULL;
if (shsurf->saved_position_valid)
weston_view_set_position(shsurf->view,
shsurf->saved_x, shsurf->saved_y);
else
weston_view_set_initial_position(shsurf->view, shsurf->shell);
shsurf->saved_position_valid = false;
if (shsurf->saved_rotation_valid) {
wl_list_insert(&shsurf->view->geometry.transformation_list,
&shsurf->rotation.transform.link);
shsurf->saved_rotation_valid = false;
}
}
static void
unset_maximized(struct shell_surface *shsurf)
{
struct weston_surface *surface =
weston_desktop_surface_get_surface(shsurf->desktop_surface);
/* undo all maximized things here */
shell_surface_set_output(shsurf, get_default_output(surface->compositor));
if (shsurf->saved_position_valid)
weston_view_set_position(shsurf->view,
shsurf->saved_x, shsurf->saved_y);
else
weston_view_set_initial_position(shsurf->view, shsurf->shell);
shsurf->saved_position_valid = false;
if (shsurf->saved_rotation_valid) {
wl_list_insert(&shsurf->view->geometry.transformation_list,
&shsurf->rotation.transform.link);
shsurf->saved_rotation_valid = false;
}
}
static void
set_minimized(struct weston_surface *surface)
{
struct shell_surface *shsurf;
struct workspace *current_ws;
struct weston_view *view;
view = get_default_view(surface);
if (!view)
return;
assert(weston_surface_get_main_surface(view->surface) == view->surface);
shsurf = get_shell_surface(surface);
current_ws = get_current_workspace(shsurf->shell);
weston_layer_entry_remove(&view->layer_link);
weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
drop_focus_state(shsurf->shell, current_ws, view->surface);
surface_keyboard_focus_lost(surface);
shell_surface_update_child_surface_layers(shsurf);
weston_view_damage_below(view);
}
static struct desktop_shell *
shell_surface_get_shell(struct shell_surface *shsurf)
{
return shsurf->shell;
}
static int
black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
{
struct weston_view *fs_view = surface->committed_private;
struct weston_surface *fs_surface = fs_view->surface;
int n;
int rem;
int ret;
n = snprintf(buf, len, "black background surface for ");
if (n < 0)
return n;
rem = (int)len - n;
if (rem < 0)
rem = 0;
if (fs_surface->get_label)
ret = fs_surface->get_label(fs_surface, buf + n, rem);
else
ret = snprintf(buf + n, rem, "<unknown>");
if (ret < 0)
return n;
return n + ret;
}
static void
black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
static struct weston_view *
create_black_surface(struct weston_compositor *ec,
struct weston_view *fs_view,
float x, float y, int w, int h)
{
struct weston_surface *surface = NULL;
struct weston_view *view;
surface = weston_surface_create(ec);
if (surface == NULL) {
weston_log("no memory\n");
return NULL;
}
view = weston_view_create(surface);
if (surface == NULL) {
weston_log("no memory\n");
weston_surface_destroy(surface);
return NULL;
}
surface->committed = black_surface_committed;
surface->committed_private = fs_view;
weston_surface_set_label_func(surface, black_surface_get_label);
weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
pixman_region32_fini(&surface->opaque);
pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
pixman_region32_fini(&surface->input);
pixman_region32_init_rect(&surface->input, 0, 0, w, h);
weston_surface_set_size(surface, w, h);
weston_view_set_position(view, x, y);
return view;
}
static void
shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
{
struct weston_surface *surface =
weston_desktop_surface_get_surface(shsurf->desktop_surface);
struct weston_output *output = shsurf->fullscreen_output;
assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
if (!shsurf->fullscreen.black_view)
shsurf->fullscreen.black_view =
create_black_surface(surface->compositor,
shsurf->view,
output->x, output->y,
output->width,
output->height);
weston_view_geometry_dirty(shsurf->fullscreen.black_view);
weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
weston_layer_entry_insert(&shsurf->view->layer_link,
&shsurf->fullscreen.black_view->layer_link);
weston_view_geometry_dirty(shsurf->fullscreen.black_view);
weston_surface_damage(surface);
shsurf->fullscreen.black_view->is_mapped = true;
shsurf->state.lowered = false;
}
/* Create black surface and append it to the associated fullscreen surface.
* Handle size dismatch and positioning according to the method. */
static void
shell_configure_fullscreen(struct shell_surface *shsurf)
{
struct weston_surface *surface =
weston_desktop_surface_get_surface(shsurf->desktop_surface);
int32_t surf_x, surf_y, surf_width, surf_height;
/* Reverse the effect of lower_fullscreen_layer() */
weston_layer_entry_remove(&shsurf->view->layer_link);
weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
&shsurf->view->layer_link);
if (!shsurf->fullscreen_output) {
/* If there is no output, there's not much we can do.
* Position the window somewhere, whatever. */
weston_view_set_position(shsurf->view, 0, 0);
return;
}
shell_ensure_fullscreen_black_view(shsurf);
surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
&surf_width, &surf_height);
if (surface->buffer_ref.buffer)
center_on_output(shsurf->view, shsurf->fullscreen_output);
}
static void
shell_map_fullscreen(struct shell_surface *shsurf)
{
shell_configure_fullscreen(shsurf);
}
static struct weston_output *
get_focused_output(struct weston_compositor *compositor)
{
struct weston_seat *seat;
struct weston_output *output = NULL;
wl_list_for_each(seat, &compositor->seat_list, link) {
struct weston_touch *touch = weston_seat_get_touch(seat);
struct weston_pointer *pointer = weston_seat_get_pointer(seat);
struct weston_keyboard *keyboard =
weston_seat_get_keyboard(seat);
/* Priority has touch focus, then pointer and
* then keyboard focus. We should probably have
* three for loops and check frist for touch,
* then for pointer, etc. but unless somebody has some
* objections, I think this is sufficient. */
if (touch && touch->focus)
output = touch->focus->output;
else if (pointer && pointer->focus)
output = pointer->focus->output;
else if (keyboard && keyboard->focus)
output = keyboard->focus->output;
if (output)
break;
}
return output;
}
static void
destroy_shell_seat(struct wl_listener *listener, void *data)
{
struct shell_seat *shseat =
container_of(listener,
struct shell_seat, seat_destroy_listener);
wl_list_remove(&shseat->seat_destroy_listener.link);
free(shseat);
}
static void
shell_seat_caps_changed(struct wl_listener *listener, void *data)
{
struct weston_keyboard *keyboard;
struct weston_pointer *pointer;
struct shell_seat *seat;
seat = container_of(listener, struct shell_seat, caps_changed_listener);
keyboard = weston_seat_get_keyboard(seat->seat);
pointer = weston_seat_get_pointer(seat->seat);
if (keyboard &&
wl_list_empty(&seat->keyboard_focus_listener.link)) {
wl_signal_add(&keyboard->focus_signal,
&seat->keyboard_focus_listener);
} else if (!keyboard) {
wl_list_remove(&seat->keyboard_focus_listener.link);
wl_list_init(&seat->keyboard_focus_listener.link);
}
if (pointer &&
wl_list_empty(&seat->pointer_focus_listener.link)) {
wl_signal_add(&pointer->focus_signal,
&seat->pointer_focus_listener);
} else if (!pointer) {
wl_list_remove(&seat->pointer_focus_listener.link);
wl_list_init(&seat->pointer_focus_listener.link);
}
}
static struct shell_seat *
create_shell_seat(struct weston_seat *seat)
{
struct shell_seat *shseat;
shseat = calloc(1, sizeof *shseat);
if (!shseat) {
weston_log("no memory to allocate shell seat\n");
return NULL;
}
shseat->seat = seat;
shseat->seat_destroy_listener.notify = destroy_shell_seat;
wl_signal_add(&seat->destroy_signal,
&shseat->seat_destroy_listener);
shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
wl_list_init(&shseat->keyboard_focus_listener.link);
shseat->pointer_focus_listener.notify = handle_pointer_focus;
wl_list_init(&shseat->pointer_focus_listener.link);
shseat->caps_changed_listener.notify = shell_seat_caps_changed;
wl_signal_add(&seat->updated_caps_signal,
&shseat->caps_changed_listener);
shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
return shseat;
}
static struct shell_seat *
get_shell_seat(struct weston_seat *seat)
{
struct wl_listener *listener;
listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
assert(listener != NULL);
return container_of(listener,
struct shell_seat, seat_destroy_listener);
}
static void
fade_out_done_idle_cb(void *data)
{
struct shell_surface *shsurf = data;
weston_surface_destroy(shsurf->view->surface);
if (shsurf->output_destroy_listener.notify) {
wl_list_remove(&shsurf->output_destroy_listener.link);
shsurf->output_destroy_listener.notify = NULL;
}
free(shsurf);
}
static void
fade_out_done(struct weston_view_animation *animation, void *data)
{
struct shell_surface *shsurf = data;
struct wl_event_loop *loop;
loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
if (weston_view_is_mapped(shsurf->view)) {
shsurf->view->is_mapped = false;
wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
}
}
struct shell_surface *
get_shell_surface(struct weston_surface *surface)
{
if (weston_surface_is_desktop_surface(surface)) {
struct weston_desktop_surface *desktop_surface =
weston_surface_get_desktop_surface(surface);
return weston_desktop_surface_get_user_data(desktop_surface);
}
return NULL;
}
/*
* libweston-desktop
*/
static void
desktop_surface_added(struct weston_desktop_surface *desktop_surface,
void *shell)
{
struct weston_desktop_client *client =
weston_desktop_surface_get_client(desktop_surface);
struct wl_client *wl_client =
weston_desktop_client_get_client(client);
struct weston_view *view;
struct shell_surface *shsurf;
struct weston_surface *surface =
weston_desktop_surface_get_surface(desktop_surface);
view = weston_desktop_surface_create_view(desktop_surface);
if (!view)
return;
shsurf = calloc(1, sizeof *shsurf);
if (!shsurf) {
if (wl_client)
wl_client_post_no_memory(wl_client);
else
weston_log("no memory to allocate shell surface\n");
return;
}
weston_surface_set_label_func(surface, shell_surface_get_label);
shsurf->shell = (struct desktop_shell *) shell;
shsurf->unresponsive = 0;
shsurf->saved_position_valid = false;
shsurf->saved_rotation_valid = false;
shsurf->desktop_surface = desktop_surface;
shsurf->view = view;
shsurf->fullscreen.black_view = NULL;
wl_list_init(&shsurf->fullscreen.transform.link);
shell_surface_set_output(
shsurf, get_default_output(shsurf->shell->compositor));
wl_signal_init(&shsurf->destroy_signal);
/* empty when not in use */
wl_list_init(&shsurf->rotation.transform.link);
weston_matrix_init(&shsurf->rotation.rotation);
wl_list_init(&shsurf->workspace_transform.link);
weston_desktop_surface_set_user_data(desktop_surface, shsurf);
weston_desktop_surface_set_activated(desktop_surface,
shsurf->focus_count > 0);
}
static void
desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
void *shell)
{
struct shell_surface *shsurf =
weston_desktop_surface_get_user_data(desktop_surface);
struct weston_surface *surface =
weston_desktop_surface_get_surface(desktop_surface);
if (!shsurf)
return;
wl_signal_emit(&shsurf->destroy_signal, shsurf);
if (shsurf->fullscreen.black_view)
weston_surface_destroy(shsurf->fullscreen.black_view->surface);
weston_surface_set_label_func(surface, NULL);
weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
shsurf->desktop_surface = NULL;
weston_desktop_surface_unlink_view(shsurf->view);
if (weston_surface_is_mapped(surface) &&
shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
pixman_region32_fini(&surface->pending.input);
pixman_region32_init(&surface->pending.input);
pixman_region32_fini(&surface->input);
pixman_region32_init(&surface->input);
weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
fade_out_done, shsurf);
} else {
weston_view_destroy(shsurf->view);
if (shsurf->output_destroy_listener.notify) {
wl_list_remove(&shsurf->output_destroy_listener.link);
shsurf->output_destroy_listener.notify = NULL;
}
free(shsurf);
}
}
static void
set_maximized_position(struct desktop_shell *shell,
struct shell_surface *shsurf)
{
pixman_rectangle32_t area;
struct weston_geometry geometry;
get_output_work_area(shell, shsurf->output, &area);
geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
weston_view_set_position(shsurf->view,
area.x - geometry.x,
area.y - geometry.y);
}
static void
set_position_from_xwayland(struct shell_surface *shsurf)
{
struct weston_geometry geometry;
float x;
float y;
assert(shsurf->xwayland.is_set);
geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
x = shsurf->xwayland.x - geometry.x;
y = shsurf->xwayland.y - geometry.y;
weston_view_set_position(shsurf->view, x, y);
#ifdef WM_DEBUG
weston_log("%s: XWM %d, %d; geometry %d, %d; view %f, %f\n",
__func__, shsurf->xwayland.x, shsurf->xwayland.y,
geometry.x, geometry.y, x, y);
#endif
}
static void
map(struct desktop_shell *shell, struct shell_surface *shsurf,
int32_t sx, int32_t sy)
{
struct weston_surface *surface =
weston_desktop_surface_get_surface(shsurf->desktop_surface);
struct weston_compositor *compositor = shell->compositor;
struct weston_seat *seat;
/* initial positioning, see also configure() */
if (shsurf->state.fullscreen) {
center_on_output(shsurf->view, shsurf->fullscreen_output);
shell_map_fullscreen(shsurf);
} else if (shsurf->state.maximized) {
set_maximized_position(shell, shsurf);
} else if (shsurf->xwayland.is_set) {
set_position_from_xwayland(shsurf);
} else {
weston_view_set_initial_position(shsurf->view, shell);
}
/* Surface stacking order, see also activate(). */
shell_surface_update_layer(shsurf);
weston_view_update_transform(shsurf->view);
shsurf->view->is_mapped = true;
if (shsurf->state.maximized) {
surface->output = shsurf->output;
weston_view_set_output(shsurf->view, shsurf->output);
}
if (!shell->locked) {
wl_list_for_each(seat, &compositor->seat_list, link)
activate(shell, shsurf->view, seat,
WESTON_ACTIVATE_FLAG_CONFIGURE);
}
if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
switch (shell->win_animation_type) {
case ANIMATION_FADE:
weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
break;
case ANIMATION_ZOOM:
weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
break;
case ANIMATION_NONE:
default:
break;
}
}
}
static void
desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
int32_t sx, int32_t sy, void *data)
{
struct shell_surface *shsurf =
weston_desktop_surface_get_user_data(desktop_surface);
struct weston_surface *surface =
weston_desktop_surface_get_surface(desktop_surface);
struct weston_view *view = shsurf->view;
struct desktop_shell *shell = data;
bool was_fullscreen;
bool was_maximized;
if (surface->width == 0)
return;
was_fullscreen = shsurf->state.fullscreen;
was_maximized = shsurf->state.maximized;
shsurf->state.fullscreen =
weston_desktop_surface_get_fullscreen(desktop_surface);
shsurf->state.maximized =
weston_desktop_surface_get_maximized(desktop_surface);
if (!weston_surface_is_mapped(surface)) {
map(shell, shsurf, sx, sy);
surface->is_mapped = true;
if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
++surface->ref_count;
return;
}
if (sx == 0 && sy == 0 &&
shsurf->last_width == surface->width &&
shsurf->last_height == surface->height &&
was_fullscreen == shsurf->state.fullscreen &&
was_maximized == shsurf->state.maximized)
return;
if (was_fullscreen)
unset_fullscreen(shsurf);
if (was_maximized)
unset_maximized(shsurf);
if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
!shsurf->saved_position_valid) {
shsurf->saved_x = shsurf->view->geometry.x;
shsurf->saved_y = shsurf->view->geometry.y;
shsurf->saved_position_valid = true;
if (!wl_list_empty(&shsurf->rotation.transform.link)) {
wl_list_remove(&shsurf->rotation.transform.link);
wl_list_init(&shsurf->rotation.transform.link);
weston_view_geometry_dirty(shsurf->view);
shsurf->saved_rotation_valid = true;
}
}
if (shsurf->state.fullscreen) {
shell_configure_fullscreen(shsurf);
} else if (shsurf->state.maximized) {
set_maximized_position(shell, shsurf);
surface->output = shsurf->output;
} else {
float from_x, from_y;
float to_x, to_y;
float x, y;
if (shsurf->resize_edges) {
sx = 0;
sy = 0;
}
if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
sx = shsurf->last_width - surface->width;
if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
sy = shsurf->last_height - surface->height;
weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
x = shsurf->view->geometry.x + to_x - from_x;
y = shsurf->view->geometry.y + to_y - from_y;
weston_view_set_position(shsurf->view, x, y);
}
shsurf->last_width = surface->width;
shsurf->last_height = surface->height;
/* XXX: would a fullscreen surface need the same handling? */
if (surface->output) {
wl_list_for_each(view, &surface->views, surface_link)
weston_view_update_transform(view);
}
}
static void
get_maximized_size(struct shell_surface *shsurf, int32_t *width, int32_t *height)
{
struct desktop_shell *shell;
pixman_rectangle32_t area;
shell = shell_surface_get_shell(shsurf);
get_output_work_area(shell, shsurf->output, &area);
*width = area.width;
*height = area.height;
}
static void
set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
struct weston_output *output)
{
struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
struct weston_surface *surface =
weston_desktop_surface_get_surface(shsurf->desktop_surface);
int32_t width = 0, height = 0;
if (fullscreen) {
/* handle clients launching in fullscreen */
if (output == NULL && !weston_surface_is_mapped(surface)) {
/* Set the output to the one that has focus currently. */
output = get_focused_output(surface->compositor);
}
shell_surface_set_output(shsurf, output);
shsurf->fullscreen_output = shsurf->output;
width = shsurf->output->width;
height = shsurf->output->height;
} else if (weston_desktop_surface_get_maximized(desktop_surface)) {
get_maximized_size(shsurf, &width, &height);
}
weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
weston_desktop_surface_set_size(desktop_surface, width, height);
}
static void
desktop_surface_move(struct weston_desktop_surface *desktop_surface,
struct weston_seat *seat, uint32_t serial, void *shell)
{
struct weston_pointer *pointer = weston_seat_get_pointer(seat);
struct weston_touch *touch = weston_seat_get_touch(seat);
struct shell_surface *shsurf =
weston_desktop_surface_get_user_data(desktop_surface);
struct weston_surface *surface =
weston_desktop_surface_get_surface(shsurf->desktop_surface);
struct wl_resource *resource = surface->resource;
struct weston_surface *focus;
if (pointer &&
pointer->focus &&
pointer->button_count > 0 &&
pointer->grab_serial == serial) {
focus = weston_surface_get_main_surface(pointer->focus->surface);
if ((focus == surface) &&
(surface_move(shsurf, pointer, true) < 0))
wl_resource_post_no_memory(resource);
} else if (touch &&
touch->focus &&
touch->grab_serial == serial) {
focus = weston_surface_get_main_surface(touch->focus->surface);
if ((focus == surface) &&
(surface_touch_move(shsurf, touch) < 0))
wl_resource_post_no_memory(resource);
}
}
static void
desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
struct weston_seat *seat, uint32_t serial,
enum weston_desktop_surface_edge edges, void *shell)
{
struct weston_pointer *pointer = weston_seat_get_pointer(seat);
struct shell_surface *shsurf =
weston_desktop_surface_get_user_data(desktop_surface);
struct weston_surface *surface =
weston_desktop_surface_get_surface(shsurf->desktop_surface);
struct wl_resource *resource = surface->resource;
struct weston_surface *focus;
if (!pointer ||
pointer->button_count == 0 ||
pointer->grab_serial != serial ||
pointer->focus == NULL)
return;
focus = weston_surface_get_main_surface(pointer->focus->surface);
if (focus != surface)
return;
if (surface_resize(shsurf, pointer, edges) < 0)
wl_resource_post_no_memory(resource);
}
static void
desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
bool fullscreen,
struct weston_output *output, void *shell)
{
struct shell_surface *shsurf =
weston_desktop_surface_get_user_data(desktop_surface);
set_fullscreen(shsurf, fullscreen, output);
}
static void
set_maximized(struct shell_surface *shsurf, bool maximized)
{
struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
struct weston_surface *surface =
weston_desktop_surface_get_surface(shsurf->desktop_surface);
int32_t width = 0, height = 0;
if (maximized) {
struct weston_output *output;
if (!weston_surface_is_mapped(surface))
output = get_focused_output(surface->compositor);
else
output = surface->output;
shell_surface_set_output(shsurf, output);
get_maximized_size(shsurf, &width, &height);
}
weston_desktop_surface_set_maximized(desktop_surface, maximized);
weston_desktop_surface_set_size(desktop_surface, width, height);
}
static void
desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
bool maximized, void *shell)
{
struct shell_surface *shsurf =
weston_desktop_surface_get_user_data(desktop_surface);
set_maximized(shsurf, maximized);
}
static void
desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
void *shell)
{
struct weston_surface *surface =
weston_desktop_surface_get_surface(desktop_surface);
/* apply compositor's own minimization logic (hide) */
set_minimized(surface);
}
static void
set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
{
struct shell_grab *grab;
if (pointer->grab->interface == &busy_cursor_grab_interface)
return;
grab = malloc(sizeof *grab);
if (!grab)
return;
shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
WESTON_DESKTOP_SHELL_CURSOR_BUSY);
/* Mark the shsurf as ungrabbed so that button binding is able
* to move it. */
shsurf->grabbed = 0;
}
static void
end_busy_cursor(struct weston_compositor *compositor,
struct weston_desktop_client *desktop_client)
{
struct shell_surface *shsurf;
struct shell_grab *grab;
struct weston_seat *seat;
wl_list_for_each(seat, &compositor->seat_list, link) {
struct weston_pointer *pointer = weston_seat_get_pointer(seat);
struct weston_desktop_client *grab_client;
if (!pointer)
continue;
if (pointer->grab->interface != &busy_cursor_grab_interface)
continue;
grab = (struct shell_grab *) pointer->grab;
shsurf = grab->shsurf;
if (!shsurf)
continue;
grab_client =
weston_desktop_surface_get_client(shsurf->desktop_surface);
if (grab_client == desktop_client) {
shell_grab_end(grab);
free(grab);
}
}
}
static void
desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
void *user_data)
{
struct shell_surface *shsurf =
weston_desktop_surface_get_user_data(desktop_surface);
bool *unresponsive = user_data;
shsurf->unresponsive = *unresponsive;
}
static void
desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
void *shell_)
{
struct desktop_shell *shell = shell_;
struct shell_surface *shsurf;
struct weston_seat *seat;
bool unresponsive = true;
weston_desktop_client_for_each_surface(desktop_client,
desktop_surface_set_unresponsive,
&unresponsive);
wl_list_for_each(seat, &shell->compositor->seat_list, link) {
struct weston_pointer *pointer = weston_seat_get_pointer(seat);
struct weston_desktop_client *grab_client;
if (!pointer || !pointer->focus)
continue;
shsurf = get_shell_surface(pointer->focus->surface);
if (!shsurf)
continue;
grab_client =
weston_desktop_surface_get_client(shsurf->desktop_surface);
if (grab_client == desktop_client)
set_busy_cursor(shsurf, pointer);
}
}
static void
desktop_surface_pong(struct weston_desktop_client *desktop_client,
void *shell_)
{
struct desktop_shell *shell = shell_;
bool unresponsive = false;
weston_desktop_client_for_each_surface(desktop_client,
desktop_surface_set_unresponsive,
&unresponsive);
end_busy_cursor(shell->compositor, desktop_client);
}
static void
desktop_surface_set_xwayland_position(struct weston_desktop_surface *surface,
int32_t x, int32_t y, void *shell_)
{
struct shell_surface *shsurf =
weston_desktop_surface_get_user_data(surface);
shsurf->xwayland.x = x;
shsurf->xwayland.y = y;
shsurf->xwayland.is_set = true;
}
static const struct weston_desktop_api shell_desktop_api = {
.struct_size = sizeof(struct weston_desktop_api),
.surface_added = desktop_surface_added,
.surface_removed = desktop_surface_removed,
.committed = desktop_surface_committed,
.move = desktop_surface_move,
.resize = desktop_surface_resize,
.fullscreen_requested = desktop_surface_fullscreen_requested,
.maximized_requested = desktop_surface_maximized_requested,
.minimized_requested = desktop_surface_minimized_requested,
.ping_timeout = desktop_surface_ping_timeout,
.pong = desktop_surface_pong,
.set_xwayland_position = desktop_surface_set_xwayland_position,
};
/* ************************ *
* end of libweston-desktop *
* ************************ */
static void
configure_static_view(struct weston_view *ev, struct weston_layer *layer, int x, int y)
{
struct weston_view *v, *next;
if (!ev->output)
return;
wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
if (v->output == ev->output && v != ev) {
weston_view_unmap(v);
v->surface->committed = NULL;
weston_surface_set_label_func(v->surface, NULL);
}
}
weston_view_set_position(ev, ev->output->x + x, ev->output->y + y);
ev->surface->is_mapped = true;
ev->is_mapped = true;
if (wl_list_empty(&ev->layer_link.link)) {
weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
weston_compositor_schedule_repaint(ev->surface->compositor);
}
}
static struct shell_output *
find_shell_output_from_weston_output(struct desktop_shell *shell,
struct weston_output *output)
{
struct shell_output *shell_output;
wl_list_for_each(shell_output, &shell->output_list, link) {
if (shell_output->output == output)
return shell_output;
}
return NULL;
}
static int
background_get_label(struct weston_surface *surface, char *buf, size_t len)
{
return snprintf(buf, len, "background for output %s",
surface->output->name);
}
static void
background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
{
struct desktop_shell *shell = es->committed_private;
struct weston_view *view;
view = container_of(es->views.next, struct weston_view, surface_link);
configure_static_view(view, &shell->background_layer, 0, 0);
}
static void
handle_background_surface_destroy(struct wl_listener *listener, void *data)
{
struct shell_output *output =
container_of(listener, struct shell_output, background_surface_listener);
weston_log("background surface gone\n");
wl_list_remove(&output->background_surface_listener.link);
output->background_surface = NULL;
}
static void
desktop_shell_set_background(struct wl_client *client,
struct wl_resource *resource,
struct wl_resource *output_resource,
struct wl_resource *surface_resource)
{
struct desktop_shell *shell = wl_resource_get_user_data(resource);
struct weston_surface *surface =
wl_resource_get_user_data(surface_resource);
struct shell_output *sh_output;
struct weston_view *view, *next;
struct weston_config_section *section;
if (surface->committed) {
wl_resource_post_error(surface_resource,
WL_DISPLAY_ERROR_INVALID_OBJECT,
"surface role already assigned");
return;
}
wl_list_for_each_safe(view, next, &surface->views, surface_link)
weston_view_destroy(view);
view = weston_view_create(surface);
surface->committed = background_committed;
surface->committed_private = shell;
weston_surface_set_label_func(surface, background_get_label);
surface->output = weston_head_from_resource(output_resource)->output;
weston_view_set_output(view, surface->output);
section = weston_config_get_section(wet_get_config(shell->compositor),
"shell", NULL, NULL);
if (section) {
char *size;
int n;
int32_t width, height;
weston_config_section_get_string(section, "size", &size, NULL);
if(size){
n = sscanf(size, "%dx%d", &width, &height);
if (n == 2) {
if (surface->output->width > width)
surface->output->width = width;
if (surface->output->height > height)
surface->output->height = height;
}
}
}
sh_output = find_shell_output_from_weston_output(shell, surface->output);
if (sh_output->background_surface) {
/* The output already has a background, tell our helper
* there is no need for another one. */
weston_desktop_shell_send_configure(resource, 0,
surface_resource,
0, 0);
} else {
weston_desktop_shell_send_configure(resource, 0,
surface_resource,
surface->output->width,
surface->output->height);
sh_output->background_surface = surface;
sh_output->background_surface_listener.notify =
handle_background_surface_destroy;
wl_signal_add(&surface->destroy_signal,
&sh_output->background_surface_listener);
}
}
static int
panel_get_label(struct weston_surface *surface, char *buf, size_t len)
</