
    i                     z   d dl Z d dlZd dlZd dlmZ d dlmZmZmZmZm	Z	 ddl
mZ ddlmZ d Zerd Znd	 Z G d
 de      ZddddZddddZi Zi ZdD ]F  Z ee      j.                  Zej3                         ee<   	 eeee   <   ej3                         eee   <   H  G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z  G d de      Z! G d  d!e!      Z" G d" d#e"      Z# G d$ d%e"      Z$ G d& d'e!      Z% G d( d)e!      Z& G d* d+e      Z'e'Z( G d, d-e      Z) G d. d/e)      Z* G d0 d1e)      Z+ G d2 d3e)      Z, G d4 d5e)      Z- G d6 d7e)      Z. G d8 d9e)      Z/ G d: d;e/      Z0 G d< d=e0      Z1 G d> d?e0      Z2 G d@ dAe0      Z3 G dB dCe0      Z4 G dD dEe0      Z5 G dF dGe0      Z6 G dH dIe0      Z7 G dJ dKe0      Z8 G dL dMe)      Z9 G dN dOe)      Z: G dP dQe:      Z; G dR dSe)      Z< G dT dUe)      Z= G dV dWe)      Z> G dX dYe)      Z? G dZ d[e)      Z@ G d\ d]e@      ZA G d^ d_e)      ZB G d` dae)      ZC G db dceC      ZD G dd dee      ZE G df dge)      ZF G dh die)      ZG G dj dke)      ZH G dl dme      ZI eId      ZJ eId      ZK eId      ZL G dn doe      ZM eMdp      ZN eMdq      ZO G dr dse      ZP eP       ZQ G dt due      ZR G dv dwe)      ZS G dx dyeS      ZT G dz d{e      ZU G d| d}eU      ZV G d~ de      ZW G d deU      ZX G d deU      ZYd ZZy# e$ r Y w xY w)    N)array)PY3binary_typebyte2int
indexbytes	iterbytes   )X)lockc                 $    | j                  d      S )Nlatin1)decode)bss    Z/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/Xlib/protocol/rq.pydecode_stringr   $   s    99X    c                 "    | j                         S N)tobytesas    r   encode_arrayr   (   s    yy{r   c                 "    | j                         S r   )tostringr   s    r   r   r   +   s    zz|r   c                       e Zd Zy)BadDataErrorN__name__
__module____qualname__ r   r   r   r   /   s    r   r   bhl)   r	      BHLbhilc                   8    e Zd ZdZdZdZdZdZdZdZ	dZ
d Zd Zy)Fielda  Field objects represent the data fields of a Struct.

    Field objects must have the following attributes:

       name         -- the field name, or None
       structcode   -- the struct codes representing this field
       structvalues -- the number of values encodes by structcode

    Additionally, these attributes should either be None or real methods:

       check_value  -- check a value before it is converted to binary
       parse_value  -- parse a value after it has been converted from binary

    If one of these attributes are None, no check or additional
    parsings will be done one values when converting to or from binary
    form.  Otherwise, the methods should have the following behaviour:

       newval = check_value(val)
         Check that VAL is legal when converting to binary form.  The
         value can also be converted to another Python value.  In any
         case, return the possibly new value.  NEWVAL should be a
         single Python value if structvalues is 1, a tuple of
         structvalues elements otherwise.

       newval = parse_value(val, display)
         VAL is an unpacked Python value, which now can be further
         refined.  DISPLAY is the current Display object.  Return the
         new value.  VAL will be a single value if structvalues is 1,
         a tuple of structvalues elements otherwise.

    If `structcode' is None the Field must have the method
    f.parse_binary_value() instead.  See its documentation string for
    details.
    Nr   Fc                      y r   r!   selfs    r   __init__zField.__init__{   s    r   c                 6    t        dj                  |             )aB  value, remaindata = f.parse_binary_value(data, display, length, format)

        Decode a value for this field from the binary string DATA.
        If there are a LengthField and/or a FormatField connected to this
        field, their values will be LENGTH and FORMAT, respectively.  If
        there are no such fields the parameters will be None.

        DISPLAY is the display involved, which is really only used by
        the Resource fields.

        The decoded value is returned as VALUE, and the remaining part
        of DATA shold be returned as REMAINDATA.
        z9Neither structcode or parse_binary_value provided for {0})RuntimeErrorformatr/   datadisplaylengthr3   s        r   parse_binary_valuezField.parse_binary_value~   s      ##)6$<1 	1r   )r   r   r    __doc__namedefault
structcodestructvaluescheck_valueparse_valuekeyword_argsr0   r8   r!   r   r   r,   r,   M   s7    !D DGJLKKL1r   r,   c                       e Zd Zd Zy)Padc                 `    || _         d|z  | _        dj                  |      | _        d| _        y )N    z{0}xr   )sizevaluer3   r<   r=   )r/   rE   s     r   r0   zPad.__init__   s,    	T\
 ---r   Nr   r   r    r0   r!   r   r   rB   rB      s    r   rB   c                       e Zd Zd Zy)ConstantFieldc                     || _         y r   rF   r/   rF   s     r   r0   zConstantField.__init__   s	    
