1 /* fhandler_dev_clipboard: code to access /dev/clipboard
3 Copyright 2000, 2001 Red Hat, Inc
5 Written by Charles Wilson (cwilson@ece.gatech.edu)
7 This file is part of Cygwin.
9 This software is a copyrighted work licensed under the terms of the
10 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
27 * FIXME: should we use GetClipboardSequenceNumber to tell if the clipboard has
28 * changed? How does /dev/clipboard operate under (say) linux?
31 static const NO_COPY char *CYGWIN_NATIVE = "CYGWIN_NATIVE_CLIPBOARD";
32 /* this is MT safe because windows format id's are atomic */
33 static UINT cygnativeformat;
35 fhandler_dev_clipboard::fhandler_dev_clipboard (const char *name):
36 fhandler_base (FH_CLIPBOARD, name)
38 set_cb (sizeof *this);
43 /* FIXME: check for errors and loop until we can open the clipboard */
45 cygnativeformat = RegisterClipboardFormat (CYGWIN_NATIVE);
50 * Special clipboard dup to duplicate input and output
55 fhandler_dev_clipboard::dup (fhandler_base * child)
57 fhandler_dev_clipboard *fhc = (fhandler_dev_clipboard *) child;
59 if (!fhc->open (get_name (), get_flags (), 0))
60 system_printf ("error opening clipboard, %E");
62 fhc->membuffer = membuffer;
70 fhandler_dev_clipboard::open (const char *, int flags, mode_t)
79 cygnativeformat = RegisterClipboardFormat (CYGWIN_NATIVE);
85 set_clipboard (const void *buf, size_t len)
88 unsigned char *clipbuf;
89 /* Native CYGWIN format */
91 hmem = GlobalAlloc (GMEM_MOVEABLE, len + sizeof (size_t));
94 system_printf ("Couldn't allocate global buffer for write\n");
97 clipbuf = (unsigned char *) GlobalLock (hmem);
98 memcpy (clipbuf + sizeof (size_t), buf, len);
99 *(size_t *) (clipbuf) = len;
102 if (!cygnativeformat)
103 cygnativeformat = RegisterClipboardFormat (CYGWIN_NATIVE);
104 if (!SetClipboardData (cygnativeformat, hmem))
107 ("Couldn't write native format to the clipboard %04x %x\n",
108 cygnativeformat, hmem);
109 /* FIXME: return an appriate error code &| set_errno(); */
113 if (GlobalFree (hmem))
116 ("Couldn't free global buffer after write to the clipboard\n");
117 /* FIXME: return an appriate error code &| set_errno(); */
121 /* CF_TEXT/CF_OEMTEXT for copying to wordpad and the like */
124 hmem = GlobalAlloc (GMEM_MOVEABLE, len + 2);
127 system_printf ("Couldn't allocate global buffer for write\n");
130 clipbuf = (unsigned char *) GlobalLock (hmem);
131 memcpy (clipbuf, buf, len);
132 *(clipbuf + len) = '\0';
133 *(clipbuf + len + 1) = '\0';
135 if (!SetClipboardData
136 ((current_codepage == ansi_cp ? CF_TEXT : CF_OEMTEXT), hmem))
138 system_printf ("Couldn't write to the clipboard\n");
139 /* FIXME: return an appriate error code &| set_errno(); */
143 if (GlobalFree (hmem))
146 ("Couldn't free global buffer after write to the clipboard\n");
147 /* FIXME: return an appriate error code &| set_errno(); */
152 /* FIXME: arbitrary seeking is not handled */
154 fhandler_dev_clipboard::write (const void *buf, size_t len)
158 /* write to our membuffer */
159 size_t cursize = msize;
160 void *tempbuffer = realloc (membuffer, cursize + len);
163 system_printf ("Couldn't realloc() clipboard buffer for write\n");
166 membuffer = tempbuffer;
167 msize = cursize + len;
168 memcpy ((unsigned char *) membuffer + cursize, buf, len);
170 /* now pass to windows */
171 if (set_clipboard (membuffer, msize))
173 /* FIXME: membuffer is now out of sync with pos, but msize is used above */
186 /* FIXME: return 0 bytes written, file not open */
192 fhandler_dev_clipboard::read (void *ptr, size_t len)
200 formatlist[0] = cygnativeformat;
201 formatlist[1] = current_codepage == ansi_cp ? CF_TEXT : CF_OEMTEXT;
203 if ((format = GetPriorityClipboardFormat (formatlist, 2)) > 0)
205 hglb = GetClipboardData (format);
206 if (format == cygnativeformat)
208 unsigned char *buf = (unsigned char *) GlobalLock (hglb);
209 size_t buflen = (*(size_t *) buf);
210 ret = ((len > (buflen - pos)) ? (buflen - pos) : len);
211 memcpy (ptr, buf + sizeof (size_t)+ pos , ret);
213 if (pos + len - ret >= buflen)
220 lpstr = (LPSTR) GlobalLock (hglb);
223 ((len > (strlen (lpstr) - pos)) ? (strlen (lpstr) - pos) :
226 memcpy (ptr, lpstr + pos, ret);
227 //ret = snprintf((char *) ptr, len, "%s", lpstr);//+pos);
229 if (pos + len - ret >= strlen (lpstr))
241 system_printf ("a non-accepted format! %d\n", format);
254 fhandler_dev_clipboard::lseek (off_t offset, int whence)
256 /* On reads we check this at read time, not seek time.
257 * On writes we use this to decide how to write - empty and write, or open, copy, empty
261 /* treat seek like rewind */
269 fhandler_dev_clipboard::close (void)
280 fhandler_dev_clipboard::dump ()
282 paranoid_printf ("here, fhandler_dev_clipboard");