Skip to content

Commit

Permalink
Enable to start session
Browse files Browse the repository at this point in the history
  • Loading branch information
Aki-7 committed Apr 2, 2023
1 parent 68b7375 commit 5531da2
Show file tree
Hide file tree
Showing 12 changed files with 295 additions and 25 deletions.
29 changes: 25 additions & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,22 @@ jobs:
steps:
- name: Install dependencies
run: |
echo "deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-15 main" | sudo tee -a /etc/apt/sources.list
echo "deb-src http://apt.llvm.org/jammy/ llvm-toolchain-jammy-15 main" | sudo tee -a /etc/apt/sources.list
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
sudo apt-get update
sudo apt-get install \
libwayland-dev libwlroots-dev libpixman-1-dev \
libxkbcommon-dev libglu1-mesa-dev libglew-dev \
meson libfreetype-dev librsvg2-dev libcglm-dev clang-15
meson libfreetype-dev librsvg2-dev libcglm-dev
- name: Install clang15
if: matrix.clang-version == 15
run: |
echo "deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-15 main" | sudo tee -a /etc/apt/sources.list
echo "deb-src http://apt.llvm.org/jammy/ llvm-toolchain-jammy-15 main" | sudo tee -a /etc/apt/sources.list
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
sudo apt-get update
sudo apt-get install clang-15 clang-format-15 clang-tidy-15
ln -s /usr/bin/clang-tidy /usr/bin/clang-tidy-15
ln -s /usr/bin/clang-format /usr/bin/clang-format-15
- uses: actions/setup-python@v1
with:
Expand All @@ -46,12 +54,25 @@ jobs:
unzip grpc-dev.zip
working-directory: ./grpc-dev

- name: Clone zen-protocols
uses: actions/checkout@v2
with:
repository: zwin-project/zen-protocols
path: zen-protocols

- name: Install zen-protocols
working-directory: ./zen-protocols
run: |
meson build
sudo ninja -C build install
- name: Clone zen-remote
uses: actions/checkout@v2
with:
repository: zwin-project/zen-remote
path: zen-remote
submodules: recursive
ref: 'v0.2.0'

- name: Build zen-remote
working-directory: ./zen-remote
Expand Down
1 change: 1 addition & 0 deletions desktop/include/zen-desktop/xr-system.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct zn_desktop_xr_system {
struct wl_list resource_list; // wl_resource::link of zen_xr_system

struct wl_listener zn_xr_system_destroy_listener;
struct wl_listener session_status_changed_listener;
};

struct zn_desktop_xr_system *zn_desktop_xr_system_create(
Expand Down
39 changes: 37 additions & 2 deletions desktop/src/xr-system.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ zn_desktop_xr_system_handle_destroy(struct wl_resource *resource)

static void
zn_desktop_xr_system_protocol_connect(
struct wl_client *client UNUSED, struct wl_resource *resource UNUSED)
{}
struct wl_client *client UNUSED, struct wl_resource *resource)
{
struct zn_desktop_xr_system *self = wl_resource_get_user_data(resource);

zn_xr_system_connect(self->zn_xr_system);
}