r   NrG   r!   r   r   rI   rI      s    r   rI   c                       e Zd ZdZdZy)Opcoder'   r%   Nr   r   r    r<   r=   r!   r   r   rN   rN          JLr   rN   c                       e Zd ZdZdZd Zy)	ReplyCoder'   r%   c                     d| _         y Nr%   rK   r.   s    r   r0   zReplyCode.__init__   s	    
r   N)r   r   r    r<   r=   r0   r!   r   r   rR   rR      s    JLr   rR   c                   "    e Zd ZdZdZdZdZd Zy)LengthFieldal  A LengthField stores the length of some other Field whose size
    may vary, e.g. List and String8.

    Its name should be the same as the name of the field whose size
    it stores.  The other_fields attribute can be used to specify the
    names of other fields whose sizes are stored by this field, so
    a single length field can set the length of multiple fields.

    The lf.get_binary_value() method of LengthFields is not used, instead
    a lf.get_binary_length() should be provided.

    Unless LengthField.get_binary_length() is overridden in child classes,
    there should also be a lf.calc_length().
    r)   r%   Nc                     |S )zjnewlen = lf.calc_length(length)

        Return a new length NEWLEN based on the provided LENGTH.
        r!   r/   r7   s     r   calc_lengthzLengthField.calc_length   s	     r   )r   r   r    r9   r<   r=   other_fieldsrY   r!   r   r   rV   rV      s     JLLr   rV   c                       e Zd Zy)TotalLengthFieldNr   r!   r   r   r\   r\      s    r   r\   c                       e Zd ZdZdZd Zy)RequestLengthr(   r%   c                     |dz  S )Nr&   r!   rX   s     r   rY   zRequestLength.calc_length   s    {r   Nr   r   r    r<   r=   rY   r!   r   r   r^   r^      s    JLr   r^   c                       e Zd ZdZdZd Zy)ReplyLengthr)   r%   c                     |dz
  dz  S )N    r&   r!   rX   s     r   rY   zReplyLength.calc_length   s    !!r   Nr`   r!   r   r   rb   rb      s    JL"r   rb   c                       e Zd Zd Zy)LengthOfc                     t        |t        t        f      r|d   | _        |dd  | _        n|| _        t
        |   | _        y )Nr   r%   )
isinstancelisttupler:   rZ   unsigned_codesr<   r/   r:   rE   s      r   r0   zLengthOf.__init__   s<    dT5M*QDI $QRDDI(.r   NrG   r!   r   r   rf   rf      s    /r   rf   c                   &    e Zd ZdZdZd Zd Zd Zy)	OddLengthr'   r%   c                     || _         y r   )r:   r/   r:   s     r   r0   zOddLength.__init__   s	    	r   c                     |dz  S )Nr	   r!   rX   s     r   rY   zOddLength.calc_length   s    zr   c                     |dk(  ryy)Nr   evenoddr!   )r/   rF   r6   s      r   r?   zOddLength.parse_value   s    A:r   N)r   r   r    r<   r=   r0   rY   r?   r!   r   r   rn   rn      s    JLr   rn   c                       e Zd ZdZdZd Zy)FormatFieldzA FormatField encodes the format of some other field, in a manner
    similar to LengthFields.

    The ff.get_binary_value() method is not used, replaced by
    ff.get_binary_format().
    r%   c                 .    || _         t        |   | _        y r   )r:   rk   r<   rl   s      r   r0   zFormatField.__init__   s    	(.r   N)r   r   r    r9   r=   r0   r!   r   r   rv   rv      s     L/r   rv   c                       e Zd ZddZy)
ValueFieldNc                      || _         || _        y r   )r:   r;   )r/   r:   r;   s      r   r0   zValueField.__init__  s    	r   r   rG   r!   r   r   ry   ry     s    r   ry   c                       e Zd ZdZdZy)Int8r"   r%   NrO   r!   r   r   r|   r|     rP   r   r|   c                       e Zd ZdZdZy)Int16r#   r%   NrO   r!   r   r   r~   r~     rP   r   r~   c                       e Zd ZdZdZy)Int32r$   r%   NrO   r!   r   r   r   r     rP   r   r   c                       e Zd ZdZdZy)Card8r'   r%   NrO   r!   r   r   r   r     rP   r   r   c                       e Zd ZdZdZy)Card16r(   r%   NrO   r!   r   r   r   r     rP   r   r   c                       e Zd ZdZdZy)Card32r)   r%   NrO   r!   r   r   r   r     rP   r   r   c                   (    e Zd ZdZdZddZd Zd Zy)Resource__resource__resourceNc                 @    t         j                  | ||       || _        y r   )r   r0   codes)r/   r:   r   r;   s       r   r0   zResource.__init__%  s    dG,
r   c                 h    t        || j                        r t        || j                               S |S r   )hasattrcast_functiongetattrrL   s     r   r>   zResource.check_value)  s/    5$,,-575$"4"4577Lr   c                 r    || j                   v r|S |j                  | j                        }|r	 |||      S |S r   )r   get_resource_class
class_namer/   rF   r6   cs       r   r?   zResource.parse_value/  s>     DJJL&&t7We$$Lr   )r!   N)r   r   r    r   r   r0   r>   r?   r!   r   r   r   r   !  s    "MJ
r   r   c                       e Zd ZdZdZy)Window
__window__windowNr   r   r    r   r   r!   r   r   r   r   ;       MJr   r   c                       e Zd ZdZdZy)Pixmap
__pixmap__pixmapNr   r!   r   r   r   r   ?  r   r   r   c                       e Zd ZdZdZy)Drawable__drawable__drawableNr   r!   r   r   r   r   C      "MJr   r   c                       e Zd ZdZdZy)Fontable__fontable__fontableNr   r!   r   r   r   r   G  r   r   r   c                       e Zd ZdZdZy)Font__font__fontNr   r!   r   r   r   r   K  s    MJr   r   c                       e Zd ZdZdZy)GC__gc__gcNr   r!   r   r   r   r   O  s    MJr   r   c                       e Zd ZdZdZy)Colormap__colormap__colormapNr   r!   r   r   r   r   S  r   r   r   c                       e Zd ZdZdZy)Cursor
__cursor__cursorNr   r!   r   r   r   r   W  r   r   r   c                       e Zd ZdZdZd Zy)Boolr%   r'   c                 
    |  S r   r!   rL   s     r   r>   zBool.check_value`  s    9}r   N)r   r   r    r=   r<   r>   r!   r   r   r   r   \  s    LJr   r   c                       e Zd ZdZddZd Zy)Setr%   Nc                 \    t         j                  | ||       t        |   | _        || _        y r   )ry   r0   rk   r<   values)r/   r:   rE   r   r;   s        r   r0   zSet.__init__f  s'    D$0(.r   c                 r    || j                   vr(t        d| j                  d|d| j                         |S )Nzfield z: argument z not in )r   
