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, 2011 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 /* The segment address and the size of the buffer in low
73 memory used to move data between us and WinOldAp module. */
76 unsigned short rm_segment
;
77 } clipboard_xfer_buf_info
;
79 /* The last text we put into the clipboard. This is used to prevent
80 passing back our own text from the clipboard, instead of using the
81 kill ring. The former is undesirable because the clipboard data
82 could be MULEtilated by inappropriately chosen
83 (next-)selection-coding-system. For this reason, we must store the
84 text *after* it was encoded/Unix-to-DOS-converted. */
85 static unsigned char *last_clipboard_text
;
87 /* The size of allocated storage for storing the clipboard data. */
88 static size_t clipboard_storage_size
;
90 /* C functions to access the Windows 3.1x clipboard from DOS apps.
92 The information was obtained from the Microsoft Knowledge Base,
93 article Q67675 and can be found at:
94 http://www.microsoft.com/kb/developr/win_dk/q67675.htm */
96 /* See also Ralf Brown's Interrupt List.
98 I also seem to remember reading about this in Dr. Dobbs Journal a
99 while ago, but if you knew my memory... :-)
101 Dale P. Smith <dpsm@en.com> */
103 /* Return the WinOldAp support version, or 0x1700 if not supported. */
105 identify_winoldap_version (void)
109 /* Calls Int 2Fh/AX=1700h
110 Return Values AX == 1700H: Clipboard functions not available
111 <> 1700H: AL = Major version number
112 AH = Minor version number */
114 __dpmi_int(0x2f, ®s
);
118 /* Open the clipboard, return non-zero if successfull. */
120 open_clipboard (void)
124 /* Is WINOLDAP supported? */
125 /* Kludge alert!! If WinOldAp is not supported, we return a 0,
126 which is the same as ``Clipboard already open''. Currently,
127 this is taken as an error by all the functions that use
128 `open_clipboard', but if somebody someday will use that ``open''
129 clipboard, they will have interesting time debugging it... */
130 if (identify_winoldap_version () == 0x1700)
133 /* Calls Int 2Fh/AX=1701h
134 Return Values AX == 0: Clipboard already open
135 <> 0: Clipboard opened */
137 __dpmi_int(0x2f, ®s
);
141 /* Empty clipboard, return non-zero if successfull. */
143 empty_clipboard (void)
147 /* Calls Int 2Fh/AX=1702h
148 Return Values AX == 0: Error occurred
149 <> 0: OK, Clipboard emptied */
151 __dpmi_int(0x2f, ®s
);
155 /* Ensure we have a buffer in low memory with enough memory for data
156 of size WANT_SIZE. Return the linear address of the buffer. */
158 alloc_xfer_buf (unsigned want_size
)
162 /* If the usual DJGPP transfer buffer is large enough, use that. */
163 if (want_size
<= _go32_info_block
.size_of_transfer_buffer
)
164 return __tb
& 0xfffff;
166 /* Don't even try to allocate more than 1MB of memory: DOS cannot
167 possibly handle that (it will overflow the BX register below). */
168 if (want_size
> 0xfffff)
171 /* Need size rounded up to the nearest paragraph, and in
172 paragraph units (1 paragraph = 16 bytes). */
173 clipboard_xfer_buf_info
.size
= (want_size
+ 15) >> 4;
175 /* The NT DPMI host crashes us if we free DOS memory via the
176 DPMI service. Work around by calling DOS allocate/free block. */
178 regs
.x
.bx
= clipboard_xfer_buf_info
.size
;
179 __dpmi_int (0x21, ®s
);
180 if (regs
.x
.flags
& 1)
182 clipboard_xfer_buf_info
.size
= 0;
186 clipboard_xfer_buf_info
.rm_segment
= regs
.x
.ax
;
187 return (((int)clipboard_xfer_buf_info
.rm_segment
) << 4) & 0xfffff;
190 /* Free our clipboard buffer. We always free it after use, because
191 keeping it leaves less free conventional memory for subprocesses.
192 The clipboard buffer tends to be large in size, because for small
193 clipboard data sizes we use the DJGPP transfer buffer. */
197 /* If the size is 0, we used DJGPP transfer buffer, so don't free. */
198 if (clipboard_xfer_buf_info
.size
)
202 /* The NT DPMI host crashes us if we free DOS memory via
203 the DPMI service. Work around by calling DOS free block. */
205 regs
.x
.es
= clipboard_xfer_buf_info
.rm_segment
;
206 __dpmi_int (0x21, ®s
);
207 clipboard_xfer_buf_info
.size
= 0;
211 /* Copy data into the clipboard, return zero if successfull. */
213 set_clipboard_data (unsigned Format
, void *Data
, unsigned Size
, int Raw
)
217 unsigned long xbuf_addr
, buf_offset
;
218 unsigned char *dp
= Data
, *dstart
= dp
;
220 if (Format
!= CF_OEMTEXT
)
223 /* need to know final size after '\r' chars are inserted (the
224 standard CF_OEMTEXT clipboard format uses CRLF line endings,
225 while Emacs uses just LF internally). */
226 truelen
= Size
+ 1; /* +1 for the terminating null */
230 /* avoid using strchr because it recomputes the length everytime */
231 while ((dp
= memchr (dp
, '\n', Size
- (dp
- dstart
))) != 0)
238 if (clipboard_compact (truelen
) < truelen
)
241 if ((xbuf_addr
= alloc_xfer_buf (truelen
)) == 0)
244 /* Move the buffer into the low memory, convert LF into CR-LF if needed. */
247 dosmemput (Data
, Size
, xbuf_addr
);
249 /* Terminate with a null, otherwise Windows does strange things
250 when the text size is an integral multiple of 32 bytes. */
251 _farpokeb (_dos_ds
, xbuf_addr
+ Size
, '\0');
256 buf_offset
= xbuf_addr
;
257 _farsetsel (_dos_ds
);
260 /* Don't allow them to put binary data into the clipboard, since
261 it will cause yanked data to be truncated at the first null. */
265 _farnspokeb (buf_offset
++, '\r');
266 _farnspokeb (buf_offset
++, *dp
++);
269 /* Terminate with a null, otherwise Windows does strange things
270 when the text size is an integral multiple of 32 bytes. */
271 _farnspokeb (buf_offset
, '\0');
274 /* Stash away the data we are about to put into the clipboard, so we
275 could later check inside get_clipboard_data whether the clipboard
276 still holds our data. */
277 if (clipboard_storage_size
< truelen
)
279 clipboard_storage_size
= truelen
+ 100;
280 last_clipboard_text
=
281 (char *) xrealloc (last_clipboard_text
, clipboard_storage_size
);
283 if (last_clipboard_text
)
284 dosmemget (xbuf_addr
, truelen
, last_clipboard_text
);
286 /* Calls Int 2Fh/AX=1703h with:
287 DX = WinOldAp-Supported Clipboard format
288 ES:BX = Pointer to data
289 SI:CX = Size of data in bytes
290 Return Values AX == 0: Error occurred
291 <> 0: OK. Data copied into the Clipboard. */
294 regs
.x
.si
= truelen
>> 16;
295 regs
.x
.cx
= truelen
& 0xffff;
296 regs
.x
.es
= xbuf_addr
>> 4;
297 regs
.x
.bx
= xbuf_addr
& 15;
298 __dpmi_int(0x2f, ®s
);
302 /* If the above failed, invalidate the local copy of the clipboard. */
304 *last_clipboard_text
= '\0';
306 /* Zero means success, otherwise (1, 2, or 3) it's an error. */
307 return regs
.x
.ax
> 0 ? 0 : 3;
310 /* Return the size of the clipboard data of format FORMAT. */
312 get_clipboard_data_size (unsigned Format
)
316 /* Calls Int 2Fh/AX=1704h with:
317 DX = WinOldAp-Supported Clipboard format
318 Return Values DX:AX == Size of the data in bytes, including any
320 == 0 If data in this format is not in
324 __dpmi_int(0x2f, ®s
);
325 return ( (((unsigned)regs
.x
.dx
) << 16) | regs
.x
.ax
);
328 /* Get clipboard data, return its length.
329 Warning: this doesn't check whether DATA has enough space to hold
332 get_clipboard_data (unsigned Format
, void *Data
, unsigned Size
, int Raw
)
335 unsigned long xbuf_addr
;
336 unsigned char *dp
= Data
;
338 if (Format
!= CF_OEMTEXT
)
344 if ((xbuf_addr
= alloc_xfer_buf (Size
)) == 0)
347 /* Calls Int 2Fh/AX=1705h with:
348 DX = WinOldAp-Supported Clipboard format
349 ES:BX = Pointer to data buffer to hold data
350 Return Values AX == 0: Error occurred (or data in this format is not
355 regs
.x
.es
= xbuf_addr
>> 4;
356 regs
.x
.bx
= xbuf_addr
& 15;
357 __dpmi_int(0x2f, ®s
);
360 unsigned char null_char
= '\0';
361 unsigned long xbuf_beg
= xbuf_addr
;
363 /* If last_clipboard_text is NULL, we don't want to slow down
364 the next loop by an additional test. */
365 register unsigned char *lcdp
=
366 last_clipboard_text
== NULL
? &null_char
: last_clipboard_text
;
368 /* Copy data from low memory, remove CR
369 characters before LF if needed. */
370 _farsetsel (_dos_ds
);
373 register unsigned char c
= _farnspeekb (xbuf_addr
++);
378 if ((*dp
++ = c
) == '\r' && !Raw
&& _farnspeekb (xbuf_addr
) == '\n')
386 /* Windows reportedly rounds up the size of clipboard data
387 (passed in SIZE) to a multiple of 32, and removes trailing
388 spaces from each line without updating SIZE. We therefore
389 bail out when we see the first null character. */
394 /* If the text in clipboard is identical to what we put there
395 last time set_clipboard_data was called, pretend there's no
396 data in the clipboard. This is so we don't pass our own text
397 from the clipboard (which might be troublesome if the killed
398 text includes null characters). */
399 if (last_clipboard_text
&&
400 xbuf_addr
- xbuf_beg
== (long)(lcdp
- last_clipboard_text
))
401 dp
= (unsigned char *)Data
+ 1;
406 return (unsigned) (dp
- (unsigned char *)Data
- 1);
409 /* Close clipboard, return non-zero if successfull. */
411 close_clipboard (void)
415 /* Calls Int 2Fh/AX=1708h
416 Return Values AX == 0: Error occurred
419 __dpmi_int(0x2f, ®s
);
423 /* Compact clipboard data so that at least SIZE bytes is available. */
425 clipboard_compact (unsigned Size
)
429 /* Calls Int 2Fh/AX=1709H with:
430 SI:CX = Desired memory size in bytes.
431 Return Values DX:AX == Number of bytes of largest block of free memory.
432 == 0 if error or no memory */
434 regs
.x
.si
= Size
>> 16;
435 regs
.x
.cx
= Size
& 0xffff;
436 __dpmi_int(0x2f, ®s
);
437 return ((unsigned)regs
.x
.dx
<< 16) | regs
.x
.ax
;
440 static char no_mem_msg
[] =
441 "(Not enough DOS memory to put saved text into clipboard.)";
442 static char binary_msg
[] =
443 "(Binary characters in saved text; clipboard data not set.)";
444 static char system_error_msg
[] =
445 "(Clipboard interface failure; clipboard data not set.)";
447 DEFUN ("w16-set-clipboard-data", Fw16_set_clipboard_data
, Sw16_set_clipboard_data
, 1, 2, 0,
448 doc
: /* This sets the clipboard data to the given text. */)
449 (Lisp_Object string
, Lisp_Object frame
)
451 unsigned ok
= 1, put_status
= 0;
452 int nbytes
, no_crlf_conversion
;
453 unsigned char *src
, *dst
= NULL
;
455 CHECK_STRING (string
);
458 frame
= Fselected_frame ();
460 CHECK_LIVE_FRAME (frame
);
461 if ( !FRAME_MSDOS_P (XFRAME (frame
)))
466 if (!open_clipboard ())
469 nbytes
= SBYTES (string
);
470 src
= SDATA (string
);
472 /* Do we need to encode this text? */
473 for (dst
= src
; dst
< src
+ nbytes
; dst
++)
475 if (*dst
== '\0' || *dst
>= 0x80)
478 if (dst
>= src
+ nbytes
)
480 /* No multibyte characters in text. We need not encode it, but we
481 will have to convert it to DOS CR-LF style. */
482 no_crlf_conversion
= 0;
483 Vlast_coding_system_used
= Qraw_text
;
484 dst
= NULL
; /* so we don't try to free a random pointer */
488 /* We must encode contents of STRING according to what
489 clipboard-coding-system specifies. */
490 struct coding_system coding
;
491 Lisp_Object coding_system
=
492 NILP (Vnext_selection_coding_system
) ?
493 Vselection_coding_system
: Vnext_selection_coding_system
;
495 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
496 coding
.dst_bytes
= nbytes
* 4;
497 coding
.destination
= (unsigned char *) xmalloc (coding
.dst_bytes
);
498 Vnext_selection_coding_system
= Qnil
;
499 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
500 dst
= coding
.destination
;
501 encode_coding_object (&coding
, string
, 0, 0,
502 SCHARS (string
), nbytes
, Qnil
);
503 no_crlf_conversion
= 1;
504 nbytes
= coding
.produced
;
505 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
509 ok
= empty_clipboard ()
511 = set_clipboard_data (CF_OEMTEXT
, src
, nbytes
, no_crlf_conversion
))
514 if (!no_crlf_conversion
)
517 if (ok
) goto unblock
;
527 /* Notify user if the text is too large to fit into DOS memory.
528 (This will happen somewhere after 600K bytes (470K in DJGPP v1.x),
529 depending on user system configuration.) If we just silently
530 fail the function, people might wonder why their text sometimes
531 doesn't make it to the clipboard. */
537 message2 (no_mem_msg
, sizeof (no_mem_msg
) - 1, 0);
540 message2 (binary_msg
, sizeof (binary_msg
) - 1, 0);
543 message2 (system_error_msg
, sizeof (system_error_msg
) - 1, 0);
546 sit_for (make_number (2), 0, 2);
551 return (ok
&& put_status
== 0 ? string
: Qnil
);
554 DEFUN ("w16-get-clipboard-data", Fw16_get_clipboard_data
, Sw16_get_clipboard_data
, 0, 1, 0,
555 doc
: /* This gets the clipboard data in text format. */)
558 unsigned data_size
, truelen
;
559 unsigned char *htext
= NULL
;
560 Lisp_Object ret
= Qnil
;
561 int require_decoding
= 0;
564 frame
= Fselected_frame ();
566 CHECK_LIVE_FRAME (frame
);
567 if ( !FRAME_MSDOS_P (XFRAME (frame
)))
572 if (!open_clipboard ())
575 if ((data_size
= get_clipboard_data_size (CF_OEMTEXT
)) == 0 ||
576 (htext
= (unsigned char *)xmalloc (data_size
)) == 0)
579 /* need to know final size after '\r' chars are removed because
580 we can't change the string size manually, and doing an extra
582 if ((truelen
= get_clipboard_data (CF_OEMTEXT
, htext
, data_size
, 0)) == 0)
585 /* Do we need to decode it? */
587 /* If the clipboard data contains any 8-bit Latin-1 code, we
588 need to decode it. */
591 for (i
= 0; i
< truelen
; i
++)
593 if (htext
[i
] >= 0x80)
595 require_decoding
= 1;
600 if (require_decoding
)
602 struct coding_system coding
;
603 Lisp_Object coding_system
= Vnext_selection_coding_system
;
605 truelen
= get_clipboard_data (CF_OEMTEXT
, htext
, data_size
, 1);
606 if (NILP (coding_system
))
607 coding_system
= Vselection_coding_system
;
608 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
609 coding
.source
= htext
;
610 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
611 /* We explicitly disable composition handling because selection
612 data should not contain any composition sequence. */
613 coding
.mode
&= CODING_ANNOTATION_MASK
;
614 decode_coding_object (&coding
, Qnil
, 0, 0, truelen
, truelen
, Qt
);
615 ret
= coding
.dst_object
;
616 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
620 ret
= make_unibyte_string ((char *) htext
, truelen
);
621 Vlast_coding_system_used
= Qraw_text
;
625 Vnext_selection_coding_system
= Qnil
;
638 /* Support checking for a clipboard selection. */
640 DEFUN ("x-selection-exists-p", Fx_selection_exists_p
, Sx_selection_exists_p
,
642 doc
: /* Whether there is an owner for the given X Selection.
643 The arg should be the name of the selection in question, typically one of
644 the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
645 \(Those are literal upper-case symbol names, since that's what X expects.)
646 For convenience, the symbol nil is the same as `PRIMARY',
647 and t is the same as `SECONDARY'. */)
648 (Lisp_Object selection
)
650 CHECK_SYMBOL (selection
);
652 /* Return nil for SECONDARY selection. For PRIMARY (or nil)
653 selection, check if there is some text on the kill-ring;
654 for CLIPBOARD, check if the clipboard currently has valid
655 text format contents.
657 The test for killed text on the kill-ring emulates the Emacs
658 behavior on X, where killed text is also put into X selection
659 by the X interface code. (On MSDOS, killed text is only put
660 into the clipboard if we run under Windows, so we cannot check
661 the clipboard alone.) */
662 if ((EQ (selection
, Qnil
) || EQ (selection
, QPRIMARY
))
663 && ! NILP (Fsymbol_value (Fintern_soft (build_string ("kill-ring"),
667 if (EQ (selection
, QCLIPBOARD
))
669 Lisp_Object val
= Qnil
;
671 if (open_clipboard ())
673 if (get_clipboard_data_size (CF_OEMTEXT
))
683 syms_of_win16select (void)
685 defsubr (&Sw16_set_clipboard_data
);
686 defsubr (&Sw16_get_clipboard_data
);
687 defsubr (&Sx_selection_exists_p
);
689 DEFVAR_LISP ("selection-coding-system", Vselection_coding_system
,
690 doc
: /* Coding system for communicating with other programs.
692 For MS-Windows and MS-DOS:
693 When sending or receiving text via selection and clipboard, the text
694 is encoded or decoded by this coding system. The default value is
695 the current system default encoding on 9x/Me, `utf-16le-dos'
696 \(Unicode) on NT/W2K/XP, and `iso-latin-1-dos' on MS-DOS.
699 When sending text via selection and clipboard, if the target
700 data-type matches with the type of this coding system, it is used
701 for encoding the text. Otherwise (including the case that this
702 variable is nil), a proper coding system is used as below:
704 data-type coding system
705 --------- -------------
707 COMPOUND_TEXT compound-text-with-extensions
709 C_STRING no-conversion
711 When receiving text, if this coding system is non-nil, it is used
712 for decoding regardless of the data-type. If this is nil, a
713 proper coding system is used according to the data-type as above.
715 See also the documentation of the variable `x-select-request-type' how
716 to control which data-type to request for receiving text.
718 The default value is nil. */);
719 Vselection_coding_system
= intern ("iso-latin-1-dos");
721 DEFVAR_LISP ("next-selection-coding-system", Vnext_selection_coding_system
,
722 doc
: /* Coding system for the next communication with other programs.
723 Usually, `selection-coding-system' is used for communicating with
724 other programs (X Windows clients or MS Windows programs). But, if this
725 variable is set, it is used for the next communication only.
726 After the communication, this variable is set to nil. */);
727 Vnext_selection_coding_system
= Qnil
;
729 QPRIMARY
= intern ("PRIMARY"); staticpro (&QPRIMARY
);
730 QCLIPBOARD
= intern ("CLIPBOARD"); staticpro (&QCLIPBOARD
);