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