Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 08d986eeb4afe02762643c1a77c0fc57bd42c2e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# *****************************************************************************
# * Copyright (c) 2011, 2012 Wind River Systems, Inc. and others.
# * All rights reserved. This program and the accompanying materials
# * are made available under the terms of the Eclipse Public License v1.0
# * which accompanies this distribution, and is available at
# * http://www.eclipse.org/legal/epl-v10.html
# *
# * Contributors:
# *     Wind River Systems - initial API and implementation
# *****************************************************************************

from tcf.services import terminals
from tcf import channel
from tcf.channel.Command import Command

class TerminalContext(terminals.TerminalContext):
    def __init__(self, service, props):
        super(TerminalContext, self).__init__(props)
        self.service = service

    def exit(self, done):
        service = self.service
        done = service._makeCallback(done)
        context_id = self.getID()
        class ExitCommand(Command):
            def __init__(self):
                super(ExitCommand, self).__init__(
                    service.channel, service, "exit", (context_id,))
            def done(self, error, args):
                if not error:
                    assert len(args) == 1
                    error = self.toError(args[0])
                done.doneCommand(self.token, error)
        return ExitCommand().token


class TerminalsProxy(terminals.TerminalsService):
    def __init__(self, channel):
        self.channel = channel
        self.listeners = {}

    def getContext(self, id, done):
        done = self._makeCallback(done)
        service = self
        class GetContextCommand(Command):
            def __init__(self):
                super(GetContextCommand, self).__init__(
                    service.channel, service, "getContext", (id,))
            def done(self, error, args):
                ctx = None
                if not error:
                    assert len(args) == 2
                    error = self.toError(args[0])
                    if args[1]: ctx = TerminalContext(service, args[1])
                done.doneGetContext(self.token, error, ctx)
        return GetContextCommand().token

    def launch(self, terminal_type, encoding, environment, done):
        done = self._makeCallback(done)
        service = self
        class LaunchCommand(Command):
            def __init__(self):
                super(LaunchCommand, self).__init__(service.channel, service, "launch", (terminal_type, encoding, environment))
            def done(self, error, args):
                ctx = None
                if not error:
                    assert len(args) == 2
                    error = self.toError(args[0])
                    if args[1]: ctx = TerminalContext(service, args[1])
                done.doneLaunch(self.token, error, ctx)
        return LaunchCommand().token

    def setWinSize(self, context_id, newWidth, newHeight, done):
        done = self._makeCallback(done)
        service = self
        class SetWinSizeCommand(Command):
            def __init__(self):
                super(SetWinSizeCommand, self).__init__(service.channel, service, "setWinSize", (context_id, newWidth, newHeight))
            def done(self, error, args):
                if not error:
                    assert len(args) == 1
                    error = self.toError(args[0])
                done.doneCommand(self.token, error)
        return SetWinSizeCommand().token

    def exit(self, context_id, done):
        done = self._makeCallback(done)
        service = self
        class ExitCommand(Command):
            def __init__(self):
                super(ExitCommand, self).__init__(service.channel, service, "exit", (context_id,))
            def done(self, error, args):
                if not error:
                    assert len(args) == 1
                    error = self.toError(args[0])
                done.doneCommand(self.token, error)
        return ExitCommand().token

    def addListener(self, listener):
        l = ChannelEventListener(self, listener)
        self.channel.addEventListener(self, l)
        self.listeners[listener] = l

    def removeListener(self, listener):
        l = self.listeners.pop(listener, None)
        if l: self.channel.removeEventListener(self, l)


class ChannelEventListener(channel.EventListener):
    def __init__(self, service, listener):
        self.service = service
        self.listener = listener
    def event(self, name, data):
        try:
            args = channel.fromJSONSequence(data)
            if name == "exited":
                assert len(args) == 2
                self.listener.exited(args[0], args[1])
            elif name == "winSizeChanged":
                assert len(args) == 3
                self.listener.winSizeChanged(args[0], args[1], args[2])
            else:
                raise IOError("Terminals service: unknown event: " + name);
        except Exception as x:
            self.service.channel.terminate(x)

Back to the top