]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - windows/winstuff.h
c1918d4ad2744273786a45d8079c2f7129859292
[PuTTY.git] / windows / winstuff.h
1 /*
2  * winstuff.h: Windows-specific inter-module stuff.
3  */
4
5 #ifndef PUTTY_WINSTUFF_H
6 #define PUTTY_WINSTUFF_H
7
8 #ifndef AUTO_WINSOCK
9 #include <winsock2.h>
10 #endif
11 #include <windows.h>
12 #include <stdio.h>                     /* for FILENAME_MAX */
13
14 /* We use uintptr_t for Win32/Win64 portability, so we should in
15  * principle include stdint.h, which defines it according to the C
16  * standard. But older versions of Visual Studio - including the one
17  * used for official PuTTY builds as of 2015-09-28 - don't provide
18  * stdint.h at all, but do (non-standardly) define uintptr_t in
19  * stddef.h. So here we try to make sure _some_ standard header is
20  * included which defines uintptr_t. */
21 #include <stddef.h>
22 #if !defined _MSC_VER || _MSC_VER >= 1600
23 #include <stdint.h>
24 #endif
25
26 #include "tree234.h"
27
28 #include "winhelp.h"
29
30 #define BUILDINFO_PLATFORM "Windows"
31
32 struct Filename {
33     char *path;
34 };
35 #define f_open(filename, mode, isprivate) ( fopen((filename)->path, (mode)) )
36
37 struct FontSpec {
38     char *name;
39     int isbold;
40     int height;
41     int charset;
42 };
43 struct FontSpec *fontspec_new(const char *name,
44                                int bold, int height, int charset);
45
46 #ifndef CLEARTYPE_QUALITY
47 #define CLEARTYPE_QUALITY 5
48 #endif
49 #define FONT_QUALITY(fq) ( \
50     (fq) == FQ_DEFAULT ? DEFAULT_QUALITY : \
51     (fq) == FQ_ANTIALIASED ? ANTIALIASED_QUALITY : \
52     (fq) == FQ_NONANTIALIASED ? NONANTIALIASED_QUALITY : \
53     CLEARTYPE_QUALITY)
54
55 #define PLATFORM_IS_UTF16 /* enable UTF-16 processing when exchanging
56                            * wchar_t strings with environment */
57
58 /*
59  * Where we can, we use GetWindowLongPtr and friends because they're
60  * more useful on 64-bit platforms, but they're a relatively recent
61  * innovation, missing from VC++ 6 and older MinGW.  Degrade nicely.
62  * (NB that on some systems, some of these things are available but
63  * not others...)
64  */
65
66 #ifndef GCLP_HCURSOR
67 /* GetClassLongPtr and friends */
68 #undef  GetClassLongPtr
69 #define GetClassLongPtr GetClassLong
70 #undef  SetClassLongPtr
71 #define SetClassLongPtr SetClassLong
72 #define GCLP_HCURSOR GCL_HCURSOR
73 /* GetWindowLongPtr and friends */
74 #undef  GetWindowLongPtr
75 #define GetWindowLongPtr GetWindowLong
76 #undef  SetWindowLongPtr
77 #define SetWindowLongPtr SetWindowLong
78 #undef  GWLP_USERDATA
79 #define GWLP_USERDATA GWL_USERDATA
80 #undef  DWLP_MSGRESULT
81 #define DWLP_MSGRESULT DWL_MSGRESULT
82 /* Since we've clobbered the above functions, we should clobber the
83  * associated type regardless of whether it's defined. */
84 #undef LONG_PTR
85 #define LONG_PTR LONG
86 #endif
87
88 #define BOXFLAGS DLGWINDOWEXTRA
89 #define BOXRESULT (DLGWINDOWEXTRA + sizeof(LONG_PTR))
90 #define DF_END 0x0001
91
92 #ifdef __WINE__
93 #define NO_SECUREZEROMEMORY            /* winelib doesn't have this */
94 #endif
95
96 #ifndef NO_SECUREZEROMEMORY
97 #define PLATFORM_HAS_SMEMCLR /* inhibit cross-platform one in misc.c */
98 #endif
99
100 #ifndef __WINE__
101 /* Up-to-date Windows headers warn that the unprefixed versions of
102  * these names are deprecated. */
103 #define stricmp _stricmp
104 #define strnicmp _strnicmp
105 #else
106 /* Compiling with winegcc, _neither_ version of these functions
107  * exists. Use the POSIX names. */
108 #define stricmp strcasecmp
109 #define strnicmp strncasecmp
110 #endif
111
112 #define BROKEN_PIPE_ERROR_CODE ERROR_BROKEN_PIPE   /* used in sshshare.c */
113
114 /*
115  * Dynamically linked functions. These come in two flavours:
116  *
117  *  - GET_WINDOWS_FUNCTION does not expose "name" to the preprocessor,
118  *    so will always dynamically link against exactly what is specified
119  *    in "name". If you're not sure, use this one.
120  *
121  *  - GET_WINDOWS_FUNCTION_PP allows "name" to be redirected via
122  *    preprocessor definitions like "#define foo bar"; this is principally
123  *    intended for the ANSI/Unicode DoSomething/DoSomethingA/DoSomethingW.
124  *    If your function has an argument of type "LPTSTR" or similar, this
125  *    is the variant to use.
126  *    (However, it can't always be used, as it trips over more complicated
127  *    macro trickery such as the WspiapiGetAddrInfo wrapper for getaddrinfo.)
128  *
129  * (DECL_WINDOWS_FUNCTION works with both these variants.)
130  */
131 #define DECL_WINDOWS_FUNCTION(linkage, rettype, name, params) \
132     typedef rettype (WINAPI *t_##name) params; \
133     linkage t_##name p_##name
134 #define STR1(x) #x
135 #define STR(x) STR1(x)
136 #define GET_WINDOWS_FUNCTION_PP(module, name) \
137     (p_##name = module ? (t_##name) GetProcAddress(module, STR(name)) : NULL)
138 #define GET_WINDOWS_FUNCTION(module, name) \
139     (p_##name = module ? (t_##name) GetProcAddress(module, #name) : NULL)
140
141 /*
142  * Global variables. Most modules declare these `extern', but
143  * window.c will do `#define PUTTY_DO_GLOBALS' before including this
144  * module, and so will get them properly defined.
145 */
146 #ifndef GLOBAL
147 #ifdef PUTTY_DO_GLOBALS
148 #define GLOBAL
149 #else
150 #define GLOBAL extern
151 #endif
152 #endif
153
154 #ifndef DONE_TYPEDEFS
155 #define DONE_TYPEDEFS
156 typedef struct conf_tag Conf;
157 typedef struct backend_tag Backend;
158 typedef struct terminal_tag Terminal;
159 #endif
160
161 #define PUTTY_REG_POS "Software\\SimonTatham\\PuTTY"
162 #define PUTTY_REG_PARENT "Software\\SimonTatham"
163 #define PUTTY_REG_PARENT_CHILD "PuTTY"
164 #define PUTTY_REG_GPARENT "Software"
165 #define PUTTY_REG_GPARENT_CHILD "SimonTatham"
166
167 /* Result values for the jumplist registry functions. */
168 #define JUMPLISTREG_OK 0
169 #define JUMPLISTREG_ERROR_INVALID_PARAMETER 1
170 #define JUMPLISTREG_ERROR_KEYOPENCREATE_FAILURE 2
171 #define JUMPLISTREG_ERROR_VALUEREAD_FAILURE 3
172 #define JUMPLISTREG_ERROR_VALUEWRITE_FAILURE 4
173 #define JUMPLISTREG_ERROR_INVALID_VALUE 5
174
175 #define PUTTY_HELP_FILE "putty.hlp"
176 #define PUTTY_CHM_FILE "putty.chm"
177 #define PUTTY_HELP_CONTENTS "putty.cnt"
178
179 #define GETTICKCOUNT GetTickCount
180 #define CURSORBLINK GetCaretBlinkTime()
181 #define TICKSPERSEC 1000               /* GetTickCount returns milliseconds */
182
183 #define DEFAULT_CODEPAGE CP_ACP
184 #define USES_VTLINE_HACK
185
186 typedef HDC Context;
187
188 typedef unsigned int uint32; /* int is 32-bits on Win32 and Win64. */
189 #define PUTTY_UINT32_DEFINED
190
191 #ifndef NO_GSSAPI
192 /*
193  * GSS-API stuff
194  */
195 #define GSS_CC CALLBACK
196 /*
197 typedef struct Ssh_gss_buf {
198     size_t length;
199     char *value;
200 } Ssh_gss_buf;
201
202 #define SSH_GSS_EMPTY_BUF (Ssh_gss_buf) {0,NULL}
203 typedef void *Ssh_gss_name;
204 */
205 #endif
206
207 /*
208  * Window handles for the windows that can be running during a
209  * PuTTY session.
210  */
211 GLOBAL HWND hwnd;       /* the main terminal window */
212 GLOBAL HWND logbox;
213
214 /*
215  * The all-important instance handle.
216  */
217 GLOBAL HINSTANCE hinst;
218
219 /*
220  * Help file stuff in winhelp.c.
221  */
222 void init_help(void);
223 void shutdown_help(void);
224 int has_help(void);
225 void launch_help(HWND hwnd, const char *topic);
226 void quit_help(HWND hwnd);
227
228 /*
229  * The terminal and logging context are notionally local to the
230  * Windows front end, but they must be shared between window.c and
231  * windlg.c. Likewise the saved-sessions list.
232  */
233 GLOBAL Terminal *term;
234 GLOBAL void *logctx;
235
236 #define WM_NETEVENT  (WM_APP + 5)
237
238 /*
239  * On Windows, we send MA_2CLK as the only event marking the second
240  * press of a mouse button. Compare unix.h.
241  */
242 #define MULTICLICK_ONLY_EVENT 1
243
244 /*
245  * On Windows, data written to the clipboard must be NUL-terminated.
246  */
247 #define SELECTION_NUL_TERMINATED 1
248
249 /*
250  * On Windows, copying to the clipboard terminates lines with CRLF.
251  */
252 #define SEL_NL { 13, 10 }
253
254 /*
255  * sk_getxdmdata() does not exist under Windows (not that I
256  * couldn't write it if I wanted to, but I haven't bothered), so
257  * it's a macro which always returns NULL. With any luck this will
258  * cause the compiler to notice it can optimise away the
259  * implementation of XDM-AUTHORIZATION-1 in x11fwd.c :-)
260  */
261 #define sk_getxdmdata(socket, lenp) (NULL)
262
263 /*
264  * File-selector filter strings used in the config box. On Windows,
265  * these strings are of exactly the type needed to go in
266  * `lpstrFilter' in an OPENFILENAME structure.
267  */
268 #define FILTER_KEY_FILES ("PuTTY Private Key Files (*.ppk)\0*.ppk\0" \
269                               "All Files (*.*)\0*\0\0\0")
270 #define FILTER_WAVE_FILES ("Wave Files (*.wav)\0*.WAV\0" \
271                                "All Files (*.*)\0*\0\0\0")
272 #define FILTER_DYNLIB_FILES ("Dynamic Library Files (*.dll)\0*.dll\0" \
273                                  "All Files (*.*)\0*\0\0\0")
274
275 /*
276  * Exports from winnet.c.
277  */
278 extern int select_result(WPARAM, LPARAM);
279
280 /*
281  * winnet.c dynamically loads WinSock 2 or WinSock 1 depending on
282  * what it can get, which means any WinSock routines used outside
283  * that module must be exported from it as function pointers. So
284  * here they are.
285  */
286 DECL_WINDOWS_FUNCTION(GLOBAL, int, WSAAsyncSelect,
287                       (SOCKET, HWND, u_int, long));
288 DECL_WINDOWS_FUNCTION(GLOBAL, int, WSAEventSelect,
289                       (SOCKET, WSAEVENT, long));
290 DECL_WINDOWS_FUNCTION(GLOBAL, int, WSAGetLastError, (void));
291 DECL_WINDOWS_FUNCTION(GLOBAL, int, WSAEnumNetworkEvents,
292                       (SOCKET, WSAEVENT, LPWSANETWORKEVENTS));
293 #ifdef NEED_DECLARATION_OF_SELECT
294 /* This declaration is protected by an ifdef for the sake of building
295  * against winelib, in which you have to include winsock2.h before
296  * stdlib.h so that the right fd_set type gets defined. It would be a
297  * pain to do that throughout this codebase, so instead I arrange that
298  * only a modules actually needing to use (or define, or initialise)
299  * this function pointer will see its declaration, and _those_ modules
300  * - which will be Windows-specific anyway - can take more care. */
301 DECL_WINDOWS_FUNCTION(GLOBAL, int, select,
302                       (int, fd_set FAR *, fd_set FAR *,
303                        fd_set FAR *, const struct timeval FAR *));
304 #endif
305
306 extern int socket_writable(SOCKET skt);
307
308 extern void socket_reselect_all(void);
309
310 /*
311  * Exports from winctrls.c.
312  */
313
314 struct ctlpos {
315     HWND hwnd;
316     WPARAM font;
317     int dlu4inpix;
318     int ypos, width;
319     int xoff;
320     int boxystart, boxid;
321     char *boxtext;
322 };
323
324 /*
325  * Exports from winutils.c.
326  */
327 typedef struct filereq_tag filereq; /* cwd for file requester */
328 BOOL request_file(filereq *state, OPENFILENAME *of, int preserve, int save);
329 filereq *filereq_new(void);
330 void filereq_free(filereq *state);
331 int message_box(LPCTSTR text, LPCTSTR caption, DWORD style, DWORD helpctxid);
332 char *GetDlgItemText_alloc(HWND hwnd, int id);
333 void split_into_argv(char *, int *, char ***, char ***);
334
335 /*
336  * Private structure for prefslist state. Only in the header file
337  * so that we can delegate allocation to callers.
338  */
339 struct prefslist {
340     int listid, upbid, dnbid;
341     int srcitem;
342     int dummyitem;
343     int dragging;
344 };
345
346 /*
347  * This structure is passed to event handler functions as the `dlg'
348  * parameter, and hence is passed back to winctrls access functions.
349  */
350 struct dlgparam {
351     HWND hwnd;                         /* the hwnd of the dialog box */
352     struct winctrls *controltrees[8];  /* can have several of these */
353     int nctrltrees;
354     char *wintitle;                    /* title of actual window */
355     char *errtitle;                    /* title of error sub-messageboxes */
356     void *data;                        /* data to pass in refresh events */
357     union control *focused, *lastfocused; /* which ctrl has focus now/before */
358     char shortcuts[128];               /* track which shortcuts in use */
359     int coloursel_wanted;              /* has an event handler asked for
360                                         * a colour selector? */
361     struct { unsigned char r, g, b, ok; } coloursel_result;   /* 0-255 */
362     tree234 *privdata;                 /* stores per-control private data */
363     int ended, endresult;              /* has the dialog been ended? */
364     int fixed_pitch_fonts;             /* are we constrained to fixed fonts? */
365 };
366
367 /*
368  * Exports from winctrls.c.
369  */
370 void ctlposinit(struct ctlpos *cp, HWND hwnd,
371                 int leftborder, int rightborder, int topborder);
372 HWND doctl(struct ctlpos *cp, RECT r,
373            char *wclass, int wstyle, int exstyle, char *wtext, int wid);
374 void bartitle(struct ctlpos *cp, char *name, int id);
375 void beginbox(struct ctlpos *cp, char *name, int idbox);
376 void endbox(struct ctlpos *cp);
377 void editboxfw(struct ctlpos *cp, int password, char *text,
378                int staticid, int editid);
379 void radioline(struct ctlpos *cp, char *text, int id, int nacross, ...);
380 void bareradioline(struct ctlpos *cp, int nacross, ...);
381 void radiobig(struct ctlpos *cp, char *text, int id, ...);
382 void checkbox(struct ctlpos *cp, char *text, int id);
383 void statictext(struct ctlpos *cp, char *text, int lines, int id);
384 void staticbtn(struct ctlpos *cp, char *stext, int sid,
385                char *btext, int bid);
386 void static2btn(struct ctlpos *cp, char *stext, int sid,
387                 char *btext1, int bid1, char *btext2, int bid2);
388 void staticedit(struct ctlpos *cp, char *stext,
389                 int sid, int eid, int percentedit);
390 void staticddl(struct ctlpos *cp, char *stext,
391                int sid, int lid, int percentlist);
392 void combobox(struct ctlpos *cp, char *text, int staticid, int listid);
393 void staticpassedit(struct ctlpos *cp, char *stext,
394                     int sid, int eid, int percentedit);
395 void bigeditctrl(struct ctlpos *cp, char *stext,
396                  int sid, int eid, int lines);
397 void ersatztab(struct ctlpos *cp, char *stext, int sid, int lid, int s2id);
398 void editbutton(struct ctlpos *cp, char *stext, int sid,
399                 int eid, char *btext, int bid);
400 void sesssaver(struct ctlpos *cp, char *text,
401                int staticid, int editid, int listid, ...);
402 void envsetter(struct ctlpos *cp, char *stext, int sid,
403                char *e1stext, int e1sid, int e1id,
404                char *e2stext, int e2sid, int e2id,
405                int listid, char *b1text, int b1id, char *b2text, int b2id);
406 void charclass(struct ctlpos *cp, char *stext, int sid, int listid,
407                char *btext, int bid, int eid, char *s2text, int s2id);
408 void colouredit(struct ctlpos *cp, char *stext, int sid, int listid,
409                 char *btext, int bid, ...);
410 void prefslist(struct prefslist *hdl, struct ctlpos *cp, int lines,
411                char *stext, int sid, int listid, int upbid, int dnbid);
412 int handle_prefslist(struct prefslist *hdl,
413                      int *array, int maxmemb,
414                      int is_dlmsg, HWND hwnd,
415                      WPARAM wParam, LPARAM lParam);
416 void progressbar(struct ctlpos *cp, int id);
417 void fwdsetter(struct ctlpos *cp, int listid, char *stext, int sid,
418                char *e1stext, int e1sid, int e1id,
419                char *e2stext, int e2sid, int e2id,
420                char *btext, int bid,
421                char *r1text, int r1id, char *r2text, int r2id);
422
423 void dlg_auto_set_fixed_pitch_flag(void *dlg);
424 int dlg_get_fixed_pitch_flag(void *dlg);
425 void dlg_set_fixed_pitch_flag(void *dlg, int flag);
426
427 #define MAX_SHORTCUTS_PER_CTRL 16
428
429 /*
430  * This structure is what's stored for each `union control' in the
431  * portable-dialog interface.
432  */
433 struct winctrl {
434     union control *ctrl;
435     /*
436      * The control may have several components at the Windows
437      * level, with different dialog IDs. To avoid needing N
438      * separate platformsidectrl structures (which could be stored
439      * separately in a tree234 so that lookup by ID worked), we
440      * impose the constraint that those IDs must be in a contiguous
441      * block.
442      */
443     int base_id;
444     int num_ids;
445     /*
446      * Remember what keyboard shortcuts were used by this control,
447      * so that when we remove it again we can take them out of the
448      * list in the dlgparam.
449      */
450     char shortcuts[MAX_SHORTCUTS_PER_CTRL];
451     /*
452      * Some controls need a piece of allocated memory in which to
453      * store temporary data about the control.
454      */
455     void *data;
456 };
457 /*
458  * And this structure holds a set of the above, in two separate
459  * tree234s so that it can find an item by `union control' or by
460  * dialog ID.
461  */
462 struct winctrls {
463     tree234 *byctrl, *byid;
464 };
465 struct controlset;
466 struct controlbox;
467
468 void winctrl_init(struct winctrls *);
469 void winctrl_cleanup(struct winctrls *);
470 void winctrl_add(struct winctrls *, struct winctrl *);
471 void winctrl_remove(struct winctrls *, struct winctrl *);
472 struct winctrl *winctrl_findbyctrl(struct winctrls *, union control *);
473 struct winctrl *winctrl_findbyid(struct winctrls *, int);
474 struct winctrl *winctrl_findbyindex(struct winctrls *, int);
475 void winctrl_layout(struct dlgparam *dp, struct winctrls *wc,
476                     struct ctlpos *cp, struct controlset *s, int *id);
477 int winctrl_handle_command(struct dlgparam *dp, UINT msg,
478                            WPARAM wParam, LPARAM lParam);
479 void winctrl_rem_shortcuts(struct dlgparam *dp, struct winctrl *c);
480 int winctrl_context_help(struct dlgparam *dp, HWND hwnd, int id);
481
482 void dp_init(struct dlgparam *dp);
483 void dp_add_tree(struct dlgparam *dp, struct winctrls *tree);
484 void dp_cleanup(struct dlgparam *dp);
485
486 /*
487  * Exports from wincfg.c.
488  */
489 void win_setup_config_box(struct controlbox *b, HWND *hwndp, int has_help,
490                           int midsession, int protocol);
491
492 /*
493  * Exports from windlg.c.
494  */
495 void defuse_showwindow(void);
496 int do_config(void);
497 int do_reconfig(HWND, int);
498 void showeventlog(HWND);
499 void showabout(HWND);
500 void force_normal(HWND hwnd);
501 void modal_about_box(HWND hwnd);
502 void show_help(HWND hwnd);
503
504 /*
505  * Exports from winmisc.c.
506  */
507 extern OSVERSIONINFO osVersion;
508 void dll_hijacking_protection(void);
509 BOOL init_winver(void);
510 HMODULE load_system32_dll(const char *libname);
511 const char *win_strerror(int error);
512 void restrict_process_acl(void);
513 GLOBAL int restricted_acl;
514
515 /*
516  * Exports from sizetip.c.
517  */
518 void UpdateSizeTip(HWND src, int cx, int cy);
519 void EnableSizeTip(int bEnable);
520
521 /*
522  * Exports from unicode.c.
523  */
524 struct unicode_data;
525 void init_ucs(Conf *, struct unicode_data *);
526
527 /*
528  * Exports from winhandl.c.
529  */
530 #define HANDLE_FLAG_OVERLAPPED 1
531 #define HANDLE_FLAG_IGNOREEOF 2
532 #define HANDLE_FLAG_UNITBUFFER 4
533 struct handle;
534 typedef int (*handle_inputfn_t)(struct handle *h, void *data, int len);
535 typedef void (*handle_outputfn_t)(struct handle *h, int new_backlog);
536 struct handle *handle_input_new(HANDLE handle, handle_inputfn_t gotdata,
537                                 void *privdata, int flags);
538 struct handle *handle_output_new(HANDLE handle, handle_outputfn_t sentdata,
539                                  void *privdata, int flags);
540 int handle_write(struct handle *h, const void *data, int len);
541 void handle_write_eof(struct handle *h);
542 HANDLE *handle_get_events(int *nevents);
543 void handle_free(struct handle *h);
544 void handle_got_event(HANDLE event);
545 void handle_unthrottle(struct handle *h, int backlog);
546 int handle_backlog(struct handle *h);
547 void *handle_get_privdata(struct handle *h);
548 struct handle *handle_add_foreign_event(HANDLE event,
549                                         void (*callback)(void *), void *ctx);
550
551 /*
552  * winpgntc.c needs to schedule callbacks for asynchronous agent
553  * requests. This has to be done differently in GUI and console, so
554  * there's an exported function used for the purpose.
555  * 
556  * Also, we supply FLAG_SYNCAGENT to force agent requests to be
557  * synchronous in pscp and psftp.
558  */
559 void agent_schedule_callback(void (*callback)(void *, void *, int),
560                              void *callback_ctx, void *data, int len);
561 #define FLAG_SYNCAGENT 0x1000
562
563 /*
564  * Exports from winser.c.
565  */
566 extern Backend serial_backend;
567
568 /*
569  * Exports from winjump.c.
570  */
571 #define JUMPLIST_SUPPORTED             /* suppress #defines in putty.h */
572 void add_session_to_jumplist(const char * const sessionname);
573 void remove_session_from_jumplist(const char * const sessionname);
574 void clear_jumplist(void);
575 BOOL set_explicit_app_user_model_id();
576
577 /*
578  * Extra functions in winstore.c over and above the interface in
579  * storage.h.
580  *
581  * These functions manipulate the Registry section which mirrors the
582  * current Windows 7 jump list. (Because the real jump list storage is
583  * write-only, we need to keep another copy of whatever we put in it,
584  * so that we can put in a slightly modified version the next time.)
585  */
586
587 /* Adds a saved session to the registry jump list mirror. 'item' is a
588  * string naming a saved session. */
589 int add_to_jumplist_registry(const char *item);
590
591 /* Removes an item from the registry jump list mirror. */
592 int remove_from_jumplist_registry(const char *item);
593
594 /* Returns the current jump list entries from the registry. Caller
595  * must free the returned pointer, which points to a contiguous
596  * sequence of NUL-terminated strings in memory, terminated with an
597  * empty one. */
598 char *get_jumplist_registry_entries(void);
599
600 #endif