
    ^jD                       d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	  e	d          Z
ej        dk    r ed           G d	 d
ej                  Z ej        e          ZeZ ej        e          ZdZdZdZ G d d          Z G d de          Z G d de          Z ej        ej                  Z ee          Z eej                  Z G d de          Z G d de          Z G d dej        ed          Z  G d dee          Z! G d de           Z" G d  d!e!          Z#d" Z$d# Z%d$ Z& G d% d&          Z'eeffd0d/Z(dS )1a  Minimal Windows COM interface.

Allows pyglet to use COM interfaces on Windows without comtypes.  Unlike
comtypes, this module does not provide property interfaces, read typelibs,
nice-ify return values.  We don't need anything that sophisticated to work with COM's.

Interfaces should derive from pIUnknown if their implementation is returned by the COM.
The Python COM interfaces are actually pointers to the implementation (take note
when translating methods that take an interface as argument).
(example: A Double Pointer is simply POINTER(MyInterface) as pInterface is already a POINTER.)

Interfaces can define methods::

    class IDirectSound8(com.pIUnknown):
        _methods_ = [
            ('CreateSoundBuffer', com.STDMETHOD()),
            ('GetCaps', com.STDMETHOD(LPDSCAPS)),
            ...
        ]

Only use METHOD, STDMETHOD or VOIDMETHOD for the method types (not ordinary ctypes
function types).  The 'this' pointer is bound automatically... e.g., call::

    device = IDirectSound8()
    DirectSoundCreate8(None, ctypes.byref(device), None)

    caps = DSCAPS()
    device.GetCaps(caps)

Because STDMETHODs use HRESULT as the return type, there is no need to check
the return value.

Don't forget to manually manage memory... call Release() when you're done with
an interface.
    )annotationsN)Sequence)debug_print	debug_comwin32z8pyglet.libs.win32.com requires a Windows build of Pythonc                      e Zd Zdej        fdej        fdej        fdej        dz  fgZddZd dZ	d!dZ
d"dZed#d            ZdS )$GUIDData1Data2Data3Data4   lintw1w2b1b2b3b4b5b6b7b8returnNonec                X    || _         || _        || _        ||||||	|
|f| j        d d <   d S N)r
   r   r   r   )selfr   r   r   r   r   r   r   r   r   r   r   s               P/home/agentuser/manim-venv/lib/python3.11/site-packages/pyglet/libs/win32/com.py__init__zGUID.__init__;   s;    


RRRR8
111    strc                    | j         \  }}}}}}}}d| j        dd| j        dd| j        dd|dd|dd|dd|dd|dd|dd|dd|ddS )NzGUID(x, ))r   r
   r   r   )	r   r   r   r   r   r   r   r   r   s	            r    __repr__zGUID.__repr__B   s   )-&BBBB Gtz  G  G  Gtz  G  G  Gtz  G  G  Gr  G  G  Gr  G  G  GWY  G  G  G_a  G  G  Ggi  G  G  Goq  G  G  Gwy  G  G  G  @B  G  G  G  G  	Gr"   otherobjectboolc                l    t          |t                    ot          |           t          |          k    S r   )
isinstancer	   bytes)r   r)   s     r    __eq__zGUID.__eq__F   s(    %&&F5;;%,,+FFr"   c                :    t          t          |                     S r   )hashr.   r   s    r    __hash__zGUID.__hash__I   s    E$KK   r"   textc                z   t          j        d|          sd| }t          |          t                              d          d          }t                              d          d          }t                              d          d          }fdt          dd	          D             } | |||g|R  S )
zConvert a GUID string into a GUID object.

        Valid: 'bd4ec2d2-0662-4bee-ba8e-6f29f032e096' and '{bd4ec2d2-0662-4bee-ba8e-6f29f032e096}'
        z^\{?([0-9A-Fa-f]{8})-([0-9A-Fa-f]{4})-([0-9A-Fa-f]{4})-([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})-([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})\}?$zInvalid GUID format:             c                V    g | ]%}t                              |          d           &S )r7   )r   group).0imatchs     r    
