Improve buffer handling in Windows sk_getaddr() -- we were passing
[u/mdw/putty] / windows / winstuff.h
CommitLineData
8c3cd914 1/*
2 * winstuff.h: Windows-specific inter-module stuff.
3 */
4
f7f27309 5#ifndef PUTTY_WINSTUFF_H
6#define PUTTY_WINSTUFF_H
7
7440fd44 8#ifndef AUTO_WINSOCK
9#include <winsock2.h>
10#endif
11#include <windows.h>
9a30e26b 12#include <stdio.h> /* for FILENAME_MAX */
13
fe8abbf4 14#include "tree234.h"
15
16#include "winhelp.h"
17
9a30e26b 18struct Filename {
19 char path[FILENAME_MAX];
20};
c6940f12 21#define f_open(filename, mode, isprivate) ( fopen((filename).path, (mode)) )
9a30e26b 22
23struct FontSpec {
24 char name[64];
25 int isbold;
26 int height;
27 int charset;
28};
29
17c7fed1 30#ifndef CLEARTYPE_QUALITY
31#define CLEARTYPE_QUALITY 5
32#endif
33#define FONT_QUALITY(fq) ( \
34 (fq) == FQ_DEFAULT ? DEFAULT_QUALITY : \
35 (fq) == FQ_ANTIALIASED ? ANTIALIASED_QUALITY : \
36 (fq) == FQ_NONANTIALIASED ? NONANTIALIASED_QUALITY : \
37 CLEARTYPE_QUALITY)
38
61ef0c16 39#define PLATFORM_IS_UTF16 /* enable UTF-16 processing when exchanging
40 * wchar_t strings with environment */
41
158cb9b1 42/*
43 * Where we can, we use GetWindowLongPtr and friends because they're
44 * more useful on 64-bit platforms, but they're a relatively recent
45 * innovation, missing from VC++ 6 and older MinGW. Degrade nicely.
46 * (NB that on some systems, some of these things are available but
47 * not others...)
48 */
17c7fed1 49
158cb9b1 50#ifndef GCLP_HCURSOR
51/* GetClassLongPtr and friends */
52#undef GetClassLongPtr
53#define GetClassLongPtr GetClassLong
54#undef SetClassLongPtr
55#define SetClassLongPtr SetClassLong
56#define GCLP_HCURSOR GCL_HCURSOR
57/* GetWindowLongPtr and friends */
58#undef GetWindowLongPtr
17c7fed1 59#define GetWindowLongPtr GetWindowLong
158cb9b1 60#undef SetWindowLongPtr
17c7fed1 61#define SetWindowLongPtr SetWindowLong
158cb9b1 62#undef GWLP_USERDATA
17c7fed1 63#define GWLP_USERDATA GWL_USERDATA
158cb9b1 64#undef DWLP_MSGRESULT
17c7fed1 65#define DWLP_MSGRESULT DWL_MSGRESULT
158cb9b1 66/* Since we've clobbered the above functions, we should clobber the
67 * associated type regardless of whether it's defined. */
68#undef LONG_PTR
69#define LONG_PTR LONG
17c7fed1 70#endif
71
2979718a 72#define BOXFLAGS DLGWINDOWEXTRA
1e5eefb6 73#define BOXRESULT (DLGWINDOWEXTRA + sizeof(LONG_PTR))
8ee3ff16 74#define DF_END 0x0001
75
8c3cd914 76/*
77 * Global variables. Most modules declare these `extern', but
78 * window.c will do `#define PUTTY_DO_GLOBALS' before including this
79 * module, and so will get them properly defined.
2979718a 80*/
f7f27309 81#ifndef GLOBAL
8c3cd914 82#ifdef PUTTY_DO_GLOBALS
83#define GLOBAL
84#else
85#define GLOBAL extern
86#endif
f7f27309 87#endif
88
a8327734 89#ifndef DONE_TYPEDEFS
90#define DONE_TYPEDEFS
91typedef struct config_tag Config;
92typedef struct backend_tag Backend;
93typedef struct terminal_tag Terminal;
94#endif
95
f7f27309 96#define PUTTY_REG_POS "Software\\SimonTatham\\PuTTY"
97#define PUTTY_REG_PARENT "Software\\SimonTatham"
98#define PUTTY_REG_PARENT_CHILD "PuTTY"
99#define PUTTY_REG_GPARENT "Software"
100#define PUTTY_REG_GPARENT_CHILD "SimonTatham"
101
a9dc49af 102#define PUTTY_HELP_FILE "putty.hlp"
cb2708d3 103#define PUTTY_CHM_FILE "putty.chm"
a9dc49af 104#define PUTTY_HELP_CONTENTS "putty.cnt"
105
f7f27309 106#define GETTICKCOUNT GetTickCount
107#define CURSORBLINK GetCaretBlinkTime()
108#define TICKSPERSEC 1000 /* GetTickCount returns milliseconds */
109
110#define DEFAULT_CODEPAGE CP_ACP
111
112typedef HDC Context;
8c3cd914 113
86557057 114#ifndef NO_GSSAPI
115/*
116 * GSS-API stuff
117 */
118typedef struct Ssh_gss_buf {
119 int length;
120 char *value;
121} Ssh_gss_buf;
122
123#define SSH_GSS_EMPTY_BUF (Ssh_gss_buf) {0,NULL}
2c2bce14 124typedef void *Ssh_gss_name;
86557057 125#endif
126
f7f27309 127/*
5321c0c6 128 * Window handles for the windows that can be running during a
f7f27309 129 * PuTTY session.
130 */
5321c0c6 131GLOBAL HWND hwnd; /* the main terminal window */
f7f27309 132GLOBAL HWND logbox;
133
134/*
135 * The all-important instance handle.
136 */
8c3cd914 137GLOBAL HINSTANCE hinst;
138
139/*
cb2708d3 140 * Help file stuff in winhelp.c.
6b78788a 141 */
cb2708d3 142void init_help(void);
143void shutdown_help(void);
144int has_help(void);
145void launch_help(HWND hwnd, const char *topic);
146void quit_help(HWND hwnd);
6b78788a 147
148/*
a8327734 149 * The terminal and logging context are notionally local to the
150 * Windows front end, but they must be shared between window.c and
0b4f0bc0 151 * windlg.c. Likewise the saved-sessions list.
a8327734 152 */
153GLOBAL Terminal *term;
154GLOBAL void *logctx;
155
604fab0c 156#define WM_NETEVENT (WM_APP + 5)
f7f27309 157
158/*
e6346999 159 * On Windows, we send MA_2CLK as the only event marking the second
160 * press of a mouse button. Compare unix.h.
161 */
162#define MULTICLICK_ONLY_EVENT 1
163
164/*
165 * On Windows, data written to the clipboard must be NUL-terminated.
166 */
167#define SELECTION_NUL_TERMINATED 1
168
169/*
0f351587 170 * On Windows, copying to the clipboard terminates lines with CRLF.
171 */
172#define SEL_NL { 13, 10 }
173
174/*
2f92b717 175 * sk_getxdmdata() does not exist under Windows (not that I
176 * couldn't write it if I wanted to, but I haven't bothered), so
0a4022a1 177 * it's a macro which always returns NULL. With any luck this will
2f92b717 178 * cause the compiler to notice it can optimise away the
179 * implementation of XDM-AUTHORIZATION-1 in x11fwd.c :-)
180 */
0a4022a1 181#define sk_getxdmdata(socket, lenp) (NULL)
2f92b717 182
183/*
fe8abbf4 184 * File-selector filter strings used in the config box. On Windows,
185 * these strings are of exactly the type needed to go in
186 * `lpstrFilter' in an OPENFILENAME structure.
187 */
188#define FILTER_KEY_FILES ("PuTTY Private Key Files (*.ppk)\0*.ppk\0" \
189 "All Files (*.*)\0*\0\0\0")
190#define FILTER_WAVE_FILES ("Wave Files (*.wav)\0*.WAV\0" \
191 "All Files (*.*)\0*\0\0\0")
192
193/*
2ac3322e 194 * On some versions of Windows, it has been known for WM_TIMER to
195 * occasionally get its callback time simply wrong, and call us
196 * back several minutes early. Defining these symbols enables
197 * compensation code in timing.c.
198 */
199#define TIMING_SYNC
200#define TIMING_SYNC_TICKCOUNT
201
202/*
7440fd44 203 * winnet.c dynamically loads WinSock 2 or WinSock 1 depending on
204 * what it can get, which means any WinSock routines used outside
205 * that module must be exported from it as function pointers. So
206 * here they are.
207 */
208extern int (WINAPI *p_WSAAsyncSelect)
209 (SOCKET s, HWND hWnd, u_int wMsg, long lEvent);
210extern int (WINAPI *p_WSAEventSelect)
211 (SOCKET s, WSAEVENT hEventObject, long lNetworkEvents);
212extern int (WINAPI *p_select)
213 (int nfds, fd_set FAR * readfds, fd_set FAR * writefds,
214 fd_set FAR *exceptfds, const struct timeval FAR * timeout);
215extern int (WINAPI *p_WSAGetLastError)(void);
216extern int (WINAPI *p_WSAEnumNetworkEvents)
217 (SOCKET s, WSAEVENT hEventObject, LPWSANETWORKEVENTS lpNetworkEvents);
218
39934deb 219extern int socket_writable(SOCKET skt);
220
9f7ebadc 221extern void socket_reselect_all(void);
222
7440fd44 223/*
8c3cd914 224 * Exports from winctrls.c.
225 */
226
227struct ctlpos {
228 HWND hwnd;
229 WPARAM font;
230 int dlu4inpix;
231 int ypos, width;
232 int xoff;
3ac9cd9f 233 int boxystart, boxid;
8c3cd914 234 char *boxtext;
235};
236
ca20bfcf 237/*
c0a81592 238 * Exports from winutils.c.
239 */
9754e5ca 240typedef struct filereq_tag filereq; /* cwd for file requester */
241BOOL request_file(filereq *state, OPENFILENAME *of, int preserve, int save);
242filereq *filereq_new(void);
243void filereq_free(filereq *state);
690695e0 244int message_box(LPCTSTR text, LPCTSTR caption, DWORD style, DWORD helpctxid);
d3a1a808 245void split_into_argv(char *, int *, char ***, char ***);
c0a81592 246
247/*
ca20bfcf 248 * Private structure for prefslist state. Only in the header file
249 * so that we can delegate allocation to callers.
250 */
251struct prefslist {
252 int listid, upbid, dnbid;
253 int srcitem;
254 int dummyitem;
255 int dragging;
256};
257
f7f27309 258/*
fe8abbf4 259 * This structure is passed to event handler functions as the `dlg'
260 * parameter, and hence is passed back to winctrls access functions.
261 */
262struct dlgparam {
263 HWND hwnd; /* the hwnd of the dialog box */
264 struct winctrls *controltrees[8]; /* can have several of these */
265 int nctrltrees;
f6f450e2 266 char *wintitle; /* title of actual window */
fe8abbf4 267 char *errtitle; /* title of error sub-messageboxes */
268 void *data; /* data to pass in refresh events */
269 union control *focused, *lastfocused; /* which ctrl has focus now/before */
4e6d4091 270 char shortcuts[128]; /* track which shortcuts in use */
fe8abbf4 271 int coloursel_wanted; /* has an event handler asked for
272 * a colour selector? */
fe8abbf4 273 struct { unsigned char r, g, b, ok; } coloursel_result; /* 0-255 */
4e6d4091 274 tree234 *privdata; /* stores per-control private data */
fe8abbf4 275 int ended, endresult; /* has the dialog been ended? */
276};
277
278/*
f7f27309 279 * Exports from winctrls.c.
280 */
8c3cd914 281void ctlposinit(struct ctlpos *cp, HWND hwnd,
32874aea 282 int leftborder, int rightborder, int topborder);
ca20bfcf 283HWND doctl(struct ctlpos *cp, RECT r,
32874aea 284 char *wclass, int wstyle, int exstyle, char *wtext, int wid);
8c3cd914 285void bartitle(struct ctlpos *cp, char *name, int id);
3ac9cd9f 286void beginbox(struct ctlpos *cp, char *name, int idbox);
8c3cd914 287void endbox(struct ctlpos *cp);
e73667f3 288void editboxfw(struct ctlpos *cp, int password, char *text,
289 int staticid, int editid);
32874aea 290void radioline(struct ctlpos *cp, char *text, int id, int nacross, ...);
d74d141c 291void bareradioline(struct ctlpos *cp, int nacross, ...);
8c3cd914 292void radiobig(struct ctlpos *cp, char *text, int id, ...);
293void checkbox(struct ctlpos *cp, char *text, int id);
66ee282a 294void statictext(struct ctlpos *cp, char *text, int lines, int id);
8c3cd914 295void staticbtn(struct ctlpos *cp, char *stext, int sid,
32874aea 296 char *btext, int bid);
af282e3b 297void static2btn(struct ctlpos *cp, char *stext, int sid,
298 char *btext1, int bid1, char *btext2, int bid2);
8c3cd914 299void staticedit(struct ctlpos *cp, char *stext,
32874aea 300 int sid, int eid, int percentedit);
2c9c6388 301void staticddl(struct ctlpos *cp, char *stext,
302 int sid, int lid, int percentlist);
b8ae1f0f 303void combobox(struct ctlpos *cp, char *text, int staticid, int listid);
6e522441 304void staticpassedit(struct ctlpos *cp, char *stext,
32874aea 305 int sid, int eid, int percentedit);
6e522441 306void bigeditctrl(struct ctlpos *cp, char *stext,
32874aea 307 int sid, int eid, int lines);
308void ersatztab(struct ctlpos *cp, char *stext, int sid, int lid, int s2id);
8c3cd914 309void editbutton(struct ctlpos *cp, char *stext, int sid,
32874aea 310 int eid, char *btext, int bid);
8c3cd914 311void sesssaver(struct ctlpos *cp, char *text,
32874aea 312 int staticid, int editid, int listid, ...);
8c3cd914 313void envsetter(struct ctlpos *cp, char *stext, int sid,
32874aea 314 char *e1stext, int e1sid, int e1id,
315 char *e2stext, int e2sid, int e2id,
316 int listid, char *b1text, int b1id, char *b2text, int b2id);
8c3cd914 317void charclass(struct ctlpos *cp, char *stext, int sid, int listid,
32874aea 318 char *btext, int bid, int eid, char *s2text, int s2id);
8c3cd914 319void colouredit(struct ctlpos *cp, char *stext, int sid, int listid,
32874aea 320 char *btext, int bid, ...);
fe8abbf4 321void prefslist(struct prefslist *hdl, struct ctlpos *cp, int lines,
322 char *stext, int sid, int listid, int upbid, int dnbid);
ca20bfcf 323int handle_prefslist(struct prefslist *hdl,
324 int *array, int maxmemb,
325 int is_dlmsg, HWND hwnd,
326 WPARAM wParam, LPARAM lParam);
6e522441 327void progressbar(struct ctlpos *cp, int id);
d74d141c 328void fwdsetter(struct ctlpos *cp, int listid, char *stext, int sid,
329 char *e1stext, int e1sid, int e1id,
330 char *e2stext, int e2sid, int e2id,
a4b92c62 331 char *btext, int bid,
332 char *r1text, int r1id, char *r2text, int r2id);
f7f27309 333
fe8abbf4 334#define MAX_SHORTCUTS_PER_CTRL 16
335
336/*
337 * This structure is what's stored for each `union control' in the
338 * portable-dialog interface.
339 */
340struct winctrl {
341 union control *ctrl;
342 /*
343 * The control may have several components at the Windows
344 * level, with different dialog IDs. To avoid needing N
345 * separate platformsidectrl structures (which could be stored
346 * separately in a tree234 so that lookup by ID worked), we
347 * impose the constraint that those IDs must be in a contiguous
348 * block.
349 */
350 int base_id;
351 int num_ids;
352 /*
353 * Remember what keyboard shortcuts were used by this control,
354 * so that when we remove it again we can take them out of the
355 * list in the dlgparam.
356 */
357 char shortcuts[MAX_SHORTCUTS_PER_CTRL];
358 /*
359 * Some controls need a piece of allocated memory in which to
360 * store temporary data about the control.
361 */
362 void *data;
363};
364/*
365 * And this structure holds a set of the above, in two separate
366 * tree234s so that it can find an item by `union control' or by
367 * dialog ID.
368 */
369struct winctrls {
370 tree234 *byctrl, *byid;
371};
d1582b2e 372struct controlset;
373struct controlbox;
374
fe8abbf4 375void winctrl_init(struct winctrls *);
376void winctrl_cleanup(struct winctrls *);
377void winctrl_add(struct winctrls *, struct winctrl *);
378void winctrl_remove(struct winctrls *, struct winctrl *);
379struct winctrl *winctrl_findbyctrl(struct winctrls *, union control *);
380struct winctrl *winctrl_findbyid(struct winctrls *, int);
381struct winctrl *winctrl_findbyindex(struct winctrls *, int);
382void winctrl_layout(struct dlgparam *dp, struct winctrls *wc,
383 struct ctlpos *cp, struct controlset *s, int *id);
384int winctrl_handle_command(struct dlgparam *dp, UINT msg,
385 WPARAM wParam, LPARAM lParam);
d1582b2e 386void winctrl_rem_shortcuts(struct dlgparam *dp, struct winctrl *c);
387int winctrl_context_help(struct dlgparam *dp, HWND hwnd, int id);
388
4e6d4091 389void dp_init(struct dlgparam *dp);
390void dp_add_tree(struct dlgparam *dp, struct winctrls *tree);
391void dp_cleanup(struct dlgparam *dp);
392
d1582b2e 393/*
394 * Exports from wincfg.c.
395 */
396void win_setup_config_box(struct controlbox *b, HWND *hwndp, int has_help,
8a7f1392 397 int midsession, int protocol);
fe8abbf4 398
f7f27309 399/*
400 * Exports from windlg.c.
401 */
402void defuse_showwindow(void);
403int do_config(void);
f89c3294 404int do_reconfig(HWND, int);
f7f27309 405void showeventlog(HWND);
406void showabout(HWND);
f7f27309 407void force_normal(HWND hwnd);
d1582b2e 408void modal_about_box(HWND hwnd);
409void show_help(HWND hwnd);
f7f27309 410
f7f27309 411/*
8ee3ff16 412 * Exports from winmisc.c.
413 */
4c48c989 414extern OSVERSIONINFO osVersion;
415BOOL init_winver(void);
416
8ee3ff16 417/*
f7f27309 418 * Exports from sizetip.c.
419 */
420void UpdateSizeTip(HWND src, int cx, int cy);
421void EnableSizeTip(int bEnable);
422
facd762c 423/*
424 * Exports from unicode.c.
425 */
07b2a9cd 426struct unicode_data;
21d2b241 427void init_ucs(Config *, struct unicode_data *);
facd762c 428
c44bf5bd 429/*
34292b1d 430 * Exports from winhandl.c.
431 */
bdebd7e9 432#define HANDLE_FLAG_OVERLAPPED 1
433#define HANDLE_FLAG_IGNOREEOF 2
799e067e 434#define HANDLE_FLAG_UNITBUFFER 4
34292b1d 435struct handle;
436typedef int (*handle_inputfn_t)(struct handle *h, void *data, int len);
437typedef void (*handle_outputfn_t)(struct handle *h, int new_backlog);
0e03ceff 438struct handle *handle_input_new(HANDLE handle, handle_inputfn_t gotdata,
bdebd7e9 439 void *privdata, int flags);
0e03ceff 440struct handle *handle_output_new(HANDLE handle, handle_outputfn_t sentdata,
bdebd7e9 441 void *privdata, int flags);
34292b1d 442int handle_write(struct handle *h, const void *data, int len);
443HANDLE *handle_get_events(int *nevents);
444void handle_free(struct handle *h);
445void handle_got_event(HANDLE event);
446void handle_unthrottle(struct handle *h, int backlog);
447int handle_backlog(struct handle *h);
0e03ceff 448void *handle_get_privdata(struct handle *h);
34292b1d 449
450/*
c44bf5bd 451 * pageantc.c needs to schedule callbacks for asynchronous agent
452 * requests. This has to be done differently in GUI and console, so
453 * there's an exported function used for the purpose.
454 *
455 * Also, we supply FLAG_SYNCAGENT to force agent requests to be
456 * synchronous in pscp and psftp.
457 */
458void agent_schedule_callback(void (*callback)(void *, void *, int),
459 void *callback_ctx, void *data, int len);
460#define FLAG_SYNCAGENT 0x1000
461
7374c779 462/*
463 * Exports from winser.c.
464 */
465extern Backend serial_backend;
466
f7f27309 467#endif