Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

DRAFT: core: more hyprutils smart pointer usage and safe reference to widgets #686

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pkg_check_modules(
pangocairo
libdrm
gbm
hyprutils>=0.3.3
hyprutils>=0.5.0
sdbus-c++>=2.0.0
hyprgraphics)

Expand Down
13 changes: 4 additions & 9 deletions src/auth/Auth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
CAuth::CAuth() {
static const auto ENABLEPAM = g_pConfigManager->getValue<Hyprlang::INT>("auth:pam:enabled");
if (*ENABLEPAM)
m_vImpls.push_back(std::make_shared<CPam>());
m_vImpls.emplace_back(makeShared<CPam>());
static const auto ENABLEFINGERPRINT = g_pConfigManager->getValue<Hyprlang::INT>("auth:fingerprint:enabled");
if (*ENABLEFINGERPRINT)
m_vImpls.push_back(std::make_shared<CFingerprint>());
m_vImpls.emplace_back(makeShared<CFingerprint>());

RASSERT(!m_vImpls.empty(), "At least one authentication method must be enabled!");
}
Expand All @@ -32,12 +32,7 @@ void CAuth::submitInput(const std::string& input) {
}

bool CAuth::checkWaiting() {
for (const auto& i : m_vImpls) {
if (i->checkWaiting())
return true;
}

return false;
return std::ranges::any_of(m_vImpls, [](const auto& i) { return i->checkWaiting(); });
}

