<s> """<STR_LIT>""" <EOL> __docformat__ = '<STR_LIT>' <EOL> import sys <EOL> import threading <EOL> import os <EOL> from ctypes import * <EOL> from ctypes . util import find_library <EOL> import time <EOL> import logging <EOL> from Skype4Py . api import Command , SkypeAPIBase , timeout2float , finalize_opts <EOL> from Skype4Py . enums import * <EOL> from Skype4Py . errors import SkypeAPIError <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> PropertyChangeMask = <NUM_LIT> <EOL> PropertyNotify = <NUM_LIT> <EOL> ClientMessage = <NUM_LIT> <EOL> PropertyNewValue = <NUM_LIT:0> <EOL> PropertyDelete = <NUM_LIT:1> <EOL> c_ulong_p = POINTER ( c_ulong ) <EOL> DisplayP = c_void_p <EOL> Atom = c_ulong <EOL> AtomP = c_ulong_p <EOL> XID = c_ulong <EOL> Window = XID <EOL> Bool = c_int <EOL> Status = c_int <EOL> Time = c_ulong <EOL> c_int_p = POINTER ( c_int ) <EOL> align = ( sizeof ( c_long ) == <NUM_LIT:8> and sizeof ( c_int ) == <NUM_LIT:4> ) <EOL> class XClientMessageEvent ( Structure ) : <EOL> if align : <EOL> _fields_ = [ ( '<STR_LIT:type>' , c_int ) , <EOL> ( '<STR_LIT>' , c_int ) , <EOL> ( '<STR_LIT>' , c_ulong ) , <EOL> ( '<STR_LIT>' , Bool ) , <EOL> ( '<STR_LIT>' , c_int ) , <EOL> ( '<STR_LIT>' , DisplayP ) , <EOL> ( '<STR_LIT>' , Window ) , <EOL> ( '<STR_LIT>' , Atom ) , <EOL> ( '<STR_LIT>' , c_int ) , <EOL> ( '<STR_LIT>' , c_int ) , <EOL> ( '<STR_LIT:data>' , c_char * <NUM_LIT:20> ) ] <EOL> else : <EOL> _fields_ = [ ( '<STR_LIT:type>' , c_int ) , <EOL> ( '<STR_LIT>' , c_ulong ) , <EOL> ( '<STR_LIT>' , Bool ) , <EOL> ( '<STR_LIT>' , DisplayP ) , <EOL> ( '<STR_LIT>' , Window ) , <EOL> ( '<STR_LIT>' , Atom ) , <EOL> ( '<STR_LIT>' , c_int ) , <EOL> ( '<STR_LIT:data>' , c_char * <NUM_LIT:20> ) ] <EOL> class XPropertyEvent ( Structure ) : <EOL> if align : <EOL> _fields_ = [ ( '<STR_LIT:type>' , c_int ) , <EOL> ( '<STR_LIT>' , c_int ) , <EOL> ( '<STR_LIT>' , c_ulong ) , <EOL> ( '<STR_LIT>' , Bool ) , <EOL> ( '<STR_LIT>' , c_int ) , <EOL> ( '<STR_LIT>' , DisplayP ) , <EOL> ( '<STR_LIT>' , Window ) , <EOL> ( '<STR_LIT>' , Atom ) , <EOL> ( '<STR_LIT:time>' , Time ) , <EOL> ( '<STR_LIT:state>' , c_int ) , <EOL> ( '<STR_LIT>' , c_int ) ] <EOL> else : <EOL> _fields_ = [ ( '<STR_LIT:type>' , c_int ) , <EOL> ( '<STR_LIT>' , c_ulong ) , <EOL> ( '<STR_LIT>' , Bool ) , <EOL> ( '<STR_LIT>' , DisplayP ) , <EOL> ( '<STR_LIT>' , Window ) , <EOL> ( '<STR_LIT>' , Atom ) , <EOL> ( '<STR_LIT:time>' , Time ) , <EOL> ( '<STR_LIT:state>' , c_int ) ] <EOL> class XErrorEvent ( Structure ) : <EOL> if align : <EOL> _fields_ = [ ( '<STR_LIT:type>' , c_int ) , <EOL> ( '<STR_LIT>' , c_int ) , <EOL> ( '<STR_LIT>' , DisplayP ) , <EOL> ( '<STR_LIT>' , XID ) , <EOL> ( '<STR_LIT>' , c_ulong ) , <EOL> ( '<STR_LIT>' , c_ubyte ) , <EOL> ( '<STR_LIT>' , c_ubyte ) , <EOL> ( '<STR_LIT>' , c_ubyte ) ] <EOL> else : <EOL> _fields_ = [ ( '<STR_LIT:type>' , c_int ) , <EOL> ( '<STR_LIT>' , DisplayP ) , <EOL> ( '<STR_LIT>' , XID ) , <EOL> ( '<STR_LIT>' , c_ulong ) , <EOL> ( '<STR_LIT>' , c_ubyte ) , <EOL> ( '<STR_LIT>' , c_ubyte ) , <EOL> ( '<STR_LIT>' , c_ubyte ) ] <EOL> class XEvent ( Union ) : <EOL> if align : <EOL> _fields_ = [ ( '<STR_LIT:type>' , c_int ) , <EOL> ( '<STR_LIT>' , XClientMessageEvent ) , <EOL> ( '<STR_LIT>' , XPropertyEvent ) , <EOL> ( '<STR_LIT>' , XErrorEvent ) , <EOL> ( '<STR_LIT>' , c_long * <NUM_LIT> ) ] <EOL> else : <EOL> _fields_ = [ ( '<STR_LIT:type>' , c_int ) , <EOL> ( '<STR_LIT>' , XClientMessageEvent ) , <EOL> ( '<STR_LIT>' , XPropertyEvent ) , <EOL> ( '<STR_LIT>' , XErrorEvent ) , <EOL> ( '<STR_LIT>' , c_long * <NUM_LIT> ) ] <EOL> XEventP = POINTER ( XEvent ) <EOL> if getattr ( sys , '<STR_LIT>' , False ) : <EOL> class X ( object ) : <EOL> def __getattr__ ( self , name ) : <EOL> return self <EOL> def __setattr__ ( self , name , value ) : <EOL> pass <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> pass <EOL> x11 = X ( ) <EOL> else : <EOL> libpath = find_library ( '<STR_LIT>' ) <EOL> if not libpath : <EOL> raise ImportError ( '<STR_LIT>' ) <EOL> x11 = cdll . LoadLibrary ( libpath ) <EOL> del libpath <EOL> x11 . XCloseDisplay . argtypes = ( DisplayP , ) <EOL> x11 . XCloseDisplay . restype = None <EOL> x11 . XCreateSimpleWindow . argtypes = ( DisplayP , Window , c_int , c_int , c_uint , <EOL> c_uint , c_uint , c_ulong , c_ulong ) <EOL> x11 . XCreateSimpleWindow . restype = Window <EOL> x11 . XDefaultRootWindow . argtypes = ( DisplayP , ) <EOL> x11 . XDefaultRootWindow . restype = Window <EOL> x11 . XDeleteProperty . argtypes = ( DisplayP , Window , Atom ) <EOL> x11 . XDeleteProperty . restype = None <EOL> x11 . XDestroyWindow . argtypes = ( DisplayP , Window ) <EOL> x11 . XDestroyWindow . restype = None <EOL> x11 . XFree . argtypes = ( c_void_p , ) <EOL> x11 . XFree . restype = None <EOL> x11 . XGetAtomName . argtypes = ( DisplayP , Atom ) <EOL> x11 . XGetAtomName . restype = c_void_p <EOL> x11 . XGetErrorText . argtypes = ( DisplayP , c_int , c_char_p , c_int ) <EOL> x11 . XGetErrorText . restype = None <EOL> x11 . XGetWindowProperty . argtypes = ( DisplayP , Window , Atom , c_long , c_long , Bool , <EOL> Atom , AtomP , c_int_p , c_ulong_p , c_ulong_p , POINTER ( POINTER ( Window ) ) ) <EOL> x11 . XGetWindowProperty . restype = c_int <EOL> x11 . XInitThreads . argtypes = ( ) <EOL> x11 . XInitThreads . restype = Status <EOL> x11 . XInternAtom . argtypes = ( DisplayP , c_char_p , Bool ) <EOL> x11 . XInternAtom . restype = Atom <EOL> x11 . XNextEvent . argtypes = ( DisplayP , XEventP ) <EOL> x11 . XNextEvent . restype = None <EOL> x11 . XOpenDisplay . argtypes = ( c_char_p , ) <EOL> x11 . XOpenDisplay . restype = DisplayP <EOL> x11 . XPending . argtypes = ( DisplayP , ) <EOL> x11 . XPending . restype = c_int <EOL> x11 . XSelectInput . argtypes = ( DisplayP , Window , c_long ) <EOL> x11 . XSelectInput . restype = None <EOL> x11 . XSendEvent . argtypes = ( DisplayP , Window , Bool , c_long , XEventP ) <EOL> x11 . XSendEvent . restype = Status <EOL> x11 . XLockDisplay . argtypes = ( DisplayP , ) <EOL> x11 . XLockDisplay . restype = None <EOL> x11 . XUnlockDisplay . argtypes = ( DisplayP , ) <EOL> x11 . XUnlockDisplay . restype = None <EOL> def threads_init ( gtk = True ) : <EOL> """<STR_LIT>""" <EOL> x11 . XInitThreads ( ) <EOL> if gtk : <EOL> from gtk . gdk import threads_init <EOL> threads_init ( ) <EOL> class SkypeAPI ( SkypeAPIBase ) : <EOL> def __init__ ( self , opts ) : <EOL> self . logger = logging . getLogger ( '<STR_LIT>' ) <EOL> SkypeAPIBase . __init__ ( self ) <EOL> finalize_opts ( opts ) <EOL> threads_init ( gtk = False ) <EOL> self . disp = x11 . XOpenDisplay ( None ) <EOL> if not self . disp : <EOL> raise SkypeAPIError ( '<STR_LIT>' ) <EOL> self . win_root = x11 . XDefaultRootWindow ( self . disp ) <EOL> self . win_self = x11 . XCreateSimpleWindow ( self . disp , self . win_root , <EOL> <NUM_LIT:100> , <NUM_LIT:100> , <NUM_LIT:100> , <NUM_LIT:100> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> x11 . XSelectInput ( self . disp , self . win_root , PropertyChangeMask ) <EOL> self . win_skype = self . get_skype ( ) <EOL> ctrl = '<STR_LIT>' <EOL> self . atom_msg = x11 . XInternAtom ( self . disp , ctrl , False ) <EOL> self . atom_msg_begin = x11 . XInternAtom ( self . disp , ctrl + '<STR_LIT>' , False ) <EOL> self . loop_event = threading . Event ( ) <EOL> self . loop_timeout = <NUM_LIT> <EOL> self . loop_break = False <EOL> def __del__ ( self ) : <EOL> if x11 : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> x11 . XDestroyWindow ( self . disp , self . win_self ) <EOL> x11 . XCloseDisplay ( self . disp ) <EOL> def run ( self ) : <EOL> self . logger . info ( '<STR_LIT>' ) <EOL> event = XEvent ( ) <EOL> data = '<STR_LIT>' <EOL> while not self . loop_break and x11 : <EOL> while x11 . XPending ( self . disp ) : <EOL> self . loop_timeout = <NUM_LIT> <EOL> x11 . XNextEvent ( self . disp , byref ( event ) ) <EOL> if event . type == ClientMessage : <EOL> if event . xclient . format == <NUM_LIT:8> : <EOL> if event . xclient . message_type == self . atom_msg_begin : <EOL> data = str ( event . xclient . data ) <EOL> elif event . xclient . message_type == self . atom_msg : <EOL> if data != '<STR_LIT>' : <EOL> data += str ( event . xclient . data ) <EOL> else : <EOL> self . logger . warning ( '<STR_LIT>' ) <EOL> else : <EOL> continue <EOL> if len ( event . xclient . data ) != <NUM_LIT:20> and data : <EOL> self . notify ( data . decode ( '<STR_LIT:utf-8>' ) ) <EOL> data = '<STR_LIT>' <EOL> elif event . type == PropertyNotify : <EOL> namep = x11 . XGetAtomName ( self . disp , event . xproperty . atom ) <EOL> is_inst = ( c_char_p ( namep ) . value == '<STR_LIT>' ) <EOL> x11 . XFree ( namep ) <EOL> if is_inst : <EOL> if event . xproperty . state == PropertyNewValue : <EOL> self . win_skype = self . get_skype ( ) <EOL> time . sleep ( <NUM_LIT:1.0> ) <EOL> self . set_attachment_status ( apiAttachAvailable ) <EOL> elif event . xproperty . state == PropertyDelete : <EOL> self . win_skype = None <EOL> self . set_attachment_status ( apiAttachNotAvailable ) <EOL> self . loop_event . wait ( self . loop_timeout ) <EOL> if self . loop_event . isSet ( ) : <EOL> self . loop_timeout = <NUM_LIT> <EOL> elif self . loop_timeout < <NUM_LIT:1.0> : <EOL> self . loop_timeout *= <NUM_LIT:2> <EOL> self . loop_event . clear ( ) <EOL> self . logger . info ( '<STR_LIT>' ) <EOL> def get_skype ( self ) : <EOL> """<STR_LIT>""" <EOL> skype_inst = x11 . XInternAtom ( self . disp , '<STR_LIT>' , True ) <EOL> if not skype_inst : <EOL> return <EOL> type_ret = Atom ( ) <EOL> format_ret = c_int ( ) <EOL> nitems_ret = c_ulong ( ) <EOL> bytes_after_ret = c_ulong ( ) <EOL> winp = pointer ( Window ( ) ) <EOL> fail = x11 . XGetWindowProperty ( self . disp , self . win_root , skype_inst , <EOL> <NUM_LIT:0> , <NUM_LIT:1> , False , <NUM_LIT> , byref ( type_ret ) , byref ( format_ret ) , <EOL> byref ( nitems_ret ) , byref ( bytes_after_ret ) , byref ( winp ) ) <EOL> if not fail and format_ret . value == <NUM_LIT:32> and nitems_ret . value == <NUM_LIT:1> : <EOL> return winp . contents . value <EOL> def close ( self ) : <EOL> self . loop_break = True <EOL> self . loop_event . set ( ) <EOL> while self . isAlive ( ) : <EOL> time . sleep ( <NUM_LIT> ) <EOL> SkypeAPIBase . close ( self ) <EOL> def set_friendly_name ( self , friendly_name ) : <EOL> SkypeAPIBase . set_friendly_name ( self , friendly_name ) <EOL> if self . attachment_status == apiAttachSuccess : <EOL> self . set_attachment_status ( apiAttachUnknown ) <EOL> self . attach ( ) <EOL> def attach ( self , timeout , wait = True ) : <EOL> if self . attachment_status == apiAttachSuccess : <EOL> return <EOL> self . acquire ( ) <EOL> try : <EOL> if not self . isAlive ( ) : <EOL> try : <EOL> self . start ( ) <EOL> except AssertionError : <EOL> raise SkypeAPIError ( '<STR_LIT>' ) <EOL> try : <EOL> self . wait = True <EOL> t = threading . Timer ( timeout2float ( timeout ) , lambda : setattr ( self , '<STR_LIT>' , False ) ) <EOL> if wait : <EOL> t . start ( ) <EOL> while self . wait : <EOL> self . win_skype = self . get_skype ( ) <EOL> if self . win_skype is not None : <EOL> break <EOL> else : <EOL> time . sleep ( <NUM_LIT:1.0> ) <EOL> else : <EOL> raise SkypeAPIError ( '<STR_LIT>' ) <EOL> finally : <EOL> t . cancel ( ) <EOL> command = Command ( '<STR_LIT>' % self . friendly_name , '<STR_LIT>' , True , timeout ) <EOL> self . release ( ) <EOL> try : <EOL> self . send_command ( command , True ) <EOL> finally : <EOL> self . acquire ( ) <EOL> if command . Reply != '<STR_LIT:OK>' : <EOL> self . win_skype = None <EOL> self . set_attachment_status ( apiAttachRefused ) <EOL> return <EOL> self . set_attachment_status ( apiAttachSuccess ) <EOL> finally : <EOL> self . release ( ) <EOL> command = Command ( '<STR_LIT>' % self . protocol , Blocking = True ) <EOL> self . send_command ( command , True ) <EOL> self . protocol = int ( command . Reply . rsplit ( None , <NUM_LIT:1> ) [ - <NUM_LIT:1> ] ) <EOL> def is_running ( self ) : <EOL> return ( self . get_skype ( ) is not None ) <EOL> def startup ( self , minimized , nosplash ) : <EOL> if not self . is_running ( ) : <EOL> if os . fork ( ) == <NUM_LIT:0> : <EOL> os . setsid ( ) <EOL> os . execlp ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def shutdown ( self ) : <EOL> from signal import SIGINT <EOL> fh = os . popen ( '<STR_LIT>' ) <EOL> pid = fh . readline ( ) . strip ( ) <EOL> fh . close ( ) <EOL> if pid : <EOL> os . kill ( int ( pid ) , SIGINT ) <EOL> skype_inst = x11 . XInternAtom ( self . disp , '<STR_LIT>' , True ) <EOL> if skype_inst : <EOL> x11 . XDeleteProperty ( self . disp , self . win_root , skype_inst ) <EOL> self . win_skype = None <EOL> self . set_attachment_status ( apiAttachNotAvailable ) <EOL> def send_command ( self , command , force = False ) : <EOL> if self . attachment_status != apiAttachSuccess and not force : <EOL> self . attach ( command . Timeout ) <EOL> self . push_command ( command ) <EOL> self . notifier . sending_command ( command ) <EOL> cmd = u'<STR_LIT>' % ( command . Id , command . Command ) <EOL> self . logger . debug ( '<STR_LIT>' , repr ( cmd ) ) <EOL> if command . Blocking : <EOL> command . _event = bevent = threading . Event ( ) <EOL> else : <EOL> command . _timer = timer = threading . Timer ( command . timeout2float ( ) , self . pop_command , ( command . Id , ) ) <EOL> event = XEvent ( ) <EOL> event . xclient . type = ClientMessage <EOL> event . xclient . display = self . disp <EOL> event . xclient . window = self . win_self <EOL> event . xclient . message_type = self . atom_msg_begin <EOL> event . xclient . format = <NUM_LIT:8> <EOL> cmd = cmd . encode ( '<STR_LIT:utf-8>' ) + '<STR_LIT:\x00>' <EOL> for i in xrange ( <NUM_LIT:0> , len ( cmd ) , <NUM_LIT:20> ) : <EOL> event . xclient . data = cmd [ i : i + <NUM_LIT:20> ] <EOL> x11 . XSendEvent ( self . disp , self . win_skype , False , <NUM_LIT:0> , byref ( event ) ) <EOL> event . xclient . message_type = self . atom_msg <EOL> self . loop_event . set ( ) <EOL> if command . Blocking : <EOL> bevent . wait ( command . timeout2float ( ) ) <EOL> if not bevent . isSet ( ) : <EOL> raise SkypeAPIError ( '<STR_LIT>' ) <EOL> else : <EOL> timer . start ( ) <EOL> def notify ( self , cmd ) : <EOL> self . logger . debug ( '<STR_LIT>' , repr ( cmd ) ) <EOL> if cmd . startswith ( u'<STR_LIT:#>' ) : <EOL> p = cmd . find ( u'<STR_LIT:U+0020>' ) <EOL> command = self . pop_command ( int ( cmd [ <NUM_LIT:1> : p ] ) ) <EOL> if command is not None : <EOL> command . Reply = cmd [ p + <NUM_LIT:1> : ] <EOL> if command . Blocking : <EOL> command . _event . set ( ) <EOL> else : <EOL> command . _timer . cancel ( ) <EOL> self . notifier . reply_received ( command ) <EOL> else : <EOL> self . notifier . notification_received ( cmd [ p + <NUM_LIT:1> : ] ) <EOL> else : <EOL> self . notifier . notification_received ( cmd ) </s>
