1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
/************************************************************************/
/* */
/* Simple io streams using X11 windows properties. */
/* */
/************************************************************************/
# include "appFrameConfig.h"
# include <stdlib.h>
# include <sioXprop.h>
# include <appDebugon.h>
# ifdef USE_MOTIF
# include <X11/Xlib.h>
# include <X11/Xatom.h>
static Atom XA_XV_CLIPBOARD= None;
/************************************************************************/
/* */
/* Refuse to seek on copy/paste streams. */
/* */
/************************************************************************/
static int sioXpropSeek( void * voidp,
long pos )
{ LDEB(pos); return -1; }
/************************************************************************/
/* */
/* Input.. Paste. */
/* */
/************************************************************************/
typedef struct XpropPasteStream
{
Display * xpsDisplay;
APP_WINDOW xpsWindow;
APP_ATOM xpsProperty;
long xpsOffset;
long xpsExhausted;
int xpsDeleteOnClose;
} XpropPasteStream;
static int sioXpropPasteClose( void * voidxps )
{
XpropPasteStream * xps= (XpropPasteStream *)voidxps;
if ( xps->xpsDeleteOnClose )
{
XDeleteProperty( xps->xpsDisplay, xps->xpsWindow, xps->xpsProperty );
}
free( voidxps );
return 0;
}
static int sioInXpropReadBytes( void * voidxps,
unsigned char * buffer,
int count )
{
XpropPasteStream * xps= (XpropPasteStream *)voidxps;
Atom typeFound;
int formatFound;
unsigned long itemsReturned;
unsigned long itemsLeft;
unsigned char * dataReturned;
int ret;
if ( xps->xpsExhausted )
{ return -1; }
ret= XGetWindowProperty( xps->xpsDisplay, xps->xpsWindow, xps->xpsProperty,
xps->xpsOffset/4, count/4, False, AnyPropertyType,
&typeFound, &formatFound,
&itemsReturned, &itemsLeft, &dataReturned );
if ( ret != Success )
{ LLDEB(ret,Success); return -1; }
if ( itemsReturned == 0 )
{ LDEB(itemsReturned); return -1; }
if ( formatFound != 8 )
{ LLDEB(typeFound,formatFound); return -1; }
memcpy( buffer, dataReturned, itemsReturned );
XFree( dataReturned );
xps->xpsOffset += itemsReturned;
if ( itemsLeft == 0 )
{ xps->xpsExhausted= 1; }
return itemsReturned;
}
SimpleInputStream * sioInOpenPaste( APP_WIDGET w,
APP_EVENT * event )
{
XSelectionEvent * selEvent= &(event->xselection);
SimpleInputStream * sis;
XpropPasteStream * xps;
xps= (XpropPasteStream *)malloc( sizeof( XpropPasteStream ) );
if ( ! xps )
{ XDEB(xps); return (SimpleInputStream *)0; }
xps->xpsDisplay= XtDisplay( w );
xps->xpsWindow= XtWindow( w );
xps->xpsProperty= selEvent->property;
xps->xpsOffset= 0L;
xps->xpsExhausted= 0;
xps->xpsDeleteOnClose= 1;
sis= sioInOpen( (void *)xps,
sioInXpropReadBytes, sioXpropSeek, sioXpropPasteClose );
if ( ! sis )
{ XDEB(sis); free( xps ); return (SimpleInputStream *)0; }
return sis;
}
SimpleInputStream * sioInOpenXvPaste( APP_WIDGET w )
{
Display * display= XtDisplay( w );
int screen= DefaultScreen( display );
SimpleInputStream * sis;
XpropPasteStream * xps;
if ( XA_XV_CLIPBOARD == None )
{
XA_XV_CLIPBOARD= XInternAtom( display, "XV_CLIPBOARD", False );
if ( XA_XV_CLIPBOARD == None )
{ XDEB(XA_XV_CLIPBOARD); return (SimpleInputStream *)0; }
}
xps= (XpropPasteStream *)malloc( sizeof( XpropPasteStream ) );
if ( ! xps )
{ XDEB(xps); return (SimpleInputStream *)0; }
xps->xpsDisplay= display;
xps->xpsWindow= XRootWindow( display, screen );
xps->xpsProperty= XA_XV_CLIPBOARD;
xps->xpsOffset= 0L;
xps->xpsExhausted= 0;
xps->xpsDeleteOnClose= 0;
sis= sioInOpen( (void *)xps,
sioInXpropReadBytes, sioXpropSeek, sioXpropPasteClose );
if ( ! sis )
{ XDEB(sis); free( xps ); return (SimpleInputStream *)0; }
return sis;
}
/************************************************************************/
/* */
/* Output.. Copy. */
/* */
/************************************************************************/
typedef struct XpropCopyStream
{
Display * xcsDisplay;
APP_WINDOW xcsRequestor;
Atom xcsProperty;
Atom xcsTarget;
int xcsPropMode;
} XpropCopyStream;
static int sioXpropCopyClose( void * voidxcs )
{
free( voidxcs );
return 0;
}
static int sioOutCopyWriteBytes( void * voidxcs,
const unsigned char * buffer,
int count )
{
XpropCopyStream * xcs= (XpropCopyStream *)voidxcs;
XChangeProperty( xcs->xcsDisplay, xcs->xcsRequestor,
xcs->xcsProperty, xcs->xcsTarget,
8, xcs->xcsPropMode, buffer, count );
xcs->xcsPropMode= PropModeAppend;
return count;
}
SimpleOutputStream * sioOutOpenCopy( APP_WIDGET w,
APP_EVENT * event )
{
XSelectionEvent * selEvent= &(event->xselection);
SimpleOutputStream * sos;
XpropCopyStream * xcs;
xcs= (XpropCopyStream *)malloc( sizeof( XpropCopyStream ) );
if ( ! xcs )
{ XDEB(xcs); return (SimpleOutputStream *)0; }
xcs->xcsDisplay= XtDisplay( w );
xcs->xcsRequestor= selEvent->requestor;
xcs->xcsProperty= selEvent->property;
xcs->xcsTarget= selEvent->target;
xcs->xcsPropMode= PropModeReplace;
sos= sioOutOpen( (void *)xcs, sioOutCopyWriteBytes,
sioXpropSeek, sioXpropCopyClose );
if ( ! sos )
{ XDEB(sos); free( xcs ); return (SimpleOutputStream *)0; }
return sos;
}
SimpleOutputStream * sioOutOpenXvCopy( APP_WIDGET w )
{
Display * display= XtDisplay( w );
int screen= DefaultScreen( display );
SimpleOutputStream * sos;
XpropCopyStream * xcs;
if ( ! XA_XV_CLIPBOARD )
{
XA_XV_CLIPBOARD= XInternAtom( display, "XV_CLIPBOARD", False );
if ( ! XA_XV_CLIPBOARD )
{ XDEB(XA_XV_CLIPBOARD); return (SimpleOutputStream *)0; }
}
xcs= (XpropCopyStream *)malloc( sizeof( XpropCopyStream ) );
if ( ! xcs )
{ XDEB(xcs); return (SimpleOutputStream *)0; }
xcs->xcsDisplay= XtDisplay( w );
xcs->xcsRequestor= XRootWindow( display, screen );
xcs->xcsProperty= XA_XV_CLIPBOARD;
xcs->xcsTarget= XA_STRING;
xcs->xcsPropMode= PropModeReplace;
sos= sioOutOpen( (void *)xcs, sioOutCopyWriteBytes,
sioXpropSeek, sioXpropCopyClose );
if ( ! sos )
{ XDEB(sos); free( xcs ); return (SimpleOutputStream *)0; }
return sos;
}
# endif
|