6e615dadace60e64c9a023499c20550add5c481a
[reactos.git] / win32ss / user / win32csr / dllmain.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: subsys/csrss/win32csr/dllmain.c
5 * PURPOSE: Initialization
6 * PROGRAMMERS: Dmitry Philippov (shedon@mail.ru)
7 */
8
9 /* INCLUDES ******************************************************************/
10 #define NDEBUG
11 #include "w32csr.h"
12 #include <debug.h>
13
14
15 /* GLOBALS *******************************************************************/
16
17 HANDLE Win32CsrApiHeap;
18 HINSTANCE Win32CsrDllHandle = NULL;
19
20 PCSR_API_ROUTINE Win32CsrApiDefinitions[] =
21 {
22 CsrGetHandle,
23 CsrGetHandle,
24 CsrCloseHandle,
25 CsrVerifyHandle,
26 CsrDuplicateHandle,
27 CsrGetInputWaitHandle,
28 CsrFillOutputChar,
29 CsrReadInputEvent,
30 CsrWriteConsoleOutputChar,
31 CsrWriteConsoleOutputAttrib,
32 CsrFillOutputAttrib,
33 CsrSetTextAttrib,
34 CsrWriteConsoleOutput,
35 CsrFlushInputBuffer,
36 CsrReadConsoleOutputChar,
37 CsrReadConsoleOutputAttrib,
38 CsrExitReactos,
39 CsrHardwareStateProperty,
40 CsrCreateDesktop,
41 CsrShowDesktop,
42 CsrHideDesktop,
43 CsrSetLogonNotifyWindow,
44 CsrRegisterLogonProcess,
45 CsrGenerateCtrlEvent,
46 };
47
48
49 /*
50 static CSRSS_API_DEFINITION Win32CsrApiDefinitions[] =
51 {
52 CSRSS_DEFINE_API(GET_INPUT_HANDLE, CsrGetHandle),
53 CSRSS_DEFINE_API(GET_OUTPUT_HANDLE, CsrGetHandle),
54 CSRSS_DEFINE_API(CLOSE_HANDLE, CsrCloseHandle),
55 CSRSS_DEFINE_API(VERIFY_HANDLE, CsrVerifyHandle),
56 CSRSS_DEFINE_API(DUPLICATE_HANDLE, CsrDuplicateHandle),
57 CSRSS_DEFINE_API(GET_INPUT_WAIT_HANDLE, CsrGetInputWaitHandle),
58 CSRSS_DEFINE_API(WRITE_CONSOLE, CsrWriteConsole),
59 CSRSS_DEFINE_API(READ_CONSOLE, CsrReadConsole),
60 CSRSS_DEFINE_API(ALLOC_CONSOLE, CsrAllocConsole),
61 CSRSS_DEFINE_API(FREE_CONSOLE, CsrFreeConsole),
62 CSRSS_DEFINE_API(SCREEN_BUFFER_INFO, CsrGetScreenBufferInfo),
63 CSRSS_DEFINE_API(SET_CURSOR, CsrSetCursor),
64 CSRSS_DEFINE_API(FILL_OUTPUT, CsrFillOutputChar),
65 CSRSS_DEFINE_API(READ_INPUT, CsrReadInputEvent),
66 CSRSS_DEFINE_API(WRITE_CONSOLE_OUTPUT_CHAR, CsrWriteConsoleOutputChar),
67 CSRSS_DEFINE_API(WRITE_CONSOLE_OUTPUT_ATTRIB, CsrWriteConsoleOutputAttrib),
68 CSRSS_DEFINE_API(FILL_OUTPUT_ATTRIB, CsrFillOutputAttrib),
69 CSRSS_DEFINE_API(GET_CURSOR_INFO, CsrGetCursorInfo),
70 CSRSS_DEFINE_API(SET_CURSOR_INFO, CsrSetCursorInfo),
71 CSRSS_DEFINE_API(SET_ATTRIB, CsrSetTextAttrib),
72 CSRSS_DEFINE_API(GET_CONSOLE_MODE, CsrGetConsoleMode),
73 CSRSS_DEFINE_API(SET_CONSOLE_MODE, CsrSetConsoleMode),
74 CSRSS_DEFINE_API(CREATE_SCREEN_BUFFER, CsrCreateScreenBuffer),
75 CSRSS_DEFINE_API(SET_SCREEN_BUFFER, CsrSetScreenBuffer),
76 CSRSS_DEFINE_API(SET_TITLE, CsrSetTitle),
77 CSRSS_DEFINE_API(GET_TITLE, CsrGetTitle),
78 CSRSS_DEFINE_API(WRITE_CONSOLE_OUTPUT, CsrWriteConsoleOutput),
79 CSRSS_DEFINE_API(FLUSH_INPUT_BUFFER, CsrFlushInputBuffer),
80 CSRSS_DEFINE_API(SCROLL_CONSOLE_SCREEN_BUFFER, CsrScrollConsoleScreenBuffer),
81 CSRSS_DEFINE_API(READ_CONSOLE_OUTPUT_CHAR, CsrReadConsoleOutputChar),
82 CSRSS_DEFINE_API(READ_CONSOLE_OUTPUT_ATTRIB, CsrReadConsoleOutputAttrib),
83 CSRSS_DEFINE_API(GET_NUM_INPUT_EVENTS, CsrGetNumberOfConsoleInputEvents),
84 CSRSS_DEFINE_API(EXIT_REACTOS, CsrExitReactos),
85 CSRSS_DEFINE_API(PEEK_CONSOLE_INPUT, CsrPeekConsoleInput),
86 CSRSS_DEFINE_API(READ_CONSOLE_OUTPUT, CsrReadConsoleOutput),
87 CSRSS_DEFINE_API(WRITE_CONSOLE_INPUT, CsrWriteConsoleInput),
88 CSRSS_DEFINE_API(SETGET_CONSOLE_HW_STATE, CsrHardwareStateProperty),
89 CSRSS_DEFINE_API(GET_CONSOLE_WINDOW, CsrGetConsoleWindow),
90 CSRSS_DEFINE_API(CREATE_DESKTOP, CsrCreateDesktop),
91 CSRSS_DEFINE_API(SHOW_DESKTOP, CsrShowDesktop),
92 CSRSS_DEFINE_API(HIDE_DESKTOP, CsrHideDesktop),
93 CSRSS_DEFINE_API(SET_CONSOLE_ICON, CsrSetConsoleIcon),
94 CSRSS_DEFINE_API(SET_LOGON_NOTIFY_WINDOW, CsrSetLogonNotifyWindow),
95 CSRSS_DEFINE_API(REGISTER_LOGON_PROCESS, CsrRegisterLogonProcess),
96 CSRSS_DEFINE_API(GET_CONSOLE_CP, CsrGetConsoleCodePage),
97 CSRSS_DEFINE_API(SET_CONSOLE_CP, CsrSetConsoleCodePage),
98 CSRSS_DEFINE_API(GET_CONSOLE_OUTPUT_CP, CsrGetConsoleOutputCodePage),
99 CSRSS_DEFINE_API(SET_CONSOLE_OUTPUT_CP, CsrSetConsoleOutputCodePage),
100 CSRSS_DEFINE_API(GET_PROCESS_LIST, CsrGetProcessList),
101 CSRSS_DEFINE_API(ADD_CONSOLE_ALIAS, CsrAddConsoleAlias),
102 CSRSS_DEFINE_API(GET_CONSOLE_ALIAS, CsrGetConsoleAlias),
103 CSRSS_DEFINE_API(GET_ALL_CONSOLE_ALIASES, CsrGetAllConsoleAliases),
104 CSRSS_DEFINE_API(GET_ALL_CONSOLE_ALIASES_LENGTH, CsrGetAllConsoleAliasesLength),
105 CSRSS_DEFINE_API(GET_CONSOLE_ALIASES_EXES, CsrGetConsoleAliasesExes),
106 CSRSS_DEFINE_API(GET_CONSOLE_ALIASES_EXES_LENGTH, CsrGetConsoleAliasesExesLength),
107 CSRSS_DEFINE_API(GENERATE_CTRL_EVENT, CsrGenerateCtrlEvent),
108 CSRSS_DEFINE_API(SET_SCREEN_BUFFER_SIZE, CsrSetScreenBufferSize),
109 CSRSS_DEFINE_API(GET_CONSOLE_SELECTION_INFO, CsrGetConsoleSelectionInfo),
110 CSRSS_DEFINE_API(GET_COMMAND_HISTORY_LENGTH, CsrGetCommandHistoryLength),
111 CSRSS_DEFINE_API(GET_COMMAND_HISTORY, CsrGetCommandHistory),
112 CSRSS_DEFINE_API(EXPUNGE_COMMAND_HISTORY, CsrExpungeCommandHistory),
113 CSRSS_DEFINE_API(SET_HISTORY_NUMBER_COMMANDS, CsrSetHistoryNumberCommands),
114 CSRSS_DEFINE_API(GET_HISTORY_INFO, CsrGetHistoryInfo),
115 CSRSS_DEFINE_API(SET_HISTORY_INFO, CsrSetHistoryInfo),
116 { 0, 0, NULL }
117 };
118 */
119
120 static HHOOK hhk = NULL;
121
122 /* FUNCTIONS *****************************************************************/
123
124 LRESULT
125 CALLBACK
126 KeyboardHookProc(
127 int nCode,
128 WPARAM wParam,
129 LPARAM lParam)
130 {
131 return CallNextHookEx(hhk, nCode, wParam, lParam);
132 }
133
134 ULONG
135 InitializeVideoAddressSpace(VOID)
136 {
137 OBJECT_ATTRIBUTES ObjectAttributes;
138 UNICODE_STRING PhysMemName = RTL_CONSTANT_STRING(L"\\Device\\PhysicalMemory");
139 NTSTATUS Status;
140 HANDLE PhysMemHandle;
141 PVOID BaseAddress;
142 LARGE_INTEGER Offset;
143 SIZE_T ViewSize;
144 CHAR IVTAndBda[1024+256];
145
146 /* Free the 1MB pre-reserved region. In reality, ReactOS should simply support us mapping the view into the reserved area, but it doesn't. */
147 BaseAddress = 0;
148 ViewSize = 1024 * 1024;
149 Status = ZwFreeVirtualMemory(NtCurrentProcess(),
150 &BaseAddress,
151 &ViewSize,
152 MEM_RELEASE);
153 if (!NT_SUCCESS(Status))
154 {
155 DPRINT1("Couldn't unmap reserved memory (%x)\n", Status);
156 return 0;
157 }
158
159 /* Open the physical memory section */
160 InitializeObjectAttributes(&ObjectAttributes,
161 &PhysMemName,
162 0,
163 NULL,
164 NULL);
165 Status = ZwOpenSection(&PhysMemHandle,
166 SECTION_ALL_ACCESS,
167 &ObjectAttributes);
168 if (!NT_SUCCESS(Status))
169 {
170 DPRINT1("Couldn't open \\Device\\PhysicalMemory\n");
171 return 0;
172 }
173
174 /* Map the BIOS and device registers into the address space */
175 Offset.QuadPart = 0xa0000;
176 ViewSize = 0x100000 - 0xa0000;
177 BaseAddress = (PVOID)0xa0000;
178 Status = ZwMapViewOfSection(PhysMemHandle,
179 NtCurrentProcess(),
180 &BaseAddress,
181 0,
182 ViewSize,
183 &Offset,
184 &ViewSize,
185 ViewUnmap,
186 0,
187 PAGE_EXECUTE_READWRITE);
188 if (!NT_SUCCESS(Status))
189 {
190 DPRINT1("Couldn't map physical memory (%x)\n", Status);
191 ZwClose(PhysMemHandle);
192 return 0;
193 }
194
195 /* Close physical memory section handle */
196 ZwClose(PhysMemHandle);
197
198 if (BaseAddress != (PVOID)0xa0000)
199 {
200 DPRINT1("Couldn't map physical memory at the right address (was %x)\n",
201 BaseAddress);
202 return 0;
203 }
204
205 /* Allocate some low memory to use for the non-BIOS
206 * parts of the v86 mode address space
207 */
208 BaseAddress = (PVOID)0x1;
209 ViewSize = 0xa0000 - 0x1000;
210 Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
211 &BaseAddress,
212 0,
213 &ViewSize,
214 MEM_RESERVE | MEM_COMMIT,
215 PAGE_EXECUTE_READWRITE);
216 if (!NT_SUCCESS(Status))
217 {
218 DPRINT1("Failed to allocate virtual memory (Status %x)\n", Status);
219 return 0;
220 }
221 if (BaseAddress != (PVOID)0x0)
222 {
223 DPRINT1("Failed to allocate virtual memory at right address (was %x)\n",
224 BaseAddress);
225 return 0;
226 }
227
228 /* Get the real mode IVT and BDA from the kernel */
229 Status = NtVdmControl(VdmInitialize, IVTAndBda);
230 if (!NT_SUCCESS(Status))
231 {
232 DPRINT1("NtVdmControl failed (status %x)\n", Status);
233 return 0;
234 }
235
236 /* Return success */
237 return 1;
238 }
239
240 /**********************************************************************
241 * CsrpInitVideo/3
242 *
243 * TODO: we need a virtual device for sessions other than
244 * TODO: the console one
245 */
246 NTSTATUS
247 CsrpInitVideo (VOID)
248 {
249 OBJECT_ATTRIBUTES ObjectAttributes;
250 UNICODE_STRING DeviceName = RTL_CONSTANT_STRING(L"\\??\\DISPLAY1");
251 IO_STATUS_BLOCK Iosb;
252 HANDLE VideoHandle = (HANDLE) 0;
253 NTSTATUS Status = STATUS_SUCCESS;
254
255 DPRINT("CSR: %s called\n", __FUNCTION__);
256
257 InitializeVideoAddressSpace();
258
259 InitializeObjectAttributes(&ObjectAttributes,
260 &DeviceName,
261 0,
262 NULL,
263 NULL);
264 Status = NtOpenFile(&VideoHandle,
265 FILE_ALL_ACCESS,
266 &ObjectAttributes,
267 &Iosb,
268 0,
269 0);
270 if (NT_SUCCESS(Status))
271 {
272 NtClose(VideoHandle);
273 }
274 return Status;
275 }
276
277 BOOL WINAPI
278 DllMain(HANDLE hDll,
279 DWORD dwReason,
280 LPVOID lpReserved)
281 {
282 if (DLL_PROCESS_ATTACH == dwReason)
283 {
284 Win32CsrDllHandle = hDll;
285 //
286 // HACK HACK HACK ReactOS to BOOT! Initialization BUG ALERT! See bug 5655.
287 //
288 hhk = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProc, NULL, 0);
289 // BUG ALERT! BUG ALERT! BUG ALERT! BUG ALERT! BUG ALERT! BUG ALERT! BUG ALERT!
290 // BUG ALERT! BUG ALERT! BUG ALERT! BUG ALERT! BUG ALERT! BUG ALERT! BUG ALERT!
291 // BUG ALERT! BUG ALERT! BUG ALERT! BUG ALERT! BUG ALERT! BUG ALERT! BUG ALERT!
292 }
293
294 return TRUE;
295 }
296
297 NTSTATUS FASTCALL
298 Win32CsrEnumProcesses(CSRSS_ENUM_PROCESS_PROC EnumProc,
299 PVOID Context)
300 {
301 return CsrEnumProcesses(EnumProc, Context);
302 }
303
304 VOID
305 WINAPI
306 PrivateCsrssManualGuiCheck(LONG Check)
307 {
308 NtUserCallOneParam(Check, ONEPARAM_ROUTINE_CSRSS_GUICHECK);
309 }
310
311 DWORD
312 WINAPI
313 CreateSystemThreads(PVOID pParam)
314 {
315 NtUserCallOneParam((DWORD)pParam, ONEPARAM_ROUTINE_CREATESYSTEMTHREADS);
316 DPRINT1("This thread should not terminate!\n");
317 return 0;
318 }
319
320 CSR_SERVER_DLL_INIT(Win32CsrInitialization)
321 {
322 HANDLE ServerThread;
323 CLIENT_ID ClientId;
324 NTSTATUS Status;
325
326 Win32CsrApiHeap = RtlGetProcessHeap();
327
328 CsrpInitVideo(); // Must go into winsrv.dll
329
330 NtUserInitialize(0, NULL, NULL);
331
332 PrivateCsrssManualGuiCheck(0);
333
334 /* Setup the DLL Object */
335 LoadedServerDll->ApiBase = BASESRV_FIRST_API_NUMBER;
336 LoadedServerDll->HighestApiSupported = BasepMaxApiNumber;
337 LoadedServerDll->DispatchTable = Win32CsrApiDefinitions;
338 // LoadedServerDll->ValidTable = BaseServerApiServerValidTable;
339 // LoadedServerDll->NameTable = BaseServerApiNameTable;
340 // LoadedServerDll->SizeOfProcessData = 0;
341 // LoadedServerDll->ConnectCallback = NULL;
342 // LoadedServerDll->DisconnectCallback = NULL;
343 LoadedServerDll->HardErrorCallback = Win32CsrHardError;
344 LoadedServerDll->NewProcessCallback = Win32CsrDuplicateHandleTable;
345 LoadedServerDll->DisconnectCallback = Win32CsrReleaseConsole;
346
347 /* Start Raw Input Threads */
348 Status = RtlCreateUserThread(NtCurrentProcess(), NULL, TRUE, 0, 0, 0, (PTHREAD_START_ROUTINE)CreateSystemThreads, (PVOID)0, &ServerThread, &ClientId);
349 if (NT_SUCCESS(Status))
350 {
351 NtResumeThread(ServerThread, NULL);
352 NtClose(ServerThread);
353 }
354 else
355 DPRINT1("Cannot start Raw Input Thread!\n");
356
357 return STATUS_SUCCESS;
358 }
359
360 /* EOF */