def Setup(self, plugin, waitTime): """ This will be called inside the thread at the beginning. """ self.plugin = plugin self.waitTime = waitTime self.timer = Timer(0, self.OnTimeOut) self.lastEvent = None wc = WNDCLASS() wc.hInstance = GetModuleHandle(None) wc.lpszClassName = "HiddenMceMessageReceiver" wc.lpfnWndProc = WNDPROC(self.MyWndProc) if not RegisterClass(byref(wc)): raise WinError() self.hwnd = CreateWindowEx(0, wc.lpszClassName, "MCE Remote Message Receiver", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, wc.hInstance, None) if not self.hwnd: raise WinError() self.wc = wc self.hinst = wc.hInstance self.dll = WinDLL(os.path.join(PLUGIN_DIR, "MceIr.dll")) if not self.dll.MceIrRegisterEvents(self.hwnd): raise self.plugin.Exceptions.DeviceNotFound self.dll.MceIrSetRepeatTimes(1, 1)
def __init__(self, deviceInterfaceGuid, callback, dataSize=1, suppressRepeat=False): self.callback = callback self.dataSize = dataSize self.suppressRepeat = suppressRepeat self.deviceInterfaceGuid = unicode(deviceInterfaceGuid) if self.dll is None: self.__class__.dll = WinDLL( join(eg.sitePackagesDir, "WinUsbWrapper.dll").encode('mbcs')) self.Open()
def Setup( self, plugin, waitTime, pollTime, useDefaultPollTime, initTime = 15.0, checkRepeatFlag = False, repeatReleaseTime = 200 ): """ This will be called inside the thread at the beginning. """ self.lock = Lock() self.abort = False self.plugin = plugin self.waitTime = float(waitTime)/1000.0 self.repeatReleaseTime = float(repeatReleaseTime)/1000.0 self.pollTime = pollTime self.useDefaultPollTime = useDefaultPollTime self.defaultPollTime = -1 self.LastKeyCode = -1 self.checkRepeatFlag = checkRepeatFlag self.repeatCode = c_int(0) self.systemCode = c_int(0) self.keyCode = c_int(0) self.lastEvent = eg.EventGhostEvent() self.keyStillPressed = False self.initTerminated = False self.timerInit = None self.timerKey = None self.hwnd = None self.dll = None # load irremote.dll try: regHandle = _winreg.OpenKey( _winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Hauppauge WinTV Infrared Remote', 0, _winreg.KEY_READ ) InstallString = _winreg.QueryValueEx(regHandle, 'UninstallString')[0] _winreg.CloseKey( regHandle ) irremoteDir = InstallString.partition(' ')[0].rsplit('\\',1)[0] dllPath = os.path.join(irremoteDir, "irremote.DLL") self.dll = windll.LoadLibrary(dllPath) self.dll = WinDLL(dllPath) except: plugin.PrintError("Couldn't find irremote.dll! Reinstalling the Hauppauge " "WinTV Infrared Remote package can solve the problem." ) raise self.plugin.Exceptions.DeviceNotFound self.IR_Open = WINFUNCTYPE( c_int, HWND, c_int, c_byte, c_int ) self.IR_Close = WINFUNCTYPE( c_int, HWND, c_int ) self.IR_GetSystemKeyCode = WINFUNCTYPE( c_int, POINTER( c_int), POINTER( c_int), POINTER( c_int) ) self.TIMERPROC = WINFUNCTYPE( None, HWND, c_uint, c_uint, DWORD ) self.IR_Open = self.dll.IR_Open self.IR_Close = self.dll.IR_Close self.IR_GetSystemKeyCode = self.dll.IR_GetSystemKeyCode wc = WNDCLASS() wc.hInstance = GetDesktopWindow() wc.lpszClassName = "HaupPluginEventSinkWndClass" wc.lpfnWndProc = WNDPROC(self.MyWndProc) if not RegisterClass(byref(wc)): raise WinError() self.hwnd = CreateWindow( wc.lpszClassName, "HaupaugePlugin Event Window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, wc.hInstance, None ) if not self.hwnd: raise WinError() self.wc = wc self.hinst = wc.hInstance self.timerInit = Timer( initTime, self.PostInit) # Init delayed init timer ( in case of standby problems) self.timerInit.start()
class HauppaugeIRMessageReceiver(eg.ThreadWorker): """ A thread with a hidden window to receive win32 messages from the driver """ @eg.LogIt def Setup( self, plugin, waitTime, pollTime, useDefaultPollTime, initTime = 15.0, checkRepeatFlag = False, repeatReleaseTime = 200 ): """ This will be called inside the thread at the beginning. """ self.lock = Lock() self.abort = False self.plugin = plugin self.waitTime = float(waitTime)/1000.0 self.repeatReleaseTime = float(repeatReleaseTime)/1000.0 self.pollTime = pollTime self.useDefaultPollTime = useDefaultPollTime self.defaultPollTime = -1 self.LastKeyCode = -1 self.checkRepeatFlag = checkRepeatFlag self.repeatCode = c_int(0) self.systemCode = c_int(0) self.keyCode = c_int(0) self.lastEvent = eg.EventGhostEvent() self.keyStillPressed = False self.initTerminated = False self.timerInit = None self.timerKey = None self.hwnd = None self.dll = None # load irremote.dll try: regHandle = _winreg.OpenKey( _winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Hauppauge WinTV Infrared Remote', 0, _winreg.KEY_READ ) InstallString = _winreg.QueryValueEx(regHandle, 'UninstallString')[0] _winreg.CloseKey( regHandle ) irremoteDir = InstallString.partition(' ')[0].rsplit('\\',1)[0] dllPath = os.path.join(irremoteDir, "irremote.DLL") self.dll = windll.LoadLibrary(dllPath) self.dll = WinDLL(dllPath) except: plugin.PrintError("Couldn't find irremote.dll! Reinstalling the Hauppauge " "WinTV Infrared Remote package can solve the problem." ) raise self.plugin.Exceptions.DeviceNotFound self.IR_Open = WINFUNCTYPE( c_int, HWND, c_int, c_byte, c_int ) self.IR_Close = WINFUNCTYPE( c_int, HWND, c_int ) self.IR_GetSystemKeyCode = WINFUNCTYPE( c_int, POINTER( c_int), POINTER( c_int), POINTER( c_int) ) self.TIMERPROC = WINFUNCTYPE( None, HWND, c_uint, c_uint, DWORD ) self.IR_Open = self.dll.IR_Open self.IR_Close = self.dll.IR_Close self.IR_GetSystemKeyCode = self.dll.IR_GetSystemKeyCode wc = WNDCLASS() wc.hInstance = GetDesktopWindow() wc.lpszClassName = "HaupPluginEventSinkWndClass" wc.lpfnWndProc = WNDPROC(self.MyWndProc) if not RegisterClass(byref(wc)): raise WinError() self.hwnd = CreateWindow( wc.lpszClassName, "HaupaugePlugin Event Window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, wc.hInstance, None ) if not self.hwnd: raise WinError() self.wc = wc self.hinst = wc.hInstance self.timerInit = Timer( initTime, self.PostInit) # Init delayed init timer ( in case of standby problems) self.timerInit.start() @eg.LogItWithReturn def Finish(self): """ This will be called inside the thread when it finishes. It will even be called if the thread exits through an exception. """ self.abort = True self.lock.acquire() if self.dll: self.dll.IR_Close(self.hwnd, 0); #print "Irremote is stopped" if self.timerInit : self.timerInit.cancel() #print "Init aborted" if self.hwnd: windll.user32.KillTimer(self.hwnd, 1) DestroyWindow(self.hwnd) UnregisterClass(self.wc.lpszClassName, self.hinst) if self.defaultPollTime != -1 : regHandle = _winreg.OpenKey( _winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE\hauppauge\IR', 0, _winreg.KEY_WRITE | _winreg.KEY_READ ) _winreg.SetValueEx( regHandle, 'PollRate', 0, _winreg.REG_DWORD, int(self.defaultPollTime) ) _winreg.CloseKey( regHandle ) self.lock.release() def MyWndProc(self, hwnd, mesg, wParam, lParam) : if mesg == WM_TIMER: keyHit = self.IR_GetSystemKeyCode(byref(self.repeatCode), byref(self.systemCode), byref(self.keyCode) ); if keyHit == 1: #print "RepeatCode = ", self.repeatCode, " systemCode = ", self.systemCode, " keyCode = ", self.keyCode, " time = ", time() if self.timerKey : self.timerKey.cancel() key = self.keyCode.value repeat = ( self.repeatCode.value == 0 ) if ( not repeat and self.checkRepeatFlag ) or not ( self.keyStillPressed and self.LastKeyCode == key ) : self.LastKeyCode = key if not self.systemCode.value in HauppaugeIRTable: eventString = "%d" % key elif key in HauppaugeIRTable[ self.systemCode.value ]: eventString = HauppaugeIRTable[ self.systemCode.value ][key] else: eventString = "%d" % key self.lastEvent = self.plugin.TriggerEnduringEvent(eventString) self.keyStillPressed = True releaseTime = self.waitTime else : releaseTime = self.repeatReleaseTime self.timerKey = Timer(releaseTime, self.OnTimeOut) self.timerKey.start() return 1 def OnTimeOut(self): #print "Release Key" self.keyStillPressed = False self.lastEvent.SetShouldEnd() @eg.LogIt def PostInit( self ) : self.lock.acquire() if self.abort : self.lock.release() return self.timerInit = Timer( 60.0, self.OnPostInitTimeOut) # Init timeout timer self.timerInit.start() returnVal = False if ( not self.useDefaultPollTime ) : regHandle = _winreg.OpenKey( _winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE\hauppauge\IR', 0, _winreg.KEY_WRITE | _winreg.KEY_READ ) self.defaultPollTime = int( _winreg.QueryValueEx(regHandle, 'PollRate')[0] ) _winreg.SetValueEx( regHandle, 'PollRate', 0, _winreg.REG_DWORD, int(self.pollTime) ) returnVal = self.IR_Open(self.hwnd, 0, 0, 0); _winreg.SetValueEx( regHandle, 'PollRate', 0, _winreg.REG_DWORD, int(self.defaultPollTime) ) _winreg.CloseKey( regHandle ) self.defaultPollTime = -1 else : returnVal = self.IR_Open(self.hwnd, 0, 0, 0); self.timerInit.cancel() if not self.initTerminated : if not returnVal and not self.initTerminated : self.plugin.PrintError("Couldn't start the Hauppauge remote control") self.Stop() self.plugin.TriggerEvent("InitError") else : #print "Irremote is started" pass del self.timerInit self.timerInit = None self.lock.release() @eg.LogIt def OnPostInitTimeOut( self ) : self.initTerminated = True self.plugin.PrintError("Couldn't start the Hauppauge remote control") self.Stop() self.plugin.TriggerEvent("InitError")
class MceMessageReceiver(eg.ThreadWorker): """ A thread with a hidden window to receive win32 messages from the driver """ def Setup(self, plugin, waitTime): """ This will be called inside the thread at the beginning. """ self.plugin = plugin self.waitTime = waitTime self.timer = Timer(0, self.OnTimeOut) self.lastEvent = None wc = WNDCLASS() wc.hInstance = GetModuleHandle(None) wc.lpszClassName = "HiddenMceMessageReceiver" wc.lpfnWndProc = WNDPROC(self.MyWndProc) if not RegisterClass(byref(wc)): raise WinError() self.hwnd = CreateWindowEx(0, wc.lpszClassName, "MCE Remote Message Receiver", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, wc.hInstance, None) if not self.hwnd: raise WinError() self.wc = wc self.hinst = wc.hInstance self.dll = WinDLL(os.path.join(PLUGIN_DIR, "MceIr.dll")) if not self.dll.MceIrRegisterEvents(self.hwnd): raise self.plugin.Exceptions.DeviceNotFound self.dll.MceIrSetRepeatTimes(1, 1) @eg.LogIt def Finish(self): """ This will be called inside the thread when it finishes. It will even be called if the thread exits through an exception. """ self.dll.MceIrUnregisterEvents() DestroyWindow(self.hwnd) UnregisterClass(self.wc.lpszClassName, self.hinst) self.Stop() # is this needed? #@eg.LogIt def MyWndProc(self, dummyHwnd, mesg, dummyWParam, lParam): if mesg == WM_USER: self.timer.cancel() key = lParam & 0xFFFF #repeatCounter = (lParam >> 16) if key in KEY_MAP: eventString = KEY_MAP[key] else: eventString = "%X" % key if not self.lastEvent: self.lastEvent = self.plugin.TriggerEnduringEvent(eventString) self.timer = Timer(self.waitTime, self.OnTimeOut) self.timer.start() return 1 def OnTimeOut(self): if self.lastEvent: self.lastEvent.SetShouldEnd() self.lastEvent = None