1 /* 16-bit Windows Selection processing for emacs on MS-Windows
3 Copyright (C) 1996-1997, 2001-2011 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>
36 #include "dispextern.h" /* frame.h seems to want this */
37 #include "frame.h" /* Need this to get the X window of selected_frame */
38 #include "blockinput.h"
40 #include "character.h"
42 #include "composite.h"
44 /* If ever some function outside this file will need to call any
45 clipboard-related function, the following prototypes and constants
46 should be put on a header file. Right now, nobody else uses them. */
49 #define CF_BITMAP 0x02
50 #define CF_METAFILE 0x03
54 #define CF_OEMTEXT 0x07
55 #define CF_DIBBITMAP 0x08
56 #define CF_WINWRITE 0x80
57 #define CF_DSPTEXT 0x81
58 #define CF_DSPBITMAP 0x82
60 unsigned identify_winoldap_version (void);
61 unsigned open_clipboard (void);
62 unsigned empty_clipboard (void);
63 unsigned set_clipboard_data (unsigned, void *, unsigned, int);
64 unsigned get_clipboard_data_size (unsigned);
65 unsigned get_clipboard_data (unsigned, void *, unsigned, int);
66 unsigned close_clipboard (void);
67 unsigned clipboard_compact (unsigned);
69 Lisp_Object QCLIPBOARD
, QPRIMARY
;
71 /* The segment address and the size of the buffer in low
72 memory used to move data between us and WinOldAp module. */
75 unsigned short rm_segment
;
76 } clipboard_xfer_buf_info
;
78 /* The last text we put into the clipboard. This is used to prevent
79 passing back our own text from the clipboard, instead of using the
80 kill ring. The former is undesirable because the clipboard data
81 could be MULEtilated by inappropriately chosen
82 (next-)selection-coding-system. For this reason, we must store the
83 text *after* it was encoded/Unix-to-DOS-converted. */
84 static unsigned char *last_clipboard_text
;
86 /* The size of allocated storage for storing the clipboard data. */
87 static size_t clipboard_storage_size
;
89 /* C functions to access the Windows 3.1x clipboard from DOS apps.
91 The information was obtained from the Microsoft Knowledge Base,
92 article Q67675 and can be found at:
93 http://www.microsoft.com/kb/developr/win_dk/q67675.htm */
95 /* See also Ralf Brown's Interrupt List.
97 I also seem to remember reading about this in Dr. Dobbs Journal a
98 while ago, but if you knew my memory... :-)
100 Dale P. Smith <dpsm@en.com> */
102 /* Return the WinOldAp support version, or 0x1700 if not supported. */
104 identify_winoldap_version (void)
108 /* Calls Int 2Fh/AX=1700h
109 Return Values AX == 1700H: Clipboard functions not available
110 <> 1700H: AL = Major version number
111 AH = Minor version number */
113 __dpmi_int (0x2f, ®s
);
117 /* Open the clipboard, return non-zero if successfull. */
119 open_clipboard (void)
123 /* Is WINOLDAP supported? */
124 /* Kludge alert!! If WinOldAp is not supported, we return a 0,
125 which is the same as ``Clipboard already open''. Currently,
126 this is taken as an error by all the functions that use
127 `open_clipboard', but if somebody someday will use that ``open''
128 clipboard, they will have interesting time debugging it... */
129 if (identify_winoldap_version () == 0x1700)
132 /* Calls Int 2Fh/AX=1701h
133 Return Values AX == 0: Clipboard already open
134 <> 0: Clipboard opened */
136 __dpmi_int (0x2f, ®s
);
140 /* Empty clipboard, return non-zero if successfull. */
142 empty_clipboard (void)
146 /* Calls Int 2Fh/AX=1702h
147 Return Values AX == 0: Error occurred
148 <> 0: OK, Clipboard emptied */
150 __dpmi_int (0x2f, ®s
);
154 /* Ensure we have a buffer in low memory with enough memory for data
155 of size WANT_SIZE. Return the linear address of the buffer. */
157 alloc_xfer_buf (unsigned want_size
)
161 /* If the usual DJGPP transfer buffer is large enough, use that. */
162 if (want_size
<= _go32_info_block
.size_of_transfer_buffer
)
163 return __tb
& 0xfffff;
165 /* Don't even try to allocate more than 1MB of memory: DOS cannot
166 possibly handle that (it will overflow the BX register below). */
167 if (want_size
> 0xfffff)
170 /* Need size rounded up to the nearest paragraph, and in
171 paragraph units (1 paragraph = 16 bytes). */
172 clipboard_xfer_buf_info
.size
= (want_size
+ 15) >> 4;
174 /* The NT DPMI host crashes us if we free DOS memory via the
175 DPMI service. Work around by calling DOS allocate/free block. */
177 regs
.x
.bx
= clipboard_xfer_buf_info
.size
;
178 __dpmi_int (0x21, ®s
);
179 if (regs
.x
.flags
& 1)
181 clipboard_xfer_buf_info
.size
= 0;
185 clipboard_xfer_buf_info
.rm_segment
= regs
.x
.ax
;
186 return (((int)clipboard_xfer_buf_info
.rm_segment
) << 4) & 0xfffff;
189 /* Free our clipboard buffer. We always free it after use, because
190 keeping it leaves less free conventional memory for subprocesses.
191 The clipboard buffer tends to be large in size, because for small
192 clipboard data sizes we use the DJGPP transfer buffer. */
196 /* If the size is 0, we used DJGPP transfer buffer, so don't free. */
197 if (clipboard_xfer_buf_info
.size
)
201 /* The NT DPMI host crashes us if we free DOS memory via
202 the DPMI service. Work around by calling DOS free block. */
204 regs
.x
.es
= clipboard_xfer_buf_info
.rm_segment
;
205 __dpmi_int (0x21, ®s
);
206 clipboard_xfer_buf_info
.size
= 0;
210 /* Copy data into the clipboard, return zero if successfull. */
212 set_clipboard_data (unsigned Format
, void *Data
, unsigned Size
, int Raw
)
216 unsigned long xbuf_addr
, buf_offset
;
217 unsigned char *dp
= Data
, *dstart
= dp
;
219 if (Format
!= CF_OEMTEXT
)
222 /* need to know final size after '\r' chars are inserted (the
223 standard CF_OEMTEXT clipboard format uses CRLF line endings,
224 while Emacs uses just LF internally). */
225 truelen
= Size
+ 1; /* +1 for the terminating null */
229 /* avoid using strchr because it recomputes the length everytime */
230 while ((dp
= memchr (dp
, '\n', Size
- (dp
- dstart
))) != 0)
237 if (clipboard_compact (truelen
) < truelen
)
240 if ((xbuf_addr
= alloc_xfer_buf (truelen
)) == 0)
243 /* Move the buffer into the low memory, convert LF into CR-LF if needed. */
246 dosmemput (Data
, Size
, xbuf_addr
);
248 /* Terminate with a null, otherwise Windows does strange things
249 when the text size is an integral multiple of 32 bytes. */
250 _farpokeb (_dos_ds
, xbuf_addr
+ Size
, '\0');
255 buf_offset
= xbuf_addr
;
256 _farsetsel (_dos_ds
);
259 /* Don't allow them to put binary data into the clipboard, since
260 it will cause yanked data to be truncated at the first null. */
264 _farnspokeb (buf_offset
++, '\r');
265 _farnspokeb (buf_offset
++, *dp
++);
268 /* Terminate with a null, otherwise Windows does strange things
269 when the text size is an integral multiple of 32 bytes. */
270 _farnspokeb (buf_offset
, '\0');
273 /* Stash away the data we are about to put into the clipboard, so we
274 could later check inside get_clipboard_data whether the clipboard
275 still holds our data. */
276 if (clipboard_storage_size
< truelen
)
278 clipboard_storage_size
= truelen
+ 100;
279 last_clipboard_text
=
280 (char *) xrealloc (last_clipboard_text
, clipboard_storage_size
);
282 if (last_clipboard_text
)
283 dosmemget (xbuf_addr
, truelen
, last_clipboard_text
);
285 /* Calls Int 2Fh/AX=1703h with:
286 DX = WinOldAp-Supported Clipboard format
287 ES:BX = Pointer to data
288 SI:CX = Size of data in bytes
289 Return Values AX == 0: Error occurred
290 <> 0: OK. Data copied into the Clipboard. */
293 regs
.x
.si
= truelen
>> 16;
294 regs
.x
.cx
= truelen
& 0xffff;
295 regs
.x
.es
= xbuf_addr
>> 4;
296 regs
.x
.bx
= xbuf_addr
& 15;
297 __dpmi_int (0x2f, ®s
);
301 /* If the above failed, invalidate the local copy of the clipboard. */
303 *last_clipboard_text
= '\0';
305 /* Zero means success, otherwise (1, 2, or 3) it's an error. */
306 return regs
.x
.ax
> 0 ? 0 : 3;
309 /* Return the size of the clipboard data of format FORMAT. */
311 get_clipboard_data_size (unsigned Format
)
315 /* Calls Int 2Fh/AX=1704h with:
316 DX = WinOldAp-Supported Clipboard format
317 Return Values DX:AX == Size of the data in bytes, including any
319 == 0 If data in this format is not in
323 __dpmi_int (0x2f, ®s
);
324 return ( (((unsigned)regs
.x
.dx
) << 16) | regs
.x
.ax
);
327 /* Get clipboard data, return its length.
328 Warning: this doesn't check whether DATA has enough space to hold
331 get_clipboard_data (unsigned Format
, void *Data
, unsigned Size
, int Raw
)
334 unsigned long xbuf_addr
;
335 unsigned char *dp
= Data
;
337 if (Format
!= CF_OEMTEXT
)
343 if ((xbuf_addr
= alloc_xfer_buf (Size
)) == 0)
346 /* Calls Int 2Fh/AX=1705h with:
347 DX = WinOldAp-Supported Clipboard format
348 ES:BX = Pointer to data buffer to hold data
349 Return Values AX == 0: Error occurred (or data in this format is not
354 regs
.x
.es
= xbuf_addr
>> 4;
355 regs
.x
.bx
= xbuf_addr
& 15;
356 __dpmi_int (0x2f, ®s
);
359 unsigned char null_char
= '\0';
360 unsigned long xbuf_beg
= xbuf_addr
;
362 /* If last_clipboard_text is NULL, we don't want to slow down
363 the next loop by an additional test. */
364 register unsigned char *lcdp
=
365 last_clipboard_text
== NULL
? &null_char
: last_clipboard_text
;
367 /* Copy data from low memory, remove CR
368 characters before LF if needed. */
369 _farsetsel (_dos_ds
);
372 register unsigned char c
= _farnspeekb (xbuf_addr
++);
377 if ((*dp
++ = c
) == '\r' && !Raw
&& _farnspeekb (xbuf_addr
) == '\n')
385 /* Windows reportedly rounds up the size of clipboard data
386 (passed in SIZE) to a multiple of 32, and removes trailing
387 spaces from each line without updating SIZE. We therefore
388 bail out when we see the first null character. */
393 /* If the text in clipboard is identical to what we put there
394 last time set_clipboard_data was called, pretend there's no
395 data in the clipboard. This is so we don't pass our own text
396 from the clipboard (which might be troublesome if the killed
397 text includes null characters). */
398 if (last_clipboard_text
&&
399 xbuf_addr
- xbuf_beg
== (long)(lcdp
- last_clipboard_text
))
400 dp
= (unsigned char *)Data
+ 1;
405 return (unsigned) (dp
- (unsigned char *)Data
- 1);
408 /* Close clipboard, return non-zero if successfull. */
410 close_clipboard (void)
414 /* Calls Int 2Fh/AX=1708h
415 Return Values AX == 0: Error occurred
418 __dpmi_int (0x2f, ®s
);
422 /* Compact clipboard data so that at least SIZE bytes is available. */
424 clipboard_compact (unsigned Size
)
428 /* Calls Int 2Fh/AX=1709H with:
429 SI:CX = Desired memory size in bytes.
430 Return Values DX:AX == Number of bytes of largest block of free memory.
431 == 0 if error or no memory */
433 regs
.x
.si
= Size
>> 16;
434 regs
.x
.cx
= Size
& 0xffff;
435 __dpmi_int (0x2f, ®s
);
436 return ((unsigned)regs
.x
.dx
<< 16) | regs
.x
.ax
;
439 static char no_mem_msg
[] =
440 "(Not enough DOS memory to put saved text into clipboard.)";
441 static char binary_msg
[] =
442 "(Binary characters in saved text; clipboard data not set.)";
443 static char system_error_msg
[] =
444 "(Clipboard interface failure; clipboard data not set.)";
446 DEFUN ("w16-set-clipboard-data", Fw16_set_clipboard_data
, Sw16_set_clipboard_data
, 1, 2, 0,
447 doc
: /* This sets the clipboard data to the given text. */)
448 (Lisp_Object string
, Lisp_Object frame
)
450 unsigned ok
= 1, put_status
= 0;
451 int nbytes
, no_crlf_conversion
;
452 unsigned char *src
, *dst
= NULL
;
454 CHECK_STRING (string
);
457 frame
= Fselected_frame ();
459 CHECK_LIVE_FRAME (frame
);
460 if ( !FRAME_MSDOS_P (XFRAME (frame
)))
465 if (!open_clipboard ())
468 nbytes
= SBYTES (string
);
469 src
= SDATA (string
);
471 /* Do we need to encode this text? */
472 for (dst
= src
; dst
< src
+ nbytes
; dst
++)
474 if (*dst
== '\0' || *dst
>= 0x80)
477 if (dst
>= src
+ nbytes
)
479 /* No multibyte characters in text. We need not encode it, but we
480 will have to convert it to DOS CR-LF style. */
481 no_crlf_conversion
= 0;
482 Vlast_coding_system_used
= Qraw_text
;
483 dst
= NULL
; /* so we don't try to free a random pointer */
487 /* We must encode contents of STRING according to what
488 clipboard-coding-system specifies. */
489 struct coding_system coding
;
490 Lisp_Object coding_system
=
491 NILP (Vnext_selection_coding_system
) ?
492 Vselection_coding_system
: Vnext_selection_coding_system
;
494 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
495 coding
.dst_bytes
= nbytes
* 4;
496 coding
.destination
= (unsigned char *) xmalloc (coding
.dst_bytes
);
497 Vnext_selection_coding_system
= Qnil
;
498 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
499 dst
= coding
.destination
;
500 encode_coding_object (&coding
, string
, 0, 0,
501 SCHARS (string
), nbytes
, Qnil
);
502 no_crlf_conversion
= 1;
503 nbytes
= coding
.produced
;
504 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
508 ok
= empty_clipboard ()
510 = set_clipboard_data (CF_OEMTEXT
, src
, nbytes
, no_crlf_conversion
))
513 if (!no_crlf_conversion
)
516 if (ok
) goto unblock
;
526 /* Notify user if the text is too large to fit into DOS memory.
527 (This will happen somewhere after 600K bytes (470K in DJGPP v1.x),
528 depending on user system configuration.) If we just silently
529 fail the function, people might wonder why their text sometimes
530 doesn't make it to the clipboard. */
536 message2 (no_mem_msg
, sizeof (no_mem_msg
) - 1, 0);
539 message2 (binary_msg
, sizeof (binary_msg
) - 1, 0);
542 message2 (system_error_msg
, sizeof (system_error_msg
) - 1, 0);
545 sit_for (make_number (2), 0, 2);
550 return (ok
&& put_status
== 0 ? string
: Qnil
);
553 DEFUN ("w16-get-clipboard-data", Fw16_get_clipboard_data
, Sw16_get_clipboard_data
, 0, 1, 0,
554 doc
: /* This gets the clipboard data in text format. */)
557 unsigned data_size
, truelen
;
558 unsigned char *htext
= NULL
;
559 Lisp_Object ret
= Qnil
;
560 int require_decoding
= 0;
563 frame
= Fselected_frame ();
565 CHECK_LIVE_FRAME (frame
);
566 if ( !FRAME_MSDOS_P (XFRAME (frame
)))
571 if (!open_clipboard ())
574 if ((data_size
= get_clipboard_data_size (CF_OEMTEXT
)) == 0 ||
575 (htext
= (unsigned char *)xmalloc (data_size
)) == 0)
578 /* need to know final size after '\r' chars are removed because
579 we can't change the string size manually, and doing an extra
581 if ((truelen
= get_clipboard_data (CF_OEMTEXT
, htext
, data_size
, 0)) == 0)
584 /* Do we need to decode it? */
586 /* If the clipboard data contains any 8-bit Latin-1 code, we
587 need to decode it. */
590 for (i
= 0; i
< truelen
; i
++)
592 if (htext
[i
] >= 0x80)
594 require_decoding
= 1;
599 if (require_decoding
)
601 struct coding_system coding
;
602 Lisp_Object coding_system
= Vnext_selection_coding_system
;
604 truelen
= get_clipboard_data (CF_OEMTEXT
, htext
, data_size
, 1);
605 if (NILP (coding_system
))
606 coding_system
= Vselection_coding_system
;
607 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
608 coding
.source
= htext
;
609 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
610 /* We explicitly disable composition handling because selection
611 data should not contain any composition sequence. */
612 coding
.mode
&= CODING_ANNOTATION_MASK
;
613 decode_coding_object (&coding
, Qnil
, 0, 0, truelen
, truelen
, Qt
);
614 ret
= coding
.dst_object
;
615 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
619 ret
= make_unibyte_string ((char *) htext
, truelen
);
620 Vlast_coding_system_used
= Qraw_text
;
624 Vnext_selection_coding_system
= Qnil
;
637 /* Support checking for a clipboard selection. */
639 DEFUN ("x-selection-exists-p", Fx_selection_exists_p
, Sx_selection_exists_p
,
641 doc
: /* Whether there is an owner for the given X Selection.
642 The arg should be the name of the selection in question, typically one of
643 the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
644 \(Those are literal upper-case symbol names, since that's what X expects.)
645 For convenience, the symbol nil is the same as `PRIMARY',
646 and t is the same as `SECONDARY'. */)
647 (Lisp_Object selection
)
649 CHECK_SYMBOL (selection
);
651 /* Return nil for SECONDARY selection. For PRIMARY (or nil)
652 selection, check if there is some text on the kill-ring;
653 for CLIPBOARD, check if the clipboard currently has valid
654 text format contents.
656 The test for killed text on the kill-ring emulates the Emacs
657 behavior on X, where killed text is also put into X selection
658 by the X interface code. (On MSDOS, killed text is only put
659 into the clipboard if we run under Windows, so we cannot check
660 the clipboard alone.) */
661 if ((EQ (selection
, Qnil
) || EQ (selection
, QPRIMARY
))
662 && ! NILP (Fsymbol_value (Fintern_soft (build_string ("kill-ring"),
666 if (EQ (selection
, QCLIPBOARD
))
668 Lisp_Object val
= Qnil
;
670 if (open_clipboard ())
672 if (get_clipboard_data_size (CF_OEMTEXT
))
682 syms_of_win16select (void)
684 defsubr (&Sw16_set_clipboard_data
);
685 defsubr (&Sw16_get_clipboard_data
);
686 defsubr (&Sx_selection_exists_p
);
688 DEFVAR_LISP ("selection-coding-system", Vselection_coding_system
,
689 doc
: /* Coding system for communicating with other programs.
691 For MS-Windows and MS-DOS:
692 When sending or receiving text via selection and clipboard, the text
693 is encoded or decoded by this coding system. The default value is
694 the current system default encoding on 9x/Me, `utf-16le-dos'
695 \(Unicode) on NT/W2K/XP, and `iso-latin-1-dos' on MS-DOS.
698 When sending text via selection and clipboard, if the target
699 data-type matches with the type of this coding system, it is used
700 for encoding the text. Otherwise (including the case that this
701 variable is nil), a proper coding system is used as below:
703 data-type coding system
704 --------- -------------
706 COMPOUND_TEXT compound-text-with-extensions
708 C_STRING no-conversion
710 When receiving text, if this coding system is non-nil, it is used
711 for decoding regardless of the data-type. If this is nil, a
712 proper coding system is used according to the data-type as above.
714 See also the documentation of the variable `x-select-request-type' how
715 to control which data-type to request for receiving text.
717 The default value is nil. */);
718 Vselection_coding_system
= intern ("iso-latin-1-dos");
720 DEFVAR_LISP ("next-selection-coding-system", Vnext_selection_coding_system
,
721 doc
: /* Coding system for the next communication with other programs.
722 Usually, `selection-coding-system' is used for communicating with
723 other programs (X Windows clients or MS Windows programs). But, if this
724 variable is set, it is used for the next communication only.
725 After the communication, this variable is set to nil. */);
726 Vnext_selection_coding_system
= Qnil
;
728 QPRIMARY
= intern ("PRIMARY"); staticpro (&QPRIMARY
);
729 QCLIPBOARD
= intern ("CLIPBOARD"); staticpro (&QCLIPBOARD
);