cons.handler.nt.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* Client interface for General purpose Win32 console save/restore server
  2. Having the same interface as its Linux counterpart:
  3. Copyright (C) 1994 Janne Kukonlehto <jtklehto@stekt.oulu.fi>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. Note:
  16. show_console_contents doesn't know how to write to its window
  17. the rest works fine.
  18. */
  19. #include <config.h>
  20. #ifndef _OS_NT
  21. #error This file is for Win32 operating systems.
  22. #else
  23. #include <windows.h>
  24. #include "util.debug.h"
  25. int cons_saver_pid = 1;
  26. #include "tty.h"
  27. #include "util.h"
  28. #include "win.h"
  29. #include "cons.saver.h"
  30. signed char console_flag = 1;
  31. static HANDLE hSaved, hNew;
  32. void show_console_contents (int starty, unsigned char begin_line, unsigned char end_line)
  33. {
  34. DWORD dw;
  35. COORD c0 = { 0, 0 };
  36. COORD csize;
  37. SMALL_RECT rect;
  38. CHAR_INFO *pchar;
  39. csize.X = COLS;
  40. csize.Y = end_line-begin_line;
  41. rect.Left = 0;
  42. rect.Top = begin_line;
  43. rect.Right = COLS;
  44. rect.Bottom = end_line;
  45. // -- This code reads characters and attributes
  46. pchar = malloc (sizeof(CHAR_INFO) * (end_line-begin_line) * COLS);
  47. // Copy from one console to the curses virtual screen
  48. win32APICALL(ReadConsoleOutput (hSaved, pchar, csize, c0, &rect));
  49. // FIXME: this should've work, but refresh() is called after this write :-(
  50. win32APICALL(WriteConsoleOutput (hNew, pchar, csize, c0, &rect));
  51. #ifdef USE_NCURSES
  52. // Here we read only characters so that we can printw to stdscr.
  53. // pchar = malloc (sizeof(TCHAR) * (end_line-begin_line) * COLS);
  54. // Copy from one console to the curses virtual screen
  55. // ReadConsoleOutputCharacter (hSaved, pchar, (end_line-begin_line) * COLS, c0, &dw);
  56. // mvprintw(0, begin_line, "%.*s", (end_line-begin_line) * COLS, pchar);
  57. #else
  58. //#error show_console_contents not written for S-Lang
  59. #endif
  60. free (pchar);
  61. }
  62. void handle_console (unsigned char action)
  63. {
  64. static SECURITY_ATTRIBUTES sa;
  65. COORD c;
  66. CONSOLE_SCREEN_BUFFER_INFO csbi;
  67. long lMode;
  68. switch (action){
  69. case CONSOLE_INIT: // Init
  70. hSaved = GetStdHandle (STD_OUTPUT_HANDLE); // Save Standard handle
  71. sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  72. sa.lpSecurityDescriptor = NULL;
  73. sa.bInheritHandle = TRUE; // Create a new console buffer
  74. win32APICALL_HANDLE(hNew, /* = */ CreateConsoleScreenBuffer (GENERIC_WRITE | GENERIC_READ,
  75. FILE_SHARE_READ | FILE_SHARE_WRITE, &sa,
  76. CONSOLE_TEXTMODE_BUFFER, NULL));
  77. win32APICALL(GetConsoleScreenBufferInfo(hSaved, &csbi)); // ... with same size
  78. win32APICALL(SetConsoleScreenBufferSize(hNew, csbi.dwSize));
  79. win32APICALL(SetConsoleActiveScreenBuffer(hNew)); // ... that becomes standard handle
  80. win32APICALL(SetConsoleMode(hNew, ENABLE_PROCESSED_INPUT));
  81. win32APICALL(SetStdHandle(STD_OUTPUT_HANDLE, hNew));
  82. break;
  83. case CONSOLE_DONE: // Clean Up
  84. win32APICALL(CloseHandle (hNew));
  85. break;
  86. case CONSOLE_SAVE: // Save
  87. win32APICALL(SetConsoleActiveScreenBuffer (hNew)); // Current = our standard handle
  88. win32APICALL(SetStdHandle (STD_OUTPUT_HANDLE, hNew));
  89. break;
  90. case CONSOLE_RESTORE: // Restore
  91. win32APICALL(SetConsoleActiveScreenBuffer (hSaved)); // Put saved (shell) screen buffer
  92. win32APICALL(SetStdHandle (STD_OUTPUT_HANDLE, hSaved));
  93. break;
  94. default:
  95. win32Trace(("Invalid action code %d received in handle_console", action));
  96. }
  97. }
  98. #endif // !_OS_NT