ValueErrorr:   r/   vals     r   r>   zSet.check_valuek  s6    dkk! $		3= > > 
r   r   )r   r   r    r=   r0   r>   r!   r   r   r   r   c  s    L
r   r   c                       e Zd Zd Zy)Gravityc                 ~   t         j                  | |dt        j                  t        j                  t        j
                  t        j                  t        j                  t        j                  t        j                  t        j                  t        j                  t        j                  t        j                  f       y rT   )r   r0   r
   ForgetGravityStaticGravityNorthWestGravityNorthGravityNorthEastGravityWestGravityCenterGravityEastGravitySouthWestGravitySouthGravitySouthEastGravityrp   s     r   r0   zGravity.__init__s  sh    T4Q__aoo$%$6$6$%$6$6$%OOQ]]$%$6$6$%$6$6%8 	9r   NrG   r!   r   r   r   r   r  s    9r   r   c                       e Zd ZdZd Zy)FixedBinaryr%   c                 \    t         j                  | |       dj                  |      | _        y )Nz{0}s)ry   r0   r3   r<   rl   s      r   r0   zFixedBinary.__init__  s"    D$' ---r   N)r   r   r    r=   r0   r!   r   r   r   r   |  s    L.r   r   c                   $    e Zd ZdZddZd Zd Zy)BinaryNc                 >    t         j                  | |       || _        y r   ry   r0   padr/   r:   r   s      r   r0   zBinary.__init__      D$'r   c                 f    |}t        |      }| j                  r|dd|dz  z
  dz  z  z   |d fS ||d fS NrD   r&   )lenr   r/   r   	val_bytesslens       r   
pack_valuezBinary.pack_value  sH    	9~88uTAX(:;;T4GGdD((r   c                 \    ||dfS | j                   r|d|dz  z
  dz  z   }n|}|d | ||d  fS Nr   r&   r   r/   r5   r6   r7   r3   r   s         r   r8   zBinary.parse_binary_value  sK    >988a&1*n12DDGV}d45k))r   r%   r   r   r    r<   r0   r   r8   r!   r   r   r   r     s    J)	*r   r   c                   $    e Zd ZdZddZd Zd Zy)String8Nc                 >    t         j                  | |       || _        y r   r   r   s      r   r0   zString8.__init__  r   r   c                     t        |t              r|}n|j                         }t        |      }| j                  r|dd|dz  z
  dz  z  z   |d fS ||d fS r   )rh   bytesencoder   r   r   s       r   r   zString8.pack_value  s]    c5!I

I9~88uTAX(:;;T4GGdD((r   c                     |t        |      dfS | j                  r|d|dz  z
  dz  z   }n|}t        |d |       }|||d  fS r   )r   r   )r/   r5   r6   r7   r3   r   data_strs          r   r8   zString8.parse_binary_value  s[    > &++88a&1*n12DD gv/de$$r   r   r   r!   r   r   r   r     s    J
)%r   r   c                   $    e Zd ZdZddZd Zd Zy)String16Nc                 >    t         j                  | |       || _        y r   r   r   s      r   r0   zString16.__init__  r   r   c                     t        |t              rt        t        |            }t	        |      }| j
                  r	d|dz  z  }nd}t        j                  dd|z  z   g| |z   |dfS )z'Convert 8-byte string into 16-byte lists     r	   r   >r(   N)rh   r   ri   r   r   r   structpack)r/   r   r   r   s       r   r   zString16.pack_value  sf    c5!y~&C3x88TAX&CC{{3t+2c2S8$DDr   c                     |dk(  rt        |      dz  dz
  }n|dk(  rt        |      dz  }| j                  r	||dz  z   }n|}t        j                  dd|z  z   |d |dz         ||dz  d  fS )Nrt   r	   r%   rs   r   r(   )r   r   r   unpackr   s         r   r8   zString16.parse_binary_value  s    U?Y!^a'FvY!^F88VaZ(DD}}S3</kvz1BCT$QR()_TTr   r   r   r!   r   r   r   r     s    JEUr   r   c                   (    e Zd ZdZdZddZd Zd Zy)ListzThe List, FixedList and Object fields store compound data objects.
    The type of data objects must be provided as an object with the
    following attributes and methods:

    ...

    Nc                 L    t         j                  | |       || _        || _        y r   )ry   r0   typer   )r/   r:   r   r   s       r   r0   zList.__init__  s     D$'	r   c                    |<g }| j                   j                  8|r:| j                   j                  ||      \  }}|j                  |       |r3nd| j                   j                  z   }t	        j
                  |      }d}	|	|z   t        |      k  rt	        j                  |||	|	|z          }
| j                   j                  dk(  r|
d   }
| j                   j                  |j                  |
       n+|j                  | j                   j                  |
|             |	|z   }	|	|z   t        |      k  r||	d  }nd gt        |      z  }| j                   j                  4t        d|      D ]$  }| j                   j                  ||      \  ||<   }& nd| j                   j                  z   }t	        j
                  |      }d}	t        d|      D ]|  }t	        j                  |||	|	|z          }
| j                   j                  dk(  r|
d   }
| j                   j                  |
||<   n| j                   j                  |
|      ||<   |	|z   }	~ ||	d  }| j                  r|t        |      dz  d  }||fS )N=r   r%   r&   )r   r<   parse_binaryappendr   calcsizer   r   r=   r?   intranger   )r/   r5   r6   r7   r3   retr   scoder   posvis               r   r8   zList.parse_binary_value  sS   >Cyy##+ $		 6 6tW EICJJsO  dii222u-DjCI-eT#sTz-BCAyy--2aDyy,,4

1

