2020-12-30 10:29:21 +00:00
|
|
|
#include "config.h"
|
2020-09-28 20:53:59 +01:00
|
|
|
#include <assert.h>
|
2020-09-11 20:48:28 +01:00
|
|
|
#include "labwc.h"
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
static void
|
|
|
|
|
move_to_front(struct view *view)
|
2020-09-11 20:48:28 +01:00
|
|
|
{
|
|
|
|
|
wl_list_remove(&view->link);
|
|
|
|
|
wl_list_insert(&view->server->views, &view->link);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-30 10:29:21 +00:00
|
|
|
#if HAVE_XWAYLAND
|
2020-09-28 20:41:41 +01:00
|
|
|
static struct wlr_xwayland_surface *
|
|
|
|
|
top_parent_of(struct view *view)
|
2020-09-11 20:48:28 +01:00
|
|
|
{
|
|
|
|
|
struct wlr_xwayland_surface *s = view->xwayland_surface;
|
2020-09-28 20:41:41 +01:00
|
|
|
while (s->parent) {
|
2020-09-11 20:48:28 +01:00
|
|
|
s = s->parent;
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
2020-09-11 20:48:28 +01:00
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
static void
|
|
|
|
|
move_xwayland_sub_views_to_front(struct view *parent)
|
2020-09-11 20:48:28 +01:00
|
|
|
{
|
2020-09-28 20:41:41 +01:00
|
|
|
if (!parent || parent->type != LAB_XWAYLAND_VIEW) {
|
2020-09-11 20:48:28 +01:00
|
|
|
return;
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
2020-09-11 20:48:28 +01:00
|
|
|
struct view *view, *next;
|
|
|
|
|
wl_list_for_each_reverse_safe(view, next, &parent->server->views, link)
|
|
|
|
|
{
|
|
|
|
|
/* need to stop here, otherwise loops keeps going forever */
|
2020-09-28 20:41:41 +01:00
|
|
|
if (view == parent) {
|
2020-09-11 20:48:28 +01:00
|
|
|
break;
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
|
|
|
|
if (view->type != LAB_XWAYLAND_VIEW) {
|
2020-09-11 20:48:28 +01:00
|
|
|
continue;
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
|
|
|
|
if (!view->mapped && !view->minimized) {
|
2020-09-11 20:48:28 +01:00
|
|
|
continue;
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
|
|
|
|
if (top_parent_of(view) != parent->xwayland_surface) {
|
2020-09-11 20:48:28 +01:00
|
|
|
continue;
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
2020-09-11 20:48:28 +01:00
|
|
|
move_to_front(view);
|
|
|
|
|
/* TODO: we should probably focus on these too here */
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-30 10:29:21 +00:00
|
|
|
#endif
|
2020-09-11 20:48:28 +01:00
|
|
|
|
|
|
|
|
/* Activate/deactivate toplevel surface */
|
2020-09-28 20:41:41 +01:00
|
|
|
static void
|
|
|
|
|
set_activated(struct wlr_surface *surface, bool activated)
|
2020-09-11 20:48:28 +01:00
|
|
|
{
|
2020-09-28 20:41:41 +01:00
|
|
|
if (!surface) {
|
2020-09-11 20:48:28 +01:00
|
|
|
return;
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
2020-09-11 20:48:28 +01:00
|
|
|
if (wlr_surface_is_xdg_surface(surface)) {
|
2020-09-14 17:35:44 +01:00
|
|
|
struct wlr_xdg_surface *s;
|
|
|
|
|
s = wlr_xdg_surface_from_wlr_surface(surface);
|
|
|
|
|
wlr_xdg_toplevel_set_activated(s, activated);
|
2020-12-30 10:29:21 +00:00
|
|
|
#if HAVE_XWAYLAND
|
2020-09-14 17:35:44 +01:00
|
|
|
} else if (wlr_surface_is_xwayland_surface(surface)) {
|
|
|
|
|
struct wlr_xwayland_surface *s;
|
|
|
|
|
s = wlr_xwayland_surface_from_wlr_surface(surface);
|
|
|
|
|
wlr_xwayland_surface_activate(s, activated);
|
2020-12-30 10:29:21 +00:00
|
|
|
#endif
|
2020-09-11 20:48:28 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
void
|
2020-10-08 20:08:41 +01:00
|
|
|
desktop_focus_view(struct seat *seat, struct view *view)
|
2020-09-14 17:35:44 +01:00
|
|
|
{
|
2020-09-18 20:28:48 +01:00
|
|
|
if (!view) {
|
2020-10-08 20:22:52 +01:00
|
|
|
seat_focus_surface(seat, NULL);
|
2020-09-14 17:35:44 +01:00
|
|
|
return;
|
2020-09-18 20:28:48 +01:00
|
|
|
}
|
2020-09-28 20:41:41 +01:00
|
|
|
if (view->minimized) {
|
2020-10-08 20:08:41 +01:00
|
|
|
/* this will unmap and then focus */
|
2020-09-28 20:41:41 +01:00
|
|
|
view_unminimize(view);
|
2020-10-08 20:08:41 +01:00
|
|
|
return;
|
2020-09-28 20:41:41 +01:00
|
|
|
} else if (view->mapped) {
|
2020-10-08 20:08:41 +01:00
|
|
|
struct wlr_surface *prev_surface;
|
|
|
|
|
prev_surface = seat->seat->keyboard_state.focused_surface;
|
|
|
|
|
if (prev_surface == view->surface) {
|
|
|
|
|
/* Don't re-focus an already focused surface. */
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (prev_surface) {
|
|
|
|
|
set_activated(prev_surface, false);
|
|
|
|
|
}
|
|
|
|
|
move_to_front(view);
|
|
|
|
|
set_activated(view->surface, true);
|
2020-10-08 20:22:52 +01:00
|
|
|
seat_focus_surface(seat, view->surface);
|
2020-12-30 10:29:21 +00:00
|
|
|
#if HAVE_XWAYLAND
|
2020-10-08 20:08:41 +01:00
|
|
|
move_xwayland_sub_views_to_front(view);
|
2020-12-30 10:29:21 +00:00
|
|
|
#endif
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
2020-09-14 17:35:44 +01:00
|
|
|
}
|
|
|
|
|
|
2020-09-11 20:48:28 +01:00
|
|
|
/*
|
|
|
|
|
* Some xwayland apps produce unmapped surfaces on startup and also leave
|
|
|
|
|
* some unmapped surfaces kicking around on 'close' (for example * leafpad's
|
|
|
|
|
* "about" dialogue). Whilst this is not normally a problem, we have to be
|
2020-09-14 17:35:44 +01:00
|
|
|
* careful when cycling between views. The only views we should focus are
|
2020-09-11 20:48:28 +01:00
|
|
|
* those that are already mapped and those that have been minimized.
|
|
|
|
|
*/
|
2020-09-28 20:41:41 +01:00
|
|
|
static bool
|
|
|
|
|
isfocusable(struct view *view)
|
2020-09-11 20:48:28 +01:00
|
|
|
{
|
|
|
|
|
/* filter out those xwayland surfaces that have never been mapped */
|
2020-09-28 20:41:41 +01:00
|
|
|
if (!view->surface) {
|
2020-09-11 20:48:28 +01:00
|
|
|
return false;
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
2020-09-11 20:48:28 +01:00
|
|
|
return (view->mapped || view->minimized);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
static bool
|
|
|
|
|
has_focusable_view(struct wl_list *wl_list)
|
2020-09-11 20:48:28 +01:00
|
|
|
{
|
|
|
|
|
struct view *view;
|
|
|
|
|
wl_list_for_each (view, wl_list, link) {
|
2020-09-28 20:41:41 +01:00
|
|
|
if (isfocusable(view)) {
|
2020-09-11 20:48:28 +01:00
|
|
|
return true;
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
2020-09-11 20:48:28 +01:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
static struct view *
|
|
|
|
|
first_view(struct server *server)
|
2020-09-11 20:48:28 +01:00
|
|
|
{
|
|
|
|
|
struct view *view;
|
|
|
|
|
view = wl_container_of(server->views.next, view, link);
|
|
|
|
|
return view;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
struct view *
|
2020-10-31 14:46:33 +00:00
|
|
|
desktop_cycle_view(struct server *server, struct view *current)
|
2020-09-11 20:48:28 +01:00
|
|
|
{
|
2020-10-31 14:46:33 +00:00
|
|
|
if (!has_focusable_view(&server->views)) {
|
2020-09-11 20:48:28 +01:00
|
|
|
return NULL;
|
2020-10-31 14:46:33 +00:00
|
|
|
}
|
2020-09-11 20:48:28 +01:00
|
|
|
|
|
|
|
|
struct view *view = current ? current : first_view(server);
|
|
|
|
|
|
|
|
|
|
/* Replacement for wl_list_for_each_from() */
|
|
|
|
|
do {
|
|
|
|
|
view = wl_container_of(view->link.next, view, link);
|
|
|
|
|
} while (&view->link == &server->views || !isfocusable(view));
|
2021-01-09 22:51:20 +00:00
|
|
|
damage_all_outputs(server);
|
2020-09-11 20:48:28 +01:00
|
|
|
return view;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
static bool
|
|
|
|
|
has_mapped_view(struct wl_list *wl_list)
|
2020-09-18 20:28:48 +01:00
|
|
|
{
|
|
|
|
|
struct view *view;
|
|
|
|
|
wl_list_for_each (view, wl_list, link) {
|
2020-09-28 20:41:41 +01:00
|
|
|
if (view->mapped) {
|
2020-09-18 20:28:48 +01:00
|
|
|
return true;
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
2020-09-18 20:28:48 +01:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-31 14:32:31 +00:00
|
|
|
static struct view *
|
|
|
|
|
topmost_mapped_view(struct server *server)
|
2020-09-18 20:28:48 +01:00
|
|
|
{
|
2020-09-28 20:41:41 +01:00
|
|
|
if (!has_mapped_view(&server->views)) {
|
2020-09-18 20:28:48 +01:00
|
|
|
return NULL;
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
2020-10-31 14:32:31 +00:00
|
|
|
|
|
|
|
|
/* start from tail of server->views */
|
|
|
|
|
struct view *view = wl_container_of(server->views.prev, view, link);
|
2020-09-18 20:28:48 +01:00
|
|
|
do {
|
|
|
|
|
view = wl_container_of(view->link.next, view, link);
|
|
|
|
|
} while (&view->link == &server->views || !view->mapped);
|
|
|
|
|
return view;
|
|
|
|
|
}
|
2020-10-31 14:32:31 +00:00
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
void
|
2020-10-31 14:32:31 +00:00
|
|
|
desktop_focus_topmost_mapped_view(struct server *server)
|
2020-09-18 20:28:48 +01:00
|
|
|
{
|
2020-10-31 14:32:31 +00:00
|
|
|
struct view *view = topmost_mapped_view(server);
|
|
|
|
|
desktop_focus_view(&server->seat, view);
|
2020-09-18 20:28:48 +01:00
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
static bool
|
|
|
|
|
_view_at(struct view *view, double lx, double ly, struct wlr_surface **surface,
|
|
|
|
|
double *sx, double *sy)
|
2020-09-11 20:48:28 +01:00
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* 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;
|
2020-12-30 10:29:21 +00:00
|
|
|
#if HAVE_XWAYLAND
|
2020-09-11 20:48:28 +01:00
|
|
|
case LAB_XWAYLAND_VIEW:
|
2020-09-15 20:41:01 +01:00
|
|
|
_surface = wlr_surface_surface_at(view->surface, view_sx,
|
|
|
|
|
view_sy, &_sx, &_sy);
|
2020-09-11 20:48:28 +01:00
|
|
|
break;
|
2020-12-30 10:29:21 +00:00
|
|
|
#endif
|
2020-09-11 20:48:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_surface) {
|
|
|
|
|
*sx = _sx;
|
|
|
|
|
*sy = _sy;
|
|
|
|
|
*surface = _surface;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
struct view *
|
|
|
|
|
desktop_view_at(struct server *server, double lx, double ly,
|
|
|
|
|
struct wlr_surface **surface, double *sx, double *sy,
|
|
|
|
|
int *view_area)
|
2020-09-11 20:48:28 +01:00
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* 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) {
|
2020-09-28 20:41:41 +01:00
|
|
|
if (!view->mapped) {
|
2020-09-11 20:48:28 +01:00
|
|
|
continue;
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
|
|
|
|
if (_view_at(view, lx, ly, surface, sx, sy)) {
|
2020-09-11 20:48:28 +01:00
|
|
|
return view;
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
|
|
|
|
if (!view->server_side_deco) {
|
2020-09-11 20:48:28 +01:00
|
|
|
continue;
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
2021-03-20 14:41:39 +00:00
|
|
|
*view_area = ssd_at(view, lx, ly);
|
|
|
|
|
if (*view_area != LAB_SSD_NONE) {
|
2020-09-11 20:48:28 +01:00
|
|
|
return view;
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
2020-09-11 20:48:28 +01:00
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|