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