Example #1
0
 def __start__(self):
     self.buf = collections.deque()
     self.ScanDevices()
     self.hookDll = CDLL(
         abspath(join(dirname(__file__), "RawInputHook.dll")))
     self.messageReceiver = eg.MessageReceiver("RawInputWindow")
     self.messageReceiver.AddHandler(WM_INPUT, self.OnRawInput)
     self.messageReceiver.AddHandler(WM_COPYDATA, self.OnCopyData)
     self.messageReceiver.Start()
     rid = (RAWINPUTDEVICE * 1)()
     rid[0].usUsagePage = 0x01
     rid[0].usUsage = 0x06
     rid[0].dwFlags = RIDEV_INPUTSINK
     rid[0].hwndTarget = self.messageReceiver.hwnd
     RegisterRawInputDevices(rid, 1, sizeof(rid[0]))
     self.hookDll.Start(self.messageReceiver.hwnd)
Example #2
0
 def __start__(self, *dummyArgs):
     self.names, self.hwnds = EnumProcesses()
     self.flashing = set()
     self.lastActivated = None
     eg.messageReceiver.AddHandler(WM_APP + 1, self.WindowGotFocusProc)
     eg.messageReceiver.AddHandler(WM_APP + 2, self.WindowCreatedProc)
     eg.messageReceiver.AddHandler(WM_APP + 3, self.WindowDestroyedProc)
     eg.messageReceiver.AddHandler(WM_SHELLHOOKMESSAGE, self.MyWndProc)
     RegisterShellHookWindow(eg.messageReceiver.hwnd)
     self.hookDll = CDLL(abspath(join(dirname(__file__), "TaskHook.dll")))
     self.hookDll.StartHook()
     trayWindow = 0
     if "explorer" in self.names:
         for hwnd in self.names["explorer"].hwnds:
             if GetClassName(hwnd) == "Shell_TrayWnd":
                 trayWindow = hwnd
                 break
     self.desktopHwnds = (GetShellWindow(), trayWindow)
Example #3
0
 def __start__(self):
     self.dll = None
     self.hOpen = None
     pluginDir = os.path.abspath(os.path.dirname(__file__))
     dll = CDLL(os.path.join(pluginDir, "TTUSBIR.dll"))
     self.cCallback = IRCALLBACKFUNC(self.IrCallback)
     self.hOpen = dll.irOpen(0, USBIR_MODE_DIV, self.cCallback, 0)
     if self.hOpen == -1:
         raise self.Exceptions.DeviceNotFound
     #        self.irGetUniqueCode = dll.ir_GetUniqueCode
     #        self.irGetUniqueCode.restype  = DWORD
     self.dll = dll
     self.data = []
     self.timer = eg.ResettableTimer(self.OnTimeout)
     self.dataLock = Lock()
     self.lastTime = clock()
     self.startByte = 1
     self.dll.irSetPowerLED(self.hOpen, 0)
     self.ledTimer = eg.ResettableTimer(partial(self.dll.irSetPowerLED, self.hOpen, 0))
Example #4
0
    def __start__(self):
        self.dll = None
        self.hOpen = None
        pluginDir = os.path.abspath(os.path.dirname(__file__))
        dll = CDLL(os.path.join(pluginDir, "TTUSBIR.dll"))
        self.cCallback = IRCALLBACKFUNC(self.IrCallback)
        self.hOpen = dll.irOpen(0, USBIR_MODE_DIV, self.cCallback, 0)
        if self.hOpen == -1:
            raise self.Exceptions.DeviceNotFound
#        self.irGetUniqueCode = dll.ir_GetUniqueCode
#        self.irGetUniqueCode.restype  = DWORD
        self.dll = dll
        self.data = []
        self.timer = eg.ResettableTimer(self.OnTimeout)
        self.dataLock = Lock()
        self.lastTime = clock()
        self.startByte = 1
        self.dll.irSetPowerLED(self.hOpen, 0)
        self.ledTimer = eg.ResettableTimer(
            partial(self.dll.irSetPowerLED, self.hOpen, 0))
