1 #include "bcclipboard.h"
2 #include "bcwindowbase.h"
5 BC_Clipboard::BC_Clipboard(char *display_name) : Thread()
7 Thread::set_synchronous(1);
9 in_display = BC_WindowBase::init_display(display_name);
10 out_display = BC_WindowBase::init_display(display_name);
11 completion_atom = XInternAtom(out_display, "BC_CLOSE_EVENT", False);
13 secondary = XInternAtom(out_display, "CLIPBOARD", False);
14 in_win = XCreateSimpleWindow(in_display,
15 DefaultRootWindow(in_display),
23 out_win = XCreateSimpleWindow(out_display,
24 DefaultRootWindow(out_display),
36 BC_Clipboard::~BC_Clipboard()
38 if(data[0]) delete [] data[0];
39 if(data[1]) delete [] data[1];
41 XDestroyWindow(in_display, in_win);
42 XCloseDisplay(in_display);
43 XDestroyWindow(out_display, out_win);
44 XCloseDisplay(out_display);
47 int BC_Clipboard::start_clipboard()
53 int BC_Clipboard::stop_clipboard()
56 XClientMessageEvent *ptr = (XClientMessageEvent*)&event;
58 event.type = ClientMessage;
59 ptr->message_type = completion_atom;
61 XSendEvent(out_display, out_win, 0, 0, &event);
67 void BC_Clipboard::run()
70 XClientMessageEvent *ptr;
75 //printf("BC_Clipboard::run 1\n");
76 XNextEvent(out_display, &event);
77 //printf("BC_Clipboard::run 2 %d\n", event.type);
79 XLockDisplay(out_display);
84 ptr = (XClientMessageEvent*)&event;
85 if(ptr->message_type == completion_atom)
89 //printf("ClientMessage %x %x %d\n", ptr->message_type, ptr->data.l[0], primary_atom);
93 case SelectionRequest:
96 XSelectionRequestEvent *request = (XSelectionRequestEvent*)&event;
97 char *data_ptr = (request->selection == primary ? data[0] : data[1]);
99 //printf("BC_Clipboard::run 2\n");
100 XChangeProperty(out_display,
106 (unsigned char*)data_ptr,
109 reply.xselection.property = request->property;
110 reply.xselection.type = SelectionNotify;
111 reply.xselection.display = request->display;
112 reply.xselection.requestor = request->requestor;
113 reply.xselection.selection = request->selection;
114 reply.xselection.target = request->target;
115 reply.xselection.time = request->time;
118 XSendEvent(out_display, request->requestor, 0, 0, &reply);
121 //printf("SelectionRequest\n");
127 if(data[0]) data[0][0] = 0;
128 if(data[1]) data[1][0] = 0;
131 XUnlockDisplay(out_display);
135 int BC_Clipboard::to_clipboard(char *data, long len, int clipboard_num)
139 XStoreBuffer(display, data, len, clipboard_num);
142 XLockDisplay(out_display);
144 // Store in local buffer
145 if(this->data[clipboard_num] && length[clipboard_num] != len + 1)
147 delete [] this->data[clipboard_num];
148 this->data[clipboard_num] = 0;
151 if(!this->data[clipboard_num])
153 length[clipboard_num] = len;
154 this->data[clipboard_num] = new char[len + 1];
155 memcpy(this->data[clipboard_num], data, len);
156 this->data[clipboard_num][len] = 0;
159 XSetSelectionOwner(out_display,
160 (clipboard_num == PRIMARY_SELECTION) ? primary : secondary,
166 XUnlockDisplay(out_display);
170 int BC_Clipboard::from_clipboard(char *data, long maxlen, int clipboard_num)
178 data2 = XFetchBuffer(display, &len, clipboard_num);
179 for(i = 0; i < len && i < maxlen; i++)
189 XLockDisplay(in_display);
192 Atom type_return, pty;
194 unsigned long nitems, size, new_size, total;
197 pty = (clipboard_num == PRIMARY_SELECTION) ? primary : secondary;
198 /* a property of our window
199 for apps to put their
202 XConvertSelection(in_display,
203 clipboard_num == PRIMARY_SELECTION ? primary : secondary,
212 XNextEvent(in_display, &event);
213 }while(event.type != SelectionNotify && event.type != None);
215 if(event.type != None)
218 XGetWindowProperty(in_display,
229 (unsigned char**)&temp_data);
231 if(temp_data) XFree(temp_data);
235 XGetWindowProperty(in_display,
246 (unsigned char**)&temp_data);
249 if(type_return && temp_data)
251 strncpy(data, temp_data, maxlen);
257 if(temp_data) XFree(temp_data);
260 XUnlockDisplay(in_display);
265 long BC_Clipboard::clipboard_len(int clipboard_num)
272 data2 = XFetchBuffer(display, &len, clipboard_num);
279 XLockDisplay(in_display);
282 Atom type_return, pty;
284 unsigned long nitems, pty_size, total;
288 pty = (clipboard_num == PRIMARY_SELECTION) ? primary : secondary;
289 /* a property of our window
290 for apps to put their
292 XConvertSelection(in_display,
293 (clipboard_num == PRIMARY_SELECTION) ? primary : secondary,
301 XNextEvent(in_display, &event);
302 }while(event.type != SelectionNotify && event.type != None);
304 if(event.type != None)
307 XGetWindowProperty(in_display,
318 (unsigned char**)&temp_data);
322 result = pty_size + 1;
334 XUnlockDisplay(in_display);