aboutsummaryrefslogtreecommitdiff
path: root/winsup/cygwin/fhandler_clipboard.cc
blob: 05f54ffb327a2e9feb2165a32ddf43b237105ec8 (plain)
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* fhandler_dev_clipboard: code to access /dev/clipboard

   Written by Charles Wilson (cwilson@ece.gatech.edu)

This file is part of Cygwin.

This software is a copyrighted work licensed under the terms of the
Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
details. */

#include "winsup.h"
#include <wchar.h>
#include "cygerrno.h"
#include "path.h"
#include "fhandler.h"
#include "sync.h"
#include "dtable.h"
#include "cygheap.h"
#include "child_info.h"
#include <sys/clipboard.h>

/*
 * Robert Collins:
 * FIXME: should we use GetClipboardSequenceNumber to tell if the clipboard has
 * changed? How does /dev/clipboard operate under (say) linux?
 */

fhandler_dev_clipboard::fhandler_dev_clipboard ()
  : fhandler_base (), pos (0), membuffer (NULL), msize (0)
{
  /* FIXME: check for errors and loop until we can open the clipboard */
  OpenClipboard (NULL);
  cygnativeformat = RegisterClipboardFormatW (CYGWIN_NATIVE);
  CloseClipboard ();
}

/*
 * Special clipboard dup to duplicate input and output
 * handles.
 */

int
fhandler_dev_clipboard::dup (fhandler_base * child, int flags)
{
  fhandler_dev_clipboard *fhc = (fhandler_dev_clipboard *) child;
  fhc->pos = fhc->msize = 0;
  fhc->membuffer = NULL;
  return fhandler_base::dup (child, flags);
}

int
fhandler_dev_clipboard::set_clipboard (const void *buf, size_t len)
{
  HGLOBAL hmem;
  /* Native CYGWIN format */
  if (OpenClipboard (NULL))
    {
      cygcb_t *clipbuf;

      hmem = GlobalAlloc (GMEM_MOVEABLE, sizeof (cygcb_t) + len);
      if (!hmem)
	{
	  __seterrno ();
	  CloseClipboard ();
	  return -1;
	}
      clipbuf = (cygcb_t *) GlobalLock (hmem);

      clock_gettime (CLOCK_REALTIME, &clipbuf->ts);
#ifdef __x86_64__
      /* ts overlays cb_sec and cb_nsec such that no conversion is needed */
#elif __i386__
      /* Expand 32-bit timespec layout to 64-bit layout.
         NOTE: Steps must be done in this order to avoid data loss. */
      clipbuf->cb_nsec = clipbuf->ts.tv_nsec;
      clipbuf->cb_sec  = clipbuf->ts.tv_sec;
#endif
      clipbuf->cb_size = len;
      memcpy (&clipbuf[1], buf, len); // append user-supplied data

      GlobalUnlock (hmem);
      EmptyClipboard ();
      HANDLE ret = SetClipboardData (cygnativeformat, hmem);
      CloseClipboard ();
      /* According to MSDN, hmem must not be free'd after transferring the
	 data to the clipboard via SetClipboardData. */
      /* GlobalFree (hmem); */
      if (!ret)
	{
	  __seterrno ();
	  return -1;
	}
    }

  /* CF_TEXT/CF_OEMTEXT for copying to wordpad and the like */
  len = sys_mbstowcs (NULL, 0, (const char *) buf, len);
  if (!len)
    {
      set_errno (EILSEQ);
      return -1;
    }
  if (OpenClipboard (NULL))
    {
      PWCHAR clipbuf;

      hmem = GlobalAlloc (GMEM_MOVEABLE, (len + 1) * sizeof (WCHAR));
      if (!hmem)
	{
	  __seterrno ();
	  CloseClipboard ();
	  return -1;
	}
      clipbuf = (PWCHAR) GlobalLock (hmem);
      sys_mbstowcs (clipbuf, len + 1, (const char *) buf);
      GlobalUnlock (hmem);
      HANDLE ret = SetClipboardData (CF_UNICODETEXT, hmem);
      CloseClipboard ();
      /* According to MSDN, hmem must not be free'd after transferring the
	 data to the clipboard via SetClipboardData. */
      /* GlobalFree (hmem); */
      if (!ret)
	{
	  __seterrno ();
	  return -1;
	}
    }
  return 0;
}

/* FIXME: arbitrary seeking is not handled */
ssize_t __stdcall
fhandler_dev_clipboard::write (const void *buf, size_t len)
{
  /* write to our membuffer */
  size_t cursize = msize;
  void *tempbuffer = realloc (membuffer, cursize + len);
  if (!tempbuffer)
    {
      debug_printf ("Couldn't realloc() clipboard buffer for write");
      return -1;
    }
  membuffer = tempbuffer;
  msize = cursize + len;
  memcpy ((unsigned char *) membuffer + cursize, buf, len);

  /* now pass to windows */
  if (set_clipboard (membuffer, msize))
    {
      /* FIXME: membuffer is now out of sync with pos, but msize
		is used above */
      return -1;
    }

  pos = msize;
  return len;
}