Example #5
0
class RawInput(eg.PluginBase):
    def __start__(self):
        self.buf = collections.deque()
        self.ScanDevices()
        self.hookDll = CDLL(
            abspath(join(dirname(__file__), "RawInputHook.dll")))
        self.messageReceiver = eg.MessageReceiver("RawInputWindow")
        self.messageReceiver.AddHandler(WM_INPUT, self.OnRawInput)
        self.messageReceiver.AddHandler(WM_COPYDATA, self.OnCopyData)
        self.messageReceiver.Start()
        rid = (RAWINPUTDEVICE * 1)()
        rid[0].usUsagePage = 0x01
        rid[0].usUsage = 0x06
        rid[0].dwFlags = RIDEV_INPUTSINK
        rid[0].hwndTarget = self.messageReceiver.hwnd
        RegisterRawInputDevices(rid, 1, sizeof(rid[0]))
        self.hookDll.Start(self.messageReceiver.hwnd)

    def __stop__(self):
        self.hookDll.Stop()
        self.messageReceiver.Stop()

    def ScanDevices(self):
        nDevices = UINT(0)
        if -1 == GetRawInputDeviceList(None, byref(nDevices),
                                       sizeof(RAWINPUTDEVICELIST)):
            raise WinError()
        rawInputDeviceList = (RAWINPUTDEVICELIST * nDevices.value)()
        if -1 == GetRawInputDeviceList(
                cast(rawInputDeviceList, PRAWINPUTDEVICELIST), byref(nDevices),
                sizeof(RAWINPUTDEVICELIST)):
            raise WinError()

        cbSize = UINT()
        for i in range(nDevices.value):
            GetRawInputDeviceInfo(rawInputDeviceList[i].hDevice,
                                  RIDI_DEVICENAME, None, byref(cbSize))
            deviceName = create_unicode_buffer(cbSize.value)
            GetRawInputDeviceInfo(rawInputDeviceList[i].hDevice,
                                  RIDI_DEVICENAME, byref(deviceName),
                                  byref(cbSize))
            ridDeviceInfo = RID_DEVICE_INFO()
            cbSize.value = ridDeviceInfo.cbSize = sizeof(RID_DEVICE_INFO)
            GetRawInputDeviceInfo(rawInputDeviceList[i].hDevice,
                                  RIDI_DEVICEINFO, byref(ridDeviceInfo),
                                  byref(cbSize))
            if ridDeviceInfo.dwType != RIM_TYPEKEYBOARD:
                continue
            print "hDevice:", rawInputDeviceList[i].hDevice
            print "Type:", RIM_TYPES[rawInputDeviceList[i].dwType]
            print "DeviceName:", deviceName.value
            if ridDeviceInfo.dwType == RIM_TYPEHID:
                hid = ridDeviceInfo.hid
                print "dwVendorId: %04X" % hid.dwVendorId
                print "dwProductId: %04X" % hid.dwProductId
                print "dwVersionNumber: %04X" % hid.dwVersionNumber
                print "usUsagePage:", hid.usUsagePage
                print "usUsage:", hid.usUsage
            if ridDeviceInfo.dwType == RIM_TYPEKEYBOARD:
                kbd = ridDeviceInfo.keyboard
                print "dwType:", kbd.dwType
                print "dwSubType:", kbd.dwSubType
                print "dwKeyboardMode:", kbd.dwKeyboardMode
                print "dwNumberOfFunctionKeys:", kbd.dwNumberOfFunctionKeys
                print "dwNumberOfIndicators:", kbd.dwNumberOfIndicators
                print "dwNumberOfKeysTotal:", kbd.dwNumberOfKeysTotal
            if ridDeviceInfo.dwType == RIM_TYPEMOUSE:
                mouse = ridDeviceInfo.mouse
                print "dwId:", mouse.dwId
                print "dwNumberOfButtons:", mouse.dwNumberOfButtons
                print "dwSampleRate:", mouse.dwSampleRate
                print "fHasHorizontalWheel:", mouse.fHasHorizontalWheel
            print

    def OnRawInput(self, hwnd, mesg, wParam, lParam):
        pcbSize = UINT()
        GetRawInputData(lParam, RID_INPUT, None, byref(pcbSize),
                        sizeof(RAWINPUTHEADER))
        buf = create_string_buffer(pcbSize.value)
        GetRawInputData(lParam, RID_INPUT, buf, byref(pcbSize),
                        sizeof(RAWINPUTHEADER))
        pRawInput = cast(buf, POINTER(RAWINPUT))
        keyboard = pRawInput.contents.data.keyboard
        if keyboard.VKey == 0xFF:
            eg.eventThread.Call(eg.Print, "0xFF")
            return 0
        #print "Scan code:", keyboard.MakeCode
        info = ""
        mTime = time.clock()
        if keyboard.Message == WM_KEYDOWN:
            transition = "KEYDOWN"
        elif keyboard.Message == WM_KEYUP:
            transition = "KEYUP"
        else:
            transition = " %d" % keyboard.Message
        info = "%f " % mTime
        info += "RawI %s: %s(%d), " % (transition, VK_KEYS[keyboard.VKey],
                                       keyboard.VKey)
        if GetAsyncKeyState(162):  #LCtrl
            info += "LCtrl "
        if GetAsyncKeyState(163):  #RCtrl
            info += "RCtrl "
        info += "Scan: %d, " % keyboard.MakeCode
        info += "Extra: %d, " % keyboard.ExtraInformation
        info += "Device: %r, " % pRawInput.contents.header.hDevice
        #print "Flags:", keyboard.Flags
        rawKeyboardData = RawKeyboardData(
            keyboard.VKey, pRawInput.contents.header.hDevice, keyboard.Message
            in (WM_KEYDOWN, WM_SYSKEYDOWN), time.clock())
        self.buf.append(rawKeyboardData)
        eg.eventThread.Call(eg.Print, info)
        if GET_RAWINPUT_CODE_WPARAM(wParam) == RIM_INPUT:
            return DefWindowProc(hwnd, mesg, wParam, lParam)
        return 0

    def OnCopyData(self, hwnd, mesg, wParam, lParam):
        copyData = cast(lParam, PCOPYDATASTRUCT)
        hEvent = cast(copyData.contents.lpData, POINTER(HEVENT))
        if not (copyData.contents.dwData == 0
                and copyData.contents.cbData == sizeof(HEVENT)
                and hEvent.contents.dwHookType == WH_KEYBOARD):
            eg.eventThread.Call(eg.Print, "return")
            return
        mTime = time.clock()
        msg = MSG()
        while PeekMessage(byref(msg), 0, WM_INPUT, WM_INPUT, PM_REMOVE):
            self.OnRawInput(0, msg.message, msg.wParam, msg.lParam)
        vKey = hEvent.contents.wParam
        repeatCount = hEvent.contents.lParam & 0xFFFF
        keyState = (hEvent.contents.lParam >> 30) & 0x01
        extended = (hEvent.contents.lParam >> 24) & 0x01
        if (hEvent.contents.lParam >> 31) & 0x01:
            transition = "KEYUP"
            state = False
        else:
            transition = "KEYDOWN"
            state = True
        info = "%f Hook %s: %s(%d), keyState=%d, extended=%d" % (
            mTime,
            transition,
            VK_KEYS[vKey],
            hEvent.contents.wParam,
            keyState,
            extended,
        )
        if GetAsyncKeyState(162):  #LCtrl
            info += "LCtrl "
        if GetAsyncKeyState(163):  #RCtrl
            info += "RCtrl "
        i = 0
        while i < len(self.buf):
            rawKeyboardData = self.buf[i]
            if rawKeyboardData.tick < time.clock() - 0.2:
                eg.eventThread.Call(eg.Print, "*** removed to old message")
                del self.buf[i]
                continue
            if (rawKeyboardData.vKey == vKey
                    and rawKeyboardData.state == state):
                del self.buf[i]
                #                if rawKeyboardData.device != 65603:
                #                    eg.eventThread.Call(eg.Print, "blocked")
                #                    return 1
                eg.eventThread.Call(eg.Print, info)
                return 0
            i += 1
        eg.eventThread.Call(eg.Print, "not found")
