Ensure our network layer is properly cleaned up before PuTTY exits.
[u/mdw/putty] / window.c
index cbcba52..a779dbc 100644 (file)
--- a/window.c
+++ b/window.c
 #endif
 #endif
 
+#ifndef NO_MULTIMON
 #if WINVER < 0x0500
 #define COMPILE_MULTIMON_STUBS
 #include <multimon.h>
 #endif
+#endif
 
 #include <stdio.h>
 #include <stdlib.h>
 #define VK_PROCESSKEY 0xE5
 #endif
 
-/* Needed for mouse wheel support and not defined in earlier SDKs. */
+/* Mouse wheel support. */
 #ifndef WM_MOUSEWHEEL
-#define WM_MOUSEWHEEL 0x020A
+#define WM_MOUSEWHEEL 0x020A          /* not defined in earlier SDKs */
+#endif
+#ifndef WHEEL_DELTA
+#define WHEEL_DELTA 120
 #endif
 
 static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
@@ -101,9 +106,12 @@ static WPARAM pend_netevent_wParam = 0;
 static LPARAM pend_netevent_lParam = 0;
 static void enact_pending_netevent(void);
 static void flash_window(int mode);
+static void sys_cursor_update(void);
 
 static time_t last_movement = 0;
 
+static int caret_x = -1, caret_y = -1;
+
 #define FONT_NORMAL 0
 #define FONT_BOLD 1
 #define FONT_UNDERLINE 2
@@ -151,6 +159,8 @@ static char *window_name, *icon_name;
 
 static int compose_state = 0;
 
+static int wsa_started = 0;
+
 static OSVERSIONINFO osVersion;
 
 static UINT wm_mousewheel = WM_MOUSEWHEEL;
@@ -184,6 +194,7 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
        WSACleanup();
        return 1;
     }
+    wsa_started = 1;
     /* WISHLIST: maybe allow config tweaking even if winsock not present? */
     sk_init();
 
@@ -717,7 +728,8 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
                timer_id = 0;
            }
            HideCaret(hwnd);
-           term_out();
+           if (GetCapture() != hwnd)
+               term_out();
            term_update();
            ShowCaret(hwnd);
 
@@ -744,6 +756,14 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
        }
     }
 
+    cleanup_exit(msg.wParam);
+}
+
+/*
+ * Clean up and exit.
+ */
+void cleanup_exit(int code)
+{
     /*
      * Clean up.
      */
@@ -751,7 +771,9 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
     sfree(logpal);
     if (pal)
        DeleteObject(pal);
-    WSACleanup();
+    sk_cleanup();
+    if (wsa_started)
+       WSACleanup();
 
     if (cfg.protocol == PROT_SSH) {
        random_save_seed();
@@ -760,7 +782,7 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
 #endif
     }
 
-    return msg.wParam;
+    exit(code);
 }
 
 /*
@@ -1511,7 +1533,8 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
       case WM_TIMER:
        if (pending_netevent)
            enact_pending_netevent();
-       term_out();
+       if (GetCapture() != hwnd)
+           term_out();
        noise_regular();
        HideCaret(hwnd);
        term_update();
@@ -1931,7 +1954,8 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
         */
        noise_ultralight(lParam);
 
-       if (wParam & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON)) {
+       if (wParam & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON) &&
+           GetCapture() == hwnd) {
            Mouse_Button b;
            if (wParam & MK_LBUTTON)
                b = MBT_LEFT;
@@ -2036,6 +2060,7 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
        show_mouseptr(1);
        has_focus = FALSE;
        DestroyCaret();
+       caret_x = caret_y = -1;        /* ensure caret is replaced next time */
        term_out();
        term_update();
        break;
@@ -2152,6 +2177,9 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
       case WM_FULLSCR_ON_MAX:
        fullscr_on_max = TRUE;
        break;
+      case WM_MOVE:
+       sys_cursor_update();
+       break;
       case WM_SIZE:
 #ifdef RDB_DEBUG_PATCH
        debug((27, "WM_SIZE %s (%d,%d)",
@@ -2235,6 +2263,7 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
                    reset_window(0);
            }
        }
+       sys_cursor_update();
        return 0;
       case WM_VSCROLL:
        switch (LOWORD(wParam)) {
@@ -2344,6 +2373,7 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
        /* wParam == Font number */
        /* lParam == Locale */
        set_input_locale((HKL)lParam);
+       sys_cursor_update();
        break;
       case WM_IME_NOTIFY:
        if(wParam == IMN_SETOPENSTATUS) {
@@ -2417,7 +2447,7 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
            return TRUE;
        }
       default:
-       if (message == wm_mousewheel) {
+       if (message == wm_mousewheel || message == WM_MOUSEWHEEL) {
            int shift_pressed=0, control_pressed=0, alt_pressed=0;
 
            if (message == WM_MOUSEWHEEL) {
@@ -2479,13 +2509,35 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
  */
 void sys_cursor(int x, int y)
 {
+    int cx, cy;
+
+    if (!has_focus) return;
+
+    /*
+     * Avoid gratuitously re-updating the cursor position and IMM
+     * window if there's no actual change required.
+     */
+    cx = x * font_width + offset_width;
+    cy = y * font_height + offset_height;
+    if (cx == caret_x && cy == caret_y)
+       return;
+    caret_x = cx;
+    caret_y = cy;
+
+    sys_cursor_update();
+}
+
+static void sys_cursor_update(void)
+{
     COMPOSITIONFORM cf;
     HIMC hIMC;
 
     if (!has_focus) return;
-    
-    SetCaretPos(x * font_width + offset_width,
-               y * font_height + offset_height);
+
+    if (caret_x < 0 || caret_y < 0)
+       return;
+
+    SetCaretPos(caret_x, caret_y);
 
     /* IMM calls on Win98 and beyond only */
     if(osVersion.dwPlatformId == VER_PLATFORM_WIN32s) return; /* 3.11 */
@@ -2496,8 +2548,8 @@ void sys_cursor(int x, int y)
     /* we should have the IMM functions */
     hIMC = ImmGetContext(hwnd);
     cf.dwStyle = CFS_POINT;
-    cf.ptCurrentPos.x = x * font_width + offset_width;
-    cf.ptCurrentPos.y = y * font_height + offset_height;
+    cf.ptCurrentPos.x = caret_x;
+    cf.ptCurrentPos.y = caret_y;
     ImmSetCompositionWindow(hIMC, &cf);
 
     ImmReleaseContext(hwnd, hIMC);
@@ -4084,7 +4136,7 @@ void fatalbox(char *fmt, ...)
     vsprintf(stuff, fmt, ap);
     va_end(ap);
     MessageBox(hwnd, stuff, "PuTTY Fatal Error", MB_ICONERROR | MB_OK);
-    exit(1);
+    cleanup_exit(1);
 }
 
 /*
@@ -4183,6 +4235,11 @@ void set_iconic(int iconic)
  */
 void move_window(int x, int y)
 {
+    if (cfg.resize_action == RESIZE_DISABLED || 
+        cfg.resize_action == RESIZE_FONT ||
+       IsZoomed(hwnd))
+       return;
+
     SetWindowPos(hwnd, NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
 }