mirror of
https://github.com/hyprwm/Hyprland
synced 2024-11-08 07:46:00 +01:00
Screencopy: unify frame and client between impls + event
Adds a new event to both hooks and ipc: screencopy
This commit is contained in:
parent
12604b7676
commit
edad24c257
4 changed files with 90 additions and 63 deletions
|
@ -85,9 +85,9 @@ static const struct zwlr_screencopy_frame_v1_interface screencopyFrameImpl = {
|
|||
.copy_with_damage = handleCopyWithDamage,
|
||||
};
|
||||
|
||||
static SScreencopyClient* clientFromResource(wl_resource* resource) {
|
||||
static CScreencopyClient* clientFromResource(wl_resource* resource) {
|
||||
ASSERT(wl_resource_instance_of(resource, &zwlr_screencopy_manager_v1_interface, &screencopyMgrImpl));
|
||||
return (SScreencopyClient*)wl_resource_get_user_data(resource);
|
||||
return (CScreencopyClient*)wl_resource_get_user_data(resource);
|
||||
}
|
||||
|
||||
static SScreencopyFrame* frameFromResource(wl_resource* resource) {
|
||||
|
@ -95,7 +95,7 @@ static SScreencopyFrame* frameFromResource(wl_resource* resource) {
|
|||
return (SScreencopyFrame*)wl_resource_get_user_data(resource);
|
||||
}
|
||||
|
||||
void CScreencopyProtocolManager::removeClient(SScreencopyClient* client, bool force) {
|
||||
void CScreencopyProtocolManager::removeClient(CScreencopyClient* client, bool force) {
|
||||
if (!force) {
|
||||
if (!client || client->ref <= 0)
|
||||
return;
|
||||
|
@ -113,6 +113,36 @@ static void handleManagerResourceDestroy(wl_resource* resource) {
|
|||
g_pProtocolManager->m_pScreencopyProtocolManager->removeClient(PCLIENT, true);
|
||||
}
|
||||
|
||||
CScreencopyClient::~CScreencopyClient() {
|
||||
g_pHookSystem->unhook(tickCallback);
|
||||
}
|
||||
|
||||
CScreencopyClient::CScreencopyClient() {
|
||||
lastMeasure = std::chrono::system_clock::now();
|
||||
tickCallback = g_pHookSystem->hookDynamic("tick", [&](void* self, std::any data) { onTick(); });
|
||||
}
|
||||
|
||||
void CScreencopyClient::onTick() {
|
||||
const auto SINCELAST = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - lastMeasure).count() / 1000.0;
|
||||
|
||||
if (SINCELAST < 0.5)
|
||||
return;
|
||||
|
||||
framesInLastHalfSecond = frameCounter;
|
||||
frameCounter = 0;
|
||||
lastMeasure = std::chrono::system_clock::now();
|
||||
|
||||
if (framesInLastHalfSecond > 3 && !sentScreencast) {
|
||||
EMIT_HOOK_EVENT("screencast", (std::vector<uint64_t>{1, (uint64_t)framesInLastHalfSecond, (uint64_t)clientOwner}));
|
||||
g_pEventManager->postEvent(SHyprIPCEvent{"screencast", "1," + std::to_string(clientOwner)});
|
||||
sentScreencast = true;
|
||||
} else if (framesInLastHalfSecond < 4 && sentScreencast) {
|
||||
EMIT_HOOK_EVENT("screencast", (std::vector<uint64_t>{0, (uint64_t)framesInLastHalfSecond, (uint64_t)clientOwner}));
|
||||
g_pEventManager->postEvent(SHyprIPCEvent{"screencast", "0," + std::to_string(clientOwner)});
|
||||
sentScreencast = false;
|
||||
}
|
||||
}
|
||||
|
||||
void CScreencopyProtocolManager::bindManager(wl_client* client, void* data, uint32_t version, uint32_t id) {
|
||||
const auto PCLIENT = &m_lClients.emplace_back();
|
||||
|
||||
|
@ -320,6 +350,8 @@ void CScreencopyProtocolManager::shareAllFrames(CMonitor* pMonitor, bool dmabuf)
|
|||
|
||||
shareFrame(f);
|
||||
|
||||
++f->client->frameCounter;
|
||||
|
||||
framesToRemove.push_back(f);
|
||||
}
|
||||
|
||||
|
|
|
@ -5,21 +5,42 @@
|
|||
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include "../managers/HookSystemManager.hpp"
|
||||
|
||||
class CMonitor;
|
||||
|
||||
struct SScreencopyClient {
|
||||
enum eClientOwners
|
||||
{
|
||||
CLIENT_SCREENCOPY = 0,
|
||||
CLIENT_TOPLEVEL_EXPORT
|
||||
};
|
||||
|
||||
class CScreencopyClient {
|
||||
public:
|
||||
CScreencopyClient();
|
||||
~CScreencopyClient();
|
||||
|
||||
int ref = 0;
|
||||
wl_resource* resource = nullptr;
|
||||
|
||||
bool operator==(const SScreencopyClient& other) const {
|
||||
eClientOwners clientOwner = CLIENT_SCREENCOPY;
|
||||
|
||||
int frameCounter = 0;
|
||||
int framesInLastHalfSecond = 0;
|
||||
std::chrono::system_clock::time_point lastMeasure;
|
||||
bool sentScreencast = false;
|
||||
|
||||
void onTick();
|
||||
HOOK_CALLBACK_FN* tickCallback = nullptr;
|
||||
|
||||
bool operator==(const CScreencopyClient& other) const {
|
||||
return resource == other.resource;
|
||||
}
|
||||
};
|
||||
|
||||
struct SScreencopyFrame {
|
||||
wl_resource* resource = nullptr;
|
||||
SScreencopyClient* client = nullptr;
|
||||
CScreencopyClient* client = nullptr;
|
||||
|
||||
uint32_t shmFormat = 0;
|
||||
uint32_t dmabufFormat = 0;
|
||||
|
@ -34,6 +55,7 @@ struct SScreencopyFrame {
|
|||
wlr_buffer* buffer = nullptr;
|
||||
|
||||
CMonitor* pMonitor = nullptr;
|
||||
CWindow* pWindow = nullptr;
|
||||
|
||||
bool operator==(const SScreencopyFrame& other) const {
|
||||
return resource == other.resource && client == other.client;
|
||||
|
@ -45,7 +67,7 @@ class CScreencopyProtocolManager {
|
|||
CScreencopyProtocolManager();
|
||||
|
||||
void bindManager(wl_client* client, void* data, uint32_t version, uint32_t id);
|
||||
void removeClient(SScreencopyClient* client, bool force = false);
|
||||
void removeClient(CScreencopyClient* client, bool force = false);
|
||||
void removeFrame(SScreencopyFrame* frame, bool force = false);
|
||||
void displayDestroy();
|
||||
|
||||
|
@ -59,7 +81,7 @@ class CScreencopyProtocolManager {
|
|||
private:
|
||||
wl_global* m_pGlobal = nullptr;
|
||||
std::list<SScreencopyFrame> m_lFrames;
|
||||
std::list<SScreencopyClient> m_lClients;
|
||||
std::list<CScreencopyClient> m_lClients;
|
||||
|
||||
wl_listener m_liDisplayDestroy;
|
||||
|
||||
|
|
|
@ -75,17 +75,17 @@ static const struct hyprland_toplevel_export_manager_v1_interface toplevelExport
|
|||
|
||||
static const struct hyprland_toplevel_export_frame_v1_interface toplevelFrameImpl = {.copy = handleCopyFrame, .destroy = handleDestroyFrame};
|
||||
|
||||
static SToplevelClient* clientFromResource(wl_resource* resource) {
|
||||
static CScreencopyClient* clientFromResource(wl_resource* resource) {
|
||||
ASSERT(wl_resource_instance_of(resource, &hyprland_toplevel_export_manager_v1_interface, &toplevelExportManagerImpl));
|
||||
return (SToplevelClient*)wl_resource_get_user_data(resource);
|
||||
return (CScreencopyClient*)wl_resource_get_user_data(resource);
|
||||
}
|
||||
|
||||
static SToplevelFrame* frameFromResource(wl_resource* resource) {
|
||||
static SScreencopyFrame* frameFromResource(wl_resource* resource) {
|
||||
ASSERT(wl_resource_instance_of(resource, &hyprland_toplevel_export_frame_v1_interface, &toplevelFrameImpl));
|
||||
return (SToplevelFrame*)wl_resource_get_user_data(resource);
|
||||
return (SScreencopyFrame*)wl_resource_get_user_data(resource);
|
||||
}
|
||||
|
||||
void CToplevelExportProtocolManager::removeClient(SToplevelClient* client, bool force) {
|
||||
void CToplevelExportProtocolManager::removeClient(CScreencopyClient* client, bool force) {
|
||||
if (!force) {
|
||||
if (!client || client->ref <= 0)
|
||||
return;
|
||||
|
@ -106,6 +106,7 @@ static void handleManagerResourceDestroy(wl_resource* resource) {
|
|||
void CToplevelExportProtocolManager::bindManager(wl_client* client, void* data, uint32_t version, uint32_t id) {
|
||||
const auto PCLIENT = &m_lClients.emplace_back();
|
||||
|
||||
PCLIENT->clientOwner = CLIENT_TOPLEVEL_EXPORT;
|
||||
PCLIENT->resource = wl_resource_create(client, &hyprland_toplevel_export_manager_v1_interface, version, id);
|
||||
|
||||
if (!PCLIENT->resource) {
|
||||
|
@ -128,7 +129,7 @@ static void handleFrameResourceDestroy(wl_resource* resource) {
|
|||
g_pProtocolManager->m_pToplevelExportProtocolManager->removeFrame(PFRAME);
|
||||
}
|
||||
|
||||
void CToplevelExportProtocolManager::removeFrame(SToplevelFrame* frame, bool force) {
|
||||
void CToplevelExportProtocolManager::removeFrame(SScreencopyFrame* frame, bool force) {
|
||||
if (!frame)
|
||||
return;
|
||||
|
||||
|
@ -283,7 +284,7 @@ void CToplevelExportProtocolManager::onMonitorRender(CMonitor* pMonitor) {
|
|||
if (m_vFramesAwaitingWrite.empty())
|
||||
return; // nothing to share
|
||||
|
||||
std::vector<SToplevelFrame*> framesToRemove;
|
||||
std::vector<SScreencopyFrame*> framesToRemove;
|
||||
|
||||
// share frame if correct output
|
||||
for (auto& f : m_vFramesAwaitingWrite) {
|
||||
|
@ -299,6 +300,8 @@ void CToplevelExportProtocolManager::onMonitorRender(CMonitor* pMonitor) {
|
|||
|
||||
shareFrame(f);
|
||||
|
||||
++f->client->frameCounter;
|
||||
|
||||
framesToRemove.push_back(f);
|
||||
}
|
||||
|
||||
|
@ -307,7 +310,7 @@ void CToplevelExportProtocolManager::onMonitorRender(CMonitor* pMonitor) {
|
|||
}
|
||||
}
|
||||
|
||||
void CToplevelExportProtocolManager::shareFrame(SToplevelFrame* frame) {
|
||||
void CToplevelExportProtocolManager::shareFrame(SScreencopyFrame* frame) {
|
||||
if (!frame->buffer) {
|
||||
return;
|
||||
}
|
||||
|
@ -337,7 +340,7 @@ void CToplevelExportProtocolManager::shareFrame(SToplevelFrame* frame) {
|
|||
hyprland_toplevel_export_frame_v1_send_ready(frame->resource, tvSecHi, tvSecLo, now.tv_nsec);
|
||||
}
|
||||
|
||||
bool CToplevelExportProtocolManager::copyFrameShm(SToplevelFrame* frame, timespec* now) {
|
||||
bool CToplevelExportProtocolManager::copyFrameShm(SScreencopyFrame* frame, timespec* now) {
|
||||
void* data;
|
||||
uint32_t format;
|
||||
size_t stride;
|
||||
|
@ -423,7 +426,7 @@ bool CToplevelExportProtocolManager::copyFrameShm(SToplevelFrame* frame, timespe
|
|||
return true;
|
||||
}
|
||||
|
||||
bool CToplevelExportProtocolManager::copyFrameDmabuf(SToplevelFrame* frame) {
|
||||
bool CToplevelExportProtocolManager::copyFrameDmabuf(SScreencopyFrame* frame) {
|
||||
// todo
|
||||
Debug::log(ERR, "DMABUF copying not impl'd!");
|
||||
return false;
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include "../defines.hpp"
|
||||
#include "wlr-foreign-toplevel-management-unstable-v1-protocol.h"
|
||||
#include "hyprland-toplevel-export-v1-protocol.h"
|
||||
#include "Screencopy.hpp"
|
||||
|
||||
#include <list>
|
||||
#include <vector>
|
||||
|
@ -10,61 +11,30 @@
|
|||
class CMonitor;
|
||||
class CWindow;
|
||||
|
||||
struct SToplevelClient {
|
||||
int ref = 0;
|
||||
wl_resource* resource = nullptr;
|
||||
|
||||
bool operator==(const SToplevelClient& other) const {
|
||||
return resource == other.resource;
|
||||
}
|
||||
};
|
||||
|
||||
struct SToplevelFrame {
|
||||
wl_resource* resource = nullptr;
|
||||
SToplevelClient* client = nullptr;
|
||||
|
||||
uint32_t shmFormat = 0;
|
||||
uint32_t dmabufFormat = 0;
|
||||
wlr_box box = {0};
|
||||
int shmStride = 0;
|
||||
|
||||
bool overlayCursor = false;
|
||||
|
||||
wlr_buffer_cap bufferCap = WLR_BUFFER_CAP_SHM;
|
||||
|
||||
wlr_buffer* buffer = nullptr;
|
||||
|
||||
CWindow* pWindow = nullptr;
|
||||
|
||||
bool operator==(const SToplevelFrame& other) const {
|
||||
return resource == other.resource && client == other.client;
|
||||
}
|
||||
};
|
||||
|
||||
class CToplevelExportProtocolManager {
|
||||
public:
|
||||
CToplevelExportProtocolManager();
|
||||
|
||||
void bindManager(wl_client* client, void* data, uint32_t version, uint32_t id);
|
||||
void captureToplevel(wl_client* client, wl_resource* resource, uint32_t frame, int32_t overlay_cursor, CWindow* handle);
|
||||
void removeClient(SToplevelClient* client, bool force = false);
|
||||
void removeFrame(SToplevelFrame* frame, bool force = false);
|
||||
void removeClient(CScreencopyClient* client, bool force = false);
|
||||
void removeFrame(SScreencopyFrame* frame, bool force = false);
|
||||
void copyFrame(wl_client* client, wl_resource* resource, wl_resource* buffer, int32_t ignore_damage);
|
||||
void displayDestroy();
|
||||
void onWindowUnmap(CWindow* pWindow);
|
||||
|
||||
private:
|
||||
wl_global* m_pGlobal = nullptr;
|
||||
std::list<SToplevelFrame> m_lFrames;
|
||||
std::list<SToplevelClient> m_lClients;
|
||||
std::list<SScreencopyFrame> m_lFrames;
|
||||
std::list<CScreencopyClient> m_lClients;
|
||||
|
||||
wl_listener m_liDisplayDestroy;
|
||||
|
||||
std::vector<SToplevelFrame*> m_vFramesAwaitingWrite;
|
||||
std::vector<SScreencopyFrame*> m_vFramesAwaitingWrite;
|
||||
|
||||
void shareFrame(SToplevelFrame* frame);
|
||||
bool copyFrameDmabuf(SToplevelFrame* frame);
|
||||
bool copyFrameShm(SToplevelFrame* frame, timespec* now);
|
||||
void shareFrame(SScreencopyFrame* frame);
|
||||
bool copyFrameDmabuf(SScreencopyFrame* frame);
|
||||
bool copyFrameShm(SScreencopyFrame* frame, timespec* now);
|
||||
|
||||
void onMonitorRender(CMonitor* pMonitor);
|
||||
};
|
Loading…
Reference in a new issue