OSDN Git Service

In global gesture maps, allow gestures to emulate key presses by specifying the targe...
authorJames Teh <jamie@jantrid.net>
Wed, 8 Dec 2010 06:40:58 +0000 (16:40 +1000)
committerJames Teh <jamie@jantrid.net>
Wed, 8 Dec 2010 06:40:58 +0000 (16:40 +1000)
This will be used to allow keys on braille displays to emulate key presses on the system keyboard.
This is done by generating a fake script.

source/inputCore.py
source/scriptHandler.py

index 0b9d6da..f2fc46b 100644 (file)
@@ -281,8 +281,11 @@ class InputManager(baseObject.AutoPropertyObject):
                runScript = False\r
                if script:\r
                        scriptName = scriptHandler.getScriptName(script)\r
-                       scriptLocation=scriptHandler.getScriptLocation(script)\r
-                       log.info("Input help: gesture %s, bound to script %s on %s"%(gesture.identifiers[0],scriptName,scriptLocation))\r
+                       scriptLocation = scriptHandler.getScriptLocation(script)\r
+                       logMsg = "Input help: gesture %s, bound to script %s" % (gesture.identifiers[0], scriptName)\r
+                       if scriptLocation:\r
+                               logMsg += " on %s" % scriptLocation\r
+                       log.info(logMsg)\r
                        if scriptName == "toggleInputHelp":\r
                                runScript = True\r
                        else:\r
index 3a83f92..07e213c 100644 (file)
@@ -21,6 +21,15 @@ _lastScriptRef=None #Holds a weakref to the last script that was executed
 _lastScriptCount=0 #The amount of times the last script was repeated\r
 _isScriptRunning=False\r
 \r
+def _makeKbEmulateScript(scriptName):\r
+       import keyboardHandler\r
+       keyName = scriptName[3:]\r
+       emuGesture = keyboardHandler.KeyboardInputGesture.fromName(keyName)\r
+       func = lambda gesture: inputCore.manager.emulateGesture(emuGesture)\r
+       func.__name__ = "script_%s" % scriptName\r
+       func.__doc__ = _("Emulates pressing %s on the system keyboard") % keyName\r
+       return func\r
+\r
 def _getObjScript(obj, gesture, globalMapScripts):\r
        # Search the scripts from the global gesture maps.\r
        for cls, scriptName in globalMapScripts:\r
@@ -28,10 +37,14 @@ def _getObjScript(obj, gesture, globalMapScripts):
                        if scriptName is None:\r
                                # The global map specified that no script should execute for this gesture and object.\r
                                return None\r
+                       if scriptName.startswith("kb:"):\r
+                               # Emulate a key press.\r
+                               return _makeKbEmulateScript(scriptName)\r
                        try:\r
                                return getattr(obj, "script_%s" % scriptName)\r
                        except AttributeError:\r
                                pass\r
+\r
        # Search the object itself for in-built bindings.\r
        return obj.getScript(gesture)\r
 \r
@@ -93,12 +106,16 @@ def getScriptName(script):
        return script.__name__[7:]\r
 \r
 def getScriptLocation(script):\r
+       try:\r
+               instance = script.__self__\r
+       except AttributeError:\r
+               # Not an instance method, so this must be a fake script.\r
+               return None\r
        name=script.__name__\r
-       for cls in script.__self__.__class__.__mro__:\r
+       for cls in instance.__class__.__mro__:\r
                if name in cls.__dict__:\r
                        return "%s.%s"%(cls.__module__,cls.__name__)\r
 \r
-\r
 def _queueScriptCallback(script,gesture):\r
        global _numScriptsQueued\r
        _numScriptsQueued-=1\r
@@ -121,13 +138,14 @@ def executeScript(script,gesture):
        global _lastScriptTime, _lastScriptCount, _lastScriptRef, _isScriptRunning \r
        lastScriptRef=_lastScriptRef() if _lastScriptRef else None\r
        #We don't allow the same script to be executed from with in itself, but we still should pass the key through\r
-       if _isScriptRunning and lastScriptRef==script.im_func:\r
+       scriptFunc=getattr(script,"__func__",script)\r
+       if _isScriptRunning and lastScriptRef==scriptFunc:\r
                return gesture.send()\r
        _isScriptRunning=True\r
        try:\r
                scriptTime=time.time()\r
-               scriptRef=weakref.ref(script.im_func)\r
-               if (scriptTime-_lastScriptTime)<=0.5 and script.im_func==lastScriptRef:\r
+               scriptRef=weakref.ref(scriptFunc)\r
+               if (scriptTime-_lastScriptTime)<=0.5 and scriptFunc==lastScriptRef:\r
                        _lastScriptCount+=1\r
                else:\r
                        _lastScriptCount=0\r