Update.
[midnight-commander.git] / pc / cons_nt.c
blob77f42e4c338570aa42bab80860a389c8c9d02669
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>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 Note:
20 show_console_contents doesn't know how to write to its window
21 the rest works fine.
23 #include <config.h>
25 #include "../src/global.h"
26 #include "trace_nt.h"
28 int cons_saver_pid = 1;
30 #include "../src/tty.h"
31 #include "../src/util.h"
32 #include "../src/win.h"
33 #include "../src/cons.saver.h"
35 signed char console_flag = 1;
36 static HANDLE hSaved, hNew;
38 void show_console_contents (int starty, unsigned char begin_line,
39 unsigned char end_line)
41 COORD c0 = { 0, 0 };
42 COORD csize;
43 SMALL_RECT rect;
44 CHAR_INFO *pchar;
46 csize.X = COLS;
47 csize.Y = end_line-begin_line;
48 rect.Left = 0;
49 rect.Top = begin_line;
50 rect.Right = COLS;
51 rect.Bottom = end_line;
53 /* -- This code reads characters and attributes */
54 pchar = malloc (sizeof(CHAR_INFO) * (end_line-begin_line) * COLS);
55 /* Copy from one console to the curses virtual screen */
56 win32APICALL(ReadConsoleOutput (hSaved, pchar, csize, c0, &rect));
58 /* FIXME: this should've work,
59 but refresh() is called after this write :-( */
60 win32APICALL(WriteConsoleOutput (hNew, pchar, csize, c0, &rect));
62 free (pchar);
65 void handle_console (unsigned char action)
67 static SECURITY_ATTRIBUTES sa;
68 CONSOLE_SCREEN_BUFFER_INFO csbi;
70 switch (action){
71 case CONSOLE_INIT:
72 /* Save Standard handle */
73 hSaved = GetStdHandle (STD_OUTPUT_HANDLE);
75 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
76 sa.lpSecurityDescriptor = NULL;
77 /* Create a new console buffer */
78 sa.bInheritHandle = TRUE;
79 win32APICALL_HANDLE(hNew,
80 CreateConsoleScreenBuffer (GENERIC_WRITE | GENERIC_READ,
81 FILE_SHARE_READ | FILE_SHARE_WRITE, &sa,
82 CONSOLE_TEXTMODE_BUFFER, NULL));
83 win32APICALL(GetConsoleScreenBufferInfo(hSaved, &csbi));
84 win32APICALL(SetConsoleScreenBufferSize(hNew, csbi.dwSize));
86 /* that becomes standard handle */
87 win32APICALL(SetConsoleActiveScreenBuffer(hNew));
88 win32APICALL(SetConsoleMode(hNew, ENABLE_PROCESSED_INPUT));
89 win32APICALL(SetStdHandle(STD_OUTPUT_HANDLE, hNew));
90 break;
92 case CONSOLE_DONE:
93 win32APICALL(CloseHandle (hNew));
94 break;
96 case CONSOLE_SAVE:
97 /* Current = our standard handle */
98 win32APICALL(SetConsoleActiveScreenBuffer (hNew));
99 win32APICALL(SetStdHandle (STD_OUTPUT_HANDLE, hNew));
100 break;
102 case CONSOLE_RESTORE:
103 /* Put saved (shell) screen buffer */
104 win32APICALL(SetConsoleActiveScreenBuffer (hSaved));
105 win32APICALL(SetStdHandle (STD_OUTPUT_HANDLE, hSaved));
106 break;
107 default:
108 win32Trace(("Invalid action code %d sent to handle_console", action));