Subject: Re: Xlib in perl

From: [email-masked]
Date: Mon, 10 Dec 90 07:55:51 EST

Hi -

I have been using perl in conjunction with X11R4 for a while now, but
not in quite the same manner as you have. Enclosed you will find a
bunch of perl code that I use, with which you can do what you want,
although I would like to be acknowledged as a contributor in any
postings you may make. Essentially, everything that you would need for
the protocol layer is defined, with the exception of the replies.
Replies would be strait forward to add though, as I assume you will
discover. Let me know if this is useful (although I know that it is a bit ugly)...

#!/usr/dec/perl

# Global variables used in this package include:
#
#    $Name{'name'}            eval flavor of a specific item name space
#    $Size{'name'}            byte count of a specific item
#    $Type{'type'}            simple type definition
#    $Template{'name'}        pack/unpack flavor of name's structure
#    @XEventCodes             names of all events mapped to code number
#    @XRequestCodes           names of all requests mapped to code number
#    @<XEventName>            elements of specific event structures
#    @<XRequestName>          elements of specific request structures
#    @XTrapHeader             elements of the XTrap header structure
#    $XTrapHeaderTypes[num]   equates for use in decomposing XTrap headers
#

#    ARGS:             <none>
#    RETURN:           <nothing meaningful>
#    VARIABLES SET:    $Size{'name'}
#                      $Template{'name'}
sub XTypeSizeInit
{
    # all types decompose to one of these Size elements
    $Size{'INT32'}	= 4;
    $Template{'INT32'}	= 'l';
    $Size{'INT16'}	= 2;
    $Template{'INT16'}	= 's';
    $Size{'INT8'}	= 1;
    $Template{'INT8'}	= 'c';
    $Size{'CARD32'}	= 4;
    $Template{'CARD32'}	= 'L';
    $Size{'CARD16'}	= 2;
    $Template{'CARD16'}	= 'S';
    $Size{'CARD8'}	= 1;
    $Template{'CARD8'}	= 'C';
    $Size{'BITS32'}	= 4;
    $Template{'BITS32'}	= 'L';
    $Size{'BITS16'}	= 2;
    $Template{'BITS16'}	= 'S';
    $Size{'BYTE'}	= 1;
    $Template{'BYTE'}	= 'C';
    $Size{'BOOL'}	= 1;
    $Template{'BOOL'}	= 'C';
}

#    ARGS:             <none>
#    RETURN:           <nothing meaningful>
#    VARIABLES SET:    $Type{'name'}
sub XSimpleTypeInit
{
    $Type{'Window'}	= "CARD32";
    $Type{'Drawable'}	= "CARD32";
    $Type{'Font'}	= "CARD32";
    $Type{'Pixmap'}	= "CARD32";
    $Type{'Cursor'}	= "CARD32";
    $Type{'Colormap'}	= "CARD32";
    $Type{'GContext'}	= "CARD32";
    $Type{'Atom'}	= "CARD32";
    $Type{'VisualID'}	= "CARD32";
    $Type{'Time'}	= "CARD32";
    $Type{'KeyCode'}	= "CARD8";
    $Type{'KeySym'}	= "CARD32";
    $Type{'KeyButMask'}	= "CARD16";
}

