Пример #1
0
    def createNewMonitoredDevice(self,device_class_name,deviceConfig):
        #print2err("#### createNewMonitoredDevice: ",device_class_name)
        self._all_device_config_errors=dict()

        try:
            device_instance=None
            device_config=None
            device_event_ids=None
            event_classes=None
            
            device_instance_and_config=self.addDeviceToMonitor(device_class_name,deviceConfig)
            if device_instance_and_config:
                device_instance,device_config,device_event_ids,event_classes=device_instance_and_config 
                DeviceConstants.addClassMapping(device_instance.__class__)
                EventConstants.addClassMappings(device_instance.__class__,device_event_ids,event_classes)
            else:
                print2err('## Device was not started by the ioHub Server: ',device_class_name)
                raise ioHubError("Device config validation failed")
                
        except:
            print2err("Error during device creation ....")
            printExceptionDetailsToStdErr()
            raise ioHubError("Error during device creation ....")


        # Update DataStore Structure if required.
        if psychopy.iohub._DATA_STORE_AVAILABLE:        
            try:            
                if self.emrt_file is not None:
                    self.emrt_file.updateDataStoreStructure(device_instance,event_classes)
            except:
                print2err("Error while updating datastore for device addition:",device_instance,device_event_ids)
                printExceptionDetailsToStdErr()


        self.log("Adding ioServer and DataStore event listeners......")

        # add event listeners for saving events
        if psychopy.iohub._DATA_STORE_AVAILABLE and self.emrt_file is not None:
            if device_config['save_events']:
                device_instance._addEventListener(self.emrt_file,device_event_ids)
                self.log("DataStore listener for device added: device: %s eventIDs: %s"%(device_instance.__class__.__name__,device_event_ids))
                #print2err("DataStore listener for device added: device: %s eventIDs: %s"%(device_instance.__class__.__name__,device_event_ids))
            else:
                #print2err("DataStore saving disabled for device: %s"%(device_instance.__class__.__name__,))
                self.log("DataStore saving disabled for device: %s"%(device_instance.__class__.__name__,))
        else:
            #print2err("DataStore Not Evabled. No events will be saved.")
            self.log("DataStore Not Enabled. No events will be saved.")
    

        # Add Device Monitor for Keyboard or Mouse device type 
        deviceDict=ioServer.deviceDict
        iohub=self
        if device_class_name in ('Mouse','Keyboard'):
            if Computer.system == 'win32':  
                if self._hookDevice is None:
                    iohub.log("Creating pyHook Monitors....")
                    #print2err("Creating pyHook Monitor....")
    
                    class pyHookDevice(object):
                        def __init__(self):
                            import pyHook
                            self._hookManager=pyHook.HookManager()
                            
                            self._mouseHooked=False
                            self._keyboardHooked=False
                            
                            if device_class_name == 'Mouse':
                                #print2err("Hooking Mouse.....")
                                self._hookManager.MouseAll = deviceDict['Mouse']._nativeEventCallback
                                self._hookManager.HookMouse()
                                self._mouseHooked=True
                            elif device_class_name == 'Keyboard':
                                #print2err("Hooking Keyboard.....")
                                self._hookManager.KeyAll = deviceDict['Keyboard']._nativeEventCallback
                                self._hookManager.HookKeyboard()
                                self._keyboardHooked=True
    
                            #iohub.log("WindowsHook PumpEvents Periodic Timer Created.")
                
                        def _poll(self):
                            import pythoncom
                            # PumpWaitingMessages returns 1 if a WM_QUIT message was received, else 0
                            if pythoncom.PumpWaitingMessages() == 1:
                                raise KeyboardInterrupt()               
        
                    #print2err("Creating pyHook Monitor......")
                    self._hookDevice=pyHookDevice()
                    hookMonitor=DeviceMonitor(self._hookDevice,0.00375)
                    self.deviceMonitors.append(hookMonitor)
                
                    #print2err("Created pyHook Monitor.")
                else:
                    #print2err("UPDATING pyHook Monitor....")
                    if device_class_name == 'Mouse' and self._hookDevice._mouseHooked is False:
                        #print2err("Hooking Mouse.....")
                        self._hookDevice._hookManager.MouseAll = deviceDict['Mouse']._nativeEventCallback
                        self._hookDevice._hookManager.HookMouse()
                        self._hookDevice._mouseHooked=True
                    elif device_class_name == 'Keyboard' and self._hookDevice._keyboardHooked is False:
                        #print2err("Hooking Keyboard.....")
                        self._hookDevice._hookManager.KeyAll = deviceDict['Keyboard']._nativeEventCallback
                        self._hookDevice._hookManager.HookKeyboard()
                        self._hookDevice._keyboardHooked=True
                
                    #print2err("Finished Updating pyHook Monitor.")
                
            elif Computer.system == 'linux2':
                # TODO: consider switching to xlib-ctypes implementation of xlib
                # https://github.com/garrybodsworth/pyxlib-ctypes
                from .devices import pyXHook
                if self._hookManager is None:
                    #iohub.log("Creating pyXHook Monitors....")
                    self._hookManager = pyXHook.HookManager()
                    self._hookManager._mouseHooked=False
                    self._hookManager._keyboardHooked=False

                    if device_class_name == 'Keyboard':
                        #print2err("Hooking Keyboard.....")
                        self._hookManager.HookKeyboard()
                        self._hookManager.KeyDown = deviceDict['Keyboard']._nativeEventCallback
                        self._hookManager.KeyUp = deviceDict['Keyboard']._nativeEventCallback
                        self._hookManager._keyboardHooked=True
                    elif device_class_name == 'Mouse':                
                        #print2err("Hooking Mouse.....")
                        self._hookManager.HookMouse()
                        self._hookManager.MouseAllButtonsDown = deviceDict['Mouse']._nativeEventCallback
                        self._hookManager.MouseAllButtonsUp = deviceDict['Mouse']._nativeEventCallback
                        self._hookManager.MouseAllMotion = deviceDict['Mouse']._nativeEventCallback
                        self._hookManager._mouseHooked=True
    
                    #print2err("Starting pyXHook.HookManager.....")
                    self._hookManager.start()
                    #iohub.log("pyXHook Thread Created.")
                    #print2err("pyXHook.HookManager thread created.")
                else:
                    #iohub.log("Updating pyXHook Monitor....")
                    if device_class_name == 'Keyboard' and self._hookManager._keyboardHooked is False:
                        #print2err("Hooking Keyboard.....")
                        self._hookManager.HookKeyboard()
                        self._hookManager.KeyDown = deviceDict['Keyboard']._nativeEventCallback
                        self._hookManager.KeyUp = deviceDict['Keyboard']._nativeEventCallback
                        self._hookManager._keyboardHooked=True
                    if device_class_name == 'Mouse' and self._hookManager._mouseHooked is False:                
                        #print2err("Hooking Mouse.....")
                        self._hookManager.HookMouse()
                        self._hookManager.MouseAllButtonsDown = deviceDict['Mouse']._nativeEventCallback
                        self._hookManager.MouseAllButtonsUp = deviceDict['Mouse']._nativeEventCallback
                        self._hookManager.MouseAllMotion = deviceDict['Mouse']._nativeEventCallback
                        self._hookManager._mouseHooked=True
                    #iohub.log("Finished Updating pyXHook Monitor....")
                    

            else: # OSX
                if self._hookDevice is None:
                    self._hookDevice=[]
                    
                if  device_class_name == 'Mouse' and 'Mouse' not in self._hookDevice:
                    #print2err("Hooking OSX Mouse.....")
                    mouseHookMonitor=DeviceMonitor(deviceDict['Mouse'],0.004)
                    self.deviceMonitors.append(mouseHookMonitor)
                    deviceDict['Mouse']._CGEventTapEnable(deviceDict['Mouse']._tap, True)
                    self._hookDevice.append('Mouse')
                    #print2err("Done Hooking OSX Mouse.....")
                if device_class_name == 'Keyboard'  and 'Keyboard' not in self._hookDevice:
                    #print2err("Hooking OSX Keyboard.....")
                    kbHookMonitor=DeviceMonitor(deviceDict['Keyboard'],0.004)
                    self.deviceMonitors.append(kbHookMonitor)
                    deviceDict['Keyboard']._CGEventTapEnable(deviceDict['Keyboard']._tap, True)
                    self._hookDevice.append('Keyboard')
                    #print2err("DONE Hooking OSX Keyboard.....")


            return [device_class_name, device_config['name'], device_instance._getRPCInterface()]