const std::string& CAuth::getCurrentFailText() {
Expand All @@ -64,7 +59,7 @@ size_t CAuth::getFailedAttempts() {
return m_sCurrentFail.failedAttempts;
}

std::shared_ptr<IAuthImplementation> CAuth::getImpl(eAuthImplementations implType) {
SP<IAuthImplementation> CAuth::getImpl(eAuthImplementations implType) {
for (const auto& i : m_vImpls) {
if (i->getImplType() == implType)
return i;
Expand Down
29 changes: 15 additions & 14 deletions src/auth/Auth.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#pragma once

#include <memory>
#include <optional>
#include <vector>

#include "../defines.hpp"

enum eAuthImplementations {
AUTH_IMPL_PAM = 0,
AUTH_IMPL_FINGERPRINT = 1,
Expand All @@ -28,23 +29,23 @@ class CAuth {
public:
CAuth();

void start();
void start();

void submitInput(const std::string& input);
bool checkWaiting();
void submitInput(const std::string& input);
bool checkWaiting();

const std::string& getCurrentFailText();
const std::string& getCurrentFailText();

std::optional<std::string> getFailText(eAuthImplementations implType);
std::optional<std::string> getPrompt(eAuthImplementations implType);
size_t getFailedAttempts();
std::optional<std::string> getFailText(eAuthImplementations implType);
std::optional<std::string> getPrompt(eAuthImplementations implType);
size_t getFailedAttempts();

std::shared_ptr<IAuthImplementation> getImpl(eAuthImplementations implType);
SP<IAuthImplementation> getImpl(eAuthImplementations implType);

void terminate();
void terminate();

void enqueueUnlock();
void enqueueFail(const std::string& failText, eAuthImplementations implType);
void enqueueUnlock();
void enqueueFail(const std::string& failText, eAuthImplementations implType);

// Should only be set via the main thread
bool m_bDisplayFailText = false;
Expand All @@ -56,7 +57,7 @@ class CAuth {
size_t failedAttempts = 0;
} m_sCurrentFail;

std::vector<std::shared_ptr<IAuthImplementation>> m_vImpls;
std::vector<SP<IAuthImplementation>> m_vImpls;
};

inline std::unique_ptr<CAuth> g_pAuth;
inline UP<CAuth> g_pAuth;
2 changes: 1 addition & 1 deletion src/config/ConfigDataValues.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ICustomConfigValueData {

class CLayoutValueData : public ICustomConfigValueData {
public:
CLayoutValueData() {};
CLayoutValueData() = default;
virtual ~CLayoutValueData() {};

virtual eConfigValueDataTypes getDataType() {
Expand Down
3 changes: 1 addition & 2 deletions src/config/ConfigManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <hyprlang.hpp>
#include <optional>
#include <vector>
#include <memory>
#include <unordered_map>

#include "../defines.hpp"
Expand Down Expand Up @@ -41,4 +40,4 @@ class CConfigManager {
Hyprlang::CConfig m_config;
};

inline std::unique_ptr<CConfigManager> g_pConfigManager;
inline UP<CConfigManager> g_pConfigManager;
3 changes: 1 addition & 2 deletions src/core/AnimationManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <hyprutils/animation/AnimatedVariable.hpp>

#include "../helpers/AnimatedVariable.hpp"
#include "../helpers/Math.hpp"
#include "../defines.hpp"

class CHyprlockAnimationManager : public Hyprutils::Animation::CAnimationManager {
Expand All @@ -31,4 +30,4 @@ class CHyprlockAnimationManager : public Hyprutils::Animation::CAnimationManager
bool m_bTickScheduled = false;
};

inline std::unique_ptr<CHyprlockAnimationManager> g_pAnimationManager;
inline UP<CHyprlockAnimationManager> g_pAnimationManager;
6 changes: 3 additions & 3 deletions src/core/Egl.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#pragma once

#include <memory>

#include <EGL/egl.h>
#include <EGL/eglext.h>

#include "../defines.hpp"

class CEGL {
public:
CEGL(wl_display*);
Expand All @@ -19,4 +19,4 @@ class CEGL {
void makeCurrent(EGLSurface surf);
};

inline std::unique_ptr<CEGL> g_pEGL;
inline UP<CEGL> g_pEGL;
25 changes: 13 additions & 12 deletions src/core/LockSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@ CSessionLockSurface::~CSessionLockSurface() {
wl_egl_window_destroy(eglWindow);
}

CSessionLockSurface::CSessionLockSurface(COutput* output) : output(output) {
CSessionLockSurface::CSessionLockSurface(const SP<COutput>& pOutput) : m_outputRef(pOutput) {
surface = makeShared<CCWlSurface>(g_pHyprlock->getCompositor()->sendCreateSurface());

if (!surface) {
Debug::log(CRIT, "Couldn't create wl_surface");
exit(1);
}
RASSERT(surface, "Couldn't create wl_surface");

static const auto FRACTIONALSCALING = g_pConfigManager->getValue<Hyprlang::INT>("general:fractional_scaling");
const auto ENABLE_FSV1 = *FRACTIONALSCALING == 1 || /* auto enable */ (*FRACTIONALSCALING == 2);
Expand Down Expand Up @@ -45,7 +41,7 @@ CSessionLockSurface::CSessionLockSurface(COutput* output) : output(output) {
if (!PVIEWPORTER)
Debug::log(LOG, "No viewporter support! Oops, won't be able to scale!");

lockSurface = makeShared<CCExtSessionLockSurfaceV1>(g_pHyprlock->getSessionLock()->sendGetLockSurface(surface->resource(), output->output->resource()));
lockSurface = makeShared<CCExtSessionLockSurfaceV1>(g_pHyprlock->getSessionLock()->sendGetLockSurface(surface->resource(), pOutput->m_wlOutput->resource()));

if (!lockSurface) {
Debug::log(CRIT, "Couldn't create ext_session_lock_surface_v1");
Expand All @@ -62,6 +58,8 @@ void CSessionLockSurface::configure(const Vector2D& size_, uint32_t serial_) {
const bool SAMESIZE = logicalSize == size_;
const bool SAMESCALE = appliedScale == fractionalScale;

const auto POUTPUT = m_outputRef.lock();

serial = serial_;
logicalSize = size_;
appliedScale = fractionalScale;
Expand All @@ -71,8 +69,8 @@ void CSessionLockSurface::configure(const Vector2D& size_, uint32_t serial_) {
viewport->sendSetDestination(logicalSize.x, logicalSize.y);
surface->sendSetBufferScale(1);
} else {
size = size_ * output->scale;
surface->sendSetBufferScale(output->scale);
size = size_ * POUTPUT->scale;
surface->sendSetBufferScale(POUTPUT->scale);
}

if (!SAMESERIAL)
Expand Down Expand Up @@ -103,8 +101,8 @@ void CSessionLockSurface::configure(const Vector2D& size_, uint32_t serial_) {
}

if (readyForFrame && !(SAMESIZE && SAMESCALE)) {
g_pRenderer->removeWidgetsFor(this);
Debug::log(LOG, "Reloading widgets");
Debug::log(LOG, "output {} changed, reloading widgets!", POUTPUT->stringPort);
g_pRenderer->reconfigureWidgetsFor(this);
}

readyForFrame = true;
Expand All @@ -129,7 +127,10 @@ void CSessionLockSurface::render() {
if (g_pHyprlock->m_bTerminate)
return;

Debug::log(TRACE, "[{}] frame {}, Current fps: {:.2f}", output->stringPort, m_frames, 1000.f / (frameTime - m_lastFrameTime));
if (Debug::verbose) {
const auto POUTPUT = m_outputRef.lock();
Debug::log(TRACE, "[{}] frame {}, Current fps: {:.2f}", POUTPUT->stringPort, m_frames, 1000.f / (frameTime - m_lastFrameTime));
}

m_lastFrameTime = frameTime;

Expand Down
8 changes: 5 additions & 3 deletions src/core/LockSurface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CRenderer;

class CSessionLockSurface {
public:
CSessionLockSurface(COutput* output);
CSessionLockSurface(const SP<COutput>& pOutput);
~CSessionLockSurface();

void configure(const Vector2D& size, uint32_t serial);
Expand All @@ -28,7 +28,8 @@ class CSessionLockSurface {
void onScaleUpdate();

private:
COutput* output = nullptr;
WP<COutput> m_outputRef;

SP<CCWlSurface> surface = nullptr;
SP<CCExtSessionLockSurfaceV1> lockSurface = nullptr;
uint32_t serial = 0;
Expand All @@ -49,4 +50,5 @@ class CSessionLockSurface {
SP<CCWlCallback> frameCallback = nullptr;

friend class CRenderer;
};
friend class COutput;
};
31 changes: 21 additions & 10 deletions src/core/Output.cpp
Original file line number Diff line number Diff line change
@@ -1,43 +1,54 @@
#include "Output.hpp"
#include "../helpers/Log.hpp"
#include "hyprlock.hpp"
#include "../renderer/Renderer.hpp"

COutput::COutput(SP<CCWlOutput> output_, uint32_t name_) : name(name_), output(output_) {
output->setDescription([this](CCWlOutput* r, const char* description) {
void COutput::create(WP<COutput> pSelf, SP<CCWlOutput> pWlOutput, uint32_t _name) {
name = _name;
m_wlOutput = pWlOutput;
m_self = pSelf;

m_wlOutput->setDescription([this](CCWlOutput* r, const char* description) {
stringDesc = description ? std::string{description} : "";
Debug::log(LOG, "output {} description {}", name, stringDesc);
});

output->setName([this](CCWlOutput* r, const char* name) {
m_wlOutput->setName([this](CCWlOutput* r, const char* name) {
stringName = std::string{name} + stringName;
stringPort = std::string{name};
Debug::log(LOG, "output {} name {}", name, name);
});

output->setScale([this](CCWlOutput* r, int32_t sc) { scale = sc; });
m_wlOutput->setScale([this](CCWlOutput* r, int32_t sc) { scale = sc; });

output->setDone([this](CCWlOutput* r) {
m_wlOutput->setDone([this](CCWlOutput* r) {
Debug::log(LOG, "output {} done", name);
if (g_pHyprlock->m_bLocked && !sessionLockSurface) {
if (g_pHyprlock->m_bLocked && !m_sessionLockSurface) {
// if we are already locked, create a surface dynamically
Debug::log(LOG, "Creating a surface dynamically for output as we are already locked");
sessionLockSurface = std::make_unique<CSessionLockSurface>(this);
createSessionLockSurface();
}
});

output->setMode([this](CCWlOutput* r, uint32_t flags, int32_t width, int32_t height, int32_t refresh) {
m_wlOutput->setMode([this](CCWlOutput* r, uint32_t flags, int32_t width, int32_t height, int32_t refresh) {
// handle portrait mode and flipped cases
if (transform % 2 == 1)
size = {height, width};
else
size = {width, height};
});

output->setGeometry(
m_wlOutput->setGeometry(
[this](CCWlOutput* r, int32_t x, int32_t y, int32_t physical_width, int32_t physical_height, int32_t subpixel, const char* make, const char* model, int32_t transform_) {
transform = (wl_output_transform)transform_;

Debug::log(LOG, "output {} make {} model {}", name, make ? make : "", model ? model : "");
});
}

void COutput::createSessionLockSurface() {
m_sessionLockSurface = makeUnique<CSessionLockSurface>(m_self.lock());
}

Vector2D COutput::getViewport() const {
return (m_sessionLockSurface) ? m_sessionLockSurface->size : size;
}
33 changes: 20 additions & 13 deletions src/core/Output.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,33 @@

#include "../defines.hpp"
#include "wayland.hpp"
#include "../helpers/Math.hpp"
#include "LockSurface.hpp"
#include <memory>

class COutput {
public:
COutput(SP<CCWlOutput> output, uint32_t name);
COutput() = default;
~COutput() = default;

uint32_t name = 0;
bool focused = false;
wl_output_transform transform = WL_OUTPUT_TRANSFORM_NORMAL;
Vector2D size;
int scale = 1;
std::string stringName = "";
std::string stringPort = "";
std::string stringDesc = "";
void create(WP<COutput> pSelf, SP<CCWlOutput> pWlOutput, uint32_t name);

std::unique_ptr<CSessionLockSurface> sessionLockSurface;
uint32_t name = 0;
bool focused = false;
wl_output_transform transform = WL_OUTPUT_TRANSFORM_NORMAL;
Vector2D size;
int scale = 1;
std::string stringName = "";
std::string stringPort = "";
std::string stringDesc = "";

SP<CCWlOutput> output = nullptr;
UP<CSessionLockSurface> m_sessionLockSurface;

SP<CCWlOutput> m_wlOutput = nullptr;

WP<COutput> m_self;

void createSessionLockSurface();

Vector2D getViewport() const;

private:
};
2 changes: 1 addition & 1 deletion src/core/Seat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void CSeatManager::registerSeat(SP<CCWlSeat> seat) {
}

void CSeatManager::registerCursorShape(SP<CCWpCursorShapeManagerV1> shape) {
m_pCursorShape = std::make_unique<CCursorShape>(shape);
m_pCursorShape = makeUnique<CCursorShape>(shape);
}

bool CSeatManager::registered() {
Expand Down
Loading