499#8#8G#DE*C DjCI- CDz &3v;&Cyy##+q&) IA#'99#9#9$#HLCFDI dii222u-q&) %AeT#sTz-BCAyy--2aDyy,,4!"A!%!6!6q'!BA*C% CDz88D	A'DDyr   c                 R   | j                   j                  rt        | j                   j                        dk(  rq| j                   j                  (|D cg c]  }| j                   j                  |       }}t	        t
        | j                   j                     |      }t        |      }nDg }|D ],  }|j                  | j                   j                  |             . dj                  |      }| j                  rt        |      }|dd|dz  z
  dz  z  z   }|t        |      d fS c c}w )Nr%   r   rD   r&   )r   r<   r   r>   r   struct_to_array_codesr   r  r   joinr   )r/   r   r  r   r5   dlens         r   r   zList.pack_value-  s    99C		(<(<$=$Byy$$09<=Atyy,,Q/==+DII,@,@A3GA?DD 5DII00345 88D>D88t9D%AqLA#566DSXt## >s   "D$r   )r   r   r    r9   r<   r0   r8   r   r!   r   r   r   r     s     J
6p$r   r   c                        e Zd ZddZd Zd Zy)	FixedListc                 B    t         j                  | |||       || _        y r   )r   r0   rE   )r/   r:   rE   r   r   s        r   r0   zFixedList.__init__C  s    dD$,	r   c                 H    t         j                  | ||| j                  |      S r   )r   r8   rE   r4   s        r   r8   zFixedList.parse_binary_valueG  s    &&tT7DIIvNNr   c                     t        |      | j                  k7  rt        d| j                  z        t        j                  | |      S )Nz length mismatch for FixedList %s)r   rE   r   r:   r   r   r   s     r   r   zFixedList.pack_valueJ  s8    s8tyy ADIIMNNtS))r   Nr   r   r   r    r0   r8   r   r!   r   r   r  r  B  s    O*r   r  c                   ,    e Zd ZddZd Zd Zd Zd Zy)ObjectNc                     t         j                  | ||       || _        | j                  j                  | _        | j                  j                  | _        y r   )ry   r0   r   r<   r=   )r/   r:   r   r;   s       r   r0   zObject.__init__Q  s>    D$0	)).. II22r   c                 :    | j                   j                  ||      S r   )r   r   r4   s        r   r8   zObject.parse_binary_valueW  s    yy%%dG44r   c                 :    | j                   j                  ||      S r   )r   r?   )r/   r   r6   s      r   r?   zObject.parse_valueZ  s    yy$$S'22r   c                 8    | j                   j                  |      S r   )r   r   r   s     r   r   zObject.pack_value]  s    yy##C((r   c                    t        |t              rg }d}| j                  j                  D ]l  }|j                  s|j
                  ||   }n|j                  ||         }|j                  dk(  r|j                  |       n|j                  |       |dz   }n |S t        |t              r|}n+t        |t              r|j                  }nt        d|z        g }| j                  j                  D ]{  }|j                  s|j
                  ||j                     }n|j                  ||j                           }|j                  dk(  r|j                  |       k|j                  |       } |S )Nr   r%   z9Object value must be tuple, dictionary or DictWrapper: %s)rh   rj   r   fieldsr:   r>   r=   r  extenddictDictWrapper_data	TypeError)r/   r   valsr	  fr  r5   s          r   r>   zObject.check_value`  s<   c5!DAYY%% 
66}},FMM#a&1~~*AAAA
 Kc4 D[)99DWZ]]^^!! 		#Avv==(QVVAd166l3A>>Q&KKNKKN		# r   r   )r   r   r    r0   r8   r?   r   r>   r!   r   r   r  r  P  s    353)$r   r  c                       e Zd ZdZd Zd Zy)PropertyDataNc                 T   |t        |      |dz  z  }nt        |      }|dk(  rd }||fS |dk(  rd|d | f}||d|dz  z
  dz  z   d  }||fS |dk(  r-dt        t        d   |d d|z         f}|d||dz  z   z  d  }||fS |dk(  r#dt        t        d   |d d|z         f}|d|z  d  }|fS )N   r   r&      r	   rd   )r   r  r   array_unsigned_codes)r/   r5   r6   r7   r3   r  s         r   r8   zPropertyData.parse_binary_value  s   >Y6Q;/F[FQ;C Dy q[d7Fm$C1vz>Q"6789D Dy r\u1!4d;AJ6GHICVfqj0123D Dy	 r\u1!4d;AJ6GHICF
$DDyr   c                    |\  }}|dvrt        dj                  |            t        |t              r+|dz  }t	        |      }||z  r|||z  z
  }|d | }n|}||z  }nIt        |t
              rt        |      }|dz  }t        t        |   |      }t        |      }t	        |      }t	        |      }	|dd|	dz  z
  dz  z  z   }|||fS )N)r&  r'  rd   z Invalid property data format {0}r&  rD   r&   )
r   r3   rh   r   r   rj   ri   r   r(  r   )
r/   rF   fmtr   rE   vlenr5   r  r   dls
             r   r   zPropertyData.pack_value  s    Sk!AHHMNNc;'!8Ds8Dd{dTk)5Dz4<D #u%3i!8D*40#6A?Ds8DYeBF
a/00T3r   r   r   r    r<   r8   r   r!   r   r   r$  r$    s    J.r   r$  c                       e Zd Zd Zd Zd Zy)FixedPropertyDatac                 >    t         j                  | |       || _        y r   )r$  r0   rE   rl   s      r   r0   zFixedPropertyData.__init__  s    dD)	r   c                 T    t         j                  | ||| j                  |dz  z  |      S )Nr&  )r$  r8   rE   r4   s        r   r8   z$FixedPropertyData.parse_binary_value  s/    ..tT7/3yyVq[/I6S 	Sr   c                     t         j                  | |      \  }}}t        |      | j                  k7  rt	        d|      |||fS )Nz)Wrong data length for FixedPropertyData: )r$  r   r   rE   r   )r/   rF   r5   r  r*  s        r   r   zFixedPropertyData.pack_value  sM    &11$>dCt9		!"' + , , T3r   Nr  r!   r   r   r/  r/    s    Sr   r/  c                   *    e Zd ZdZdZdZd Zd Zd Zy)	ValueListNTusekeywordsc                 0   || _         dj                  t        |   |      j                         | _        t        j                  | j                        | _        g | _        d}|D ]1  }|j                   s| j                  j                  ||f       |dz  }3 y )Nz={0}{1}xr%   )
r:   r3   rk   r   maskcoder   r  maskcodelenr  r  )r/   r:   maskr   r  flagr"  s          r   r0   zValueList.__init__  s    	")).*>DKKM!??4==9 	!Avv""At9-qy	!r   c                    d}d}|| j                   k(  r|}| j                  D ]  \  }}|j                  |v s||z  }||j                     }|j                  |j                  |      }t	        j
                  d|j                  z   |      }||z   ddt        |      z
  z  z   } t	        j
                  | j                  |      |z   d d fS )Nr   r   r   rD   r&   )	r;   r  r:   r>   r   r   r<   r   r7  )	r/   argkeysr9  r5   fieldr:  r   ds	            r   r   zValueList.pack_value  s    $,,C;; 		7KE4zzS d{%**o$$0++C0CKKe&6&6 6<ax%1s1v:"66		7 {{4==$/$6dBBr   c           
      J   i }t        t        j                  | j                  |d | j                         d         }|| j                  d  }| j
                  D ]  \  }}||z  s|j                  ryt        j                  d|j                  z   |d t        j                  d|j                  z                }	|j                  dk(  r|	d   }	|j                  -|j                  |	|      }	n|j                  |d d |d d       \  }	}
|	||j                  <   |dd  } t        |      |fS )Nr   r   r%   r&   )r  r   r   r7  r8  r  r<   r  r=   r?   r8   r:   r  )r/   r5   r6   r7   r3   rr9  r>  r:  r!  r?  s              r   r8   zValueList.parse_binary_value  s)   6==5Fd6F6F0GHKLD$$%&;; 	 KE4d{##!==u/?/?)?)-.VvsUEUEU?U/V)WYD))Q.#Aw((4$00w? $66tBQx$PTUGD! $%**ABx	 " 1~t##r   )	r   r   r    r<   r@   r;   r0   r   r8   r!   r   r   r4  r4    s!    JLG
!C($r   r4  c                       e Zd ZdZd Zd Zy)KeyboardMappingNc                     |t        |      }nd|z  |z  }t        t        d   t        |d |             }g }t	        dt        |      |      D ]  }|j                  ||||z            |||d  fS )Nr&   r   )r   r   r(  r   r  r  )	r/   r5   r6   r7   r3   r  r   r  r	  s	            r   r8   z"KeyboardMapping.parse_binary_value  s    >t9Dv:&D&q)5et+=>q#a&&) 	*AJJqQZ()	* DKr   c                 @   d}|D ]  }t        |t        |            } t        t        d         }|D ]S  }|D ]  }|j	                  |        t        t        |      |      D ]!  }|j	                  t        j                         # U t        |      t        |      |fS )Nr   r&   )	maxr   r   r(  r  r  r
   NoSymbolr   r/   rF   keycodesr  r   kr	  s          r   r   zKeyboardMapping.pack_value$  s     	-A8SV,H	- &q)* 	%A 3q68, %$%	% AE
H44r   r-  r!   r   r   rC  rC    s    J 5r   rC  c                       e Zd ZdZd Zd Zy)ModifierMappingNc                     t        t        d   |d d|z         }g }t        dd      D ]  }|j                  |||z  |dz   |z          ! ||d|z  d  fS )Nr%   r&  r   )r   r(  r  r  )r/   r5   r6   r7   r3   r   r  r	  s           r   r8   z"ModifierMapping.parse_binary_value7  sq    &q)4V+<=q! 	9AJJqVq1u&678	9 DV%%%r   c                 V   t        |      dk7  rt        d      d}|D ]  }t        |t        |            } t        t        d         }|D ]E  }|D ]  }|j                  |        t        t        |      |      D ]  }|j                  d        G t        |      t        |      |fS )Nr&  z/ModifierMapping list should have eight elementsr   r%   )r   r   rF  r   r(  r  r  r   rH  s          r   r   zModifierMapping.pack_value@  s    u:?PQQ 	-A8SV,H	- &q)* 	A 3q68, 	 AE
H44r   r-  r!   r   r   rL  rL  4  s    J&5r   rL  c                       e Zd ZdZd Zd Zy)
EventFieldNc                 t    t        |t              st        |d| j                        |j                  d d fS )Nz is not an Event for field )rh   Eventr   r:   _binaryrL   s     r   r   zEventField.pack_valueU  s1    %'E499UVV}}dD((r   c                     ddl m} |j                  j                  t	        |      dz  |j
                        }t        |      t        k(  r|t        |d         } |||d d       |dd  fS )Nr%   )event   rd   )r6   
binarydata)	 rU  event_classesgetr   AnyEventr   r  r   )r/   r5   r6   r7   r3   rU  estructs          r   r8   zEventField.parse_binary_value[  se    ''++HTNT,A5>>R=D jq12GtCRyA49LLr   )r   r   r    r<   r   r8   r!   r   r   rP  rP  R  s    J)Mr   rP  c                       e Zd Zd Zy)	ScalarObjc                 <    || _         d| _        d | _        d | _        y rT   )r<   r=   r?   r>   )r/   codes     r   r0   zScalarObj.__init__l  s!    r   NrG   r!   r   r   r^  r^  k  s     r   r^  c                        e Zd ZdZdZd Zd Zy)ResourceObjr)   r%   c                      || _         d | _        y r   )r   r>   )r/   r   s     r   r0   zResourceObj.__init__z  s    $r   c                 R    |j                  | j                        }|r	 |||      S |S r   )r   r   r   s       r   r?   zResourceObj.parse_value~  s-     &&t7We$$Lr   N)r   r   r    r<   r=   r0   r?   r!   r   r   rb  rb  v  s    JL r   rb  r   r   c                       e Zd ZdZd Zd Zy)StrClassNc                 L    t        t        |            |z   j                         S r   )chrr   r   r   s     r   r   zStrClass.pack_value  s    CH#++--r   c                 D    t        |      dz   }t        |d|       ||d  fS rT   )r   r   )r/   r5   r6   r   s       r   r   zStrClass.parse_binary  s,    ~!T!D\*DK77r   )r   r   r    r<   r   r   r!   r   r   rf  rf    s    J.8r   rf  c                   2    e Zd ZdZd Zd Zd ZddZddZy)	StructaQ  Struct objects represents a binary data structure.  It can
    contain both fields with static and dynamic sizes.  However, all
    static fields must appear before all dynamic fields.

    Fields are represented by various subclasses of the abstract base
    class Field.  The fields of a structure are given as arguments
    when instantiating a Struct object.

    Struct objects have two public methods:

      to_binary()    -- build a binary representation of the structure
                        with the values given as arguments
      parse_binary() -- convert a binary (string) representation into
                        a Python dictionary or object.

    These functions will be generated dynamically for each Struct
    object to make conversion as fast as possible.  They are
    generated the first time the methods are called.
    c                 |   || _         d| _        d| _        g | _        d | _        g | _        | j                   D ]  }|j                  v| j
                  rJ | j                  |j                  z   | _        |j                  dkD  sK| j                  j                  |       | j                  |j                  z   | _        | j
                  j                  |        t        j                  | j                        | _        | j
                  rd | _        d| _        y | j                  dd  | _        | j                  | _        y )Nr   r   r%   )r  static_codesstatic_valuesstatic_fieldsstatic_size
var_fieldsr<   r=   r  r   r  )r/   r  r"  s      r   r0   zStruct.__init__  s      	*A ||'??**$($5$5$D! >>A%&&--a0)-););ann)LD&
 &&q)!	*$ "??4+<+<=??"DO !D"//3DO $ 2 2Dr   c                    | j                   D cg c]*  }t        |t              r|j                  r|j                  , }}t	        t        ||            }t        |      j                  |      rCdj                  t        |      j                  |            }t        dj                  |            |j                  |       | j                   D ]g  }|j                  s|j                  |vs|j                  $t        dj                  |j                              |j                  ||j                  <   i | j                  }i }i }	i }
| j                  D ]  }|j                  r$|j!                  ||j                     |      \  }}}n"|j!                  ||j                           \  }}}|||j                  <   ||	|j                  <   ||
|j                  <   |t#        |      z  } g }| j$                  D ]  }t        |t&              r_t        |t(              r!|j+                  |j-                  |             E|j+                  |j-                  |	|j                                  st        |t.              r|j+                  |
|j                            t        |t0              r|j+                  |j2                         |j4                  dk(  r[|j6                  /|j+                  |j7                  ||j                                  |j+                  ||j                            8|j6                  /|j9                  |j7                  ||j                                  s|j9                  ||j                             t;        j<                  | j>                  g| }| j                  D cg c]  }||j                      }}|dj                  |      z   S c c}w c c}w )ag  data = s.to_binary(...)

        Convert Python values into the binary representation.  The
        arguments will be all value fields with names, in the order
        given when the Struct object was instantiated.  With one
        exception: fields with default arguments will be last.

        Returns the binary representation as the string DATA.
        , z:{0} arguments were passed both positionally and by keywordzMissing required argument {0}r%   r   ) r  rh   ry   r:   r  zipsetintersectionr  r   r3   updater;   rp  rq  r@   r   r   ro  rV   r\   r  rY   rv   rI   rF   r=   r>   r  r   r   rm  )r/   varargsr=  r"  names
field_argsdupestotal_lengthvar_valslengthsformatsr  r$   fm
pack_itemsstatic_part	var_partss                    r   	to_binaryzStruct.to_binary  s=    "& :Aq*-!&&  : :#eW-.
z?''-IIc*o::4@AEX__`efgg$ 	/Avv166399$#$C$J$J166$RSS%&YY
166"		/ '' 		#A~~<<
166(:DA1b<<
166(:;1b HQVVGAFFO GAFFOCF"L		# 
## #	>A![) a!12%%ammL&AB%%ammGAFFO&DE A{+!!'!&&/2 A}-!!!''* >>Q&}}0"))!--
1668J*KL #))*QVV*<= }}0"))!--
1668J*KL"))*QVV*<=G#	>J kk$"3"3AjA/3?!Xaff%?	?SXXi000[:X @s   /O%9O*c                     t        |      t        u r | j                  | S t        |t              r | j                  di |S t        |t
              r | j                  di |j                  S t        d|z        )z This function allows Struct objects to be used in List and
        Object fields.  Each item represents the arguments to pass to
        to_binary, either a tuple, a dictionary or a DictWrapper.

        z%s is not a tuple or a listr!   )r   rj   r  rh   r  r  r  r   rL   s     r   r   zStruct.pack_value1  sr     ;%!4>>5))t$!4>>*E**{+!4>>0EKK00<FGGr   c                 r   i }d}| j                   D ]  }|j                  snxt        |t              rngt        |t              rnV|j
                  dk(  r||   }n||||j
                  z    }|j                  |j                  |||      }|||j                  <   ||j
                  z   } |st        |      S |S )zThis function is used by List and Object fields to convert
        Struct objects with no var_fields into Python values.

        r   r%   rawdict)ro  r:   rh   rV   rv   r=   r?   r  )r/   r   r6   r  r  vnor"  	field_vals           r   r?   zStruct.parse_valueC  s     ## 	'A 66A{+A{+ >>Q& #CI #CANN(: ;I==, !i' RI'AFF&C;	'> s##
