| 1 |
#!/usr/bin/python |
|---|
| 2 |
# Copyright (c) 2001-2004 Twisted Matrix Laboratories. |
|---|
| 3 |
# See LICENSE for details. |
|---|
| 4 |
# |
|---|
| 5 |
# Modified by Ed Suominen 2006 for use with WinDictator unit tests. |
|---|
| 6 |
|
|---|
| 7 |
from twisted.internet import stdio, reactor |
|---|
| 8 |
from twisted.protocols import basic |
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
class CommandProcessorProtocol(basic.LineReceiver): |
|---|
| 12 |
""" |
|---|
| 13 |
A protocol for a mock I{typist} command processor |
|---|
| 14 |
""" |
|---|
| 15 |
delimiter = '\n' |
|---|
| 16 |
|
|---|
| 17 |
def connectionMade(self): |
|---|
| 18 |
self.responseDelay = 0.05 |
|---|
| 19 |
self.pressed = {} |
|---|
| 20 |
self.keycodes = { |
|---|
| 21 |
'a':38, |
|---|
| 22 |
'b':56, |
|---|
| 23 |
'c':54, |
|---|
| 24 |
'1':10, |
|---|
| 25 |
'2':11, |
|---|
| 26 |
'3':12, |
|---|
| 27 |
'slow':1, |
|---|
| 28 |
'Shift_L':50, |
|---|
| 29 |
'Return':36 |
|---|
| 30 |
} |
|---|
| 31 |
|
|---|
| 32 |
def complain(self, *args): |
|---|
| 33 |
if len(args) > 1: |
|---|
| 34 |
msg = args[0] % args[1:] |
|---|
| 35 |
else: |
|---|
| 36 |
msg = args[0] |
|---|
| 37 |
self.sendLine("ERROR: %s" % msg) |
|---|
| 38 |
raise Exception(msg) |
|---|
| 39 |
|
|---|
| 40 |
def lineReceived(self, line): |
|---|
| 41 |
""" |
|---|
| 42 |
Process the command and keysym by returning the keycode and complaining |
|---|
| 43 |
if more than two keys are pressed (not including shift) at a time. |
|---|
| 44 |
""" |
|---|
| 45 |
cmd, keySym = line.split() |
|---|
| 46 |
if cmd not in ('d', 'u', 'c'): |
|---|
| 47 |
self.complain("Improper command '%s'", cmd) |
|---|
| 48 |
else: |
|---|
| 49 |
keycode = self.keycodes[keySym] |
|---|
| 50 |
reactor.callLater( |
|---|
| 51 |
self.responseDelay, self.transport.write, "%d\n" % keycode) |
|---|
| 52 |
if cmd == 'd': |
|---|
| 53 |
self.pressed[keySym] = (keySym != "Shift_L") |
|---|
| 54 |
if sum(self.pressed.values()) > 1: |
|---|
| 55 |
self.complain( |
|---|
| 56 |
"Trying to press keys %s together at once!", |
|---|
| 57 |
', '.join(self.pressed.keys())) |
|---|
| 58 |
self.pressed.clear() |
|---|
| 59 |
elif cmd == 'u': |
|---|
| 60 |
if keySym not in self.pressed: |
|---|
| 61 |
self.complain( |
|---|
| 62 |
"Tried releasing a key that hadn't been pressed!") |
|---|
| 63 |
else: |
|---|
| 64 |
del self.pressed[keySym] |
|---|
| 65 |
|
|---|
| 66 |
def connectionLost(self, reason): |
|---|
| 67 |
""" |
|---|
| 68 |
I'm not needed anymore...sniff... |
|---|
| 69 |
""" |
|---|
| 70 |
reactor.stop() |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
if __name__ == '__main__': |
|---|
| 74 |
stdio.StandardIO(CommandProcessorProtocol()) |
|---|
| 75 |
reactor.run() |
|---|