1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 09:27:59 +00:00

windows/window.c: move (most) static vars into WinGuiSeat.

This is a piece of refactoring that's been overdue forever. In the
Unix front end, all the variables specific to a particular SSH session
live in a big 'inst' structure, and calls to any of the trait APIs
like Seat or TermWin can recover the right 'inst' from their input
context pointer. But the Windows frontend was written before that kind
of thing became habitual to me, and all its variables have been just
lying around at the top level of window.c, and most of those context
pointers were just ignored.

Now I've swept them all up and put them where they ought to be, in a
big context structure. This was made immeasurably easier by the very
nifty 'clang-rename' tool, which can rename a single variable in a C
source file, and find just the right set of identifiers to change even
if other variables in the file have the same name, even in sub-scopes.
So I started by clang-renaming all the top-level variables in question
to have names beginning with a prefix that didn't previously appear
anywhere in the code; checked that still worked; and then moved all
the declarations into the struct type and did a purely textual
search-and-replace of the prefix with 'wgs->'.

One potential benefit of this change is that it allows more than one
instance of the WinGuiSeat structure to exist in the same process. I
don't have any immediate plans to actually do that, but it's nice to
know it wouldn't be ruled out if we ever did need it.

But that's not the main reason I did it. The main reason is that I
recently looked at the output of a Windows build using clang -Wall,
and was horrified by the number of casual shadowings of
generically-named global variables like 'term' and 'conf' with local
variables of the same name. That seemed like a recipe for confusion,
and I decided the best way to disambiguate them all was to do this
refactoring that I'd wanted anyway for years.

A few uses of the global variables occurred in functions that didn't
have convenient access to the right WinGuiSeat via a callback
parameter of some kind. Those had to be treated specially. Most were
cleaned up in advance by the previous few commits; the remaining fixes
in this commit itself were in functions like modalfatalbox(),
nonfatal() and cleanup_exit(). The error reporting functions want the
terminal HWND to use as a MessageBox parameter; they also have the
side effect of un-hiding the mouse pointer in the terminal window, in
case it's hidden; and cleanup_exit wanted to free some resources
dangling off the WinGuiSeat.

For most of those cases, I've made a linked list of all currently live
WinGuiSeat structures, so that they can loop over _all_ live instances
(whether there's one as usual, none, or maybe more than one in
future). The parent window for non-connection-specific error messages
is found by simply picking one arbitrarily off the linked list (if
any); the cleanups are done by iterating over the _whole_ list.

The mouse-pointer unhiding is dealt with by simply allowing
show_mouseptr to take a _null_ WinGuiSeat pointer. The only thing it
needs the context for at all is to check whether pointer-hiding is
enabled in the session's Conf; so if we're _un_-hiding the pointer we
don't need to check, and can unhide it unconditionally.

The remaining global variables in window.c are the new linked list of
all WinGuiSeat structures; 'wm_mousewheel' and 'trust_icon' which
really should be global across all WinGuiSeats even if we were to have
more than one; and there's a static variable 'cursor_visible' in
show_mouseptr() which is likewise legitimately Seat-independent (since
it records the last value we passed to the Win32 API ShowCursor
function, and _that's_ global across the whole process state).

All of this should cause no functional change whatsoever.
This commit is contained in:
Simon Tatham 2022-09-13 09:00:19 +01:00
parent 4b3a8cbf61
commit f9e572595b
2 changed files with 1133 additions and 968 deletions

View File

@ -1,14 +1,142 @@
/*
* Small implementation of Seat and LogPolicy shared between window.c
* and dialog.c.
* Main state structure for an instance of the Windows PuTTY front
* end, containing all the PuTTY objects and all the Windows API
* resources like the window handle.
*/
typedef struct WinGuiSeat WinGuiSeat;
struct PopupMenu {
HMENU menu;
};
enum { SYSMENU, CTXMENU }; /* indices into popup_menus field */
#define FONT_NORMAL 0
#define FONT_BOLD 1
#define FONT_UNDERLINE 2
#define FONT_BOLDUND 3
#define FONT_WIDE 0x04
#define FONT_HIGH 0x08
#define FONT_NARROW 0x10
#define FONT_OEM 0x20
#define FONT_OEMBOLD 0x21
#define FONT_OEMUND 0x22
#define FONT_OEMBOLDUND 0x23
#define FONT_MAXNO 0x40
#define FONT_SHIFT 5
enum BoldMode {
BOLD_NONE, BOLD_SHADOW, BOLD_FONT
};
enum UnderlineMode {
UND_LINE, UND_FONT
};
struct _dpi_info {
POINT cur_dpi;
RECT new_wnd_rect;
};
/*
* Against the future possibility of having more than one of these
* in a process (and the current possibility of having zero), we
* keep a linked list of all live WinGuiSeats, so that cleanups
* can be done to any that exist.
*/
struct WinGuiSeatListNode {
struct WinGuiSeatListNode *next, *prev;
};
extern struct WinGuiSeatListNode wgslisthead; /* static end pointer */
struct WinGuiSeat {
HWND term_hwnd;
struct WinGuiSeatListNode wgslistnode;
Seat seat;
TermWin termwin;
LogPolicy logpolicy;
HWND term_hwnd;
int caret_x, caret_y;
int kbd_codepage;
Ldisc *ldisc;
Backend *backend;
cmdline_get_passwd_input_state cmdline_get_passwd_state;
struct unicode_data ucsdata;
bool session_closed;
bool reconfiguring;
const SessionSpecial *specials;
HMENU specials_menu;
int n_specials;
struct PopupMenu popup_menus[2];
HMENU savedsess_menu;
Conf *conf;
LogContext *logctx;
Terminal *term;
int cursor_type;
int vtmode;
HFONT fonts[FONT_MAXNO];
LOGFONT lfont;
bool fontflag[FONT_MAXNO];
enum BoldMode bold_font_mode;
bool bold_colours;
enum UnderlineMode und_mode;
int descent, font_strikethrough_y;
COLORREF colours[OSC4_NCOLOURS];
HPALETTE pal;
LPLOGPALETTE logpal;
bool tried_pal;
COLORREF colorref_modifier;
struct _dpi_info dpi_info;
int dbltime, lasttime, lastact;
Mouse_Button lastbtn;
bool send_raw_mouse;
int wheel_accumulator;
bool pointer_indicates_raw_mouse;
BusyStatus busy_status;
wchar_t *window_name, *icon_name;
int compose_state;
HDC wintw_hdc;
bool resizing;
long next_flash;
bool flashing;
};
extern const LogPolicyVtable win_gui_logpolicy_vt; /* in dialog.c */
static inline void wgs_link(WinGuiSeat *wgs)
{
wgs->wgslistnode.prev = wgslisthead.prev;
wgs->wgslistnode.next = &wgslisthead;
wgs->wgslistnode.prev->next = &wgs->wgslistnode;
wgs->wgslistnode.next->prev = &wgs->wgslistnode;
}
static inline void wgs_unlink(WinGuiSeat *wgs)
{
wgs->wgslistnode.prev->next = wgs->wgslistnode.next;
wgs->wgslistnode.next->prev = wgs->wgslistnode.prev;
}

File diff suppressed because it is too large Load Diff