r   c           	         i }t        j                  | j                  |d| j                         }i }i }d}| j                  D ]
  }	|	j
                  snt        |	t              rd|	j
                  g}
|	j                  r|
j                  |	j                         ||   }|	j                  |	j                  ||      }|
D ]  }|||<   	 nwt        |	t              r||   ||	j
                  <   nT|	j                  dk(  r||   }n||||	j                  z    }|	j                  |	j                  ||      }|||	j
                  <   ||	j                  z   } || j                  d }| j                  D ]X  }	|	j                  |||j                  |	j
                        |j                  |	j
                              \  ||	j
                  <   }Z |st!        |      }||fS )a  values, remdata = s.parse_binary(data, display, rawdict = False)

        Convert a binary representation of the structure into Python values.

        DATA is a string or a buffer containing the binary data.
        DISPLAY should be a Xlib.protocol.display.Display object if
        there are any Resource fields or Lists with ResourceObjs.

        The Python values are returned as VALUES.  If RAWDICT is true,
        a Python dictionary is returned, where the keys are field
        names and the values are the corresponding Python value.  If
        RAWDICT is false, a DictWrapper will be returned where all
        fields are available as attributes.

        REMDATA are the remaining binary data, unused by the Struct object.

        Nr   r%   )r   r   rm  rp  ro  r:   rh   rV   rZ   r  r?   rv   r=   rq  r8   rZ  r  )r/   r5   r6   r  r  r   r~  r  r  r"  f_namesr  f_names                r   r   zStruct.parse_binaryn  s   & mmD--t4ET5E5E/FG## #	'A
 66
 A{+66(>>NN1>>2H	==, !i AI% 0F&/GFO0 A{+"%c( >>Q& #CI #CANN(: ;I==, !i AI'AFF&CG#	'J D$$%&
  	6A ! 4 4T75<[[5H5<[[5H!6CK	6 c"CDyr   NF)	r   r   r    r9   r0   r  r   r?   r   r!   r   r   rk  rk    s&    ("3RX1vH$)VKr   rk  c                   V    e Zd Z e edd       ed       edd            Zd Zd Z	y)	TextElements8stringr%   deltar   r   c                    d}i }|D ]  }t        |      t        t        fv rd|f}t        |t        t
        t        f      r_t        |t              r|\  }}n
