1 /* 16-bit Windows Selection processing for emacs on MS-Windows
3 Copyright (C) 1996-1997, 2001-2015 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
10 (at 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 Lisp_Object QCLIPBOARD
, QPRIMARY
;
70 /* The segment address and the size of the buffer in low
71 memory used to move data between us and WinOldAp module. */
74 unsigned short rm_segment
;
75 } clipboard_xfer_buf_info
;
77 /* The last text we put into the clipboard. This is used to prevent
78 passing back our own text from the clipboard, instead of using the
79 kill ring. The former is undesirable because the clipboard data
80 could be MULEtilated by inappropriately chosen
81 (next-)selection-coding-system. For this reason, we must store the
82 text *after* it was encoded/Unix-to-DOS-converted. */
83 static unsigned char *last_clipboard_text
;
85 /* The size of allocated storage for storing the clipboard data. */
86 static size_t clipboard_storage_size
;
88 /* C functions to access the Windows 3.1x clipboard from DOS apps.
90 The information was obtained from the Microsoft Knowledge Base,
91 article Q67675 and can be found at:
92 http://www.microsoft.com/kb/developr/win_dk/q67675.htm */
94 /* See also Ralf Brown's Interrupt List.
96 I also seem to remember reading about this in Dr. Dobbs Journal a
97 while ago, but if you knew my memory... :-)
99 Dale P. Smith <dpsm@en.com> */
101 /* Return the WinOldAp support version, or 0x1700 if not supported. */
103 identify_winoldap_version (void)
107 /* Calls Int 2Fh/AX=1700h
108 Return Values AX == 1700H: Clipboard functions not available
109 <> 1700H: AL = Major version number
110 AH = Minor version number */
112 __dpmi_int (0x2f, ®s
);
116 /* Open the clipboard, return non-zero if successful. */
118 open_clipboard (void)
122 /* Is WINOLDAP supported? */
123 /* Kludge alert!! If WinOldAp is not supported, we return a 0,
124 which is the same as ``Clipboard already open''. Currently,
125 this is taken as an error by all the functions that use
126 `open_clipboard', but if somebody someday will use that ``open''
127 clipboard, they will have interesting time debugging it... */
128 if (identify_winoldap_version () == 0x1700)
131 /* Calls Int 2Fh/AX=1701h
132 Return Values AX == 0: Clipboard already open
133 <> 0: Clipboard opened */
135 __dpmi_int (0x2f, ®s
);
139 /* Empty clipboard, return non-zero if successful. */
141 empty_clipboard (void)
145 /* Calls Int 2Fh/AX=1702h
146 Return Values AX == 0: Error occurred
147 <> 0: OK, Clipboard emptied */
149 __dpmi_int (0x2f, ®s
);
153 /* Ensure we have a buffer in low memory with enough memory for data
154 of size WANT_SIZE. Return the linear address of the buffer. */
156 alloc_xfer_buf (unsigned want_size
)
160 /* If the usual DJGPP transfer buffer is large enough, use that. */
161 if (want_size
<= _go32_info_block
.size_of_transfer_buffer
)
162 return __tb
& 0xfffff;
164 /* Don't even try to allocate more than 1MB of memory: DOS cannot
165 possibly handle that (it will overflow the BX register below). */
166 if (want_size
> 0xfffff)
169 /* Need size rounded up to the nearest paragraph, and in
170 paragraph units (1 paragraph = 16 bytes). */
171 clipboard_xfer_buf_info
.size
= (want_size
+ 15) >> 4;
173 /* The NT DPMI host crashes us if we free DOS memory via the
174 DPMI service. Work around by calling DOS allocate/free block. */
176 regs
.x
.bx
= clipboard_xfer_buf_info
.size
;
177 __dpmi_int (0x21, ®s
);
178 if (regs
.x
.flags
& 1)
180 clipboard_xfer_buf_info
.size
= 0;
184 clipboard_xfer_buf_info
.rm_segment
= regs
.x
.ax
;
185 return (((int)clipboard_xfer_buf_info
.rm_segment
) << 4) & 0xfffff;
188 /* Free our clipboard buffer. We always free it after use, because
189 keeping it leaves less free conventional memory for subprocesses.
190 The clipboard buffer tends to be large in size, because for small
191 clipboard data sizes we use the DJGPP transfer buffer. */
195 /* If the size is 0, we used DJGPP transfer buffer, so don't free. */
196 if (clipboard_xfer_buf_info
.size
)
200 /* The NT DPMI host crashes us if we free DOS memory via
201 the DPMI service. Work around by calling DOS free block. */
203 regs
.x
.es
= clipboard_xfer_buf_info
.rm_segment
;
204 __dpmi_int (0x21, ®s
);
205 clipboard_xfer_buf_info
.size
= 0;
209 /* Copy data into the clipboard, return zero if successful. */
211 set_clipboard_data (unsigned Format
, void *Data
, unsigned Size
, int Raw
)
215 unsigned long xbuf_addr
, buf_offset
;
216 unsigned char *dp
= Data
, *dstart
= dp
;
218 if (Format
!= CF_OEMTEXT
)
221 /* need to know final size after '\r' chars are inserted (the
222 standard CF_OEMTEXT clipboard format uses CRLF line endings,
223 while Emacs uses just LF internally). */
224 truelen
= Size
+ 1; /* +1 for the terminating null */
228 /* avoid using strchr because it recomputes the length everytime */
229 while ((dp
= memchr (dp
, '\n', Size
- (dp
- dstart
))) != 0)
236 if (clipboard_compact (truelen
) < truelen
)
239 if ((xbuf_addr
= alloc_xfer_buf (truelen
)) == 0)
242 /* Move the buffer into the low memory, convert LF into CR-LF if needed. */
245 dosmemput (Data
, Size
, xbuf_addr
);
247 /* Terminate with a null, otherwise Windows does strange things
248 when the text size is an integral multiple of 32 bytes. */
249 _farpokeb (_dos_ds
, xbuf_addr
+ Size
, '\0');
254 buf_offset
= xbuf_addr
;
255 _farsetsel (_dos_ds
);
258 /* Don't allow them to put binary data into the clipboard, since
259 it will cause yanked data to be truncated at the first null. */
263 _farnspokeb (buf_offset
++, '\r');
264 _farnspokeb (buf_offset
++, *dp
++);
267 /* Terminate with a null, otherwise Windows does strange things
268 when the text size is an integral multiple of 32 bytes. */
269 _farnspokeb (buf_offset
, '\0');
272 /* Stash away the data we are about to put into the clipboard, so we
273 could later check inside get_clipboard_data whether the clipboard
274 still holds our data. */
275 if (clipboard_storage_size
< truelen
)
277 clipboard_storage_size
= truelen
+ 100;
278 last_clipboard_text
=
279 (char *) xrealloc (last_clipboard_text
, clipboard_storage_size
);
281 if (last_clipboard_text
)
282 dosmemget (xbuf_addr
, truelen
, last_clipboard_text
);
284 /* Calls Int 2Fh/AX=1703h with:
285 DX = WinOldAp-Supported Clipboard format
286 ES:BX = Pointer to data
287 SI:CX = Size of data in bytes
288 Return Values AX == 0: Error occurred
289 <> 0: OK. Data copied into the Clipboard. */
292 regs
.x
.si
= truelen
>> 16;
293 regs
.x
.cx
= truelen
& 0xffff;
294 regs
.x
.es
= xbuf_addr
>> 4;
295 regs
.x
.bx
= xbuf_addr
& 15;
296 __dpmi_int (0x2f, ®s
);
300 /* If the above failed, invalidate the local copy of the clipboard. */
302 *last_clipboard_text
= '\0';
304 /* Zero means success, otherwise (1, 2, or 3) it's an error. */
305 return regs
.x
.ax
> 0 ? 0 : 3;
308 /* Return the size of the clipboard data of format FORMAT. */
310 get_clipboard_data_size (unsigned Format
)
314 /* Calls Int 2Fh/AX=1704h with:
315 DX = WinOldAp-Supported Clipboard format
316 Return Values DX:AX == Size of the data in bytes, including any
318 == 0 If data in this format is not in
322 __dpmi_int (0x2f, ®s
);
323 return ( (((unsigned)regs
.x
.dx
) << 16) | regs
.x
.ax
);
326 /* Get clipboard data, return its length.
327 Warning: this doesn't check whether DATA has enough space to hold
330 get_clipboard_data (unsigned Format
, void *Data
, unsigned Size
, int Raw
)
333 unsigned long xbuf_addr
;
334 unsigned char *dp
= Data
;
336 if (Format
!= CF_OEMTEXT
)
342 if ((xbuf_addr
= alloc_xfer_buf (Size
)) == 0)
345 /* Calls Int 2Fh/AX=1705h with:
346 DX = WinOldAp-Supported Clipboard format
347 ES:BX = Pointer to data buffer to hold data
348 Return Values AX == 0: Error occurred (or data in this format is not
353 regs
.x
.es
= xbuf_addr
>> 4;
354 regs
.x
.bx
= xbuf_addr
& 15;
355 __dpmi_int (0x2f, ®s
);
358 unsigned char null_char
= '\0';
359 unsigned long xbuf_beg
= xbuf_addr
;
361 /* If last_clipboard_text is NULL, we don't want to slow down
362 the next loop by an additional test. */
363 register unsigned char *lcdp
=
364 last_clipboard_text
== NULL
? &null_char
: last_clipboard_text
;
366 /* Copy data from low memory, remove CR
367 characters before LF if needed. */
368 _farsetsel (_dos_ds
);
371 register unsigned char c
= _farnspeekb (xbuf_addr
++);
376 if ((*dp
++ = c
) == '\r' && !Raw
&& _farnspeekb (xbuf_addr
) == '\n')
384 /* Windows reportedly rounds up the size of clipboard data
385 (passed in SIZE) to a multiple of 32, and removes trailing
386 spaces from each line without updating SIZE. We therefore
387 bail out when we see the first null character. */
392 /* If the text in clipboard is identical to what we put there
393 last time set_clipboard_data was called, pretend there's no
394 data in the clipboard. This is so we don't pass our own text
395 from the clipboard (which might be troublesome if the killed
396 text includes null characters). */
397 if (last_clipboard_text
&&
398 xbuf_addr
- xbuf_beg
== (long)(lcdp
- last_clipboard_text
))
399 dp
= (unsigned char *)Data
+ 1;
404 return (unsigned) (dp
- (unsigned char *)Data
- 1);
407 /* Close clipboard, return non-zero if successful. */
409 close_clipboard (void)
413 /* Calls Int 2Fh/AX=1708h
414 Return Values AX == 0: Error occurred
417 __dpmi_int (0x2f, ®s
);
421 /* Compact clipboard data so that at least SIZE bytes is available. */
423 clipboard_compact (unsigned Size
)
427 /* Calls Int 2Fh/AX=1709H with:
428 SI:CX = Desired memory size in bytes.
429 Return Values DX:AX == Number of bytes of largest block of free memory.
430 == 0 if error or no memory */
432 regs
.x
.si
= Size
>> 16;
433 regs
.x
.cx
= Size
& 0xffff;
434 __dpmi_int (0x2f, ®s
);
435 return ((unsigned)regs
.x
.dx
<< 16) | regs
.x
.ax
;
438 static char no_mem_msg
[] =
439 "(Not enough DOS memory to put saved text into clipboard.)";
440 static char binary_msg
[] =
441 "(Binary characters in saved text; clipboard data not set.)";
442 static char system_error_msg
[] =
443 "(Clipboard interface failure; clipboard data not set.)";
445 DEFUN ("w16-set-clipboard-data", Fw16_set_clipboard_data
, Sw16_set_clipboard_data
, 1, 2, 0,
446 doc
: /* This sets the clipboard data to the given text. */)
447 (Lisp_Object string
, Lisp_Object frame
)
449 unsigned ok
= 1, put_status
= 0;
450 int nbytes
, no_crlf_conversion
;
451 unsigned char *src
, *dst
= NULL
;
453 CHECK_STRING (string
);
455 if (!FRAME_MSDOS_P (decode_live_frame (frame
)))
460 if (!open_clipboard ())
463 nbytes
= SBYTES (string
);
464 src
= SDATA (string
);
466 /* Do we need to encode this text? */
467 for (dst
= src
; dst
< src
+ nbytes
; dst
++)
469 if (*dst
== '\0' || *dst
>= 0x80)
472 if (dst
>= src
+ nbytes
)
474 /* No multibyte characters in text. We need not encode it, but we
475 will have to convert it to DOS CR-LF style. */
476 no_crlf_conversion
= 0;
477 Vlast_coding_system_used
= Qraw_text
;
478 dst
= NULL
; /* so we don't try to free a random pointer */
482 /* We must encode contents of STRING according to what
483 clipboard-coding-system specifies. */
484 struct coding_system coding
;
485 Lisp_Object coding_system
=
486 NILP (Vnext_selection_coding_system
) ?
487 Vselection_coding_system
: Vnext_selection_coding_system
;
489 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
490 coding
.dst_bytes
= nbytes
* 4;
491 coding
.destination
= xmalloc (coding
.dst_bytes
);
492 Vnext_selection_coding_system
= Qnil
;
493 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
494 dst
= coding
.destination
;
495 encode_coding_object (&coding
, string
, 0, 0,
496 SCHARS (string
), nbytes
, Qnil
);
497 no_crlf_conversion
= 1;
498 nbytes
= coding
.produced
;
499 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
503 ok
= empty_clipboard ()
505 = set_clipboard_data (CF_OEMTEXT
, src
, nbytes
, no_crlf_conversion
))
508 if (!no_crlf_conversion
)
511 if (ok
) goto unblock
;
521 /* Notify user if the text is too large to fit into DOS memory.
522 (This will happen somewhere after 600K bytes (470K in DJGPP v1.x),
523 depending on user system configuration.) If we just silently
524 fail the function, people might wonder why their text sometimes
525 doesn't make it to the clipboard. */
531 message3 (make_unibyte_string (no_mem_msg
, sizeof (no_mem_msg
) - 1));
534 message3 (make_unibyte_string (binary_msg
, sizeof (binary_msg
) - 1));
537 message3 (make_unibyte_string (system_error_msg
, sizeof (system_error_msg
) - 1));
540 sit_for (make_number (2), 0, 2);
545 return (ok
&& put_status
== 0 ? string
: Qnil
);
548 DEFUN ("w16-get-clipboard-data", Fw16_get_clipboard_data
, Sw16_get_clipboard_data
, 0, 1, 0,
549 doc
: /* This gets the clipboard data in text format. */)
552 unsigned data_size
, truelen
;
553 unsigned char *htext
= NULL
;
554 Lisp_Object ret
= Qnil
;
555 int require_decoding
= 0;
557 if (!FRAME_MSDOS_P (decode_live_frame (frame
)))
562 if (!open_clipboard ())
565 if ((data_size
= get_clipboard_data_size (CF_OEMTEXT
)) == 0 ||
566 (htext
= xmalloc (data_size
)) == 0)
569 /* need to know final size after '\r' chars are removed because
570 we can't change the string size manually, and doing an extra
572 if ((truelen
= get_clipboard_data (CF_OEMTEXT
, htext
, data_size
, 0)) == 0)
575 /* Do we need to decode it? */
577 /* If the clipboard data contains any 8-bit Latin-1 code, we
578 need to decode it. */
581 for (i
= 0; i
< truelen
; i
++)
583 if (htext
[i
] >= 0x80)
585 require_decoding
= 1;
590 if (require_decoding
)
592 struct coding_system coding
;
593 Lisp_Object coding_system
= Vnext_selection_coding_system
;
595 truelen
= get_clipboard_data (CF_OEMTEXT
, htext
, data_size
, 1);
596 if (NILP (coding_system
))
597 coding_system
= Vselection_coding_system
;
598 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
599 coding
.source
= htext
;
600 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
601 /* We explicitly disable composition handling because selection
602 data should not contain any composition sequence. */
603 coding
.common_flags
&= ~CODING_ANNOTATION_MASK
;
604 decode_coding_object (&coding
, Qnil
, 0, 0, truelen
, truelen
, Qt
);
605 ret
= coding
.dst_object
;
606 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
610 ret
= make_unibyte_string ((char *) htext
, truelen
);
611 Vlast_coding_system_used
= Qraw_text
;
615 Vnext_selection_coding_system
= Qnil
;
628 /* Support checking for a clipboard selection. */
630 DEFUN ("w16-selection-exists-p", Fw16_selection_exists_p
, Sw16_selection_exists_p
,
632 doc
: /* Whether there is an owner for the given X selection.
633 SELECTION should be the name of the selection in question, typically
634 one of the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'. (X expects
635 these literal upper-case names.) The symbol nil is the same as
636 `PRIMARY', and t is the same as `SECONDARY'.
638 TERMINAL should be a terminal object or a frame specifying the X
639 server to query. If omitted or nil, that stands for the selected
640 frame's display, or the first available X display. */)
641 (Lisp_Object selection
, Lisp_Object terminal
)
643 CHECK_SYMBOL (selection
);
645 /* Return nil for SECONDARY selection. For PRIMARY (or nil)
646 selection, check if there is some text on the kill-ring;
647 for CLIPBOARD, check if the clipboard currently has valid
648 text format contents.
650 The test for killed text on the kill-ring emulates the Emacs
651 behavior on X, where killed text is also put into X selection
652 by the X interface code. (On MSDOS, killed text is only put
653 into the clipboard if we run under Windows, so we cannot check
654 the clipboard alone.) */
655 if ((EQ (selection
, Qnil
) || EQ (selection
, QPRIMARY
))
656 && ! NILP (Fsymbol_value (Fintern_soft (build_string ("kill-ring"),
660 if (EQ (selection
, QCLIPBOARD
))
662 Lisp_Object val
= Qnil
;
664 if (open_clipboard ())
666 if (get_clipboard_data_size (CF_OEMTEXT
))
676 syms_of_win16select (void)
678 defsubr (&Sw16_set_clipboard_data
);
679 defsubr (&Sw16_get_clipboard_data
);
680 defsubr (&Sw16_selection_exists_p
);
682 DEFVAR_LISP ("selection-coding-system", Vselection_coding_system
,
683 doc
: /* Coding system for communicating with other programs.
685 For MS-Windows and MS-DOS:
686 When sending or receiving text via selection and clipboard, the text
687 is encoded or decoded by this coding system. The default value is
688 the current system default encoding on 9x/Me, `utf-16le-dos'
689 (Unicode) on NT/W2K/XP, and `iso-latin-1-dos' on MS-DOS.
692 When sending text via selection and clipboard, if the target
693 data-type matches with the type of this coding system, it is used
694 for encoding the text. Otherwise (including the case that this
695 variable is nil), a proper coding system is used as below:
697 data-type coding system
698 --------- -------------
700 COMPOUND_TEXT compound-text-with-extensions
702 C_STRING no-conversion
704 When receiving text, if this coding system is non-nil, it is used
705 for decoding regardless of the data-type. If this is nil, a
706 proper coding system is used according to the data-type as above.
708 See also the documentation of the variable `x-select-request-type' how
709 to control which data-type to request for receiving text.
711 The default value is nil. */);
712 Vselection_coding_system
= intern ("iso-latin-1-dos");
714 DEFVAR_LISP ("next-selection-coding-system", Vnext_selection_coding_system
,
715 doc
: /* Coding system for the next communication with other programs.
716 Usually, `selection-coding-system' is used for communicating with
717 other programs (X Windows clients or MS Windows programs). But, if this
718 variable is set, it is used for the next communication only.
719 After the communication, this variable is set to nil. */);
720 Vnext_selection_coding_system
= Qnil
;
722 QPRIMARY
= intern ("PRIMARY"); staticpro (&QPRIMARY
);
723 QCLIPBOARD
= intern ("CLIPBOARD"); staticpro (&QCLIPBOARD
);