1 /* 16-bit Windows Selection processing for emacs on MS-Windows
3 Copyright (C) 1996-1997, 2001-2016 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or (at
10 your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /* These functions work by using WinOldAp interface. WinOldAp
21 (WINOLDAP.MOD) is a Microsoft Windows extension supporting
22 "old" (character-mode) application access to Dynamic Data Exchange,
23 menus, and the Windows clipboard. */
25 /* Written by Dale P. Smith <dpsm@en.com> */
26 /* Adapted to DJGPP by Eli Zaretskii <eliz@gnu.org> */
33 #include <sys/farptr.h>
35 #include "dispextern.h" /* frame.h seems to want this */
36 #include "frame.h" /* Need this to get the X window of selected_frame */
37 #include "blockinput.h"
38 #include "character.h"
41 #include "composite.h"
43 /* If ever some function outside this file will need to call any
44 clipboard-related function, the following prototypes and constants
45 should be put on a header file. Right now, nobody else uses them. */
48 #define CF_BITMAP 0x02
49 #define CF_METAFILE 0x03
53 #define CF_OEMTEXT 0x07
54 #define CF_DIBBITMAP 0x08
55 #define CF_WINWRITE 0x80
56 #define CF_DSPTEXT 0x81
57 #define CF_DSPBITMAP 0x82
59 unsigned identify_winoldap_version (void);
60 unsigned open_clipboard (void);
61 unsigned empty_clipboard (void);
62 unsigned set_clipboard_data (unsigned, void *, unsigned, int);
63 unsigned get_clipboard_data_size (unsigned);
64 unsigned get_clipboard_data (unsigned, void *, unsigned, int);
65 unsigned close_clipboard (void);
66 unsigned clipboard_compact (unsigned);
68 /* The segment address and the size of the buffer in low
69 memory used to move data between us and WinOldAp module. */
72 unsigned short rm_segment
;
73 } clipboard_xfer_buf_info
;
75 /* The last text we put into the clipboard. This is used to prevent
76 passing back our own text from the clipboard, instead of using the
77 kill ring. The former is undesirable because the clipboard data
78 could be MULEtilated by inappropriately chosen
79 (next-)selection-coding-system. For this reason, we must store the
80 text *after* it was encoded/Unix-to-DOS-converted. */
81 static unsigned char *last_clipboard_text
;
83 /* The size of allocated storage for storing the clipboard data. */
84 static size_t clipboard_storage_size
;
86 /* C functions to access the Windows 3.1x clipboard from DOS apps.
88 The information was obtained from the Microsoft Knowledge Base,
89 article Q67675 and can be found at:
90 http://www.microsoft.com/kb/developr/win_dk/q67675.htm */
92 /* See also Ralf Brown's Interrupt List.
94 I also seem to remember reading about this in Dr. Dobbs Journal a
95 while ago, but if you knew my memory... :-)
97 Dale P. Smith <dpsm@en.com> */
99 /* Return the WinOldAp support version, or 0x1700 if not supported. */
101 identify_winoldap_version (void)
105 /* Calls Int 2Fh/AX=1700h
106 Return Values AX == 1700H: Clipboard functions not available
107 <> 1700H: AL = Major version number
108 AH = Minor version number */
110 __dpmi_int (0x2f, ®s
);
114 /* Open the clipboard, return non-zero if successful. */
116 open_clipboard (void)
120 /* Is WINOLDAP supported? */
121 /* Kludge alert!! If WinOldAp is not supported, we return a 0,
122 which is the same as ``Clipboard already open''. Currently,
123 this is taken as an error by all the functions that use
124 `open_clipboard', but if somebody someday will use that ``open''
125 clipboard, they will have interesting time debugging it... */
126 if (identify_winoldap_version () == 0x1700)
129 /* Calls Int 2Fh/AX=1701h
130 Return Values AX == 0: Clipboard already open
131 <> 0: Clipboard opened */
133 __dpmi_int (0x2f, ®s
);
137 /* Empty clipboard, return non-zero if successful. */
139 empty_clipboard (void)
143 /* Calls Int 2Fh/AX=1702h
144 Return Values AX == 0: Error occurred
145 <> 0: OK, Clipboard emptied */
147 __dpmi_int (0x2f, ®s
);
151 /* Ensure we have a buffer in low memory with enough memory for data
152 of size WANT_SIZE. Return the linear address of the buffer. */
154 alloc_xfer_buf (unsigned want_size
)
158 /* If the usual DJGPP transfer buffer is large enough, use that. */
159 if (want_size
<= _go32_info_block
.size_of_transfer_buffer
)
160 return __tb
& 0xfffff;
162 /* Don't even try to allocate more than 1MB of memory: DOS cannot
163 possibly handle that (it will overflow the BX register below). */
164 if (want_size
> 0xfffff)
167 /* Need size rounded up to the nearest paragraph, and in
168 paragraph units (1 paragraph = 16 bytes). */
169 clipboard_xfer_buf_info
.size
= (want_size
+ 15) >> 4;
171 /* The NT DPMI host crashes us if we free DOS memory via the
172 DPMI service. Work around by calling DOS allocate/free block. */
174 regs
.x
.bx
= clipboard_xfer_buf_info
.size
;
175 __dpmi_int (0x21, ®s
);
176 if (regs
.x
.flags
& 1)
178 clipboard_xfer_buf_info
.size
= 0;
182 clipboard_xfer_buf_info
.rm_segment
= regs
.x
.ax
;
183 return (((int)clipboard_xfer_buf_info
.rm_segment
) << 4) & 0xfffff;
186 /* Free our clipboard buffer. We always free it after use, because
187 keeping it leaves less free conventional memory for subprocesses.
188 The clipboard buffer tends to be large in size, because for small
189 clipboard data sizes we use the DJGPP transfer buffer. */
193 /* If the size is 0, we used DJGPP transfer buffer, so don't free. */
194 if (clipboard_xfer_buf_info
.size
)
198 /* The NT DPMI host crashes us if we free DOS memory via
199 the DPMI service. Work around by calling DOS free block. */
201 regs
.x
.es
= clipboard_xfer_buf_info
.rm_segment
;
202 __dpmi_int (0x21, ®s
);
203 clipboard_xfer_buf_info
.size
= 0;
207 /* Copy data into the clipboard, return zero if successful. */
209 set_clipboard_data (unsigned Format
, void *Data
, unsigned Size
, int Raw
)
213 unsigned long xbuf_addr
, buf_offset
;
214 unsigned char *dp
= Data
, *dstart
= dp
;
216 if (Format
!= CF_OEMTEXT
)
219 /* need to know final size after '\r' chars are inserted (the
220 standard CF_OEMTEXT clipboard format uses CRLF line endings,
221 while Emacs uses just LF internally). */
222 truelen
= Size
+ 1; /* +1 for the terminating null */
226 /* avoid using strchr because it recomputes the length everytime */
227 while ((dp
= memchr (dp
, '\n', Size
- (dp
- dstart
))) != 0)
234 if (clipboard_compact (truelen
) < truelen
)
237 if ((xbuf_addr
= alloc_xfer_buf (truelen
)) == 0)
240 /* Move the buffer into the low memory, convert LF into CR-LF if needed. */
243 dosmemput (Data
, Size
, xbuf_addr
);
245 /* Terminate with a null, otherwise Windows does strange things
246 when the text size is an integral multiple of 32 bytes. */
247 _farpokeb (_dos_ds
, xbuf_addr
+ Size
, '\0');
252 buf_offset
= xbuf_addr
;
253 _farsetsel (_dos_ds
);
256 /* Don't allow them to put binary data into the clipboard, since
257 it will cause yanked data to be truncated at the first null. */
261 _farnspokeb (buf_offset
++, '\r');
262 _farnspokeb (buf_offset
++, *dp
++);
265 /* Terminate with a null, otherwise Windows does strange things
266 when the text size is an integral multiple of 32 bytes. */
267 _farnspokeb (buf_offset
, '\0');
270 /* Stash away the data we are about to put into the clipboard, so we
271 could later check inside get_clipboard_data whether the clipboard
272 still holds our data. */
273 if (clipboard_storage_size
< truelen
)
275 clipboard_storage_size
= truelen
+ 100;
276 last_clipboard_text
=
277 (char *) xrealloc (last_clipboard_text
, clipboard_storage_size
);
279 if (last_clipboard_text
)
280 dosmemget (xbuf_addr
, truelen
, last_clipboard_text
);
282 /* Calls Int 2Fh/AX=1703h with:
283 DX = WinOldAp-Supported Clipboard format
284 ES:BX = Pointer to data
285 SI:CX = Size of data in bytes
286 Return Values AX == 0: Error occurred
287 <> 0: OK. Data copied into the Clipboard. */
290 regs
.x
.si
= truelen
>> 16;
291 regs
.x
.cx
= truelen
& 0xffff;
292 regs
.x
.es
= xbuf_addr
>> 4;
293 regs
.x
.bx
= xbuf_addr
& 15;
294 __dpmi_int (0x2f, ®s
);
298 /* If the above failed, invalidate the local copy of the clipboard. */
300 *last_clipboard_text
= '\0';
302 /* Zero means success, otherwise (1, 2, or 3) it's an error. */
303 return regs
.x
.ax
> 0 ? 0 : 3;
306 /* Return the size of the clipboard data of format FORMAT. */
308 get_clipboard_data_size (unsigned Format
)
312 /* Calls Int 2Fh/AX=1704h with:
313 DX = WinOldAp-Supported Clipboard format
314 Return Values DX:AX == Size of the data in bytes, including any
316 == 0 If data in this format is not in
320 __dpmi_int (0x2f, ®s
);
321 return ( (((unsigned)regs
.x
.dx
) << 16) | regs
.x
.ax
);
324 /* Get clipboard data, return its length.
325 Warning: this doesn't check whether DATA has enough space to hold
328 get_clipboard_data (unsigned Format
, void *Data
, unsigned Size
, int Raw
)
331 unsigned long xbuf_addr
;
332 unsigned char *dp
= Data
;
334 if (Format
!= CF_OEMTEXT
)
340 if ((xbuf_addr
= alloc_xfer_buf (Size
)) == 0)
343 /* Calls Int 2Fh/AX=1705h with:
344 DX = WinOldAp-Supported Clipboard format
345 ES:BX = Pointer to data buffer to hold data
346 Return Values AX == 0: Error occurred (or data in this format is not
351 regs
.x
.es
= xbuf_addr
>> 4;
352 regs
.x
.bx
= xbuf_addr
& 15;
353 __dpmi_int (0x2f, ®s
);
356 unsigned char null_char
= '\0';
357 unsigned long xbuf_beg
= xbuf_addr
;
359 /* If last_clipboard_text is NULL, we don't want to slow down
360 the next loop by an additional test. */
361 register unsigned char *lcdp
=
362 last_clipboard_text
== NULL
? &null_char
: last_clipboard_text
;
364 /* Copy data from low memory, remove CR
365 characters before LF if needed. */
366 _farsetsel (_dos_ds
);
369 register unsigned char c
= _farnspeekb (xbuf_addr
++);
374 if ((*dp
++ = c
) == '\r' && !Raw
&& _farnspeekb (xbuf_addr
) == '\n')
382 /* Windows reportedly rounds up the size of clipboard data
383 (passed in SIZE) to a multiple of 32, and removes trailing
384 spaces from each line without updating SIZE. We therefore
385 bail out when we see the first null character. */
390 /* If the text in clipboard is identical to what we put there
391 last time set_clipboard_data was called, pretend there's no
392 data in the clipboard. This is so we don't pass our own text
393 from the clipboard (which might be troublesome if the killed
394 text includes null characters). */
395 if (last_clipboard_text
&&
396 xbuf_addr
- xbuf_beg
== (long)(lcdp
- last_clipboard_text
))
397 dp
= (unsigned char *)Data
+ 1;
402 return (unsigned) (dp
- (unsigned char *)Data
- 1);
405 /* Close clipboard, return non-zero if successful. */
407 close_clipboard (void)
411 /* Calls Int 2Fh/AX=1708h
412 Return Values AX == 0: Error occurred
415 __dpmi_int (0x2f, ®s
);
419 /* Compact clipboard data so that at least SIZE bytes is available. */
421 clipboard_compact (unsigned Size
)
425 /* Calls Int 2Fh/AX=1709H with:
426 SI:CX = Desired memory size in bytes.
427 Return Values DX:AX == Number of bytes of largest block of free memory.
428 == 0 if error or no memory */
430 regs
.x
.si
= Size
>> 16;
431 regs
.x
.cx
= Size
& 0xffff;
432 __dpmi_int (0x2f, ®s
);
433 return ((unsigned)regs
.x
.dx
<< 16) | regs
.x
.ax
;
436 static char no_mem_msg
[] =
437 "(Not enough DOS memory to put saved text into clipboard.)";
438 static char binary_msg
[] =
439 "(Binary characters in saved text; clipboard data not set.)";
440 static char system_error_msg
[] =
441 "(Clipboard interface failure; clipboard data not set.)";
443 DEFUN ("w16-set-clipboard-data", Fw16_set_clipboard_data
, Sw16_set_clipboard_data
, 1, 2, 0,
444 doc
: /* This sets the clipboard data to the given text. */)
445 (Lisp_Object string
, Lisp_Object frame
)
447 unsigned ok
= 1, put_status
= 0;
448 int nbytes
, no_crlf_conversion
;
449 unsigned char *src
, *dst
= NULL
;
451 CHECK_STRING (string
);
453 if (!FRAME_MSDOS_P (decode_live_frame (frame
)))
458 if (!open_clipboard ())
461 nbytes
= SBYTES (string
);
462 src
= SDATA (string
);
464 /* Do we need to encode this text? */
465 for (dst
= src
; dst
< src
+ nbytes
; dst
++)
467 if (*dst
== '\0' || *dst
>= 0x80)
470 if (dst
>= src
+ nbytes
)
472 /* No multibyte characters in text. We need not encode it, but we
473 will have to convert it to DOS CR-LF style. */
474 no_crlf_conversion
= 0;
475 Vlast_coding_system_used
= Qraw_text
;
476 dst
= NULL
; /* so we don't try to free a random pointer */
480 /* We must encode contents of STRING according to what
481 clipboard-coding-system specifies. */
482 struct coding_system coding
;
483 Lisp_Object coding_system
=
484 NILP (Vnext_selection_coding_system
) ?
485 Vselection_coding_system
: Vnext_selection_coding_system
;
487 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
488 coding
.dst_bytes
= nbytes
* 4;
489 coding
.destination
= xmalloc (coding
.dst_bytes
);
490 Vnext_selection_coding_system
= Qnil
;
491 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
492 dst
= coding
.destination
;
493 encode_coding_object (&coding
, string
, 0, 0,
494 SCHARS (string
), nbytes
, Qnil
);
495 no_crlf_conversion
= 1;
496 nbytes
= coding
.produced
;
497 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
501 ok
= empty_clipboard ()
503 = set_clipboard_data (CF_OEMTEXT
, src
, nbytes
, no_crlf_conversion
))
506 if (!no_crlf_conversion
)
509 if (ok
) goto unblock
;
519 /* Notify user if the text is too large to fit into DOS memory.
520 (This will happen somewhere after 600K bytes (470K in DJGPP v1.x),
521 depending on user system configuration.) If we just silently
522 fail the function, people might wonder why their text sometimes
523 doesn't make it to the clipboard. */
529 message3 (make_unibyte_string (no_mem_msg
, sizeof (no_mem_msg
) - 1));
532 message3 (make_unibyte_string (binary_msg
, sizeof (binary_msg
) - 1));
535 message3 (make_unibyte_string (system_error_msg
, sizeof (system_error_msg
) - 1));
538 sit_for (make_number (2), 0, 2);
543 return (ok
&& put_status
== 0 ? string
: Qnil
);
546 DEFUN ("w16-get-clipboard-data", Fw16_get_clipboard_data
, Sw16_get_clipboard_data
, 0, 1, 0,
547 doc
: /* This gets the clipboard data in text format. */)
550 unsigned data_size
, truelen
;
551 unsigned char *htext
= NULL
;
552 Lisp_Object ret
= Qnil
;
553 int require_decoding
= 0;
555 if (!FRAME_MSDOS_P (decode_live_frame (frame
)))
560 if (!open_clipboard ())
563 if ((data_size
= get_clipboard_data_size (CF_OEMTEXT
)) == 0 ||
564 (htext
= xmalloc (data_size
)) == 0)
567 /* need to know final size after '\r' chars are removed because
568 we can't change the string size manually, and doing an extra
570 if ((truelen
= get_clipboard_data (CF_OEMTEXT
, htext
, data_size
, 0)) == 0)
573 /* Do we need to decode it? */
575 /* If the clipboard data contains any 8-bit Latin-1 code, we
576 need to decode it. */
579 for (i
= 0; i
< truelen
; i
++)
581 if (htext
[i
] >= 0x80)
583 require_decoding
= 1;
588 if (require_decoding
)
590 struct coding_system coding
;
591 Lisp_Object coding_system
= Vnext_selection_coding_system
;
593 truelen
= get_clipboard_data (CF_OEMTEXT
, htext
, data_size
, 1);
594 if (NILP (coding_system
))
595 coding_system
= Vselection_coding_system
;
596 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
597 coding
.source
= htext
;
598 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
599 /* We explicitly disable composition handling because selection
600 data should not contain any composition sequence. */
601 coding
.common_flags
&= ~CODING_ANNOTATION_MASK
;
602 decode_coding_object (&coding
, Qnil
, 0, 0, truelen
, truelen
, Qt
);
603 ret
= coding
.dst_object
;
604 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
608 ret
= make_unibyte_string ((char *) htext
, truelen
);
609 Vlast_coding_system_used
= Qraw_text
;
613 Vnext_selection_coding_system
= Qnil
;
626 /* Support checking for a clipboard selection. */
628 DEFUN ("w16-selection-exists-p", Fw16_selection_exists_p
, Sw16_selection_exists_p
,
630 doc
: /* Whether there is an owner for the given X selection.
631 SELECTION should be the name of the selection in question, typically
632 one of the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'. (X expects
633 these literal upper-case names.) The symbol nil is the same as
634 `PRIMARY', and t is the same as `SECONDARY'.
636 TERMINAL should be a terminal object or a frame specifying the X
637 server to query. If omitted or nil, that stands for the selected
638 frame's display, or the first available X display. */)
639 (Lisp_Object selection
, Lisp_Object terminal
)
641 CHECK_SYMBOL (selection
);
643 /* Return nil for SECONDARY selection. For PRIMARY (or nil)
644 selection, check if there is some text on the kill-ring;
645 for CLIPBOARD, check if the clipboard currently has valid
646 text format contents.
648 The test for killed text on the kill-ring emulates the Emacs
649 behavior on X, where killed text is also put into X selection
650 by the X interface code. (On MSDOS, killed text is only put
651 into the clipboard if we run under Windows, so we cannot check
652 the clipboard alone.) */
653 if ((EQ (selection
, Qnil
) || EQ (selection
, QPRIMARY
))
654 && ! NILP (Fsymbol_value (Fintern_soft (build_string ("kill-ring"),
658 if (EQ (selection
, QCLIPBOARD
))
660 Lisp_Object val
= Qnil
;
662 if (open_clipboard ())
664 if (get_clipboard_data_size (CF_OEMTEXT
))
674 syms_of_win16select (void)
676 defsubr (&Sw16_set_clipboard_data
);
677 defsubr (&Sw16_get_clipboard_data
);
678 defsubr (&Sw16_selection_exists_p
);
680 DEFVAR_LISP ("selection-coding-system", Vselection_coding_system
,
681 doc
: /* Coding system for communicating with other programs.
683 For MS-Windows and MS-DOS:
684 When sending or receiving text via selection and clipboard, the text
685 is encoded or decoded by this coding system. The default value is
686 the current system default encoding on 9x/Me, `utf-16le-dos'
687 \(Unicode) on NT/W2K/XP, and `iso-latin-1-dos' on MS-DOS.
690 When sending text via selection and clipboard, if the target
691 data-type matches with the type of this coding system, it is used
692 for encoding the text. Otherwise (including the case that this
693 variable is nil), a proper coding system is used as below:
695 data-type coding system
696 --------- -------------
698 COMPOUND_TEXT compound-text-with-extensions
700 C_STRING no-conversion
702 When receiving text, if this coding system is non-nil, it is used
703 for decoding regardless of the data-type. If this is nil, a
704 proper coding system is used according to the data-type as above.
706 See also the documentation of the variable `x-select-request-type' how
707 to control which data-type to request for receiving text.
709 The default value is nil. */);
710 Vselection_coding_system
= intern ("iso-latin-1-dos");
712 DEFVAR_LISP ("next-selection-coding-system", Vnext_selection_coding_system
,
713 doc
: /* Coding system for the next communication with other programs.
714 Usually, `selection-coding-system' is used for communicating with
715 other programs (X Windows clients or MS Windows programs). But, if this
716 variable is set, it is used for the next communication only.
717 After the communication, this variable is set to nil. */);
718 Vnext_selection_coding_system
= Qnil
;
720 DEFSYM (QCLIPBOARD
, "CLIPBOARD");