<listcomp>z$GUID.from_string.<locals>.<listcomp>\   s-    <<<!c%++a.."%%<<<r"         )rer>   
ValueErrorr   r;   range)clsr4   msgd1d2d3d4r>   s          @r    from_stringzGUID.from_stringL   s      L  NRS S 	"0$00CS//! Q$$Q$$Q$$<<<<uQ||<<<s2r2#####r"   N)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)   r*   r   r+   )r   r   )r4   r#   r   r	   )__name__
__module____qualname__ctypesc_ulongc_ushortc_ubyte_fields_r!   r(   r/   r3   classmethodrK    r"   r    r	   r	   3   s        	&.!	&/"	&/"	&.1$%	H9 9 9 9G G G GG G G G! ! ! ! $ $ $ [$ $ $r"   r	   l   @   l   @   c                      e Zd ZdZd Zd ZdS )METHODzCOM method.c                    || _         || _        t          j        | j         g| j        R  | _        t          j        | j         t          j        g| j        R  | _        d S r   )restypeargtypesrO   WINFUNCTYPE	prototypec_void_pdirect_prototype)r   rY   argss      r    r!   zMETHOD.__init__m   sY    +DLI4=III & 24< aSWS` a a ar"   c                .    |                      ||          S r   )r\   )r   r=   names      r    get_com_proxyzMETHOD.get_com_proxyt   s    ~~a&&&r"   N)rL   rM   rN   __doc__r!   rb   rU   r"   r    rW   rW   j   s;        b b b' ' ' ' 'r"   rW   c                  "     e Zd ZdZ fdZ xZS )	STDMETHODz%COM method with HRESULT return value.c                P     t                      j        t          j        g|R   d S r   )superr!   rO   HRESULTr   r_   	__class__s     r    r!   zSTDMETHOD.__init__{   s*    /$//////r"   rL   rM   rN   rc   r!   __classcell__rj   s   @r    re   re   x   s>        //0 0 0 0 0 0 0 0 0r"   re   c                  "     e Zd ZdZ fdZ xZS )
VOIDMETHODz COM method with no return value.c                <     t                      j        d g|R   d S r   )rg   r!   ri   s     r    r!   zVOIDMETHOD.__init__   s(    %%%%%%%r"   rk   rm   s   @r    ro   ro      s>        **& & & & & & & & &r"   ro   c                  ,     e Zd Zd fd	Zd fd	Z xZS )_InterfaceMetaTc                  t          |          dk    r0t          dt          |          dz
   d|           sJ |d         f}d|vrd|d<   g }|d         t          j        ur=|d                                         D ]"}|                    |j        d                    #t          |          }t          |d                   }t          |          }t          |          |z   }	t          |	          D ]^\  }
\  }}t          | d|
 d| d	d
	                    d |j
        D                       pd d|j        dn|j        j         	          sJ _t          d| t          j        fdd |	D             i          }||d<   ||d<   dt          j        |          ff|d<   t                                          | |||          S )Nr6   z	Ignoring z
 bases on r   	_methods_rU   [z]: z: r&   c              3  $   K   | ]}|j         V  d S r   )rL   )r<   ts     r    	<genexpr>z)_InterfaceMeta.__new__.<locals>.<genexpr>   s$      +L+L1AJ+L+L+L+L+L+Lr"   voidz -> Vtable_rS   c                &    g | ]\  }}||j         fS rU   )r^   )r<   nr%   s      r    r?   z*_InterfaceMeta.__new__.<locals>.<listcomp>   s'    4e4e4eQUQRTUa9K5L4e4e4er"   _vtbl_struct_typevtbl_own_offsetvtbl_ptr)len
_debug_comrO   	Structureget_interface_inheritanceextend__dict__tuple	enumeratejoinrZ   rY   rL   _StructMetaPOINTERrg   __new__)rE   ra   basesdctcreate_pointer_typeinh_methodsinterface_typenew_methodsr~   all_methodsr=   method_namemtvtbl_struct_typerj   s                 r    r   z_InterfaceMeta.__new__   sK   u::>>J#e**q.JJDJJKKKKK1XKEc!!!C86+++#(8#E#E#G#G I I"">#:;#GHHHHK((C,--k**K((;6$-k$:$: 	Z 	Z A R Y Y Y Yk Y Y"&))+L+L+L+L+L"L"L"VPVY Y+-:+=2:CVY Y Z Z Z Z Z Z ''7'7'7(.(8':(24e4eYd4e4e4e'fh h $4 !0&7G(H(HIKJwwsD%555r"   c                   t                                          |||           |r1t          d| t          j        |d                   fd| i           d S d S )Npr   _type_)rg   r!   _pInterfaceMetarO   r   )r   ra   r   r   r   rj   s        r    r!   z_InterfaceMeta.__init__   sk    uc*** 	W JJJa)A)A(ChPTEUVVVVV		W 	Wr"   )T)rL   rM   rN   r   r!   rl   rm   s   @r    rr   rr      sf         6  6  6  6  6  6DW W W W W W W W W Wr"   rr   c                       e Zd Z fdZ xZS )r   c           	        |                     dd           @|d         j        }t          d| d|fd|                     dd          id          |d<   t          j                  D ]5\  }\  }|                    |j        z             }||d	fd

}||<   6t                                          | |||          }	ddl	m
}
 |	|
<   |	S )Nr   r   __HelperInterfacert   rU   F)r   )_m_ic                   t          d| dj         d| d dd                    t          t          | g|R                      d          sJ  || g|R  S )NzCalling COM z of z (z)[z] through pointer: (r&   r'   )r   rL   r   maprepr)r   r   r   r_   r   targets       r    pinterface_method_forwardz:_pInterfaceMeta.__new__.<locals>.pinterface_method_forward   s    ! #V #V #V #V #VB #V #VR] #V #V/3yyTD=4==9Q9Q/R/R#V #V #V W W W W Wr$&r"   )_pointer_type_cache)getr   rr   r   rt   rb   r~   rg   r   rO   r   )rE   ra   r   r   interface_baser=   methodmr   pointer_typer   r   r   rj   s              @@r    r   z_pInterfaceMeta.__new__   s9    4(( >"1X_N $$>$>$>$>%3$5%0#''+r2J2J$K8=? ? ?F #CM )2&2B(C(C 	9 	9$A$V$$Q)?%?MMA:; ' ' ' ' ' ' ' '  9CwwsD%== 	/.....&2F#r"   )rL   rM   rN   r   rl   rm   s   @r    r   r      s8        ' ' ' ' ' ' ' ' 'r"   r   c                  $    e Zd Zed             ZdS )	Interfacec                t    t          | j        d| j                            t                                       S )ah  Return a reverse iterator over the interfaces implemented by this interface,
        ranging from just after the base type `Interface` to this type.
        `Interface` does not represent an actual interface, but merely the base concept of
        them. Viewing it as part of an interface's inheritance chain is meaningless, so
        it is excluded.
        N)reversed__mro__indexr   )rE   s    r    r   z#Interface.get_interface_inheritance   s/     $AS[%6%6y%A%A$ABCCCr"   N)rL   rM   rN   rT   r   rU   r"   r    r   r      s2        D D [D D Dr"   r   F)	metaclassr   c                  (    e Zd ZeZed             ZdS )
pInterfacec                d    t          |t                    s|S |                    | j                  S )z@When dealing with a COMObject, pry a fitting interface out of it)r-   	COMObjectas_interfacer   )rE   objs     r    
from_paramzpInterface.from_param   s1     #y)) 	J
+++r"   N)rL   rM   rN   r   r   rT   r   rU   r"   r    r   r      s2        F, , [, , ,r"   r   )r   c                  z    e Zd Zd eeej                  fd eej                  fd eej                  fgZ	dS )IUnknownQueryInterfaceAddRefReleaseN
rL   rM   rN   re   REFIIDrO   r]   rW   c_intrt   rU   r"   r    r   r      S        	99VV_==>	66&,''(	FF6<(()IIIr"   r   c                  z    e Zd Zd eeej                  fd eej                  fd eej                  fgZ	dS )	pIUnknownr   r   r   Nr   rU   r"   r    r   r     r   r"   r   c                      fd}|S )zMCreate a callback returning E_NOTIMPL for methods not present on a COMObject.c                 @    t          d d           sJ t          S )NzNon-implemented method z called in )r   	E_NOTIMPL)r   interface_namer   s    r    missing_cb_funcz&_missing_impl.<locals>.missing_cb_func  s.    \K\\N\\]]]]]r"   rU   )r   r   r   s   `` r    _missing_implr     s*          r"   c                      fd}|S )zcIf a method was found in class, create a callback extracting self from the struct
    pointer.
    c                    t          d d           sJ t          j        | z   t          j        t          j                            j        j        } |g|R  }|t          n|S )NCOMObject method  called through interface )r   rO   castr   	py_objectcontentsvalueS_OK)r   r_   r   resultr   method_funcr   self_distances       r    self_extracting_cb_funcz,_found_impl.<locals>.self_extracting_cb_func  sw    ekeeUceefffff{1},fnV=M.N.NOOX^T)D)))~tt61r"   rU   )r   r   r   r   r   s   ```` r    _found_implr     s6    
2 2 2 2 2 2 2 2 #"r"   c                      fd}|S )zA method implemented in a previous interface modifies the COMOboject pointer so it
    corresponds to an earlier interface and passes it on to the actual implementation.
    c                P    t          d d d           sJ  | z   g|R  S )Nr   r   z, adjusting pointer by )r   )r   r_   r   r   offsetoriginal_methods     r    adjustor_cb_funcz&_adjust_impl.<locals>.adjustor_cb_func*  sr     Mk M M+M MDJM M N N 	N 	N Nq6z1D1111r"   rU   )r   r   r   r   r   s   ```` r    _adjust_implr   %  s6    
2 2 2 2 2 2 2 2
 r"   c                  .     e Zd ZdZ fdZd Zd Z xZS )r   a  A COMObject for implementing C callbacks in Python.
    Specify the interface types it supports in `_interfaces_`, and any methods to be implemented
    by those interfaces as standard python methods. If the names match, they will be run as
    callbacks with all arguments supplied as the types specified in the corresponding interface,
    and `self` available as usual.
    Remember to call `super().__init__()`.

    COMObjects can be passed to ctypes functions directly as long as the corresponding argtype is
    an `Interface` pointer, or a `pInterface` subclass.

    IUnknown's methods will be autogenerated in case IUnknown is implemented.
    c          
     :    t                      j        di | | j                            dd          }|st	          d          |D ]+}|D ]&}||u rt          ||          rt	          d          ',t          j        t          j                  }g }i }i }t          |          D ]*\  }	}|
                                }
|
D ]}||vr|	|z  ||<   +t          |v rd }d }d }|| _        || _        || _        t          |          D ]\  }	}g }|j        j        D ]\  }}||v r)||         \  }}t#          |j        ||||	z
  |z            }nZt'          | |d           x}t)          |j        |          }n*t+          |j        ||t-          |          |	z
  |z            }||	f||<   |                     ||                      |j        | }|                    t          j        |                     g }t          |          D ]6\  }	}|                    d|	 t          j        |j                  f           7|                    d	t          j        f           || _        || _        t;          | j         d
t          j        fd|i          | _        d S )N_interfaces_rU   z9At least one interface must be defined to use a COMObjectz Only specify the leaf interfacesc                x    dt          j        |t          j        t           j                            d<   t          S )Nr   )rO   r   r   r]   E_NOINTERFACE)r   iid_ptrres_ptrs      r    r   z3COMObject.__init_subclass__.<locals>.QueryInterface_  s*    KLGV^FO%D%DEEaH$$r"   c                0    | xj         dz  c_         | j         S Nr6   )
_vrefcountr2   s    r    r   z+COMObject.__init_subclass__.<locals>.AddRefc  s    1$&r"   c                ~    | j         dk    rt          d|  d| j                    sJ | xj         dz  c_         | j         S )Nr   z
COMObject z: Release while refcount was r6   )r   r   r2   s    r    r   z,COMObject.__init_subclass__.<locals>.Releaseg  sZ    ?a''%YTYYYY     1$&r"   	vtbl_ptr_self__StructrS   ) rg   __init_subclass__r   r   	TypeError
issubclassrO   sizeofr]   r   r   r   r   r   r   r}   rS   r   rL   getattrr   r   r   appendpointerr   r   _interface_to_vtbl_offset_vtbl_pointersr   r   _struct_type)rE   kwargsimplemented_leaf_interfacesr   r)   	_ptr_sizer   implemented_methodsr   r=   r   baser   r   r   wrappersr   method_typefuncimplementing_vtbl_idxmthfound_methodvtblfieldsitfrj   s                            r    r   zCOMObject.__init_subclass__@  s|   !!++F+++&)l&6&6~r&J&J#* 	YWXXX9 	H 	HN4 H H!U**ne44 H#$FGGGHH M&/22	  %'!!*+F!G!G 	D 	DA~"<<>>E D D88867)m-d3D 000% % %' ' '' ' ' "0CCJ!CK!*+F!G!G 	8 	8A~H,:,L,U 2 2(["555 3Fk2R/D/&~'>'2'+(=(AY'NP PCC )0[$(G(GGP+N,C[QQ).*A*5*6+./J+K+Ka+OS\*\^ ^
 9<Qx'4C 0 011113>3X>D!!&."6"67777 ;<< 	T 	TFAsMM?q??FN3;P,Q,QRSSSSw 01222(A%+&#,'?'?'?&BRATWaciVjkkr"   c                j    d| _          | j        g | j        t          j        |           R  | _        d S r   )r   r   r   rO   r   _structr2   s    r    r!   zCOMObject.__init__  s;    (t(V$*=Vv?OPT?U?UVVVr"   c                    | j                             |d           x}t          d|           t          j        | j        |          S )NzDoes not implement )r   r   r   rO   byrefr  )r   r   r   s      r    r   zCOMObject.as_interface  sM     488NNNFWB.BBCCC|DL&111r"   )rL   rM   rN   rc   r   r!   r   rl   rm   s   @r    r   r   2  sn         Xl Xl Xl Xl XltW W W	2 	2 	2 	2 	2 	2 	2r"   r   	interfacepInterface | Interfaceguidtarget_interfaceerrorsSequence[int]r   r+   c                    	 |                      |t          j        |                     dS # t          $ r}|D ]}|dz  r Y d}~dS Y d}~nd}~ww xY wd|  d| d}t	          |          )a  
    Determine if the target interface is available from the source.

    Args:
        interface:
            The interface to query from.
        guid:
            The GUID of the target interface.
        target_interface:
            The interface object to query from.
        errors:
            Acceptable errors to consider as caught.

    Returns:
        If the query was successful, returns True. If so, the target interface object is available.
    Tl    NFzFailed to Query Interface z with GUID: .)r   rO   r  OSError	Exception)r  r
  r  r  eerrorrF   s          r    is_availabler    s    $  v|4D'E'EFFFt    	 	Ez! uuuuuu	 	 	 	 	
 Fy
E
Ed
E
E
EC
C..s   (, 
A
AAA)
r  r	  r
  r	   r  r	  r  r  r   r+   ))rc   
__future__r   rO   rB   systypingr   pyglet.utilr   r   platformImportErrorr   r	   r   LPGUIDIIDr   r   r   r   rW   re   ro   r   _DummyPointerTypetype_PointerMetar   rr   r   r   r   r   r   r   r   r   r   r  rU   r"   r    <module>r      sY  " "F # " " " " "  				 



       # # # # # #[%%
<7
+P
Q
QQ+$ +$ +$ +$ +$6 +$ +$ +$\ 
		
				' ' ' ' ' ' ' '0 0 0 0 0 0 0 0& & & & & & & & #FN6<00 t%&&d6#$$*W *W *W *W *W[ *W *W *WZ( ( ( ( (l ( ( (V	D 	D 	D 	D 	D NPU 	D 	D 	D 	D
, 
, 
, 
, 
,"o 
, 
, 
, 
,    y       
     # # #
 
 
u2 u2 u2 u2 u2 u2 u2 u2p +4])C      r"   