115 lines
3.7 KiB
C
115 lines
3.7 KiB
C
#include "editor.h"
|
|||
|
|
#include <X11/Xatom.h>
|
||
|
|
#include <stdlib.h>
|
||
|
|
#include <string.h>
|
||
|
|
|
||
|
|
void clipboard_init(Editor *ed) {
|
||
|
|
ed->atom_clipboard = XInternAtom(ed->dpy, "CLIPBOARD", False);
|
||
|
|
ed->atom_utf8 = XInternAtom(ed->dpy, "UTF8_STRING", False);
|
||
|
|
ed->atom_targets = XInternAtom(ed->dpy, "TARGETS", False);
|
||
|
|
ed->atom_prop = XInternAtom(ed->dpy, "X11_EDITOR_CLIP", False);
|
||
|
|
ed->clip_data = NULL;
|
||
|
|
}
|
||
|
|
|
||
|
|
void clipboard_copy(Editor *ed) {
|
||
|
|
if (!ed->has_sel) return;
|
||
|
|
|
||
|
|
int sy, sx, ey, ex;
|
||
|
|
editor_get_sel_bounds(ed, &sy, &sx, &ey, &ex);
|
||
|
|
if (sy == ey && sx == ex) return;
|
||
|
|
|
||
|
|
size_t total_size = 1;
|
||
|
|
for (int y = sy; y <= ey; y++) {
|
||
|
|
int line_len = (int)strlen(ed->lines[y]);
|
||
|
|
int a = (y == sy) ? sx : 0;
|
||
|
|
int b = (y == ey) ? ex : line_len;
|
||
|
|
if (b > a) total_size += (size_t)(b - a);
|
||
|
|
if (y < ey) total_size += 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
char *buf = malloc(total_size);
|
||
|
|
if (!buf) return;
|
||
|
|
|
||
|
|
size_t pos = 0;
|
||
|
|
for (int y = sy; y <= ey; y++) {
|
||
|
|
int line_len = (int)strlen(ed->lines[y]);
|
||
|
|
int a = (y == sy) ? sx : 0;
|
||
|
|
int b = (y == ey) ? ex : line_len;
|
||
|
|
if (b > a) {
|
||
|
|
memcpy(buf + pos, &ed->lines[y][a], (size_t)(b - a));
|
||
|
|
pos += (size_t)(b - a);
|
||
|
|
}
|
||
|
|
if (y < ey) {
|
||
|
|
buf[pos++] = '\n';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
buf[pos] = '\0';
|
||
|
|
|
||
|
|
free(ed->clip_data);
|
||
|
|
ed->clip_data = buf;
|
||
|
|
|
||
|
|
XSetSelectionOwner(ed->dpy, ed->atom_clipboard, ed->win, CurrentTime);
|
||
|
|
XSetSelectionOwner(ed->dpy, XA_PRIMARY, ed->win, CurrentTime);
|
||
|
|
XFlush(ed->dpy);
|
||
|
|
}
|
||
|
|
|
||
|
|
void clipboard_request_paste(Editor *ed) {
|
||
|
|
XConvertSelection(ed->dpy, ed->atom_clipboard, ed->atom_utf8,
|
||
|
|
ed->atom_prop, ed->win, CurrentTime);
|
||
|
|
XFlush(ed->dpy);
|
||
|
|
}
|
||
|
|
|
||
|
|
void clipboard_handle_selection_request(Editor *ed, XSelectionRequestEvent *req) {
|
||
|
|
XSelectionEvent resp;
|
||
|
|
memset(&resp, 0, sizeof(resp));
|
||
|
|
resp.type = SelectionNotify;
|
||
|
|
resp.requestor = req->requestor;
|
||
|
|
resp.selection = req->selection;
|
||
|
|
resp.target = req->target;
|
||
|
|
resp.time = req->time;
|
||
|
|
resp.property = None;
|
||
|
|
|
||
|
|
if (ed->clip_data && req->property != None) {
|
||
|
|
if (req->target == ed->atom_targets) {
|
||
|
|
Atom targets[2];
|
||
|
|
targets[0] = ed->atom_utf8;
|
||
|
|
targets[1] = XA_STRING;
|
||
|
|
XChangeProperty(ed->dpy, req->requestor, req->property, XA_ATOM, 32,
|
||
|
|
PropModeReplace, (unsigned char *)targets, 2);
|
||
|
|
resp.property = req->property;
|
||
|
|
} else if (req->target == ed->atom_utf8 || req->target == XA_STRING) {
|
||
|
|
XChangeProperty(ed->dpy, req->requestor, req->property, req->target, 8,
|
||
|
|
PropModeReplace, (unsigned char *)ed->clip_data,
|
||
|
|
(int)strlen(ed->clip_data));
|
||
|
|
resp.property = req->property;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
XSendEvent(ed->dpy, req->requestor, False, 0, (XEvent *)&resp);
|
||
|
|
XFlush(ed->dpy);
|
||
|
|
}
|
||
|
|
|
||
|
|
void clipboard_handle_selection_notify(Editor *ed, XSelectionEvent *sev) {
|
||
|
|
if (sev->property == None) return;
|
||
|
|
|
||
|
|
Atom actual_type;
|
||
|
|
int actual_format;
|
||
|
|
unsigned long nitems, bytes_after;
|
||
|
|
unsigned char *data = NULL;
|
||
|
|
|
||
|
|
if (XGetWindowProperty(ed->dpy, ed->win, sev->property, 0, 1024 * 1024, True,
|
||
|
|
AnyPropertyType, &actual_type, &actual_format,
|
||
|
|
&nitems, &bytes_after, &data) == Success && data) {
|
||
|
|
if (nitems > 0) {
|
||
|
|
char *text = malloc(nitems + 1);
|
||
|
|
if (text) {
|
||
|
|
memcpy(text, data, nitems);
|
||
|
|
text[nitems] = '\0';
|
||
|
|
if (ed->has_sel) editor_delete_selection(ed);
|
||
|
|
editor_insert_text(ed, text);
|
||
|
|
free(text);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
XFree(data);
|
||
|
|
}
|
||
|
|
}
|