Skip to content

Commit

Permalink
Move display connection external
Browse files Browse the repository at this point in the history
Signed-off-by: Joel Winarske <joel.winarske@gmail.com>
  • Loading branch information
jwinarske committed May 6, 2024
1 parent 3868151 commit bf58c95
Show file tree
Hide file tree
Showing 25 changed files with 253 additions and 128 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if (NOT CMAKE_BUILD_TYPE)
endif ()

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_DIR}/third_party/sanitizers-cmake/cmake)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/third_party/sanitizers-cmake/cmake)

if (NOT BUILD_NUMBER)
set(BUILD_NUMBER 0)
Expand Down
6 changes: 6 additions & 0 deletions cmake/context.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
# limitations under the License.
#

if (CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
set(BUILD_STANDALONE ON)
else()
set(BUILD_STANDALONE OFF)
endif()

#
# Branch
#
Expand Down
7 changes: 7 additions & 0 deletions cmake/options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
# limitations under the License.
#

#
# Configure options
#
option(ENABLE_XDG_CLIENT "Enable XDG Client" ON)
option(ENABLE_AGL_CLIENT "Enable AGL Shell" OFF)
option(ENABLE_IVI_SHELL_CLIENT "Enable IVI Shell Client" OFF)
option(ENABLE_DRM_LEASE_CLIENT "Enable DRM Lease Client" OFF)

#
# Link Time Optimization
Expand Down
5 changes: 4 additions & 1 deletion cmake/wayland.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ endmacro()
set(WAYLAND_PROTOCOL_SOURCES)

file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/protocols)
include_directories(${CMAKE_CURRENT_BINARY_DIR}/protocols)

add_protocol(${WAYLAND_PROTOCOLS_BASE}/stable/xdg-shell/xdg-shell.xml)
add_protocol(${CMAKE_CURRENT_SOURCE_DIR}/third_party/agl/protocol/agl-shell.xml)
Expand Down Expand Up @@ -121,6 +120,10 @@ configure_file(cmake/wayland-protocols.h.in ${CMAKE_CURRENT_BINARY_DIR}/protocol

add_library(wayland-gen STATIC ${WAYLAND_PROTOCOL_SOURCES})
target_link_libraries(wayland-gen PUBLIC PkgConfig::WAYLAND)
target_include_directories(wayland-gen PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_BINARY_DIR}/protocols
)

