mirror of
https://github.com/noctalia-dev/noctalia-shell.git
synced 2026-05-11 17:08:27 +08:00
feat(shell): add initial layer-shell bar foundation
This commit is contained in:
+121
-4
@@ -1,5 +1,5 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
project(Noctalia VERSION 0.1.0 LANGUAGES CXX)
|
||||
project(Noctalia VERSION 0.1.0 LANGUAGES C CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
@@ -9,8 +9,106 @@ set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
|
||||
# --- Dependencies ---
|
||||
find_package(PkgConfig REQUIRED)
|
||||
find_package(sdbus-c++ REQUIRED)
|
||||
set(SDBUS_TARGET SDBusCpp::sdbus-c++)
|
||||
pkg_check_modules(WAYLAND_CLIENT REQUIRED wayland-client)
|
||||
pkg_check_modules(WAYLAND_PROTOCOLS REQUIRED wayland-protocols)
|
||||
pkg_get_variable(WAYLAND_PROTOCOLS_PKGDATADIR wayland-protocols pkgdatadir)
|
||||
file(REAL_PATH "${WAYLAND_PROTOCOLS_PKGDATADIR}" WAYLAND_PROTOCOLS_PKGDATADIR)
|
||||
|
||||
find_program(WAYLAND_SCANNER_EXECUTABLE wayland-scanner REQUIRED)
|
||||
|
||||
set(GENERATED_PROTOCOL_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated")
|
||||
file(MAKE_DIRECTORY "${GENERATED_PROTOCOL_DIR}")
|
||||
set(PROTOCOL_PREPARE_SCRIPT
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/PrepareWaylandProtocol.cmake")
|
||||
|
||||
set(WLR_LAYER_SHELL_SOURCE_XML
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/protocols/wlr-layer-shell-unstable-v1.xml")
|
||||
set(WLR_LAYER_SHELL_XML
|
||||
"${GENERATED_PROTOCOL_DIR}/wlr-layer-shell-unstable-v1.scanner.xml")
|
||||
set(XDG_OUTPUT_XML
|
||||
"${WAYLAND_PROTOCOLS_PKGDATADIR}/unstable/xdg-output/xdg-output-unstable-v1.xml")
|
||||
set(XDG_SHELL_XML
|
||||
"${WAYLAND_PROTOCOLS_PKGDATADIR}/stable/xdg-shell/xdg-shell.xml")
|
||||
|
||||
set(XDG_OUTPUT_PROTOCOL_C
|
||||
"${GENERATED_PROTOCOL_DIR}/xdg-output-unstable-v1-client-protocol.c")
|
||||
set(XDG_OUTPUT_PROTOCOL_H
|
||||
"${GENERATED_PROTOCOL_DIR}/xdg-output-unstable-v1-client-protocol.h")
|
||||
set(XDG_SHELL_PROTOCOL_C
|
||||
"${GENERATED_PROTOCOL_DIR}/xdg-shell-client-protocol.c")
|
||||
set(XDG_SHELL_PROTOCOL_H
|
||||
"${GENERATED_PROTOCOL_DIR}/xdg-shell-client-protocol.h")
|
||||
|
||||
set(WLR_LAYER_SHELL_PROTOCOL_C
|
||||
"${GENERATED_PROTOCOL_DIR}/wlr-layer-shell-unstable-v1-client-protocol.c")
|
||||
set(WLR_LAYER_SHELL_PROTOCOL_H
|
||||
"${GENERATED_PROTOCOL_DIR}/wlr-layer-shell-unstable-v1-client-protocol.h")
|
||||
|
||||
add_custom_command(
|
||||
OUTPUT "${WLR_LAYER_SHELL_XML}"
|
||||
COMMAND "${CMAKE_COMMAND}"
|
||||
-DINPUT_XML=${WLR_LAYER_SHELL_SOURCE_XML}
|
||||
-DOUTPUT_XML=${WLR_LAYER_SHELL_XML}
|
||||
-P "${PROTOCOL_PREPARE_SCRIPT}"
|
||||
DEPENDS
|
||||
"${WLR_LAYER_SHELL_SOURCE_XML}"
|
||||
"${PROTOCOL_PREPARE_SCRIPT}"
|
||||
VERBATIM
|
||||
)
|
||||
|
||||
add_custom_command(
|
||||
OUTPUT "${WLR_LAYER_SHELL_PROTOCOL_C}"
|
||||
COMMAND "${WAYLAND_SCANNER_EXECUTABLE}" private-code "${WLR_LAYER_SHELL_XML}" "${WLR_LAYER_SHELL_PROTOCOL_C}"
|
||||
DEPENDS "${WLR_LAYER_SHELL_XML}"
|
||||
VERBATIM
|
||||
)
|
||||
|
||||
add_custom_command(
|
||||
OUTPUT "${WLR_LAYER_SHELL_PROTOCOL_H}"
|
||||
COMMAND "${WAYLAND_SCANNER_EXECUTABLE}" client-header "${WLR_LAYER_SHELL_XML}" "${WLR_LAYER_SHELL_PROTOCOL_H}"
|
||||
DEPENDS "${WLR_LAYER_SHELL_XML}"
|
||||
VERBATIM
|
||||
)
|
||||
|
||||
add_custom_command(
|
||||
OUTPUT "${XDG_OUTPUT_PROTOCOL_C}"
|
||||
COMMAND "${WAYLAND_SCANNER_EXECUTABLE}" private-code "${XDG_OUTPUT_XML}" "${XDG_OUTPUT_PROTOCOL_C}"
|
||||
DEPENDS "${XDG_OUTPUT_XML}"
|
||||
VERBATIM
|
||||
)
|
||||
|
||||
add_custom_command(
|
||||
OUTPUT "${XDG_OUTPUT_PROTOCOL_H}"
|
||||
COMMAND "${WAYLAND_SCANNER_EXECUTABLE}" client-header "${XDG_OUTPUT_XML}" "${XDG_OUTPUT_PROTOCOL_H}"
|
||||
DEPENDS "${XDG_OUTPUT_XML}"
|
||||
VERBATIM
|
||||
)
|
||||
|
||||
add_custom_command(
|
||||
OUTPUT "${XDG_SHELL_PROTOCOL_C}"
|
||||
COMMAND "${WAYLAND_SCANNER_EXECUTABLE}" private-code "${XDG_SHELL_XML}" "${XDG_SHELL_PROTOCOL_C}"
|
||||
DEPENDS "${XDG_SHELL_XML}"
|
||||
VERBATIM
|
||||
)
|
||||
|
||||
add_custom_command(
|
||||
OUTPUT "${XDG_SHELL_PROTOCOL_H}"
|
||||
COMMAND "${WAYLAND_SCANNER_EXECUTABLE}" client-header "${XDG_SHELL_XML}" "${XDG_SHELL_PROTOCOL_H}"
|
||||
DEPENDS "${XDG_SHELL_XML}"
|
||||
VERBATIM
|
||||
)
|
||||
|
||||
add_custom_target(noctalia_wayland_protocols
|
||||
DEPENDS
|
||||
"${WLR_LAYER_SHELL_PROTOCOL_C}"
|
||||
"${WLR_LAYER_SHELL_PROTOCOL_H}"
|
||||
"${XDG_OUTPUT_PROTOCOL_C}"
|
||||
"${XDG_OUTPUT_PROTOCOL_H}"
|
||||
"${XDG_SHELL_PROTOCOL_C}"
|
||||
"${XDG_SHELL_PROTOCOL_H}"
|
||||
)
|
||||
|
||||
# --- Target ---
|
||||
add_executable(noctalia
|
||||
@@ -18,10 +116,29 @@ add_executable(noctalia
|
||||
src/app/Application.cpp
|
||||
src/notification/NotificationManager.cpp
|
||||
src/dbus/notification/NotificationService.cpp
|
||||
src/shell/BarShell.cpp
|
||||
src/wayland/LayerSurface.cpp
|
||||
src/wayland/WaylandConnection.cpp
|
||||
"${WLR_LAYER_SHELL_PROTOCOL_C}"
|
||||
"${XDG_OUTPUT_PROTOCOL_C}"
|
||||
"${XDG_SHELL_PROTOCOL_C}"
|
||||
)
|
||||
target_compile_definitions(noctalia PRIVATE NOCTALIA_HAVE_WLR_LAYER_SHELL=1)
|
||||
|
||||
target_include_directories(noctalia PRIVATE src)
|
||||
target_link_libraries(noctalia PRIVATE ${SDBUS_TARGET})
|
||||
add_dependencies(noctalia noctalia_wayland_protocols)
|
||||
|
||||
target_include_directories(noctalia PRIVATE
|
||||
src
|
||||
"${GENERATED_PROTOCOL_DIR}"
|
||||
${WAYLAND_CLIENT_INCLUDE_DIRS}
|
||||
)
|
||||
target_link_libraries(noctalia PRIVATE
|
||||
SDBusCpp::sdbus-c++
|
||||
${WAYLAND_CLIENT_LIBRARIES}
|
||||
)
|
||||
target_compile_options(noctalia PRIVATE
|
||||
${WAYLAND_CLIENT_CFLAGS_OTHER}
|
||||
)
|
||||
|
||||
target_compile_options(noctalia PRIVATE
|
||||
-Wall
|
||||
|
||||
@@ -0,0 +1,190 @@
|
||||
# Skia Shell Implementation Plan
|
||||
|
||||
## Goal
|
||||
Build a Wayland shell and bar runtime that keeps Qt's memory footprint out of the process while preserving the features that matter for Noctalia:
|
||||
|
||||
- multi-monitor layer-shell surfaces
|
||||
- smooth animations
|
||||
- shader-capable rendering
|
||||
- room for notifications, OSDs, dashboards, and lock surfaces
|
||||
|
||||
The rendering target is `Skia + OpenGL` for the first pass. Wayland integration stays direct through `libwayland-client` and generated protocol bindings.
|
||||
|
||||
## Principles
|
||||
- Get a real bar surface on screen early.
|
||||
- Keep the scene graph tiny and domain-specific.
|
||||
- Prefer surface-level invalidation first; defer region-level optimization.
|
||||
- Defer blur and advanced effects until frame scheduling is stable.
|
||||
- Keep service integrations isolated behind small adapters.
|
||||
|
||||
## Initial Stack
|
||||
- `libwayland-client`
|
||||
- `wayland-protocols`
|
||||
- `wayland-scanner`
|
||||
- `zwlr-layer-shell-v1`
|
||||
- `zxdg-output-manager-v1`
|
||||
- `Skia`
|
||||
- `fmt`
|
||||
- `spdlog`
|
||||
- `sdbus-c++`
|
||||
|
||||
## Repo Layout
|
||||
```text
|
||||
src/
|
||||
app/
|
||||
dbus/
|
||||
notification/
|
||||
shell/
|
||||
wayland/
|
||||
render/
|
||||
scene/
|
||||
ui/
|
||||
services/
|
||||
```
|
||||
|
||||
## Milestones
|
||||
|
||||
### Phase 0: Foundation
|
||||
- Add build plumbing for Wayland and protocol code generation.
|
||||
- Introduce shell-specific modules without disrupting the notification path.
|
||||
- Keep `Application` as the single bootstrap point.
|
||||
|
||||
Exit criteria:
|
||||
- Project configures with Wayland dependencies.
|
||||
- Shell modules compile even if they only log and discover globals.
|
||||
|
||||
### Phase 1: Bare Layer-Shell Surface
|
||||
- Bind `wl_compositor`, `wl_seat`, `wl_output`, `zwlr_layer_shell_v1`, and `zxdg_output_manager_v1`.
|
||||
- Create one top-anchored layer-shell bar surface.
|
||||
- Handle `configure`, `ack_configure`, size, and frame callbacks.
|
||||
- Target one monitor first.
|
||||
|
||||
Exit criteria:
|
||||
- A blank bar surface appears reliably on one output.
|
||||
|
||||
### Phase 2: Skia Rendering
|
||||
- Create a GPU-backed Skia render target per surface.
|
||||
- Render background, rounded rects, and text.
|
||||
- Keep redraw logic tied to Wayland frame callbacks.
|
||||
|
||||
Exit criteria:
|
||||
- A Skia-rendered bar background and text appear on screen.
|
||||
|
||||
### Phase 3: Minimal Scene Graph
|
||||
Start with:
|
||||
- `Node`
|
||||
- `ContainerNode`
|
||||
- `RectNode`
|
||||
- `TextNode`
|
||||
|
||||
Each node should support:
|
||||
- bounds
|
||||
- visibility
|
||||
- opacity
|
||||
- transform
|
||||
- dirty state
|
||||
- paint traversal
|
||||
|
||||
Exit criteria:
|
||||
- The bar is rendered from a retained tree rather than raw draw calls.
|
||||
|
||||
### Phase 4: Invalidation and Animation
|
||||
- Add per-surface invalidation.
|
||||
- Add property animations for opacity, position, and size.
|
||||
- Only redraw while dirty or while animations are active.
|
||||
|
||||
Exit criteria:
|
||||
- Idle surfaces stay idle; animated surfaces redraw smoothly.
|
||||
|
||||
### Phase 5: Multi-Monitor
|
||||
- Create one `BarSurface` per output.
|
||||
- Handle output add/remove/change.
|
||||
- Use `xdg-output` metadata for stable naming.
|
||||
|
||||
Exit criteria:
|
||||
- Multiple monitors each get an independently managed bar.
|
||||
|
||||
### Phase 6: Services and Components
|
||||
Initial components:
|
||||
- workspaces
|
||||
- clock
|
||||
- notification indicator
|
||||
- media status
|
||||
- audio status
|
||||
|
||||
Initial services:
|
||||
- notifications
|
||||
- MPRIS
|
||||
- battery / power
|
||||
- audio
|
||||
|
||||
Exit criteria:
|
||||
- The bar is data-driven by internal models, not protocol objects.
|
||||
|
||||
## Initial Class Layout
|
||||
|
||||
### `src/app/`
|
||||
- `Application`
|
||||
- Owns shell bootstrap, notification service, and lifecycle coordination.
|
||||
|
||||
### `src/shell/`
|
||||
- `BarShell`
|
||||
- High-level shell runtime.
|
||||
- Coordinates Wayland connection and later the bar surfaces.
|
||||
|
||||
### `src/wayland/`
|
||||
- `WaylandConnection`
|
||||
- Connects to the display.
|
||||
- Binds registry globals.
|
||||
- Tracks outputs and core shell interfaces.
|
||||
- `Output`
|
||||
- Output identity and geometry metadata.
|
||||
- `LayerSurface`
|
||||
- One layer-shell surface and its configure/frame lifecycle.
|
||||
|
||||
### `src/render/`
|
||||
- `Renderer`
|
||||
- Abstract renderer entry point.
|
||||
- `SkiaRenderer`
|
||||
- Skia/OpenGL implementation.
|
||||
|
||||
### `src/scene/`
|
||||
- `Node`
|
||||
- `ContainerNode`
|
||||
- `RectNode`
|
||||
- `TextNode`
|
||||
- `AnimationController`
|
||||
|
||||
### `src/ui/`
|
||||
- `BarView`
|
||||
- Builds the scene subtree for one bar.
|
||||
- `WorkspaceStrip`
|
||||
- `ClockView`
|
||||
|
||||
### `src/services/`
|
||||
- `NotificationModel`
|
||||
- `MprisModel`
|
||||
- `AudioModel`
|
||||
|
||||
## First Four Execution Steps
|
||||
1. Land Wayland build plumbing and protocol generation.
|
||||
2. Build a `WaylandConnection` that discovers required globals and outputs.
|
||||
3. Split a `BarShell` runtime out of `Application`.
|
||||
4. Add a `LayerSurface` that can open a blank bar on one output.
|
||||
|
||||
## Phase 1 Task Checklist
|
||||
- [ ] Add `FindPkgConfig`-based Wayland dependency wiring to CMake.
|
||||
- [ ] Generate client protocol headers/code for:
|
||||
- [ ] `wlr-layer-shell-unstable-v1`
|
||||
- [ ] `xdg-output-unstable-v1`
|
||||
- [ ] Add `src/shell/BarShell.*`
|
||||
- [ ] Add `src/wayland/WaylandConnection.*`
|
||||
- [ ] Bind required globals from the registry.
|
||||
- [ ] Track available outputs.
|
||||
- [ ] Print a startup summary of discovered globals and outputs.
|
||||
- [ ] Prepare the app bootstrap so shell startup does not block future DBus work.
|
||||
|
||||
## Immediate Follow-Up After Phase 1
|
||||
- Add `LayerSurface`.
|
||||
- Get one blank bar surface on screen.
|
||||
- Add configure and frame handling before introducing Skia.
|
||||
@@ -0,0 +1,7 @@
|
||||
if (NOT DEFINED INPUT_XML OR NOT DEFINED OUTPUT_XML)
|
||||
message(FATAL_ERROR "INPUT_XML and OUTPUT_XML must be set")
|
||||
endif()
|
||||
|
||||
file(READ "${INPUT_XML}" PROTOCOL_XML)
|
||||
string(REPLACE "name=\"namespace\"" "name=\"name_space\"" PROTOCOL_XML "${PROTOCOL_XML}")
|
||||
file(WRITE "${OUTPUT_XML}" "${PROTOCOL_XML}")
|
||||
@@ -0,0 +1,407 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<protocol name="wlr_layer_shell_unstable_v1">
|
||||
<copyright>
|
||||
Copyright © 2017 Drew DeVault
|
||||
|
||||
Permission to use, copy, modify, distribute, and sell this
|
||||
software and its documentation for any purpose is hereby granted
|
||||
without fee, provided that the above copyright notice appear in
|
||||
all copies and that both that copyright notice and this permission
|
||||
notice appear in supporting documentation, and that the name of
|
||||
the copyright holders not be used in advertising or publicity
|
||||
pertaining to distribution of the software without specific,
|
||||
written prior permission. The copyright holders make no
|
||||
representations about the suitability of this software for any
|
||||
purpose. It is provided "as is" without express or implied
|
||||
warranty.
|
||||
|
||||
THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
|
||||
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
|
||||
AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
||||
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
|
||||
THIS SOFTWARE.
|
||||
</copyright>
|
||||
|
||||
<interface name="zwlr_layer_shell_v1" version="5">
|
||||
<description summary="create surfaces that are layers of the desktop">
|
||||
Clients can use this interface to assign the surface_layer role to
|
||||
wl_surfaces. Such surfaces are assigned to a "layer" of the output and
|
||||
rendered with a defined z-depth respective to each other. They may also be
|
||||
anchored to the edges and corners of a screen and specify input handling
|
||||
semantics. This interface should be suitable for the implementation of
|
||||
many desktop shell components, and a broad number of other applications
|
||||
that interact with the desktop.
|
||||
</description>
|
||||
|
||||
<request name="get_layer_surface">
|
||||
<description summary="create a layer_surface from a surface">
|
||||
Create a layer surface for an existing surface. This assigns the role of
|
||||
layer_surface, or raises a protocol error if another role is already
|
||||
assigned.
|
||||
|
||||
Creating a layer surface from a wl_surface which has a buffer attached
|
||||
or committed is a client error, and any attempts by a client to attach
|
||||
or manipulate a buffer prior to the first layer_surface.configure call
|
||||
must also be treated as errors.
|
||||
|
||||
After creating a layer_surface object and setting it up, the client
|
||||
must perform an initial commit without any buffer attached.
|
||||
The compositor will reply with a layer_surface.configure event.
|
||||
The client must acknowledge it and is then allowed to attach a buffer
|
||||
to map the surface.
|
||||
|
||||
You may pass NULL for output to allow the compositor to decide which
|
||||
output to use. Generally this will be the one that the user most
|
||||
recently interacted with.
|
||||
|
||||
Clients can specify a namespace that defines the purpose of the layer
|
||||
surface.
|
||||
</description>
|
||||
<arg name="id" type="new_id" interface="zwlr_layer_surface_v1"/>
|
||||
<arg name="surface" type="object" interface="wl_surface"/>
|
||||
<arg name="output" type="object" interface="wl_output" allow-null="true"/>
|
||||
<arg name="layer" type="uint" enum="layer" summary="layer to add this surface to"/>
|
||||
<arg name="namespace" type="string" summary="namespace for the layer surface"/>
|
||||
</request>
|
||||
|
||||
<enum name="error">
|
||||
<entry name="role" value="0" summary="wl_surface has another role"/>
|
||||
<entry name="invalid_layer" value="1" summary="layer value is invalid"/>
|
||||
<entry name="already_constructed" value="2" summary="wl_surface has a buffer attached or committed"/>
|
||||
</enum>
|
||||
|
||||
<enum name="layer">
|
||||
<description summary="available layers for surfaces">
|
||||
These values indicate which layers a surface can be rendered in. They
|
||||
are ordered by z depth, bottom-most first. Traditional shell surfaces
|
||||
will typically be rendered between the bottom and top layers.
|
||||
Fullscreen shell surfaces are typically rendered at the top layer.
|
||||
Multiple surfaces can share a single layer, and ordering within a
|
||||
single layer is undefined.
|
||||
</description>
|
||||
|
||||
<entry name="background" value="0"/>
|
||||
<entry name="bottom" value="1"/>
|
||||
<entry name="top" value="2"/>
|
||||
<entry name="overlay" value="3"/>
|
||||
</enum>
|
||||
|
||||
<!-- Version 3 additions -->
|
||||
|
||||
<request name="destroy" type="destructor" since="3">
|
||||
<description summary="destroy the layer_shell object">
|
||||
This request indicates that the client will not use the layer_shell
|
||||
object any more. Objects that have been created through this instance
|
||||
are not affected.
|
||||
</description>
|
||||
</request>
|
||||
</interface>
|
||||
|
||||
<interface name="zwlr_layer_surface_v1" version="5">
|
||||
<description summary="layer metadata interface">
|
||||
An interface that may be implemented by a wl_surface, for surfaces that
|
||||
are designed to be rendered as a layer of a stacked desktop-like
|
||||
environment.
|
||||
|
||||
Layer surface state (layer, size, anchor, exclusive zone,
|
||||
margin, interactivity) is double-buffered, and will be applied at the
|
||||
time wl_surface.commit of the corresponding wl_surface is called.
|
||||
|
||||
Attaching a null buffer to a layer surface unmaps it.
|
||||
|
||||
Unmapping a layer_surface means that the surface cannot be shown by the
|
||||
compositor until it is explicitly mapped again. The layer_surface
|
||||
returns to the state it had right after layer_shell.get_layer_surface.
|
||||
The client can re-map the surface by performing a commit without any
|
||||
buffer attached, waiting for a configure event and handling it as usual.
|
||||
</description>
|
||||
|
||||
<request name="set_size">
|
||||
<description summary="sets the size of the surface">
|
||||
Sets the size of the surface in surface-local coordinates. The
|
||||
compositor will display the surface centered with respect to its
|
||||
anchors.
|
||||
|
||||
If you pass 0 for either value, the compositor will assign it and
|
||||
inform you of the assignment in the configure event. You must set your
|
||||
anchor to opposite edges in the dimensions you omit; not doing so is a
|
||||
protocol error. Both values are 0 by default.
|
||||
|
||||
Size is double-buffered, see wl_surface.commit.
|
||||
</description>
|
||||
<arg name="width" type="uint"/>
|
||||
<arg name="height" type="uint"/>
|
||||
</request>
|
||||
|
||||
<request name="set_anchor">
|
||||
<description summary="configures the anchor point of the surface">
|
||||
Requests that the compositor anchor the surface to the specified edges
|
||||
and corners. If two orthogonal edges are specified (e.g. 'top' and
|
||||
'left'), then the anchor point will be the intersection of the edges
|
||||
(e.g. the top left corner of the output); otherwise the anchor point
|
||||
will be centered on that edge, or in the center if none is specified.
|
||||
|
||||
Anchor is double-buffered, see wl_surface.commit.
|
||||
</description>
|
||||
<arg name="anchor" type="uint" enum="anchor"/>
|
||||
</request>
|
||||
|
||||
<request name="set_exclusive_zone">
|
||||
<description summary="configures the exclusive geometry of this surface">
|
||||
Requests that the compositor avoids occluding an area with other
|
||||
surfaces. The compositor's use of this information is
|
||||
implementation-dependent - do not assume that this region will not
|
||||
actually be occluded.
|
||||
|
||||
A positive value is only meaningful if the surface is anchored to one
|
||||
edge or an edge and both perpendicular edges. If the surface is not
|
||||
anchored, anchored to only two perpendicular edges (a corner), anchored
|
||||
to only two parallel edges or anchored to all edges, a positive value
|
||||
will be treated the same as zero.
|
||||
|
||||
A positive zone is the distance from the edge in surface-local
|
||||
coordinates to consider exclusive.
|
||||
|
||||
Surfaces that do not wish to have an exclusive zone may instead specify
|
||||
how they should interact with surfaces that do. If set to zero, the
|
||||
surface indicates that it would like to be moved to avoid occluding
|
||||
surfaces with a positive exclusive zone. If set to -1, the surface
|
||||
indicates that it would not like to be moved to accommodate for other
|
||||
surfaces, and the compositor should extend it all the way to the edges
|
||||
it is anchored to.
|
||||
|
||||
For example, a panel might set its exclusive zone to 10, so that
|
||||
maximized shell surfaces are not shown on top of it. A notification
|
||||
might set its exclusive zone to 0, so that it is moved to avoid
|
||||
occluding the panel, but shell surfaces are shown underneath it. A
|
||||
wallpaper or lock screen might set their exclusive zone to -1, so that
|
||||
they stretch below or over the panel.
|
||||
|
||||
The default value is 0.
|
||||
|
||||
Exclusive zone is double-buffered, see wl_surface.commit.
|
||||
</description>
|
||||
<arg name="zone" type="int"/>
|
||||
</request>
|
||||
|
||||
<request name="set_margin">
|
||||
<description summary="sets a margin from the anchor point">
|
||||
Requests that the surface be placed some distance away from the anchor
|
||||
point on the output, in surface-local coordinates. Setting this value
|
||||
for edges you are not anchored to has no effect.
|
||||
|
||||
The exclusive zone includes the margin.
|
||||
|
||||
Margin is double-buffered, see wl_surface.commit.
|
||||
</description>
|
||||
<arg name="top" type="int"/>
|
||||
<arg name="right" type="int"/>
|
||||
<arg name="bottom" type="int"/>
|
||||
<arg name="left" type="int"/>
|
||||
</request>
|
||||
|
||||
<enum name="keyboard_interactivity">
|
||||
<description summary="types of keyboard interaction possible for a layer shell surface">
|
||||
Types of keyboard interaction possible for layer shell surfaces. The
|
||||
rationale for this is twofold: (1) some applications are not interested
|
||||
in keyboard events and not allowing them to be focused can improve the
|
||||
desktop experience; (2) some applications will want to take exclusive
|
||||
keyboard focus.
|
||||
</description>
|
||||
|
||||
<entry name="none" value="0">
|
||||
<description summary="no keyboard focus is possible">
|
||||
This value indicates that this surface is not interested in keyboard
|
||||
events and the compositor should never assign it the keyboard focus.
|
||||
|
||||
This is the default value, set for newly created layer shell surfaces.
|
||||
|
||||
This is useful for e.g. desktop widgets that display information or
|
||||
only have interaction with non-keyboard input devices.
|
||||
</description>
|
||||
</entry>
|
||||
<entry name="exclusive" value="1">
|
||||
<description summary="request exclusive keyboard focus">
|
||||
Request exclusive keyboard focus if this surface is above the shell surface layer.
|
||||
|
||||
For the top and overlay layers, the seat will always give
|
||||
exclusive keyboard focus to the top-most layer which has keyboard
|
||||
interactivity set to exclusive. If this layer contains multiple
|
||||
surfaces with keyboard interactivity set to exclusive, the compositor
|
||||
determines the one receiving keyboard events in an implementation-
|
||||
defined manner. In this case, no guarantee is made when this surface
|
||||
will receive keyboard focus (if ever).
|
||||
|
||||
For the bottom and background layers, the compositor is allowed to use
|
||||
normal focus semantics.
|
||||
|
||||
This setting is mainly intended for applications that need to ensure
|
||||
they receive all keyboard events, such as a lock screen or a password
|
||||
prompt.
|
||||
</description>
|
||||
</entry>
|
||||
<entry name="on_demand" value="2" since="4">
|
||||
<description summary="request regular keyboard focus semantics">
|
||||
This requests the compositor to allow this surface to be focused and
|
||||
unfocused by the user in an implementation-defined manner. The user
|
||||
should be able to unfocus this surface even regardless of the layer
|
||||
it is on.
|
||||
|
||||
Typically, the compositor will want to use its normal mechanism to
|
||||
manage keyboard focus between layer shell surfaces with this setting
|
||||
and regular toplevels on the desktop layer (e.g. click to focus).
|
||||
Nevertheless, it is possible for a compositor to require a special
|
||||
interaction to focus or unfocus layer shell surfaces (e.g. requiring
|
||||
a click even if focus follows the mouse normally, or providing a
|
||||
keybinding to switch focus between layers).
|
||||
|
||||
This setting is mainly intended for desktop shell components (e.g.
|
||||
panels) that allow keyboard interaction. Using this option can allow
|
||||
implementing a desktop shell that can be fully usable without the
|
||||
mouse.
|
||||
</description>
|
||||
</entry>
|
||||
</enum>
|
||||
|
||||
<request name="set_keyboard_interactivity">
|
||||
<description summary="requests keyboard events">
|
||||
Set how keyboard events are delivered to this surface. By default,
|
||||
layer shell surfaces do not receive keyboard events; this request can
|
||||
be used to change this.
|
||||
|
||||
This setting is inherited by child surfaces set by the get_popup
|
||||
request.
|
||||
|
||||
Layer surfaces receive pointer, touch, and tablet events normally. If
|
||||
you do not want to receive them, set the input region on your surface
|
||||
to an empty region.
|
||||
|
||||
Keyboard interactivity is double-buffered, see wl_surface.commit.
|
||||
</description>
|
||||
<arg name="keyboard_interactivity" type="uint" enum="keyboard_interactivity"/>
|
||||
</request>
|
||||
|
||||
<request name="get_popup">
|
||||
<description summary="assign this layer_surface as an xdg_popup parent">
|
||||
This assigns an xdg_popup's parent to this layer_surface. This popup
|
||||
should have been created via xdg_surface::get_popup with the parent set
|
||||
to NULL, and this request must be invoked before committing the popup's
|
||||
initial state.
|
||||
|
||||
See the documentation of xdg_popup for more details about what an
|
||||
xdg_popup is and how it is used.
|
||||
</description>
|
||||
<arg name="popup" type="object" interface="xdg_popup"/>
|
||||
</request>
|
||||
|
||||
<request name="ack_configure">
|
||||
<description summary="ack a configure event">
|
||||
When a configure event is received, if a client commits the
|
||||
surface in response to the configure event, then the client
|
||||
must make an ack_configure request sometime before the commit
|
||||
request, passing along the serial of the configure event.
|
||||
|
||||
If the client receives multiple configure events before it
|
||||
can respond to one, it only has to ack the last configure event.
|
||||
|
||||
A client is not required to commit immediately after sending
|
||||
an ack_configure request - it may even ack_configure several times
|
||||
before its next surface commit.
|
||||
|
||||
A client may send multiple ack_configure requests before committing, but
|
||||
only the last request sent before a commit indicates which configure
|
||||
event the client really is responding to.
|
||||
</description>
|
||||
<arg name="serial" type="uint" summary="the serial from the configure event"/>
|
||||
</request>
|
||||
|
||||
<request name="destroy" type="destructor">
|
||||
<description summary="destroy the layer_surface">
|
||||
This request destroys the layer surface.
|
||||
</description>
|
||||
</request>
|
||||
|
||||
<event name="configure">
|
||||
<description summary="suggest a surface change">
|
||||
The configure event asks the client to resize its surface.
|
||||
|
||||
Clients should arrange their surface for the new states, and then send
|
||||
an ack_configure request with the serial sent in this configure event at
|
||||
some point before committing the new surface.
|
||||
|
||||
The client is free to dismiss all but the last configure event it
|
||||
received.
|
||||
|
||||
The width and height arguments specify the size of the window in
|
||||
surface-local coordinates.
|
||||
|
||||
The size is a hint, in the sense that the client is free to ignore it if
|
||||
it doesn't resize, pick a smaller size (to satisfy aspect ratio or
|
||||
resize in steps of NxM pixels). If the client picks a smaller size and
|
||||
is anchored to two opposite anchors (e.g. 'top' and 'bottom'), the
|
||||
surface will be centered on this axis.
|
||||
|
||||
If the width or height arguments are zero, it means the client should
|
||||
decide its own window dimension.
|
||||
</description>
|
||||
<arg name="serial" type="uint"/>
|
||||
<arg name="width" type="uint"/>
|
||||
<arg name="height" type="uint"/>
|
||||
</event>
|
||||
|
||||
<event name="closed">
|
||||
<description summary="surface should be closed">
|
||||
The closed event is sent by the compositor when the surface will no
|
||||
longer be shown. The output may have been destroyed or the user may
|
||||
have asked for it to be removed. Further changes to the surface will be
|
||||
ignored. The client should destroy the resource after receiving this
|
||||
event, and create a new surface if they so choose.
|
||||
</description>
|
||||
</event>
|
||||
|
||||
<enum name="error">
|
||||
<entry name="invalid_surface_state" value="0" summary="provided surface state is invalid"/>
|
||||
<entry name="invalid_size" value="1" summary="size is invalid"/>
|
||||
<entry name="invalid_anchor" value="2" summary="anchor bitfield is invalid"/>
|
||||
<entry name="invalid_keyboard_interactivity" value="3" summary="keyboard interactivity is invalid"/>
|
||||
<entry name="invalid_exclusive_edge" value="4" summary="exclusive edge is invalid given the surface anchors"/>
|
||||
</enum>
|
||||
|
||||
<enum name="anchor" bitfield="true">
|
||||
<entry name="top" value="1" summary="the top edge of the anchor rectangle"/>
|
||||
<entry name="bottom" value="2" summary="the bottom edge of the anchor rectangle"/>
|
||||
<entry name="left" value="4" summary="the left edge of the anchor rectangle"/>
|
||||
<entry name="right" value="8" summary="the right edge of the anchor rectangle"/>
|
||||
</enum>
|
||||
|
||||
<!-- Version 2 additions -->
|
||||
|
||||
<request name="set_layer" since="2">
|
||||
<description summary="change the layer of the surface">
|
||||
Change the layer that the surface is rendered on.
|
||||
|
||||
Layer is double-buffered, see wl_surface.commit.
|
||||
</description>
|
||||
<arg name="layer" type="uint" enum="zwlr_layer_shell_v1.layer" summary="layer to move this surface to"/>
|
||||
</request>
|
||||
|
||||
<!-- Version 5 additions -->
|
||||
|
||||
<request name="set_exclusive_edge" since="5">
|
||||
<description summary="set the edge the exclusive zone will be applied to">
|
||||
Requests an edge for the exclusive zone to apply. The exclusive
|
||||
edge will be automatically deduced from anchor points when possible,
|
||||
but when the surface is anchored to a corner, it will be necessary
|
||||
to set it explicitly to disambiguate, as it is not possible to deduce
|
||||
which one of the two corner edges should be used.
|
||||
|
||||
The edge must be one the surface is anchored to, otherwise the
|
||||
invalid_exclusive_edge protocol error will be raised.
|
||||
</description>
|
||||
<arg name="edge" type="uint" enum="anchor"/>
|
||||
</request>
|
||||
</interface>
|
||||
</protocol>
|
||||
+12
-5
@@ -1,10 +1,9 @@
|
||||
#include "Application.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
|
||||
Application::Application()
|
||||
: m_service(m_manager)
|
||||
{
|
||||
Application::Application() {
|
||||
m_manager.setEventCallback([](const Notification& n, NotificationEvent event) {
|
||||
const char* kind = (event == NotificationEvent::Added) ? "added" : "updated";
|
||||
std::cout << "[noctalia] event " << kind << " id=" << n.id << '\n';
|
||||
@@ -12,6 +11,14 @@ Application::Application()
|
||||
}
|
||||
|
||||
void Application::run() {
|
||||
std::cout << "noctalia: listening on org.freedesktop.Notifications\n";
|
||||
m_service.run();
|
||||
m_shell.initialize();
|
||||
|
||||
try {
|
||||
m_service = std::make_unique<NotificationService>(m_manager);
|
||||
std::cout << "noctalia: listening on org.freedesktop.Notifications\n";
|
||||
m_service->run();
|
||||
} catch (const std::exception& e) {
|
||||
std::cout << "noctalia: notifications disabled: " << e.what() << '\n';
|
||||
m_shell.run();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
|
||||
#include "dbus/notification/NotificationService.hpp"
|
||||
#include "notification/NotificationManager.hpp"
|
||||
#include "shell/BarShell.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
class Application {
|
||||
public:
|
||||
@@ -10,6 +13,7 @@ public:
|
||||
void run();
|
||||
|
||||
private:
|
||||
BarShell m_shell;
|
||||
NotificationManager m_manager;
|
||||
NotificationService m_service;
|
||||
std::unique_ptr<NotificationService> m_service;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
#include "shell/BarShell.hpp"
|
||||
|
||||
BarShell::BarShell()
|
||||
: m_layerSurface(m_connection) {}
|
||||
|
||||
bool BarShell::initialize() {
|
||||
if (!m_connection.connect()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
m_layerSurface.initialize();
|
||||
return true;
|
||||
}
|
||||
|
||||
void BarShell::run() {
|
||||
while (m_layerSurface.isRunning()) {
|
||||
m_layerSurface.dispatch();
|
||||
}
|
||||
}
|
||||
|
||||
const WaylandConnection& BarShell::connection() const noexcept {
|
||||
return m_connection;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include "wayland/LayerSurface.hpp"
|
||||
#include "wayland/WaylandConnection.hpp"
|
||||
|
||||
class BarShell {
|
||||
public:
|
||||
BarShell();
|
||||
|
||||
bool initialize();
|
||||
void run();
|
||||
const WaylandConnection& connection() const noexcept;
|
||||
|
||||
private:
|
||||
WaylandConnection m_connection;
|
||||
LayerSurface m_layerSurface;
|
||||
};
|
||||
@@ -0,0 +1,304 @@
|
||||
#include "wayland/LayerSurface.hpp"
|
||||
|
||||
#include "wayland/WaylandConnection.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <wayland-client.h>
|
||||
|
||||
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr std::uint32_t kBarHeight = 36;
|
||||
constexpr std::uint32_t kAnchorMask =
|
||||
ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP |
|
||||
ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT |
|
||||
ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
|
||||
|
||||
const zwlr_layer_surface_v1_listener kLayerSurfaceListener = {
|
||||
.configure = &LayerSurface::handleConfigure,
|
||||
.closed = &LayerSurface::handleClosed,
|
||||
};
|
||||
|
||||
const wl_callback_listener kFrameListener = {
|
||||
.done = &LayerSurface::handleFrameDone,
|
||||
};
|
||||
|
||||
const wl_buffer_listener kBufferListener = {
|
||||
.release = &LayerSurface::handleBufferRelease,
|
||||
};
|
||||
|
||||
int createAnonymousFile(std::size_t size) {
|
||||
char fileTemplate[] = "/tmp/noctalia-layer-surface-XXXXXX";
|
||||
const int fd = mkstemp(fileTemplate);
|
||||
if (fd < 0) {
|
||||
throw std::runtime_error("failed to create shm temp file");
|
||||
}
|
||||
|
||||
unlink(fileTemplate);
|
||||
|
||||
if (ftruncate(fd, static_cast<off_t>(size)) != 0) {
|
||||
close(fd);
|
||||
throw std::runtime_error("failed to resize shm temp file");
|
||||
}
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
LayerSurface::LayerSurface(WaylandConnection& connection)
|
||||
: m_connection(connection) {}
|
||||
|
||||
LayerSurface::~LayerSurface() {
|
||||
cleanup();
|
||||
}
|
||||
|
||||
bool LayerSurface::initialize() {
|
||||
if (!m_connection.hasRequiredGlobals()) {
|
||||
std::cout << "[noctalia] layer surface skipped: missing compositor/shm/layer-shell globals\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!createSurface()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
m_running = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LayerSurface::isRunning() const noexcept {
|
||||
return m_running;
|
||||
}
|
||||
|
||||
void LayerSurface::dispatch() {
|
||||
if (!m_running) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (wl_display_dispatch(m_connection.display()) < 0) {
|
||||
throw std::runtime_error("failed to dispatch Wayland events");
|
||||
}
|
||||
}
|
||||
|
||||
void LayerSurface::handleConfigure(void* data,
|
||||
zwlr_layer_surface_v1* layerSurface,
|
||||
std::uint32_t serial,
|
||||
std::uint32_t width,
|
||||
std::uint32_t height) {
|
||||
auto* self = static_cast<LayerSurface*>(data);
|
||||
zwlr_layer_surface_v1_ack_configure(layerSurface, serial);
|
||||
|
||||
self->m_width = (width == 0) ? 1920 : width;
|
||||
self->m_height = (height == 0) ? kBarHeight : height;
|
||||
self->m_configured = true;
|
||||
|
||||
if (!self->createOrResizeBuffer(self->m_width, self->m_height)) {
|
||||
self->m_running = false;
|
||||
return;
|
||||
}
|
||||
|
||||
self->render();
|
||||
}
|
||||
|
||||
void LayerSurface::handleClosed(void* data,
|
||||
zwlr_layer_surface_v1* /*layerSurface*/) {
|
||||
auto* self = static_cast<LayerSurface*>(data);
|
||||
self->m_running = false;
|
||||
}
|
||||
|
||||
void LayerSurface::handleFrameDone(void* data,
|
||||
wl_callback* callback,
|
||||
std::uint32_t /*callbackData*/) {
|
||||
auto* self = static_cast<LayerSurface*>(data);
|
||||
|
||||
if (callback != nullptr) {
|
||||
wl_callback_destroy(callback);
|
||||
}
|
||||
|
||||
self->m_frameCallback = nullptr;
|
||||
|
||||
if (self->m_running && self->m_configured) {
|
||||
self->requestFrame();
|
||||
}
|
||||
}
|
||||
|
||||
void LayerSurface::handleBufferRelease(void* data,
|
||||
wl_buffer* /*buffer*/) {
|
||||
auto* self = static_cast<LayerSurface*>(data);
|
||||
self->m_bufferBusy = false;
|
||||
}
|
||||
|
||||
bool LayerSurface::createSurface() {
|
||||
m_surface = wl_compositor_create_surface(m_connection.compositor());
|
||||
if (m_surface == nullptr) {
|
||||
throw std::runtime_error("failed to create wl_surface");
|
||||
}
|
||||
|
||||
wl_output* output = nullptr;
|
||||
if (!m_connection.outputs().empty()) {
|
||||
output = m_connection.outputs().front().output;
|
||||
}
|
||||
|
||||
m_layerSurface = zwlr_layer_shell_v1_get_layer_surface(
|
||||
m_connection.layerShell(),
|
||||
m_surface,
|
||||
output,
|
||||
ZWLR_LAYER_SHELL_V1_LAYER_TOP,
|
||||
"noctalia-bar");
|
||||
if (m_layerSurface == nullptr) {
|
||||
throw std::runtime_error("failed to create layer surface");
|
||||
}
|
||||
|
||||
if (zwlr_layer_surface_v1_add_listener(m_layerSurface, &kLayerSurfaceListener, this) != 0) {
|
||||
throw std::runtime_error("failed to add layer surface listener");
|
||||
}
|
||||
|
||||
zwlr_layer_surface_v1_set_anchor(m_layerSurface, kAnchorMask);
|
||||
zwlr_layer_surface_v1_set_size(m_layerSurface, 0, kBarHeight);
|
||||
zwlr_layer_surface_v1_set_exclusive_zone(m_layerSurface, static_cast<int32_t>(kBarHeight));
|
||||
zwlr_layer_surface_v1_set_margin(m_layerSurface, 0, 0, 0, 0);
|
||||
zwlr_layer_surface_v1_set_keyboard_interactivity(
|
||||
m_layerSurface, ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_NONE);
|
||||
|
||||
wl_surface_commit(m_surface);
|
||||
if (wl_display_roundtrip(m_connection.display()) < 0) {
|
||||
throw std::runtime_error("failed during layer surface initial roundtrip");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LayerSurface::createOrResizeBuffer(std::uint32_t width, std::uint32_t height) {
|
||||
if (width == 0 || height == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::size_t pixelCount = static_cast<std::size_t>(width) * static_cast<std::size_t>(height);
|
||||
const std::size_t bufferSize = pixelCount * sizeof(std::uint32_t);
|
||||
|
||||
destroyBuffer();
|
||||
|
||||
m_shmBuffer.fd = createAnonymousFile(bufferSize);
|
||||
m_shmBuffer.size = bufferSize;
|
||||
m_shmBuffer.data = mmap(nullptr, bufferSize, PROT_READ | PROT_WRITE, MAP_SHARED, m_shmBuffer.fd, 0);
|
||||
if (m_shmBuffer.data == MAP_FAILED) {
|
||||
const int savedErrno = errno;
|
||||
close(m_shmBuffer.fd);
|
||||
m_shmBuffer.fd = -1;
|
||||
throw std::runtime_error("failed to mmap shm buffer: " + std::string(std::strerror(savedErrno)));
|
||||
}
|
||||
|
||||
m_shmBuffer.pool = wl_shm_create_pool(
|
||||
m_connection.shm(), m_shmBuffer.fd, static_cast<int32_t>(bufferSize));
|
||||
if (m_shmBuffer.pool == nullptr) {
|
||||
destroyBuffer();
|
||||
throw std::runtime_error("failed to create wl_shm_pool");
|
||||
}
|
||||
|
||||
m_shmBuffer.buffer = wl_shm_pool_create_buffer(
|
||||
m_shmBuffer.pool,
|
||||
0,
|
||||
static_cast<int32_t>(width),
|
||||
static_cast<int32_t>(height),
|
||||
static_cast<int32_t>(width * sizeof(std::uint32_t)),
|
||||
WL_SHM_FORMAT_XRGB8888);
|
||||
if (m_shmBuffer.buffer == nullptr) {
|
||||
destroyBuffer();
|
||||
throw std::runtime_error("failed to create wl_buffer");
|
||||
}
|
||||
|
||||
if (wl_buffer_add_listener(m_shmBuffer.buffer, &kBufferListener, this) != 0) {
|
||||
destroyBuffer();
|
||||
throw std::runtime_error("failed to add wl_buffer listener");
|
||||
}
|
||||
|
||||
m_shmBuffer.pixels.resize(pixelCount);
|
||||
return true;
|
||||
}
|
||||
|
||||
void LayerSurface::render() {
|
||||
if (m_surface == nullptr || m_shmBuffer.buffer == nullptr || m_bufferBusy) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::fill(m_shmBuffer.pixels.begin(), m_shmBuffer.pixels.end(), 0x00121A24u);
|
||||
std::memcpy(m_shmBuffer.data, m_shmBuffer.pixels.data(), m_shmBuffer.size);
|
||||
|
||||
wl_surface_attach(m_surface, m_shmBuffer.buffer, 0, 0);
|
||||
wl_surface_damage_buffer(m_surface, 0, 0, static_cast<int32_t>(m_width), static_cast<int32_t>(m_height));
|
||||
requestFrame();
|
||||
wl_surface_commit(m_surface);
|
||||
m_bufferBusy = true;
|
||||
}
|
||||
|
||||
void LayerSurface::requestFrame() {
|
||||
if (m_frameCallback != nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_frameCallback = wl_surface_frame(m_surface);
|
||||
if (m_frameCallback != nullptr) {
|
||||
wl_callback_add_listener(m_frameCallback, &kFrameListener, this);
|
||||
}
|
||||
}
|
||||
|
||||
void LayerSurface::destroyBuffer() {
|
||||
if (m_frameCallback != nullptr) {
|
||||
wl_callback_destroy(m_frameCallback);
|
||||
m_frameCallback = nullptr;
|
||||
}
|
||||
|
||||
if (m_shmBuffer.buffer != nullptr) {
|
||||
wl_buffer_destroy(m_shmBuffer.buffer);
|
||||
m_shmBuffer.buffer = nullptr;
|
||||
}
|
||||
|
||||
if (m_shmBuffer.pool != nullptr) {
|
||||
wl_shm_pool_destroy(m_shmBuffer.pool);
|
||||
m_shmBuffer.pool = nullptr;
|
||||
}
|
||||
|
||||
if (m_shmBuffer.data != nullptr && m_shmBuffer.data != MAP_FAILED) {
|
||||
munmap(m_shmBuffer.data, m_shmBuffer.size);
|
||||
m_shmBuffer.data = nullptr;
|
||||
}
|
||||
|
||||
if (m_shmBuffer.fd >= 0) {
|
||||
close(m_shmBuffer.fd);
|
||||
m_shmBuffer.fd = -1;
|
||||
}
|
||||
|
||||
m_shmBuffer.size = 0;
|
||||
m_shmBuffer.pixels.clear();
|
||||
m_bufferBusy = false;
|
||||
}
|
||||
|
||||
void LayerSurface::cleanup() {
|
||||
destroyBuffer();
|
||||
|
||||
if (m_layerSurface != nullptr) {
|
||||
zwlr_layer_surface_v1_destroy(m_layerSurface);
|
||||
m_layerSurface = nullptr;
|
||||
}
|
||||
|
||||
if (m_surface != nullptr) {
|
||||
wl_surface_destroy(m_surface);
|
||||
m_surface = nullptr;
|
||||
}
|
||||
|
||||
m_running = false;
|
||||
m_configured = false;
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
struct wl_buffer;
|
||||
struct wl_callback;
|
||||
struct wl_shm_pool;
|
||||
struct wl_surface;
|
||||
struct zwlr_layer_surface_v1;
|
||||
|
||||
class WaylandConnection;
|
||||
|
||||
class LayerSurface {
|
||||
public:
|
||||
explicit LayerSurface(WaylandConnection& connection);
|
||||
~LayerSurface();
|
||||
|
||||
LayerSurface(const LayerSurface&) = delete;
|
||||
LayerSurface& operator=(const LayerSurface&) = delete;
|
||||
|
||||
bool initialize();
|
||||
bool isRunning() const noexcept;
|
||||
void dispatch();
|
||||
|
||||
static void handleConfigure(void* data,
|
||||
zwlr_layer_surface_v1* layerSurface,
|
||||
std::uint32_t serial,
|
||||
std::uint32_t width,
|
||||
std::uint32_t height);
|
||||
static void handleClosed(void* data,
|
||||
zwlr_layer_surface_v1* layerSurface);
|
||||
static void handleFrameDone(void* data,
|
||||
wl_callback* callback,
|
||||
std::uint32_t callbackData);
|
||||
static void handleBufferRelease(void* data,
|
||||
wl_buffer* buffer);
|
||||
|
||||
private:
|
||||
struct ShmBuffer {
|
||||
int fd = -1;
|
||||
std::size_t size = 0;
|
||||
void* data = nullptr;
|
||||
wl_shm_pool* pool = nullptr;
|
||||
wl_buffer* buffer = nullptr;
|
||||
std::vector<std::uint32_t> pixels;
|
||||
};
|
||||
|
||||
bool createSurface();
|
||||
bool createOrResizeBuffer(std::uint32_t width, std::uint32_t height);
|
||||
void render();
|
||||
void requestFrame();
|
||||
void destroyBuffer();
|
||||
void cleanup();
|
||||
|
||||
WaylandConnection& m_connection;
|
||||
wl_surface* m_surface = nullptr;
|
||||
zwlr_layer_surface_v1* m_layerSurface = nullptr;
|
||||
wl_callback* m_frameCallback = nullptr;
|
||||
bool m_running = false;
|
||||
bool m_configured = false;
|
||||
bool m_bufferBusy = false;
|
||||
std::uint32_t m_width = 0;
|
||||
std::uint32_t m_height = 0;
|
||||
ShmBuffer m_shmBuffer;
|
||||
};
|
||||
@@ -0,0 +1,244 @@
|
||||
#include "wayland/WaylandConnection.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
|
||||
#include <wayland-client.h>
|
||||
|
||||
#if NOCTALIA_HAVE_WLR_LAYER_SHELL
|
||||
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
|
||||
#endif
|
||||
#include "xdg-output-unstable-v1-client-protocol.h"
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr std::uint32_t kCompositorVersion = 4;
|
||||
constexpr std::uint32_t kSeatVersion = 5;
|
||||
constexpr std::uint32_t kShmVersion = 1;
|
||||
#if NOCTALIA_HAVE_WLR_LAYER_SHELL
|
||||
constexpr std::uint32_t kLayerShellVersion = 4;
|
||||
#endif
|
||||
constexpr std::uint32_t kXdgOutputManagerVersion = 3;
|
||||
|
||||
const wl_registry_listener kRegistryListener = {
|
||||
.global = &WaylandConnection::handleGlobal,
|
||||
.global_remove = &WaylandConnection::handleGlobalRemove,
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
WaylandConnection::WaylandConnection() = default;
|
||||
|
||||
WaylandConnection::~WaylandConnection() {
|
||||
cleanup();
|
||||
}
|
||||
|
||||
bool WaylandConnection::connect() {
|
||||
if (m_display != nullptr) {
|
||||
return true;
|
||||
}
|
||||
|
||||
m_display = wl_display_connect(nullptr);
|
||||
if (m_display == nullptr) {
|
||||
throw std::runtime_error("failed to connect to Wayland display");
|
||||
}
|
||||
|
||||
m_registry = wl_display_get_registry(m_display);
|
||||
if (m_registry == nullptr) {
|
||||
cleanup();
|
||||
throw std::runtime_error("failed to acquire Wayland registry");
|
||||
}
|
||||
|
||||
if (wl_registry_add_listener(m_registry, &kRegistryListener, this) != 0) {
|
||||
cleanup();
|
||||
throw std::runtime_error("failed to add Wayland registry listener");
|
||||
}
|
||||
|
||||
if (wl_display_roundtrip(m_display) < 0) {
|
||||
cleanup();
|
||||
throw std::runtime_error("failed during Wayland registry roundtrip");
|
||||
}
|
||||
|
||||
if (wl_display_roundtrip(m_display) < 0) {
|
||||
cleanup();
|
||||
throw std::runtime_error("failed during Wayland output discovery roundtrip");
|
||||
}
|
||||
|
||||
logStartupSummary();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WaylandConnection::isConnected() const noexcept {
|
||||
return m_display != nullptr;
|
||||
}
|
||||
|
||||
bool WaylandConnection::hasRequiredGlobals() const noexcept {
|
||||
return m_compositor != nullptr && m_shm != nullptr && m_layerShell != nullptr;
|
||||
}
|
||||
|
||||
bool WaylandConnection::hasLayerShell() const noexcept {
|
||||
return m_hasLayerShellGlobal;
|
||||
}
|
||||
|
||||
bool WaylandConnection::hasXdgOutputManager() const noexcept {
|
||||
return m_xdgOutputManager != nullptr;
|
||||
}
|
||||
|
||||
wl_display* WaylandConnection::display() const noexcept {
|
||||
return m_display;
|
||||
}
|
||||
|
||||
wl_compositor* WaylandConnection::compositor() const noexcept {
|
||||
return m_compositor;
|
||||
}
|
||||
|
||||
wl_shm* WaylandConnection::shm() const noexcept {
|
||||
return m_shm;
|
||||
}
|
||||
|
||||
zwlr_layer_shell_v1* WaylandConnection::layerShell() const noexcept {
|
||||
return m_layerShell;
|
||||
}
|
||||
|
||||
const std::vector<WaylandOutput>& WaylandConnection::outputs() const noexcept {
|
||||
return m_outputs;
|
||||
}
|
||||
|
||||
void WaylandConnection::handleGlobal(void* data,
|
||||
wl_registry* registry,
|
||||
std::uint32_t name,
|
||||
const char* interface,
|
||||
std::uint32_t version) {
|
||||
auto* self = static_cast<WaylandConnection*>(data);
|
||||
self->bindGlobal(registry, name, interface, version);
|
||||
}
|
||||
|
||||
void WaylandConnection::handleGlobalRemove(void* data,
|
||||
wl_registry* /*registry*/,
|
||||
std::uint32_t name) {
|
||||
auto* self = static_cast<WaylandConnection*>(data);
|
||||
std::erase_if(self->m_outputs, [name](const WaylandOutput& output) {
|
||||
return output.name == name;
|
||||
});
|
||||
}
|
||||
|
||||
void WaylandConnection::bindGlobal(wl_registry* registry,
|
||||
std::uint32_t name,
|
||||
const char* interface,
|
||||
std::uint32_t version) {
|
||||
const std::string interfaceName = interface;
|
||||
|
||||
if (interfaceName == wl_compositor_interface.name) {
|
||||
const auto bindVersion = std::min(version, kCompositorVersion);
|
||||
m_compositor = static_cast<wl_compositor*>(
|
||||
wl_registry_bind(registry, name, &wl_compositor_interface, bindVersion));
|
||||
return;
|
||||
}
|
||||
|
||||
if (interfaceName == wl_seat_interface.name) {
|
||||
const auto bindVersion = std::min(version, kSeatVersion);
|
||||
m_seat = static_cast<wl_seat*>(
|
||||
wl_registry_bind(registry, name, &wl_seat_interface, bindVersion));
|
||||
return;
|
||||
}
|
||||
|
||||
if (interfaceName == wl_shm_interface.name) {
|
||||
const auto bindVersion = std::min(version, kShmVersion);
|
||||
m_shm = static_cast<wl_shm*>(
|
||||
wl_registry_bind(registry, name, &wl_shm_interface, bindVersion));
|
||||
return;
|
||||
}
|
||||
|
||||
if (interfaceName == "zwlr_layer_shell_v1") {
|
||||
m_hasLayerShellGlobal = true;
|
||||
#if NOCTALIA_HAVE_WLR_LAYER_SHELL
|
||||
const auto bindVersion = std::min(version, kLayerShellVersion);
|
||||
m_layerShell = static_cast<zwlr_layer_shell_v1*>(
|
||||
wl_registry_bind(registry, name, &zwlr_layer_shell_v1_interface, bindVersion));
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
if (interfaceName == zxdg_output_manager_v1_interface.name) {
|
||||
const auto bindVersion = std::min(version, kXdgOutputManagerVersion);
|
||||
m_xdgOutputManager = static_cast<zxdg_output_manager_v1*>(
|
||||
wl_registry_bind(registry, name, &zxdg_output_manager_v1_interface, bindVersion));
|
||||
return;
|
||||
}
|
||||
|
||||
if (interfaceName == wl_output_interface.name) {
|
||||
auto* output = static_cast<wl_output*>(
|
||||
wl_registry_bind(registry, name, &wl_output_interface, 1));
|
||||
m_outputs.push_back(WaylandOutput{
|
||||
.name = name,
|
||||
.interfaceName = interfaceName,
|
||||
.version = version,
|
||||
.output = output,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void WaylandConnection::cleanup() {
|
||||
if (m_xdgOutputManager != nullptr) {
|
||||
zxdg_output_manager_v1_destroy(m_xdgOutputManager);
|
||||
m_xdgOutputManager = nullptr;
|
||||
}
|
||||
|
||||
if (m_layerShell != nullptr) {
|
||||
#if NOCTALIA_HAVE_WLR_LAYER_SHELL
|
||||
zwlr_layer_shell_v1_destroy(m_layerShell);
|
||||
#endif
|
||||
m_layerShell = nullptr;
|
||||
}
|
||||
|
||||
if (m_seat != nullptr) {
|
||||
wl_seat_destroy(m_seat);
|
||||
m_seat = nullptr;
|
||||
}
|
||||
|
||||
if (m_shm != nullptr) {
|
||||
wl_shm_destroy(m_shm);
|
||||
m_shm = nullptr;
|
||||
}
|
||||
|
||||
if (m_compositor != nullptr) {
|
||||
wl_compositor_destroy(m_compositor);
|
||||
m_compositor = nullptr;
|
||||
}
|
||||
|
||||
for (auto& output : m_outputs) {
|
||||
if (output.output != nullptr) {
|
||||
wl_output_destroy(output.output);
|
||||
output.output = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
if (m_registry != nullptr) {
|
||||
wl_registry_destroy(m_registry);
|
||||
m_registry = nullptr;
|
||||
}
|
||||
|
||||
if (m_display != nullptr) {
|
||||
wl_display_disconnect(m_display);
|
||||
m_display = nullptr;
|
||||
}
|
||||
|
||||
m_outputs.clear();
|
||||
}
|
||||
|
||||
void WaylandConnection::logStartupSummary() const {
|
||||
std::cout << "[noctalia] wayland connected"
|
||||
<< " compositor=" << (m_compositor != nullptr ? "yes" : "no")
|
||||
<< " shm=" << (m_shm != nullptr ? "yes" : "no")
|
||||
<< " layer-shell=" << (hasLayerShell() ? "yes" : "no")
|
||||
<< " xdg-output=" << (hasXdgOutputManager() ? "yes" : "no")
|
||||
<< " outputs=" << m_outputs.size()
|
||||
<< '\n';
|
||||
|
||||
for (const auto& output : m_outputs) {
|
||||
std::cout << "[noctalia] output global=" << output.name
|
||||
<< " version=" << output.version
|
||||
<< '\n';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
struct wl_compositor;
|
||||
struct wl_display;
|
||||
struct wl_output;
|
||||
struct wl_registry;
|
||||
struct wl_seat;
|
||||
struct wl_shm;
|
||||
struct zwlr_layer_shell_v1;
|
||||
struct zxdg_output_manager_v1;
|
||||
|
||||
struct WaylandOutput {
|
||||
std::uint32_t name = 0;
|
||||
std::string interfaceName;
|
||||
std::uint32_t version = 0;
|
||||
wl_output* output = nullptr;
|
||||
};
|
||||
|
||||
class WaylandConnection {
|
||||
public:
|
||||
WaylandConnection();
|
||||
~WaylandConnection();
|
||||
|
||||
WaylandConnection(const WaylandConnection&) = delete;
|
||||
WaylandConnection& operator=(const WaylandConnection&) = delete;
|
||||
|
||||
bool connect();
|
||||
|
||||
bool isConnected() const noexcept;
|
||||
bool hasRequiredGlobals() const noexcept;
|
||||
bool hasLayerShell() const noexcept;
|
||||
bool hasXdgOutputManager() const noexcept;
|
||||
wl_display* display() const noexcept;
|
||||
wl_compositor* compositor() const noexcept;
|
||||
wl_shm* shm() const noexcept;
|
||||
zwlr_layer_shell_v1* layerShell() const noexcept;
|
||||
const std::vector<WaylandOutput>& outputs() const noexcept;
|
||||
static void handleGlobal(void* data,
|
||||
wl_registry* registry,
|
||||
std::uint32_t name,
|
||||
const char* interface,
|
||||
std::uint32_t version);
|
||||
static void handleGlobalRemove(void* data,
|
||||
wl_registry* registry,
|
||||
std::uint32_t name);
|
||||
|
||||
private:
|
||||
void bindGlobal(wl_registry* registry,
|
||||
std::uint32_t name,
|
||||
const char* interface,
|
||||
std::uint32_t version);
|
||||
void cleanup();
|
||||
void logStartupSummary() const;
|
||||
|
||||
wl_display* m_display = nullptr;
|
||||
wl_registry* m_registry = nullptr;
|
||||
wl_compositor* m_compositor = nullptr;
|
||||
wl_seat* m_seat = nullptr;
|
||||
wl_shm* m_shm = nullptr;
|
||||
zwlr_layer_shell_v1* m_layerShell = nullptr;
|
||||
zxdg_output_manager_v1* m_xdgOutputManager = nullptr;
|
||||
bool m_hasLayerShellGlobal = false;
|
||||
std::vector<WaylandOutput> m_outputs;
|
||||
};
|
||||
Reference in New Issue
Block a user