#ifdef MSDOS
#include <config.h>
#include <string.h>
#include <dpmi.h>
#include <go32.h>
#include <sys/farptr.h>
#include "lisp.h"
#include "dispextern.h"
#include "frame.h"
#include "blockinput.h"
#include "buffer.h"
#include "charset.h"
#include "coding.h"
#include "composite.h"
#define CF_TEXT 0x01
#define CF_BITMAP 0x02
#define CF_METAFILE 0x03
#define CF_SYLK 0x04
#define CF_DIF 0x05
#define CF_TIFF 0x06
#define CF_OEMTEXT 0x07
#define CF_DIBBITMAP 0x08
#define CF_WINWRITE 0x80
#define CF_DSPTEXT 0x81
#define CF_DSPBITMAP 0x82
unsigned identify_winoldap_version (void);
unsigned open_clipboard (void);
unsigned empty_clipboard (void);
unsigned set_clipboard_data (unsigned, void *, unsigned, int);
unsigned get_clipboard_data_size (unsigned);
unsigned get_clipboard_data (unsigned, void *, unsigned, int);
unsigned close_clipboard (void);
unsigned clipboard_compact (unsigned);
Lisp_Object QCLIPBOARD, QPRIMARY;
static Lisp_Object Vselection_coding_system;
static Lisp_Object Vnext_selection_coding_system;
static struct {
unsigned long size;
unsigned short rm_segment;
} clipboard_xfer_buf_info;
static unsigned char *last_clipboard_text;
static size_t clipboard_storage_size;
#if __DJGPP__ < 2
typedef _go32_dpmi_registers __dpmi_regs;
#define __tb _go32_info_block.linear_address_of_transfer_buffer
#define _dos_ds _go32_info_block.selector_for_linear_memory
static int
__dpmi_int (intno, regs)
int intno;
__dpmi_regs *regs;
{
regs->x.ss = regs->x.sp = regs->x.flags = 0;
return _go32_dpmi_simulate_int (intno, regs);
}
#endif
unsigned
identify_winoldap_version ()
{
__dpmi_regs regs;
regs.x.ax = 0x1700;
__dpmi_int(0x2f, ®s);
return regs.x.ax;
}
unsigned
open_clipboard ()
{
__dpmi_regs regs;
if (identify_winoldap_version () == 0x1700)
return 0;
regs.x.ax = 0x1701;
__dpmi_int(0x2f, ®s);
return regs.x.ax;
}
unsigned
empty_clipboard ()
{
__dpmi_regs regs;
regs.x.ax = 0x1702;
__dpmi_int(0x2f, ®s);
return regs.x.ax;
}
static unsigned long
alloc_xfer_buf (want_size)
unsigned want_size;
{
__dpmi_regs regs;
if (want_size <= _go32_info_block.size_of_transfer_buffer)
return __tb & 0xfffff;
if (want_size > 0xfffff)
return 0;
clipboard_xfer_buf_info.size = (want_size + 15) >> 4;
regs.h.ah = 0x48;
regs.x.bx = clipboard_xfer_buf_info.size;
__dpmi_int (0x21, ®s);
if (regs.x.flags & 1)
{
clipboard_xfer_buf_info.size = 0;
return 0;
}
clipboard_xfer_buf_info.rm_segment = regs.x.ax;
return (((int)clipboard_xfer_buf_info.rm_segment) << 4) & 0xfffff;
}
static void
free_xfer_buf ()
{
if (clipboard_xfer_buf_info.size)
{
__dpmi_regs regs;
regs.h.ah = 0x49;
regs.x.es = clipboard_xfer_buf_info.rm_segment;
__dpmi_int (0x21, ®s);
clipboard_xfer_buf_info.size = 0;
}
}
unsigned
set_clipboard_data (Format, Data, Size, Raw)
unsigned Format;
void *Data;
unsigned Size;
int Raw;
{
__dpmi_regs regs;
unsigned truelen;
unsigned long xbuf_addr, buf_offset;
unsigned char *dp = Data, *dstart = dp;
if (Format != CF_OEMTEXT)
return 3;
truelen = Size + 1;
if (!Raw)
{
while ((dp = memchr (dp, '\n', Size - (dp - dstart))) != 0)
{
truelen++;
dp++;
}
}
if (clipboard_compact (truelen) < truelen)
return 1;
if ((xbuf_addr = alloc_xfer_buf (truelen)) == 0)
return 1;
if (Raw)
{
dosmemput (Data, Size, xbuf_addr);
_farpokeb (_dos_ds, xbuf_addr + Size, '\0');
}
else
{
dp = Data;
buf_offset = xbuf_addr;
_farsetsel (_dos_ds);
while (Size--)
{
if (*dp == '\0')
return 2;
if (*dp == '\n')
_farnspokeb (buf_offset++, '\r');
_farnspokeb (buf_offset++, *dp++);
}
_farnspokeb (buf_offset, '\0');
}
if (clipboard_storage_size < truelen)
{
clipboard_storage_size = truelen + 100;
last_clipboard_text =
(char *) xrealloc (last_clipboard_text, clipboard_storage_size);
}
if (last_clipboard_text)
dosmemget (xbuf_addr, truelen, last_clipboard_text);
regs.x.ax = 0x1703;
regs.x.dx = Format;
regs.x.si = truelen >> 16;
regs.x.cx = truelen & 0xffff;
regs.x.es = xbuf_addr >> 4;
regs.x.bx = xbuf_addr & 15;
__dpmi_int(0x2f, ®s);
free_xfer_buf ();
if (regs.x.ax == 0)
*last_clipboard_text = '\0';
return regs.x.ax > 0 ? 0 : 3;
}
unsigned
get_clipboard_data_size (Format)
unsigned Format;
{
__dpmi_regs regs;
regs.x.ax = 0x1704;
regs.x.dx = Format;
__dpmi_int(0x2f, ®s);
return ( (((unsigned)regs.x.dx) << 16) | regs.x.ax);
}
unsigned
get_clipboard_data (Format, Data, Size, Raw)
unsigned Format;
void *Data;
unsigned Size;
int Raw;
{
__dpmi_regs regs;
unsigned long xbuf_addr;
unsigned char *dp = Data;
if (Format != CF_OEMTEXT)
return 0;
if (Size == 0)
return 0;
if ((xbuf_addr = alloc_xfer_buf (Size)) == 0)
return 0;
regs.x.ax = 0x1705;
regs.x.dx = Format;
regs.x.es = xbuf_addr >> 4;
regs.x.bx = xbuf_addr & 15;
__dpmi_int(0x2f, ®s);
if (regs.x.ax != 0)
{
unsigned char null_char = '\0';
unsigned long xbuf_beg = xbuf_addr;
register unsigned char *lcdp =
last_clipboard_text == NULL ? &null_char : last_clipboard_text;
_farsetsel (_dos_ds);
while (Size--)
{
register unsigned char c = _farnspeekb (xbuf_addr++);
if (*lcdp == c)
lcdp++;
if ((*dp++ = c) == '\r' && !Raw && _farnspeekb (xbuf_addr) == '\n')
{
dp--;
*dp++ = '\n';
xbuf_addr++;
if (*lcdp == '\n')
lcdp++;
}
else if (c == '\0')
break;
}
if (last_clipboard_text &&
xbuf_addr - xbuf_beg == (long)(lcdp - last_clipboard_text))
dp = (unsigned char *)Data + 1;
}
free_xfer_buf ();
return (unsigned) (dp - (unsigned char *)Data - 1);
}
unsigned
close_clipboard ()
{
__dpmi_regs regs;
regs.x.ax = 0x1708;
__dpmi_int(0x2f, ®s);
return regs.x.ax;
}
unsigned
clipboard_compact (Size)
unsigned Size;
{
__dpmi_regs regs;
regs.x.ax = 0x1709;
regs.x.si = Size >> 16;
regs.x.cx = Size & 0xffff;
__dpmi_int(0x2f, ®s);
return ((unsigned)regs.x.dx << 16) | regs.x.ax;
}
static char no_mem_msg[] =
"(Not enough DOS memory to put saved text into clipboard.)";
static char binary_msg[] =
"(Binary characters in saved text; clipboard data not set.)";
static char system_error_msg[] =
"(Clipboard interface failure; clipboard data not set.)";
DEFUN ("w16-set-clipboard-data", Fw16_set_clipboard_data, Sw16_set_clipboard_data, 1, 2, 0,
doc: )
(string, frame)
Lisp_Object string, frame;
{
unsigned ok = 1, put_status = 0;
int nbytes, charset_info, no_crlf_conversion;
unsigned char *src, *dst = NULL;
CHECK_STRING (string);
if (NILP (frame))
frame = Fselected_frame ();
CHECK_LIVE_FRAME (frame);
if ( !FRAME_MSDOS_P (XFRAME (frame)))
goto done;
BLOCK_INPUT;
nbytes = SBYTES (string);
src = SDATA (string);
charset_info = find_charset_in_text (src, SCHARS (string), nbytes,
NULL, Qnil);
if (charset_info == 0)
{
no_crlf_conversion = 0;
}
else
{
int bufsize;
struct coding_system coding;
unsigned char *htext2;
if (NILP (Vnext_selection_coding_system))
Vnext_selection_coding_system = Vselection_coding_system;
setup_coding_system
(Fcheck_coding_system (Vnext_selection_coding_system), &coding);
if (SYMBOLP (coding.pre_write_conversion)
&& !NILP (Ffboundp (coding.pre_write_conversion)))
{
string = run_pre_post_conversion_on_str (string, &coding, 1);
src = SDATA (string);
nbytes = SBYTES (string);
}
coding.src_multibyte = 1;
coding.dst_multibyte = 0;
Vnext_selection_coding_system = Qnil;
coding.mode |= CODING_MODE_LAST_BLOCK;
Vlast_coding_system_used = coding.symbol;
bufsize = encoding_buffer_size (&coding, nbytes);
dst = (unsigned char *) xmalloc (bufsize);
encode_coding (&coding, src, dst, nbytes, bufsize);
no_crlf_conversion = 1;
nbytes = coding.produced;
src = dst;
}
if (!open_clipboard ())
goto error;
ok = empty_clipboard ()
&& ((put_status
= set_clipboard_data (CF_OEMTEXT, src, nbytes, no_crlf_conversion))
== 0);
if (!no_crlf_conversion)
Vlast_coding_system_used = Qraw_text;
close_clipboard ();
if (ok) goto unblock;
error:
ok = 0;
unblock:
if (dst)
xfree (dst);
UNBLOCK_INPUT;
if (put_status)
{
switch (put_status)
{
case 1:
message2 (no_mem_msg, sizeof (no_mem_msg) - 1, 0);
break;
case 2:
message2 (binary_msg, sizeof (binary_msg) - 1, 0);
break;
case 3:
message2 (system_error_msg, sizeof (system_error_msg) - 1, 0);
break;
}
sit_for (make_number (2), 0, 2);
}
done:
return (ok && put_status == 0 ? string : Qnil);
}
DEFUN ("w16-get-clipboard-data", Fw16_get_clipboard_data, Sw16_get_clipboard_data, 0, 1, 0,
doc: )
(frame)
Lisp_Object frame;
{
unsigned data_size, truelen;
unsigned char *htext;
Lisp_Object ret = Qnil;
int no_crlf_conversion, require_encoding = 0;
if (NILP (frame))
frame = Fselected_frame ();
CHECK_LIVE_FRAME (frame);
if ( !FRAME_MSDOS_P (XFRAME (frame)))
goto done;
BLOCK_INPUT;
if (!open_clipboard ())
goto unblock;
if ((data_size = get_clipboard_data_size (CF_OEMTEXT)) == 0 ||
(htext = (unsigned char *)xmalloc (data_size)) == 0)
goto closeclip;
if ((truelen = get_clipboard_data (CF_OEMTEXT, htext, data_size, 0)) == 0)
goto closeclip;
{
int i;
for (i = 0; i < truelen; i++)
{
if (htext[i] >= 0x80)
{
require_encoding = 1;
break;
}
}
}
if (require_encoding)
{
int bufsize;
unsigned char *buf;
struct coding_system coding;
if (NILP (Vnext_selection_coding_system))
Vnext_selection_coding_system = Vselection_coding_system;
setup_coding_system
(Fcheck_coding_system (Vnext_selection_coding_system), &coding);
coding.src_multibyte = 0;
coding.dst_multibyte = 1;
Vnext_selection_coding_system = Qnil;
coding.mode |= CODING_MODE_LAST_BLOCK;
coding.composing = COMPOSITION_DISABLED;
truelen = get_clipboard_data (CF_OEMTEXT, htext, data_size, 1);
bufsize = decoding_buffer_size (&coding, truelen);
buf = (unsigned char *) xmalloc (bufsize);
decode_coding (&coding, htext, buf, truelen, bufsize);
ret = make_string_from_bytes ((char *) buf,
coding.produced_char, coding.produced);
xfree (buf);
if (SYMBOLP (coding.post_read_conversion)
&& !NILP (Ffboundp (coding.post_read_conversion)))
ret = run_pre_post_conversion_on_str (ret, &coding, 0);
Vlast_coding_system_used = coding.symbol;
}
else
{
ret = make_unibyte_string ((char *) htext, truelen);
Vlast_coding_system_used = Qraw_text;
}
xfree (htext);
closeclip:
close_clipboard ();
unblock:
UNBLOCK_INPUT;
done:
return (ret);
}
DEFUN ("x-selection-exists-p", Fx_selection_exists_p, Sx_selection_exists_p,
0, 1, 0,
doc: )
(selection)
Lisp_Object selection;
{
CHECK_SYMBOL (selection);
if ((EQ (selection, Qnil) || EQ (selection, QPRIMARY))
&& ! NILP (SYMBOL_VALUE (Fintern_soft (build_string ("kill-ring"),
Qnil))))
return Qt;
if (EQ (selection, QCLIPBOARD))
{
Lisp_Object val = Qnil;
if (open_clipboard ())
{
if (get_clipboard_data_size (CF_OEMTEXT))
val = Qt;
close_clipboard ();
}
return val;
}
return Qnil;
}
void
syms_of_win16select ()
{
defsubr (&Sw16_set_clipboard_data);
defsubr (&Sw16_get_clipboard_data);
defsubr (&Sx_selection_exists_p);
DEFVAR_LISP ("selection-coding-system", &Vselection_coding_system,
doc: );
Vselection_coding_system = intern ("iso-latin-1-dos");
DEFVAR_LISP ("next-selection-coding-system", &Vnext_selection_coding_system,
doc: );
Vnext_selection_coding_system = Qnil;
QPRIMARY = intern ("PRIMARY"); staticpro (&QPRIMARY);
QCLIPBOARD = intern ("CLIPBOARD"); staticpro (&QCLIPBOARD);
}
#endif