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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
| #include <xcb/xcb.h>
#include <xcb/xcb_atom.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <iostream>
#include <string.h>
using namespace std;
#ifndef UINT32_MAX
#define UINT32_MAX 0xffffffff
#endif
#define XCB_EVENT_RESPONSE_TYPE_MASK (0x7f)
#define XCB_EVENT_RESPONSE_TYPE(e) (e->response_type & XCB_EVENT_RESPONSE_TYPE_MASK)
#define XCB_EVENT_SENT(e) (e->response_type & ~XCB_EVENT_RESPONSE_TYPE_MASK)
xcb_atom_t get_atom_from_string(xcb_connection_t *connect, const char *atom_str)
{
xcb_intern_atom_cookie_t cookie_;
xcb_intern_atom_reply_t *reply_;
cookie_ = xcb_intern_atom (connect,0,strlen(atom_str),atom_str);
reply_ = xcb_intern_atom_reply (connect, cookie_, NULL);
xcb_atom_t atom = reply_->atom;
free(reply_);
return atom;
}
int main (int argc, char **argv)
{
xcb_connection_t *connect;
xcb_screen_t *screen;
xcb_window_t win;
xcb_generic_event_t *event;
int done;
// open connection with the server
connect = xcb_connect (NULL, NULL);
if (xcb_connection_has_error (connect) /*== NULL*/)
{
cout << "cannot connect to X server " << endl;
xcb_disconnect (connect);
return 0;
}
screen = xcb_setup_roots_iterator (xcb_get_setup (connect)).data;
/* create window */
uint32_t mask;
uint32_t values[2]; //3 для XCB_CW_OVERRIDE_REDIRECT
//mask = XCB_CW_BACK_PIXMAP| XCB_CW_EVENT_MASK; - фон окна - pixmap рабочего стола
mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; // | XCB_CW_OVERRIDE_REDIRECT; - этот флаг - если вам не нужна рамка и голова( но все сообщения перемещения/ресайз и т.д. на вашей совести )
//values[0] = XCB_BACK_PIXMAP_PARENT_RELATIVE;//- фон окна - pixmap рабочего стола
values[0] = screen->white_pixel;
values[1] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_PROPERTY_CHANGE;
//values[2] = 1;//значение XCB_CW_OVERRIDE_REDIRECT;
win = xcb_generate_id (connect);
xcb_create_window (connect, XCB_COPY_FROM_PARENT, win, screen->root,
0, 0, 500, 500, 0,
XCB_WINDOW_CLASS_INPUT_OUTPUT,
screen->root_visual,
mask, values);
//далее смотрим какие прилржения готовы обменятся информацией
//CLIPBOARD - выделение/копирование клавой PRIMARY - мышкой
///++++++++++++++++++++++++++++++++
xcb_atom_t PRIMARY = get_atom_from_string(connect, "PRIMARY");
xcb_atom_t CLIPBOARD = get_atom_from_string(connect, "CLIPBOARD");
cout << "PRIMARY atom: " << PRIMARY << endl;
cout << "CLIPBOARD atom: " << CLIPBOARD << endl;
xcb_get_selection_owner_cookie_t cookie_primary, cookie_clipboard;
cookie_primary = xcb_get_selection_owner(connect, PRIMARY);//XCB_ATOM_PRIMARY
cookie_clipboard = xcb_get_selection_owner(connect, CLIPBOARD);
xcb_get_selection_owner_reply_t *reply_primary = xcb_get_selection_owner_reply( connect, cookie_primary, NULL );
xcb_get_selection_owner_reply_t *reply_clipboard = xcb_get_selection_owner_reply( connect, cookie_clipboard, NULL );
xcb_window_t win_owner_primary = reply_primary->owner;
xcb_window_t win_owner_clipboard = reply_clipboard->owner;
xcb_get_property_cookie_t cookie_property_primary;
xcb_get_property_cookie_t cookie_property_clipboard;
cookie_property_primary = xcb_get_property_unchecked(connect, 0, win_owner_primary, get_atom_from_string(connect, "_NET_WM_NAME")
, get_atom_from_string(connect, "UTF8_STRING"), 0, UINT32_MAX);
cookie_property_clipboard = xcb_get_property_unchecked(connect, 0, win_owner_clipboard, get_atom_from_string(connect, "_NET_WM_NAME")
, get_atom_from_string(connect, "UTF8_STRING"), 0, UINT32_MAX);
xcb_get_property_reply_t *reply_property_primary = xcb_get_property_reply(connect, cookie_property_primary, NULL);
xcb_get_property_reply_t *reply_property_clipboard = xcb_get_property_reply(connect, cookie_property_clipboard, NULL);
void *data = NULL;
data = xcb_get_property_value(reply_property_primary);
if (reply_property_primary && data )
{
cout << "PRIMARY owner: "<< (char*)data << " id " << win_owner_primary << endl;
}else cout << "PRIMARY owner: NONE" << endl;
data = NULL;
data = xcb_get_property_value(reply_property_clipboard);
if (reply_property_clipboard && data )
{
cout << "CLIPBOARD owner: "<<(char*)data << " id " << win_owner_clipboard << endl;
}else cout << "CLIPBOARD owner: NONE" << endl;
cout << "XSEL_DATA atom: " << get_atom_from_string(connect, "XSEL_DATA") << endl;
free(reply_property_primary);
free(reply_primary);
free(reply_property_clipboard);
free(reply_clipboard);
xcb_atom_t utf8_string = get_atom_from_string(connect, "UTF8_STRING");
xcb_atom_t xsel_data = get_atom_from_string(connect, "XSEL_DATA"); // вот этот атом - сама суть X SLECTION DATA
xcb_convert_selection(connect,
win,
PRIMARY, //XCB_ATOM_PRIMARY, одноху...венно
utf8_string,
//NULL, - здесь Нулл не работает :)
xsel_data,
XCB_CURRENT_TIME);
xcb_flush (connect);
int done_ev = 0;
int i = 0;
xcb_generic_event_t *ev;
while(!done_ev)
{
ev = xcb_wait_for_event (connect);
if(!ev) return 0;
if ( XCB_EVENT_RESPONSE_TYPE(ev) != XCB_SELECTION_NOTIFY ) continue; // пока не произойдет convert_selection
cout << "XCB_EVENT_RESPONSE_TYPE(ev) " << (int)XCB_EVENT_RESPONSE_TYPE(ev) << endl;
cout << "XCB_SELECTION_NOTIFY " << (int)XCB_SELECTION_NOTIFY << endl;
xcb_selection_notify_event_t *event_notify = (xcb_selection_notify_event_t *) ev;
cout << endl<<endl<< "event_notify" << endl << endl;
cout << "property " << (int)event_notify->property << endl;
cout << "requestor " << (int)event_notify->requestor << endl;
cout << " win id: " << win <<endl;
cout << "response_type " << (int)event_notify->response_type << endl;
cout << "selection " << (int)event_notify->selection << endl;
cout << "sequence " << (int)event_notify->sequence << endl;
cout << "target " << (int)event_notify->target << endl;
cout << "time " << (int)event_notify->time << endl;
cout<<"++++++++++++++++++"<<endl<<endl;
if (event_notify->property)
{
xcb_get_property_reply_t *reply;
xcb_get_property_cookie_t cookie;
cookie = xcb_get_property_unchecked(connect, 0, event_notify->requestor, event_notify->property,utf8_string, 0, UINT32_MAX);
reply = xcb_get_property_reply(connect, cookie, 0);
void *value = xcb_get_property_value(reply);
int val_len = xcb_get_property_value_length(reply);
if (reply && value && val_len)
{
cout << val_len << endl;
cout << (char *)value<<endl;
xcb_delete_property(connect,
event_notify->requestor,
event_notify->property);
}
free(reply);
}
free(ev);
done_ev = 1;
}
//xcb_flush (connect);
/* map (show) the window */
//вот здесь далее в комментариях сам вопрос - КАК МНЕ СКОПИРОВАТЬ ТЕКСТ ИЗ СВОЕЙ ПРОГРАММЫ В БУФЕР ОБМЕНА
// xcb_flush (connect);
// char *str = "hallo";
//xcb_change_property(connect,XCB_PROP_MODE_REPLACE,0,xsel_data, utf8_string,32,strlen(str),str);
//xcb_flush (connect);
// xcb_set_selection_owner(connect, win, PRIMARY, XCB_CURRENT_TIME);
//xcb_set_selection_owner(connect, win, CLIPBOARD, XCB_CURRENT_TIME);
// Н Е Р А Б О Т А Е Т !!!
xcb_flush (connect);
xcb_map_window(connect, win);
xcb_flush (connect);
/* event loop */
done = 0;
while (!done)
{
event = xcb_wait_for_event (connect);
//switch (event->response_type & ~0x80)
switch (XCB_EVENT_RESPONSE_TYPE(event))
{
//(re)draw the window
case XCB_EXPOSE:
cout << "XCB_EXPOSE" << endl;
break;
// exit on keypress
case XCB_KEY_PRESS:
done = 1;
break;
}
free (event);
}
/* close connection to server */
xcb_disconnect (connect);
return 0; |