Пример #2
0
    def createNewMonitoredDevice(self, device_class_name, deviceConfig):
        #print2err("#### createNewMonitoredDevice: ",device_class_name)
        self._all_device_config_errors = dict()

        try:
            device_instance = None
            device_config = None
            device_event_ids = None
            event_classes = None

            device_instance_and_config = self.addDeviceToMonitor(
                device_class_name, deviceConfig)
            if device_instance_and_config:
                device_instance, device_config, device_event_ids, event_classes = device_instance_and_config
                DeviceConstants.addClassMapping(device_instance.__class__)
                EventConstants.addClassMappings(device_instance.__class__,
                                                device_event_ids,
                                                event_classes)
            else:
                print2err('## Device was not started by the ioHub Server: ',
                          device_class_name)
                raise ioHubError("Device config validation failed")

        except:
            print2err("Error during device creation ....")
            printExceptionDetailsToStdErr()
            raise ioHubError("Error during device creation ....")

        # Update DataStore Structure if required.
        if psychopy.iohub._DATA_STORE_AVAILABLE:
            try:
                if self.emrt_file is not None:
                    self.emrt_file.updateDataStoreStructure(
                        device_instance, event_classes)
            except:
                print2err(
                    "Error while updating datastore for device addition:",
                    device_instance, device_event_ids)
                printExceptionDetailsToStdErr()

        self.log("Adding ioServer and DataStore event listeners......")

        # add event listeners for saving events
        if psychopy.iohub._DATA_STORE_AVAILABLE and self.emrt_file is not None:
            if device_config['save_events']:
                device_instance._addEventListener(self.emrt_file,
                                                  device_event_ids)
                self.log(
                    "DataStore listener for device added: device: %s eventIDs: %s"
                    % (device_instance.__class__.__name__, device_event_ids))
                #print2err("DataStore listener for device added: device: %s eventIDs: %s"%(device_instance.__class__.__name__,device_event_ids))
            else:
                #print2err("DataStore saving disabled for device: %s"%(device_instance.__class__.__name__,))
                self.log("DataStore saving disabled for device: %s" %
                         (device_instance.__class__.__name__, ))
        else:
            #print2err("DataStore Not Evabled. No events will be saved.")
            self.log("DataStore Not Enabled. No events will be saved.")

        # Add Device Monitor for Keyboard or Mouse device type
        deviceDict = ioServer.deviceDict
        iohub = self
        if device_class_name in ('Mouse', 'Keyboard'):
            if Computer.system == 'win32':
                if self._hookDevice is None:
                    iohub.log("Creating pyHook Monitors....")

                    #print2err("Creating pyHook Monitor....")

                    class pyHookDevice(object):
                        def __init__(self):
                            import pyHook
                            self._hookManager = pyHook.HookManager()

                            self._mouseHooked = False
                            self._keyboardHooked = False

                            if device_class_name == 'Mouse':
                                #print2err("Hooking Mouse.....")
                                self._hookManager.MouseAll = deviceDict[
                                    'Mouse']._nativeEventCallback
                                self._hookManager.HookMouse()
                                self._mouseHooked = True
                            elif device_class_name == 'Keyboard':
                                #print2err("Hooking Keyboard.....")
                                self._hookManager.KeyAll = deviceDict[
                                    'Keyboard']._nativeEventCallback
                                self._hookManager.HookKeyboard()
                                self._keyboardHooked = True

                            #iohub.log("WindowsHook PumpEvents Periodic Timer Created.")

                        def _poll(self):
                            import pythoncom
                            # PumpWaitingMessages returns 1 if a WM_QUIT message was received, else 0
                            if pythoncom.PumpWaitingMessages() == 1:
                                raise KeyboardInterrupt()

                    #print2err("Creating pyHook Monitor......")
                    self._hookDevice = pyHookDevice()
                    hookMonitor = DeviceMonitor(
                        self._hookDevice,
                        self.config.get('windows_msgpump_interval', 0.00375))
                    self.deviceMonitors.append(hookMonitor)

                    #print2err("Created pyHook Monitor.")
                else:
                    #print2err("UPDATING pyHook Monitor....")
                    if device_class_name == 'Mouse' and self._hookDevice._mouseHooked is False:
                        #print2err("Hooking Mouse.....")
                        self._hookDevice._hookManager.MouseAll = deviceDict[
                            'Mouse']._nativeEventCallback
                        self._hookDevice._hookManager.HookMouse()
                        self._hookDevice._mouseHooked = True
                    elif device_class_name == 'Keyboard' and self._hookDevice._keyboardHooked is False:
                        #print2err("Hooking Keyboard.....")
                        self._hookDevice._hookManager.KeyAll = deviceDict[
                            'Keyboard']._nativeEventCallback
                        self._hookDevice._hookManager.HookKeyboard()
                        self._hookDevice._keyboardHooked = True

                    #print2err("Finished Updating pyHook Monitor.")

            elif Computer.system == 'linux2':
                # TODO: consider switching to xlib-ctypes implementation of xlib
                # https://github.com/garrybodsworth/pyxlib-ctypes
                from .devices import pyXHook
                if self._hookManager is None:
                    #iohub.log("Creating pyXHook Monitors....")
                    self._hookManager = pyXHook.HookManager()
                    self._hookManager._mouseHooked = False
                    self._hookManager._keyboardHooked = False

                    if device_class_name == 'Keyboard':
                        #print2err("Hooking Keyboard.....")
                        self._hookManager.HookKeyboard()
                        self._hookManager.KeyDown = deviceDict[
                            'Keyboard']._nativeEventCallback
                        self._hookManager.KeyUp = deviceDict[
                            'Keyboard']._nativeEventCallback
                        self._hookManager._keyboardHooked = True
                    elif device_class_name == 'Mouse':
                        #print2err("Hooking Mouse.....")
                        self._hookManager.HookMouse()
                        self._hookManager.MouseAllButtonsDown = deviceDict[
                            'Mouse']._nativeEventCallback
                        self._hookManager.MouseAllButtonsUp = deviceDict[
                            'Mouse']._nativeEventCallback
                        self._hookManager.MouseAllMotion = deviceDict[
                            'Mouse']._nativeEventCallback
                        self._hookManager._mouseHooked = True

                    #print2err("Starting pyXHook.HookManager.....")
                    self._hookManager.start()
                    #iohub.log("pyXHook Thread Created.")
                    #print2err("pyXHook.HookManager thread created.")
                else:
                    #iohub.log("Updating pyXHook Monitor....")
                    if device_class_name == 'Keyboard' and self._hookManager._keyboardHooked is False:
                        #print2err("Hooking Keyboard.....")
                        self._hookManager.HookKeyboard()
                        self._hookManager.KeyDown = deviceDict[
                            'Keyboard']._nativeEventCallback
                        self._hookManager.KeyUp = deviceDict[
                            'Keyboard']._nativeEventCallback
                        self._hookManager._keyboardHooked = True
                    if device_class_name == 'Mouse' and self._hookManager._mouseHooked is False:
                        #print2err("Hooking Mouse.....")
                        self._hookManager.HookMouse()
                        self._hookManager.MouseAllButtonsDown = deviceDict[
                            'Mouse']._nativeEventCallback
                        self._hookManager.MouseAllButtonsUp = deviceDict[
                            'Mouse']._nativeEventCallback
                        self._hookManager.MouseAllMotion = deviceDict[
                            'Mouse']._nativeEventCallback
                        self._hookManager._mouseHooked = True
                    #iohub.log("Finished Updating pyXHook Monitor....")

            else:  # OSX
                if self._hookDevice is None:
                    self._hookDevice = []

                if device_class_name == 'Mouse' and 'Mouse' not in self._hookDevice:
                    #print2err("Hooking OSX Mouse.....")
                    mouseHookMonitor = DeviceMonitor(deviceDict['Mouse'],
                                                     0.004)
                    self.deviceMonitors.append(mouseHookMonitor)
                    deviceDict['Mouse']._CGEventTapEnable(
                        deviceDict['Mouse']._tap, True)
                    self._hookDevice.append('Mouse')
                    #print2err("Done Hooking OSX Mouse.....")
                if device_class_name == 'Keyboard' and 'Keyboard' not in self._hookDevice:
                    #print2err("Hooking OSX Keyboard.....")
                    kbHookMonitor = DeviceMonitor(deviceDict['Keyboard'],
                                                  0.004)
                    self.deviceMonitors.append(kbHookMonitor)
                    deviceDict['Keyboard']._CGEventTapEnable(
                        deviceDict['Keyboard']._tap, True)
                    self._hookDevice.append('Keyboard')
                    #print2err("DONE Hooking OSX Keyboard.....")

            return [
                device_class_name, device_config['name'],
                device_instance._getRPCInterface()
            ]