]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - sercfg.c
Fix line endings (svn:eol-style properties and actual CRs).
[PuTTY.git] / 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 i;
33     Config *cfg = (Config *)data;
34
35     if (event == EVENT_REFRESH) {
36         dlg_update_start(ctrl, dlg);
37         dlg_listbox_clear(ctrl, dlg);
38         for (i = 0; i < lenof(parities); i++)
39             dlg_listbox_addwithid(ctrl, dlg, parities[i].name,
40                                   parities[i].val);
41         for (i = 0; i < lenof(parities); i++)
42             if (cfg->serparity == parities[i].val)
43                 dlg_listbox_select(ctrl, dlg, i);
44         dlg_update_done(ctrl, dlg);
45     } else if (event == EVENT_SELCHANGE) {
46         int i = dlg_listbox_index(ctrl, dlg);
47         if (i < 0)
48             i = SER_PAR_NONE;
49         else
50             i = dlg_listbox_getid(ctrl, dlg, i);
51         cfg->serparity = i;
52     }
53 }
54
55 static void serial_flow_handler(union control *ctrl, void *dlg,
56                                 void *data, int event)
57 {
58     static const struct {
59         const char *name;
60         int val;
61     } flows[] = {
62         {"None", SER_FLOW_NONE},
63         {"XON/XOFF", SER_FLOW_XONXOFF},
64         {"RTS/CTS", SER_FLOW_RTSCTS},
65         {"DSR/DTR", SER_FLOW_DSRDTR},
66     };
67     int i;
68     Config *cfg = (Config *)data;
69
70     if (event == EVENT_REFRESH) {
71         dlg_update_start(ctrl, dlg);
72         dlg_listbox_clear(ctrl, dlg);
73         for (i = 0; i < lenof(flows); i++)
74             dlg_listbox_addwithid(ctrl, dlg, flows[i].name,
75                                   flows[i].val);
76         for (i = 0; i < lenof(flows); i++)
77             if (cfg->serflow == flows[i].val)
78                 dlg_listbox_select(ctrl, dlg, i);
79         dlg_update_done(ctrl, dlg);
80     } else if (event == EVENT_SELCHANGE) {
81         int i = dlg_listbox_index(ctrl, dlg);
82         if (i < 0)
83             i = SER_PAR_NONE;
84         else
85             i = dlg_listbox_getid(ctrl, dlg, i);
86         cfg->serflow = i;
87     }
88 }
89
90 void ser_setup_config_box(struct controlbox *b, int midsession)
91 {
92     struct controlset *s;
93     union control *c;
94
95     /*
96      * Add the serial back end to the protocols list at the top of
97      * the config box.
98      */
99     s = ctrl_getset(b, "Session", "hostport",
100                     "Specify your connection by host name or IP address");
101     {
102         int i;
103         extern void config_protocolbuttons_handler(union control *, void *,
104                                                    void *, int);
105         for (i = 0; i < s->ncontrols; i++) {
106             c = s->ctrls[i];
107             if (c->generic.type == CTRL_RADIO &&
108                 c->generic.handler == config_protocolbuttons_handler) {
109                 c->radio.nbuttons++;
110                 c->radio.ncolumns++;
111                 c->radio.buttons =
112                     sresize(c->radio.buttons, c->radio.nbuttons, char *);
113                 c->radio.buttons[c->radio.nbuttons-1] =
114                     dupstr("Serial");
115                 c->radio.buttondata =
116                     sresize(c->radio.buttondata, c->radio.nbuttons, intorptr);
117                 c->radio.buttondata[c->radio.nbuttons-1] = I(PROT_SERIAL);
118                 if (c->radio.shortcuts) {
119                     c->radio.shortcuts =
120                         sresize(c->radio.shortcuts, c->radio.nbuttons, char);
121                     c->radio.shortcuts[c->radio.nbuttons-1] = NO_SHORTCUT;
122                 }
123             }
124         }
125     }
126
127     /*
128      * Entirely new Connection/Serial panel for serial port
129      * configuration.
130      */
131     ctrl_settitle(b, "Connection/Serial",
132                   "Options controlling local serial lines");
133
134     if (!midsession) {
135         /*
136          * We don't permit switching to a different serial port in
137          * midflight, although we do allow all other
138          * reconfiguration.
139          */
140         s = ctrl_getset(b, "Connection/Serial", "serline",
141                         "Select a serial line");
142         ctrl_editbox(s, "Serial line to connect to", 'l', 40,
143                      HELPCTX(serial_line),
144                      dlg_stdeditbox_handler, I(offsetof(Config,serline)),
145                      I(sizeof(((Config *)0)->serline)));
146     }
147
148     s = ctrl_getset(b, "Connection/Serial", "sercfg", "Configure the serial line");
149     ctrl_editbox(s, "Speed (baud)", 's', 40,
150                  HELPCTX(serial_speed),
151                  dlg_stdeditbox_handler, I(offsetof(Config,serspeed)), I(-1));
152     ctrl_editbox(s, "Data bits", 'b', 40,
153                  HELPCTX(serial_databits),
154                  dlg_stdeditbox_handler,I(offsetof(Config,serdatabits)),I(-1));
155     /*
156      * Stop bits come in units of one half.
157      */
158     ctrl_editbox(s, "Stop bits", 't', 40,
159                  HELPCTX(serial_stopbits),
160                  dlg_stdeditbox_handler,I(offsetof(Config,serstopbits)),I(-2));
161     ctrl_droplist(s, "Parity", 'p', 40,
162                   HELPCTX(serial_parity),
163                   serial_parity_handler, I(0));
164     ctrl_droplist(s, "Flow control", 'f', 40,
165                   HELPCTX(serial_flow),
166                   serial_flow_handler, I(0));
167 }