Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add layer shell exclusive zone and layout handling #143

Merged
merged 2 commits into from
Nov 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/Output.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ class Output : public OutputListeners
private:
void output_frame(wl_listener *listener, void *data);
void refreshImage();

void calculateMargins(std::array<uint16_t, 2> &offset, std::array<uint16_t, 2> &size);
};
20 changes: 17 additions & 3 deletions include/util/FixedPoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,24 @@ class FixedPoint
}

#define FIXEDPOINT_RELTATIVE_OP(OP) \
template<class OtherType> \
constexpr bool operator OP (FixedPoint<exponent, OtherType> const &other) const noexcept \
template<int otherExponent, class OtherType> \
constexpr bool operator OP (FixedPoint<otherExponent, OtherType> const &other) const noexcept \
{ \
return value OP other.value; \
if constexpr (otherExponent == exponent) \
return value OP other.value; \
else if constexpr (exponent > otherExponent) \
{ \
if (value != (other.value >> (exponent - otherExponent))) \
return value OP (other.value >> (exponent - otherExponent)); \
return (value << (exponent - otherExponent)) OP other.value; \
} \
else \
{ \
static_assert(otherExponent > exponent); \
if ((value >> (otherExponent - exponent)) != other.value) \
return (value >> (otherExponent - exponent)) OP other.value; \
return value OP (other.value << (otherExponent - exponent)); \
} \
}

FIXEDPOINT_RELTATIVE_OP(==);
Expand Down
141 changes: 128 additions & 13 deletions source/Output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,134 @@ void Output::refreshImage()
}
}

namespace
{
template<uint32_t anchorDir, uint32_t oppositeDir, bool direction>
void setLayerShellShape(std::array<uint16_t, 2> offset, std::array<uint16_t, 2> size, LayerSurface &layerSurface, int &outSize)
{
auto &current(wlr_layer_surface_v1_from_wlr_surface(layerSurface.surface)->current);
auto anchor(current.anchor);
auto &margin(current.margin);

if ((direction ?
margin.top + margin.bottom :
margin.right + margin.left) + outSize
> size[direction]) // takes full screen length
anchor |= anchorDir | oppositeDir; // effectivly behaves as if it were anchored both sides

if (FixedPoint<0, int> minPos(offset[direction] + (direction ? margin.top : margin.left));
(anchor & anchorDir) || (direction ? layerSurface.y : layerSurface.x) < minPos)
{
(direction ? layerSurface.y : layerSurface.x) = minPos;
if (anchor & oppositeDir)
{
outSize = size[direction] - (direction ? margin.top + margin.bottom : margin.right + margin.left);
}
}
else if (FixedPoint<0, int> maxPos(offset[direction] + size[direction] - (outSize + (direction ? margin.bottom : margin.right)));
(anchor & oppositeDir) || (direction ? layerSurface.y : layerSurface.x) > maxPos)
{
(direction ? layerSurface.y : layerSurface.x) = maxPos;
}
}

void setLayerShellShape(std::array<uint16_t, 2> offset, std::array<uint16_t, 2> size, LayerSurface &layerSurface)
{
auto &current(wlr_layer_surface_v1_from_wlr_surface(layerSurface.surface)->current);

int width = current.desired_width;
int height = current.desired_height;
setLayerShellShape<ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT, ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT, 0>(offset, size, layerSurface, width);
setLayerShellShape<ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP, ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM, 1>(offset, size, layerSurface, height);
if (width != current.actual_width || height != current.actual_height)
wlr_layer_surface_v1_configure(wlr_layer_surface_v1_from_wlr_surface(layerSurface.surface), width, height);
}

template<uint32_t anchorDir, uint32_t oppositeDir, bool direction, bool positive>
void tryDir(std::array<uint16_t, 2> &offset, std::array<uint16_t, 2> &size, LayerSurface &layerSurface)
{
auto exclusive_zone(wlr_layer_surface_v1_from_wlr_surface(layerSurface.surface)->current.exclusive_zone);
auto anchor(wlr_layer_surface_v1_from_wlr_surface(layerSurface.surface)->current.anchor);
auto *wlrLayerSurface(wlr_layer_surface_v1_from_wlr_surface(layerSurface.surface));
if ((anchor & anchorDir) && !(anchor & oppositeDir))
{
auto &margin(wlrLayerSurface->current.margin);
if constexpr (positive)
{
exclusive_zone += (direction ? margin.top : margin.left);
offset[direction] += exclusive_zone;
size[direction] -= exclusive_zone;
}
else
{
exclusive_zone += (direction ? margin.bottom : margin.right);
size[direction] -= exclusive_zone;
}
}
}
}

