Hyprland/src/protocols/Screencopy.hpp
Vaxry 1ed1ce9506
internal: new shared_ptr and weak_ptr implementation (#5883)
moves std::shared_ptrs to a new implementation

Advantages:
- you can dereference a weak_ptr directly. This will obviously segfault on a nullptr deref if it's expired.
   - this is useful to avoid the .lock() hell where we are 100% sure the pointer _should_ be valid. (and if it isn't, it should throw.)
- weak_ptrs are still valid while the SP is being destroyed.
   - reasoning: while an object (e.g. CWindow) is being destroyed, its `weak_ptr self` should be accessible (the sp is still alive, and so is CWindow), but it's not because by stl it's already expired (to prevent resurrection)
   - this impl solves it differently. w_p is expired, but can still be dereferenced and used. Creating `s_p`s is not possible anymore, though.
   - this is useful in destructors and callbacks.
2024-05-05 17:16:00 +01:00

99 lines
No EOL
3 KiB
C++

#pragma once
#include "../defines.hpp"
#include "wlr-screencopy-unstable-v1-protocol.h"
#include <list>
#include <vector>
#include "../managers/HookSystemManager.hpp"
#include "../helpers/Timer.hpp"
class CMonitor;
enum eClientOwners {
CLIENT_SCREENCOPY = 0,
CLIENT_TOPLEVEL_EXPORT
};
class CScreencopyClient {
public:
CScreencopyClient();
~CScreencopyClient();
int ref = 0;
wl_resource* resource = nullptr;
eClientOwners clientOwner = CLIENT_SCREENCOPY;
int frameCounter = 0;
int framesInLastHalfSecond = 0;
CTimer lastMeasure;
CTimer lastFrame;
bool sentScreencast = false;
void onTick();
SP<HOOK_CALLBACK_FN> tickCallback;
bool operator==(const CScreencopyClient& other) const {
return resource == other.resource;
}
};
struct SScreencopyFrame {
wl_resource* resource = nullptr;
CScreencopyClient* client = nullptr;
uint32_t shmFormat = 0;
uint32_t dmabufFormat = 0;
CBox box = {};
int shmStride = 0;
bool overlayCursor = false;
bool withDamage = false;
wlr_buffer_cap bufferCap = WLR_BUFFER_CAP_SHM;
wlr_buffer* buffer = nullptr;
CMonitor* pMonitor = nullptr;
PHLWINDOWREF pWindow;
bool operator==(const SScreencopyFrame& other) const {
return resource == other.resource && client == other.client;
}
};
class CScreencopyProtocolManager {
public:
CScreencopyProtocolManager();
void bindManager(wl_client* client, void* data, uint32_t version, uint32_t id);
void removeClient(CScreencopyClient* client, bool force = false);
void removeFrame(SScreencopyFrame* frame, bool force = false);
void displayDestroy();
void captureOutput(wl_client* client, wl_resource* resource, uint32_t frame, int32_t overlay_cursor, wl_resource* output, CBox box = {0, 0, 0, 0});
void copyFrame(wl_client* client, wl_resource* resource, wl_resource* buffer);
void onOutputCommit(CMonitor* pMonitor, wlr_output_event_commit* e);
private:
wl_global* m_pGlobal = nullptr;
std::list<SScreencopyFrame> m_lFrames;
std::list<CScreencopyClient> m_lClients;
wl_listener m_liDisplayDestroy;
std::vector<SScreencopyFrame*> m_vFramesAwaitingWrite;
wlr_buffer* m_pLastMonitorBackBuffer = nullptr;
void shareAllFrames(CMonitor* pMonitor);
void shareFrame(SScreencopyFrame* frame);
void sendFrameDamage(SScreencopyFrame* frame);
bool copyFrameDmabuf(SScreencopyFrame* frame);
bool copyFrameShm(SScreencopyFrame* frame, timespec* now);
friend class CScreencopyClient;
};