174 lines
5.3 KiB
C++
174 lines
5.3 KiB
C++
#include "ui/clipboard.hpp"
|
|||
|
|
|
||
|
|
#include <X11/Xatom.h>
|
||
|
|
#include <cstring>
|
||
|
|
#include <vector>
|
||
|
|
|
||
|
|
namespace htab::ui {
|
||
|
|
|
||
|
|
X11Clipboard::~X11Clipboard() {
|
||
|
|
if (dpy_) {
|
||
|
|
if (win_ != None) XDestroyWindow(dpy_, win_);
|
||
|
|
XCloseDisplay(dpy_);
|
||
|
|
dpy_ = nullptr;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void X11Clipboard::init(gemui_app_t* app) {
|
||
|
|
if (dpy_) return;
|
||
|
|
dpy_ = XOpenDisplay(nullptr);
|
||
|
|
if (!dpy_) return;
|
||
|
|
int screen = DefaultScreen(dpy_);
|
||
|
|
win_ = XCreateSimpleWindow(dpy_, RootWindow(dpy_, screen), -10, -10, 1, 1, 0, 0, 0);
|
||
|
|
XSelectInput(dpy_, win_, PropertyChangeMask);
|
||
|
|
atom_clipboard_ = XInternAtom(dpy_, "CLIPBOARD", False);
|
||
|
|
atom_targets_ = XInternAtom(dpy_, "TARGETS", False);
|
||
|
|
atom_utf8_ = XInternAtom(dpy_, "UTF8_STRING", False);
|
||
|
|
atom_text_ = XInternAtom(dpy_, "TEXT", False);
|
||
|
|
xfd_ = ConnectionNumber(dpy_);
|
||
|
|
if (app && xfd_ >= 0) {
|
||
|
|
gemui_add_fd(app, xfd_, &X11Clipboard::on_fd_ready, this);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void X11Clipboard::shutdown(gemui_app_t* app) {
|
||
|
|
if (app && xfd_ >= 0) {
|
||
|
|
gemui_del_fd(app, xfd_);
|
||
|
|
}
|
||
|
|
if (dpy_) {
|
||
|
|
if (win_ != None) {
|
||
|
|
XDestroyWindow(dpy_, win_);
|
||
|
|
win_ = None;
|
||
|
|
}
|
||
|
|
XCloseDisplay(dpy_);
|
||
|
|
dpy_ = nullptr;
|
||
|
|
}
|
||
|
|
xfd_ = -1;
|
||
|
|
}
|
||
|
|
|
||
|
|
void X11Clipboard::on_fd_ready(gemui_app_t*, int, void* ud) {
|
||
|
|
if (ud) static_cast<X11Clipboard*>(ud)->process_events();
|
||
|
|
}
|
||
|
|
|
||
|
|
void X11Clipboard::process_events() {
|
||
|
|
if (!dpy_) return;
|
||
|
|
while (XPending(dpy_) > 0) {
|
||
|
|
XEvent ev;
|
||
|
|
XNextEvent(dpy_, &ev);
|
||
|
|
if (ev.type == SelectionRequest) {
|
||
|
|
handle_selection_request(ev.xselectionrequest);
|
||
|
|
} else if (ev.type == SelectionClear) {
|
||
|
|
if (ev.xselectionclear.selection == atom_clipboard_) {
|
||
|
|
owns_clipboard_ = false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void X11Clipboard::handle_selection_request(const XSelectionRequestEvent& req) {
|
||
|
|
XSelectionEvent resp;
|
||
|
|
std::memset(&resp, 0, sizeof(resp));
|
||
|
|
resp.type = SelectionNotify;
|
||
|
|
resp.display = req.display;
|
||
|
|
resp.requestor = req.requestor;
|
||
|
|
resp.selection = req.selection;
|
||
|
|
resp.target = req.target;
|
||
|
|
resp.time = req.time;
|
||
|
|
resp.property = None;
|
||
|
|
|
||
|
|
if (req.property != None) {
|
||
|
|
if (req.target == atom_targets_) {
|
||
|
|
Atom targets[4] = {atom_targets_, atom_utf8_, XA_STRING, atom_text_};
|
||
|
|
XChangeProperty(dpy_, req.requestor, req.property, XA_ATOM, 32, PropModeReplace,
|
||
|
|
reinterpret_cast<const unsigned char*>(targets), 4);
|
||
|
|
resp.property = req.property;
|
||
|
|
} else if (req.target == atom_utf8_ || req.target == XA_STRING || req.target == atom_text_) {
|
||
|
|
Atom type = (req.target == XA_STRING) ? XA_STRING : atom_utf8_;
|
||
|
|
XChangeProperty(dpy_, req.requestor, req.property, type, 8, PropModeReplace,
|
||
|
|
reinterpret_cast<const unsigned char*>(text_.data()),
|
||
|
|
static_cast<int>(text_.size()));
|
||
|
|
resp.property = req.property;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
XSendEvent(dpy_, req.requestor, False, 0, reinterpret_cast<XEvent*>(&resp));
|
||
|
|
XFlush(dpy_);
|
||
|
|
}
|
||
|
|
|
||
|
|
void X11Clipboard::copy_grid(const ClipboardGrid& grid) {
|
||
|
|
internal_ = grid;
|
||
|
|
set_text(grid_to_tsv(grid));
|
||
|
|
}
|
||
|
|
|
||
|
|
void X11Clipboard::set_text(const std::string& text) {
|
||
|
|
text_ = text;
|
||
|
|
if (!dpy_ || win_ == None) return;
|
||
|
|
XSetSelectionOwner(dpy_, atom_clipboard_, win_, CurrentTime);
|
||
|
|
XSetSelectionOwner(dpy_, XA_PRIMARY, win_, CurrentTime);
|
||
|
|
XFlush(dpy_);
|
||
|
|
owns_clipboard_ = (XGetSelectionOwner(dpy_, atom_clipboard_) == win_);
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string X11Clipboard::get_external_text(gemui_app_t* app) const {
|
||
|
|
if (!app) return "";
|
||
|
|
std::vector<char> buf(65536, '\0');
|
||
|
|
if (!gemui_clipboard_get(app, buf.data(), buf.size())) {
|
||
|
|
return "";
|
||
|
|
}
|
||
|
|
return std::string(buf.data());
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string X11Clipboard::grid_to_tsv(const ClipboardGrid& grid) {
|
||
|
|
int rows = grid.src_range.row_count();
|
||
|
|
int cols = grid.src_range.col_count();
|
||
|
|
if (rows <= 0 || cols <= 0) return "";
|
||
|
|
std::vector<std::vector<std::string>> table(static_cast<size_t>(rows),
|
||
|
|
std::vector<std::string>(static_cast<size_t>(cols)));
|
||
|
|
for (const auto& c : grid.cells) {
|
||
|
|
if (c.dr >= 0 && c.dr < rows && c.dc >= 0 && c.dc < cols) {
|
||
|
|
auto sv = to_string(c.evaluated_value);
|
||
|
|
table[static_cast<size_t>(c.dr)][static_cast<size_t>(c.dc)] =
|
||
|
|
std::holds_alternative<std::string>(sv) ? std::get<std::string>(sv) : c.raw_input;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
std::string out;
|
||
|
|
for (int r = 0; r < rows; ++r) {
|
||
|
|
if (r > 0) out.push_back('\n');
|
||
|
|
for (int c = 0; c < cols; ++c) {
|
||
|
|
if (c > 0) out.push_back('\t');
|
||
|
|
out += table[static_cast<size_t>(r)][static_cast<size_t>(c)];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return out;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::vector<CellEdit> X11Clipboard::parse_tsv_to_edits(const std::string& text,
|
||
|
|
int dest_row,
|
||
|
|
int dest_col) {
|
||
|
|
std::vector<CellEdit> edits;
|
||
|
|
if (text.empty()) return edits;
|
||
|
|
|
||
|
|
int r = dest_row;
|
||
|
|
int c = dest_col;
|
||
|
|
size_t start = 0;
|
||
|
|
for (size_t i = 0; i <= text.size(); ++i) {
|
||
|
|
bool at_end = (i == text.size());
|
||
|
|
char ch = at_end ? '\n' : text[i];
|
||
|
|
if (ch == '\t' || ch == '\n' || ch == '\r') {
|
||
|
|
if (at_end && start == i && c == dest_col && i > 0 && text[i - 1] == '\n') {
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
edits.push_back(CellEdit{r, c, text.substr(start, i - start), std::nullopt});
|
||
|
|
if (ch == '\t') {
|
||
|
|
++c;
|
||
|
|
} else {
|
||
|
|
if (ch == '\r' && i + 1 < text.size() && text[i + 1] == '\n') ++i;
|
||
|
|
++r;
|
||
|
|
c = dest_col;
|
||
|
|
}
|
||
|
|
start = i + 1;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return edits;
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace htab::ui
|