OSDN Git Service

hu: renamed chapter0.{t2t -> t2tinc}.
[nvdajp/nvdajp.git] / source / brailleInput.py
1 #brailleInput.py
2 #A part of NonVisual Desktop Access (NVDA)
3 #This file is covered by the GNU General Public License.
4 #See the file COPYING for more details.
5 #Copyright (C) 2012-2013 NV Access Limited, Rui Batista
6
7 import os.path
8 import louis
9 import braille
10 import config
11 from logHandler import log
12 import winUser
13 import inputCore
14
15 """Framework for handling braille input from the user.
16 All braille input is represented by a {BrailleInputGesture}.
17 Normally, all that is required is to create and execute a L{BrailleInputGesture},
18 as there are built-in gesture bindings for braille input.
19 """
20
21 #: The singleton BrailleInputHandler instance.
22 #: @type: L{BrailleInputHandler}
23 handler = None
24
25 def initialize():
26         global handler
27         handler = BrailleInputHandler()
28         log.info("Braille input initialized")
29
30 def terminate():
31         global handler
32         handler = None
33
34 class BrailleInputHandler(object):
35         """Handles braille input.
36         """
37
38         def input(self, dots):
39                 """Handle one cell of braille input.
40                 """
41                 # liblouis requires us to set the highest bit for proper use of dotsIO.
42                 char = unichr(dots | 0x8000)
43                 text = louis.backTranslate(
44                         [os.path.join(braille.TABLES_DIR, config.conf["braille"]["inputTable"]),
45                         "braille-patterns.cti"],
46                         char, mode=louis.dotsIO)
47                 chars = text[0]
48                 if len(chars) > 0:
49                         self.sendChars(chars)
50
51         def sendChars(self, chars):
52                 inputs = []
53                 for ch in chars:
54                         input = winUser.Input()
55                         input.type = winUser.INPUT_KEYBOARD
56                         input.ii.ki = winUser.KeyBdInput()
57                         input.ii.ki.wScan = ord(ch)
58                         input.ii.ki.dwFlags = winUser.KEYEVENTF_UNICODE
59                         inputs.append(input)
60                 winUser.SendInput(inputs)
61
62 class BrailleInputGesture(inputCore.InputGesture):
63         """Input (dots and/or space bar) from a braille keyboard.
64         This could either be as part of a braille display or a stand-alone unit.
65         L{dots} and L{space} should be set appropriately.
66         """
67
68         #: Bitmask of pressed dots.
69         #: @type: int
70         dots = 0
71
72         #: Whether the space bar is pressed.
73         #: @type: bool
74         space = False
75
76         def _get_identifiers(self):
77                 if self.space and self.dots:
78                         dotsString = "+".join("dot%d" % (i+1) for i in xrange(8) if self.dots & (1 << i))
79                         return ("bk:space+%s" % dotsString,
80                                 "bk:space+dots")
81                 elif self.dots or self.space:
82                         return ("bk:dots",)
83                 else:
84                         return ()
85
86         def _get_displayName(self):
87                 if not self.dots and not self.space:
88                         return None
89                 # Translators: Reported before braille input in input help mode.
90                 out = [_("braille")]
91                 if self.space and self.dots:
92                         # Translators: Reported when braille space is pressed with dots in input help mode.
93                         out.append(_("space with dot"))
94                 elif self.dots:
95                         # Translators: Reported when braille dots are pressed in input help mode.
96                         out.append(_("dot"))
97                 elif self.space:
98                         # Translators: Reported when braille space is pressed in input help mode.
99                         out.append(_("space"))
100                 if self.dots:
101                         for dot in xrange(8):
102                                 if self.dots & (1 << dot):
103                                         out.append(str(dot + 1))
104                 return " ".join(out)