#    ARGS:             <none>
#    RETURN:           <nothing meaningful>
#    VARIABLES SET:    @<XRequestName>
#                      @XRequestCodes
#                      $Size{'<XRequestName>'}
#                      $Name{'<XRequestName>'}
#                      $Template{'<XRequestName>'}
sub XProtoRequestInit
{
    @X_CreateWindow = ("CARD8 reqType","CARD8 depth","CARD16 length",
        	       "Window wid","Window parent","INT16 x","INT16 y",
        	       "CARD16 width","CARD16 height","CARD16 borderWidth",
        	       "CARD16 class","VisualID visual","CARD32 mask");
    @X_ChangeWindowAttributes = ("CARD8 reqType","BYTE pad","CARD16 length",
        			 "Window window","CARD32 valueMask");
    @X_GetWindowAttributes = ("CARD8 reqType","BYTE pad","CARD16 length",
        		      "Window wid");
    @X_DestroyWindow = ("CARD8 reqType","BYTE pad","CARD16 length",
        		"Window wid");
    @X_DestroySubwindows = ("CARD8 reqType","BYTE pad","CARD16 length",
        		    "Window wid");
    @X_ChangeSaveSet = ("CARD8 reqType","BYTE mode","CARD16 length",
        		"Window window");
    @X_ReparentWindow = ("CARD8 reqType","BYTE pad","CARD16 length",
        		 "Window window","Window parent","INT16 x","INT16 y");
    @X_MapWindow = ("CARD8 reqType","BYTE pad","CARD16 length","Window wid");
    @X_MapSubwindows = ("CARD8 reqType","BYTE pad","CARD16 length",
        		"Window wid");
    @X_UnmapWindow = ("CARD8 reqType","BYTE pad","CARD16 length","Window wid");
    @X_UnmapSubwindows = ("CARD8 reqType","BYTE pad","CARD16 length",
        		  "Window wid");
    @X_ConfigureWindow = ("CARD8 reqType","CARD8 pad","CARD16 length",
        		  "Window window","CARD16 mask","CARD16 pad2");
    @X_CirculateWindow = ("CARD8 reqType","CARD8 direction","CARD16 length",
        		  "Window window");
    @X_GetGeometry = ("CARD8 reqType","CARD8 direction","CARD16 length",
        	      "Drawable drawable");
    @X_QueryTree = ("CARD8 reqType","CARD8 direction","CARD16 length",
        	    "Window window");
    @X_InternAtom = ("CARD8 reqType","BOOL onlyIfExists","CARD16 length",
        	     "CARD16 nbytes","CARD16 pad");
    @X_GetAtomName = ("CARD8 reqType","BOOL onlyIfExists","CARD16 length",
        	      "Atom atom");
    @X_ChangeProperty = ("CARD8 reqType","CARD8 mode","CARD16 length",
        		 "Window window","Atom property","Atom type",
        		 "CARD8 format","BYTE pad1","BYTE pad2","BYTE pad3",
        		 "CARD32 nUnits");
    @X_DeleteProperty = ("CARD8 reqType","BYTE pad","CARD16 length",
        		 "Window window","Atom property");
    @X_GetProperty = ("CARD8 reqType","BOOL delete","CARD16 length",
        	      "Window window","Atom property","Atom type",
        	      "CARD32 longOffset","CARD32 longLength");
    @X_ListProperties = ("CARD8 reqType","CARD8 direction","CARD16 length",
        		 "Window window");
    @X_SetSelectionOwner = ("CARD8 reqType","BYTE pad","CARD16 length",
        		    "Window window","Atom selection","Time time");
    @X_GetSelectionOwner = ("CARD8 reqType","BYTE pad","CARD16 length",
        		    "Atom selection");
    @X_ConvertSelection = ("CARD8 reqType","BYTE pad","CARD16 length",
        		   "Window requestor","Atom selection","Atom target",
        		   "Atom property","Time time");
    @X_SendEvent = ("CARD8 reqType","BOOL propagate","CARD16 length",
        	    "Window destination","CARD32 eventMask","xEvent event");

    @X_GrabPointer = ("CARD8 reqType","BOOL ownerEvents","CARD16 length",
        	      "Window grabWindow","CARD16 eventMask",
        	      "BYTE pointerMode","BYTE keyboardMode",
                      "Window confineTo","Cursor cursor","Time time");
    @X_UngrabPointer = ("CARD8 reqType","BOOL ownerEvents","CARD16 length",
        		"Time time");
    @X_GrabButton = ("CARD8 reqType","BOOL ownerEvents","CARD16 length",
        	     "Window grabWindow","CARD16 eventMask",
        	     "BYTE pointerMode","BYTE keyboardMode",
        	     "Window confineTo","Cursor cursor","CARD8 button",
        	     "BYTE pad","CARD16 modifiers");
    @X_UngrabButton = ("CARD8 reqType","CARD8 button","CARD16 length",
        	       "Window grabWindow","CARD16 modifiers","CARD16 pad");
    @X_ChangeActivePointerGrab = ("CARD8 reqType","BYTE pad","CARD16 length",
        			  "Cursor cursor","Time time",
        			  "CARD16 eventMask","CARD16 pad2");
    @X_GrabKeyboard = ("CARD8 reqType","BOOL ownerEvents","CARD16 length",
        	       "Window grabWindow","Time time","BYTE pointerMode",
        	       "BYTE keyboardMode","CARD16 pad");
    @X_UngrabKeyboard = ("CARD8 reqType","BOOL ownerEvents","CARD16 length",
        		 "Time time");
    @X_GrabKey = ("CARD8 reqType","BOOL ownerEvents","CARD16 length",
        	  "Window grabWindow","CARD16 modifiers","CARD8 key",
        	  "BYTE pointerMode","BYTE keyboardMode","BYTE pad1",
        	  "BYTE pad2","BYTE pad3");
    @X_UngrabKey = ("CARD8 reqType","CARD8 key","CARD16 length",
        	    "Window grabWindow","CARD16 modifiers","CARD16 pad");
    @X_AllowEvents = ("CARD8 reqType","CARD8 mode","CARD16 length",
        	      "Time time");
    @X_GrabServer = ("CARD8 reqType","CARD8 mode","CARD16 length");
    @X_UngrabServer = ("CARD8 reqType","CARD8 mode","CARD16 length");
    @X_QueryPointer = ("CARD8 reqType","CARD8 mode","CARD16 length",
        	       "Window window");
    @X_GetMotionEvents = ("CARD8 reqType","BYTE pad","CARD16 length",
        		  "Window window","Time start","Time stop");
    @X_TranslateCoords = ("CARD8 reqType","BYTE pad","CARD16 length",
        		  "Window srcWid","Window dstWid","INT16 srcX",
        		  "INT16 srcY");
    @X_WarpPointer = ("CARD8 reqType","BYTE pad","CARD16 length",
        	      "Window srcWid","Window dstWid","INT16 srcX",
        	      "INT16 srcY","CARD16 srcWidth","CARD16 srcHeight",
        	      "INT16 dstX","INT16 dstY");
    @X_SetInputFocus = ("CARD8 reqType","CARD8 revertTo","CARD16 length",
        		"Window focus","Time time");
    @X_GetInputFocus = ("CARD8 reqType","CARD8 revertTo","CARD16 length");
    @X_QueryKeymap = ("CARD8 reqType","CARD8 revertTo","CARD16 length");
    @X_OpenFont = ("CARD8 reqType","BYTE pad","CARD16 length","Font fid",
        	   "CARD16 nbytes","BYTE pad1","BYTE pad2");
    @X_CloseFont = ("CARD8 reqType","BYTE pad","CARD16 length","Font fid");
    @X_QueryFont = ("CARD8 reqType","BYTE pad","CARD16 length","Fontable font");
    @X_QueryTextExtents = ("CARD8 reqType","BOOL oddLength","CARD16 length",
        		   "Font fid");
    @X_ListFonts = ("CARD8 reqType","BYTE pad","CARD16 length",
        	    "CARD16 maxNames","CARD16 nbytes");
    @X_ListFontsWithInfo = ("CARD8 reqType","BYTE pad","CARD16 length",
        	            "CARD16 maxNames","CARD16 nbytes");
    @X_SetFontPath = ("CARD8 reqType","BYTE pad","CARD16 length",
        	      "CARD16 nFonts","BYTE pad1","BYTE pad2");
    @X_GetFontPath = ("CARD8 reqType","BYTE pad","CARD16 length");
    @X_CreatePixmap = ("CARD8 reqType","CARD8 depth","CARD16 length",
        	       "Pixmap pid","Drawable drawable","CARD16 width",
        	       "CARD16 height");
    @X_FreePixmap = ("CARD8 reqType","CARD8 depth","CARD16 length",
        	     "Pixmap pid");
    @X_CreateGC = ("CARD8 reqType","BYTE pad","CARD16 length","GContext gc",
        	   "Drawable drawable","CARD32 mask");
    @X_ChangeGC = ("CARD8 reqType","BYTE pad","CARD16 length","GContext gc",
        	   "CARD32 mask");
    @X_CopyGC = ("CARD8 reqType","BYTE pad","CARD16 length","GContext srcGC",
        	 "GContext dstGC","CARD32 mask");
    @X_SetDashes = ("CARD8 reqType","BYTE pad","CARD16 length","GContext gc",
        	    "CARD16 dashOffset","CARD16 nDashes");
    @X_SetClipRectangles = ("CARD8 reqType","BYTE ordering","CARD16 length",
        		    "GContext gc","INT16 xOrigin","INT16 yOrigin");
    @X_FreeGC = ("CARD8 reqType","BYTE pad","CARD16 length","GContext gc");
    @X_ClearArea = ("CARD8 reqType","BOOL exposures","CARD16 length",
        	    "Window window","INT16 x","INT16 y","CARD16 width",
        	    "CARD16 height");
    @X_CopyArea = ("CARD8 reqType","BYTE pad","CARD16 length",
        	   "Drawable srcDrawable","Drawable dstDrawable",
        	   "GContext gc","INT16 srcX","INT16 srcY","INT16 dstX",
        	   "INT16 dstY","CARD16 width","CARD16 height");
    @X_CopyPlane = ("CARD8 reqType","BYTE pad","CARD16 length",
        	    "Drawable srcDrawable","Drawable dstDrawable",
        	    "GContext gc","INT16 srcX","INT16 srcY","INT16 dstX",
        	    "INT16 dstY","CARD16 width","CARD16 height",
        	    "CARD32 bitPlane");
    @X_PolyPoint = ("CARD8 reqType","BYTE coordMode","CARD16 length",
        	    "Drawable drawable","GContext gc");
    @X_PolyLine = ("CARD8 reqType","BYTE coordMode","CARD16 length",
                   "Drawable drawable","GContext gc");
    @X_PolySegment = ("CARD8 reqType","BYTE pad","CARD16 length",
        	      "Drawable drawable","GContext gc");
    @X_PolyRectangle = ("CARD8 reqType","BYTE pad","CARD16 length",
        		"Drawable drawable","GContext gc");
    @X_PolyArc = ("CARD8 reqType","BYTE pad","CARD16 length",
        	  "Drawable drawable","GContext gc");
    @X_FillPoly = ("CARD8 reqType","BYTE pad","CARD16 length",
        	   "Drawable drawable","GContext gc","BYTE shape",
        	   "BYTE coordMode","CARD16 pad1");
    @X_PolyFillRectangle = ("CARD8 reqType","BYTE pad","CARD16 length",
        		    "Drawable drawable","GContext gc");
    @X_PolyFillArc = ("CARD8 reqType","BYTE pad","CARD16 length",
        	      "Drawable drawable","GContext gc");
    @X_PutImage = ("CARD8 reqType","CARD8 format","CARD16 length",
        	   "Drawable drawable","GContext gc","CARD16 width",
        	   "CARD16 height","INT16 dstX","INT16 dstY","CARD8 leftPad",
        	   "CARD8 depth","CARD16 pad");
    @X_GetImage = ("CARD8 reqType","CARD8 format","CARD16 length",
        	   "Drawable drawable","INT16 x","INT16 y","CARD16 width",
        	   "CARD16 height","CARD32 planeMask");
    @X_PolyText8 = ("CARD8 reqType","CARD8 pad","CARD16 length",
        	    "Drawable drawable","GContext gc","INT16 x","INT16 y");
    @X_PolyText16 = ("CARD8 reqType","CARD8 pad","CARD16 length",
        	     "Drawable drawable","GContext gc","INT16 x","INT16 y");
    @X_ImageText8 = ("CARD8 reqType","BYTE nChars","CARD16 length",
        	     "Drawable drawable","GContext gc","INT16 x","INT16 y");
    @X_ImageText16 = ("CARD8 reqType","BYTE nChars","CARD16 length",
        	      "Drawable drawable","GContext gc","INT16 x","INT16 y");
    @X_CreateColormap = ("CARD8 reqType","BYTE alloc","CARD16 length",
        		 "Colormap mid","Window window","VisualID visual");
    @X_FreeColormap = ("CARD8 reqType","BYTE alloc","CARD16 length",
        	       "Colormap mid");
    @X_CopyColormapAndFree = ("CARD8 reqType","BYTE pad","CARD16 length",
        		      "Colormap mid","Colormap srcCmap");
    @X_InstallColormap = ("CARD8 reqType","BYTE alloc","CARD16 length",
        	          "Colormap mid");
    @X_UninstallColormap = ("CARD8 reqType","BYTE alloc","CARD16 length",
        		    "Colormap mid");
    @X_ListInstalledColormaps = ("CARD8 reqType","BYTE alloc","CARD16 length",
        			 "Window wid");
    @X_AllocColor = ("CARD8 reqType","BYTE pad","CARD16 length",
        	     "Colormap cmap","CARD16 red","CARD16 green",
        	     "CARD16 blue","CARD16 pad2");
    @X_AllocNamedColor = ("CARD8 reqType","BYTE pad","CARD16 length",
        		  "Colormap cmap","CARD16 nbytes","BYTE pad1",
        		  "BYTE pad2");
    @X_AllocColorCells = ("CARD8 reqType","BOOL contiguous","CARD16 length",
        		  "Colormap cmap","CARD16 colors","CARD16 planes");
    @X_AllocColorPlanes = ("CARD8 reqType","BOOL contiguous","CARD16 length",
        		   "Colormap cmap","CARD16 colors","CARD16 red",
        		   "CARD16 green","CARD16 blue");
    @X_FreeColors = ("CARD8 reqType","BYTE pad","CARD16 length",
        	     "Colormap cmap","CARD32 planeMask");
    @X_StoreColors = ("CARD8 reqType","BYTE pad","CARD16 length",
        	      "Colormap cmap");
    @X_StoreNamedColor = ("CARD8 reqType","CARD8 flags","CARD16 length",
        		  "Colormap cmap","CARD32 pixel","CARD16 nbytes",
        		  "BYTE pad1","BYTE pad2");
    @X_QueryColors = ("CARD8 reqType","BYTE pad","CARD16 length",
        	      "Colormap cmap");
    @X_LookupColor = ("CARD8 reqType","BYTE pad","CARD16 length",
        	      "Colormap cmap","CARD16 nbytes","BYTE pad1",
        	      "BYTE pad2");
    @X_CreateCursor = ("CARD8 reqType","BYTE pad","CARD16 length",
        	       "Cursor cid","Pixmap source","Pixmap mask",
        	       "CARD16 foreRed","CARD16 foreGreen","CARD16 foreBlue",
        	       "CARD16 backRed","CARD16 backGreen","CARD16 backBlue",
        	       "CARD16 x","CARD16 y");
    @X_CreateGlyphCursor = ("CARD8 reqType","BYTE pad","CARD16 length",
        		    "Cursor cid","Font source","Font mask",
        		    "CARD16 sourceChar","CARD16 maskChar",
        		    "CARD16 foreRed","CARD16 foreGreen",
        		    "CARD16 foreBlue","CARD16 backRed",
        		    "CARD16 backGreen","CARD16 backBlue");
    @X_FreeCursor = ("CARD8 reqType","BYTE pad","CARD16 length",
        	     "Cursor cid");
    @X_RecolorCursor = ("CARD8 reqType","BYTE pad","CARD16 length",
        		"Cursor cursor","CARD16 foreRed","CARD16 foreGreen",
        		"CARD16 foreBlue","CARD16 backRed",
        		"CARD16 backGreen","CARD16 backBlue");
    @X_QueryBestSize = ("CARD8 reqType","CARD8 class","CARD16 length",
        		"Drawable drawable","CARD16 width","CARD16 height");
    @X_QueryExtension = ("CARD8 reqType","BYTE pad","CARD16 length",
        		 "CARD16 nbytes","BYTE pad1","BYTE pad2");
    @X_ListExtensions = ("CARD8 reqType","BYTE pad","CARD16 length");
    @X_ChangeKeyboardMapping = ("CARD8 reqType","CARD8 keyCodes",
        			"CARD16 length","KeyCode firstKeyCode",
        			"CARD8 keySymsPerKeyCode","CARD16 pad1");
    @X_GetKeyboardMapping = ("CARD8 reqType","BYTE pad","CARD16 length",
        		     "KeyCode firstKeyCode","CARD8 count",
        		     "CARD16 pad1");
    @X_ChangeKeyboardControl = ("CARD8 reqType","BYTE pad","CARD16 length",
        			"CARD32 mask");
    @X_GetKeyboardControl = ("CARD8 reqType","BYTE pad","CARD16 length");
    @X_Bell = ("CARD8 reqType","INT8 percent","CARD16 length");
    @X_ChangePointerControl = ("CARD8 reqType","BYTE pad","CARD16 length",
        		       "INT16 accelNum","INT16 accelDenum",
        		       "INT16 threshold","BOOL doAccel",
        		       "BOOL doThresh");
    @X_GetPointerControl = ("CARD8 reqType","BYTE pad","CARD16 length");
    @X_SetScreenSaver = ("CARD8 reqType","BYTE pad","CARD16 length",
        		 "INT16 timeout","INT16 interval","BYTE preferBlank",
        		 "BYTE allowExpose;","CARD16 pad2");
    @X_GetScreenSaver = ("CARD8 reqType","BYTE pad","CARD16 length");
    @X_ChangeHosts = ("CARD8 reqType","BYTE mode","CARD16 length",
        	      "CARD8 hostFamily","BYTE pad","CARD16 hostLength");
    @X_ListHosts = ("CARD8 reqType","BYTE pad","CARD16 length");
    @X_SetAccessControl = ("CARD8 reqType","BYTE mode","CARD16 length");
    @X_SetCloseDownMode = ("CARD8 reqType","BYTE mode","CARD16 length");
    @X_KillClient = ("CARD8 reqType","BYTE mode","CARD16 length",
        	     "CARD32 resource");
    @X_RotateProperties = ("CARD8 reqType","BYTE pad","CARD16 length",
        		   "Window window","CARD16 nAtoms","INT16 nPositions");
    @X_ForceScreenSaver = ("CARD8 reqType","BYTE mode","CARD16 length");
    @X_SetPointerMapping = ("CARD8 reqType","CARD8 nElts","CARD16 length");
    @X_GetPointerMapping = ("CARD8 reqType","CARD8 nElts","CARD16 length");
    @X_SetModifierMapping = ("CARD8 reqType","CARD8 numKeyPerModifier",
        		     "CARD16 length");
    @X_GetModifierMapping = ("CARD8 reqType","CARD8 numKeyPerModifier",
        		     "CARD16 length");
    @X_NoOperation = ("CARD8 reqType","BYTE pad","CARD16 length");

    @XRequestCodes = ("(unused)","X_CreateWindow","X_ChangeWindowAttributes",
                     "X_GetWindowAttributes","X_DestroyWindow",
                     "X_DestroySubwindows","X_ChangeSaveSet","X_ReparentWindow",
                     "X_MapWindow","X_MapSubwindows","X_UnmapWindow",
                     "X_UnmapSubwindows","X_ConfigureWindow",
                     "X_CirculateWindow","X_GetGeometry","X_QueryTree",
                     "X_InternAtom","X_GetAtomName","X_ChangeProperty",
                     "X_DeleteProperty","X_GetProperty","X_ListProperties",
                     "X_SetSelectionOwner","X_GetSelectionOwner",
        	     "X_ConvertSelection","X_SendEvent","X_GrabPointer",
                     "X_UngrabPointer","X_GrabButton","X_UngrabButton",
                     "X_ChangeActivePointerGrab","X_GrabKeyboard",
                     "X_UngrabKeyboard","X_GrabKey","X_UngrabKey",
                     "X_AllowEvents","X_GrabServer","X_UngrabServer",
                     "X_QueryPointer","X_GetMotionEvents","X_TranslateCoords",
                     "X_WarpPointer","X_SetInputFocus","X_GetInputFocus",
                     "X_QueryKeymap","X_OpenFont","X_CloseFont","X_QueryFont",
                     "X_QueryTextExtents","X_ListFonts","X_ListFontsWithInfo",
                     "X_SetFontPath","X_GetFontPath","X_CreatePixmap",
                     "X_FreePixmap","X_CreateGC","X_ChangeGC","X_CopyGC",
                     "X_SetDashes","X_SetClipRectangles","X_FreeGC",
                     "X_ClearArea","X_CopyArea","X_CopyPlane","X_PolyPoint",
                     "X_PolyLine","X_PolySegment","X_PolyRectangle",
                     "X_PolyArc","X_FillPoly","X_PolyFillRectangle",
                     "X_PolyFillArc","X_PutImage","X_GetImage","X_PolyText8",
                     "X_PolyText16","X_ImageText8","X_ImageText16",
                     "X_CreateColormap","X_FreeColormap",
        	     "X_CopyColormapAndFree","X_InstallColormap",
        	     "X_UninstallColormap","X_ListInstalledColormaps",
        	     "X_AllocColor","X_AllocNamedColor","X_AllocColorCells",
        	     "X_AllocColorPlanes","X_FreeColors","X_StoreColors",
        	     "X_StoreNamedColor","X_QueryColors","X_LookupColor",
        	     "X_CreateCursor","X_CreateGlyphCursor","X_FreeCursor",
        	     "X_RecolorCursor","X_QueryBestSize","X_QueryExtension",
        	     "X_ListExtensions","X_ChangeKeyboardMapping",
        	     "X_GetKeyboardMapping","X_ChangeKeyboardControl",
        	     "X_GetKeyboardControl","X_Bell","X_ChangePointerControl",
        	     "X_GetPointerControl","X_SetScreenSaver",
        	     "X_GetScreenSaver","X_ChangeHosts","X_ListHosts",
        	     "X_SetAccessControl","X_SetCloseDownMode","X_KillClient",
        	     "X_RotateProperties","X_ForceScreenSaver",
        	     "X_SetPointerMapping","X_GetPointerMapping",
        	     "X_SetModifierMapping","X_GetModifierMapping",
        	     "(unused)","(unused)","(unused)","(unused)","(unused)",
        	     "(unused)","(unused)","X_NoOperation");
    local(@tmp);
    foreach (@XRequestCodes)
    {   eval "next if (!defined(\@$_)); \@tmp = \@$_";
        ($Size{"$_"},$Template{"$_"}) = &DefToTemplate(@tmp);
        $Name{"$_"} = &DefToNames(@tmp);
    }
}

