1 /* 16-bit Windows Selection processing for emacs on MS-Windows
3 Copyright (C) 1996, 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 2008, 2009, 2010 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* These functions work by using WinOldAp interface. WinOldAp
22 (WINOLDAP.MOD) is a Microsoft Windows extension supporting
23 "old" (character-mode) application access to Dynamic Data Exchange,
24 menus, and the Windows clipboard. */
26 /* Written by Dale P. Smith <dpsm@en.com> */
27 /* Adapted to DJGPP by Eli Zaretskii <eliz@gnu.org> */
34 #include <sys/farptr.h>
37 #include "dispextern.h" /* frame.h seems to want this */
38 #include "frame.h" /* Need this to get the X window of selected_frame */
39 #include "blockinput.h"
41 #include "character.h"
43 #include "composite.h"
45 /* If ever some function outside this file will need to call any
46 clipboard-related function, the following prototypes and constants
47 should be put on a header file. Right now, nobody else uses them. */
50 #define CF_BITMAP 0x02
51 #define CF_METAFILE 0x03
55 #define CF_OEMTEXT 0x07
56 #define CF_DIBBITMAP 0x08
57 #define CF_WINWRITE 0x80
58 #define CF_DSPTEXT 0x81
59 #define CF_DSPBITMAP 0x82
61 unsigned identify_winoldap_version (void);
62 unsigned open_clipboard (void);
63 unsigned empty_clipboard (void);
64 unsigned set_clipboard_data (unsigned, void *, unsigned, int);
65 unsigned get_clipboard_data_size (unsigned);
66 unsigned get_clipboard_data (unsigned, void *, unsigned, int);
67 unsigned close_clipboard (void);
68 unsigned clipboard_compact (unsigned);
70 Lisp_Object QCLIPBOARD
, QPRIMARY
;
72 /* Coding system for communicating with other Windows programs via the
74 static Lisp_Object Vselection_coding_system
;
76 /* Coding system for the next communicating with other Windows programs. */
77 static Lisp_Object Vnext_selection_coding_system
;
79 /* The segment address and the size of the buffer in low
80 memory used to move data between us and WinOldAp module. */
83 unsigned short rm_segment
;
84 } clipboard_xfer_buf_info
;
86 /* The last text we put into the clipboard. This is used to prevent
87 passing back our own text from the clipboard, instead of using the
88 kill ring. The former is undesirable because the clipboard data
89 could be MULEtilated by inappropriately chosen
90 (next-)selection-coding-system. For this reason, we must store the
91 text *after* it was encoded/Unix-to-DOS-converted. */
92 static unsigned char *last_clipboard_text
;
94 /* The size of allocated storage for storing the clipboard data. */
95 static size_t clipboard_storage_size
;
97 /* C functions to access the Windows 3.1x clipboard from DOS apps.
99 The information was obtained from the Microsoft Knowledge Base,
100 article Q67675 and can be found at:
101 http://www.microsoft.com/kb/developr/win_dk/q67675.htm */
103 /* See also Ralf Brown's Interrupt List.
105 I also seem to remember reading about this in Dr. Dobbs Journal a
106 while ago, but if you knew my memory... :-)
108 Dale P. Smith <dpsm@en.com> */
110 /* Return the WinOldAp support version, or 0x1700 if not supported. */
112 identify_winoldap_version (void)
116 /* Calls Int 2Fh/AX=1700h
117 Return Values AX == 1700H: Clipboard functions not available
118 <> 1700H: AL = Major version number
119 AH = Minor version number */
121 __dpmi_int(0x2f, ®s
);
125 /* Open the clipboard, return non-zero if successfull. */
127 open_clipboard (void)
131 /* Is WINOLDAP supported? */
132 /* Kludge alert!! If WinOldAp is not supported, we return a 0,
133 which is the same as ``Clipboard already open''. Currently,
134 this is taken as an error by all the functions that use
135 `open_clipboard', but if somebody someday will use that ``open''
136 clipboard, they will have interesting time debugging it... */
137 if (identify_winoldap_version () == 0x1700)
140 /* Calls Int 2Fh/AX=1701h
141 Return Values AX == 0: Clipboard already open
142 <> 0: Clipboard opened */
144 __dpmi_int(0x2f, ®s
);
148 /* Empty clipboard, return non-zero if successfull. */
150 empty_clipboard (void)
154 /* Calls Int 2Fh/AX=1702h
155 Return Values AX == 0: Error occurred
156 <> 0: OK, Clipboard emptied */
158 __dpmi_int(0x2f, ®s
);
162 /* Ensure we have a buffer in low memory with enough memory for data
163 of size WANT_SIZE. Return the linear address of the buffer. */
165 alloc_xfer_buf (unsigned want_size
)
169 /* If the usual DJGPP transfer buffer is large enough, use that. */
170 if (want_size
<= _go32_info_block
.size_of_transfer_buffer
)
171 return __tb
& 0xfffff;
173 /* Don't even try to allocate more than 1MB of memory: DOS cannot
174 possibly handle that (it will overflow the BX register below). */
175 if (want_size
> 0xfffff)
178 /* Need size rounded up to the nearest paragraph, and in
179 paragraph units (1 paragraph = 16 bytes). */
180 clipboard_xfer_buf_info
.size
= (want_size
+ 15) >> 4;
182 /* The NT DPMI host crashes us if we free DOS memory via the
183 DPMI service. Work around by calling DOS allocate/free block. */
185 regs
.x
.bx
= clipboard_xfer_buf_info
.size
;
186 __dpmi_int (0x21, ®s
);
187 if (regs
.x
.flags
& 1)
189 clipboard_xfer_buf_info
.size
= 0;
193 clipboard_xfer_buf_info
.rm_segment
= regs
.x
.ax
;
194 return (((int)clipboard_xfer_buf_info
.rm_segment
) << 4) & 0xfffff;
197 /* Free our clipboard buffer. We always free it after use, because
198 keeping it leaves less free conventional memory for subprocesses.
199 The clipboard buffer tends to be large in size, because for small
200 clipboard data sizes we use the DJGPP transfer buffer. */
204 /* If the size is 0, we used DJGPP transfer buffer, so don't free. */
205 if (clipboard_xfer_buf_info
.size
)
209 /* The NT DPMI host crashes us if we free DOS memory via
210 the DPMI service. Work around by calling DOS free block. */
212 regs
.x
.es
= clipboard_xfer_buf_info
.rm_segment
;
213 __dpmi_int (0x21, ®s
);
214 clipboard_xfer_buf_info
.size
= 0;
218 /* Copy data into the clipboard, return zero if successfull. */
220 set_clipboard_data (unsigned Format
, void *Data
, unsigned Size
, int Raw
)
224 unsigned long xbuf_addr
, buf_offset
;
225 unsigned char *dp
= Data
, *dstart
= dp
;
227 if (Format
!= CF_OEMTEXT
)
230 /* need to know final size after '\r' chars are inserted (the
231 standard CF_OEMTEXT clipboard format uses CRLF line endings,
232 while Emacs uses just LF internally). */
233 truelen
= Size
+ 1; /* +1 for the terminating null */
237 /* avoid using strchr because it recomputes the length everytime */
238 while ((dp
= memchr (dp
, '\n', Size
- (dp
- dstart
))) != 0)
245 if (clipboard_compact (truelen
) < truelen
)
248 if ((xbuf_addr
= alloc_xfer_buf (truelen
)) == 0)
251 /* Move the buffer into the low memory, convert LF into CR-LF if needed. */
254 dosmemput (Data
, Size
, xbuf_addr
);
256 /* Terminate with a null, otherwise Windows does strange things
257 when the text size is an integral multiple of 32 bytes. */
258 _farpokeb (_dos_ds
, xbuf_addr
+ Size
, '\0');
263 buf_offset
= xbuf_addr
;
264 _farsetsel (_dos_ds
);
267 /* Don't allow them to put binary data into the clipboard, since
268 it will cause yanked data to be truncated at the first null. */
272 _farnspokeb (buf_offset
++, '\r');
273 _farnspokeb (buf_offset
++, *dp
++);
276 /* Terminate with a null, otherwise Windows does strange things
277 when the text size is an integral multiple of 32 bytes. */
278 _farnspokeb (buf_offset
, '\0');
281 /* Stash away the data we are about to put into the clipboard, so we
282 could later check inside get_clipboard_data whether the clipboard
283 still holds our data. */
284 if (clipboard_storage_size
< truelen
)
286 clipboard_storage_size
= truelen
+ 100;
287 last_clipboard_text
=
288 (char *) xrealloc (last_clipboard_text
, clipboard_storage_size
);
290 if (last_clipboard_text
)
291 dosmemget (xbuf_addr
, truelen
, last_clipboard_text
);
293 /* Calls Int 2Fh/AX=1703h with:
294 DX = WinOldAp-Supported Clipboard format
295 ES:BX = Pointer to data
296 SI:CX = Size of data in bytes
297 Return Values AX == 0: Error occurred
298 <> 0: OK. Data copied into the Clipboard. */
301 regs
.x
.si
= truelen
>> 16;
302 regs
.x
.cx
= truelen
& 0xffff;
303 regs
.x
.es
= xbuf_addr
>> 4;
304 regs
.x
.bx
= xbuf_addr
& 15;
305 __dpmi_int(0x2f, ®s
);
309 /* If the above failed, invalidate the local copy of the clipboard. */
311 *last_clipboard_text
= '\0';
313 /* Zero means success, otherwise (1, 2, or 3) it's an error. */
314 return regs
.x
.ax
> 0 ? 0 : 3;
317 /* Return the size of the clipboard data of format FORMAT. */
319 get_clipboard_data_size (unsigned Format
)
323 /* Calls Int 2Fh/AX=1704h with:
324 DX = WinOldAp-Supported Clipboard format
325 Return Values DX:AX == Size of the data in bytes, including any
327 == 0 If data in this format is not in
331 __dpmi_int(0x2f, ®s
);
332 return ( (((unsigned)regs
.x
.dx
) << 16) | regs
.x
.ax
);
335 /* Get clipboard data, return its length.
336 Warning: this doesn't check whether DATA has enough space to hold
339 get_clipboard_data (unsigned Format
, void *Data
, unsigned Size
, int Raw
)
342 unsigned long xbuf_addr
;
343 unsigned char *dp
= Data
;
345 if (Format
!= CF_OEMTEXT
)
351 if ((xbuf_addr
= alloc_xfer_buf (Size
)) == 0)
354 /* Calls Int 2Fh/AX=1705h with:
355 DX = WinOldAp-Supported Clipboard format
356 ES:BX = Pointer to data buffer to hold data
357 Return Values AX == 0: Error occurred (or data in this format is not
362 regs
.x
.es
= xbuf_addr
>> 4;
363 regs
.x
.bx
= xbuf_addr
& 15;
364 __dpmi_int(0x2f, ®s
);
367 unsigned char null_char
= '\0';
368 unsigned long xbuf_beg
= xbuf_addr
;
370 /* If last_clipboard_text is NULL, we don't want to slow down
371 the next loop by an additional test. */
372 register unsigned char *lcdp
=
373 last_clipboard_text
== NULL
? &null_char
: last_clipboard_text
;
375 /* Copy data from low memory, remove CR
376 characters before LF if needed. */
377 _farsetsel (_dos_ds
);
380 register unsigned char c
= _farnspeekb (xbuf_addr
++);
385 if ((*dp
++ = c
) == '\r' && !Raw
&& _farnspeekb (xbuf_addr
) == '\n')
393 /* Windows reportedly rounds up the size of clipboard data
394 (passed in SIZE) to a multiple of 32, and removes trailing
395 spaces from each line without updating SIZE. We therefore
396 bail out when we see the first null character. */
401 /* If the text in clipboard is identical to what we put there
402 last time set_clipboard_data was called, pretend there's no
403 data in the clipboard. This is so we don't pass our own text
404 from the clipboard (which might be troublesome if the killed
405 text includes null characters). */
406 if (last_clipboard_text
&&
407 xbuf_addr
- xbuf_beg
== (long)(lcdp
- last_clipboard_text
))
408 dp
= (unsigned char *)Data
+ 1;
413 return (unsigned) (dp
- (unsigned char *)Data
- 1);
416 /* Close clipboard, return non-zero if successfull. */
418 close_clipboard (void)
422 /* Calls Int 2Fh/AX=1708h
423 Return Values AX == 0: Error occurred
426 __dpmi_int(0x2f, ®s
);
430 /* Compact clipboard data so that at least SIZE bytes is available. */
432 clipboard_compact (unsigned Size
)
436 /* Calls Int 2Fh/AX=1709H with:
437 SI:CX = Desired memory size in bytes.
438 Return Values DX:AX == Number of bytes of largest block of free memory.
439 == 0 if error or no memory */
441 regs
.x
.si
= Size
>> 16;
442 regs
.x
.cx
= Size
& 0xffff;
443 __dpmi_int(0x2f, ®s
);
444 return ((unsigned)regs
.x
.dx
<< 16) | regs
.x
.ax
;
447 static char no_mem_msg
[] =
448 "(Not enough DOS memory to put saved text into clipboard.)";
449 static char binary_msg
[] =
450 "(Binary characters in saved text; clipboard data not set.)";
451 static char system_error_msg
[] =
452 "(Clipboard interface failure; clipboard data not set.)";
454 DEFUN ("w16-set-clipboard-data", Fw16_set_clipboard_data
, Sw16_set_clipboard_data
, 1, 2, 0,
455 doc
: /* This sets the clipboard data to the given text. */)
456 (Lisp_Object string
, Lisp_Object frame
)
458 unsigned ok
= 1, put_status
= 0;
459 int nbytes
, no_crlf_conversion
;
460 unsigned char *src
, *dst
= NULL
;
462 CHECK_STRING (string
);
465 frame
= Fselected_frame ();
467 CHECK_LIVE_FRAME (frame
);
468 if ( !FRAME_MSDOS_P (XFRAME (frame
)))
473 if (!open_clipboard ())
476 nbytes
= SBYTES (string
);
477 src
= SDATA (string
);
479 /* Do we need to encode this text? */
480 for (dst
= src
; dst
< src
+ nbytes
; dst
++)
482 if (*dst
== '\0' || *dst
>= 0x80)
485 if (dst
>= src
+ nbytes
)
487 /* No multibyte characters in text. We need not encode it, but we
488 will have to convert it to DOS CR-LF style. */
489 no_crlf_conversion
= 0;
490 Vlast_coding_system_used
= Qraw_text
;
491 dst
= NULL
; /* so we don't try to free a random pointer */
495 /* We must encode contents of STRING according to what
496 clipboard-coding-system specifies. */
497 struct coding_system coding
;
498 Lisp_Object coding_system
=
499 NILP (Vnext_selection_coding_system
) ?
500 Vselection_coding_system
: Vnext_selection_coding_system
;
502 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
503 coding
.dst_bytes
= nbytes
* 4;
504 coding
.destination
= (unsigned char *) xmalloc (coding
.dst_bytes
);
505 Vnext_selection_coding_system
= Qnil
;
506 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
507 dst
= coding
.destination
;
508 encode_coding_object (&coding
, string
, 0, 0,
509 SCHARS (string
), nbytes
, Qnil
);
510 no_crlf_conversion
= 1;
511 nbytes
= coding
.produced
;
512 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
516 ok
= empty_clipboard ()
518 = set_clipboard_data (CF_OEMTEXT
, src
, nbytes
, no_crlf_conversion
))
521 if (!no_crlf_conversion
)
524 if (ok
) goto unblock
;
534 /* Notify user if the text is too large to fit into DOS memory.
535 (This will happen somewhere after 600K bytes (470K in DJGPP v1.x),
536 depending on user system configuration.) If we just silently
537 fail the function, people might wonder why their text sometimes
538 doesn't make it to the clipboard. */
544 message2 (no_mem_msg
, sizeof (no_mem_msg
) - 1, 0);
547 message2 (binary_msg
, sizeof (binary_msg
) - 1, 0);
550 message2 (system_error_msg
, sizeof (system_error_msg
) - 1, 0);
553 sit_for (make_number (2), 0, 2);
558 return (ok
&& put_status
== 0 ? string
: Qnil
);
561 DEFUN ("w16-get-clipboard-data", Fw16_get_clipboard_data
, Sw16_get_clipboard_data
, 0, 1, 0,
562 doc
: /* This gets the clipboard data in text format. */)
565 unsigned data_size
, truelen
;
566 unsigned char *htext
= NULL
;
567 Lisp_Object ret
= Qnil
;
568 int require_decoding
= 0;
571 frame
= Fselected_frame ();
573 CHECK_LIVE_FRAME (frame
);
574 if ( !FRAME_MSDOS_P (XFRAME (frame
)))
579 if (!open_clipboard ())
582 if ((data_size
= get_clipboard_data_size (CF_OEMTEXT
)) == 0 ||
583 (htext
= (unsigned char *)xmalloc (data_size
)) == 0)
586 /* need to know final size after '\r' chars are removed because
587 we can't change the string size manually, and doing an extra
589 if ((truelen
= get_clipboard_data (CF_OEMTEXT
, htext
, data_size
, 0)) == 0)
592 /* Do we need to decode it? */
594 /* If the clipboard data contains any 8-bit Latin-1 code, we
595 need to decode it. */
598 for (i
= 0; i
< truelen
; i
++)
600 if (htext
[i
] >= 0x80)
602 require_decoding
= 1;
607 if (require_decoding
)
609 struct coding_system coding
;
610 Lisp_Object coding_system
= Vnext_selection_coding_system
;
612 truelen
= get_clipboard_data (CF_OEMTEXT
, htext
, data_size
, 1);
613 if (NILP (coding_system
))
614 coding_system
= Vselection_coding_system
;
615 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
616 coding
.source
= htext
;
617 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
618 /* We explicitly disable composition handling because selection
619 data should not contain any composition sequence. */
620 coding
.mode
&= CODING_ANNOTATION_MASK
;
621 decode_coding_object (&coding
, Qnil
, 0, 0, truelen
, truelen
, Qt
);
622 ret
= coding
.dst_object
;
623 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
627 ret
= make_unibyte_string ((char *) htext
, truelen
);
628 Vlast_coding_system_used
= Qraw_text
;
632 Vnext_selection_coding_system
= Qnil
;
645 /* Support checking for a clipboard selection. */
647 DEFUN ("x-selection-exists-p", Fx_selection_exists_p
, Sx_selection_exists_p
,
649 doc
: /* Whether there is an owner for the given X Selection.
650 The arg should be the name of the selection in question, typically one of
651 the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
652 \(Those are literal upper-case symbol names, since that's what X expects.)
653 For convenience, the symbol nil is the same as `PRIMARY',
654 and t is the same as `SECONDARY'. */)
655 (Lisp_Object selection
)
657 CHECK_SYMBOL (selection
);
659 /* Return nil for SECONDARY selection. For PRIMARY (or nil)
660 selection, check if there is some text on the kill-ring;
661 for CLIPBOARD, check if the clipboard currently has valid
662 text format contents.
664 The test for killed text on the kill-ring emulates the Emacs
665 behavior on X, where killed text is also put into X selection
666 by the X interface code. (On MSDOS, killed text is only put
667 into the clipboard if we run under Windows, so we cannot check
668 the clipboard alone.) */
669 if ((EQ (selection
, Qnil
) || EQ (selection
, QPRIMARY
))
670 && ! NILP (Fsymbol_value (Fintern_soft (build_string ("kill-ring"),
674 if (EQ (selection
, QCLIPBOARD
))
676 Lisp_Object val
= Qnil
;
678 if (open_clipboard ())
680 if (get_clipboard_data_size (CF_OEMTEXT
))
690 syms_of_win16select (void)
692 defsubr (&Sw16_set_clipboard_data
);
693 defsubr (&Sw16_get_clipboard_data
);
694 defsubr (&Sx_selection_exists_p
);
696 DEFVAR_LISP ("selection-coding-system", &Vselection_coding_system
,
697 doc
: /* Coding system for communicating with other programs.
699 For MS-Windows and MS-DOS:
700 When sending or receiving text via selection and clipboard, the text
701 is encoded or decoded by this coding system. The default value is
702 the current system default encoding on 9x/Me, `utf-16le-dos'
703 \(Unicode) on NT/W2K/XP, and `iso-latin-1-dos' on MS-DOS.
706 When sending text via selection and clipboard, if the target
707 data-type matches with the type of this coding system, it is used
708 for encoding the text. Otherwise (including the case that this
709 variable is nil), a proper coding system is used as below:
711 data-type coding system
712 --------- -------------
714 COMPOUND_TEXT compound-text-with-extensions
716 C_STRING no-conversion
718 When receiving text, if this coding system is non-nil, it is used
719 for decoding regardless of the data-type. If this is nil, a
720 proper coding system is used according to the data-type as above.
722 See also the documentation of the variable `x-select-request-type' how
723 to control which data-type to request for receiving text.
725 The default value is nil. */);
726 Vselection_coding_system
= intern ("iso-latin-1-dos");
728 DEFVAR_LISP ("next-selection-coding-system", &Vnext_selection_coding_system
,
729 doc
: /* Coding system for the next communication with other programs.
730 Usually, `selection-coding-system' is used for communicating with
731 other programs (X Windows clients or MS Windows programs). But, if this
732 variable is set, it is used for the next communication only.
733 After the communication, this variable is set to nil. */);
734 Vnext_selection_coding_system
= Qnil
;
736 QPRIMARY
= intern ("PRIMARY"); staticpro (&QPRIMARY
);
737 QCLIPBOARD
= intern ("CLIPBOARD"); staticpro (&QCLIPBOARD
);