|d   }|d   }|s|s^||d<   |d d |d<   | | j                  j                  di |z   }d}|dd  }|r6|r9t        |t              r|j                         }|t        j                  dd|      z   } t        |      }|d	d
|d
z  z
  d
z  z  z   d d fS )Nr   r   r  r     r!   z>BL   rD   r&   )r   strr   rh   rj   r  r  string_textitemr  r   r   r   r   r   )r/   rF   r5   argsr  r  m_strr  s           r   r   zTextElements8.pack_value  s"    	9AAw3,&F
 !eT;78a'#$LE5gJEhKEu$)DM%*4C[DN"@$"6"6"@"@""M"MMDE!#$KE u a*(Afkk%a88?	9D 4yeD1H122D$>>r   c           
      f   g }	 t        |      dk  r	 |dfS t        |      dk(  r:|j                  t        j                  dt        |dd             d          |dd  }nSt        |      dk(  rt        |d      dk(  r|dd  }n0| j                  j                  ||      \  }}|j                  |       )Nr%   r	   r  z>L   r   rX  )	r   r   r  r   r   r   r   r  r   )r/   r5   r6   r7   r3   r   r  s          r   r8   z TextElements8.parse_binary_value  s    4y1}  rz ~$fmmD%Qq	2BCAFGABx $1$D!)<)AABx ..;;D'J4a ! r   N)
