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