-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathx11_clipboard.c
More file actions
132 lines (122 loc) · 4.53 KB
/
Copy pathx11_clipboard.c
File metadata and controls
132 lines (122 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Bamboo - A Vietnamese Input method editor
* Copyright (C) 2018 Luong Thanh Lam <ltlam93@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <string.h> // strlen
#include <X11/Xlib.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#define MAX_TEXT_LEN 100
static pthread_t th_clipboard;
static int clipboard_running;
static char * text = NULL;
static char * old_text = NULL;
static int done = 0;
Atom targets_atom, text_atom, UTF8, XA_ATOM = 4, XA_STRING = 31;
void clipboard_exit()
{
clipboard_running = 0;
//free(text);
}
static void* thread_clipboard_copy(void* data) {
Display* display = XOpenDisplay(0);
if (!display) {
return NULL;
}
int N = DefaultScreen(display);
Window window = XCreateSimpleWindow(display, RootWindow(display, N), 0, 0, 1, 1, 0,
BlackPixel(display, N), WhitePixel(display, N));
targets_atom = XInternAtom(display, "TARGETS", 0);
text_atom = XInternAtom(display, "TEXT", 0);
UTF8 = XInternAtom(display, "UTF8_STRING", 1);
if (UTF8 == None) UTF8 = XA_STRING;
Atom selection = XInternAtom(display, (char*)data, 0);
XEvent event;
Window owner;
XSetSelectionOwner (display, selection, window, 0);
if (XGetSelectionOwner (display, selection) != window) return NULL;
while (clipboard_running==1) {
XNextEvent (display, &event);
switch (event.type) {
case SelectionRequest:
if (event.xselectionrequest.selection != selection) break;
XSelectionRequestEvent * xsr = &event.xselectionrequest;
XSelectionEvent ev = {0};
int R = 0;
ev.type = SelectionNotify, ev.display = xsr->display, ev.requestor = xsr->requestor,
ev.selection = xsr->selection, ev.time = xsr->time, ev.target = xsr->target, ev.property = xsr->property;
if (done == -1) {
ev.property = None;
XSendEvent (display, ev.requestor, 0, 0, (XEvent *)&ev);
break;
}
if (text == NULL) break;
int size = strlen(text);
if (ev.target == targets_atom) {
R = XChangeProperty (ev.display, ev.requestor, ev.property, XA_ATOM, 32, PropModeReplace, (unsigned char*)&UTF8, 1);
}
else if (ev.target == XA_STRING || ev.target == text_atom) {
R = XChangeProperty(ev.display, ev.requestor, ev.property, XA_STRING, 8, PropModeReplace, (unsigned char*)text, size);
done = 1;
}
else if (ev.target == UTF8) {
R = XChangeProperty(ev.display, ev.requestor, ev.property, UTF8, 8, PropModeReplace, (unsigned char*)text, size);
done = 1;
}
else ev.property = None;
if ((R & 2) == 0) XSendEvent (display, ev.requestor, 0, 0, (XEvent *)&ev);
break;
case SelectionClear:
clipboard_exit();
break;
}
}
clipboard_running = 0;
XCloseDisplay(display);
return NULL;
}
void clipboard_init()
{
if (clipboard_running==1) {
return;
}
XInitThreads();
clipboard_running = 1;
char *selections[] = {"PRIMARY", "CLIPBOARD"};
for (int i=0; i<sizeof selections/sizeof selections[0]; i++) {
pthread_create(&th_clipboard, NULL, &thread_clipboard_copy, selections[i]);
pthread_detach(th_clipboard);
}
}
void x11ClipboardReset() {
if (text == NULL) {
text = (char*)calloc(MAX_TEXT_LEN, sizeof(char));
}
strcpy(text, "");
}
void x11Copy(char *str) {
if (text == NULL) {
text = (char*)calloc(MAX_TEXT_LEN, sizeof(char));
}
strcpy(text, str);
done = 0;
fprintf(stderr, "...x11Clipboard text=%s, clipboard_running=%d\n", text, clipboard_running);
if (clipboard_running == 0) {
clipboard_init();
}
}