r   r   r    rk  rf   r|   r   r  r   r8   r!   r   r   r  r    s1    hx3"7m%ha8;O'?Rr   r  c                   J    e Zd Z e edd       ed       edd            Zy)TextElements16r  r%   r  r   r   N)r   r   r    rk  rf   r|   r   r  r!   r   r   r  r    s&    hx3"7m&xq9<Or   r  c                       e Zd Zd Zy)GetAttrDatac                     	 | j                   r| j                   |   S t        |      # t        $ r t        |      w xY wr   )r  AttributeErrorKeyError)r/   attrs     r   __getattr__zGetAttrData.__getattr__
  sB    	'zzzz$''$T** 	' &&	's   ( ( =N)r   r   r    r  r!   r   r   r  r  	  s    'r   r  c                   N    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zy)r  c                 "    || j                   d<   y )Nr  )__dict__)r/   r  s     r   r0   zDictWrapper.__init__  s    !%gr   c                      | j                   |   S r   r  r/   keys     r   __getitem__zDictWrapper.__getitem__  s    zz#r   c                 "    || j                   |<   y r   r  r/   r  rF   s      r   __setitem__zDictWrapper.__setitem__      

3r   c                     | j                   |= y r   r  r  s     r   __delitem__zDictWrapper.__delitem__      JJsOr   c                 "    || j                   |<   y r   r  r  s      r   __setattr__zDictWrapper.__setattr__   r  r   c                     | j                   |= y r   r  r  s     r   __delattr__zDictWrapper.__delattr__#  r  r   c                 ,    t        | j                        S r   )r  r  r.   s    r   __str__zDictWrapper.__str__&  s    4::r   c                 `    | j                   j                  dt        | j                        dS )N())	__class__r   reprr  r.   s    r   __repr__zDictWrapper.__repr__)  s     >>22D4DEEr   c                 r    t        |t              r| j                  |j                  k  S | j                  |k  S r   rh   r  r  r/   others     r   __lt__zDictWrapper.__lt__,  .    e[)::++::%%r   c                 r    t        |t              r| j                  |j                  kD  S | j                  |kD  S r   r  r  s     r   __gt__zDictWrapper.__gt__2  r  r   c                 r    t        |t              r| j                  |j                  k(  S | j                  |k(  S r   r  r  s     r   __eq__zDictWrapper.__eq__8  s.    e[)::,,::&&r   N)r   r   r    r0   r  r  r  r  r  r  r  r  r  r  r!   r   r   r  r    s:    &  F&&'r   r  c                       e Zd ZddZd Zy)RequestNc                     || _          | j                  j                  |i || _        d | _        |j                  | |d u       y r   )_errorhandler_requestr  rS  _serialsend_request)r/   r6   onerrorr  r=  s        r   r0   zRequest.__init__@  sC    $.t}}..==T7$#67r   c                 J    | j                   t        | j                   ||       S y)Nr   )r  call_error_handlerr/   errors     r   
_set_errorzRequest._set_errorF  s&    )%d&8&8%FFr   r   )r   r   r    r0   r  r!   r   r   r  r  ?  s    8r   r  c                   ,    e Zd ZddZd Zd Zd Zd Zy)ReplyRequestc                    || _          | j                  j                  |i || _        d | _        d | _        d | _        t        j                         | _	        | j                   j                  | d       |s| j                          y y )NT)_displayr  r  rS  r  r  _errorr   allocate_lock_response_lockr  reply)r/   r6   deferr  r=  s        r   r0   zReplyRequest.__init__M  sq    .t}}..==
"002""4.JJL r   c                    | j                   j                          | j                  | j                  | j                  j
                  j                          | j                   j                          | j                  j                  | j                         | j                   j                          | j                  | j                  | j                   j                          d | _        | j                  r| j                  y )N)request)	r  acquirer  r  r  send_recv_lockreleasesend_and_recvr  r.   s    r   r  zReplyRequest.replyZ  s    
 	##%jj T[[%8MM((002'')MM''$,,'?'') jj T[[%8 	##% ;;++ r   c                     | j                   j                          | j                  j                  || j                  d      \  | _        }| j                   j                          y )NTr  )r  r  _replyr   r  r  r  )r/   r5   r?  s      r   _parse_responsezReplyRequest._parse_responsen  sL    ##%00t}}PT0U