#    ARGS:             <none>
#    RETURN:           <nothing meaningful>
#    VARIABLES SET:    @<XEventName>
#                      @XEventCodes
#                      $Size{'<XEventName>'}
#                      $Name{'<XEventName>'}
#                      $Template{'<XEventName>'}
sub XProtoEventInit
{
    @KeyPress = ("BYTE type","BYTE detail","CARD16 sequenceNumber","Time time",
                 "Window root","Window event","Window child","INT16 rootX",
                 "INT16 rootY","INT16 eventX","INT16 eventY",
        	 "KeyButMask state","BOOL sameScreen","BYTE pad1");
    @KeyRelease = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        	   "Time time","Window root","Window event","Window child",
        	   "INT16 rootX","INT16 rootY","INT16 eventX","INT16 eventY",
        	   "KeyButMask state","BOOL sameScreen","BYTE pad1");
    @ButtonPress = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        	    "Time time","Window root","Window event","Window child",
        	    "INT16 rootX","INT16 rootY","INT16 eventX","INT16 eventY",
        	    "KeyButMask state","BOOL sameScreen","BYTE pad1");
    @ButtonRelease = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        	      "Time time","Window root","Window event","Window child",
        	      "INT16 rootX","INT16 rootY","INT16 eventX","INT16 eventY",
        	      "KeyButMask state","BOOL sameScreen","BYTE pad1");
    @MotionNotify = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        	     "Time time","Window root","Window event","Window child",
        	     "INT16 rootX","INT16 rootY","INT16 eventX","INT16 eventY",
        	     "KeyButMask state","BOOL sameScreen","BYTE pad1");
    @EnterNotify = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        	    "Time time","Window root","Window event","Window child",
        	    "INT16 rootX","INT16 rootY","INT16 eventX","INT16 eventY",
        	    "KeyButMask state","BYTE mode","BYTE flags");
    @LeaveNotify = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        	    "Time time","Window root","Window event","Window child",
        	    "INT16 rootX","INT16 rootY","INT16 eventX","INT16 eventY",
        	    "KeyButMask state","BYTE mode","BYTE flags");
    @FocusIn = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        	"Window window","BYTE mode","BYTE pad1","BYTE pad2",
        	"BYTE pad3","CARD32 pad4","CARD32 pad5","CARD32 pad6",
        	"CARD32 pad7","CARD32 pad8");
    @FocusOut = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        	 "Window window","BYTE mode","BYTE pad1","BYTE pad2",
        	 "BYTE pad3","CARD32 pad4","CARD32 pad5","CARD32 pad6",
        	 "CARD32 pad7","CARD32 pad8");
    @KeymapNotify = ("BYTE type","BYTE map0","BYTE map1","BYTE map2",
        	     "BYTE map3","BYTE map4","BYTE map5","BYTE map6",
        	     "BYTE map7","BYTE map8","BYTE map9","BYTE map10",
        	     "BYTE map11","BYTE map12","BYTE map13","BYTE map14",
        	     "BYTE map15","BYTE map16","BYTE map17","BYTE map18",
        	     "BYTE map19","BYTE map20","BYTE map21","BYTE map22",
        	     "BYTE map23","BYTE map24","BYTE map25","BYTE map26",
        	     "BYTE map27","BYTE map28","BYTE map29","BYTE map30");
    @Expose = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
               "Window window","CARD16 x","CARD16 y","CARD16 width",
               "CARD16 height","CARD16 count","CARD16 pad2","CARD32 pad3",
               "CARD32 pad4","CARD32 pad5");
    @GraphicsExpose = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        	       "Drawable drawable","CARD16 x","CARD16 y",
        	       "CARD16 width","CARD16 height","CARD16 minorEvent",
        	       "CARD16 count","BYTE majorEvent","BYTE pad1",
        	       "BYTE pad2","BYTE pad3","CARD32 pad4","CARD32 pad5");
    @NoExpose = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        	 "Drawable drawable","CARD16 minorEvent","BYTE majorEvent",
        	 "BYTE bpad","CARD32 pad1","CARD32 pad2","CARD32 pad3",
        	 "CARD32 pad4","CARD32 pad5");
    @VisibilityNotify = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        		 "Window window","CARD8 state","BYTE pad1","BYTE pad2",
        		 "BYTE pad3","CARD32 pad4","CARD32 pad5","CARD32 pad6",
        		 "CARD32 pad7","CARD32 pad8");
    @CreateNotify = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        	     "Window parent","Window window","INT16 x","INT16 y",
        	     "CARD16 width","CARD16 height","CARD16 borderWidth",
        	     "BOOL override","BYTE bpad","CARD32 pad1","CARD32 pad2");
    @DestroyNotify = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        	      "Window event","Window window","CARD32 pad1",
        	      "CARD32 pad2","CARD32 pad3","CARD32 pad4","CARD32 pad5");
    @UnmapNotify = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        	    "Window event","Window window","BOOL fromConfigure",
        	    "BYTE pad1","BYTE pad2","BYTE pad3","CARD32 pad4",
        	    "CARD32 pad5","CARD32 pad6","CARD32 pad7");
    @MapNotify = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        	  "Window event","Window window","BOOL override","BYTE pad1",
        	  "BYTE pad2","BYTE pad3","CARD32 pad4","CARD32 pad5",
        	  "CARD32 pad6","CARD32 pad7");
    @MapRequest = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        	   "Window parent","Window window","CARD32 pad1","CARD32 pad2",
        	   "CARD32 pad3","CARD32 pad4","CARD32 pad5");
    @ReparentNotify = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        	       "Window event","Window window","Window parent",
        	       "INT16 x","INT16 y","BOOL override","BYTE pad1",
        	       "BYTE pad2","BYTE pad3","CARD32 pad4","CARD32 pad5");
    @ConfigureNotify = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        		"Window event","Window window","Window aboveSibling",
        		"INT16 x","INT16 y","CARD16 width","CARD16 height",
        		"CARD16 borderWidth","BOOL override","BYTE bpad",
        		"CARD32 pad1");
    @ConfigureRequest = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        		 "Window parent","Window window","Window sibling",
        		 "INT16 x","INT16 y","CARD16 width","CARD16 height",
        		 "CARD16 borderWidth","CARD16 valueMask","CARD32 pad1");
    @GravityNotify = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        	      "Window event","Window window","INT16 x","INT16 y",
        	      "CARD32 pad1","CARD32 pad2","CARD32 pad3","CARD32 pad4");
    @ResizeRequest = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        	      "Window window","CARD16 width","CARD16 height",
        	      "CARD32 pad1","CARD32 pad2","CARD32 pad3","CARD32 pad4",
        	      "CARD32 pad5");
    @CirculateNotify = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        		"Window event","Window window","Window parent",
        		"BYTE place","BYTE pad1","BYTE pad2","BYTE pad3",
        		"CARD32 pad4","CARD32 pad5","CARD32 pad6");
    @CirculateRequest = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        		 "Window event","Window window","Window parent",
        		 "BYTE place","BYTE pad1","BYTE pad2","BYTE pad3",
        		 "CARD32 pad4","CARD32 pad5","CARD32 pad6");
    @PropertyNotify = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        	       "Window window","Atom atom","Time time","BYTE state",
        	       "BYTE pad1","CARD16 pad2","CARD32 pad3","CARD32 pad4",
        	       "CARD32 pad5");
    @SelectionClear = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        	       "Time time","Window window","Atom atom","CARD32 pad1",
        	       "CARD32 pad2","CARD32 pad3","CARD32 pad4");
    @SelectionRequest = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        		 "Time time","Window owner","Window requestor",
        		 "Atom selection","Atom target","Atom property",
        		 "CARD32 pad1");
    @SelectionNotify = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        		"Time time","Window requestor","Atom selection",
        		"Atom target","Atom property","CARD32 pad1",
        		"CARD32 pad2");
    @ColormapNotify = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        	       "Window window","Colormap colormap","BOOL new",
        	       "BYTE state","BYTE pad1","BYTE pad2","CARD32 pad3",
        	       "CARD32 pad4","CARD32 pad5","CARD32 pad6");
    @ClientMessage = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        	      "Window window","Atom type","INT32 longs0",
        	      "INT32 longs1","INT32 longs2","INT32 longs3",
        	      "INT32 longs4");
    @MappingNotify = ("BYTE type","BYTE detail","CARD16 sequenceNumber",
        	      "CARD8 request","KeyCode firstKeyCode","CARD8 count",
        	      "BYTE pad1","CARD32 pad2","CARD32 pad3","CARD32 pad4",
        	      "CARD32 pad5","CARD32 pad6","CARD32 pad7");
    @XEventCodes = ("(unused)","(unused)","KeyPress","KeyRelease","ButtonPress",
        	    "ButtonRelease","MotionNotify","EnterNotify","LeaveNotify",
        	    "FocusIn","FocusOut","KeymapNotify","Expose",
        	    "GraphicsExpose","NoExpose","VisibilityNotify",
        	    "CreateNotify","DestroyNotify","UnmapNotify","MapNotify",
        	    "MapRequest","ReparentNotify","ConfigureNotify",
        	    "ConfigureRequest","GravityNotify","ResizeRequest",
        	    "CirculateNotify","CirculateRequest","PropertyNotify",
        	    "SelectionClear","SelectionRequest","SelectionNotify",
        	    "ColormapNotify","ClientMessage","MappingNotify");
    local(@tmp);
    foreach (@XEventCodes)
    {   eval "next if (!defined(\@$item)); \@tmp = \@$_";
        ($Size{"$_"},$Template{"$_"}) = &DefToTemplate(@tmp);
        $Name{"$_"} = &DefToNames(@tmp);
    }
}