void Output::calculateMargins(std::array<uint16_t, 2> &offset, std::array<uint16_t, 2> &size)
{
for (auto &layer : layers)
for (auto &layerSurfacePtr : layer)
{
auto &layerSurface(*layerSurfacePtr);
auto exclusive_zone(wlr_layer_surface_v1_from_wlr_surface(layerSurface.surface)->current.exclusive_zone);
auto anchor(wlr_layer_surface_v1_from_wlr_surface(layerSurface.surface)->current.anchor);

if (exclusive_zone > 0 &&
(!!(anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP) + !!(anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM) +
!!(anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT) + !!(anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT) & 1))
{
setLayerShellShape(offset, size, layerSurface);
tryDir<ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP, ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM, 1, 1>(offset, size, layerSurface);
tryDir<ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT, ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT, 0, 1>(offset, size, layerSurface);
tryDir<ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM, ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP, 1, 0>(offset, size, layerSurface);
tryDir<ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT, ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT, 0, 0>(offset, size, layerSurface);
}
}
for (auto &layer : layers)
for (auto &layerSurfacePtr : layer)
{
auto &layerSurface(*layerSurfacePtr);
auto exclusive_zone(wlr_layer_surface_v1_from_wlr_surface(layerSurface.surface)->current.exclusive_zone);
auto anchor(wlr_layer_surface_v1_from_wlr_surface(layerSurface.surface)->current.anchor);

if (exclusive_zone == 0 ||
(exclusive_zone > 1 &&
!((!!(anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP) + !!(anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM) +
!!(anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT) + !!(anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT)) & 1)))
{
setLayerShellShape(offset, size, layerSurface);
}
}
}



void Output::output_frame(wl_listener *listener, void *data)
{
Server &server = Server::getInstance();

auto *box = wlr_output_layout_get_box(server.outputManager.getLayout(), wlr_output);
{
wm::WindowTree &windowTree = getWindowTree();

std::array<uint16_t, 2> pos({box->x, box->y});
std::array<uint16_t, 2> size({uint16_t(box->width), uint16_t(box->height)});

calculateMargins(pos, size);
std::array<FixedPoint<-4, int>, 2> posFp;

for (int i = 0; i < 2; ++i)
posFp[i] = FixedPoint<0, int>(pos[i]);
if (windowTree.getData(windowTree.getRootIndex()).getPosition() != posFp)
windowTree.getData(windowTree.getRootIndex()).move(windowTree.getRootIndex(), windowTree, posFp);
if (windowTree.getData(windowTree.getRootIndex()).getSize() != size)
windowTree.getData(windowTree.getRootIndex()).resize(windowTree.getRootIndex(), windowTree, size);
}

wlr_renderer *renderer = server.renderer;

timespec now;
Expand Down Expand Up @@ -167,16 +292,6 @@ void Output::output_frame(wl_listener *listener, void *data)
wlr_renderer_end(renderer);
wlr_output_commit(wlr_output);
refreshImage();
auto *box = wlr_output_layout_get_box(server.outputManager.getLayout(), wlr_output);
{
wm::WindowTree &windowTree = getWindowTree();

std::array<FixedPoint<-4, int>, 2> pos{{FixedPoint<0, int>(box->x), FixedPoint<0, int>(box->y)}};
if (windowTree.getData(windowTree.getRootIndex()).getPosition() != pos)
windowTree.getData(windowTree.getRootIndex()).move(windowTree.getRootIndex(), windowTree, pos);
if (windowTree.getData(windowTree.getRootIndex()).getSize() != std::array<uint16_t, 2u>{uint16_t(box->width), uint16_t(box->height)})
windowTree.getData(windowTree.getRootIndex()).resize(windowTree.getRootIndex(), windowTree, {uint16_t(box->width), uint16_t(box->height)});
}
}

void Output::setFrameListener()
Expand Down Expand Up @@ -220,8 +335,8 @@ void Output::removeLayerSurface(LayerSurface *layerSurface)
wlr_layer_surface_v1 *shell_surface = wlr_layer_surface_v1_from_wlr_surface(layerSurface->surface);

auto it(std::find_if(layers[shell_surface->current.layer].begin(),layers[shell_surface->current.layer].end(), [layerSurface](auto &a) noexcept
{
return a.get() == layerSurface;
}));
{
return a.get() == layerSurface;
}));
layers[shell_surface->current.layer].erase(it);
}
8 changes: 4 additions & 4 deletions source/OutputManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ void OutputManager::render_surface(wlr_surface *surface, int sx, int sy, void *d
}

wlr_box box = {
.x = int(ox * output->scale),
.y = int(oy * output->scale),
.width = int(float(surface->current.width) * output->scale),
.height = int(float(surface->current.height) * output->scale),
.x = int(ox * output->scale),
.y = int(oy * output->scale),
.width = int(float(surface->current.width) * output->scale),
.height = int(float(surface->current.height) * output->scale),
};

float matrix[9];
Expand Down