| 1 |
/* |
| 2 |
* Copyright (C) 2017-2018 Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "WindowDecorator.h" |
| 10 |
|
| 11 |
#include "Util.h" |
| 12 |
#include "utils/EndianSwap.h" |
| 13 |
#include "utils/log.h" |
| 14 |
|
| 15 |
#include <algorithm> |
| 16 |
#include <cassert> |
| 17 |
#include <cmath> |
| 18 |
#include <memory> |
| 19 |
#include <mutex> |
| 20 |
#include <vector> |
| 21 |
|
| 22 |
#include <linux/input.h> |
| 23 |
|
| 24 |
using namespace KODI::UTILS::POSIX; |
| 25 |
using namespace KODI::WINDOWING::WAYLAND; |
| 26 |
using namespace std::placeholders; |
| 27 |
|
| 28 |
namespace |
| 29 |
{ |
| 30 |
|
| 31 |
/// Bytes per pixel in shm storage |
| 32 |
constexpr int BYTES_PER_PIXEL{4}; |
| 33 |
/// Width of the visible border around the whole window |
| 34 |
constexpr int VISIBLE_BORDER_WIDTH{5}; |
| 35 |
/// Width of the invisible border around the whole window for easier resizing |
| 36 |
constexpr int RESIZE_BORDER_WIDTH{10}; |
| 37 |
/// Total width of the border around the window |
| 38 |
constexpr int BORDER_WIDTH{VISIBLE_BORDER_WIDTH + RESIZE_BORDER_WIDTH}; |
| 39 |
/// Height of the top bar |
| 40 |
constexpr int TOP_BAR_HEIGHT{33}; |
| 41 |
/// Maximum distance from the window corner to consider position valid for resize |
| 42 |
constexpr int RESIZE_MAX_CORNER_DISTANCE{BORDER_WIDTH}; |
| 43 |
/// Distance of buttons from edges of the top bar |
| 44 |
constexpr int BUTTONS_EDGE_DISTANCE{6}; |
| 45 |
/// Distance from button inner edge to button content |
| 46 |
constexpr int BUTTON_INNER_SEPARATION{4}; |
| 47 |
/// Button size |
| 48 |
constexpr int BUTTON_SIZE{21}; |
| 49 |
|
| 50 |
constexpr std::uint32_t TRANSPARENT{0x00000000u}; |
| 51 |
constexpr std::uint32_t BORDER_COLOR{0xFF000000u}; |
| 52 |
constexpr std::uint32_t BUTTON_COLOR_ACTIVE{0xFFFFFFFFu}; |
| 53 |
constexpr std::uint32_t BUTTON_COLOR_INACTIVE{0xFF777777u}; |
| 54 |
constexpr std::uint32_t BUTTON_HOVER_COLOR{0xFF555555u}; |
| 55 |
|
| 56 |
static_assert(BUTTON_SIZE <= TOP_BAR_HEIGHT - BUTTONS_EDGE_DISTANCE * 2, "Buttons must fit in top bar"); |
| 57 |
|
| 58 |
/* |
| 59 |
* Decorations consist of four surfaces, one for each edge of the window. It would |
| 60 |
* also be possible to position one single large surface behind the main surface |
| 61 |
* instead, but that would waste a lot of memory on big/high-density screens. |
| 62 |
* |
| 63 |
* The four surfaces are laid out as follows: Top and bottom surfaces go over the |
| 64 |
* whole width of the main surface plus the left and right borders. |
| 65 |
* Left and right surfaces only go over the height of the main surface without |
| 66 |
* the top and bottom borders. |
| 67 |
* |
| 68 |
* --------------------------------------------- |
| 69 |
* | TOP | |
| 70 |
* --------------------------------------------- |
| 71 |
* | | | | |
| 72 |
* | L | | R | |
| 73 |
* | E | | I | |
| 74 |
* | F | Main surface | G | |
| 75 |
* | T | | H | |
| 76 |
* | | | T | |
| 77 |
* | | | | |
| 78 |
* --------------------------------------------- |
| 79 |
* | BOTTOM | |
| 80 |
* --------------------------------------------- |
| 81 |
*/ |
| 82 |
|
| 83 |
|
| 84 |
CRectInt SurfaceGeometry(SurfaceIndex type, CSizeInt mainSurfaceSize) |
| 85 |
{ |
| 86 |
// Coordinates are relative to main surface |
| 87 |
switch (type) |
| 88 |
{ |
| 89 |
case SURFACE_TOP: |
| 90 |
return { |
| 91 |
CPointInt{-BORDER_WIDTH, -(BORDER_WIDTH + TOP_BAR_HEIGHT)}, |
| 92 |
CSizeInt{mainSurfaceSize.Width() + 2 * BORDER_WIDTH, TOP_BAR_HEIGHT + BORDER_WIDTH} |
| 93 |
}; |
| 94 |
case SURFACE_RIGHT: |
| 95 |
return { |
| 96 |
CPointInt{mainSurfaceSize.Width(), 0}, |
| 97 |
CSizeInt{BORDER_WIDTH, mainSurfaceSize.Height()} |
| 98 |
}; |
| 99 |
case SURFACE_BOTTOM: |
| 100 |
return { |
| 101 |
CPointInt{-BORDER_WIDTH, mainSurfaceSize.Height()}, |
| 102 |
CSizeInt{mainSurfaceSize.Width() + 2 * BORDER_WIDTH, BORDER_WIDTH} |
| 103 |
}; |
| 104 |
case SURFACE_LEFT: |
| 105 |
return { |
| 106 |
CPointInt{-BORDER_WIDTH, 0}, |
| 107 |
CSizeInt{BORDER_WIDTH, mainSurfaceSize.Height()} |
| 108 |
}; |
| 109 |
default: |
| 110 |
throw std::logic_error("Invalid surface type"); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
CRectInt SurfaceOpaqueRegion(SurfaceIndex type, CSizeInt mainSurfaceSize) |
| 115 |
{ |
| 116 |
// Coordinates are relative to main surface |
| 117 |
auto size = SurfaceGeometry(type, mainSurfaceSize).ToSize(); |
| 118 |
switch (type) |
| 119 |
{ |
| 120 |
case SURFACE_TOP: |
| 121 |
return { |
| 122 |
CPointInt{RESIZE_BORDER_WIDTH, RESIZE_BORDER_WIDTH}, |
| 123 |
size - CSizeInt{RESIZE_BORDER_WIDTH * 2, RESIZE_BORDER_WIDTH} |
| 124 |
}; |
| 125 |
case SURFACE_RIGHT: |
| 126 |
return { |
| 127 |
CPointInt{}, |
| 128 |
size - CSizeInt{RESIZE_BORDER_WIDTH, 0} |
| 129 |
}; |
| 130 |
case SURFACE_BOTTOM: |
| 131 |
return { |
| 132 |
CPointInt{RESIZE_BORDER_WIDTH, 0}, |
| 133 |
size - CSizeInt{RESIZE_BORDER_WIDTH * 2, RESIZE_BORDER_WIDTH} |
| 134 |
}; |
| 135 |
case SURFACE_LEFT: |
| 136 |
return { |
| 137 |
CPointInt{RESIZE_BORDER_WIDTH, 0}, |
| 138 |
size - CSizeInt{RESIZE_BORDER_WIDTH, 0} |
| 139 |
}; |
| 140 |
default: |
| 141 |
throw std::logic_error("Invalid surface type"); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
CRectInt SurfaceWindowRegion(SurfaceIndex type, CSizeInt mainSurfaceSize) |
| 146 |
{ |
| 147 |
return SurfaceOpaqueRegion(type, mainSurfaceSize); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Full size of decorations to be added to the main surface size |
| 152 |
*/ |
| 153 |
CSizeInt DecorationSize() |
| 154 |
{ |
| 155 |
return {2 * VISIBLE_BORDER_WIDTH, 2 * VISIBLE_BORDER_WIDTH + TOP_BAR_HEIGHT}; |
| 156 |
} |
| 157 |
|
| 158 |
std::size_t MemoryBytesForSize(CSizeInt windowSurfaceSize, int scale) |
| 159 |
{ |
| 160 |
std::size_t size{}; |
| 161 |
|
| 162 |
for (auto surface : { SURFACE_TOP, SURFACE_RIGHT, SURFACE_BOTTOM, SURFACE_LEFT }) |
| 163 |
{ |
| 164 |
size += SurfaceGeometry(surface, windowSurfaceSize).Area(); |
| 165 |
} |
| 166 |
|
| 167 |
size *= scale * scale; |
| 168 |
|
| 169 |
size *= BYTES_PER_PIXEL; |
| 170 |
|
| 171 |
return size; |
| 172 |
} |
| 173 |
|
| 174 |
std::size_t PositionInBuffer(CWindowDecorator::Buffer& buffer, CPointInt position) |
| 175 |
{ |
| 176 |
if (position.x < 0 || position.y < 0) |
| 177 |
{ |
| 178 |
throw std::invalid_argument("Position out of bounds"); |
| 179 |
} |
| 180 |
std::size_t offset{static_cast<std::size_t> (buffer.size.Width() * position.y + position.x)}; |
| 181 |
if (offset * BYTES_PER_PIXEL >= buffer.dataSize) |
| 182 |
{ |
| 183 |
throw std::invalid_argument("Position out of bounds"); |
| 184 |
} |
| 185 |
return offset; |
| 186 |
} |
| 187 |
|
| 188 |
void DrawHorizontalLine(CWindowDecorator::Buffer& buffer, std::uint32_t color, CPointInt position, int length) |
| 189 |
{ |
| 190 |
auto offsetStart = PositionInBuffer(buffer, position); |
| 191 |
auto offsetEnd = PositionInBuffer(buffer, position + CPointInt{length - 1, 0}); |
| 192 |
if (offsetEnd < offsetStart) |
| 193 |
{ |
| 194 |
throw std::invalid_argument("Invalid drawing coordinates"); |
| 195 |
} |
| 196 |
std::fill(buffer.RgbaBuffer() + offsetStart, buffer.RgbaBuffer() + offsetEnd + 1, Endian_SwapLE32(color)); |
| 197 |
} |
| 198 |
|
| 199 |
void DrawLineWithStride(CWindowDecorator::Buffer& buffer, std::uint32_t color, CPointInt position, int length, int stride) |
| 200 |
{ |
| 201 |
auto offsetStart = PositionInBuffer(buffer, position); |
| 202 |
auto offsetEnd = offsetStart + stride * (length - 1); |
| 203 |
if (offsetEnd * BYTES_PER_PIXEL >= buffer.dataSize) |
| 204 |
{ |
| 205 |
throw std::invalid_argument("Position out of bounds"); |
| 206 |
} |
| 207 |
if (offsetEnd < offsetStart) |
| 208 |
{ |
| 209 |
throw std::invalid_argument("Invalid drawing coordinates"); |
| 210 |
} |
| 211 |
auto* memory = buffer.RgbaBuffer(); |
| 212 |
for (std::size_t offset = offsetStart; offset <= offsetEnd; offset += stride) |
| 213 |
{ |
| 214 |
*(memory + offset) = Endian_SwapLE32(color); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
void DrawVerticalLine(CWindowDecorator::Buffer& buffer, std::uint32_t color, CPointInt position, int length) |
| 219 |
{ |
| 220 |
DrawLineWithStride(buffer, color, position, length, buffer.size.Width()); |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Draw rectangle inside the specified buffer coordinates |
| 225 |
*/ |
| 226 |
void DrawRectangle(CWindowDecorator::Buffer& buffer, std::uint32_t color, CRectInt rect) |
| 227 |
{ |
| 228 |
DrawHorizontalLine(buffer, color, rect.P1(), rect.Width()); |
| 229 |
DrawVerticalLine(buffer, color, rect.P1(), rect.Height()); |
| 230 |
DrawHorizontalLine(buffer, color, rect.P1() + CPointInt{1, rect.Height() - 1}, rect.Width() - 1); |
| 231 |
DrawVerticalLine(buffer, color, rect.P1() + CPointInt{rect.Width() - 1, 1}, rect.Height() - 1); |
| 232 |
} |
| 233 |
|
| 234 |
void FillRectangle(CWindowDecorator::Buffer& buffer, std::uint32_t color, CRectInt rect) |
| 235 |
{ |
| 236 |
for (int y{rect.y1}; y <= rect.y2; y++) |
| 237 |
{ |
| 238 |
DrawHorizontalLine(buffer, color, {rect.x1, y}, rect.Width() + 1); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
void DrawHorizontalLine(CWindowDecorator::Surface& surface, std::uint32_t color, CPointInt position, int length) |
| 243 |
{ |
| 244 |
for (int i{0}; i < surface.scale; i++) |
| 245 |
{ |
| 246 |
DrawHorizontalLine(surface.buffer, color, position * surface.scale + CPointInt{0, i}, length * surface.scale); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
void DrawAngledLine(CWindowDecorator::Surface& surface, std::uint32_t color, CPointInt position, int length, int stride) |
| 251 |
{ |
| 252 |
for (int i{0}; i < surface.scale; i++) |
| 253 |
{ |
| 254 |
DrawLineWithStride(surface.buffer, color, position * surface.scale + CPointInt{i, 0}, length * surface.scale, surface.buffer.size.Width() + stride); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
void DrawVerticalLine(CWindowDecorator::Surface& surface, std::uint32_t color, CPointInt position, int length) |
| 259 |
{ |
| 260 |
DrawAngledLine(surface, color, position, length, 0); |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Draw rectangle inside the specified surface coordinates |
| 265 |
*/ |
| 266 |
void DrawRectangle(CWindowDecorator::Surface& surface, std::uint32_t color, CRectInt rect) |
| 267 |
{ |
| 268 |
for (int i{0}; i < surface.scale; i++) |
| 269 |
{ |
| 270 |
DrawRectangle(surface.buffer, color, {rect.P1() * surface.scale + CPointInt{i, i}, (rect.P2() + CPointInt{1, 1}) * surface.scale - CPointInt{i, i} - CPointInt{1, 1}}); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
void FillRectangle(CWindowDecorator::Surface& surface, std::uint32_t color, CRectInt rect) |
| 275 |
{ |
| 276 |
FillRectangle(surface.buffer, color, {rect.P1() * surface.scale, (rect.P2() + CPointInt{1, 1}) * surface.scale - CPointInt{1, 1}}); |
| 277 |
} |
| 278 |
|
| 279 |
void DrawButton(CWindowDecorator::Surface& surface, std::uint32_t lineColor, CRectInt rect, bool hover) |
| 280 |
{ |
| 281 |
if (hover) |
| 282 |
{ |
| 283 |
FillRectangle(surface, BUTTON_HOVER_COLOR, rect); |
| 284 |
} |
| 285 |
DrawRectangle(surface, lineColor, rect); |
| 286 |
} |
| 287 |
|
| 288 |
wayland::shell_surface_resize ResizeEdgeForPosition(SurfaceIndex surface, CSizeInt surfaceSize, CPointInt position) |
| 289 |
{ |
| 290 |
switch (surface) |
| 291 |
{ |
| 292 |
case SURFACE_TOP: |
| 293 |
if (position.y <= RESIZE_MAX_CORNER_DISTANCE) |
| 294 |
{ |
| 295 |
if (position.x <= RESIZE_MAX_CORNER_DISTANCE) |
| 296 |
{ |
| 297 |
return wayland::shell_surface_resize::top_left; |
| 298 |
} |
| 299 |
else if (position.x >= surfaceSize.Width() - RESIZE_MAX_CORNER_DISTANCE) |
| 300 |
{ |
| 301 |
return wayland::shell_surface_resize::top_right; |
| 302 |
} |
| 303 |
else |
| 304 |
{ |
| 305 |
return wayland::shell_surface_resize::top; |
| 306 |
} |
| 307 |
} |
| 308 |
else |
| 309 |
{ |
| 310 |
if (position.x <= RESIZE_MAX_CORNER_DISTANCE) |
| 311 |
{ |
| 312 |
return wayland::shell_surface_resize::left; |
| 313 |
} |
| 314 |
else if (position.x >= surfaceSize.Width() - RESIZE_MAX_CORNER_DISTANCE) |
| 315 |
{ |
| 316 |
return wayland::shell_surface_resize::right; |
| 317 |
} |
| 318 |
else |
| 319 |
{ |
| 320 |
// Inside title bar, not resizing |
| 321 |
return wayland::shell_surface_resize::none; |
| 322 |
} |
| 323 |
} |
| 324 |
case SURFACE_RIGHT: |
| 325 |
if (position.y >= surfaceSize.Height() - RESIZE_MAX_CORNER_DISTANCE) |
| 326 |
{ |
| 327 |
return wayland::shell_surface_resize::bottom_right; |
| 328 |
} |
| 329 |
else |
| 330 |
{ |
| 331 |
return wayland::shell_surface_resize::right; |
| 332 |
} |
| 333 |
case SURFACE_BOTTOM: |
| 334 |
if (position.x <= RESIZE_MAX_CORNER_DISTANCE) |
| 335 |
{ |
| 336 |
return wayland::shell_surface_resize::bottom_left; |
| 337 |
} |
| 338 |
else if (position.x >= surfaceSize.Width() - RESIZE_MAX_CORNER_DISTANCE) |
| 339 |
{ |
| 340 |
return wayland::shell_surface_resize::bottom_right; |
| 341 |
} |
| 342 |
else |
| 343 |
{ |
| 344 |
return wayland::shell_surface_resize::bottom; |
| 345 |
} |
| 346 |
case SURFACE_LEFT: |
| 347 |
if (position.y >= surfaceSize.Height() - RESIZE_MAX_CORNER_DISTANCE) |
| 348 |
{ |
| 349 |
return wayland::shell_surface_resize::bottom_left; |
| 350 |
} |
| 351 |
else |
| 352 |
{ |
| 353 |
return wayland::shell_surface_resize::left; |
| 354 |
} |
| 355 |
|
| 356 |
default: |
| 357 |
return wayland::shell_surface_resize::none; |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
/** |
| 362 |
* Get name for resize cursor according to xdg cursor-spec |
| 363 |
*/ |
| 364 |
std::string CursorForResizeEdge(wayland::shell_surface_resize edge) |
| 365 |
{ |
| 366 |
if (edge == wayland::shell_surface_resize::top) |
| 367 |
return "n-resize"; |
| 368 |
else if (edge == wayland::shell_surface_resize::bottom) |
| 369 |
return "s-resize"; |
| 370 |
else if (edge == wayland::shell_surface_resize::left) |
| 371 |
return "w-resize"; |
| 372 |
else if (edge == wayland::shell_surface_resize::top_left) |
| 373 |
return "nw-resize"; |
| 374 |
else if (edge == wayland::shell_surface_resize::bottom_left) |
| 375 |
return "sw-resize"; |
| 376 |
else if (edge == wayland::shell_surface_resize::right) |
| 377 |
return "e-resize"; |
| 378 |
else if (edge == wayland::shell_surface_resize::top_right) |
| 379 |
return "ne-resize"; |
| 380 |
else if (edge == wayland::shell_surface_resize::bottom_right) |
| 381 |
return "se-resize"; |
| 382 |
else |
| 383 |
return ""; |
| 384 |
} |
| 385 |
|
| 386 |
template<typename T, typename InstanceProviderT> |
| 387 |
bool HandleCapabilityChange(const wayland::seat_capability& caps, |
| 388 |
const wayland::seat_capability& cap, |
| 389 |
T& proxy, |
| 390 |
InstanceProviderT instanceProvider) |
| 391 |
{ |
| 392 |
bool hasCapability = caps & cap; |
| 393 |
|
| 394 |
if ((!!proxy) != hasCapability) |
| 395 |
{ |
| 396 |
// Capability changed |
| 397 |
|
| 398 |
if (hasCapability) |
| 399 |
{ |
| 400 |
// The capability was added |
| 401 |
proxy = instanceProvider(); |
| 402 |
return true; |
| 403 |
} |
| 404 |
else |
| 405 |
{ |
| 406 |
// The capability was removed |
| 407 |
proxy.proxy_release(); |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
return false; |
| 412 |
} |
| 413 |
|
| 414 |
} |
| 415 |
|
| 416 |
CWindowDecorator::CWindowDecorator(IWindowDecorationHandler& handler, CConnection& connection, wayland::surface_t const& mainSurface) |
| 417 |
: m_handler{handler}, m_registry{connection}, m_mainSurface{mainSurface}, m_buttonColor{BUTTON_COLOR_ACTIVE} |
| 418 |
{ |
| 419 |
static_assert(std::tuple_size<decltype(m_borderSurfaces)>::value == SURFACE_COUNT, "SURFACE_COUNT must match surfaces array size"); |
| 420 |
|
| 421 |
m_registry.RequestSingleton(m_compositor, 1, 4); |
| 422 |
m_registry.RequestSingleton(m_subcompositor, 1, 1, false); |
| 423 |
m_registry.RequestSingleton(m_shm, 1, 1); |
| 424 |
|
| 425 |
m_registry.Bind(); |
| 426 |
} |
| 427 |
|
| 428 |
void CWindowDecorator::AddSeat(CSeat* seat) |
| 429 |
{ |
| 430 |
m_seats.emplace(std::piecewise_construct, std::forward_as_tuple(seat->GetGlobalName()), std::forward_as_tuple(seat)); |
| 431 |
seat->AddRawInputHandlerTouch(this); |
| 432 |
seat->AddRawInputHandlerPointer(this); |
| 433 |
} |
| 434 |
|
| 435 |
void CWindowDecorator::RemoveSeat(CSeat* seat) |
| 436 |
{ |
| 437 |
seat->RemoveRawInputHandlerTouch(this); |
| 438 |
seat->RemoveRawInputHandlerPointer(this); |
| 439 |
m_seats.erase(seat->GetGlobalName()); |
| 440 |
UpdateButtonHoverState(); |
| 441 |
} |
| 442 |
|
| 443 |
void CWindowDecorator::OnPointerEnter(CSeat* seat, |
| 444 |
std::uint32_t serial, |
| 445 |
const wayland::surface_t& surface, |
| 446 |
double surfaceX, |
| 447 |
double surfaceY) |
| 448 |
{ |
| 449 |
auto seatStateI = m_seats.find(seat->GetGlobalName()); |
| 450 |
if (seatStateI == m_seats.end()) |
| 451 |
{ |
| 452 |
return; |
| 453 |
} |
| 454 |
auto& seatState = seatStateI->second; |
| 455 |
// Reset first so we ignore events for surfaces we don't handle |
| 456 |
seatState.currentSurface = SURFACE_COUNT; |
| 457 |
std::unique_lock<CCriticalSection> lock(m_mutex); |
| 458 |
for (std::size_t i{0}; i < m_borderSurfaces.size(); i++) |
| 459 |
{ |
| 460 |
if (m_borderSurfaces[i].surface.wlSurface == surface) |
| 461 |
{ |
| 462 |
seatState.pointerEnterSerial = serial; |
| 463 |
seatState.currentSurface = static_cast<SurfaceIndex> (i); |
| 464 |
seatState.pointerPosition = {static_cast<float> (surfaceX), static_cast<float> (surfaceY)}; |
| 465 |
UpdateSeatCursor(seatState); |
| 466 |
UpdateButtonHoverState(); |
| 467 |
break; |
| 468 |
} |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
void CWindowDecorator::OnPointerLeave(CSeat* seat, |
| 473 |
std::uint32_t serial, |
| 474 |
const wayland::surface_t& surface) |
| 475 |
{ |
| 476 |
auto seatStateI = m_seats.find(seat->GetGlobalName()); |
| 477 |
if (seatStateI == m_seats.end()) |
| 478 |
{ |
| 479 |
return; |
| 480 |
} |
| 481 |
auto& seatState = seatStateI->second; |
| 482 |
seatState.currentSurface = SURFACE_COUNT; |
| 483 |
// Recreate cursor surface on reenter |
| 484 |
seatState.cursorName.clear(); |
| 485 |
seatState.cursor.proxy_release(); |
| 486 |
UpdateButtonHoverState(); |
| 487 |
} |
| 488 |
|
| 489 |
void CWindowDecorator::OnPointerMotion(CSeat* seat, std::uint32_t time, double surfaceX, double surfaceY) |
| 490 |
{ |
| 491 |
auto seatStateI = m_seats.find(seat->GetGlobalName()); |
| 492 |
if (seatStateI == m_seats.end()) |
| 493 |
{ |
| 494 |
return; |
| 495 |
} |
| 496 |
auto& seatState = seatStateI->second; |
| 497 |
if (seatState.currentSurface != SURFACE_COUNT) |
| 498 |
{ |
| 499 |
seatState.pointerPosition = {static_cast<float> (surfaceX), static_cast<float> (surfaceY)}; |
| 500 |
UpdateSeatCursor(seatState); |
| 501 |
UpdateButtonHoverState(); |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
void CWindowDecorator::OnPointerButton(CSeat* seat, std::uint32_t serial, std::uint32_t time, std::uint32_t button, wayland::pointer_button_state state) |
| 506 |
{ |
| 507 |
auto seatStateI = m_seats.find(seat->GetGlobalName()); |
| 508 |
if (seatStateI == m_seats.end()) |
| 509 |
{ |
| 510 |
return; |
| 511 |
} |
| 512 |
const auto& seatState = seatStateI->second; |
| 513 |
if (seatState.currentSurface != SURFACE_COUNT && state == wayland::pointer_button_state::pressed) |
| 514 |
{ |
| 515 |
HandleSeatClick(seatState, seatState.currentSurface, serial, button, seatState.pointerPosition); |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
void CWindowDecorator::OnTouchDown(CSeat* seat, |
| 520 |
std::uint32_t serial, |
| 521 |
std::uint32_t time, |
| 522 |
const wayland::surface_t& surface, |
| 523 |
std::int32_t id, |
| 524 |
double x, |
| 525 |
double y) |
| 526 |
{ |
| 527 |
auto seatStateI = m_seats.find(seat->GetGlobalName()); |
| 528 |
if (seatStateI == m_seats.end()) |
| 529 |
{ |
| 530 |
return; |
| 531 |
} |
| 532 |
auto& seatState = seatStateI->second; |
| 533 |
std::unique_lock<CCriticalSection> lock(m_mutex); |
| 534 |
for (std::size_t i{0}; i < m_borderSurfaces.size(); i++) |
| 535 |
{ |
| 536 |
if (m_borderSurfaces[i].surface.wlSurface == surface) |
| 537 |
{ |
| 538 |
HandleSeatClick(seatState, static_cast<SurfaceIndex> (i), serial, BTN_LEFT, {static_cast<float> (x), static_cast<float> (y)}); |
| 539 |
} |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
void CWindowDecorator::UpdateSeatCursor(SeatState& seatState) |
| 544 |
{ |
| 545 |
if (seatState.currentSurface == SURFACE_COUNT) |
| 546 |
{ |
| 547 |
// Don't set anything if not on any surface |
| 548 |
return; |
| 549 |
} |
| 550 |
|
| 551 |
LoadCursorTheme(); |
| 552 |
|
| 553 |
std::string cursorName{"default"}; |
| 554 |
|
| 555 |
{ |
| 556 |
std::unique_lock<CCriticalSection> lock(m_mutex); |
| 557 |
auto resizeEdge = ResizeEdgeForPosition(seatState.currentSurface, SurfaceGeometry(seatState.currentSurface, m_mainSurfaceSize).ToSize(), CPointInt{seatState.pointerPosition}); |
| 558 |
if (resizeEdge != wayland::shell_surface_resize::none) |
| 559 |
{ |
| 560 |
cursorName = CursorForResizeEdge(resizeEdge); |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
if (cursorName == seatState.cursorName) |
| 565 |
{ |
| 566 |
// Don't reload cursor all the time when nothing is changing |
| 567 |
return; |
| 568 |
} |
| 569 |
seatState.cursorName = cursorName; |
| 570 |
|
| 571 |
wayland::cursor_t cursor; |
| 572 |
try |
| 573 |
{ |
| 574 |
cursor = CCursorUtil::LoadFromTheme(m_cursorTheme, cursorName); |
| 575 |
} |
| 576 |
catch (std::exception const& e) |
| 577 |
{ |
| 578 |
CLog::LogF(LOGERROR, "Could not get required cursor {} from cursor theme: {}", cursorName, |
| 579 |
e.what()); |
| 580 |
return; |
| 581 |
} |
| 582 |
auto cursorImage = cursor.image(0); |
| 583 |
|
| 584 |
if (!seatState.cursor) |
| 585 |
{ |
| 586 |
seatState.cursor = m_compositor.create_surface(); |
| 587 |
} |
| 588 |
int calcScale{seatState.cursor.can_set_buffer_scale() ? m_scale : 1}; |
| 589 |
|
| 590 |
seatState.seat->SetCursor(seatState.pointerEnterSerial, seatState.cursor, cursorImage.hotspot_x() / calcScale, cursorImage.hotspot_y() / calcScale); |
| 591 |
seatState.cursor.attach(cursorImage.get_buffer(), 0, 0); |
| 592 |
seatState.cursor.damage(0, 0, cursorImage.width() / calcScale, cursorImage.height() / calcScale); |
| 593 |
if (seatState.cursor.can_set_buffer_scale()) |
| 594 |
{ |
| 595 |
seatState.cursor.set_buffer_scale(m_scale); |
| 596 |
} |
| 597 |
seatState.cursor.commit(); |
| 598 |
} |
| 599 |
|
| 600 |
void CWindowDecorator::UpdateButtonHoverState() |
| 601 |
{ |
| 602 |
std::vector<CPoint> pointerPositions; |
| 603 |
|
| 604 |
std::unique_lock<CCriticalSection> lock(m_mutex); |
| 605 |
|
| 606 |
for (auto const& seatPair : m_seats) |
| 607 |
{ |
| 608 |
auto const& seat = seatPair.second; |
| 609 |
if (seat.currentSurface == SURFACE_TOP) |
| 610 |
{ |
| 611 |
pointerPositions.emplace_back(seat.pointerPosition); |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
bool changed{false}; |
| 616 |
for (auto& button : m_buttons) |
| 617 |
{ |
| 618 |
bool wasHovered{button.hovered}; |
| 619 |
button.hovered = std::any_of(pointerPositions.cbegin(), pointerPositions.cend(), [&](CPoint point) { return button.position.PtInRect(CPointInt{point}); }); |
| 620 |
changed = changed || (button.hovered != wasHovered); |
| 621 |
} |
| 622 |
|
| 623 |
if (changed) |
| 624 |
{ |
| 625 |
// Repaint! |
| 626 |
Reset(false); |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
void CWindowDecorator::HandleSeatClick(SeatState const& seatState, SurfaceIndex surface, std::uint32_t serial, std::uint32_t button, CPoint position) |
| 631 |
{ |
| 632 |
switch (button) |
| 633 |
{ |
| 634 |
case BTN_LEFT: |
| 635 |
{ |
| 636 |
std::unique_lock<CCriticalSection> lock(m_mutex); |
| 637 |
auto resizeEdge = ResizeEdgeForPosition(surface, SurfaceGeometry(surface, m_mainSurfaceSize).ToSize(), CPointInt{position}); |
| 638 |
if (resizeEdge == wayland::shell_surface_resize::none) |
| 639 |
{ |
| 640 |
for (auto const& button : m_buttons) |
| 641 |
{ |
| 642 |
if (button.position.PtInRect(CPointInt{position})) |
| 643 |
{ |
| 644 |
button.onClick(); |
| 645 |
return; |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
m_handler.OnWindowMove(seatState.seat->GetWlSeat(), serial); |
| 650 |
} |
| 651 |
else |
| 652 |
{ |
| 653 |
m_handler.OnWindowResize(seatState.seat->GetWlSeat(), serial, resizeEdge); |
| 654 |
} |
| 655 |
} |
| 656 |
break; |
| 657 |
case BTN_RIGHT: |
| 658 |
if (surface == SURFACE_TOP) |
| 659 |
{ |
| 660 |
m_handler.OnWindowShowContextMenu(seatState.seat->GetWlSeat(), serial, CPointInt{position} - CPointInt{BORDER_WIDTH, BORDER_WIDTH + TOP_BAR_HEIGHT}); |
| 661 |
} |
| 662 |
break; |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
CWindowDecorator::BorderSurface CWindowDecorator::MakeBorderSurface() |
| 667 |
{ |
| 668 |
Surface surface; |
| 669 |
surface.wlSurface = m_compositor.create_surface(); |
| 670 |
auto subsurface = m_subcompositor.get_subsurface(surface.wlSurface, m_mainSurface); |
| 671 |
|
| 672 |
CWindowDecorator::BorderSurface boarderSurface; |
| 673 |
boarderSurface.surface = surface; |
| 674 |
boarderSurface.subsurface = subsurface; |
| 675 |
|
| 676 |
return boarderSurface; |
| 677 |
} |
| 678 |
|
| 679 |
bool CWindowDecorator::IsDecorationActive() const |
| 680 |
{ |
| 681 |
return StateHasWindowDecorations(m_windowState); |
| 682 |
} |
| 683 |
|
| 684 |
bool CWindowDecorator::StateHasWindowDecorations(IShellSurface::StateBitset state) const |
| 685 |
{ |
| 686 |
// No decorations possible if subcompositor not available |
| 687 |
return m_subcompositor && !state.test(IShellSurface::STATE_FULLSCREEN); |
| 688 |
} |
| 689 |
|
| 690 |
CSizeInt CWindowDecorator::CalculateMainSurfaceSize(CSizeInt size, IShellSurface::StateBitset state) const |
| 691 |
{ |
| 692 |
if (StateHasWindowDecorations(state)) |
| 693 |
{ |
| 694 |
// Subtract decorations |
| 695 |
return size - DecorationSize(); |
| 696 |
} |
| 697 |
else |
| 698 |
{ |
| 699 |
// Fullscreen -> no decorations |
| 700 |
return size; |
| 701 |
} |
| 702 |
} |
| 703 |
|
| 704 |
CSizeInt CWindowDecorator::CalculateFullSurfaceSize(CSizeInt size, IShellSurface::StateBitset state) const |
| 705 |
{ |
| 706 |
if (StateHasWindowDecorations(state)) |
| 707 |
{ |
| 708 |
// Add decorations |
| 709 |
return size + DecorationSize(); |
| 710 |
} |
| 711 |
else |
| 712 |
{ |
| 713 |
// Fullscreen -> no decorations |
| 714 |
return size; |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
void CWindowDecorator::SetState(CSizeInt size, int scale, IShellSurface::StateBitset state) |
| 719 |
{ |
| 720 |
CSizeInt mainSurfaceSize{CalculateMainSurfaceSize(size, state)}; |
| 721 |
std::unique_lock<CCriticalSection> lock(m_mutex); |
| 722 |
if (mainSurfaceSize == m_mainSurfaceSize && scale == m_scale && state == m_windowState) |
| 723 |
{ |
| 724 |
return; |
| 725 |
} |
| 726 |
|
| 727 |
bool wasDecorations{IsDecorationActive()}; |
| 728 |
m_windowState = state; |
| 729 |
|
| 730 |
m_buttonColor = m_windowState.test(IShellSurface::STATE_ACTIVATED) ? BUTTON_COLOR_ACTIVE : BUTTON_COLOR_INACTIVE; |
| 731 |
|
| 732 |
CLog::Log(LOGDEBUG, |
| 733 |
"CWindowDecorator::SetState: Setting full surface size {}x{} scale {} (main surface " |
| 734 |
"size {}x{}), decorations active: {}", |
| 735 |
size.Width(), size.Height(), scale, mainSurfaceSize.Width(), mainSurfaceSize.Height(), |
| 736 |
IsDecorationActive()); |
| 737 |
|
| 738 |
if (mainSurfaceSize != m_mainSurfaceSize || scale != m_scale || wasDecorations != IsDecorationActive()) |
| 739 |
{ |
| 740 |
if (scale != m_scale) |
| 741 |
{ |
| 742 |
// Reload cursor theme |
| 743 |
CLog::Log(LOGDEBUG, "CWindowDecorator::SetState: Buffer scale changed, reloading cursor theme"); |
| 744 |
m_cursorTheme = wayland::cursor_theme_t{}; |
| 745 |
for (auto& seat : m_seats) |
| 746 |
{ |
| 747 |
UpdateSeatCursor(seat.second); |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
m_mainSurfaceSize = mainSurfaceSize; |
| 752 |
m_scale = scale; |
| 753 |
CLog::Log(LOGDEBUG, "CWindowDecorator::SetState: Resetting decorations"); |
| 754 |
Reset(true); |
| 755 |
} |
| 756 |
else if (IsDecorationActive()) |
| 757 |
{ |
| 758 |
CLog::Log(LOGDEBUG, "CWindowDecorator::SetState: Repainting decorations"); |
| 759 |
// Only state differs, no reallocation needed |
| 760 |
Reset(false); |
| 761 |
} |
| 762 |
} |
| 763 |
|
| 764 |
void CWindowDecorator::Reset(bool reallocate) |
| 765 |
{ |
| 766 |
// The complete reset operation should be seen as one atomic update to the |
| 767 |
// internal state, otherwise buffer/surface state might be mismatched |
| 768 |
std::unique_lock<CCriticalSection> lock(m_mutex); |
| 769 |
|
| 770 |
if (reallocate) |
| 771 |
{ |
| 772 |
ResetButtons(); |
| 773 |
ResetSurfaces(); |
| 774 |
ResetShm(); |
| 775 |
if (IsDecorationActive()) |
| 776 |
{ |
| 777 |
AllocateBuffers(); |
| 778 |
ReattachSubsurfaces(); |
| 779 |
PositionButtons(); |
| 780 |
} |
| 781 |
} |
| 782 |
|
| 783 |
if (IsDecorationActive()) |
| 784 |
{ |
| 785 |
Repaint(); |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
void CWindowDecorator::ResetButtons() |
| 790 |
{ |
| 791 |
if (IsDecorationActive()) |
| 792 |
{ |
| 793 |
if (m_buttons.empty()) |
| 794 |
{ |
| 795 |
// Minimize |
| 796 |
m_buttons.emplace_back(); |
| 797 |
Button& minimize = m_buttons.back(); |
| 798 |
minimize.draw = [this](Surface& surface, CRectInt position, bool hover) |
| 799 |
{ |
| 800 |
DrawButton(surface, m_buttonColor, position, hover); |
| 801 |
DrawHorizontalLine(surface, m_buttonColor, position.P1() + CPointInt{BUTTON_INNER_SEPARATION, position.Height() - BUTTON_INNER_SEPARATION - 1}, position.Width() - 2 * BUTTON_INNER_SEPARATION); |
| 802 |
}; |
| 803 |
minimize.onClick = [this] { m_handler.OnWindowMinimize(); }; |
| 804 |
|
| 805 |
// Maximize |
| 806 |
m_buttons.emplace_back(); |
| 807 |
Button& maximize = m_buttons.back(); |
| 808 |
maximize.draw = [this](Surface& surface, CRectInt position, bool hover) |
| 809 |
{ |
| 810 |
DrawButton(surface, m_buttonColor, position, hover); |
| 811 |
DrawRectangle(surface, m_buttonColor, {position.P1() + CPointInt{BUTTON_INNER_SEPARATION, BUTTON_INNER_SEPARATION}, position.P2() - CPointInt{BUTTON_INNER_SEPARATION, BUTTON_INNER_SEPARATION}}); |
| 812 |
DrawHorizontalLine(surface, m_buttonColor, position.P1() + CPointInt{BUTTON_INNER_SEPARATION, BUTTON_INNER_SEPARATION + 1}, position.Width() - 2 * BUTTON_INNER_SEPARATION); |
| 813 |
}; |
| 814 |
maximize.onClick = [this] { m_handler.OnWindowMaximize(); }; |
| 815 |
|
| 816 |
// Close |
| 817 |
m_buttons.emplace_back(); |
| 818 |
Button& close = m_buttons.back(); |
| 819 |
close.draw = [this](Surface& surface, CRectInt position, bool hover) |
| 820 |
{ |
| 821 |
DrawButton(surface, m_buttonColor, position, hover); |
| 822 |
auto height = position.Height() - 2 * BUTTON_INNER_SEPARATION; |
| 823 |
DrawAngledLine(surface, m_buttonColor, position.P1() + CPointInt{BUTTON_INNER_SEPARATION, BUTTON_INNER_SEPARATION}, height, 1); |
| 824 |
DrawAngledLine(surface, m_buttonColor, position.P1() + CPointInt{position.Width() - BUTTON_INNER_SEPARATION - 1, BUTTON_INNER_SEPARATION}, height, -1); |
| 825 |
}; |
| 826 |
close.onClick = [this] { m_handler.OnWindowClose(); }; |
| 827 |
} |
| 828 |
} |
| 829 |
else |
| 830 |
{ |
| 831 |
m_buttons.clear(); |
| 832 |
} |
| 833 |
} |
| 834 |
|
| 835 |
void CWindowDecorator::ResetSurfaces() |
| 836 |
{ |
| 837 |
if (IsDecorationActive()) |
| 838 |
{ |
| 839 |
if (!m_borderSurfaces.front().surface.wlSurface) |
| 840 |
{ |
| 841 |
std::generate(m_borderSurfaces.begin(), m_borderSurfaces.end(), std::bind(&CWindowDecorator::MakeBorderSurface, this)); |
| 842 |
} |
| 843 |
} |
| 844 |
else |
| 845 |
{ |
| 846 |
for (auto& borderSurface : m_borderSurfaces) |
| 847 |
{ |
| 848 |
auto& wlSurface = borderSurface.surface.wlSurface; |
| 849 |
if (wlSurface) |
| 850 |
{ |
| 851 |
// Destroying the surface would cause some flicker because it takes effect |
| 852 |
// immediately, before the next commit on the main surface - just make it |
| 853 |
// invisible by attaching a NULL buffer |
| 854 |
wlSurface.attach(wayland::buffer_t{}, 0, 0); |
| 855 |
wlSurface.set_opaque_region(wayland::region_t{}); |
| 856 |
wlSurface.commit(); |
| 857 |
} |
| 858 |
} |
| 859 |
} |
| 860 |
} |
| 861 |
|
| 862 |
void CWindowDecorator::ReattachSubsurfaces() |
| 863 |
{ |
| 864 |
for (auto& surface : m_borderSurfaces) |
| 865 |
{ |
| 866 |
surface.subsurface.set_position(surface.geometry.x1, surface.geometry.y1); |
| 867 |
} |
| 868 |
} |
| 869 |
|
| 870 |
CRectInt CWindowDecorator::GetWindowGeometry() const |
| 871 |
{ |
| 872 |
CRectInt geometry{{0, 0}, m_mainSurfaceSize}; |
| 873 |
|
| 874 |
if (IsDecorationActive()) |
| 875 |
{ |
| 876 |
for (auto const& surface : m_borderSurfaces) |
| 877 |
{ |
| 878 |
geometry.Union(surface.windowRect + surface.geometry.P1()); |
| 879 |
} |
| 880 |
} |
| 881 |
|
| 882 |
return geometry; |
| 883 |
} |
| 884 |
|
| 885 |
void CWindowDecorator::ResetShm() |
| 886 |
{ |
| 887 |
if (IsDecorationActive()) |
| 888 |
{ |
| 889 |
m_memory = std::make_unique<CSharedMemory>(MemoryBytesForSize(m_mainSurfaceSize, m_scale)); |
| 890 |
m_memoryAllocatedSize = 0; |
| 891 |
m_shmPool = m_shm.create_pool(m_memory->Fd(), m_memory->Size()); |
| 892 |
} |
| 893 |
else |
| 894 |
{ |
| 895 |
m_memory.reset(); |
| 896 |
m_shmPool.proxy_release(); |
| 897 |
} |
| 898 |
|
| 899 |
for (auto& borderSurface : m_borderSurfaces) |
| 900 |
{ |
| 901 |
// Buffers are invalid now, reset |
| 902 |
borderSurface.surface.buffer = Buffer{}; |
| 903 |
} |
| 904 |
} |
| 905 |
|
| 906 |
void CWindowDecorator::PositionButtons() |
| 907 |
{ |
| 908 |
CPointInt position{m_borderSurfaces[SURFACE_TOP].surface.size.Width() - BORDER_WIDTH, BORDER_WIDTH + BUTTONS_EDGE_DISTANCE}; |
| 909 |
for (auto iter = m_buttons.rbegin(); iter != m_buttons.rend(); iter++) |
| 910 |
{ |
| 911 |
position.x -= (BUTTONS_EDGE_DISTANCE + BUTTON_SIZE); |
| 912 |
// Clamp if not enough space |
| 913 |
position.x = std::max(0, position.x); |
| 914 |
|
| 915 |
iter->position = CRectInt{position, position + CPointInt{BUTTON_SIZE, BUTTON_SIZE}}; |
| 916 |
} |
| 917 |
} |
| 918 |
|
| 919 |
CWindowDecorator::Buffer CWindowDecorator::GetBuffer(CSizeInt size) |
| 920 |
{ |
| 921 |
// We ignore tearing on the decorations for now. |
| 922 |
// We can always implement a clever buffer management scheme later... :-) |
| 923 |
|
| 924 |
auto totalSize{size.Area() * BYTES_PER_PIXEL}; |
| 925 |
if (m_memory->Size() < m_memoryAllocatedSize + totalSize) |
| 926 |
{ |
| 927 |
// We miscalculated something |
| 928 |
throw std::logic_error("Remaining SHM pool size is too small for requested buffer"); |
| 929 |
} |
| 930 |
// argb8888 support is mandatory |
| 931 |
auto buffer = m_shmPool.create_buffer(m_memoryAllocatedSize, size.Width(), size.Height(), size.Width() * BYTES_PER_PIXEL, wayland::shm_format::argb8888); |
| 932 |
|
| 933 |
void* data{static_cast<std::uint8_t*> (m_memory->Data()) + m_memoryAllocatedSize}; |
| 934 |
m_memoryAllocatedSize += totalSize; |
| 935 |
|
| 936 |
return { data, static_cast<std::size_t> (totalSize), size, std::move(buffer) }; |
| 937 |
} |
| 938 |
|
| 939 |
void CWindowDecorator::AllocateBuffers() |
| 940 |
{ |
| 941 |
for (std::size_t i{0}; i < m_borderSurfaces.size(); i++) |
| 942 |
{ |
| 943 |
auto& borderSurface = m_borderSurfaces[i]; |
| 944 |
if (!borderSurface.surface.buffer.data) |
| 945 |
{ |
| 946 |
borderSurface.geometry = SurfaceGeometry(static_cast<SurfaceIndex> (i), m_mainSurfaceSize); |
| 947 |
borderSurface.windowRect = SurfaceWindowRegion(static_cast<SurfaceIndex> (i), m_mainSurfaceSize); |
| 948 |
borderSurface.surface.buffer = GetBuffer(borderSurface.geometry.ToSize() * m_scale); |
| 949 |
borderSurface.surface.scale = m_scale; |
| 950 |
borderSurface.surface.size = borderSurface.geometry.ToSize(); |
| 951 |
auto opaqueRegionGeometry = SurfaceOpaqueRegion(static_cast<SurfaceIndex> (i), m_mainSurfaceSize); |
| 952 |
auto region = m_compositor.create_region(); |
| 953 |
region.add(opaqueRegionGeometry.x1, opaqueRegionGeometry.y1, opaqueRegionGeometry.Width(), opaqueRegionGeometry.Height()); |
| 954 |
borderSurface.surface.wlSurface.set_opaque_region(region); |
| 955 |
if (borderSurface.surface.wlSurface.can_set_buffer_scale()) |
| 956 |
{ |
| 957 |
borderSurface.surface.wlSurface.set_buffer_scale(m_scale); |
| 958 |
} |
| 959 |
} |
| 960 |
} |
| 961 |
} |
| 962 |
|
| 963 |
void CWindowDecorator::Repaint() |
| 964 |
{ |
| 965 |
// Fill transparent (outer) and color (inner) |
| 966 |
for (auto& borderSurface : m_borderSurfaces) |
| 967 |
{ |
| 968 |
std::fill_n(static_cast<std::uint32_t*> (borderSurface.surface.buffer.data), borderSurface.surface.buffer.size.Area(), Endian_SwapLE32(TRANSPARENT)); |
| 969 |
FillRectangle(borderSurface.surface, BORDER_COLOR, borderSurface.windowRect - CSizeInt{1, 1}); |
| 970 |
} |
| 971 |
auto& topSurface = m_borderSurfaces[SURFACE_TOP].surface; |
| 972 |
auto innerBorderColor = m_buttonColor; |
| 973 |
// Draw rectangle |
| 974 |
DrawHorizontalLine(topSurface, innerBorderColor, {BORDER_WIDTH - 1, BORDER_WIDTH - 1}, topSurface.size.Width() - 2 * BORDER_WIDTH + 2); |
| 975 |
DrawVerticalLine(topSurface, innerBorderColor, {BORDER_WIDTH - 1, BORDER_WIDTH - 1}, topSurface.size.Height() - BORDER_WIDTH + 1); |
| 976 |
DrawVerticalLine(topSurface, innerBorderColor, {topSurface.size.Width() - BORDER_WIDTH, BORDER_WIDTH - 1}, topSurface.size.Height() - BORDER_WIDTH + 1); |
| 977 |
DrawVerticalLine(m_borderSurfaces[SURFACE_LEFT].surface, innerBorderColor, {BORDER_WIDTH - 1, 0}, m_borderSurfaces[SURFACE_LEFT].surface.size.Height()); |
| 978 |
DrawVerticalLine(m_borderSurfaces[SURFACE_RIGHT].surface, innerBorderColor, {0, 0}, m_borderSurfaces[SURFACE_RIGHT].surface.size.Height()); |
| 979 |
DrawHorizontalLine(m_borderSurfaces[SURFACE_BOTTOM].surface, innerBorderColor, {BORDER_WIDTH - 1, 0}, m_borderSurfaces[SURFACE_BOTTOM].surface.size.Width() - 2 * BORDER_WIDTH + 2); |
| 980 |
// Draw white line into top bar as separator |
| 981 |
DrawHorizontalLine(topSurface, innerBorderColor, {BORDER_WIDTH - 1, topSurface.size.Height() - 1}, topSurface.size.Width() - 2 * BORDER_WIDTH + 2); |
| 982 |
// Draw buttons |
| 983 |
for (auto& button : m_buttons) |
| 984 |
{ |
| 985 |
button.draw(topSurface, button.position, button.hovered); |
| 986 |
} |
| 987 |
|
| 988 |
// Finally make everything visible |
| 989 |
CommitAllBuffers(); |
| 990 |
} |
| 991 |
|
| 992 |
void CWindowDecorator::CommitAllBuffers() |
| 993 |
{ |
| 994 |
std::unique_lock<CCriticalSection> lock(m_pendingBuffersMutex); |
| 995 |
|
| 996 |
for (auto& borderSurface : m_borderSurfaces) |
| 997 |
{ |
| 998 |
auto& wlSurface = borderSurface.surface.wlSurface; |
| 999 |
auto& wlBuffer = borderSurface.surface.buffer.wlBuffer; |
| 1000 |
// Keep buffers in list so they are kept alive even when the Buffer gets |
| 1001 |
// recreated on size change |
| 1002 |
auto emplaceResult = m_pendingBuffers.emplace(wlBuffer); |
| 1003 |
if (emplaceResult.second) |
| 1004 |
{ |
| 1005 |
// Buffer was not pending already |
| 1006 |
auto wlBufferC = reinterpret_cast<wl_buffer*> (wlBuffer.c_ptr()); |
| 1007 |
// We can refer to the buffer neither by iterator (might be invalidated) nor by |
| 1008 |
// capturing the C++ instance in the lambda (would create a reference loop and |
| 1009 |
// never allow the object to be freed), so use the raw pointer for now |
| 1010 |
wlBuffer.on_release() = [this, wlBufferC]() |
| 1011 |
{ |
| 1012 |
std::unique_lock<CCriticalSection> lock(m_pendingBuffersMutex); |
| 1013 |
// Construct a dummy object for searching the set |
| 1014 |
wayland::buffer_t findDummy(wlBufferC, wayland::proxy_t::wrapper_type::foreign); |
| 1015 |
auto iter = m_pendingBuffers.find(findDummy); |
| 1016 |
if (iter == m_pendingBuffers.end()) |
| 1017 |
{ |
| 1018 |
throw std::logic_error("Cannot release buffer that is not pending"); |
| 1019 |
} |
| 1020 |
|
| 1021 |
// Do not erase again until buffer is reattached (should not happen anyway, just to be safe) |
| 1022 |
// const_cast is OK since changing the function pointer does not affect |
| 1023 |
// the key in the set |
| 1024 |
const_cast<wayland::buffer_t&>(*iter).on_release() = nullptr; |
| 1025 |
m_pendingBuffers.erase(iter); |
| 1026 |
}; |
| 1027 |
} |
| 1028 |
|
| 1029 |
wlSurface.attach(wlBuffer, 0, 0); |
| 1030 |
wlSurface.damage(0, 0, borderSurface.surface.size.Width(), borderSurface.surface.size.Height()); |
| 1031 |
wlSurface.commit(); |
| 1032 |
} |
| 1033 |
} |
| 1034 |
|
| 1035 |
void CWindowDecorator::LoadCursorTheme() |
| 1036 |
{ |
| 1037 |
std::unique_lock<CCriticalSection> lock(m_mutex); |
| 1038 |
if (!m_cursorTheme) |
| 1039 |
{ |
| 1040 |
// Load default cursor theme |
| 1041 |
// Base size of 24px is what most cursor themes seem to have |
| 1042 |
m_cursorTheme = wayland::cursor_theme_t("", 24 * m_scale, m_shm); |
| 1043 |
} |
| 1044 |
} |