
    \
j6              	          d Z ddlZddl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 G d	 d
          Z G d de          Z G d d          Z G d dej                  Z G d d e ej
        e                              Z e ed           ej
        e          fddi          Z G d de          Z G d d eej                  e          Z G d dej        ed          Zd Zd Zd  Zi Zd! Z G d" d#          Z G d$ de%          Z  G d& d'e%          Z! G d( d)e          Z"dS )*a	  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 STDMETHOD or METHOD 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.
    N)debug_print	debug_comwin32z-pyglet.com requires a Windows build of Pythonc                   v    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 Zd Z	d Z
d	 Zd
 ZdS )GUIDData1Data2Data3Data4   c                 X    || _         || _        || _        ||||||	|
|f| j        d d <   d S N)r   r	   r
   r   )selflw1w2b1b2b3b4b5b6b7b8s               O/DATA/AppData/hermes/venv/lib/python3.11/site-packages/pyglet/libs/win32/com.py__init__zGUID.__init__[   s;    


RRRR8
111    c                 `    | j         \  }}}}}}}}d| j        | j        | j        ||||||||fz  S )Nz0GUID(%x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x))r   r   r	   r
   )	r   r   r   r   r   r   r   r   r   s	            r   __repr__zGUID.__repr__a   sM    )-&BBBBAJ
DJBBBBEP P 	Pr   c                     t          |t                    r/t          j        t	          |           t	          |                    S dS )N)
isinstancer   ctypescmpbytesr   others     r   __cmp__zGUID.__cmp__f   s7    eT"" 	9:eDkk5<<888rr   c                 l    t          |t                    ot          |           t          |          k    S r   )r"   r   r%   r&   s     r   __eq__zGUID.__eq__k   s(    %&&F5;;%,,+FFr   c                 :    t          t          |                     S r   )hashr%   r   s    r   __hash__zGUID.__hash__n   s    E$KK   r   N)__name__
__module____qualname__r#   c_ulongc_ushortc_ubyte_fields_r   r   r(   r*   r.    r   r   r   r   S   s        	&.!	&/"	&/"	&.1$%	H9 9 9P P P
  
G G G! ! ! ! !r   r   c                       e Zd ZdZd Zd ZdS )METHODzCOM method.c                 "    || _         || _        d S r   )restypeargtypes)r   r:   argss      r   r   zMETHOD.__init__z   s    r   c                 :    t          j        | j        g| j        R  S r   )r#   WINFUNCTYPEr:   r;   r-   s    r   	get_fieldzMETHOD.get_field~   s    !$,?????r   N)r/   r0   r1   __doc__r   r?   r6   r   r   r8   r8   w   s=          @ @ @ @ @r   r8   c                   "     e Zd ZdZ fdZ xZS )	STDMETHODz%COM method with HRESULT return value.c                 ^     t          t          |           j        t          j        g|R   d S r   )superrB   r   r#   HRESULT)r   r<   	__class__s     r   r   zSTDMETHOD.__init__   s0    'i'>>>>>>>r   )r/   r0   r1   r@   r   __classcell__rF   s   @r   rB   rB      s>        //? ? ? ? ? ? ? ? ?r   rB   c                       e Zd ZdZd Zd ZdS )COMMethodInstancezBinds a COM interface method.c                 0    || _         || _        || _        d S r   )nameimethod)r   rL   rM   rN   s       r   r   zCOMMethodInstance.__init__   s    	r   c                 6      fd}|S t                      )Nc                     t          d                    j        j        j        j        |                     sJ   j                                        j        j                  g| R  }t          d                    j        j        j        j        |                     sJ t          d                    |                    sJ |S )NzCOM: #{} IN {}({}, {})zCOM: #{} OUT {}({}, {})zCOM: RETURN {})
_debug_comformatrM   rL   rF   r/   rN   r?   )r<   retobjr   s     r   _callz(COMMethodInstance.__get__.<locals>._call   s    !":"A"A$&$)UXUbUkmq"r"rsssss@-dk++--dfdi@@LtLLL!";"B"B4649VYVcVlnr"s"sttttt!"2"9"9#">">?????
r   )AttributeError)r   rT   tprU   s   ``  r   __get__zCOMMethodInstance.__get__   s:    ?      Lr   N)r/   r0   r1   r@   r   rX   r6   r   r   rJ   rJ      s8        ''  
    r   rJ   c                   &    e Zd ZdZdej        fgZdS )COMInterfacez6Dummy struct to serve as the type of all COM pointers.lpVtblN)r/   r0   r1   r@   r#   c_void_pr5   r6   r   r   rZ   rZ      s%        @@	6?#HHHr   rZ   c                   "     e Zd ZdZ fdZ xZS )InterfacePtrMetazAllows interfaces to be subclassed as ctypes POINTER and expects to be populated with data from a COM object.
       TODO: Phase this out and properly use POINTER(Interface) where applicable.
    c                    g }|d d d         D ]0}|                     |j                            dd                     1|                     |                    dd                     t          |          D ]\  }\  }}t	          |||          ||<   t
          |d<   t          t          |                               | |||          S )Nr!   	_methods_r6   _type_)	extend__dict__get	enumeraterJ   rZ   rD   r^   __new__)
clsrL   basesdctmethodsbaserM   nrN   rF   s
            r   rf   zInterfacePtrMeta.__new__   s    $$B$K 	? 	?DNN4=,,["==>>>>sww{B//000'00 	5 	5NA{6&q!V44CFF$H%s++33CucJJJr   r/   r0   r1   r@   rf   rG   rH   s   @r   r^   r^      sK         K K K K K K K K Kr   r^   	Interfacer@   zBase COM interface pointer.c                   ,     e Zd ZdZdd fdZd Z xZS )	COMInterfaceMetaab  This differs in the original as an implemented interface object, not a POINTER object.
       Used when the user must implement their own functions within an interface rather than
       being created and generated by the COM object itself. The types are automatically inserted in the ctypes type
       cache so it can recognize the type arguments.
    TreturnNc                   t                                          |||           |r`|s
| t          f}n| t          j        |d                   f}ddlm} t          d                    | j                  |d| id          || <   d S d S )Nr   )_pointer_type_cachezPOINTER({})__interface__F)create_pointer_type)	rD   r   
COMPointerr#   POINTERrs   COMPointerMetarR   r/   )r   rL   rh   ri   ru   
_ptr_basesrs   rF   s          r   r   zCOMInterfaceMeta.__init__   s    uc*** 	R >"J/

