1 /*
2  * COPYRIGHT:       See COPYING in the top level directory
3  * PROJECT:         ReactOS Console Server DLL
4  * FILE:            win32ss/user/winsrv/consrv/include/conio.h
5  * PURPOSE:         Public Console I/O Interface
6  * PROGRAMMERS:     G� van Geldorp
7  *                  Jeffrey Morlan
8  *                  Hermes Belusca-Maito (hermes.belusca@sfr.fr)
9  */
10 
11 #pragma once
12 
13 #include "rect.h"
14 
15 /* Default attributes */
16 #define DEFAULT_SCREEN_ATTRIB   (FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED)
17 #define DEFAULT_POPUP_ATTRIB    (FOREGROUND_BLUE | FOREGROUND_RED   | \
18                                  BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY)
19 
20 
21 /* Object type magic numbers */
22 typedef enum _CONSOLE_IO_OBJECT_TYPE
23 {
24     UNKNOWN         = 0x00, // --> Unknown object
25     TEXTMODE_BUFFER = 0x01, // --> Output-type object for text SBs
26     GRAPHICS_BUFFER = 0x02, // --> Output-type object for graphics SBs
27     SCREEN_BUFFER   = 0x03, // --> Any SB type
28     INPUT_BUFFER    = 0x04, // --> Input-type object
29     ANY_TYPE_BUFFER = 0x07, // --> Any IO object
30 } CONSOLE_IO_OBJECT_TYPE;
31 
32 typedef struct _CONSOLE_IO_OBJECT
33 {
34     CONSOLE_IO_OBJECT_TYPE Type;
35 
36     struct _CONSOLE* /* PCONSOLE */ Console;
37     LONG ReferenceCount;    /* Is incremented each time a console object gets referenced */
38 
39     LONG AccessRead, AccessWrite;
40     LONG ExclusiveRead, ExclusiveWrite;
41 } CONSOLE_IO_OBJECT, *PCONSOLE_IO_OBJECT;
42 
43 
44 /******************************************************************************\
45 |*                                                                            *|
46 |*     Abstract "class" for screen-buffers, be they text-mode or graphics     *|
47 |*                                                                            *|
48 \******************************************************************************/
49 
50 /*
51  * See conoutput.c for the implementation
52  */
53 
54 #define GetType(This)   (((PCONSOLE_SCREEN_BUFFER)(This))->Header.Type)
55 
56 typedef struct _CONSOLE_SCREEN_BUFFER
57 {
58     CONSOLE_IO_OBJECT Header;           /* Object header - MUST BE IN FIRST PLACE */
59 
60     LIST_ENTRY ListEntry;               /* Entry in console's list of buffers */
61 
62     COORD   ScreenBufferSize;           /* Size of this screen buffer. (Rows, Columns) for text-mode and (Width, Height) for graphics-mode */
63     COORD   ViewSize;                   /* Associated "view" (i.e. console) size */
64 
65     COORD   OldScreenBufferSize;        /* Old size of this screen buffer */
66     COORD   OldViewSize;                /* Old associated view size */
67 
68     COORD   ViewOrigin;                 /* Beginning offset for the actual display area */
69 
70 /***** Put that VV in TEXTMODE_SCREEN_BUFFER ?? *****/
71     COORD   CursorPosition;             /* Current cursor position */
72     BOOLEAN CursorBlinkOn;
73     BOOLEAN ForceCursorOff;
74 //  ULONG   CursorSize;
75     CONSOLE_CURSOR_INFO CursorInfo; // FIXME: Keep this member or not ??
76 /*********************************************/
77 
78     HPALETTE PaletteHandle;             /* Handle to the color palette associated to this buffer */
79     UINT     PaletteUsage;              /* The new use of the system palette. See SetSystemPaletteUse 'uUsage' parameter */
80 
81 //  USHORT   ScreenDefaultAttrib;       /* Default screen char attribute */
82 //  USHORT   PopupDefaultAttrib;        /* Default popup char attribute */
83     USHORT Mode;                        /* Output buffer modes */
84 } CONSOLE_SCREEN_BUFFER, *PCONSOLE_SCREEN_BUFFER;
85 
86 
87 
88 /******************************************************************************\
89 |*                                                                            *|
90 |*           Text-mode and graphics-mode screen-buffer "classes"              *|
91 |*                                                                            *|
92 \******************************************************************************/
93 
94 /*
95  * See text.c for the implementation
96  */
97 
98 /************************************************************************
99  * Screen buffer structure represents the win32 screen buffer object.   *
100  * Internally, the portion of the buffer being shown CAN loop past the  *
101  * bottom of the virtual buffer and wrap around to the top.  Win32 does *
102  * not do this.  I decided to do this because it eliminates the need to *
103  * do a massive memcpy() to scroll the contents of the buffer up to     *
104  * scroll the screen on output, instead I just shift down the position  *
105  * to be displayed, and let it wrap around to the top again.            *
106  * The VirtualY member keeps track of the top Y coord that win32        *
107  * clients THINK is currently being displayed, because they think that  *
108  * when the display reaches the bottom of the buffer and another line   *
109  * being printed causes another line to scroll down, that the buffer IS *
110  * memcpy()'s up, and the bottom of the buffer is still displayed, but  *
111  * internally, I just wrap back to the top of the buffer.               *
112  ************************************************************************/
113 
114 typedef struct _TEXTMODE_BUFFER_INFO
115 {
116     COORD   ScreenBufferSize;
117     COORD   ViewSize;
118     USHORT  ScreenAttrib;
119     USHORT  PopupAttrib;
120     ULONG   CursorSize;
121     BOOLEAN IsCursorVisible;
122 } TEXTMODE_BUFFER_INFO, *PTEXTMODE_BUFFER_INFO;
123 
124 typedef struct _TEXTMODE_SCREEN_BUFFER
125 {
126     CONSOLE_SCREEN_BUFFER;      /* Screen buffer base class - MUST BE IN FIRST PLACE */
127 
128     USHORT     VirtualY;        /* Top row of buffer being displayed, reported to callers */
129     PCHAR_INFO Buffer;          /* Pointer to UNICODE screen buffer (Buffer->Char.UnicodeChar only is valid, not Char.AsciiChar) */
130 
131     USHORT ScreenDefaultAttrib; /* Default screen char attribute */
132     USHORT PopupDefaultAttrib;  /* Default popup char attribute */
133 } TEXTMODE_SCREEN_BUFFER, *PTEXTMODE_SCREEN_BUFFER;
134 
135 
136 /*
137  * See graphics.c for the implementation
138  */
139 
140 typedef struct _GRAPHICS_BUFFER_INFO
141 {
142     CONSOLE_GRAPHICS_BUFFER_INFO Info;
143 } GRAPHICS_BUFFER_INFO, *PGRAPHICS_BUFFER_INFO;
144 
145 typedef struct _GRAPHICS_SCREEN_BUFFER
146 {
147     CONSOLE_SCREEN_BUFFER;          /* Screen buffer base class - MUST BE IN FIRST PLACE */
148 
149     ULONG   BitMapInfoLength;       /* Real size of the structure pointed by BitMapInfo */
150     LPBITMAPINFO BitMapInfo;        /* Information on the bitmap buffer */
151     ULONG   BitMapUsage;            /* See the uUsage parameter of GetDIBits */
152     HANDLE  hSection;               /* Handle to the memory shared section for the bitmap buffer */
153     PVOID   BitMap;                 /* Our bitmap buffer */
154 
155     PVOID   ClientBitMap;           /* A copy of the client view of our bitmap buffer */
156     HANDLE  Mutex;                  /* Our mutex, used to synchronize read / writes to the bitmap buffer */
157     HANDLE  ClientMutex;            /* A copy of the client handle to our mutex */
158     HANDLE  ClientProcess;          /* Handle to the client process who opened the buffer, to unmap the view */
159 } GRAPHICS_SCREEN_BUFFER, *PGRAPHICS_SCREEN_BUFFER;
160 
161 
162 
163 typedef struct _CONSOLE_INPUT_BUFFER
164 {
165     CONSOLE_IO_OBJECT Header;       /* Object header - MUST BE IN FIRST PLACE */
166 
167     ULONG       InputBufferSize;    /* Size of this input buffer (maximum number of events) -- UNUSED!! */
168     ULONG       NumberOfEvents;     /* Current number of events in the queue */
169     LIST_ENTRY  InputEvents;        /* Input events queue list head */
170     HANDLE      ActiveEvent;        /* Event set when an input event is added to the queue */
171 
172     USHORT      Mode;               /* Input buffer modes */
173 } CONSOLE_INPUT_BUFFER, *PCONSOLE_INPUT_BUFFER;
174 
175 
176 /*
177  * Structure used to hold console information
178  */
179 typedef struct _CONSOLE_INFO
180 {
181     ULONG   InputBufferSize;
182     COORD   ScreenBufferSize;
183     COORD   ConsoleSize;    /* The size of the console */
184 
185     ULONG   CursorSize;
186     BOOLEAN CursorBlinkOn;
187     BOOLEAN ForceCursorOff;
188 
189     USHORT  ScreenAttrib; // CHAR_INFO ScreenFillAttrib
190     USHORT  PopupAttrib;
191 
192     ULONG   CodePage;
193 
194 } CONSOLE_INFO, *PCONSOLE_INFO;
195 
196 typedef struct _TERMINAL TERMINAL, *PTERMINAL;
197 
198 typedef struct _TERMINAL_VTBL
199 {
200     /*
201      * Internal interface (functions called by the console server only)
202      */
203     NTSTATUS (NTAPI *InitTerminal)(IN OUT PTERMINAL This,
204                                    IN struct _CONSOLE* Console);
205     VOID (NTAPI *DeinitTerminal)(IN OUT PTERMINAL This);
206 
207 
208 
209 /************ Line discipline ***************/
210 
211     /* Interface used only for text-mode screen buffers */
212 
213     NTSTATUS (NTAPI *ReadStream)(IN OUT PTERMINAL This,
214                                  IN BOOLEAN Unicode,
215                                  /**PWCHAR Buffer,**/
216                                  OUT PVOID Buffer,
217                                  IN OUT PCONSOLE_READCONSOLE_CONTROL ReadControl,
218                                  IN PVOID Parameter OPTIONAL,
219                                  IN ULONG NumCharsToRead,
220                                  OUT PULONG NumCharsRead OPTIONAL);
221     NTSTATUS (NTAPI *WriteStream)(IN OUT PTERMINAL This,
222                                   PTEXTMODE_SCREEN_BUFFER Buff,
223                                   PWCHAR Buffer,
224                                   DWORD Length,
225                                   BOOL Attrib);
226 
227 /************ Line discipline ***************/
228 
229 
230 
231     /* Interface used for both text-mode and graphics screen buffers */
232     VOID (NTAPI *DrawRegion)(IN OUT PTERMINAL This,
233                              SMALL_RECT* Region);
234     BOOL (NTAPI *SetCursorInfo)(IN OUT PTERMINAL This,
235                                 PCONSOLE_SCREEN_BUFFER ScreenBuffer);
236     BOOL (NTAPI *SetScreenInfo)(IN OUT PTERMINAL This,
237                                 PCONSOLE_SCREEN_BUFFER ScreenBuffer,
238                                 SHORT OldCursorX,
239                                 SHORT OldCursorY);
240     VOID (NTAPI *ResizeTerminal)(IN OUT PTERMINAL This);
241     VOID (NTAPI *SetActiveScreenBuffer)(IN OUT PTERMINAL This);
242     VOID (NTAPI *ReleaseScreenBuffer)(IN OUT PTERMINAL This,
243                                       IN PCONSOLE_SCREEN_BUFFER ScreenBuffer);
244 
245     /*
246      * External interface (functions corresponding to the Console API)
247      */
248     VOID (NTAPI *GetLargestConsoleWindowSize)(IN OUT PTERMINAL This,
249                                               PCOORD pSize);
250     BOOL (NTAPI *SetPalette)(IN OUT PTERMINAL This,
251                              HPALETTE PaletteHandle,
252                              UINT PaletteUsage);
253     BOOL (NTAPI *SetCodePage)(IN OUT PTERMINAL This,
254                               UINT CodePage);
255     INT  (NTAPI *ShowMouseCursor)(IN OUT PTERMINAL This,
256                                   BOOL Show);
257 
258 #if 0 // Possible future terminal interface
259     BOOL (NTAPI *GetTerminalProperty)(IN OUT PTERMINAL This,
260                                       ULONG Flag,
261                                       PVOID Info,
262                                       ULONG Size);
263     BOOL (NTAPI *SetTerminalProperty)(IN OUT PTERMINAL This,
264                                       ULONG Flag,
265                                       PVOID Info /*,
266                                       ULONG Size */);
267 #endif
268 } TERMINAL_VTBL, *PTERMINAL_VTBL;
269 
270 struct _TERMINAL
271 {
272     PTERMINAL_VTBL Vtbl;        /* Virtual table */
273     struct _CONSOLE* Console;   /* Console to which the terminal is attached to */
274     PVOID Context;              /* Private context */
275 };
276 
277 /*
278  * WARNING: Change the state of the console ONLY when the console is locked !
279  */
280 typedef enum _CONSOLE_STATE
281 {
282     CONSOLE_INITIALIZING,   /* Console is initializing */
283     CONSOLE_RUNNING     ,   /* Console running */
284     CONSOLE_TERMINATING ,   /* Console about to be destroyed (but still not) */
285     CONSOLE_IN_DESTRUCTION  /* Console in destruction */
286 } CONSOLE_STATE, *PCONSOLE_STATE;
287 
288 typedef struct _CONSOLE
289 {
290 /******************************* Console Set-up *******************************/
291     LONG ReferenceCount;                    /* Is incremented each time a handle to something in the console (a screen-buffer or the input buffer of this console) gets referenced */
292     CRITICAL_SECTION Lock;
293 
294     CONSOLE_STATE State;                    /* State of the console */
295     TERMINAL TermIFace;                     /* Terminal-specific interface */
296 
297     BOOLEAN ConsolePaused;                  /* If TRUE, the console is paused */
298 
299 /******************************** Input buffer ********************************/
300     CONSOLE_INPUT_BUFFER InputBuffer;       /* Input buffer of the console */
301     UINT InputCodePage;
302 
303 /******************************* Screen buffers *******************************/
304     LIST_ENTRY BufferList;                  /* List of all screen buffers for this console */
305     PCONSOLE_SCREEN_BUFFER ActiveBuffer;    /* Pointer to currently active screen buffer */
306     UINT OutputCodePage;
307 
308 /****************************** Other properties ******************************/
309     COORD   ConsoleSize;                    /* The current size of the console, for text-mode only */
310     BOOLEAN FixedSize;                      /* TRUE if the console is of fixed size */
311     BOOLEAN IsCJK;                          /* TRUE if Chinese, Japanese or Korean (CJK) */
312 } CONSOLE, *PCONSOLE;
313 
314 /* console.c */
315 VOID NTAPI
316 ConDrvPause(PCONSOLE Console);
317 VOID NTAPI
318 ConDrvUnpause(PCONSOLE Console);
319 
320 #define GetConsoleInputBufferMode(Console)  \
321     (Console)->InputBuffer.Mode
322 
323 #define CON_SET_OUTPUT_CP(Console, CodePage)\
324 do { \
325     (Console)->OutputCodePage = (CodePage); \
326     (Console)->IsCJK = IsCJKCodePage((Console)->OutputCodePage); \
327 } while (0)
328 
329 
330 /* conoutput.c */
331 PCHAR_INFO ConioCoordToPointer(PTEXTMODE_SCREEN_BUFFER Buff, ULONG X, ULONG Y);
332 NTSTATUS ConioResizeBuffer(PCONSOLE Console,
333                            PTEXTMODE_SCREEN_BUFFER ScreenBuffer,
334                            COORD Size);
335 
336 /* wcwidth.c */
337 int mk_wcwidth_cjk(wchar_t ucs);
338 
339 // NOTE: The check against 0x80 is to avoid calling the helper function
340 // for characters that we already know are not full-width.
341 #define IS_FULL_WIDTH(wch)  \
342     (((USHORT)(wch) >= 0x0080) && (mk_wcwidth_cjk(wch) == 2))
343 
344 /* EOF */
345