[Wine32k|User32]
[reactos.git] / reactos / subsystems / win32 / win32k / main / dllmain.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * PURPOSE: Driver entry and initialization of win32k
5 * FILE: subsystems/win32/win32k/main/main.c
6 * PROGRAMER:
7 */
8
9 #include <win32k.h>
10 #include <include/napi.h>
11
12 #define NDEBUG
13 #include <debug.h>
14
15 HANDLE hModuleWin;
16
17 PGDI_HANDLE_TABLE INTERNAL_CALL GDIOBJ_iAllocHandleTable(OUT PSECTION_OBJECT *SectionObject);
18 BOOL INTERNAL_CALL GDI_CleanupForProcess (struct _EPROCESS *Process);
19
20 HANDLE GlobalUserHeap = NULL;
21 PSECTION_OBJECT GlobalUserHeapSection = NULL;
22
23 PSERVERINFO gpsi = NULL; // Global User Server Information.
24
25 SHORT gusLanguageID;
26
27 extern ULONG_PTR Win32kSSDT[];
28 extern UCHAR Win32kSSPT[];
29 extern ULONG Win32kNumberOfSysCalls;
30
31 NTSTATUS
32 APIENTRY
33 Win32kProcessCallback(struct _EPROCESS *Process,
34 BOOLEAN Create)
35 {
36 PPROCESSINFO Win32Process;
37 DECLARE_RETURN(NTSTATUS);
38
39 DPRINT("Enter Win32kProcessCallback\n");
40 UserEnterExclusive();
41
42 /* Get the Win32 Process */
43 Win32Process = PsGetProcessWin32Process(Process);
44
45 /* Allocate one if needed */
46 if (!Win32Process)
47 {
48 /* FIXME - lock the process */
49 Win32Process = ExAllocatePoolWithTag(NonPagedPool,
50 sizeof(PROCESSINFO),
51 'p23W');
52
53 if (Win32Process == NULL) RETURN( STATUS_NO_MEMORY);
54
55 RtlZeroMemory(Win32Process, sizeof(PROCESSINFO));
56
57 PsSetProcessWin32Process(Process, Win32Process);
58 /* FIXME - unlock the process */
59 }
60
61 if (Create)
62 {
63 SIZE_T ViewSize = 0;
64 LARGE_INTEGER Offset;
65 PVOID UserBase = NULL;
66 NTSTATUS Status;
67 extern PSECTION_OBJECT GlobalUserHeapSection;
68 DPRINT("Creating W32 process PID:%d at IRQ level: %lu\n", Process->UniqueProcessId, KeGetCurrentIrql());
69
70 /* map the global heap into the process */
71 Offset.QuadPart = 0;
72 Status = MmMapViewOfSection(GlobalUserHeapSection,
73 PsGetCurrentProcess(),
74 &UserBase,
75 0,
76 0,
77 &Offset,
78 &ViewSize,
79 ViewUnmap,
80 SEC_NO_CHANGE,
81 PAGE_EXECUTE_READ); /* would prefer PAGE_READONLY, but thanks to RTL heaps... */
82 if (!NT_SUCCESS(Status))
83 {
84 DPRINT1("Failed to map the global heap! 0x%x\n", Status);
85 RETURN(Status);
86 }
87 Win32Process->HeapMappings.Next = NULL;
88 Win32Process->HeapMappings.KernelMapping = (PVOID)GlobalUserHeap;
89 Win32Process->HeapMappings.UserMapping = UserBase;
90 Win32Process->HeapMappings.Count = 1;
91
92 InitializeListHead(&Win32Process->ClassList);
93
94 InitializeListHead(&Win32Process->MenuListHead);
95
96 InitializeListHead(&Win32Process->GDIBrushAttrFreeList);
97 InitializeListHead(&Win32Process->GDIDcAttrFreeList);
98
99 InitializeListHead(&Win32Process->PrivateFontListHead);
100 ExInitializeFastMutex(&Win32Process->PrivateFontListLock);
101
102 InitializeListHead(&Win32Process->DriverObjListHead);
103 ExInitializeFastMutex(&Win32Process->DriverObjListLock);
104
105 Win32Process->KeyboardLayout = W32kGetDefaultKeyLayout();
106 EngCreateEvent((PEVENT *)&Win32Process->InputIdleEvent);
107 KeInitializeEvent(Win32Process->InputIdleEvent, NotificationEvent, FALSE);
108
109 if(Process->Peb != NULL)
110 {
111 /* map the gdi handle table to user land */
112 Process->Peb->GdiSharedHandleTable = GDI_MapHandleTable(GdiTableSection, Process);
113 Process->Peb->GdiDCAttributeList = GDI_BATCH_LIMIT;
114 }
115
116 Win32Process->peProcess = Process;
117 /* setup process flags */
118 Win32Process->W32PF_flags = 0;
119 }
120 else
121 {
122 DPRINT("Destroying W32 process PID:%d at IRQ level: %lu\n", Process->UniqueProcessId, KeGetCurrentIrql());
123 Win32Process->W32PF_flags |= W32PF_TERMINATED;
124 if (Win32Process->InputIdleEvent)
125 {
126 EngFreeMem((PVOID)Win32Process->InputIdleEvent);
127 Win32Process->InputIdleEvent = NULL;
128 }
129
130 IntCleanupMenus(Process, Win32Process);
131 IntCleanupCurIcons(Process, Win32Process);
132 CleanupMonitorImpl();
133
134 /* no process windows should exist at this point, or the function will assert! */
135 DestroyProcessClasses(Win32Process);
136
137 GDI_CleanupForProcess(Process);
138
139 co_IntGraphicsCheck(FALSE);
140
141 /*
142 * Deregister logon application automatically
143 */
144 if(LogonProcess == Win32Process)
145 {
146 LogonProcess = NULL;
147 }
148 }
149
150 RETURN( STATUS_SUCCESS);
151
152 CLEANUP:
153 UserLeave();
154 DPRINT("Leave Win32kProcessCallback, ret=%i\n",_ret_);
155 END_CLEANUP;
156 }
157
158
159 NTSTATUS
160 APIENTRY
161 Win32kThreadCallback(struct _ETHREAD *Thread,
162 PSW32THREADCALLOUTTYPE Type)
163 {
164 struct _EPROCESS *Process;
165 PTHREADINFO Win32Thread;
166 int i;
167 DECLARE_RETURN(NTSTATUS);
168
169 DPRINT("Enter Win32kThreadCallback\n");
170 UserEnterExclusive();
171
172 Process = Thread->ThreadsProcess;
173
174 /* Get the Win32 Thread */
175 Win32Thread = PsGetThreadWin32Thread(Thread);
176
177 /* Allocate one if needed */
178 if (!Win32Thread)
179 {
180 /* FIXME - lock the process */
181 Win32Thread = ExAllocatePoolWithTag(NonPagedPool,
182 sizeof(THREADINFO),
183 't23W');
184
185 if (Win32Thread == NULL) RETURN( STATUS_NO_MEMORY);
186
187 RtlZeroMemory(Win32Thread, sizeof(THREADINFO));
188
189 PsSetThreadWin32Thread(Thread, Win32Thread);
190 /* FIXME - unlock the process */
191 }
192 if (Type == PsW32ThreadCalloutInitialize)
193 {
194 HWINSTA hWinSta = NULL;
195 PTEB pTeb;
196 HDESK hDesk = NULL;
197 NTSTATUS Status;
198 PUNICODE_STRING DesktopPath;
199 PRTL_USER_PROCESS_PARAMETERS ProcessParams = (Process->Peb ? Process->Peb->ProcessParameters : NULL);
200
201 DPRINT("Creating W32 thread TID:%d at IRQ level: %lu\n", Thread->Cid.UniqueThread, KeGetCurrentIrql());
202
203 InitializeListHead(&Win32Thread->WindowListHead);
204 InitializeListHead(&Win32Thread->W32CallbackListHead);
205 InitializeListHead(&Win32Thread->PtiLink);
206 for (i = 0; i < NB_HOOKS; i++)
207 {
208 InitializeListHead(&Win32Thread->aphkStart[i]);
209 }
210
211 /*
212 * inherit the thread desktop and process window station (if not yet inherited) from the process startup
213 * info structure. See documentation of CreateProcess()
214 */
215 DesktopPath = (ProcessParams ? ((ProcessParams->DesktopInfo.Length > 0) ? &ProcessParams->DesktopInfo : NULL) : NULL);
216 Status = IntParseDesktopPath(Process,
217 DesktopPath,
218 &hWinSta,
219 &hDesk);
220 if(NT_SUCCESS(Status))
221 {
222 if(hWinSta != NULL)
223 {
224 if(Process != CsrProcess)
225 {
226 HWINSTA hProcessWinSta = (HWINSTA)InterlockedCompareExchangePointer((PVOID)&Process->Win32WindowStation, (PVOID)hWinSta, NULL);
227 if(hProcessWinSta != NULL)
228 {
229 /* our process is already assigned to a different window station, we don't need the handle anymore */
230 NtClose(hWinSta);
231 }
232 }
233 else
234 {
235 NtClose(hWinSta);
236 }
237 }
238
239 if (hDesk != NULL)
240 {
241 PDESKTOP DesktopObject;
242 Win32Thread->rpdesk = NULL;
243 Status = ObReferenceObjectByHandle(hDesk,
244 0,
245 ExDesktopObjectType,
246 KernelMode,
247 (PVOID*)&DesktopObject,
248 NULL);
249 NtClose(hDesk);
250 if(NT_SUCCESS(Status))
251 {
252 if (!IntSetThreadDesktop(DesktopObject,
253 FALSE))
254 {
255 DPRINT1("Unable to set thread desktop\n");
256 }
257 }
258 else
259 {
260 DPRINT1("Unable to reference thread desktop handle 0x%x\n", hDesk);
261 }
262 }
263 }
264 else
265 {
266 DPRINT1("No Desktop handle for this Thread!\n");
267 }
268 Win32Thread->TIF_flags &= ~TIF_INCLEANUP;
269 co_IntDestroyCaret(Win32Thread);
270 Win32Thread->ppi = PsGetCurrentProcessWin32Process();
271 if (Win32Thread->rpdesk && !Win32Thread->pDeskInfo)
272 {
273 Win32Thread->pDeskInfo = Win32Thread->rpdesk->pDeskInfo;
274 }
275 Win32Thread->MessageQueue = MsqCreateMessageQueue(Thread);
276 Win32Thread->KeyboardLayout = W32kGetDefaultKeyLayout();
277 pTeb = NtCurrentTeb();
278 if (pTeb)
279 { /* Attempt to startup client support which should have been initialized in IntSetThreadDesktop. */
280 PCLIENTINFO pci = (PCLIENTINFO)pTeb->Win32ClientInfo;
281 Win32Thread->pClientInfo = pci;
282 pci->ppi = Win32Thread->ppi;
283 pci->fsHooks = Win32Thread->fsHooks;
284 if (Win32Thread->KeyboardLayout) pci->hKL = Win32Thread->KeyboardLayout->hkl;
285 pci->dwTIFlags = Win32Thread->TIF_flags;
286 /* CI may not have been initialized. */
287 if (!pci->pDeskInfo && Win32Thread->pDeskInfo)
288 {
289 if (!pci->ulClientDelta) pci->ulClientDelta = DesktopHeapGetUserDelta();
290
291 pci->pDeskInfo = (PVOID)((ULONG_PTR)Win32Thread->pDeskInfo - pci->ulClientDelta);
292 }
293 if (Win32Thread->pcti && pci->pDeskInfo)
294 pci->pClientThreadInfo = (PVOID)((ULONG_PTR)Win32Thread->pcti - pci->ulClientDelta);
295 else
296 pci->pClientThreadInfo = NULL;
297 }
298 else
299 {
300 DPRINT1("No TEB for this Thread!\n");
301 }
302 Win32Thread->pEThread = Thread;
303 }
304 else
305 {
306 PSINGLE_LIST_ENTRY e;
307
308 DPRINT("Destroying W32 thread TID:%d at IRQ level: %lu\n", Thread->Cid.UniqueThread, KeGetCurrentIrql());
309
310 Win32Thread->TIF_flags |= TIF_INCLEANUP;
311 DceFreeThreadDCE(Win32Thread);
312 HOOK_DestroyThreadHooks(Thread);
313 EVENT_DestroyThreadEvents(Thread);
314 /* Cleanup timers */
315 DestroyTimersForThread(Win32Thread);
316 KeSetEvent(Win32Thread->MessageQueue->NewMessages, IO_NO_INCREMENT, FALSE);
317 UnregisterThreadHotKeys(Thread);
318 /* what if this co_ func crash in umode? what will clean us up then? */
319 co_DestroyThreadWindows(Thread);
320 IntBlockInput(Win32Thread, FALSE);
321 MsqDestroyMessageQueue(Win32Thread->MessageQueue);
322 IntCleanupThreadCallbacks(Win32Thread);
323
324 /* cleanup user object references stack */
325 e = PopEntryList(&Win32Thread->ReferencesList);
326 while (e)
327 {
328 PUSER_REFERENCE_ENTRY ref = CONTAINING_RECORD(e, USER_REFERENCE_ENTRY, Entry);
329 DPRINT("thread clean: remove reference obj 0x%x\n",ref->obj);
330 UserDereferenceObject(ref->obj);
331
332 e = PopEntryList(&Win32Thread->ReferencesList);
333 }
334
335 IntSetThreadDesktop(NULL,
336 TRUE);
337
338
339 PsSetThreadWin32Thread(Thread, NULL);
340 }
341
342 RETURN( STATUS_SUCCESS);
343
344 CLEANUP:
345 UserLeave();
346 DPRINT("Leave Win32kThreadCallback, ret=%i\n",_ret_);
347 END_CLEANUP;
348 }
349
350 NTSTATUS
351 Win32kInitWin32Thread(PETHREAD Thread)
352 {
353 PEPROCESS Process;
354
355 Process = Thread->ThreadsProcess;
356
357 if (Process->Win32Process == NULL)
358 {
359 /* FIXME - lock the process */
360 Process->Win32Process = ExAllocatePoolWithTag(NonPagedPool, sizeof(PROCESSINFO), USERTAG_PROCESSINFO);
361
362 if (Process->Win32Process == NULL)
363 return STATUS_NO_MEMORY;
364
365 RtlZeroMemory(Process->Win32Process, sizeof(PROCESSINFO));
366 /* FIXME - unlock the process */
367
368 Win32kProcessCallback(Process, TRUE);
369 }
370
371 if (Thread->Tcb.Win32Thread == NULL)
372 {
373 Thread->Tcb.Win32Thread = ExAllocatePoolWithTag(NonPagedPool, sizeof(THREADINFO), USERTAG_THREADINFO);
374 if (Thread->Tcb.Win32Thread == NULL)
375 return STATUS_NO_MEMORY;
376
377 RtlZeroMemory(Thread->Tcb.Win32Thread, sizeof(THREADINFO));
378
379 Win32kThreadCallback(Thread, PsW32ThreadCalloutInitialize);
380 }
381
382 return(STATUS_SUCCESS);
383 }
384
385 C_ASSERT(sizeof(SERVERINFO) <= PAGE_SIZE);
386
387 // Return on failure
388 #define NT_ROF(x) \
389 Status = (x); \
390 if (!NT_SUCCESS(Status)) \
391 { \
392 DPRINT1("Failed '%s' (0x%lx)\n", #x, Status); \
393 return Status; \
394 }
395
396 /*
397 * This definition doesn't work
398 */
399 INIT_FUNCTION
400 NTSTATUS
401 APIENTRY
402 DriverEntry(
403 IN PDRIVER_OBJECT DriverObject,
404 IN PUNICODE_STRING RegistryPath)
405 {
406 NTSTATUS Status;
407 BOOLEAN Result;
408 WIN32_CALLOUTS_FPNS CalloutData = {0};
409 PVOID GlobalUserHeapBase = NULL;
410
411 /*
412 * Register user mode call interface
413 * (system service table index = 1)
414 */
415 Result = KeAddSystemServiceTable(Win32kSSDT,
416 NULL,
417 Win32kNumberOfSysCalls,
418 Win32kSSPT,
419 1);
420 if (Result == FALSE)
421 {
422 DPRINT1("Adding system services failed!\n");
423 return STATUS_UNSUCCESSFUL;
424 }
425
426 hModuleWin = MmPageEntireDriver(DriverEntry);
427 DPRINT("Win32k hInstance 0x%x!\n",hModuleWin);
428
429 /* Register Object Manager Callbacks */
430 CalloutData.WindowStationParseProcedure = IntWinStaObjectParse;
431 CalloutData.WindowStationDeleteProcedure = IntWinStaObjectDelete;
432 CalloutData.DesktopDeleteProcedure = IntDesktopObjectDelete;
433 CalloutData.ProcessCallout = Win32kProcessCallback;
434 CalloutData.ThreadCallout = Win32kThreadCallback;
435 CalloutData.BatchFlushRoutine = NtGdiFlushUserBatch;
436
437 /* Register our per-process and per-thread structures. */
438 PsEstablishWin32Callouts((PWIN32_CALLOUTS_FPNS)&CalloutData);
439
440 /* Create the global USER heap */
441 GlobalUserHeap = UserCreateHeap(&GlobalUserHeapSection,
442 &GlobalUserHeapBase,
443 1 * 1024 * 1024); /* FIXME - 1 MB for now... */
444 if (GlobalUserHeap == NULL)
445 {
446 DPRINT1("Failed to initialize the global heap!\n");
447 return STATUS_UNSUCCESSFUL;
448 }
449
450 /* Allocate global server info structure */
451 gpsi = UserHeapAlloc(sizeof(SERVERINFO));
452 if (!gpsi)
453 {
454 DPRINT1("Failed allocate server info structure!\n");
455 return STATUS_UNSUCCESSFUL;
456 }
457
458 RtlZeroMemory(gpsi, sizeof(SERVERINFO));
459 DPRINT("Global Server Data -> %x\n", gpsi);
460
461 NT_ROF(InitGdiHandleTable());
462 NT_ROF(InitPaletteImpl());
463
464 /* Create stock objects, ie. precreated objects commonly
465 used by win32 applications */
466 CreateStockObjects();
467 CreateSysColorObjects();
468
469 NT_ROF(InitXlateImpl());
470 NT_ROF(InitPDEVImpl());
471 NT_ROF(InitLDEVImpl());
472 NT_ROF(InitDeviceImpl());
473 NT_ROF(InitDcImpl());
474 NT_ROF(InitUserImpl());
475 NT_ROF(InitHotkeyImpl());
476 NT_ROF(InitWindowStationImpl());
477 NT_ROF(InitDesktopImpl());
478 NT_ROF(InitWindowImpl());
479 NT_ROF(InitMenuImpl());
480 NT_ROF(InitInputImpl());
481 NT_ROF(InitKeyboardImpl());
482 NT_ROF(InitMonitorImpl());
483 NT_ROF(MsqInitializeImpl());
484 NT_ROF(InitTimerImpl());
485 NT_ROF(InitAcceleratorImpl());
486 NT_ROF(InitGuiCheckImpl());
487
488 /* Initialize FreeType library */
489 if (!InitFontSupport())
490 {
491 DPRINT1("Unable to initialize font support\n");
492 return Status;
493 }
494
495 gusLanguageID = IntGdiGetLanguageID();
496
497 return STATUS_SUCCESS;
498 }
499
500 /* EOF */