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