This repository has been archived by the owner on Apr 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 94
/
sercfg.c
239 lines (224 loc) · 7.03 KB
/
sercfg.c
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* sercfg.c - the serial-port specific parts of the PuTTY
* configuration box. Centralised as cross-platform code because
* more than one platform will want to use it, but not part of the
* main configuration. The expectation is that each platform's
* local config function will call out to ser_setup_config_box() if
* it needs to set up the standard serial stuff. (Of course, it can
* then apply local tweaks after ser_setup_config_box() returns, if
* it needs to.)
*/
#include <assert.h>
#include <stdlib.h>
#include "putty.h"
#include "dialog.h"
#include "storage.h"
static void serial_parity_handler(union control *ctrl,
void *dlg,
void *data,
int event)
{
static const struct {
const char *name;
int val;
} parities[] = {
{"None", SER_PAR_NONE},
{"Odd", SER_PAR_ODD},
{"Even", SER_PAR_EVEN},
{"Mark", SER_PAR_MARK},
{"Space", SER_PAR_SPACE},
};
int mask = ctrl->listbox.context.i;
int i, j;
Conf *conf = (Conf *)data;
if (event == EVENT_REFRESH) {
/* Fetching this once at the start of the function ensures we
* remember what the right value is supposed to be when
* operations below cause reentrant calls to this function. */
int oldparity = conf_get_int(conf, CONF_serparity);
dlg_update_start(ctrl, dlg);
dlg_listbox_clear(ctrl, dlg);
for (i = 0; i < lenof(parities); i++) {
if (mask & (1 << i))
dlg_listbox_addwithid(ctrl, dlg, parities[i].name, parities[i].val);
}
for (i = j = 0; i < lenof(parities); i++) {
if (mask & (1 << i)) {
if (oldparity == parities[i].val) {
dlg_listbox_select(ctrl, dlg, j);
break;
}
j++;
}
}
if (i == lenof(parities)) { /* an unsupported setting was chosen */
dlg_listbox_select(ctrl, dlg, 0);
oldparity = SER_PAR_NONE;
}
dlg_update_done(ctrl, dlg);
conf_set_int(conf, CONF_serparity, oldparity); /* restore */
} else if (event == EVENT_SELCHANGE) {
int i = dlg_listbox_index(ctrl, dlg);
if (i < 0)
i = SER_PAR_NONE;
else
i = dlg_listbox_getid(ctrl, dlg, i);
conf_set_int(conf, CONF_serparity, i);
}
}
static void serial_flow_handler(union control *ctrl,
void *dlg,
void *data,
int event)
{
static const struct {
const char *name;
int val;
} flows[] = {
{"None", SER_FLOW_NONE},
{"XON/XOFF", SER_FLOW_XONXOFF},
{"RTS/CTS", SER_FLOW_RTSCTS},
{"DSR/DTR", SER_FLOW_DSRDTR},
};
int mask = ctrl->listbox.context.i;
int i, j;
Conf *conf = (Conf *)data;
if (event == EVENT_REFRESH) {
/* Fetching this once at the start of the function ensures we
* remember what the right value is supposed to be when
* operations below cause reentrant calls to this function. */
int oldflow = conf_get_int(conf, CONF_serflow);
dlg_update_start(ctrl, dlg);
dlg_listbox_clear(ctrl, dlg);
for (i = 0; i < lenof(flows); i++) {
if (mask & (1 << i))
dlg_listbox_addwithid(ctrl, dlg, flows[i].name, flows[i].val);
}
for (i = j = 0; i < lenof(flows); i++) {
if (mask & (1 << i)) {
if (oldflow == flows[i].val) {
dlg_listbox_select(ctrl, dlg, j);
break;
}
j++;
}
}
if (i == lenof(flows)) { /* an unsupported setting was chosen */
dlg_listbox_select(ctrl, dlg, 0);
oldflow = SER_FLOW_NONE;
}
dlg_update_done(ctrl, dlg);
conf_set_int(conf, CONF_serflow, oldflow); /* restore */
} else if (event == EVENT_SELCHANGE) {
int i = dlg_listbox_index(ctrl, dlg);
if (i < 0)
i = SER_FLOW_NONE;
else
i = dlg_listbox_getid(ctrl, dlg, i);
conf_set_int(conf, CONF_serflow, i);
}
}
void ser_setup_config_box(struct controlbox *b,
int midsession,
int parity_mask,
int flow_mask)
{
struct controlset *s;
union control *c;
if (!midsession) {
int i;
extern void config_protocolbuttons_handler(
union control *, void *, void *, int);
/*
* Add the serial back end to the protocols list at the
* top of the config box.
*/
s = ctrl_getset(b,
"Session",
"hostport",
"Specify the destination you want to connect to");
for (i = 0; i < s->ncontrols; i++) {
c = s->ctrls[i];
if (c->generic.type == CTRL_RADIO &&
c->generic.handler == config_protocolbuttons_handler) {
c->radio.nbuttons++;
c->radio.ncolumns++;
c->radio.buttons = sresize(c->radio.buttons, c->radio.nbuttons, char *);
c->radio.buttons[c->radio.nbuttons - 1] = dupstr("Serial");
c->radio.buttondata =
sresize(c->radio.buttondata, c->radio.nbuttons, intorptr);
c->radio.buttondata[c->radio.nbuttons - 1] = I(PROT_SERIAL);
if (c->radio.shortcuts) {
c->radio.shortcuts =
sresize(c->radio.shortcuts, c->radio.nbuttons, char);
c->radio.shortcuts[c->radio.nbuttons - 1] = 'r';
}
}
}
}
/*
* Entirely new Connection/Serial panel for serial port
* configuration.
*/
ctrl_settitle(
b, "Connection/Serial", "Options controlling local serial lines");
if (!midsession) {
/*
* We don't permit switching to a different serial port in
* midflight, although we do allow all other
* reconfiguration.
*/
s = ctrl_getset(b, "Connection/Serial", "serline", "Select a serial line");
ctrl_editbox(s,
"Serial line to connect to",
'l',
40,
HELPCTX(serial_line),
conf_editbox_handler,
I(CONF_serline),
I(1));
}
s = ctrl_getset(
b, "Connection/Serial", "sercfg", "Configure the serial line");
ctrl_editbox(s,
"Speed (baud)",
's',
40,
HELPCTX(serial_speed),
conf_editbox_handler,
I(CONF_serspeed),
I(-1));
ctrl_editbox(s,
"Data bits",
'b',
40,
HELPCTX(serial_databits),
conf_editbox_handler,
I(CONF_serdatabits),
I(-1));
/*
* Stop bits come in units of one half.
*/
ctrl_editbox(s,
"Stop bits",
't',
40,
HELPCTX(serial_stopbits),
conf_editbox_handler,
I(CONF_serstopbits),
I(-2));
ctrl_droplist(s,
"Parity",
'p',
40,
HELPCTX(serial_parity),
serial_parity_handler,
I(parity_mask));
ctrl_droplist(s,
"Flow control",
'f',
40,
HELPCTX(serial_flow),
serial_flow_handler,
I(flow_mask));
}