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
|
/************************************************************************/
/* */
/* Simple io streams using the C stdio. */
/* */
/************************************************************************/
# include "config.h"
# include <stdlib.h>
# include <X11/Xlib.h>
# include <X11/Xatom.h>
# include <sioXprop.h>
# include <debugon.h>
static int sioXpropClose( void * voidp )
{ free( voidp ); return 0; }
static int sioXpropSeek( void * voidp,
long pos )
{ LDEB(pos); return -1; }
typedef struct XpropInputStream
{
Display * xisDisplay;
Window xisWindow;
Atom xisProperty;
long xisOffset;
long xisExhausted;
} XpropInputStream;
static int sioInXpropReadBytes( void * voidxis,
unsigned char * buffer,
int count )
{
XpropInputStream * xis= (XpropInputStream *)voidxis;
Atom typeFound;
int formatFound;
unsigned long itemsReturned;
unsigned long itemsLeft;
unsigned char * dataReturned;
int ret;
if ( xis->xisExhausted )
{ return -1; }
ret= XGetWindowProperty( xis->xisDisplay, xis->xisWindow, xis->xisProperty,
xis->xisOffset/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 );
xis->xisOffset += itemsReturned;
if ( itemsLeft == 0 )
{ xis->xisExhausted= 1; }
return itemsReturned;
}
SimpleInputStream * sioInXpropOpen( Display * display,
Window win,
Atom property )
{
SimpleInputStream * sis;
XpropInputStream * xis;
xis= (XpropInputStream *)malloc( sizeof( XpropInputStream ) );
if ( ! xis )
{ XDEB(xis); return (SimpleInputStream *)0; }
xis->xisDisplay= display;
xis->xisWindow= win;
xis->xisProperty= property;
xis->xisOffset= 0L;
xis->xisExhausted= 0;
sis= sioInOpen( (void *)xis,
sioInXpropReadBytes, sioXpropSeek, sioXpropClose );
if ( ! sis )
{ XDEB(sis); free( xis ); return (SimpleInputStream *)0; }
return sis;
}
int sioInXpropEraseAndClose( SimpleInputStream * sis )
{
int rval= 0;
XpropInputStream * xis= (XpropInputStream *)sis->sisPrivate;
Atom typeFound;
int formatFound;
unsigned long itemsReturned;
unsigned long itemsLeft;
unsigned char * dataReturned;
int ret;
ret= XGetWindowProperty( xis->xisDisplay, xis->xisWindow, xis->xisProperty,
0L, 0, True, AnyPropertyType,
&typeFound, &formatFound,
&itemsReturned, &itemsLeft, &dataReturned );
if ( ret != Success )
{ LLDEB(ret,Success); rval= -1; }
if ( sioInClose( sis ) )
{ LDEB(1); rval= -1; }
return rval;
}
typedef struct XpropOutputStream
{
Display * xosDisplay;
Window xosRequestor;
Atom xosProperty;
Atom xosTarget;
int xosPropMode;
} XpropOutputStream;
static int sioOutXpropWriteBytes( void * voidxsc,
const unsigned char * buffer,
int count )
{
XpropOutputStream * xos= (XpropOutputStream *)voidxsc;
XChangeProperty( xos->xosDisplay, xos->xosRequestor,
xos->xosProperty, xos->xosTarget,
8, xos->xosPropMode, buffer, count );
xos->xosPropMode= PropModeAppend;
return count;
}
SimpleOutputStream * sioOutXpropOpen( Display * display,
Window requestor,
Atom property,
Atom target )
{
SimpleOutputStream * sos;
XpropOutputStream * xos;
xos= (XpropOutputStream *)malloc( sizeof( XpropOutputStream ) );
if ( ! xos )
{ XDEB(xos); return (SimpleOutputStream *)0; }
xos->xosDisplay= display;
xos->xosRequestor=requestor;
xos->xosProperty=property;
xos->xosTarget=target;
xos->xosPropMode= PropModeReplace;
sos= sioOutOpen( (void *)xos, sioOutXpropWriteBytes,
sioXpropSeek, sioXpropClose );
if ( ! sos )
{ XDEB(sos); free( xos ); return (SimpleOutputStream *)0; }
return sos;
}
|