#    ARGS:             1 - simple data type
#    RETURN:           size in bytes of type, pack format character
#    VARIABLES SET:    <none>
sub SizeFromType
{   local($type) = @_;
    return($Size{$type},$Template{$type})
        if (defined($Size{$type}) && defined($Template{$type}));
    return((defined($Type{"$type"})) ? (&SizeFromType($Type{"$type"})) : -1);
}

#    ARGS:             1 - array definition of a structure
#    RETURN:           total byte size, the pack/unpack template of structure
#    VARIABLES SET:    <none>
sub DefToTemplate
{   local(@def) = @_;
    local($tmpl,$type,$name,$size,$nbytes,$char);
    $nbytes = 0;
    foreach (@def)
    {   ($type,$name) = split(' ',$_);
	($size,$char) = &SizeFromType($type);
	next if ($size == -1);
	$tmpl .= $char;
        $nbytes += $size;
    }
    return($nbytes,$tmpl);
}

#    ARGS:             1 - array definition of a structure
#    RETURN:           eval flavor of structure's name space
#    VARIABLES SET:    <none>
sub DefToNames
{   local(@def) = @_;
    local($names,$type,$name);
    foreach (@def)
    {   ($type,$name) = split(' ',$_);
        $names = "$names\$$name,";
    }
    chop($names);
    return("($names)");
}