"FN58$<$<=
 322222(6}7K7KDM7Z7Z7A8G7NKP)R )R )R%%%	R 	Rr   c                    	 d}|                                  dd         D ]}|t          |j        d                   z  } |S # t          $ r<}|j        \  }|dk    r't          d                    |j                             d}~ww xY w)zReturns the amount of COM methods in all subclasses to determine offset of methods.
           Order must be exact from the source when calling COM methods.
        r      r!   r`   z.Interface '{}' requires a _methods_ attribute.N)mrolenrc   KeyErrorr<   	TypeErrorrR   r/   )r   resultitferrrL   s        r   __get_subclassed_methodcountz-COMInterfaceMeta.__get_subclassed_methodcount   s    		Fxxzz!B$' 9 9#cl;7888M 	 	 	hGT{"" P W WX[Xd e efff		s   ?A 
B7BBT)rq   N)r/   r0   r1   r@   r   -_COMInterfaceMeta__get_subclassed_methodcountrG   rH   s   @r   rp   rp      sb         R R R R R R R       r   rp   c                   $     e Zd ZdZd fd	Z xZS )rx   z9Required to prevent metaclass conflicts with inheritance.Tc                L    t                                          | |||          S r   )rD   rf   )rg   rL   rh   ri   ru   rF   s        r   rf   zCOMPointerMeta.__new__   s    wwsD%555r   r   rm   rH   s   @r   rx   rx      sC        CC6 6 6 6 6 6 6 6 6 6r   rx   c                   (    e Zd ZdZed             ZdS )rv   zFCOM Pointer base, could use c_void_p but need to override from_param .c                     |dS 	 |j         }	 || j                 S # t          $ r( t          d                    | j                            w xY w# t          $ r t          d|           w xY w)zAllows obj to return ctypes pointers, even if it's base is not a ctype.
           In this case, all we simply want is a ctypes pointer matching the cls interface from the obj.
        Nz2Interface {} doesn't have a pointer in this class.zJInterface method argument specified incorrectly, or passed wrong argument.)	_pointersrt   r~   r   rR   r/   rV   	Exception)rg   rT   ptr_dcts      r   
from_paramzCOMPointer.from_param   s    
 ;F	kmGks011 k k k T [ [\_\h i ijjjk  	o 	o 	ohjmnnn	os   A  2AA+N)r/   r0   r1   r@   classmethodr   r6   r   r   rv   rv      s8        PPk k [k k kr   rv   F)	metaclassru   c                       fd}|S )zJFunctions that are not implemented use this to prevent errors when called.c                  R    t          d                                        sJ dS )z7Return E_NOTIMPL because the method is not implemented.z2Undefined method: {0} was called in interface: {1}r   )rQ   rR   )r<   interface_namemethod_names    r   missing_cb_funcz&_missing_impl.<locals>.missing_cb_func  s/    NUUVacqrrsssssqr   r6   )r   r   r   s   `` r   _missing_implr     s*         
 r   c                     fd}|S )z<If a method was found in class, we can set it as a callback.c                  L    	  | i |}n# t           $ r}|d }~ww xY w|sdS |S )Nr   )r   )r<   kwr   r   method_funcs       r   cb_funcz_found_impl.<locals>.cb_func  sV    	 [$-"--FF 	 	 	I	  	1s    
r6   )r   r   r   r   s     ` r   _found_implr     s#    	 	 	 	 	 Nr   c                 H    |t          | |          S t          | ||          S )z2Create a callback function for ctypes if possible.)r   r   )	interfacerL   r   s      r   _make_callback_funcr   '  s*    Y---y$444r   c                     	 t           |          S # t          $ rE t          d                    |j                  t
          j        fd| i          }|t           | <   |cY S w xY w)z<Create virtual table structure with fields for use in COM's.zVtbl_{}r5   )_cached_structuresr~   typerR   r/   r#   	Structure)fieldsr   Vtbls      r   create_vtbl_structurer   3  sp    !&))   I$$Y%7886;K:MPZ\bOcdd%)6"s    AAAc                   B     e Zd ZdZg Z fdZd Zed             Z xZ	S )	COMObjectzYA base class for defining a COM object for use with callbacks and custom implementations.c                     t          t          |                               |           }t          | j                  dk    s