class TaskMonitorPlus(eg.PluginBase):
    def __init__(self):
        self.AddEvents()

    def __start__(self, *dummyArgs):
        self.pids, self.hwnds = EnumProcesses()
        self.flashing = set()
        self.lastActivated = None
        eg.messageReceiver.AddHandler(WM_APP + 1, self.WindowGotFocusProc)
        eg.messageReceiver.AddHandler(WM_APP + 2, self.WindowCreatedProc)
        eg.messageReceiver.AddHandler(WM_APP + 3, self.WindowDestroyedProc)
        eg.messageReceiver.AddHandler(WM_SHELLHOOKMESSAGE, self.MyWndProc)
        RegisterShellHookWindow(eg.messageReceiver.hwnd)
        self.hookDll = CDLL(abspath(join(dirname(__file__), "TaskHook.dll")))
        #self.hookDll = CDLL(abspath(join(eg.corePluginDir, "Task", "TaskHook.dll")))
        self.hookDll.StartHook()
        trayWindow = 0
        for explorerPid in [
                x for x in self.pids if self.pids[x].name == "explorer"
        ]:
            for hwnd in self.pids[explorerPid].hwnds:
                if GetClassName(hwnd) == "Shell_TrayWnd":
                    trayWindow = hwnd
                    break
            if trayWindow != 0:
                break
        self.desktopHwnds = (GetShellWindow(), trayWindow)

    def __stop__(self):
        self.hookDll.StopHook()
        FreeLibrary(self.hookDll._handle)
        DeregisterShellHookWindow(eg.messageReceiver.hwnd)
        eg.messageReceiver.RemoveHandler(WM_SHELLHOOKMESSAGE, self.MyWndProc)
        eg.messageReceiver.RemoveHandler(WM_APP + 1, self.WindowGotFocusProc)
        eg.messageReceiver.RemoveHandler(WM_APP + 2, self.WindowCreatedProc)
        eg.messageReceiver.RemoveHandler(WM_APP + 3, self.WindowDestroyedProc)

    def CheckWindow(self, hwnd):
        hwnd2 = GetAncestor(hwnd, GA_ROOT)
        if hwnd == 0 or hwnd2 in self.desktopHwnds:
            return  #"Desktop", 0, None
        if hwnd != hwnd2:
            return
        if GetWindowLong(hwnd, GWL_HWNDPARENT):
            return
        if not IsWindowVisible(hwnd):
            return

        if hwnd in self.hwnds:
            processInfo = self.pids.get(self.hwnds[hwnd].pid, None)
            return processInfo

        pid = GetWindowPid(hwnd)
        processInfo = self.pids.get(pid, None)
        if not processInfo:
            processInfo = ProcessInfo(pid)
            self.pids[pid] = processInfo
            self.TriggerEvent("Created." + processInfo.name)

        processInfo.hwnds[hwnd] = WindowInfo(hwnd)
        self.hwnds[hwnd] = processInfo
        self.TriggerEvent("NewWindow." + processInfo.name,
                          processInfo.hwnds[hwnd])
        return processInfo

    def MyWndProc(self, dummyHwnd, dummyMesg, wParam, lParam):
        if wParam == HSHELL_WINDOWDESTROYED:
            self.WindowDestroyedProc(None, None, lParam, None)
        elif wParam in (HSHELL_WINDOWACTIVATED, HSHELL_WINDOWCREATED, 0x8004):
            self.WindowGotFocusProc(None, None, lParam, None)
        elif wParam == 0x8006:
            self.WindowFlashedProc(None, None, lParam, None)
        return 1

    def WindowCreatedProc(self, dummyHwnd, dummyMesg, hwnd, dummyLParam):
        self.CheckWindow(hwnd)

    def WindowDestroyedProc(self, dummyHwnd, dummyMesg, hwnd, dummyLParam):
        #hwnd2 = GetAncestor(hwnd, GA_ROOT)
        processInfo = self.hwnds.get(hwnd, None)
        if processInfo:
            winDetails = processInfo.hwnds[hwnd]
            del processInfo.hwnds[hwnd]
            del self.hwnds[hwnd]
            pid = processInfo.pid
            if hwnd == self.lastActivated:
                self.TriggerEvent("Deactivated." + processInfo.name,
                                  winDetails)
                self.lastActivated = None
            self.TriggerEvent("ClosedWindow." + processInfo.name, winDetails)
            if len(processInfo.hwnds) == 0:
                self.TriggerEvent("Destroyed." + processInfo.name)
                self.pids.pop(pid, None)

    def WindowFlashedProc(self, dummyHwnd, dummyMesg, hwnd, dummyLParam):
        processInfo = self.hwnds.get(hwnd, None)
        if processInfo and hwnd not in self.flashing:
            self.flashing.add(hwnd)
            self.TriggerEvent("Flashed." + processInfo.name,
                              processInfo.hwnds[hwnd])

    def WindowGotFocusProc(self, dummyHwnd, dummyMesg, hwnd, dummyLParam):
        thisProcessInfo = self.CheckWindow(hwnd)
        if thisProcessInfo and hwnd != self.lastActivated:
            if hwnd in self.flashing:
                self.flashing.remove(hwnd)
            if self.lastActivated:
                lastProcessInfo = self.hwnds.get(self.lastActivated, None)
                payload = None
                if lastProcessInfo:
                    payload = lastProcessInfo.hwnds[self.lastActivated]
                    self.TriggerEvent("Deactivated." + lastProcessInfo.name,
                                      payload)
            self.TriggerEvent("Activated." + thisProcessInfo.name,
                              thisProcessInfo.hwnds[hwnd])
            self.lastActivated = hwnd
