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