int __reg2
fhandler_dev_clipboard::fstat (struct stat *buf)
{
  buf->st_mode = S_IFCHR | STD_RBITS | STD_WBITS | S_IWGRP | S_IWOTH;
  buf->st_uid = geteuid32 ();
  buf->st_gid = getegid32 ();
  buf->st_nlink = 1;
  buf->st_blksize = PREFERRED_IO_BLKSIZE;

  buf->st_ctim.tv_sec = 1164931200L;	/* Arbitrary value: 2006-12-01 */
  buf->st_ctim.tv_nsec = 0L;
  buf->st_birthtim = buf->st_atim = buf->st_mtim = buf->st_ctim;

  if (OpenClipboard (NULL))
    {
      UINT formatlist[1] = { cygnativeformat };
      int format;
      HGLOBAL hglb;
      cygcb_t *clipbuf;

      if ((format = GetPriorityClipboardFormat (formatlist, 1)) > 0
	  && (hglb = GetClipboardData (format))
	  && (clipbuf = (cygcb_t *) GlobalLock (hglb)))
	{
#ifdef __x86_64__
	  /* ts overlays cb_sec and cb_nsec such that no conversion is needed */
#elif __i386__
	  /* Compress 64-bit timespec layout to 32-bit layout.
	     NOTE: Steps must be done in this order to avoid data loss. */
	  clipbuf->ts.tv_sec  = clipbuf->cb_sec;
	  clipbuf->ts.tv_nsec = clipbuf->cb_nsec;
#endif
	  buf->st_atim = buf->st_mtim = clipbuf->ts;
	  buf->st_size = clipbuf->cb_size;
	  GlobalUnlock (hglb);
	}
      CloseClipboard ();
    }

  return 0;
}

void __reg3
fhandler_dev_clipboard::read (void *ptr, size_t& len)
{
  HGLOBAL hglb;
  size_t ret = 0;
  UINT formatlist[2];
  UINT format;
  LPVOID cb_data;
  int rach;

  if (!OpenClipboard (NULL))
    {
      len = 0;
      return;
    }
  formatlist[0] = cygnativeformat;
  formatlist[1] = CF_UNICODETEXT;
  if ((format = GetPriorityClipboardFormat (formatlist, 2)) <= 0
      || !(hglb = GetClipboardData (format))
      || !(cb_data = GlobalLock (hglb)))
    {
      CloseClipboard ();
      len = 0;
      return;
    }
  if (format == cygnativeformat)
    {
      cygcb_t *clipbuf = (cygcb_t *) cb_data;

      if (pos < (off_t) clipbuf->cb_size)
	{
	  ret = (len > (clipbuf->cb_size - pos)) ? clipbuf->cb_size - pos : len;
	  memcpy (ptr, (char *) (clipbuf + 1) + pos, ret);
	  pos += ret;
	}
    }
  else if ((rach = get_readahead ()) >= 0)
    {
      /* Deliver from read-ahead buffer. */
      char * out_ptr = (char *) ptr;
      * out_ptr++ = rach;
      ret = 1;
      while (ret < len && (rach = get_readahead ()) >= 0)
	{
	  * out_ptr++ = rach;
	  ret++;
	}
    }
  else
    {
      wchar_t *buf = (wchar_t *) cb_data;

      size_t glen = GlobalSize (hglb) / sizeof (WCHAR) - 1;
      if (pos < (off_t) glen)
	{
	  /* If caller's buffer is too small to hold at least one
	     max-size character, redirect algorithm to local
	     read-ahead buffer, finally fill class read-ahead buffer
	     with result and feed caller from there. */
	  char *conv_ptr = (char *) ptr;
	  size_t conv_len = len;
#define cprabuf_len MB_LEN_MAX	/* max MB_CUR_MAX of all encodings */
	  char cprabuf [cprabuf_len];
	  if (len < cprabuf_len)
	    {
	      conv_ptr = cprabuf;
	      conv_len = cprabuf_len;
	    }

	  /* Comparing apples and oranges here, but the below loop could become
	     extremly slow otherwise.  We rather return a few bytes less than
	     possible instead of being even more slow than usual... */
	  if (glen > pos + conv_len)
	    glen = pos + conv_len;
	  /* This loop is necessary because the number of bytes returned by
	     sys_wcstombs does not indicate the number of wide chars used for
	     it, so we could potentially drop wide chars. */
	  while ((ret = sys_wcstombs (NULL, 0, buf + pos, glen - pos))
		  != (size_t) -1
		 && (ret > conv_len
			/* Skip separated high surrogate: */
		     || ((buf [glen - 1] & 0xFC00) == 0xD800
			 && glen - pos > 1)))
	     --glen;
	  if (ret == (size_t) -1)
	    ret = 0;
	  else
	    {
	      ret = sys_wcstombs ((char *) conv_ptr, (size_t) -1,
				  buf + pos, glen - pos);
	      pos = glen;
	      /* If using read-ahead buffer, copy to class read-ahead buffer
	         and deliver first byte. */
	      if (conv_ptr == cprabuf)
		{
		  puts_readahead (cprabuf, ret);
		  char *out_ptr = (char *) ptr;
		  ret = 0;
		  while (ret < len && (rach = get_readahead ()) >= 0)
		    {
		      * out_ptr++ = rach;
		      ret++;
		    }
		}
	    }
	}
    }
  GlobalUnlock (hglb);
  CloseClipboard ();
  len = ret;
}

off_t
fhandler_dev_clipboard::lseek (off_t offset, int whence)
{
  /* On reads we check this at read time, not seek time.
   * On writes we use this to decide how to write - empty and write, or open, copy, empty
   * and write
   */
  pos = offset;
  /* treat seek like rewind */
  if (membuffer)
    {
      free (membuffer);
      membuffer = NULL;
    }
  msize = 0;
  return 0;
}

int
fhandler_dev_clipboard::close ()
{
  if (!have_execed)
    {
      pos = msize = 0;
      if (membuffer)
	{
	  free (membuffer);
	  membuffer = NULL;
	}
    }
  return fhandler_base::close ();
}

void
fhandler_dev_clipboard::fixup_after_exec ()
{
  if (!close_on_exec ())
    {
      pos = msize = 0;
      membuffer = NULL;
    }
}