2 * ReactOS W32 Subsystem
3 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 ReactOS Team
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.
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.
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.
21 * Entry Point for win32k.sys
25 #include <include/napi.h>
30 PGDI_HANDLE_TABLE INTERNAL_CALL
GDIOBJ_iAllocHandleTable(OUT PSECTION_OBJECT
*SectionObject
);
31 BOOL INTERNAL_CALL
GDI_CleanupForProcess (struct _EPROCESS
*Process
);
33 PGDI_HANDLE_TABLE GdiHandleTable
= NULL
;
34 PSECTION_OBJECT GdiTableSection
= NULL
;
36 LIST_ENTRY GlobalDriverListHead
;
38 HANDLE GlobalUserHeap
= NULL
;
39 PSECTION_OBJECT GlobalUserHeapSection
= NULL
;
41 PSERVERINFO gpsi
= NULL
; // Global User Server Information.
43 HSEMAPHORE hsemDriverMgmt
= NULL
;
45 extern ULONG_PTR Win32kSSDT
[];
46 extern UCHAR Win32kSSPT
[];
47 extern ULONG Win32kNumberOfSysCalls
;
51 Win32kProcessCallback(struct _EPROCESS
*Process
,
54 PW32PROCESS Win32Process
;
55 DECLARE_RETURN(NTSTATUS
);
57 DPRINT("Enter Win32kProcessCallback\n");
60 /* Get the Win32 Process */
61 Win32Process
= PsGetProcessWin32Process(Process
);
63 /* Allocate one if needed */
66 /* FIXME - lock the process */
67 Win32Process
= ExAllocatePoolWithTag(NonPagedPool
,
69 TAG('W', '3', '2', 'p'));
71 if (Win32Process
== NULL
) RETURN( STATUS_NO_MEMORY
);
73 RtlZeroMemory(Win32Process
, sizeof(W32PROCESS
));
75 PsSetProcessWin32Process(Process
, Win32Process
);
76 /* FIXME - unlock the process */
83 PVOID UserBase
= NULL
;
85 extern PSECTION_OBJECT GlobalUserHeapSection
;
86 DPRINT("Creating W32 process PID:%d at IRQ level: %lu\n", Process
->UniqueProcessId
, KeGetCurrentIrql());
88 /* map the global heap into the process */
90 Status
= MmMapViewOfSection(GlobalUserHeapSection
,
91 PsGetCurrentProcess(),
99 PAGE_EXECUTE_READ
); /* would prefer PAGE_READONLY, but thanks to RTL heaps... */
100 if (!NT_SUCCESS(Status
))
102 DPRINT1("Failed to map the global heap! 0x%x\n", Status
);
105 Win32Process
->HeapMappings
.Next
= NULL
;
106 Win32Process
->HeapMappings
.KernelMapping
= (PVOID
)GlobalUserHeap
;
107 Win32Process
->HeapMappings
.UserMapping
= UserBase
;
108 Win32Process
->HeapMappings
.Count
= 1;
110 InitializeListHead(&Win32Process
->ClassList
);
112 InitializeListHead(&Win32Process
->MenuListHead
);
114 InitializeListHead(&Win32Process
->PrivateFontListHead
);
115 ExInitializeFastMutex(&Win32Process
->PrivateFontListLock
);
117 InitializeListHead(&Win32Process
->DriverObjListHead
);
118 ExInitializeFastMutex(&Win32Process
->DriverObjListLock
);
120 Win32Process
->KeyboardLayout
= W32kGetDefaultKeyLayout();
122 if(Process
->Peb
!= NULL
)
124 /* map the gdi handle table to user land */
125 Process
->Peb
->GdiSharedHandleTable
= GDI_MapHandleTable(GdiTableSection
, Process
);
126 Process
->Peb
->GdiDCAttributeList
= GDI_BATCH_LIMIT
;
129 /* setup process flags */
130 Win32Process
->Flags
= 0;
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();
140 /* no process windows should exist at this point, or the function will assert! */
141 DestroyProcessClasses(Win32Process
);
143 GDI_CleanupForProcess(Process
);
145 co_IntGraphicsCheck(FALSE
);
148 * Deregister logon application automatically
150 if(LogonProcess
== Win32Process
)
155 if (Win32Process
->ProcessInfo
!= NULL
)
157 UserHeapFree(Win32Process
->ProcessInfo
);
158 Win32Process
->ProcessInfo
= NULL
;
162 RETURN( STATUS_SUCCESS
);
166 DPRINT("Leave Win32kProcessCallback, ret=%i\n",_ret_
);
173 Win32kThreadCallback(struct _ETHREAD
*Thread
,
174 PSW32THREADCALLOUTTYPE Type
)
176 struct _EPROCESS
*Process
;
177 PTHREADINFO Win32Thread
;
178 DECLARE_RETURN(NTSTATUS
);
180 DPRINT("Enter Win32kThreadCallback\n");
181 UserEnterExclusive();
183 Process
= Thread
->ThreadsProcess
;
185 /* Get the Win32 Thread */
186 Win32Thread
= PsGetThreadWin32Thread(Thread
);
188 /* Allocate one if needed */
191 /* FIXME - lock the process */
192 Win32Thread
= ExAllocatePoolWithTag(NonPagedPool
,
194 TAG('W', '3', '2', 't'));
196 if (Win32Thread
== NULL
) RETURN( STATUS_NO_MEMORY
);
198 RtlZeroMemory(Win32Thread
, sizeof(THREADINFO
));
200 PsSetThreadWin32Thread(Thread
, Win32Thread
);
201 /* FIXME - unlock the process */
203 if (Type
== PsW32ThreadCalloutInitialize
)
205 HWINSTA hWinSta
= NULL
;
208 PUNICODE_STRING DesktopPath
;
209 PRTL_USER_PROCESS_PARAMETERS ProcessParams
= (Process
->Peb
? Process
->Peb
->ProcessParameters
: NULL
);
211 DPRINT("Creating W32 thread TID:%d at IRQ level: %lu\n", Thread
->Cid
.UniqueThread
, KeGetCurrentIrql());
213 InitializeListHead(&Win32Thread
->WindowListHead
);
214 InitializeListHead(&Win32Thread
->W32CallbackListHead
);
215 InitializeListHead(&Win32Thread
->PtiLink
);
218 * inherit the thread desktop and process window station (if not yet inherited) from the process startup
219 * info structure. See documentation of CreateProcess()
221 DesktopPath
= (ProcessParams
? ((ProcessParams
->DesktopInfo
.Length
> 0) ? &ProcessParams
->DesktopInfo
: NULL
) : NULL
);
222 Status
= IntParseDesktopPath(Process
,
226 if(NT_SUCCESS(Status
))
230 if(Process
!= CsrProcess
)
232 HWINSTA hProcessWinSta
= (HWINSTA
)InterlockedCompareExchangePointer((PVOID
)&Process
->Win32WindowStation
, (PVOID
)hWinSta
, NULL
);
233 if(hProcessWinSta
!= NULL
)
235 /* our process is already assigned to a different window station, we don't need the handle anymore */
247 PDESKTOP DesktopObject
;
248 Win32Thread
->Desktop
= NULL
;
249 Status
= ObReferenceObjectByHandle(hDesk
,
253 (PVOID
*)&DesktopObject
,
256 if(NT_SUCCESS(Status
))
258 if (!IntSetThreadDesktop(DesktopObject
,
261 DPRINT1("Unable to set thread desktop\n");
266 DPRINT1("Unable to reference thread desktop handle 0x%x\n", hDesk
);
270 Win32Thread
->IsExiting
= FALSE
;
271 co_IntDestroyCaret(Win32Thread
);
272 Win32Thread
->ppi
= PsGetCurrentProcessWin32Process();
273 Win32Thread
->MessageQueue
= MsqCreateMessageQueue(Thread
);
274 Win32Thread
->KeyboardLayout
= W32kGetDefaultKeyLayout();
275 if (Win32Thread
->ThreadInfo
)
276 Win32Thread
->ThreadInfo
->ClientThreadInfo
.dwcPumpHook
= 0;
280 PSINGLE_LIST_ENTRY e
;
282 DPRINT("Destroying W32 thread TID:%d at IRQ level: %lu\n", Thread
->Cid
.UniqueThread
, KeGetCurrentIrql());
284 Win32Thread
->IsExiting
= TRUE
;
285 HOOK_DestroyThreadHooks(Thread
);
286 UnregisterThreadHotKeys(Thread
);
287 /* what if this co_ func crash in umode? what will clean us up then? */
288 co_DestroyThreadWindows(Thread
);
289 IntBlockInput(Win32Thread
, FALSE
);
290 MsqDestroyMessageQueue(Win32Thread
->MessageQueue
);
291 IntCleanupThreadCallbacks(Win32Thread
);
293 /* cleanup user object references stack */
294 e
= PopEntryList(&Win32Thread
->ReferencesList
);
297 PUSER_REFERENCE_ENTRY ref
= CONTAINING_RECORD(e
, USER_REFERENCE_ENTRY
, Entry
);
298 DPRINT("thread clean: remove reference obj 0x%x\n",ref
->obj
);
299 UserDereferenceObject(ref
->obj
);
301 e
= PopEntryList(&Win32Thread
->ReferencesList
);
304 IntSetThreadDesktop(NULL
,
307 if (Win32Thread
->ThreadInfo
!= NULL
)
309 UserHeapFree(Win32Thread
->ThreadInfo
);
310 Win32Thread
->ThreadInfo
= NULL
;
313 PsSetThreadWin32Thread(Thread
, NULL
);
316 RETURN( STATUS_SUCCESS
);
320 DPRINT("Leave Win32kThreadCallback, ret=%i\n",_ret_
);
324 /* Only used in ntuser/input.c KeyboardThreadMain(). If it's
325 not called there anymore, please delete */
327 Win32kInitWin32Thread(PETHREAD Thread
)
331 Process
= Thread
->ThreadsProcess
;
333 if (Process
->Win32Process
== NULL
)
335 /* FIXME - lock the process */
336 Process
->Win32Process
= ExAllocatePool(NonPagedPool
, sizeof(W32PROCESS
));
338 if (Process
->Win32Process
== NULL
)
339 return STATUS_NO_MEMORY
;
341 RtlZeroMemory(Process
->Win32Process
, sizeof(W32PROCESS
));
342 /* FIXME - unlock the process */
344 Win32kProcessCallback(Process
, TRUE
);
347 if (Thread
->Tcb
.Win32Thread
== NULL
)
349 Thread
->Tcb
.Win32Thread
= ExAllocatePool (NonPagedPool
, sizeof(THREADINFO
));
350 if (Thread
->Tcb
.Win32Thread
== NULL
)
351 return STATUS_NO_MEMORY
;
353 RtlZeroMemory(Thread
->Tcb
.Win32Thread
, sizeof(THREADINFO
));
355 Win32kThreadCallback(Thread
, PsW32ThreadCalloutInitialize
);
358 return(STATUS_SUCCESS
);
363 * This definition doesn't work
367 IN PDRIVER_OBJECT DriverObject
,
368 IN PUNICODE_STRING RegistryPath
)
372 WIN32_CALLOUTS_FPNS CalloutData
= {0};
373 PVOID GlobalUserHeapBase
= NULL
;
376 * Register user mode call interface
377 * (system service table index = 1)
379 Result
= KeAddSystemServiceTable (Win32kSSDT
,
381 Win32kNumberOfSysCalls
,
386 DPRINT1("Adding system services failed!\n");
387 return STATUS_UNSUCCESSFUL
;
391 * Register Object Manager Callbacks
393 CalloutData
.WindowStationParseProcedure
= IntWinStaObjectParse
;
394 CalloutData
.WindowStationDeleteProcedure
= IntWinStaObjectDelete
;
395 CalloutData
.DesktopDeleteProcedure
= IntDesktopObjectDelete
;
396 CalloutData
.ProcessCallout
= Win32kProcessCallback
;
397 CalloutData
.ThreadCallout
= Win32kThreadCallback
;
398 CalloutData
.BatchFlushRoutine
= NtGdiFlushUserBatch
;
401 * Register our per-process and per-thread structures.
403 PsEstablishWin32Callouts((PWIN32_CALLOUTS_FPNS
)&CalloutData
);
405 GlobalUserHeap
= UserCreateHeap(&GlobalUserHeapSection
,
407 1 * 1024 * 1024); /* FIXME - 1 MB for now... */
408 if (GlobalUserHeap
== NULL
)
410 DPRINT1("Failed to initialize the global heap!\n");
411 return STATUS_UNSUCCESSFUL
;
414 /* Initialize a list of loaded drivers in Win32 subsystem */
415 InitializeListHead(&GlobalDriverListHead
);
417 if(!hsemDriverMgmt
) hsemDriverMgmt
= EngCreateSemaphore();
419 Status
= InitUserImpl();
420 if (!NT_SUCCESS(Status
))
422 DPRINT1("Failed to initialize user implementation!\n");
423 return STATUS_UNSUCCESSFUL
;
426 Status
= InitHotkeyImpl();
427 if (!NT_SUCCESS(Status
))
429 DPRINT1("Failed to initialize hotkey implementation!\n");
430 return STATUS_UNSUCCESSFUL
;
433 Status
= InitWindowStationImpl();
434 if (!NT_SUCCESS(Status
))
436 DPRINT1("Failed to initialize window station implementation!\n");
437 return STATUS_UNSUCCESSFUL
;
440 Status
= InitDesktopImpl();
441 if (!NT_SUCCESS(Status
))
443 DPRINT1("Failed to initialize desktop implementation!\n");
444 return STATUS_UNSUCCESSFUL
;
447 Status
= InitWindowImpl();
448 if (!NT_SUCCESS(Status
))
450 DPRINT1("Failed to initialize window implementation!\n");
451 return STATUS_UNSUCCESSFUL
;
454 Status
= InitMenuImpl();
455 if (!NT_SUCCESS(Status
))
457 DPRINT1("Failed to initialize menu implementation!\n");
458 return STATUS_UNSUCCESSFUL
;
461 Status
= InitInputImpl();
462 if (!NT_SUCCESS(Status
))
464 DPRINT1("Failed to initialize input implementation.\n");
468 Status
= InitKeyboardImpl();
469 if (!NT_SUCCESS(Status
))
471 DPRINT1("Failed to initialize keyboard implementation.\n");
475 Status
= InitMonitorImpl();
476 if (!NT_SUCCESS(Status
))
478 DbgPrint("Failed to initialize monitor implementation!\n");
479 return STATUS_UNSUCCESSFUL
;
482 Status
= MsqInitializeImpl();
483 if (!NT_SUCCESS(Status
))
485 DPRINT1("Failed to initialize message queue implementation.\n");
489 Status
= InitTimerImpl();
490 if (!NT_SUCCESS(Status
))
492 DPRINT1("Failed to initialize timer implementation.\n");
496 Status
= InitAcceleratorImpl();
497 if (!NT_SUCCESS(Status
))
499 DPRINT1("Failed to initialize accelerator implementation.\n");
503 Status
= InitGuiCheckImpl();
504 if (!NT_SUCCESS(Status
))
506 DPRINT1("Failed to initialize GUI check implementation.\n");
510 GdiHandleTable
= GDIOBJ_iAllocHandleTable(&GdiTableSection
);
511 if (GdiHandleTable
== NULL
)
513 DPRINT1("Failed to initialize the GDI handle table.\n");
514 return STATUS_UNSUCCESSFUL
;
517 Status
= InitDcImpl();
518 if (!NT_SUCCESS(Status
))
520 DPRINT1("Failed to initialize Device context implementation!\n");
521 return STATUS_UNSUCCESSFUL
;
524 /* Initialize FreeType library */
525 if (! InitFontSupport())
527 DPRINT1("Unable to initialize font support\n");
528 return STATUS_UNSUCCESSFUL
;
531 /* Create stock objects, ie. precreated objects commonly
532 used by win32 applications */
533 CreateStockObjects();
534 CreateSysColorObjects();
536 return STATUS_SUCCESS
;