static const struct zen_xr_system_interface implementation = {
.connect = zn_desktop_xr_system_protocol_connect,
Expand Down Expand Up @@ -53,6 +57,31 @@ zn_desktop_xr_system_handle_xr_system_destroy(
zn_desktop_xr_system_destroy(self);
}

static void
zn_desktop_xr_system_handle_session_status_changed(
struct wl_listener *listener, void *data UNUSED)
{
struct zn_desktop_xr_system *self =
zn_container_of(listener, self, session_status_changed_listener);

struct wl_resource *resource = NULL;

enum zen_xr_system_status status = ZEN_XR_SYSTEM_STATUS_CONNECTED;

switch (self->zn_xr_system->status) {
case ZN_XR_SYSTEM_SESSION_STATUS_CONNECTED:
status = ZEN_XR_SYSTEM_STATUS_CONNECTED;
break;
case ZN_XR_SYSTEM_SESSION_STATUS_NOT_CONNECTED:
status = ZEN_XR_SYSTEM_STATUS_UNAVAILABLE;
break;
}

wl_resource_for_each (resource, &self->resource_list) {
zen_xr_system_send_status(resource, status);
}
}

struct zn_desktop_xr_system *
zn_desktop_xr_system_create(
struct zn_xr_system *zn_xr_system, struct wl_display *display)
Expand All @@ -78,6 +107,11 @@ zn_desktop_xr_system_create(
wl_signal_add(
&zn_xr_system->events.destroy, &self->zn_xr_system_destroy_listener);

self->session_status_changed_listener.notify =
zn_desktop_xr_system_handle_session_status_changed;
wl_signal_add(&zn_xr_system->events.session_status_changed,
&self->session_status_changed_listener);

return self;

err_free:
Expand All @@ -90,6 +124,7 @@ zn_desktop_xr_system_create(
static void
zn_desktop_xr_system_destroy(struct zn_desktop_xr_system *self)
{
wl_list_remove(&self->session_status_changed_listener.link);
wl_list_remove(&self->zn_xr_system_destroy_listener.link);
wl_global_destroy(self->global);
wl_list_remove(&self->resource_list);
Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ wayland_req = '>= 1.18.0'
wayland_protocols_req = '>= 1.24'
wlroots_req = ['>= 0.15', '< 0.16']
zen_protocols_req = '0.1.0'
zen_remote_server_req = '0.1.1'
zen_remote_server_req = '0.2.0'


glew_proj = subproject(
Expand Down
23 changes: 23 additions & 0 deletions zen/include/zen/xr-system.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,35 @@
extern "C" {
#endif

struct zn_xr_system;

struct zn_xr_system_interface {
void (*connect)(struct zn_xr_system *self);
};

enum zn_xr_system_session_status {
ZN_XR_SYSTEM_SESSION_STATUS_NOT_CONNECTED, // initial status
ZN_XR_SYSTEM_SESSION_STATUS_CONNECTED,
};

struct zn_xr_system {
void *impl_data; // @outlive
const struct zn_xr_system_interface *impl; // @nonnull, @outlive

enum zn_xr_system_session_status status;

struct {
struct wl_signal session_status_changed; // (NULL)
struct wl_signal destroy;
} events;
};

inline void
zn_xr_system_connect(struct zn_xr_system *self)
{
self->impl->connect(self);
}

#ifdef __cplusplus
}
#endif
46 changes: 46 additions & 0 deletions zen/src/backend/immersive/remote/log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#include <zen-common/log.h>
#include <zen-remote/logger.h>

#include <cstdio>

namespace zen::backend::immersive::remote {

class LogSink : public zen::remote::ILogSink
{
void Sink(zen::remote::Severity severity, const char * /*pretty_function*/,
const char *file, int line, const char *format, va_list vp) override
{
zn_log_importance_t importance = ZEN_SILENT;
switch (severity) {
case zen::remote::Severity::DEBUG:
importance = ZEN_DEBUG;
break;
case zen::remote::Severity::INFO:
importance = ZEN_INFO;
break;
case zen::remote::Severity::WARN:
importance = ZEN_WARN;
break;
case zen::remote::Severity::ERROR:
importance = ZEN_ERROR;
break;
case zen::remote::Severity::FATAL:
importance = ZEN_ERROR;
break;
default:
break;
}

// NOLINTNEXTLINE(cert-err33-c)
std::snprintf(
buffer_.data(), buffer_.size(), "[znr] [%s:%d] %s", file, line, format);

zn_vlog_(importance, buffer_.data(), vp);
}

std::array<char, 1024> buffer_;
};

} // namespace zen::backend::immersive::remote
2 changes: 2 additions & 0 deletions zen/src/backend/immersive/remote/pch/pch.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#pragma once

#include <wayland-server-core.h>
#include <zen-remote/logger.h>
#include <zen-remote/server/peer-manager.h>
#include <zen-remote/server/session.h>

#include <memory>
#include <vector>
38 changes: 30 additions & 8 deletions zen/src/backend/immersive/remote/xr-system-manager.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "xr-system-manager.h"

#include "log.h"
#include "loop.h"
#include "xr-system.h"
#include "zen-common/log.h"
Expand All @@ -11,13 +12,15 @@ namespace zen::backend::immersive::remote {

XrSystemManager::XrSystemManager(wl_display *display) : display_(display) {}

static const struct zn_xr_system_manager_interface implementation = {
const zn_xr_system_manager_interface XrSystemManager::c_implementation_ = {
XrSystemManager::HandleDestroy,
};

bool
XrSystemManager::Init()
{
zen::remote::InitializeLogger(std::make_unique<LogSink>());

auto loop = std::make_unique<Loop>(wl_display_get_event_loop(display_));

peer_manager_ = zen::remote::server::CreatePeerManager(std::move(loop));
Expand All @@ -27,7 +30,7 @@ XrSystemManager::Init()
}

c_obj_.impl_data = this;
c_obj_.impl = &implementation;
c_obj_.impl = &c_implementation_;
wl_signal_init(&c_obj_.events.new_system);

peer_manager_->on_peer_discover.Connect(
Expand All @@ -46,11 +49,11 @@ XrSystemManager::HandleDestroy(zn_xr_system_manager *c_obj)
}

void
XrSystemManager::RemoveXrSystem(uint64_t peer_id)
XrSystemManager::RemoveDeadXrSystem()
{
auto result = std::remove_if(xr_systems_.begin(), xr_systems_.end(),
[peer_id](std::unique_ptr<XrSystem> &xr_system) {
return xr_system->peer_id() == peer_id;
[](std::unique_ptr<XrSystem> &xr_system) {
return !xr_system->is_alive();
});

xr_systems_.erase(result, xr_systems_.end());
Expand All @@ -59,9 +62,20 @@ XrSystemManager::RemoveXrSystem(uint64_t peer_id)
void
XrSystemManager::HandleRemotePeerDiscover(uint64_t peer_id)
{
RemoveXrSystem(peer_id);
std::for_each(xr_systems_.begin(), xr_systems_.end(),
[peer_id](std::unique_ptr<XrSystem> &xr_system) {
if (xr_system->peer_id() == peer_id) {
xr_system->set_unavailable();
}
});
RemoveDeadXrSystem();

auto peer = peer_manager_->Get(peer_id);

auto xr_system = XrSystem::New(peer_id);
zn_debug(
"Discover a new remote peer %ld (%s)", peer_id, peer->host().c_str());

auto xr_system = XrSystem::New(peer, display_, this);

auto *xr_system_c_obj = xr_system->c_obj();

Expand All @@ -73,7 +87,15 @@ XrSystemManager::HandleRemotePeerDiscover(uint64_t peer_id)
void
XrSystemManager::HandleRemotePeerLost(uint64_t peer_id)
{
RemoveXrSystem(peer_id);
std::for_each(xr_systems_.begin(), xr_systems_.end(),
[peer_id](std::unique_ptr<XrSystem> &xr_system) {
if (xr_system->peer_id() == peer_id) {
xr_system->set_unavailable();
}
});
RemoveDeadXrSystem();

zn_debug("Lost a remote peer %ld", peer_id);
}

} // namespace zen::backend::immersive::remote
Expand Down
8 changes: 5 additions & 3 deletions zen/src/backend/immersive/remote/xr-system-manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class XrSystemManager
DISABLE_MOVE_AND_COPY(XrSystemManager);
~XrSystemManager() = default;

static void HandleDestroy(zn_xr_system_manager *c_obj);
void RemoveDeadXrSystem();

private:
friend struct zn_xr_system_manager * ::zn_xr_system_manager_create_remote(
Expand All @@ -23,12 +23,14 @@ class XrSystemManager

bool Init();

void RemoveXrSystem(uint64_t peer_id);

void HandleRemotePeerDiscover(uint64_t peer_id);

void HandleRemotePeerLost(uint64_t peer_id);

static void HandleDestroy(zn_xr_system_manager *c_obj);

static const zn_xr_system_manager_interface c_implementation_;

zn_xr_system_manager c_obj_{};

wl_display *display_; // @nonnull, @outlive
Expand Down
Loading

0 comments on commit 5531da2

Please sign in to comment.