[CMAKE]
[reactos.git] / 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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 /*
20 * Entry Point for win32k.sys
21 */
22
23 #include <win32k.h>
24 #include <include/napi.h>
25
26 #define NDEBUG
27 #include <debug.h>
28
29 HANDLE hModuleWin;
30
31 PGDI_HANDLE_TABLE INTERNAL_CALL GDIOBJ_iAllocHandleTable(OUT PSECTION_OBJECT *SectionObject);
32 BOOL INTERNAL_CALL GDI_CleanupForProcess (struct _EPROCESS *Process);
33 /* FIXME */
34 PGDI_HANDLE_TABLE GdiHandleTable = NULL;
35 PSECTION_OBJECT GdiTableSection = NULL;
36
37 LIST_ENTRY GlobalDriverListHead;
38
39 HANDLE GlobalUserHeap = NULL;
40 PSECTION_OBJECT GlobalUserHeapSection = NULL;
41
42 PSERVERINFO gpsi = NULL; // Global User Server Information.
43
44 HSEMAPHORE hsemDriverMgmt = NULL;
45
46 SHORT gusLanguageID;
47
48 extern ULONG_PTR Win32kSSDT[];
49 extern UCHAR Win32kSSPT[];
50 extern ULONG Win32kNumberOfSysCalls;
51
52 NTSTATUS
53 APIENTRY
54 Win32kProcessCallback(struct _EPROCESS *Process,
55 BOOLEAN Create)
56 {
57 PPROCESSINFO Win32Process;
58 DECLARE_RETURN(NTSTATUS);
59
60 DPRINT("Enter Win32kProcessCallback\n");
61 UserEnterExclusive();
62
63 /* Get the Win32 Process */
64 Win32Process = PsGetProcessWin32Process(Process);
65
66 /* Allocate one if needed */
67 if (!Win32Process)
68 {
69 /* FIXME - lock the process */
70 Win32Process = ExAllocatePoolWithTag(NonPagedPool,
71 sizeof(PROCESSINFO),
72 'p23W');
73
74 if (Win32Process == NULL) RETURN( STATUS_NO_MEMORY);
75
76 RtlZeroMemory(Win32Process, sizeof(PROCESSINFO));
77
78 PsSetProcessWin32Process(Process, Win32Process);
79 /* FIXME - unlock the process */
80 }
81
82 if (Create)
83 {
84 SIZE_T ViewSize = 0;
85 LARGE_INTEGER Offset;
86 PVOID UserBase = NULL;
87 NTSTATUS Status;
88 extern PSECTION_OBJECT GlobalUserHeapSection;
89 DPRINT("Creating W32 process PID:%d at IRQ level: %lu\n", Process->UniqueProcessId, KeGetCurrentIrql());
90
91 /* map the global heap into the process */
92 Offset.QuadPart = 0;
93 Status = MmMapViewOfSection(GlobalUserHeapSection,
94 PsGetCurrentProcess(),
95 &UserBase,
96 0,
97 0,
98 &Offset,
99 &ViewSize,
100 ViewUnmap,
101 SEC_NO_CHANGE,
102 PAGE_EXECUTE_READ); /* would prefer PAGE_READONLY, but thanks to RTL heaps... */
103 if (!NT_SUCCESS(Status))
104 {
105 DPRINT1("Failed to map the global heap! 0x%x\n", Status);
106 RETURN(Status);
107 }
108 Win32Process->HeapMappings.Next = NULL;
109 Win32Process->HeapMappings.KernelMapping = (PVOID)GlobalUserHeap;
110 Win32Process->HeapMappings.UserMapping = UserBase;
111 Win32Process->HeapMappings.Count = 1;
112
113 InitializeListHead(&Win32Process->ClassList);
114
115 InitializeListHead(&Win32Process->MenuListHead);
116
117 InitializeListHead(&Win32Process->GDIBrushAttrFreeList);
118 InitializeListHead(&Win32Process->GDIDcAttrFreeList);
119
120 InitializeListHead(&Win32Process->PrivateFontListHead);
121 ExInitializeFastMutex(&Win32Process->PrivateFontListLock);
122
123 InitializeListHead(&Win32Process->DriverObjListHead);
124 ExInitializeFastMutex(&Win32Process->DriverObjListLock);
125
126 Win32Process->KeyboardLayout = W32kGetDefaultKeyLayout();
127
128 if(Process->Peb != NULL)
129 {
130 /* map the gdi handle table to user land */
131 Process->Peb->GdiSharedHandleTable = GDI_MapHandleTable(GdiTableSection, Process);
132 Process->Peb->GdiDCAttributeList = GDI_BATCH_LIMIT;
133 }
134
135 Win32Process->peProcess = Process;
136 /* setup process flags */
137 Win32Process->W32PF_flags = 0;
138 }
139 else
140 {
141 DPRINT("Destroying W32 process PID:%d at IRQ level: %lu\n", Process->UniqueProcessId, KeGetCurrentIrql());
142 IntCleanupMenus(Process, Win32Process);
143 IntCleanupCurIcons(Process, Win32Process);
144 CleanupMonitorImpl();
145
146 /* no process windows should exist at this point, or the function will assert! */
147 DestroyProcessClasses(Win32Process);
148
149 GDI_CleanupForProcess(Process);
150
151 co_IntGraphicsCheck(FALSE);
152
153 /*
154 * Deregister logon application automatically
155 */
156 if(LogonProcess == Win32Process)
157 {
158 LogonProcess = 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 APIENTRY
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 't23W');
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->rpdesk = 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->TIF_flags &= ~TIF_INCLEANUP;
272 co_IntDestroyCaret(Win32Thread);
273 Win32Thread->ppi = PsGetCurrentProcessWin32Process();
274 pTeb = NtCurrentTeb();
275 if (pTeb)
276 {
277 Win32Thread->pClientInfo = (PCLIENTINFO)pTeb->Win32ClientInfo;
278 Win32Thread->pClientInfo->pClientThreadInfo = NULL;
279 }
280 Win32Thread->MessageQueue = MsqCreateMessageQueue(Thread);
281 Win32Thread->KeyboardLayout = W32kGetDefaultKeyLayout();
282 Win32Thread->pEThread = Thread;
283 }
284 else
285 {
286 PSINGLE_LIST_ENTRY e;
287
288 DPRINT("Destroying W32 thread TID:%d at IRQ level: %lu\n", Thread->Cid.UniqueThread, KeGetCurrentIrql());
289
290 Win32Thread->TIF_flags |= TIF_INCLEANUP;
291 DceFreeThreadDCE(Win32Thread);
292 HOOK_DestroyThreadHooks(Thread);
293 /* Cleanup timers */
294 DestroyTimersForThread(Win32Thread);
295 KeSetEvent(Win32Thread->MessageQueue->NewMessages, IO_NO_INCREMENT, FALSE);
296 UnregisterThreadHotKeys(Thread);
297 /* what if this co_ func crash in umode? what will clean us up then? */
298 co_DestroyThreadWindows(Thread);
299 IntBlockInput(Win32Thread, FALSE);
300 MsqDestroyMessageQueue(Win32Thread->MessageQueue);
301 IntCleanupThreadCallbacks(Win32Thread);
302
303 /* cleanup user object references stack */
304 e = PopEntryList(&Win32Thread->ReferencesList);
305 while (e)
306 {
307 PUSER_REFERENCE_ENTRY ref = CONTAINING_RECORD(e, USER_REFERENCE_ENTRY, Entry);
308 DPRINT("thread clean: remove reference obj 0x%x\n",ref->obj);
309 UserDereferenceObject(ref->obj);
310
311 e = PopEntryList(&Win32Thread->ReferencesList);
312 }
313
314 IntSetThreadDesktop(NULL,
315 TRUE);
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 = ExAllocatePoolWithTag(NonPagedPool, sizeof(PROCESSINFO), USERTAG_PROCESSINFO);
341
342 if (Process->Win32Process == NULL)
343 return STATUS_NO_MEMORY;
344
345 RtlZeroMemory(Process->Win32Process, sizeof(PROCESSINFO));
346 /* FIXME - unlock the process */
347
348 Win32kProcessCallback(Process, TRUE);
349 }
350
351 if (Thread->Tcb.Win32Thread == NULL)
352 {
353 Thread->Tcb.Win32Thread = ExAllocatePoolWithTag(NonPagedPool, sizeof(THREADINFO), USERTAG_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 APIENTRY
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 hModuleWin = MmPageEntireDriver(DriverEntry);
395 DPRINT("Win32k hInstance 0x%x!\n",hModuleWin);
396 /*
397 * Register Object Manager Callbacks
398 */
399 CalloutData.WindowStationParseProcedure = IntWinStaObjectParse;
400 CalloutData.WindowStationDeleteProcedure = IntWinStaObjectDelete;
401 CalloutData.DesktopDeleteProcedure = IntDesktopObjectDelete;
402 CalloutData.ProcessCallout = Win32kProcessCallback;
403 CalloutData.ThreadCallout = Win32kThreadCallback;
404 CalloutData.BatchFlushRoutine = NtGdiFlushUserBatch;
405
406 /*
407 * Register our per-process and per-thread structures.
408 */
409 PsEstablishWin32Callouts((PWIN32_CALLOUTS_FPNS)&CalloutData);
410
411 GlobalUserHeap = UserCreateHeap(&GlobalUserHeapSection,
412 &GlobalUserHeapBase,
413 1 * 1024 * 1024); /* FIXME - 1 MB for now... */
414 if (GlobalUserHeap == NULL)
415 {
416 DPRINT1("Failed to initialize the global heap!\n");
417 return STATUS_UNSUCCESSFUL;
418 }
419
420 /* Initialize a list of loaded drivers in Win32 subsystem */
421 InitializeListHead(&GlobalDriverListHead);
422
423 if(!hsemDriverMgmt) hsemDriverMgmt = EngCreateSemaphore();
424
425 GdiHandleTable = GDIOBJ_iAllocHandleTable(&GdiTableSection);
426 if (GdiHandleTable == NULL)
427 {
428 DPRINT1("Failed to initialize the GDI handle table.\n");
429 return STATUS_UNSUCCESSFUL;
430 }
431
432 Status = InitUserImpl();
433 if (!NT_SUCCESS(Status))
434 {
435 DPRINT1("Failed to initialize user implementation!\n");
436 return STATUS_UNSUCCESSFUL;
437 }
438
439 Status = InitHotkeyImpl();
440 if (!NT_SUCCESS(Status))
441 {
442 DPRINT1("Failed to initialize hotkey implementation!\n");
443 return STATUS_UNSUCCESSFUL;
444 }
445
446 Status = InitWindowStationImpl();
447 if (!NT_SUCCESS(Status))
448 {
449 DPRINT1("Failed to initialize window station implementation!\n");
450 return STATUS_UNSUCCESSFUL;
451 }
452
453 Status = InitDesktopImpl();
454 if (!NT_SUCCESS(Status))
455 {
456 DPRINT1("Failed to initialize desktop implementation!\n");
457 return STATUS_UNSUCCESSFUL;
458 }
459
460 Status = InitWindowImpl();
461 if (!NT_SUCCESS(Status))
462 {
463 DPRINT1("Failed to initialize window implementation!\n");
464 return STATUS_UNSUCCESSFUL;
465 }
466
467 Status = InitMenuImpl();
468 if (!NT_SUCCESS(Status))
469 {
470 DPRINT1("Failed to initialize menu implementation!\n");
471 return STATUS_UNSUCCESSFUL;
472 }
473
474 Status = InitInputImpl();
475 if (!NT_SUCCESS(Status))
476 {
477 DPRINT1("Failed to initialize input implementation.\n");
478 return(Status);
479 }
480
481 Status = InitKeyboardImpl();
482 if (!NT_SUCCESS(Status))
483 {
484 DPRINT1("Failed to initialize keyboard implementation.\n");
485 return(Status);
486 }
487
488 Status = InitMonitorImpl();
489 if (!NT_SUCCESS(Status))
490 {
491 DbgPrint("Failed to initialize monitor implementation!\n");
492 return STATUS_UNSUCCESSFUL;
493 }
494
495 Status = MsqInitializeImpl();
496 if (!NT_SUCCESS(Status))
497 {
498 DPRINT1("Failed to initialize message queue implementation.\n");
499 return(Status);
500 }
501
502 Status = InitTimerImpl();
503 if (!NT_SUCCESS(Status))
504 {
505 DPRINT1("Failed to initialize timer implementation.\n");
506 return(Status);
507 }
508
509 Status = InitAcceleratorImpl();
510 if (!NT_SUCCESS(Status))
511 {
512 DPRINT1("Failed to initialize accelerator implementation.\n");
513 return(Status);
514 }
515
516 Status = InitGuiCheckImpl();
517 if (!NT_SUCCESS(Status))
518 {
519 DPRINT1("Failed to initialize GUI check implementation.\n");
520 return(Status);
521 }
522
523 Status = InitDcImpl();
524 if (!NT_SUCCESS(Status))
525 {
526 DPRINT1("Failed to initialize Device context implementation!\n");
527 return STATUS_UNSUCCESSFUL;
528 }
529
530 /* Initialize FreeType library */
531 if (! InitFontSupport())
532 {
533 DPRINT1("Unable to initialize font support\n");
534 return STATUS_UNSUCCESSFUL;
535 }
536
537 InitXlateImpl();
538
539 /* Create stock objects, ie. precreated objects commonly
540 used by win32 applications */
541 CreateStockObjects();
542 CreateSysColorObjects();
543
544 gusLanguageID = IntGdiGetLanguageID();
545
546 return STATUS_SUCCESS;
547 }
548
549 /* EOF */