xref: /reactos/win32ss/user/winsrv/consrv/settings.c (revision b8dd046e)
1 /*
2  * COPYRIGHT:       See COPYING in the top level directory
3  * PROJECT:         ReactOS Console Server DLL
4  * FILE:            win32ss/user/winsrv/consrv/settings.c
5  * PURPOSE:         Console settings management
6  * PROGRAMMERS:     Johannes Anderwald
7  *                  Hermes Belusca-Maito (hermes.belusca@sfr.fr)
8  */
9 
10 /* INCLUDES *******************************************************************/
11 
12 #include "consrv.h"
13 #include "history.h"
14 #include "../concfg/font.h"
15 
16 #define NDEBUG
17 #include <debug.h>
18 
19 /* GLOBALS ********************************************************************/
20 
21 extern const COLORREF s_Colors[16];
22 
23 
24 /* FUNCTIONS ******************************************************************/
25 
26 NTSTATUS NTAPI
27 ConDrvChangeScreenBufferAttributes(IN PCONSOLE Console,
28                                    IN PTEXTMODE_SCREEN_BUFFER Buffer,
29                                    IN USHORT NewScreenAttrib,
30                                    IN USHORT NewPopupAttrib);
31 /*
32  * NOTE: This function explicitely references Console->ActiveBuffer.
33  * It is possible that it should go into some frontend...
34  */
35 VOID
36 ConSrvApplyUserSettings(IN PCONSOLE Console,
37                         IN PCONSOLE_STATE_INFO ConsoleInfo)
38 {
39     PCONSOLE_SCREEN_BUFFER ActiveBuffer = Console->ActiveBuffer;
40 
41     /*
42      * Apply terminal-edition settings:
43      * - QuickEdit and Insert modes,
44      * - History settings.
45      */
46     Console->QuickEdit  = !!ConsoleInfo->QuickEdit;
47     Console->InsertMode = !!ConsoleInfo->InsertMode;
48     /// Console->InputBufferSize = 0;
49     HistoryReshapeAllBuffers(Console,
50                              ConsoleInfo->HistoryBufferSize,
51                              ConsoleInfo->NumberOfHistoryBuffers,
52                              ConsoleInfo->HistoryNoDup);
53 
54     /* Copy the new console palette */
55     // FIXME: Possible buffer overflow if s_colors is bigger than ConsoleInfo->ColorTable.
56     RtlCopyMemory(Console->Colors, ConsoleInfo->ColorTable, sizeof(s_Colors));
57 
58     /* Apply cursor size */
59     ActiveBuffer->CursorInfo.bVisible = (ConsoleInfo->CursorSize != 0);
60     ActiveBuffer->CursorInfo.dwSize   = min(max(ConsoleInfo->CursorSize, 0), 100);
61 
62     /* Update the code page */
63     if ((Console->OutputCodePage != ConsoleInfo->CodePage) &&
64         IsValidCodePage(ConsoleInfo->CodePage))
65     {
66         Console->InputCodePage = Console->OutputCodePage = ConsoleInfo->CodePage;
67         // ConDrvSetConsoleCP(Console, ConsoleInfo->CodePage, TRUE);    // Output
68         // ConDrvSetConsoleCP(Console, ConsoleInfo->CodePage, FALSE);   // Input
69 
70         Console->IsCJK = IsCJKCodePage(Console->OutputCodePage);
71     }
72 
73     // FIXME: Check ConsoleInfo->WindowSize with respect to
74     // TermGetLargestConsoleWindowSize(...).
75 
76     if (GetType(ActiveBuffer) == TEXTMODE_BUFFER)
77     {
78         /* Resize its active screen-buffer */
79         PTEXTMODE_SCREEN_BUFFER Buffer = (PTEXTMODE_SCREEN_BUFFER)ActiveBuffer;
80         COORD BufSize = ConsoleInfo->ScreenBufferSize;
81 
82         if (Console->FixedSize)
83         {
84             /*
85              * The console is in fixed-size mode, so we cannot resize anything
86              * at the moment. However, keep those settings somewhere so that
87              * we can try to set them up when we will be allowed to do so.
88              */
89             if (ConsoleInfo->WindowSize.X != ActiveBuffer->OldViewSize.X ||
90                 ConsoleInfo->WindowSize.Y != ActiveBuffer->OldViewSize.Y)
91             {
92                 ActiveBuffer->OldViewSize = ConsoleInfo->WindowSize;
93             }
94 
95             /* The buffer size is not allowed to be smaller than the view size */
96             if (BufSize.X >= ActiveBuffer->OldViewSize.X && BufSize.Y >= ActiveBuffer->OldViewSize.Y)
97             {
98                 if (BufSize.X != ActiveBuffer->OldScreenBufferSize.X ||
99                     BufSize.Y != ActiveBuffer->OldScreenBufferSize.Y)
100                 {
101                     /*
102                      * The console is in fixed-size mode, so we cannot resize anything
103                      * at the moment. However, keep those settings somewhere so that
104                      * we can try to set them up when we will be allowed to do so.
105                      */
106                     ActiveBuffer->OldScreenBufferSize = BufSize;
107                 }
108             }
109         }
110         else
111         {
112             BOOL SizeChanged = FALSE;
113 
114             /* Resize the console */
115             if (ConsoleInfo->WindowSize.X != ActiveBuffer->ViewSize.X ||
116                 ConsoleInfo->WindowSize.Y != ActiveBuffer->ViewSize.Y)
117             {
118                 ActiveBuffer->ViewSize = ConsoleInfo->WindowSize;
119                 SizeChanged = TRUE;
120             }
121 
122             /* Resize the screen-buffer */
123             if (BufSize.X != ActiveBuffer->ScreenBufferSize.X ||
124                 BufSize.Y != ActiveBuffer->ScreenBufferSize.Y)
125             {
126                 if (NT_SUCCESS(ConioResizeBuffer(Console, Buffer, BufSize)))
127                     SizeChanged = TRUE;
128             }
129 
130             if (SizeChanged) TermResizeTerminal(Console);
131         }
132 
133         /* Apply foreground and background colors for both screen and popup */
134         ConDrvChangeScreenBufferAttributes(Console,
135                                            Buffer,
136                                            ConsoleInfo->ScreenAttributes,
137                                            ConsoleInfo->PopupAttributes);
138     }
139     else // if (GetType(ActiveBuffer) == GRAPHICS_BUFFER)
140     {
141         /*
142          * In any case we do NOT modify the size of the graphics screen-buffer.
143          * We just allow resizing the view only if the new size is smaller
144          * than the older one.
145          */
146         if (Console->FixedSize)
147         {
148             /*
149              * The console is in fixed-size mode, so we cannot resize anything
150              * at the moment. However, keep those settings somewhere so that
151              * we can try to set them up when we will be allowed to do so.
152              */
153             if (ConsoleInfo->WindowSize.X <= ActiveBuffer->ViewSize.X ||
154                 ConsoleInfo->WindowSize.Y <= ActiveBuffer->ViewSize.Y)
155             {
156                 ActiveBuffer->OldViewSize = ConsoleInfo->WindowSize;
157             }
158         }
159         else
160         {
161             /* Resize the view if its size is bigger than the specified size */
162             if (ConsoleInfo->WindowSize.X <= ActiveBuffer->ViewSize.X ||
163                 ConsoleInfo->WindowSize.Y <= ActiveBuffer->ViewSize.Y)
164             {
165                 ActiveBuffer->ViewSize = ConsoleInfo->WindowSize;
166                 // SizeChanged = TRUE;
167             }
168         }
169     }
170 }
171 
172 /* EOF */
173