+/*
+ * Attempt to save the screen contents. PDCurses does this if
+ * PDC_RESTORE_SCREEN is set, giving the same visual appearance on restoration
+ * as if the library had allocated a console buffer.
+ */
+static bool
+save_original_screen(TERMINAL_CONTROL_BLOCK * TCB)
+{
+ bool result = FALSE;
+ Properties *p = PropOf(TCB);
+ COORD bufferSize;
+ COORD bufferCoord;
+ SMALL_RECT readRegion;
+ size_t want;
+
+ bufferSize.X = p->SBI.dwSize.X;
+ bufferSize.Y = p->SBI.dwSize.Y;
+ want = (size_t) (bufferSize.X * bufferSize.Y);
+
+ if ((p->save_screen = malloc(want * sizeof(CHAR_INFO))) != 0) {
+ bufferCoord.X = bufferCoord.Y = 0;
+
+ readRegion.Top = 0;
+ readRegion.Left = 0;
+ readRegion.Bottom = (SHORT) (bufferSize.Y - 1);
+ readRegion.Right = (SHORT) (bufferSize.X - 1);
+
+ T(("... reading console buffer %dx%d into %d,%d - %d,%d at %d,%d",
+ bufferSize.Y, bufferSize.X,
+ readRegion.Top,
+ readRegion.Left,
+ readRegion.Bottom,
+ readRegion.Right,
+ bufferCoord.Y,
+ bufferCoord.X));
+
+ if (ReadConsoleOutput(TCB->hdl,
+ p->save_screen,
+ bufferSize,
+ bufferCoord,
+ &readRegion)) {
+ result = TRUE;
+ } else {
+ T((" error %#lx", (unsigned long) GetLastError()));
+ FreeAndNull(p->save_screen);
+
+ bufferSize.X = (SHORT) (p->SBI.srWindow.Right
+ - p->SBI.srWindow.Left + 1);
+ bufferSize.Y = (SHORT) (p->SBI.srWindow.Bottom
+ - p->SBI.srWindow.Top + 1);
+ want = (size_t) (bufferSize.X * bufferSize.Y);
+
+ if ((p->save_screen = malloc(want * sizeof(CHAR_INFO))) != 0) {
+ bufferCoord.X = bufferCoord.Y = 0;
+
+ readRegion.Top = p->SBI.srWindow.Top;
+ readRegion.Left = p->SBI.srWindow.Left;
+ readRegion.Bottom = p->SBI.srWindow.Bottom;
+ readRegion.Right = p->SBI.srWindow.Right;
+
+ T(("... reading console window %dx%d into %d,%d - %d,%d at %d,%d",
+ bufferSize.Y, bufferSize.X,
+ readRegion.Top,
+ readRegion.Left,
+ readRegion.Bottom,
+ readRegion.Right,
+ bufferCoord.Y,
+ bufferCoord.X));
+
+ if (ReadConsoleOutput(TCB->hdl,
+ p->save_screen,
+ bufferSize,
+ bufferCoord,
+ &readRegion)) {
+ result = TRUE;
+ } else {
+ T((" error %#lx", (unsigned long) GetLastError()));
+ }
+ }
+ }
+ }
+
+ T(("... save original screen contents %s", result ? "ok" : "err"));
+ return result;
+}
+