/[rdesktop]/sourceforge.net/trunk/rdesktop/xclip.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /sourceforge.net/trunk/rdesktop/xclip.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 432 by matthewc, Tue Jul 1 09:31:25 2003 UTC revision 536 by matthewc, Fri Oct 31 04:29:57 2003 UTC
# Line 25  Line 25 
25    
26  #define NUM_TARGETS 6  #define NUM_TARGETS 6
27    
28  extern Display *display;  extern Display *g_display;
29  extern Window wnd;  extern Window g_wnd;
30  extern Time last_gesturetime;  extern Time g_last_gesturetime;
31    
32  static Atom clipboard_atom, primary_atom, targets_atom, timestamp_atom;  static Atom clipboard_atom, primary_atom, targets_atom, timestamp_atom;
33  static Atom rdesktop_clipboard_target_atom, rdesktop_clipboard_formats_atom, incr_atom;  static Atom rdesktop_clipboard_target_atom, rdesktop_clipboard_formats_atom, incr_atom;
# Line 37  static int have_primary = 0; Line 37  static int have_primary = 0;
37  static int rdesktop_is_selection_owner = 0;  static int rdesktop_is_selection_owner = 0;
38    
39  static void  static void
40  xclip_provide_selection(XSelectionRequestEvent *req, Atom type, unsigned int format, uint8 *data, uint32 length)  xclip_provide_selection(XSelectionRequestEvent * req, Atom type, unsigned int format, uint8 * data,
41                            uint32 length)
42  {  {
43          XEvent xev;          XEvent xev;
44    
45          XChangeProperty(display, req->requestor, req->property,          XChangeProperty(g_display, req->requestor, req->property,
46                          type, format, PropModeReplace, data, length);                          type, format, PropModeReplace, data, length);
47    
48          xev.xselection.type = SelectionNotify;          xev.xselection.type = SelectionNotify;
# Line 52  xclip_provide_selection(XSelectionReques Line 53  xclip_provide_selection(XSelectionReques
53          xev.xselection.target = req->target;          xev.xselection.target = req->target;
54          xev.xselection.property = req->property;          xev.xselection.property = req->property;
55          xev.xselection.time = req->time;          xev.xselection.time = req->time;
56          XSendEvent(display, req->requestor, False, NoEventMask, &xev);          XSendEvent(g_display, req->requestor, False, NoEventMask, &xev);
57  }  }
58    
59  void  void
# Line 68  xclip_handle_SelectionNotify(XSelectionE Line 69  xclip_handle_SelectionNotify(XSelectionE
69                  goto fail;                  goto fail;
70    
71          DEBUG_CLIPBOARD(("xclip_handle_SelectionNotify: selection=%s, target=%s, property=%s\n",          DEBUG_CLIPBOARD(("xclip_handle_SelectionNotify: selection=%s, target=%s, property=%s\n",
72                           XGetAtomName(display, event->selection),                           XGetAtomName(g_display, event->selection),
73                           XGetAtomName(display, event->target),                           XGetAtomName(g_display, event->target),
74                           XGetAtomName(display, event->property)));                           XGetAtomName(g_display, event->property)));
75    
76          if (event->property == None)          if (event->property == None)
77                  goto fail;                  goto fail;
78    
79          res = XGetWindowProperty(display, wnd, rdesktop_clipboard_target_atom,          res = XGetWindowProperty(g_display, g_wnd, rdesktop_clipboard_target_atom,
80                                   0, XMaxRequestSize(display), True, AnyPropertyType,                                   0, XMaxRequestSize(g_display), True, AnyPropertyType,
81                                   &type, &format, &nitems, &bytes_left, &data);                                   &type, &format, &nitems, &bytes_left, &data);
82    
83          if (res != Success)          if (res != Success)
# Line 92  xclip_handle_SelectionNotify(XSelectionE Line 93  xclip_handle_SelectionNotify(XSelectionE
93                  if (type != None)                  if (type != None)
94                  {                  {
95                          supported_targets = (Atom *) data;                          supported_targets = (Atom *) data;
96                          text_target = XInternAtom(display, "TEXT", False);                          text_target = XInternAtom(g_display, "TEXT", False);
97                          for (i = 0; i < nitems; i++)                          for (i = 0; i < nitems; i++)
98                          {                          {
99                                  DEBUG_CLIPBOARD(("Target %d: %s\n", i, XGetAtomName(display, supported_targets[i])));                                  DEBUG_CLIPBOARD(("Target %d: %s\n", i,
100                                                     XGetAtomName(g_display, supported_targets[i])));
101                                  if (supported_targets[i] == text_target)                                  if (supported_targets[i] == text_target)
102                                  {                                  {
103                                          DEBUG_CLIPBOARD(("Other party supports TEXT, choosing that as best_target\n"));                                          DEBUG_CLIPBOARD(("Other party supports TEXT, choosing that as best_target\n"));
# Line 105  xclip_handle_SelectionNotify(XSelectionE Line 107  xclip_handle_SelectionNotify(XSelectionE
107                          XFree(data);                          XFree(data);
108                  }                  }
109    
110                  XConvertSelection(display, primary_atom, best_target, rdesktop_clipboard_target_atom, wnd, event->time);                  XConvertSelection(g_display, primary_atom, best_target,
111                                      rdesktop_clipboard_target_atom, g_wnd, event->time);
112                  return;                  return;
113          }          }
114    
# Line 115  xclip_handle_SelectionNotify(XSelectionE Line 118  xclip_handle_SelectionNotify(XSelectionE
118                  goto fail;                  goto fail;
119          }          }
120    
121          cliprdr_send_data(data, nitems+1);          cliprdr_send_data(data, nitems + 1);
122          XFree(data);          XFree(data);
123    
124          if (!rdesktop_is_selection_owner)          if (!rdesktop_is_selection_owner)
125                  cliprdr_send_text_format_announce();                  cliprdr_send_text_format_announce();
126          return;          return;
127    
128  fail:        fail:
129          cliprdr_send_data(NULL, 0);          cliprdr_send_data(NULL, 0);
130  }  }
131    
132  void  void
133  xclip_handle_SelectionRequest(XSelectionRequestEvent *event)  xclip_handle_SelectionRequest(XSelectionRequestEvent * event)
134  {  {
135          unsigned long nitems, bytes_left;          unsigned long nitems, bytes_left;
136            unsigned char *prop_return;
137          uint32 *wanted_format;          uint32 *wanted_format;
138          int format, res;          int format, res;
139          Atom type;          Atom type;
140    
141          DEBUG_CLIPBOARD(("xclip_handle_SelectionRequest: selection=%s, target=%s, property=%s\n",          DEBUG_CLIPBOARD(("xclip_handle_SelectionRequest: selection=%s, target=%s, property=%s\n",
142                           XGetAtomName(display, event->selection),                           XGetAtomName(g_display, event->selection),
143                           XGetAtomName(display, event->target),                           XGetAtomName(g_display, event->target),
144                           XGetAtomName(display, event->property)));                           XGetAtomName(g_display, event->property)));
145    
146          if (event->target == targets_atom)          if (event->target == targets_atom)
147          {          {
148                  xclip_provide_selection(event, XA_ATOM, 32, (uint8 *)&targets, NUM_TARGETS);                  xclip_provide_selection(event, XA_ATOM, 32, (uint8 *) & targets, NUM_TARGETS);
149                  return;                  return;
150          }          }
151          else if (event->target == timestamp_atom)          else if (event->target == timestamp_atom)
152          {          {
153                  xclip_provide_selection(event, XA_INTEGER, 32, (uint8 *)&last_gesturetime, 1);                  xclip_provide_selection(event, XA_INTEGER, 32, (uint8 *) & g_last_gesturetime, 1);
154                  return;                  return;
155          }          }
156          else if (event->target == rdesktop_clipboard_formats_atom)          else if (event->target == rdesktop_clipboard_formats_atom)
157          {          {
158                  res = XGetWindowProperty(display, event->requestor,                  res = XGetWindowProperty(g_display, event->requestor,
159                                  rdesktop_clipboard_target_atom, 0, 1, True, XA_INTEGER,                                           rdesktop_clipboard_target_atom, 0, 1, True, XA_INTEGER,
160                                  &type, &format, &nitems, &bytes_left, (unsigned char **) &wanted_format);                                           &type, &format, &nitems, &bytes_left, &prop_return);
161                    wanted_format = (uint32 *) prop_return;
162                  format = (res == Success) ? *wanted_format : CF_TEXT;                  format = (res == Success) ? *wanted_format : CF_TEXT;
163                    /* FIXME: Need to free returned data? */
164          }          }
165          else          else
166          {          {
# Line 171  xclip_handle_SelectionClear(void) Line 177  xclip_handle_SelectionClear(void)
177  {  {
178          DEBUG_CLIPBOARD(("xclip_handle_SelectionClear\n"));          DEBUG_CLIPBOARD(("xclip_handle_SelectionClear\n"));
179          have_primary = 0;          have_primary = 0;
180          XDeleteProperty(display, DefaultRootWindow(display), rdesktop_clipboard_formats_atom);          XDeleteProperty(g_display, DefaultRootWindow(g_display), rdesktop_clipboard_formats_atom);
181          cliprdr_send_text_format_announce();          cliprdr_send_text_format_announce();
182  }  }
183    
184  void  void
185  xclip_handle_PropertyNotify(XPropertyEvent *event)  xclip_handle_PropertyNotify(XPropertyEvent * event)
186  {  {
187          unsigned long nitems, bytes_left;          unsigned long nitems, bytes_left;
188          int format, res;          int format, res;
# Line 186  xclip_handle_PropertyNotify(XPropertyEve Line 192  xclip_handle_PropertyNotify(XPropertyEve
192          if (event->atom != rdesktop_clipboard_formats_atom)          if (event->atom != rdesktop_clipboard_formats_atom)
193                  return;                  return;
194    
195          if (have_primary) /* from us */          if (have_primary)       /* from us */
196                  return;                  return;
197    
198          if (event->state == PropertyNewValue)          if (event->state == PropertyNewValue)
199          {          {
200                  res = XGetWindowProperty(display, DefaultRootWindow(display),                  res = XGetWindowProperty(g_display, DefaultRootWindow(g_display),
201                          rdesktop_clipboard_formats_atom, 0, XMaxRequestSize(display), False, XA_STRING,                                           rdesktop_clipboard_formats_atom, 0,
202                          &type, &format, &nitems, &bytes_left, &data);                                           XMaxRequestSize(g_display), False, XA_STRING, &type,
203                                             &format, &nitems, &bytes_left, &data);
204    
205                  if ((res == Success) && (nitems > 0))                  if ((res == Success) && (nitems > 0))
206                  {                  {
# Line 210  xclip_handle_PropertyNotify(XPropertyEve Line 217  xclip_handle_PropertyNotify(XPropertyEve
217    
218    
219  void  void
220  ui_clip_format_announce(char *data, uint32 length)  ui_clip_format_announce(uint8 *data, uint32 length)
221  {  {
222          XSetSelectionOwner(display, primary_atom, wnd, last_gesturetime);          XSetSelectionOwner(g_display, primary_atom, g_wnd, g_last_gesturetime);
223          if (XGetSelectionOwner(display, primary_atom) != wnd)          if (XGetSelectionOwner(g_display, primary_atom) != g_wnd)
224          {          {
225                  warning("Failed to aquire ownership of PRIMARY clipboard\n");                  warning("Failed to aquire ownership of PRIMARY clipboard\n");
226                  return;                  return;
227          }          }
228    
229          have_primary = 1;          have_primary = 1;
230          XChangeProperty(display, DefaultRootWindow(display),          XChangeProperty(g_display, DefaultRootWindow(g_display),
231                          rdesktop_clipboard_formats_atom, XA_STRING, 8, PropModeReplace, data, length);                          rdesktop_clipboard_formats_atom, XA_STRING, 8, PropModeReplace, data,
232                            length);
233    
234          XSetSelectionOwner(display, clipboard_atom, wnd, last_gesturetime);          XSetSelectionOwner(g_display, clipboard_atom, g_wnd, g_last_gesturetime);
235          if (XGetSelectionOwner(display, clipboard_atom) != wnd)          if (XGetSelectionOwner(g_display, clipboard_atom) != g_wnd)
236                  warning("Failed to aquire ownership of CLIPBOARD clipboard\n");                  warning("Failed to aquire ownership of CLIPBOARD clipboard\n");
237  }  }
238    
239    
240  void  void
241  ui_clip_handle_data(char *data, uint32 length)  ui_clip_handle_data(uint8 *data, uint32 length)
242  {  {
243          xclip_provide_selection(&selection_request, XA_STRING, 8, data, length-1);          xclip_provide_selection(&selection_request, XA_STRING, 8, data, length - 1);
244  }  }
245    
246  void  void
# Line 244  ui_clip_request_data(uint32 format) Line 252  ui_clip_request_data(uint32 format)
252    
253          if (rdesktop_is_selection_owner)          if (rdesktop_is_selection_owner)
254          {          {
255                  XChangeProperty(display, wnd, rdesktop_clipboard_target_atom,                  XChangeProperty(g_display, g_wnd, rdesktop_clipboard_target_atom,
256                                  XA_INTEGER, 32, PropModeReplace, (unsigned char *) &format, 1);                                  XA_INTEGER, 32, PropModeReplace, (unsigned char *) &format, 1);
257    
258                  XConvertSelection(display, primary_atom, rdesktop_clipboard_formats_atom,                  XConvertSelection(g_display, primary_atom, rdesktop_clipboard_formats_atom,
259                                          rdesktop_clipboard_target_atom, wnd, CurrentTime);                                    rdesktop_clipboard_target_atom, g_wnd, CurrentTime);
260                  return;                  return;
261          }          }
262    
263          selectionowner = XGetSelectionOwner(display, primary_atom);          selectionowner = XGetSelectionOwner(g_display, primary_atom);
264          if (selectionowner != None)          if (selectionowner != None)
265          {          {
266                  XConvertSelection(display, primary_atom, targets_atom,                  XConvertSelection(g_display, primary_atom, targets_atom,
267                                          rdesktop_clipboard_target_atom, wnd, CurrentTime);                                    rdesktop_clipboard_target_atom, g_wnd, CurrentTime);
268                  return;                  return;
269          }          }
270    
271          /* No PRIMARY, try CLIPBOARD */          /* No PRIMARY, try CLIPBOARD */
272          selectionowner = XGetSelectionOwner(display, clipboard_atom);          selectionowner = XGetSelectionOwner(g_display, clipboard_atom);
273          if (selectionowner != None)          if (selectionowner != None)
274          {          {
275                  XConvertSelection(display, clipboard_atom, targets_atom,                  XConvertSelection(g_display, clipboard_atom, targets_atom,
276                                          rdesktop_clipboard_target_atom, wnd, CurrentTime);                                    rdesktop_clipboard_target_atom, g_wnd, CurrentTime);
277                  return;                  return;
278          }          }
279    
# Line 286  xclip_init(void) Line 294  xclip_init(void)
294          if (!cliprdr_init())          if (!cliprdr_init())
295                  return;                  return;
296    
297          primary_atom = XInternAtom(display, "PRIMARY", False);          primary_atom = XInternAtom(g_display, "PRIMARY", False);
298          clipboard_atom = XInternAtom(display, "CLIPBOARD", False);          clipboard_atom = XInternAtom(g_display, "CLIPBOARD", False);
299          targets_atom = XInternAtom(display, "TARGETS", False);          targets_atom = XInternAtom(g_display, "TARGETS", False);
300          timestamp_atom = XInternAtom(display, "TIMESTAMP", False);          timestamp_atom = XInternAtom(g_display, "TIMESTAMP", False);
301          rdesktop_clipboard_target_atom = XInternAtom(display, "_RDESKTOP_CLIPBOARD_TARGET", False);          rdesktop_clipboard_target_atom =
302          incr_atom = XInternAtom(display, "INCR", False);                  XInternAtom(g_display, "_RDESKTOP_CLIPBOARD_TARGET", False);
303            incr_atom = XInternAtom(g_display, "INCR", False);
304          targets[0] = targets_atom;          targets[0] = targets_atom;
305          targets[1] = XInternAtom(display, "TEXT", False);          targets[1] = XInternAtom(g_display, "TEXT", False);
306          targets[2] = XInternAtom(display, "UTF8_STRING", False);          targets[2] = XInternAtom(g_display, "UTF8_STRING", False);
307          targets[3] = XInternAtom(display, "text/unicode", False);          targets[3] = XInternAtom(g_display, "text/unicode", False);
308          targets[4] = XInternAtom(display, "TIMESTAMP", False);          targets[4] = XInternAtom(g_display, "TIMESTAMP", False);
309          targets[5] = XA_STRING;          targets[5] = XA_STRING;
310    
311          /* rdesktop sets _RDESKTOP_CLIPBOARD_FORMATS on the root window when acquiring the clipboard.          /* rdesktop sets _RDESKTOP_CLIPBOARD_FORMATS on the root window when acquiring the clipboard.
312             Other interested rdesktops can use this to notify their server of the available formats. */             Other interested rdesktops can use this to notify their server of the available formats. */
313          rdesktop_clipboard_formats_atom = XInternAtom(display, "_RDESKTOP_CLIPBOARD_FORMATS", False);          rdesktop_clipboard_formats_atom =
314          XSelectInput(display, DefaultRootWindow(display), PropertyChangeMask);                  XInternAtom(g_display, "_RDESKTOP_CLIPBOARD_FORMATS", False);
315            XSelectInput(g_display, DefaultRootWindow(g_display), PropertyChangeMask);
316  }  }

Legend:
Removed from v.432  
changed lines
  Added in v.536

  ViewVC Help
Powered by ViewVC 1.1.26