Avoid creating the Session/hostport control set in mid-session.
[u/mdw/putty] / sercfg.c
1 /*
2 * sercfg.c - the serial-port specific parts of the PuTTY
3 * configuration box. Centralised as cross-platform code because
4 * more than one platform will want to use it, but not part of the
5 * main configuration. The expectation is that each platform's
6 * local config function will call out to ser_setup_config_box() if
7 * it needs to set up the standard serial stuff. (Of course, it can
8 * then apply local tweaks after ser_setup_config_box() returns, if
9 * it needs to.)
10 */
11
12 #include <assert.h>
13 #include <stdlib.h>
14
15 #include "putty.h"
16 #include "dialog.h"
17 #include "storage.h"
18
19 static void serial_parity_handler(union control *ctrl, void *dlg,
20 void *data, int event)
21 {
22 static const struct {
23 const char *name;
24 int val;
25 } parities[] = {
26 {"None", SER_PAR_NONE},
27 {"Odd", SER_PAR_ODD},
28 {"Even", SER_PAR_EVEN},
29 {"Mark", SER_PAR_MARK},
30 {"Space", SER_PAR_SPACE},
31 };
32 int mask = ctrl->listbox.context.i;
33 int i, j;
34 Config *cfg = (Config *)data;
35
36 if (event == EVENT_REFRESH) {
37 dlg_update_start(ctrl, dlg);
38 dlg_listbox_clear(ctrl, dlg);
39 for (i = 0; i < lenof(parities); i++) {
40 if (mask & (1 << i))
41 dlg_listbox_addwithid(ctrl, dlg, parities[i].name,
42 parities[i].val);
43 }
44 for (i = j = 0; i < lenof(parities); i++) {
45 if (cfg->serparity == parities[i].val)
46 dlg_listbox_select(ctrl, dlg, j);
47 if (mask & (1 << i))
48 j++;
49 }
50 dlg_update_done(ctrl, dlg);
51 } else if (event == EVENT_SELCHANGE) {
52 int i = dlg_listbox_index(ctrl, dlg);
53 if (i < 0)
54 i = SER_PAR_NONE;
55 else
56 i = dlg_listbox_getid(ctrl, dlg, i);
57 cfg->serparity = i;
58 }
59 }
60
61 static void serial_flow_handler(union control *ctrl, void *dlg,
62 void *data, int event)
63 {
64 static const struct {
65 const char *name;
66 int val;
67 } flows[] = {
68 {"None", SER_FLOW_NONE},
69 {"XON/XOFF", SER_FLOW_XONXOFF},
70 {"RTS/CTS", SER_FLOW_RTSCTS},
71 {"DSR/DTR", SER_FLOW_DSRDTR},
72 };
73 int mask = ctrl->listbox.context.i;
74 int i, j;
75 Config *cfg = (Config *)data;
76
77 if (event == EVENT_REFRESH) {
78 dlg_update_start(ctrl, dlg);
79 dlg_listbox_clear(ctrl, dlg);
80 for (i = 0; i < lenof(flows); i++) {
81 if (mask & (1 << i))
82 dlg_listbox_addwithid(ctrl, dlg, flows[i].name, flows[i].val);
83 }
84 for (i = j = 0; i < lenof(flows); i++) {
85 if (cfg->serflow == flows[i].val)
86 dlg_listbox_select(ctrl, dlg, j);
87 if (mask & (1 << i))
88 j++;
89 }
90 dlg_update_done(ctrl, dlg);
91 } else if (event == EVENT_SELCHANGE) {
92 int i = dlg_listbox_index(ctrl, dlg);
93 if (i < 0)
94 i = SER_PAR_NONE;
95 else
96 i = dlg_listbox_getid(ctrl, dlg, i);
97 cfg->serflow = i;
98 }
99 }
100
101 void ser_setup_config_box(struct controlbox *b, int midsession,
102 int parity_mask, int flow_mask)
103 {
104 struct controlset *s;
105 union control *c;
106
107 if (!midsession) {
108 int i;
109 extern void config_protocolbuttons_handler(union control *, void *,
110 void *, int);
111
112 /*
113 * Add the serial back end to the protocols list at the
114 * top of the config box.
115 */
116 s = ctrl_getset(b, "Session", "hostport",
117 "Specify the destination you want to connect to");
118
119 for (i = 0; i < s->ncontrols; i++) {
120 c = s->ctrls[i];
121 if (c->generic.type == CTRL_RADIO &&
122 c->generic.handler == config_protocolbuttons_handler) {
123 c->radio.nbuttons++;
124 c->radio.ncolumns++;
125 c->radio.buttons =
126 sresize(c->radio.buttons, c->radio.nbuttons, char *);
127 c->radio.buttons[c->radio.nbuttons-1] =
128 dupstr("Serial");
129 c->radio.buttondata =
130 sresize(c->radio.buttondata, c->radio.nbuttons, intorptr);
131 c->radio.buttondata[c->radio.nbuttons-1] = I(PROT_SERIAL);
132 if (c->radio.shortcuts) {
133 c->radio.shortcuts =
134 sresize(c->radio.shortcuts, c->radio.nbuttons, char);
135 c->radio.shortcuts[c->radio.nbuttons-1] = NO_SHORTCUT;
136 }
137 }
138 }
139 }
140
141 /*
142 * Entirely new Connection/Serial panel for serial port
143 * configuration.
144 */
145 ctrl_settitle(b, "Connection/Serial",
146 "Options controlling local serial lines");
147
148 if (!midsession) {
149 /*
150 * We don't permit switching to a different serial port in
151 * midflight, although we do allow all other
152 * reconfiguration.
153 */
154 s = ctrl_getset(b, "Connection/Serial", "serline",
155 "Select a serial line");
156 ctrl_editbox(s, "Serial line to connect to", 'l', 40,
157 HELPCTX(serial_line),
158 dlg_stdeditbox_handler, I(offsetof(Config,serline)),
159 I(sizeof(((Config *)0)->serline)));
160 }
161
162 s = ctrl_getset(b, "Connection/Serial", "sercfg", "Configure the serial line");
163 ctrl_editbox(s, "Speed (baud)", 's', 40,
164 HELPCTX(serial_speed),
165 dlg_stdeditbox_handler, I(offsetof(Config,serspeed)), I(-1));
166 ctrl_editbox(s, "Data bits", 'b', 40,
167 HELPCTX(serial_databits),
168 dlg_stdeditbox_handler,I(offsetof(Config,serdatabits)),I(-1));
169 /*
170 * Stop bits come in units of one half.
171 */
172 ctrl_editbox(s, "Stop bits", 't', 40,
173 HELPCTX(serial_stopbits),
174 dlg_stdeditbox_handler,I(offsetof(Config,serstopbits)),I(-2));
175 ctrl_droplist(s, "Parity", 'p', 40,
176 HELPCTX(serial_parity),
177 serial_parity_handler, I(parity_mask));
178 ctrl_droplist(s, "Flow control", 'f', 40,
179 HELPCTX(serial_flow),
180 serial_flow_handler, I(flow_mask));
181 }