A##%r   c                 z    | j                   j                          || _        | j                   j                          yrT   )r  r  r  r  r  s     r   r  zReplyRequest._set_errors  s0    ##%##%r   c           	          d| j                   j                  d| j                  d| j                  d| j                  d	S )N<z
 serial = z	, data = z
, error = r   )r  r   r  r  r  r.   s    r   r  zReplyRequest.__repr__y  s0    <@NN<S<SUYUaUacgcmcmosozoz{{r   Nr  )r   r   r    r0   r  r  r  r  r!   r   r   r  r  L  s    (&
|r   r  c                   ,    e Zd ZddZd Zd Zd Zd Zy)rR  Nc                 ~   |rn|| _         | j                  j                  ||d      \  | _        }| j                  d   dz    | j                  d<   | j                  d   dz  | j                  d<   y | j                  r| j                  |d<   d|d<    | j                  j
                  d	i || _         d|d<   || _        y )
NTr  r      
send_eventrV  r   sequence_numberr!   )rS  _fieldsr   r  _coder  )r/   rW  r6   r=  r5   s        r   r0   zEvent.__init__~  s    %DL#||88WCG  9  IDJ 04zz&/AD/H+H'HDJJ|$!%F!3d!:DJJvzz#zzV&'D"#14<<119D9DL!"DDJr   c                     g }| j                   j                         D ]C  \  }}|dk(  r|dk(  r| j                   d   r|dz  }|j                  |dt        |             E dj	                  |      }| j
                  j                  d|dS )Nr  r   r  z = rs  r  r  )r  itemsr  r  r  r  r   )r/   kwlistkwr   kwss        r   r  zEvent.__repr__  s    zz'') 	7GB\!V|

< 8DjMMr4956	7 ii>>22C88r   c                 r    t        |t              r| j                  |j                  k  S | j                  |k  S r   rh   rR  r  r  s     r   r  zEvent.__lt__  .    eU#::++::%%r   c                 r    t        |t              r| j                  |j                  kD  S | j                  |kD  S r   r  r  s     r   r  zEvent.__gt__  r  r   c                 r    t        |t              r| j                  |j                  k(  S | j                  |k(  S r   r  r  s     r   r  zEvent.__eq__  s.    eU#::,,::&&r   )NN)r   r   r    r0   r  r  r  r  r!   r   r   rR  rR  }  s    (
9&&'r   rR  c                     	  | ||      S #  t         j                  j                  d       t        j                          Y yxY w)Nz#Exception raised by error handler.
r   )sysstderrwrite	traceback	print_exc)handlerr  r  s      r   r  r    s=    ug&&

?@s	    5A)[r  r  r   r   sixr   r   r   r   r   rX  r
   supportr   r   r   	Exceptionr   signed_codesrk   r(  r  r   itemsizerE   upperr  objectr,   rB   rI   rN   rR   rV   r\   r^   rb   rf   rn   rv   Formatry   r|   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r$  r/  r4  rC  rL  rP  r^  Card8Obj	Card16Obj	Card32Objrb  	WindowObjColormapObjrf  Strrk  r  r  r  r  r  r  rR  r  r!   r   r   <module>r     s  .     B A    $9 # CC)cc+   	 A8D!"45l40167gginT23@1F @1F% E 
]  % 8	{ 	$ "" "/{ / "/% / 
 : J J J Z Z 
v 4X X x x 8  x X 
: * 9c 9.* .*Z *8%j %B Uz  UHZ$: Z$z* *4Z 4n7: 7t &<$ <$~5j 5B5j 5<M M2    cNcN	cN	& " !	*%8v 8 jbV bJ	BJ BL<] <'& ')'+ )'Xf .|; .|b1'K 1'hU+  s   ?L11L:9L: