Skip to content

Commit 21d91f7

Browse files
committed
cleanup
Signed-off-by: Michael Pollind <mpollind@gmail.com>
1 parent 0473315 commit 21d91f7

11 files changed

+151
-123
lines changed

include/nbl/ui/CClipboardManagerXCB.h

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,36 @@
11
#ifndef _NBL_UI_C_CLIPBOARD_MANAGER_XCB_INCLUDED_
22
#define _NBL_UI_C_CLIPBOARD_MANAGER_XCB_INCLUDED_
33

4-
#include <string>
5-
#include <vector>
6-
#include <xcb/xproto.h>
74
#ifdef _NBL_PLATFORM_LINUX_
85

96
#include "nbl/core/decl/Types.h"
10-
#include "nbl/ui/IClipboardManager.h"
7+
#include "nbl/ui/IClipboardManagerXCB.h"
118
#include "nbl/ui/XCBConnection.h"
12-
139
namespace nbl::ui
1410
{
1511

12+
class IWindowXCB;
13+
class XCBConnection;
14+
1615
// details on XCB clipboard protocol: https://tronche.com/gui/x/icccm/sec-2.html#s-2
17-
class NBL_API2 CClipboardManagerXCB final : public IClipboardManager
16+
class NBL_API2 CClipboardManagerXCB final : public IClipboardManagerXCB
1817
{
19-
using base_t = IClipboardManager;
2018
public:
2119
inline CClipboardManagerXCB(core::smart_refctd_ptr<XCBConnection>&& connect):
22-
m_xcbConnection(std::move(connect)) {}
20+
IClipboardManagerXCB(),
21+
m_connection(std::move(connect)) {}
2322

2423
virtual std::string getClipboardText() override;
2524
virtual bool setClipboardText(const std::string_view& data) override;
2625

27-
void process(const IWindowXCB* window, xcb_generic_event_t* event);
26+
void process(const IWindowXCB* window, xcb_generic_event_t* event) override;
2827
private:
29-
core::smart_refctd_ptr<XCBConnection> m_xcbConnection;
30-
31-
struct {
32-
std::string m_data;
33-
std::vector<xcb_atom_t> m_formats;
34-
} m_stagedClipboard;
35-
28+
core::smart_refctd_ptr<XCBConnection> m_connection;
3629
std::mutex m_clipboardMutex;
3730
std::condition_variable m_clipboardResponseCV;
38-
std::string m_clipboardResponse;
31+
std::string m_clipboardResponse; // data sent to the clipboard by another application
32+
33+
std::string m_savedClipboard; // data saved to the clipboard for another application to read
3934

4035
XCBConnection::XCBAtomToken<core::StringLiteral("CLIPBOARD")> m_CLIPBOARD;
4136
XCBConnection::XCBAtomToken<core::StringLiteral("TARGETS")> m_TARGETS;

include/nbl/ui/CCursorControlXCB.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
#ifndef __NBL_SYSTEM_C_CURSOR_CONTROL_XCB_H_INCLUDED__
22
#define __NBL_SYSTEM_C_CURSOR_CONTROL_XCB_H_INCLUDED__
33

4+
#ifdef _NBL_PLATFORM_LINUX_
45

56
#include "nbl/ui/ICursorControl.h"
6-
#include "nbl/ui/XCBConnection.h"
7-
8-
#ifdef _NBL_PLATFORM_LINUX_
97

108
namespace nbl::ui
119
{
10+
11+
class XCBConnection;
1212
class NBL_API2 CCursorControlXCB final : public ICursorControl
1313
{
14-
core::smart_refctd_ptr<XCBConnection> m_xcbConnection;
15-
1614
public:
1715
inline CCursorControlXCB(
18-
core::smart_refctd_ptr<XCBConnection>&& xcbConnection) :
19-
m_xcbConnection(std::move(xcbConnection)) {}
16+
core::smart_refctd_ptr<XCBConnection>&& xcbConnection) :
17+
m_connection(std::move(xcbConnection)) {}
2018

2119
void setVisible(bool visible) override;
2220
bool isVisible() const override;
@@ -26,6 +24,8 @@ class NBL_API2 CCursorControlXCB final : public ICursorControl
2624

2725
SPosition getPosition() override;
2826
SRelativePosition getRelativePosition(IWindow* window) override;
27+
private:
28+
core::smart_refctd_ptr<XCBConnection> m_connection;
2929
};
3030
}
3131

include/nbl/ui/CWindowManagerXCB.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ class CWindowManagerXCB : public IWindowManager
8484
const XcbIcccm& getXcbIcccmFunctionTable() const { return m_xcbIcccm; }
8585

8686
private:
87-
8887
Xcb m_xcb = Xcb("xcb"); // function tables
8988
XcbIcccm m_xcbIcccm = XcbIcccm("xcb-icccm");
9089
};

include/nbl/ui/CWindowXCB.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
#ifndef __C_WINDOW_XCB_H_INCLUDED__
22
#define __C_WINDOW_XCB_H_INCLUDED__
33

4-
#include <cstdlib>
5-
64
#include "nbl/core/decl/smart_refctd_ptr.h"
7-
#include "nbl/ui/CClipboardManagerXCB.h"
5+
#include "nbl/ui/IClipboardManagerXCB.h"
86
#include "nbl/ui/IWindowXCB.h"
97
#include "nbl/ui/XCBConnection.h"
108

9+
#include <cstdlib>
10+
1111
namespace nbl::ui
1212
{
1313

14-
class CCursorControlXCB;
1514
class CWindowManagerXCB;
16-
class CClipboardManagerXCB;
15+
class XCBConnection;
16+
class CCursorControlXCB;
17+
class IClipboardManagerXCB;
1718

1819
class NBL_API2 CWindowXCB final : public IWindowXCB
1920
{
@@ -25,7 +26,7 @@ class NBL_API2 CWindowXCB final : public IWindowXCB
2526
// Display* getDisplay() const override { return m_dpy; }
2627
xcb_window_t getXcbWindow() const override { return m_xcbWindow; }
2728
xcb_connection_t* getXcbConnection() const override {
28-
return m_xcbConnection->getRawConnection();
29+
return m_connection->getRawConnection();
2930
}
3031

3132
virtual IClipboardManager* getClipboardManager() override;
@@ -44,9 +45,9 @@ class NBL_API2 CWindowXCB final : public IWindowXCB
4445
CWindowXCB(core::smart_refctd_ptr<system::ISystem>&& sys, uint32_t _w, uint32_t _h, E_CREATE_FLAGS _flags);
4546

4647
core::smart_refctd_ptr<CWindowManagerXCB> m_windowManager;
47-
core::smart_refctd_ptr<XCBConnection> m_xcbConnection;
48+
core::smart_refctd_ptr<XCBConnection> m_connection;
4849
core::smart_refctd_ptr<CCursorControlXCB> m_cursorControl;
49-
core::smart_refctd_ptr<CClipboardManagerXCB> m_clipboardManager;
50+
core::smart_refctd_ptr<IClipboardManagerXCB> m_clipboardManager;
5051

5152
class CDispatchThread final : public system::IThreadHandler<CDispatchThread>
5253
{

include/nbl/ui/IClipboardManagerXCB.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef _NBL_UI_I_CLIPBOARD_MANAGER_XCB_INCLUDED_
2+
#define _NBL_UI_I_CLIPBOARD_MANAGER_XCB_INCLUDED_
3+
4+
#ifdef _NBL_PLATFORM_LINUX_
5+
6+
#include "nbl/ui/IClipboardManager.h"
7+
8+
namespace nbl::ui
9+
{
10+
class XCBConnection;
11+
12+
// details on XCB clipboard protocol: https://tronche.com/gui/x/icccm/sec-2.html#s-2
13+
class NBL_API2 IClipboardManagerXCB : public IClipboardManager
14+
{
15+
public:
16+
IClipboardManagerXCB() : IClipboardManager() {}
17+
virtual ~IClipboardManagerXCB() = default;
18+
19+
virtual std::string getClipboardText() = 0;
20+
virtual bool setClipboardText(const std::string_view& data) = 0;
21+
virtual void process(const IWindowXCB* window, xcb_generic_event_t* event) = 0;
22+
};
23+
24+
}
25+
26+
#endif
27+
28+
#endif

include/nbl/ui/IWindowXCB.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#ifndef __NBL_I_WINDOW_XCB_H_INCLUDED__
22
#define __NBL_I_WINDOW_XCB_H_INCLUDED__
33

4+
#ifdef _NBL_PLATFORM_LINUX_
5+
46
#include "nbl/core/util/bitflag.h"
7+
58
#include "nbl/ui/IWindow.h"
69
#include "nbl/ui/XCBConnection.h"
7-
#include <xcb/xproto.h>
810

9-
#ifdef _NBL_PLATFORM_LINUX_
11+
#include <xcb/xproto.h>
1012

1113
namespace nbl::ui
1214
{
@@ -22,7 +24,6 @@ class NBL_API2 IWindowXCB : public IWindow
2224

2325
const void* getNativeHandle() const { return nullptr; }
2426
virtual xcb_window_t getXcbWindow() const = 0;
25-
// virtual xcb_window_t getXcbRootWindow() const = 0;
2627
virtual xcb_connection_t* getXcbConnection() const = 0;
2728

2829
virtual bool setWindowSize_impl(uint32_t width, uint32_t height) = 0;

include/nbl/ui/XCBConnection.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class XCBConnection : public core::IReferenceCounted {
2929
virtual ~XCBConnection() override;
3030

3131
template<core::StringLiteral Name>
32-
inline xcb_atom_t resolveXCBAtom(XCBAtomToken<Name>& token, bool only_if_exists = true, bool forced = false) const {
32+
inline xcb_atom_t resolveAtom(XCBAtomToken<Name>& token, bool only_if_exists = true, bool forced = false) const {
3333
const auto& xcb = m_windowManager->getXcbFunctionTable();
3434
if(token.fetched && !forced) {
3535
return token.token;

src/nbl/ui/CClipboardManagerXCB.cpp

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11

2+
#include "nbl/ui/CClipboardManagerXCB.h"
23
#include "nbl/ui/XCBConnection.h"
4+
35
#include <mutex>
46
#include <string>
57
#include <vector>
6-
#include <xcb/xproto.h>
7-
#ifdef _NBL_PLATFORM_LINUX_
88

9-
#include "nbl/ui/CClipboardManagerXCB.h"
9+
#include <xcb/xproto.h>
1010

1111
namespace nbl::ui
1212
{
@@ -24,23 +24,14 @@ namespace nbl::ui
2424

2525
bool CClipboardManagerXCB::setClipboardText(const std::string_view& data) {
2626
std::lock_guard lk(m_clipboardMutex);
27-
m_stagedClipboard.m_data = data;
28-
m_stagedClipboard.m_formats = {
29-
m_xcbConnection->resolveXCBAtom(m_formatUTF8_0),
30-
m_xcbConnection->resolveXCBAtom(m_formatUTF8_1),
31-
m_xcbConnection->resolveXCBAtom(m_formatUTF8_2),
32-
m_xcbConnection->resolveXCBAtom(m_formatGTK),
33-
m_xcbConnection->resolveXCBAtom(m_formatString),
34-
m_xcbConnection->resolveXCBAtom(m_formatText),
35-
m_xcbConnection->resolveXCBAtom(m_formatTextPlain)
36-
};
27+
m_savedClipboard = data;
3728
return true;
3829
}
3930

4031
void CClipboardManagerXCB::process(const IWindowXCB* window, xcb_generic_event_t* event) {
41-
const auto& xcb = m_xcbConnection->getXcbFunctionTable();
32+
const auto& xcb = m_connection->getXcbFunctionTable();
4233

43-
auto TARGETS = m_xcbConnection->resolveXCBAtom(m_TARGETS);
34+
auto TARGETS = m_connection->resolveAtom(m_TARGETS);
4435

4536
switch(event->response_type & ~0x80) {
4637
// XCB_ATOM
@@ -52,13 +43,21 @@ namespace nbl::ui
5243
std::vector<xcb_atom_t> targets;
5344
{
5445
std::lock_guard lk(m_clipboardMutex);
55-
for(auto& format : m_stagedClipboard.m_formats) {
46+
for(auto& format : {
47+
m_connection->resolveAtom(m_formatUTF8_0),
48+
m_connection->resolveAtom(m_formatUTF8_1),
49+
m_connection->resolveAtom(m_formatUTF8_2),
50+
m_connection->resolveAtom(m_formatGTK),
51+
m_connection->resolveAtom(m_formatString),
52+
m_connection->resolveAtom(m_formatText),
53+
m_connection->resolveAtom(m_formatTextPlain)
54+
}) {
5655
targets.push_back(format);
5756
}
5857
}
59-
targets.push_back(m_xcbConnection->resolveXCBAtom(m_TARGETS));
58+
targets.push_back(m_connection->resolveAtom(m_TARGETS));
6059
xcb.pxcb_change_property(
61-
m_xcbConnection->getRawConnection(),
60+
m_connection->getRawConnection(),
6261
XCB_PROP_MODE_REPLACE,
6362
sne->requestor,
6463
sne->property,
@@ -69,14 +68,14 @@ namespace nbl::ui
6968
} else {
7069
std::lock_guard lk(m_clipboardMutex);
7170
xcb.pxcb_change_property(
72-
m_xcbConnection->getRawConnection(),
71+
m_connection->getRawConnection(),
7372
XCB_PROP_MODE_REPLACE,
7473
sne->requestor,
7574
sne->property,
7675
sne->target,
7776
8,
78-
m_stagedClipboard.m_data.size(),
79-
m_stagedClipboard.m_data.data());
77+
m_savedClipboard.size(),
78+
m_savedClipboard.data());
8079
}
8180
}
8281

@@ -91,22 +90,21 @@ namespace nbl::ui
9190
notify.target = sne->target;
9291
notify.property = sne->property;
9392

94-
xcb.pxcb_send_event(m_xcbConnection->getRawConnection(), false,
93+
xcb.pxcb_send_event(m_connection->getRawConnection(), false,
9594
sne->requestor,
9695
XCB_EVENT_MASK_NO_EVENT, // SelectionNotify events go without mask
9796
(const char*)&notify);
9897

99-
xcb.pxcb_flush(m_xcbConnection->getRawConnection());
98+
xcb.pxcb_flush(m_connection->getRawConnection());
10099
break;
101100
}
102101
// Someone else has new content in the clipboard, so is
103102
// notifying us that we should delete our data now.
104103
case XCB_SELECTION_CLEAR: {
105104
auto* sne = reinterpret_cast<xcb_selection_clear_event_t*>(event);
106-
if (sne->selection == m_xcbConnection->resolveXCBAtom(m_CLIPBOARD)) {
105+
if (sne->selection == m_connection->resolveAtom(m_CLIPBOARD)) {
107106
std::lock_guard<std::mutex> lock(m_clipboardMutex);
108-
m_stagedClipboard.m_formats = {};
109-
m_stagedClipboard.m_data = std::string();
107+
m_savedClipboard = std::string();
110108
}
111109
break;
112110
}
@@ -119,17 +117,17 @@ namespace nbl::ui
119117
if(sne->target != TARGETS) {
120118
fieldType = sne->target;
121119
}
122-
xcb_get_property_cookie_t cookie = xcb.pxcb_get_property(m_xcbConnection->getRawConnection(), true,
120+
xcb_get_property_cookie_t cookie = xcb.pxcb_get_property(m_connection->getRawConnection(), true,
123121
sne->requestor,
124122
sne->property,
125123
fieldType, 0, 0x1fffffff); // 0x1fffffff = INT32_MAX / 4
126124
if(xcb_get_property_reply_t* reply =
127-
xcb.pxcb_get_property_reply(m_xcbConnection->getRawConnection(), cookie, nullptr)) {
125+
xcb.pxcb_get_property_reply(m_connection->getRawConnection(), cookie, nullptr)) {
128126
core::SRAIIBasedExiter exitReply([reply]() -> void {
129127
free(reply);
130128
});
131129

132-
if(reply->type == m_xcbConnection->resolveXCBAtom(m_INCR)) {
130+
if(reply->type == m_connection->resolveAtom(m_INCR)) {
133131
assert(false); // TODO
134132
} else {
135133
const auto* src = reinterpret_cast<const char*>(xcb.pxcb_get_property_value(reply));
@@ -146,6 +144,4 @@ namespace nbl::ui
146144
}
147145
}
148146
}
149-
}
150-
151-
#endif
147+
}

0 commit comments

Comments
 (0)