Example #7
0
class Task(eg.PluginBase):
    def __init__(self):
        self.AddEvents()

    def __start__(self, *dummyArgs):
        self.names, self.hwnds = EnumProcesses()
        self.flashing = set()
        self.lastActivated = None
        eg.messageReceiver.AddHandler(WM_APP + 1, self.WindowGotFocusProc)
        eg.messageReceiver.AddHandler(WM_APP + 2, self.WindowCreatedProc)
        eg.messageReceiver.AddHandler(WM_APP + 3, self.WindowDestroyedProc)
        eg.messageReceiver.AddHandler(WM_SHELLHOOKMESSAGE, self.MyWndProc)
        RegisterShellHookWindow(eg.messageReceiver.hwnd)
        self.hookDll = CDLL(abspath(join(dirname(__file__), "TaskHook.dll")))
        self.hookDll.StartHook()
        trayWindow = 0
        if "explorer" in self.names:
            for hwnd in self.names["explorer"].hwnds:
                if GetClassName(hwnd) == "Shell_TrayWnd":
                    trayWindow = hwnd
                    break
        self.desktopHwnds = (GetShellWindow(), trayWindow)

    def __stop__(self):
        self.hookDll.StopHook()
        FreeLibrary(self.hookDll._handle)
        DeregisterShellHookWindow(eg.messageReceiver.hwnd)
        eg.messageReceiver.RemoveHandler(WM_SHELLHOOKMESSAGE, self.MyWndProc)
        eg.messageReceiver.RemoveHandler(WM_APP + 1, self.WindowGotFocusProc)
        eg.messageReceiver.RemoveHandler(WM_APP + 2, self.WindowCreatedProc)
        eg.messageReceiver.RemoveHandler(WM_APP + 3, self.WindowDestroyedProc)

    def CheckWindow(self, hwnd):
        hwnd2 = GetAncestor(hwnd, GA_ROOT)
        if hwnd == 0 or hwnd2 in self.desktopHwnds:
            return "Desktop", 0
        if hwnd != hwnd2:
            return
        if GetWindowLong(hwnd, GWL_HWNDPARENT):
            return
        if not IsWindowVisible(hwnd):
            return

        if hwnd in self.hwnds:
            return self.hwnds[hwnd].name, hwnd

        pid = GetWindowPid(hwnd)
        name = splitext(GetProcessName(pid))[0]
        processInfo = self.names.get(name, None)
        if not processInfo:
            processInfo = ProcessInfo(name)
            self.names[name] = processInfo
            self.TriggerEvent("Created." + name)

        processInfo.hwnds.add(hwnd)
        self.hwnds[hwnd] = processInfo
        self.TriggerEvent("NewWindow." + name)
        return name, hwnd

    def MyWndProc(self, dummyHwnd, dummyMesg, wParam, lParam):
        if wParam == HSHELL_WINDOWDESTROYED:
            self.WindowDestroyedProc(None, None, lParam, None)
        elif wParam in (HSHELL_WINDOWACTIVATED, HSHELL_WINDOWCREATED, 0x8004):
            self.WindowGotFocusProc(None, None, lParam, None)
        elif wParam == 0x8006:
            self.WindowFlashedProc(None, None, lParam, None)
        return 1

    def WindowCreatedProc(self, dummyHwnd, dummyMesg, hwnd, dummyLParam):
        self.CheckWindow(hwnd)

    def WindowDestroyedProc(self, dummyHwnd, dummyMesg, hwnd, dummyLParam):
        #hwnd2 = GetAncestor(hwnd, GA_ROOT)
        processInfo = self.hwnds.get(hwnd, None)
        if processInfo:
            processInfo.hwnds.remove(hwnd)
            del self.hwnds[hwnd]
            name = processInfo.name
            if (name, hwnd) == self.lastActivated:
                self.TriggerEvent("Deactivated." + name)
                self.lastActivated = None
            self.TriggerEvent("ClosedWindow." + name)
            if len(processInfo.hwnds) == 0:
                self.TriggerEvent("Destroyed." + name)
                self.names.pop(name, None)

    def WindowFlashedProc(self, dummyHwnd, dummyMesg, hwnd, dummyLParam):
        processInfo = self.hwnds.get(hwnd, None)
        if processInfo and hwnd not in self.flashing:
            self.flashing.add(hwnd)
            self.TriggerEvent("Flashed." + processInfo.name)

    def WindowGotFocusProc(self, dummyHwnd, dummyMesg, hwnd, dummyLParam):
        ident = self.CheckWindow(hwnd)
        if ident and ident != self.lastActivated:
            if hwnd in self.flashing:
                self.flashing.remove(hwnd)
            if self.lastActivated:
                self.TriggerEvent("Deactivated." + self.lastActivated[0])
            self.TriggerEvent("Activated." + ident[0])
            self.lastActivated = ident