if (IPO_SUPPORT_RESULT)
set_property(TARGET wayland-gen PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
Expand Down
16 changes: 14 additions & 2 deletions examples/agl-capture.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ class App : public WestonCaptureObserver {
public:
explicit App(const Configuration &config) : logging_(std::make_unique<Logging>()), weston_capture_v1_(nullptr) {

agl_shell_ = std::make_unique<AglShell>(false);
display_ = wl_display_connect(nullptr);
if (!display_) {
spdlog::critical("Unable to connect to Wayland socket.");
exit(EXIT_FAILURE);
}

agl_shell_ = std::make_unique<AglShell>(display_, false);
auto d = agl_shell_->get_display();

// required when not creating a window
Expand Down Expand Up @@ -141,14 +147,20 @@ class App : public WestonCaptureObserver {
spdlog::debug("failed: {}", msg);
}

~App() override = default;
~App() override {
if (display_) {
wl_display_flush(display_);
wl_display_disconnect(display_);
}
};

bool run() {
/// display_dispatch is blocking
return (gRunning && agl_shell_->display_dispatch() != -1);
}

private:
struct wl_display *display_;
std::unique_ptr<Logging> logging_;
std::unique_ptr<AglShell> agl_shell_;
std::list<std::unique_ptr<WestonCapture>> weston_capture_list_;
Expand Down
14 changes: 13 additions & 1 deletion examples/agl-simple-shm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,13 @@ class App : public PointerObserver, public KeyboardObserver, public SeatObserver
explicit App(const Configuration &config) : logging_(std::make_unique<Logging>()),
gen_(rd_()) {

agl_shell_ = std::make_unique<AglShell>(config.disable_cursor);
display_ = wl_display_connect(nullptr);
if (!display_) {
spdlog::critical("Unable to connect to Wayland socket.");
exit(EXIT_FAILURE);
}

agl_shell_ = std::make_unique<AglShell>(display_, config.disable_cursor);
spdlog::info("AGL Shell Version: {}", agl_shell_->get_version());
auto seat = agl_shell_->get_seat();
if (seat.has_value()) {
Expand Down Expand Up @@ -165,6 +171,11 @@ class App : public PointerObserver, public KeyboardObserver, public SeatObserver

~App() override {
toplevel_->stop_frame_callbacks();

if (display_) {
wl_display_flush(display_);
wl_display_disconnect(display_);
}
}

bool run() {
Expand Down Expand Up @@ -301,6 +312,7 @@ class App : public PointerObserver, public KeyboardObserver, public SeatObserver
}

private:
struct wl_display *display_;
std::unique_ptr<Logging> logging_;
std::unique_ptr<AglShell> agl_shell_;
struct wl_output *output_;
Expand Down
11 changes: 10 additions & 1 deletion examples/gl-shadertoy/shadertoy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,12 @@ int main(int argc, char **argv) {

auto logging = std::make_unique<Logging>();

auto display = wl_display_connect(nullptr);
if (!display) {
spdlog::critical("Unable to connect to Wayland socket.");
exit(EXIT_FAILURE);
}

std::signal(SIGINT, handle_signal);

cxxopts::Options options("toy-shader", "Toy Shader");
Expand Down Expand Up @@ -364,7 +370,7 @@ int main(int argc, char **argv) {

auto keyboard_handler = std::make_unique<KeyboardHandler>();

XdgWindowManager wm;
XdgWindowManager wm(display);
auto seat = wm.get_seat();
if (seat.has_value()) {
auto keyboard = seat.value()->get_keyboard();
Expand Down Expand Up @@ -396,5 +402,8 @@ int main(int argc, char **argv) {

top_level->stop_frame_callbacks();

wl_display_flush(display);
wl_display_disconnect(display);

return EXIT_SUCCESS;
}
12 changes: 11 additions & 1 deletion examples/presentation-shm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ struct Feedback {
};

struct Context {
struct wl_display *display;
std::unique_ptr<XdgWindowManager> wm;
Configuration config;
XdgTopLevel *toplevel;
Expand Down Expand Up @@ -280,7 +281,13 @@ int main(int argc, char **argv) {

ctx->config.refresh_nsec = kNanoSecondPerSecond / 60;

ctx->wm = std::make_unique<XdgWindowManager>();
ctx->display = wl_display_connect(nullptr);
if (!ctx->display) {
spdlog::critical("Unable to connect to Wayland socket.");
exit(EXIT_FAILURE);
}

ctx->wm = std::make_unique<XdgWindowManager>(ctx->display);

spdlog::info("XDG Window Manager Version: {}", ctx->wm->get_version());

Expand Down Expand Up @@ -335,5 +342,8 @@ int main(int argc, char **argv) {

ctx->toplevel->stop_frame_callbacks();

wl_display_flush(ctx->display);
wl_display_disconnect(ctx->display);

return EXIT_SUCCESS;
}
11 changes: 10 additions & 1 deletion examples/simple-egl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,12 @@ int main(int argc, char **argv) {

auto logging = std::make_unique<Logging>();

auto display = wl_display_connect(nullptr);
if (!display) {
spdlog::critical("Unable to connect to Wayland socket.");
exit(EXIT_FAILURE);
}

std::signal(SIGINT, handle_signal);

cxxopts::Options options("simple-egl", "Weston simple-egl example");
Expand Down Expand Up @@ -502,7 +508,7 @@ int main(int argc, char **argv) {

auto keyboard_handler = std::make_unique<KeyboardHandler>();

XdgWindowManager wm;
XdgWindowManager wm(display);
auto seat = wm.get_seat();
if (seat.has_value()) {
auto keyboard = seat.value()->get_keyboard();
Expand Down Expand Up @@ -533,5 +539,8 @@ int main(int argc, char **argv) {

top_level->stop_frame_callbacks();

wl_display_flush(display);
wl_display_disconnect(display);

return EXIT_SUCCESS;
}
11 changes: 10 additions & 1 deletion examples/simple-ext-protocol.cc
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ int main(int argc, char **argv) {

auto logging = std::make_unique<Logging>();

auto display = wl_display_connect(nullptr);
if (!display) {
spdlog::critical("Unable to connect to Wayland socket.");
exit(EXIT_FAILURE);
}

std::signal(SIGINT, handle_signal);

cxxopts::Options options("simple-shm", "Weston simple-shm example");
Expand All @@ -172,7 +178,7 @@ int main(int argc, char **argv) {
.tearing = result["tearing"].as<bool>(),
};

XdgWindowManager wm = XdgWindowManager(false, ext_interfaces.size(),
XdgWindowManager wm = XdgWindowManager(display, false, ext_interfaces.size(),
ext_interfaces.data()
);
spdlog::info("XDG Window Manager Version: {}", wm.get_version());
Expand All @@ -199,5 +205,8 @@ int main(int argc, char **argv) {

top_level->stop_frame_callbacks();

wl_display_flush(display);
wl_display_disconnect(display);

return EXIT_SUCCESS;
}
15 changes: 14 additions & 1 deletion examples/simple-shm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include <cxxopts.hpp>

#include <wayland-client.h>
#include "window/xdg_toplevel.h"
#include "logging.h"

Expand Down Expand Up @@ -129,7 +130,13 @@ class App : public PointerObserver, public KeyboardObserver, public SeatObserver
explicit App(const Configuration &config) : logging_(std::make_unique<Logging>()),
gen_(rd_()) {

wm_ = std::make_unique<XdgWindowManager>(config.disable_cursor);
wl_display_ = wl_display_connect(nullptr);
if (!wl_display_) {
spdlog::critical("Unable to connect to Wayland socket.");
exit(EXIT_FAILURE);
}

wm_ = std::make_unique<XdgWindowManager>(wl_display_, config.disable_cursor);
auto seat = wm_->get_seat();
if (seat.has_value()) {
seat.value()->register_observer(this);
Expand Down Expand Up @@ -158,6 +165,11 @@ class App : public PointerObserver, public KeyboardObserver, public SeatObserver

~App() override {
toplevel_->stop_frame_callbacks();

if (wl_display_) {
wl_display_flush(wl_display_);
wl_display_disconnect(wl_display_);
}
}

bool run() {
Expand Down Expand Up @@ -294,6 +306,7 @@ class App : public PointerObserver, public KeyboardObserver, public SeatObserver
}

private:
struct wl_display *wl_display_;
std::unique_ptr<Logging> logging_;
std::unique_ptr<XdgWindowManager> wm_;
XdgTopLevel *toplevel_;
Expand Down
13 changes: 11 additions & 2 deletions examples/vk-shadertoy/app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,18 @@ App::App(const Configuration &config) : handlers_(std::make_unique<Handlers>()),

spdlog::info("{}", kAppTitle);

display_ = wl_display_connect(nullptr);
if (!display_) {
spdlog::critical("Unable to connect to Wayland socket.");
exit(EXIT_FAILURE);
}

std::thread t1([&] {
backend_ = std::make_unique<VulkanBackend>(kAppId, config.debug_enable);
});

std::thread t2([&] {
wm_ = std::make_unique<XdgWindowManager>(config.disable_cursor);
wm_ = std::make_unique<XdgWindowManager>(display_, config.disable_cursor);
auto seat = wm_->get_seat();
if (seat.has_value()) {
seat.value()->register_observer(handlers_.get());
Expand All @@ -66,7 +72,8 @@ App::App(const Configuration &config) : handlers_(std::make_unique<Handlers>()),
t1.join();
t2.join();

backend_->CreateSurface(wm_->get_display(), toplevel_->get_surface(), config.width, config.height, kOffscreenBuffers);
backend_->CreateSurface(wm_->get_display(), toplevel_->get_surface(), config.width, config.height,
kOffscreenBuffers);

/// paint padding
toplevel_->set_surface_damage(0, 0, config.width, config.height);
Expand All @@ -75,6 +82,8 @@ App::App(const Configuration &config) : handlers_(std::make_unique<Handlers>()),

App::~App() {
toplevel_->stop_frame_callbacks();
wl_display_flush(display_);
wl_display_flush(display_);
}

bool App::run() {
Expand Down
1 change: 1 addition & 0 deletions examples/vk-shadertoy/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class App {
bool run();

private:
struct wl_display *display_;
std::unique_ptr<Logging> logging_;
std::unique_ptr<Handlers> handlers_;
std::unique_ptr<XdgWindowManager> wm_;
Expand Down
3 changes: 0 additions & 3 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,5 @@ static constexpr uint32_t kFractionalScaleManagerMinVersion = UINT32_C(1);
static constexpr uint32_t kXdgDecorationManagerMinVersion = UINT32_C(1);
static constexpr uint32_t kWestonCaptureV1MinVersion = UINT32_C(1);

/// Logging Constants
static constexpr int64_t kLogFlushInterval = INT64_C(5);


#endif // INCLUDE_CONFIG_H_
6 changes: 5 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ add_library(waypp
${SEAT_SRC}
${WINDOW_SRC}
command.cc
logging.cc
)

if (BUILD_STANDALONE)
target_sources(waypp PRIVATE
logging.cc)
endif ()

if (ENABLE_XDG_CLIENT)
target_sources(waypp PRIVATE
window/xdg_toplevel.cc
Expand Down
2 changes: 2 additions & 0 deletions src/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include "config.h"

static constexpr int32_t kLogFlushInterval = INT32_C(5);

Logging::Logging() {
console_sink_ = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
logger_ = std::make_shared<spdlog::logger>("waypp", console_sink_);
Expand Down
3 changes: 1 addition & 2 deletions src/seat/keyboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ void Keyboard::handle_keymap(void *data,
close(fd);
xkb_state_unref(obj->xkb_state_);
obj->xkb_state_ = xkb_state_new(obj->xkb_keymap_);
}
else {
} else {
spdlog::warn("Usage without libxkbcommon is currently not supported.");
}

Expand Down
6 changes: 4 additions & 2 deletions src/seat/seat.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ class SeatObserver {

class Seat {
public:
explicit Seat(struct wl_seat *seat, struct wl_shm *wl_shm,
struct wl_compositor *wl_compositor, bool disable_cursor = false);
explicit Seat(struct wl_seat *seat,
struct wl_shm *wl_shm,
struct wl_compositor *wl_compositor,
bool disable_cursor = false);

~Seat();

Expand Down
Loading

0 comments on commit bf58c95

Please sign in to comment.