#    ARGS:             <none>
#    RETURN:           <none>
#    VARIABLES SET:    $Size{'name'}
#                      $Template{'name'}
#                      $Name{'name'}
#                      $XTrapHeaderTypes[1..2]
sub XTrapInit
{   do XTypeSizeInit();
    do XSimpleTypeInit();
    do XProtoRequestInit();
    do XProtoEventInit();
    @XTrapHeader = ("CARD32 count","CARD32 timestamp","CARD32 type",
        	    "CARD32 win_x","CARD32 win_y","CARD32 client");
    ($Size{"XTrapHeader"},$Template{"XTrapHeader"}) =
        &DefToTemplate(@XTrapHeader);
    $Name{"XTrapHeader"} = &DefToNames(@XTrapHeader);
    $XTrapHeaderTypes[1] = "XEventCodes";
    $XTrapHeaderTypes[2] = "XRequestCodes";
}

#    ARGS:             1 - File handle from which to read
#    RETURN:           The type of the datum read, or 0 on error
#    VARIABLES SET:    $XTrapDatum{'header'}
#                      $XTrapDatum{'data'}
sub XTrapReadXDatum
{   local($fh) = @_;
    local($size) = $Size{'XTrapHeader'};
    return(0) if (read(<$fh>,$XTrapDatum{'header'},$size) != $size);
    eval "local$Name{'XTrapHeader'} =
	unpack($Template{'XTrapHeader'},$XTrapDatum{'header'})";
    $size = $count - $size;
    return(0) if (read(<$fh>,$XTrapDatum{'data'},$size) != $size);
    return($type);
}

# Initialize the world
do XTrapInit();

# While there is XTrap data to read, process it
while(!eof(<>))
{   do XTrapReadDatum(<>);
    eval "local$Name{'XTrapHeader'} =
	unpack($Template{'XTrapHeader'},$XTrapDatum{'header'})";
    printf("Read Datum, type = 0x%08X, nBytes = %d\n",$type,$count);
}

exit 0

- Marc
===========================================================================
Marc Evans - WB1GRH - [email-masked]  | Synergytics     (603)635-8876
      Unix and X Software Contractor        | 21 Hinds Ln, Pelham, NH 03076
===========================================================================