J d            i |_        |                                 |S )Nr   z9Atleast one interface must be defined to use a COMObject.)rD   r   rf   r}   _interfaces_r   %_COMObject__create_interface_pointers)rg   r<   r   new_clsrF   s       r   rf   zCOMObject.__new__A  sd    	3''//443#$$q(((*e(((++---r   c                 &   t          | j                  }|ddd         D ]}g }g }|j        ddd         D ]}|j        D ]}|\  }}t	          | |d          }	t          |j        ||	          }
t          j        |j	        g|j
        R  }|                    ||f           |                     ||
                     t          t          |          |          } || }t          j        t          j        |                    | j        |<   dS )zICreate a custom ctypes structure to handle COM functions in a COM Object.Nr!   )tupler   __mro__r`   getattrr   r/   r#   r>   r:   r;   appendr   pointerr   )rg   
interfacesr   rj   r   r   rN   rL   
com_methodfound_methodmthprotoitf_structurevtbls                 r   __create_interface_pointersz%COMObject.__create_interface_pointersH  s>   3+,,
ddd# 	F 	FCGF [R0 
/ 
/	'1 	/ 	/F'-$D*#*3d#;#;L-clD,OOC".z/AXJDWXXXEMM4-000NN55::....	/ 2%--KKM !='*D!'t0D0D!E!ECM#+	F 	Fr   c                     | j         S )zkReturns pointers to the implemented interfaces in this COMObject.  Read-only.

        :type: dict
        )r   r-   s    r   pointerszCOMObject.pointersb  s     ~r   )
r/   r0   r1   r@   r   rf   r   propertyr   rG   rH   s   @r   r   r   =  sp        ccL    F F F4   X    r   r   c                       e Zd Zg ZdS )rn   N)r/   r0   r1   r`   r6   r   r   rn   rn   j  s        IIIr   )r   c                       e Zd ZdZd eej        eej                  fd eej	        ej                  fd eej	        ej                  fgZ
dS )IUnknownzSThese methods are not implemented by default yet. Strictly for COM method ordering.QueryInterfaceAddRefReleaseN)r/   r0   r1   r@   rB   r#   r\   REFIIDr8   c_intr`   r6   r   r   r   r   n  se        ]]	99V_ffoNNO	66&,889	FF6<99: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   N)
r/   r0   r1   rB   r   r#   r\   r8   r   r`   r6   r   r   r   r   w  sS        	99VV_==>	66&,''(	FF6<(()IIIr   r   )#r@   sysr#   pyglet.utilr   rQ   platformImportErrorr   r   rw   LPGUIDIIDr   r8   rB   rJ   rZ   r   r^   str
pInterfacerp   r\   rx   rv   r   r   r   r   r   r   rn   r   r   r6   r   r   <module>r      s  H" "H 


  # # # # # #[%%
<7
+E
F
FF! ! ! ! !6 ! ! !> 
		
				@ 	@ 	@ 	@ 	@ 	@ 	@ 	@? ? ? ? ? ? ? ?       ,    6#   K K K K KttNFN<8899 K K K* cc+..-v~l;;=(*GHJ J

$ $ $ $ $t $ $ $N6 6 6 6 6TT&/**,< 6 6 6k k k k kNPU k k k k*    "5 5 5    + + + + + + + +Z    *        )        
     r   