|
| 1 | +#include "ImageCaptureSource.hpp" |
| 2 | +#include "core/Output.hpp" |
| 3 | +#include "../helpers/Monitor.hpp" |
| 4 | +#include "../desktop/Window.hpp" |
| 5 | +#include "ForeignToplevel.hpp" |
| 6 | + |
| 7 | +CImageCaptureSource::CImageCaptureSource(SP<CExtImageCaptureSourceV1> resource, PHLMONITOR pMonitor) : m_resource(resource) { |
| 8 | + m_listeners.destroy = m_monitor->m_events.disconnect.registerListener([this](std::any data) { PROTO::imageCaptureSource->destroyResource(this); }); |
| 9 | + m_resource->setData(this); |
| 10 | +} |
| 11 | + |
| 12 | +CImageCaptureSource::CImageCaptureSource(SP<CExtImageCaptureSourceV1> resource, PHLWINDOW pWindow) : m_resource(resource) { |
| 13 | + m_listeners.destroy = m_window->m_events.destroy.registerListener([this](std::any data) { PROTO::imageCaptureSource->destroyResource(this); }); |
| 14 | + m_resource->setData(this); |
| 15 | +} |
| 16 | + |
| 17 | +CImageCaptureSource::~CImageCaptureSource() { |
| 18 | + m_events.destroy.emit(); |
| 19 | +} |
| 20 | + |
| 21 | +COutputImageCaptureSourceProtocol::COutputImageCaptureSourceProtocol(const wl_interface* iface, const int& ver, const std::string& name) : IWaylandProtocol(iface, ver, name) { |
| 22 | + ; |
| 23 | +} |
| 24 | + |
| 25 | +void COutputImageCaptureSourceProtocol::bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) { |
| 26 | + const auto RESOURCE = PROTO::imageCaptureSource->m_outputManagers.emplace_back(makeShared<CExtOutputImageCaptureSourceManagerV1>(client, ver, id)); |
| 27 | + |
| 28 | + if UNLIKELY (!RESOURCE->resource()) { |
| 29 | + wl_client_post_no_memory(client); |
| 30 | + PROTO::imageCaptureSource->m_outputManagers.pop_back(); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + RESOURCE->setDestroy([](CExtOutputImageCaptureSourceManagerV1* pMgr) { PROTO::imageCaptureSource->destroyResource(pMgr); }); |
| 35 | + RESOURCE->setOnDestroy([](CExtOutputImageCaptureSourceManagerV1* pMgr) { PROTO::imageCaptureSource->destroyResource(pMgr); }); |
| 36 | + RESOURCE->setCreateSource([](CExtOutputImageCaptureSourceManagerV1* pMgr, uint32_t id, wl_resource* output) { |
| 37 | + PHLMONITOR pMonitor = CWLOutputResource::fromResource(output)->m_monitor.lock(); |
| 38 | + if (!pMonitor) { |
| 39 | + LOGM(LOG, "Client tried to create source from invalid output resource"); |
| 40 | + pMgr->error(-1, "invalid output resource"); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + auto PSOURCE = |
| 45 | + PROTO::imageCaptureSource->m_sources.emplace_back(makeShared<CImageCaptureSource>(makeShared<CExtImageCaptureSourceV1>(pMgr->client(), pMgr->version(), id), pMonitor)); |
| 46 | + PSOURCE->m_self = PSOURCE; |
| 47 | + |
| 48 | + LOGM(LOG, "New capture source for monitor: {}", pMonitor->m_name); |
| 49 | + }); |
| 50 | +} |
| 51 | + |
| 52 | +CToplevelImageCaptureSourceProtocol::CToplevelImageCaptureSourceProtocol(const wl_interface* iface, const int& ver, const std::string& name) : IWaylandProtocol(iface, ver, name) { |
| 53 | + ; |
| 54 | +} |
| 55 | + |
| 56 | +void CToplevelImageCaptureSourceProtocol::bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) { |
| 57 | + const auto RESOURCE = PROTO::imageCaptureSource->m_toplevelManagers.emplace_back(makeShared<CExtForeignToplevelImageCaptureSourceManagerV1>(client, ver, id)); |
| 58 | + |
| 59 | + if UNLIKELY (!RESOURCE->resource()) { |
| 60 | + RESOURCE->noMemory(); |
| 61 | + PROTO::imageCaptureSource->m_toplevelManagers.pop_back(); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + RESOURCE->setDestroy([](CExtForeignToplevelImageCaptureSourceManagerV1* pMgr) { PROTO::imageCaptureSource->destroyResource(pMgr); }); |
| 66 | + RESOURCE->setOnDestroy([](CExtForeignToplevelImageCaptureSourceManagerV1* pMgr) { PROTO::imageCaptureSource->destroyResource(pMgr); }); |
| 67 | + RESOURCE->setCreateSource([](CExtForeignToplevelImageCaptureSourceManagerV1* pMgr, uint32_t id, wl_resource* handle) { |
| 68 | + PHLWINDOW pWindow = PROTO::foreignToplevel->windowFromHandleResource(handle); |
| 69 | + if (!pWindow) { |
| 70 | + LOGM(LOG, "Client tried to create source from invalid foreign toplevel handle resource"); |
| 71 | + pMgr->error(-1, "invalid foreign toplevel resource"); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + auto PSOURCE = |
| 76 | + PROTO::imageCaptureSource->m_sources.emplace_back(makeShared<CImageCaptureSource>(makeShared<CExtImageCaptureSourceV1>(pMgr->client(), pMgr->version(), id), pWindow)); |
| 77 | + PSOURCE->m_self = PSOURCE; |
| 78 | + |
| 79 | + LOGM(LOG, "New capture source for foreign toplevel: {}", pWindow->m_title); |
| 80 | + }); |
| 81 | +} |
| 82 | + |
| 83 | +CImageCaptureSourceProtocol::CImageCaptureSourceProtocol() { |
| 84 | + m_output = makeUnique<COutputImageCaptureSourceProtocol>(&ext_output_image_capture_source_manager_v1_interface, 1, "OutputImageCaptureSource"); |
| 85 | + m_toplevel = makeUnique<CToplevelImageCaptureSourceProtocol>(&ext_foreign_toplevel_image_capture_source_manager_v1_interface, 1, "ForeignToplevelImageCaptureSource"); |
| 86 | +} |
| 87 | + |
| 88 | +SP<CImageCaptureSource> CImageCaptureSourceProtocol::sourceFromResource(wl_resource* res) { |
| 89 | + auto data = sc<CImageCaptureSource*>(sc<CExtImageCaptureSourceV1*>(wl_resource_get_user_data(res))->data()); |
| 90 | + return data && data->m_self ? data->m_self.lock() : nullptr; |
| 91 | +} |
| 92 | + |
| 93 | +void CImageCaptureSourceProtocol::destroyResource(CExtOutputImageCaptureSourceManagerV1* resource) { |
| 94 | + std::erase_if(m_outputManagers, [&](const auto& other) { return other.get() == resource; }); |
| 95 | +} |
| 96 | +void CImageCaptureSourceProtocol::destroyResource(CExtForeignToplevelImageCaptureSourceManagerV1* resource) { |
| 97 | + std::erase_if(m_toplevelManagers, [&](const auto& other) { return other.get() == resource; }); |
| 98 | +} |
| 99 | +void CImageCaptureSourceProtocol::destroyResource(CImageCaptureSource* resource) { |
| 100 | + std::erase_if(m_sources, [&](const auto& other) { return other.get() == resource; }); |
| 101 | +} |
0 commit comments