Win32 structure cleanup (WIP):
[reactos.git] / reactos / subsystems / win32 / win32k / main / dllmain.c
1 /*
2 * ReactOS W32 Subsystem
3 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /* $Id$
20 *
21 * Entry Point for win32k.sys
22 */
23
24 #include <w32k.h>
25 #include <include/napi.h>
26
27 #define NDEBUG
28 #include <debug.h>
29
30 PGDI_HANDLE_TABLE INTERNAL_CALL GDIOBJ_iAllocHandleTable(OUT PSECTION_OBJECT *SectionObject);
31 BOOL INTERNAL_CALL GDI_CleanupForProcess (struct _EPROCESS *Process);
32 /* FIXME */
33 PGDI_HANDLE_TABLE GdiHandleTable = NULL;
34 PSECTION_OBJECT GdiTableSection = NULL;
35
36 LIST_ENTRY GlobalDriverListHead;
37
38 HANDLE GlobalUserHeap = NULL;
39 PSECTION_OBJECT GlobalUserHeapSection = NULL;
40
41 PSERVERINFO gpsi = NULL; // Global User Server Information.
42
43 HSEMAPHORE hsemDriverMgmt = NULL;
44
45 extern ULONG_PTR Win32kSSDT[];
46 extern UCHAR Win32kSSPT[];
47 extern ULONG Win32kNumberOfSysCalls;
48
49 NTSTATUS
50 STDCALL
51 Win32kProcessCallback(struct _EPROCESS *Process,
52 BOOLEAN Create)
53 {
54 PW32PROCESS Win32Process;
55 DECLARE_RETURN(NTSTATUS);
56
57 DPRINT("Enter Win32kProcessCallback\n");
58 UserEnterExclusive();
59
60 /* Get the Win32 Process */
61 Win32Process = PsGetProcessWin32Process(Process);
62
63 /* Allocate one if needed */
64 if (!Win32Process)
65 {
66 /* FIXME - lock the process */
67 Win32Process = ExAllocatePoolWithTag(NonPagedPool,
68 sizeof(W32PROCESS),
69 TAG('W', '3', '2', 'p'));
70
71 if (Win32Process == NULL) RETURN( STATUS_NO_MEMORY);
72
73 RtlZeroMemory(Win32Process, sizeof(W32PROCESS));
74
75 PsSetProcessWin32Process(Process, Win32Process);
76 /* FIXME - unlock the process */
77 }
78
79 if (Create)
80 {
81 SIZE_T ViewSize = 0;
82 LARGE_INTEGER Offset;
83 PVOID UserBase = NULL;
84 NTSTATUS Status;
85 extern PSECTION_OBJECT GlobalUserHeapSection;
86 DPRINT("Creating W32 process PID:%d at IRQ level: %lu\n", Process->UniqueProcessId, KeGetCurrentIrql());
87
88 /* map the global heap into the process */
89 Offset.QuadPart = 0;
90 Status = MmMapViewOfSection(GlobalUserHeapSection,
91 PsGetCurrentProcess(),
92 &UserBase,
93 0,
94 0,
95 &Offset,
96 &ViewSize,
97 ViewUnmap,
98 SEC_NO_CHANGE,
99 PAGE_EXECUTE_READ); /* would prefer PAGE_READONLY, but thanks to RTL heaps... */
100 if (!NT_SUCCESS(Status))
101 {
102 DPRINT1("Failed to map the global heap! 0x%x\n", Status);
103 RETURN(Status);
104 }
105 Win32Process->HeapMappings.Next = NULL;
106 Win32Process->HeapMappings.KernelMapping = (PVOID)GlobalUserHeap;
107 Win32Process->HeapMappings.UserMapping = UserBase;
108 Win32Process->HeapMappings.Count = 1;
109
110 InitializeListHead(&Win32Process->ClassList);
111
112 InitializeListHead(&Win32Process->MenuListHead);
113
114 InitializeListHead(&Win32Process->PrivateFontListHead);
115 ExInitializeFastMutex(&Win32Process->PrivateFontListLock);
116
117 InitializeListHead(&Win32Process->DriverObjListHead);
118 ExInitializeFastMutex(&Win32Process->DriverObjListLock);
119
120 Win32Process->KeyboardLayout = W32kGetDefaultKeyLayout();
121
122 if(Process->Peb != NULL)
123 {
124 /* map the gdi handle table to user land */
125 Process->Peb->GdiSharedHandleTable = GDI_MapHandleTable(GdiTableSection, Process);
126 Process->Peb->GdiDCAttributeList = GDI_BATCH_LIMIT;
127 }
128
129 /* setup process flags */
130 Win32Process->Flags = 0;
131 }
132 else
133 {
134 DPRINT("Destroying W32 process PID:%d at IRQ level: %lu\n", Process->UniqueProcessId, KeGetCurrentIrql());
135 IntCleanupMenus(Process, Win32Process);
136 IntCleanupCurIcons(Process, Win32Process);
137 IntEngCleanupDriverObjs(Process, Win32Process);
138 CleanupMonitorImpl();
139
140 /* no process windows should exist at this point, or the function will assert! */
141 DestroyProcessClasses(Win32Process);
142
143 GDI_CleanupForProcess(Process);
144
145 co_IntGraphicsCheck(FALSE);
146
147 /*
148 * Deregister logon application automatically
149 */
150 if(LogonProcess == Win32Process)
151 {
152 LogonProcess = NULL;
153 }
154
155 if (Win32Process->ProcessInfo != NULL)
156 {
157 UserHeapFree(Win32Process->ProcessInfo);
158 Win32Process->ProcessInfo = NULL;
159 }
160 }
161
162 RETURN( STATUS_SUCCESS);
163
164 CLEANUP:
165 UserLeave();
166 DPRINT("Leave Win32kProcessCallback, ret=%i\n",_ret_);
167 END_CLEANUP;
168 }
169
170
171 NTSTATUS
172 STDCALL
173 Win32kThreadCallback(struct _ETHREAD *Thread,
174 PSW32THREADCALLOUTTYPE Type)
175 {
176 struct _EPROCESS *Process;
177 PTHREADINFO Win32Thread;
178 DECLARE_RETURN(NTSTATUS);
179
180 DPRINT("Enter Win32kThreadCallback\n");
181 UserEnterExclusive();
182
183 Process = Thread->ThreadsProcess;
184
185 /* Get the Win32 Thread */
186 Win32Thread = PsGetThreadWin32Thread(Thread);
187
188 /* Allocate one if needed */
189 if (!Win32Thread)
190 {
191 /* FIXME - lock the process */
192 Win32Thread = ExAllocatePoolWithTag(NonPagedPool,
193 sizeof(THREADINFO),
194 TAG('W', '3', '2', 't'));
195
196 if (Win32Thread == NULL) RETURN( STATUS_NO_MEMORY);
197
198 RtlZeroMemory(Win32Thread, sizeof(THREADINFO));
199
200 PsSetThreadWin32Thread(Thread, Win32Thread);
201 /* FIXME - unlock the process */
202 }
203 if (Type == PsW32ThreadCalloutInitialize)
204 {
205 HWINSTA hWinSta = NULL;
206 PTEB pTeb;
207 HDESK hDesk = NULL;
208 NTSTATUS Status;
209 PUNICODE_STRING DesktopPath;
210 PRTL_USER_PROCESS_PARAMETERS ProcessParams = (Process->Peb ? Process->Peb->ProcessParameters : NULL);
211
212 DPRINT("Creating W32 thread TID:%d at IRQ level: %lu\n", Thread->Cid.UniqueThread, KeGetCurrentIrql());
213
214 InitializeListHead(&Win32Thread->WindowListHead);
215 InitializeListHead(&Win32Thread->W32CallbackListHead);
216 InitializeListHead(&Win32Thread->PtiLink);
217
218 /*
219 * inherit the thread desktop and process window station (if not yet inherited) from the process startup
220 * info structure. See documentation of CreateProcess()
221 */
222 DesktopPath = (ProcessParams ? ((ProcessParams->DesktopInfo.Length > 0) ? &ProcessParams->DesktopInfo : NULL) : NULL);
223 Status = IntParseDesktopPath(Process,
224 DesktopPath,
225 &hWinSta,
226 &hDesk);
227 if(NT_SUCCESS(Status))
228 {
229 if(hWinSta != NULL)
230 {
231 if(Process != CsrProcess)
232 {
233 HWINSTA hProcessWinSta = (HWINSTA)InterlockedCompareExchangePointer((PVOID)&Process->Win32WindowStation, (PVOID)hWinSta, NULL);
234 if(hProcessWinSta != NULL)
235 {
236 /* our process is already assigned to a different window station, we don't need the handle anymore */
237 NtClose(hWinSta);
238 }
239 }
240 else
241 {
242 NtClose(hWinSta);
243 }
244 }
245
246 if (hDesk != NULL)
247 {
248 PDESKTOP DesktopObject;
249 Win32Thread->Desktop = NULL;
250 Status = ObReferenceObjectByHandle(hDesk,
251 0,
252 ExDesktopObjectType,
253 KernelMode,
254 (PVOID*)&DesktopObject,
255 NULL);
256 NtClose(hDesk);
257 if(NT_SUCCESS(Status))
258 {
259 if (!IntSetThreadDesktop(DesktopObject,
260 FALSE))
261 {
262 DPRINT1("Unable to set thread desktop\n");
263 }
264 }
265 else
266 {
267 DPRINT1("Unable to reference thread desktop handle 0x%x\n", hDesk);
268 }
269 }
270 }
271 Win32Thread->IsExiting = FALSE;
272 co_IntDestroyCaret(Win32Thread);
273 Win32Thread->ppi = PsGetCurrentProcessWin32Process();
274 pTeb = NtCurrentTeb();
275 if (pTeb)
276 Win32Thread->pClientInfo = (PCLIENTINFO)pTeb->Win32ClientInfo;
277 Win32Thread->MessageQueue = MsqCreateMessageQueue(Thread);
278 Win32Thread->KeyboardLayout = W32kGetDefaultKeyLayout();
279 if (Win32Thread->ThreadInfo)
280 Win32Thread->ThreadInfo->ClientThreadInfo.dwcPumpHook = 0;
281 }
282 else
283 {
284 PSINGLE_LIST_ENTRY e;
285
286 DPRINT("Destroying W32 thread TID:%d at IRQ level: %lu\n", Thread->Cid.UniqueThread, KeGetCurrentIrql());
287
288 Win32Thread->IsExiting = TRUE;
289 HOOK_DestroyThreadHooks(Thread);
290 UnregisterThreadHotKeys(Thread);
291 /* what if this co_ func crash in umode? what will clean us up then? */
292 co_DestroyThreadWindows(Thread);
293 IntBlockInput(Win32Thread, FALSE);
294 MsqDestroyMessageQueue(Win32Thread->MessageQueue);
295 IntCleanupThreadCallbacks(Win32Thread);
296
297 /* cleanup user object references stack */
298 e = PopEntryList(&Win32Thread->ReferencesList);
299 while (e)
300 {
301 PUSER_REFERENCE_ENTRY ref = CONTAINING_RECORD(e, USER_REFERENCE_ENTRY, Entry);
302 DPRINT("thread clean: remove reference obj 0x%x\n",ref->obj);
303 UserDereferenceObject(ref->obj);
304
305 e = PopEntryList(&Win32Thread->ReferencesList);
306 }
307
308 IntSetThreadDesktop(NULL,
309 TRUE);
310
311 if (Win32Thread->ThreadInfo != NULL)
312 {
313 UserHeapFree(Win32Thread->ThreadInfo);
314 Win32Thread->ThreadInfo = NULL;
315 }
316
317 PsSetThreadWin32Thread(Thread, NULL);
318 }
319
320 RETURN( STATUS_SUCCESS);
321
322 CLEANUP:
323 UserLeave();
324 DPRINT("Leave Win32kThreadCallback, ret=%i\n",_ret_);
325 END_CLEANUP;
326 }
327
328 /* Only used in ntuser/input.c KeyboardThreadMain(). If it's
329 not called there anymore, please delete */
330 NTSTATUS
331 Win32kInitWin32Thread(PETHREAD Thread)
332 {
333 PEPROCESS Process;
334
335 Process = Thread->ThreadsProcess;
336
337 if (Process->Win32Process == NULL)
338 {
339 /* FIXME - lock the process */
340 Process->Win32Process = ExAllocatePool(NonPagedPool, sizeof(W32PROCESS));
341
342 if (Process->Win32Process == NULL)
343 return STATUS_NO_MEMORY;
344
345 RtlZeroMemory(Process->Win32Process, sizeof(W32PROCESS));
346 /* FIXME - unlock the process */
347
348 Win32kProcessCallback(Process, TRUE);
349 }
350
351 if (Thread->Tcb.Win32Thread == NULL)
352 {
353 Thread->Tcb.Win32Thread = ExAllocatePool (NonPagedPool, sizeof(THREADINFO));
354 if (Thread->Tcb.Win32Thread == NULL)
355 return STATUS_NO_MEMORY;
356
357 RtlZeroMemory(Thread->Tcb.Win32Thread, sizeof(THREADINFO));
358
359 Win32kThreadCallback(Thread, PsW32ThreadCalloutInitialize);
360 }
361
362 return(STATUS_SUCCESS);
363 }
364
365
366 /*
367 * This definition doesn't work
368 */
369 NTSTATUS STDCALL
370 DriverEntry (
371 IN PDRIVER_OBJECT DriverObject,
372 IN PUNICODE_STRING RegistryPath)
373 {
374 NTSTATUS Status;
375 BOOLEAN Result;
376 WIN32_CALLOUTS_FPNS CalloutData = {0};
377 PVOID GlobalUserHeapBase = NULL;
378
379 /*
380 * Register user mode call interface
381 * (system service table index = 1)
382 */
383 Result = KeAddSystemServiceTable (Win32kSSDT,
384 NULL,
385 Win32kNumberOfSysCalls,
386 Win32kSSPT,
387 1);
388 if (Result == FALSE)
389 {
390 DPRINT1("Adding system services failed!\n");
391 return STATUS_UNSUCCESSFUL;
392 }
393
394 /*
395 * Register Object Manager Callbacks
396 */
397 CalloutData.WindowStationParseProcedure = IntWinStaObjectParse;
398 CalloutData.WindowStationDeleteProcedure = IntWinStaObjectDelete;
399 CalloutData.DesktopDeleteProcedure = IntDesktopObjectDelete;
400 CalloutData.ProcessCallout = Win32kProcessCallback;
401 CalloutData.ThreadCallout = Win32kThreadCallback;
402 CalloutData.BatchFlushRoutine = NtGdiFlushUserBatch;
403
404 /*
405 * Register our per-process and per-thread structures.
406 */
407 PsEstablishWin32Callouts((PWIN32_CALLOUTS_FPNS)&CalloutData);
408
409 GlobalUserHeap = UserCreateHeap(&GlobalUserHeapSection,
410 &GlobalUserHeapBase,
411 1 * 1024 * 1024); /* FIXME - 1 MB for now... */
412 if (GlobalUserHeap == NULL)
413 {
414 DPRINT1("Failed to initialize the global heap!\n");
415 return STATUS_UNSUCCESSFUL;
416 }
417
418 /* Initialize a list of loaded drivers in Win32 subsystem */
419 InitializeListHead(&GlobalDriverListHead);
420
421 if(!hsemDriverMgmt) hsemDriverMgmt = EngCreateSemaphore();
422
423 Status = InitUserImpl();
424 if (!NT_SUCCESS(Status))
425 {
426 DPRINT1("Failed to initialize user implementation!\n");
427 return STATUS_UNSUCCESSFUL;
428 }
429
430 Status = InitHotkeyImpl();
431 if (!NT_SUCCESS(Status))
432 {
433 DPRINT1("Failed to initialize hotkey implementation!\n");
434 return STATUS_UNSUCCESSFUL;
435 }
436
437 Status = InitWindowStationImpl();
438 if (!NT_SUCCESS(Status))
439 {
440 DPRINT1("Failed to initialize window station implementation!\n");
441 return STATUS_UNSUCCESSFUL;
442 }
443
444 Status = InitDesktopImpl();
445 if (!NT_SUCCESS(Status))
446 {
447 DPRINT1("Failed to initialize desktop implementation!\n");
448 return STATUS_UNSUCCESSFUL;
449 }
450
451 Status = InitWindowImpl();
452 if (!NT_SUCCESS(Status))
453 {
454 DPRINT1("Failed to initialize window implementation!\n");
455 return STATUS_UNSUCCESSFUL;
456 }
457
458 Status = InitMenuImpl();
459 if (!NT_SUCCESS(Status))
460 {
461 DPRINT1("Failed to initialize menu implementation!\n");
462 return STATUS_UNSUCCESSFUL;
463 }
464
465 Status = InitInputImpl();
466 if (!NT_SUCCESS(Status))
467 {
468 DPRINT1("Failed to initialize input implementation.\n");
469 return(Status);
470 }
471
472 Status = InitKeyboardImpl();
473 if (!NT_SUCCESS(Status))
474 {
475 DPRINT1("Failed to initialize keyboard implementation.\n");
476 return(Status);
477 }
478
479 Status = InitMonitorImpl();
480 if (!NT_SUCCESS(Status))
481 {
482 DbgPrint("Failed to initialize monitor implementation!\n");
483 return STATUS_UNSUCCESSFUL;
484 }
485
486 Status = MsqInitializeImpl();
487 if (!NT_SUCCESS(Status))
488 {
489 DPRINT1("Failed to initialize message queue implementation.\n");
490 return(Status);
491 }
492
493 Status = InitTimerImpl();
494 if (!NT_SUCCESS(Status))
495 {
496 DPRINT1("Failed to initialize timer implementation.\n");
497 return(Status);
498 }
499
500 Status = InitAcceleratorImpl();
501 if (!NT_SUCCESS(Status))
502 {
503 DPRINT1("Failed to initialize accelerator implementation.\n");
504 return(Status);
505 }
506
507 Status = InitGuiCheckImpl();
508 if (!NT_SUCCESS(Status))
509 {
510 DPRINT1("Failed to initialize GUI check implementation.\n");
511 return(Status);
512 }
513
514 GdiHandleTable = GDIOBJ_iAllocHandleTable(&GdiTableSection);
515 if (GdiHandleTable == NULL)
516 {
517 DPRINT1("Failed to initialize the GDI handle table.\n");
518 return STATUS_UNSUCCESSFUL;
519 }
520
521 Status = InitDcImpl();
522 if (!NT_SUCCESS(Status))
523 {
524 DPRINT1("Failed to initialize Device context implementation!\n");
525 return STATUS_UNSUCCESSFUL;
526 }
527
528 /* Initialize FreeType library */
529 if (! InitFontSupport())
530 {
531 DPRINT1("Unable to initialize font support\n");
532 return STATUS_UNSUCCESSFUL;
533 }
534
535 /* Create stock objects, ie. precreated objects commonly
536 used by win32 applications */
537 CreateStockObjects();
538 CreateSysColorObjects();
539
540 return STATUS_SUCCESS;
541 }
542
543 /* EOF */