| 1 |
# WinDictator: |
|---|
| 2 |
# Dictate in Windows, have the text typed in Linux via X faked keystroke events |
|---|
| 3 |
# |
|---|
| 4 |
# Copyright (C) 2005-2006 by Edwin A. Suominen, http://www.eepatents.com |
|---|
| 5 |
# |
|---|
| 6 |
# This program is free software; you can redistribute it and/or modify it under |
|---|
| 7 |
# the terms of the GNU General Public License as published by the Free Software |
|---|
| 8 |
# Foundation; either version 2 of the License, or (at your option) any later |
|---|
| 9 |
# version. |
|---|
| 10 |
# |
|---|
| 11 |
# This program is distributed in the hope that it will be useful, but WITHOUT |
|---|
| 12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|---|
| 13 |
# FOR A PARTICULAR PURPOSE. See the file COPYING for more details. |
|---|
| 14 |
# |
|---|
| 15 |
# You should have received a copy of the GNU General Public License along with |
|---|
| 16 |
# this program; if not, write to the Free Software Foundation, Inc., 51 |
|---|
| 17 |
# Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
|---|
| 18 |
|
|---|
| 19 |
""" |
|---|
| 20 |
Process Protocol |
|---|
| 21 |
""" |
|---|
| 22 |
|
|---|
| 23 |
from twisted.internet import defer, protocol |
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
class ProcessProtocol(protocol.ProcessProtocol): |
|---|
| 27 |
""" |
|---|
| 28 |
Protocol for the I{typist} and I{xnee} processes. |
|---|
| 29 |
""" |
|---|
| 30 |
def __init__(self, dStart, dStop, lineReceiver): |
|---|
| 31 |
""" |
|---|
| 32 |
Instantiates a new process protocol object with: |
|---|
| 33 |
|
|---|
| 34 |
dStart |
|---|
| 35 |
A C{Deferred} to fire when a connection has been made to the |
|---|
| 36 |
process via STDIN and STDOUT. |
|---|
| 37 |
|
|---|
| 38 |
dStop |
|---|
| 39 |
A C{Deferred} to fire when the process has ended. |
|---|
| 40 |
|
|---|
| 41 |
lineReceiver |
|---|
| 42 |
A callback function for supplying the outside world with lines |
|---|
| 43 |
received from the process via STDOUT. |
|---|
| 44 |
|
|---|
| 45 |
""" |
|---|
| 46 |
self.dStart = dStart |
|---|
| 47 |
self.dStop = dStop |
|---|
| 48 |
self.lineReceiver = lineReceiver |
|---|
| 49 |
|
|---|
| 50 |
def connectionMade(self): |
|---|
| 51 |
""" |
|---|
| 52 |
Called when the connection has been made to the process, with |
|---|
| 53 |
I{self.transport} set to the process transport used for the connection. |
|---|
| 54 |
|
|---|
| 55 |
Fires my C{Deferred} with the transport object, thus supplying the |
|---|
| 56 |
L{Keyer) instance with a reference to it and its C{loseConnection} |
|---|
| 57 |
method. |
|---|
| 58 |
""" |
|---|
| 59 |
self.received = [] |
|---|
| 60 |
self.dStart.callback(self.transport) |
|---|
| 61 |
|
|---|
| 62 |
def outReceived(self, data): |
|---|
| 63 |
""" |
|---|
| 64 |
Handles a chunk of data from the process via STDOUT, running the |
|---|
| 65 |
I{self.lineReceiver} callback with any lines in the total accumulated |
|---|
| 66 |
data. |
|---|
| 67 |
""" |
|---|
| 68 |
lines = data.strip().split('\n') |
|---|
| 69 |
self.received.append(lines.pop(0)) |
|---|
| 70 |
while self.received: |
|---|
| 71 |
result = ''.join(self.received) |
|---|
| 72 |
self.lineReceiver(result) |
|---|
| 73 |
if lines: |
|---|
| 74 |
self.received = [lines.pop(0)] |
|---|
| 75 |
else: |
|---|
| 76 |
self.received = [] |
|---|
| 77 |
|
|---|
| 78 |
def processEnded(self, reason): |
|---|
| 79 |
""" |
|---|
| 80 |
Called when the process has shut down for some reason. |
|---|
| 81 |
""" |
|---|
| 82 |
if not self.dStop.called: |
|---|
| 83 |
self.dStop.callback(reason) |
|---|