-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.go
194 lines (147 loc) · 4.01 KB
/
input.go
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package draftTerm
import (
"encoding/json"
"os"
"io"
"fmt"
)
type Byted interface {
Bytes() []byte
}
type inputMessage struct {
Type string `json:"type"`
}
func (self inputMessage) String() string {
return fmt.Sprintf("Generic message of type %s", self.Type)
}
func (self inputMessage) Bytes() [] byte {
return []byte(self.String())
}
//
// Message received when a key has been pressed
// Data are obtained from KeyBoardEvent
type keyPressedMessage struct {
inputMessage
Key string `json:"key"`
Code byte `json:"code"`
Shift bool `json:"shift"`
Ctrl bool `json:"ctrl"`
Alt bool `json:"alt"`
}
func (self keyPressedMessage) String() string {
vJson, _ := json.Marshal(self)
return string(vJson)
}
//
// Convert input keyboard data into data to send to the pty
//
func (self keyPressedMessage)Bytes() []byte {
if len(self.Key) > 1 {
switch self.Key {
case "Spacebar":
return []byte{' '}
case "Tab":
return []byte{'\t'}
case "Backspace":
return []byte{'\b'}
case "Escape":
return []byte{27}
case "Enter":
return []byte{10}
case "ArrowUp" , "Up":
return []byte{27, 91, 65}
case "ArrowDown" , "Down":
return []byte{27, 91, 66}
case "ArrowRight" , "Right":
return []byte{27, 91, 67}
case "ArrowLeft" , "Left":
return []byte{27, 91, 68}
}
} else {
if self.Ctrl {
if self.Code > 64 && self.Code < 91 {
//OUTPUT <- fmt.Sprintf("Pressed ctrl with %s %v ",self.Key,self.Code)
return []byte{self.Code-64}
}
}
vCode:= []byte(self.Key)
return vCode
}
/*
if self.Code <32 || self.Code >136 {
return []byte {}
}
if self.Shift ==false && self.Code >=65 && self.Code <=90 {
return []byte {self.Code+32}
}else {
return []byte {self.Code}
}
*/
return [] byte {}
}
//
// Message received when the client ask for terminal resize
//
type resizeTerminalMessage struct {
inputMessage
Cols int `json:"cols"`
Rows int `json:"rows"`
}
func (self resizeTerminalMessage) String() string {
return fmt.Sprintf("Resize terminal to %d cols %d rows",self.Cols,self.Rows)
}
type inputChannel struct {
input io.Reader
pty *os.File
}
//
//
//
func NewInputChannel(pInput io.Reader, pPty *os.File) *inputChannel {
return &inputChannel{ input:pInput, pty: pPty }
}
func (self *inputChannel) ProcessIncomingMessage(pSyncChannel chan bool) error {
vBytes := make([]byte, 8000)
vLen, vError:= self.input.Read(vBytes)
if vError !=nil {
return vError
}
if vLen ==1 {
pSyncChannel<-true
return nil
}
if vLen ==0 {
return nil
}
//fmt.Printf("Received %s \n",vBytes)
vTmp:=&inputMessage{}
vError=json.Unmarshal(vBytes[0:vLen],vTmp)
if vError !=nil {
return vError;
}
switch vTmp.Type {
case "key":
vKeyPressed:=&keyPressedMessage{}
vError=json.Unmarshal(vBytes[0:vLen],vKeyPressed)
if (vError!=nil) {
return vError
}
if IsDebug() {
logDebug("Processing keyboard message %v... \n",vKeyPressed)
}
_,vError=self.pty.Write(vKeyPressed.Bytes())
return vError
case "resize":
vResize:=&resizeTerminalMessage{}
vError=json.Unmarshal(vBytes[0:vLen],vResize)
if (vError!=nil) {
return vError
}
if IsDebug() {
logDebug("Processing resize message %v... \n",vResize)
}
vError=ResizeTerminal(self.pty, vResize.Cols,vResize.Rows)
return vError
}
return nil
}