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>
32 PGDI_HANDLE_TABLE INTERNAL_CALL
GDIOBJ_iAllocHandleTable(OUT PSECTION_OBJECT
*SectionObject
);
33 BOOL INTERNAL_CALL
GDI_CleanupForProcess (struct _EPROCESS
*Process
);
35 PGDI_HANDLE_TABLE GdiHandleTable
= NULL
;
36 PSECTION_OBJECT GdiTableSection
= NULL
;
38 LIST_ENTRY GlobalDriverListHead
;
40 HANDLE GlobalUserHeap
= NULL
;
41 PSECTION_OBJECT GlobalUserHeapSection
= NULL
;
43 PSERVERINFO gpsi
= NULL
; // Global User Server Information.
45 HSEMAPHORE hsemDriverMgmt
= NULL
;
49 extern ULONG_PTR Win32kSSDT
[];
50 extern UCHAR Win32kSSPT
[];
51 extern ULONG Win32kNumberOfSysCalls
;
55 Win32kProcessCallback(struct _EPROCESS
*Process
,
58 PPROCESSINFO Win32Process
;
59 DECLARE_RETURN(NTSTATUS
);
61 DPRINT("Enter Win32kProcessCallback\n");
64 /* Get the Win32 Process */
65 Win32Process
= PsGetProcessWin32Process(Process
);
67 /* Allocate one if needed */
70 /* FIXME - lock the process */
71 Win32Process
= ExAllocatePoolWithTag(NonPagedPool
,
73 TAG('W', '3', '2', 'p'));
75 if (Win32Process
== NULL
) RETURN( STATUS_NO_MEMORY
);
77 RtlZeroMemory(Win32Process
, sizeof(PROCESSINFO
));
79 PsSetProcessWin32Process(Process
, Win32Process
);
80 /* FIXME - unlock the process */
87 PVOID UserBase
= NULL
;
89 extern PSECTION_OBJECT GlobalUserHeapSection
;
90 DPRINT("Creating W32 process PID:%d at IRQ level: %lu\n", Process
->UniqueProcessId
, KeGetCurrentIrql());
92 /* map the global heap into the process */
94 Status
= MmMapViewOfSection(GlobalUserHeapSection
,
95 PsGetCurrentProcess(),
103 PAGE_EXECUTE_READ
); /* would prefer PAGE_READONLY, but thanks to RTL heaps... */
104 if (!NT_SUCCESS(Status
))
106 DPRINT1("Failed to map the global heap! 0x%x\n", Status
);
109 Win32Process
->HeapMappings
.Next
= NULL
;
110 Win32Process
->HeapMappings
.KernelMapping
= (PVOID
)GlobalUserHeap
;
111 Win32Process
->HeapMappings
.UserMapping
= UserBase
;
112 Win32Process
->HeapMappings
.Count
= 1;
114 InitializeListHead(&Win32Process
->ClassList
);
116 InitializeListHead(&Win32Process
->MenuListHead
);
118 InitializeListHead(&Win32Process
->PrivateFontListHead
);
119 ExInitializeFastMutex(&Win32Process
->PrivateFontListLock
);
121 InitializeListHead(&Win32Process
->DriverObjListHead
);
122 ExInitializeFastMutex(&Win32Process
->DriverObjListLock
);
124 Win32Process
->KeyboardLayout
= W32kGetDefaultKeyLayout();
126 if(Process
->Peb
!= NULL
)
128 /* map the gdi handle table to user land */
129 Process
->Peb
->GdiSharedHandleTable
= GDI_MapHandleTable(GdiTableSection
, Process
);
130 Process
->Peb
->GdiDCAttributeList
= GDI_BATCH_LIMIT
;
133 /* setup process flags */
134 Win32Process
->W32PF_flags
= 0;
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();
143 /* no process windows should exist at this point, or the function will assert! */
144 DestroyProcessClasses(Win32Process
);
146 GDI_CleanupForProcess(Process
);
148 co_IntGraphicsCheck(FALSE
);
151 * Deregister logon application automatically
153 if(LogonProcess
== Win32Process
)
159 RETURN( STATUS_SUCCESS
);
163 DPRINT("Leave Win32kProcessCallback, ret=%i\n",_ret_
);
170 Win32kThreadCallback(struct _ETHREAD
*Thread
,
171 PSW32THREADCALLOUTTYPE Type
)
173 struct _EPROCESS
*Process
;
174 PTHREADINFO Win32Thread
;
175 DECLARE_RETURN(NTSTATUS
);
177 DPRINT("Enter Win32kThreadCallback\n");
178 UserEnterExclusive();
180 Process
= Thread
->ThreadsProcess
;
182 /* Get the Win32 Thread */
183 Win32Thread
= PsGetThreadWin32Thread(Thread
);
185 /* Allocate one if needed */
188 /* FIXME - lock the process */
189 Win32Thread
= ExAllocatePoolWithTag(NonPagedPool
,
191 TAG('W', '3', '2', 't'));
193 if (Win32Thread
== NULL
) RETURN( STATUS_NO_MEMORY
);
195 RtlZeroMemory(Win32Thread
, sizeof(THREADINFO
));
197 PsSetThreadWin32Thread(Thread
, Win32Thread
);
198 /* FIXME - unlock the process */
200 if (Type
== PsW32ThreadCalloutInitialize
)
202 HWINSTA hWinSta
= NULL
;
206 PUNICODE_STRING DesktopPath
;
207 PRTL_USER_PROCESS_PARAMETERS ProcessParams
= (Process
->Peb
? Process
->Peb
->ProcessParameters
: NULL
);
209 DPRINT("Creating W32 thread TID:%d at IRQ level: %lu\n", Thread
->Cid
.UniqueThread
, KeGetCurrentIrql());
211 InitializeListHead(&Win32Thread
->WindowListHead
);
212 InitializeListHead(&Win32Thread
->W32CallbackListHead
);
213 InitializeListHead(&Win32Thread
->PtiLink
);
216 * inherit the thread desktop and process window station (if not yet inherited) from the process startup
217 * info structure. See documentation of CreateProcess()
219 DesktopPath
= (ProcessParams
? ((ProcessParams
->DesktopInfo
.Length
> 0) ? &ProcessParams
->DesktopInfo
: NULL
) : NULL
);
220 Status
= IntParseDesktopPath(Process
,
224 if(NT_SUCCESS(Status
))
228 if(Process
!= CsrProcess
)
230 HWINSTA hProcessWinSta
= (HWINSTA
)InterlockedCompareExchangePointer((PVOID
)&Process
->Win32WindowStation
, (PVOID
)hWinSta
, NULL
);
231 if(hProcessWinSta
!= NULL
)
233 /* our process is already assigned to a different window station, we don't need the handle anymore */
245 PDESKTOP DesktopObject
;
246 Win32Thread
->Desktop
= NULL
;
247 Status
= ObReferenceObjectByHandle(hDesk
,
251 (PVOID
*)&DesktopObject
,
254 if(NT_SUCCESS(Status
))
256 if (!IntSetThreadDesktop(DesktopObject
,
259 DPRINT1("Unable to set thread desktop\n");
264 DPRINT1("Unable to reference thread desktop handle 0x%x\n", hDesk
);
268 Win32Thread
->IsExiting
= FALSE
;
269 co_IntDestroyCaret(Win32Thread
);
270 Win32Thread
->ppi
= PsGetCurrentProcessWin32Process();
271 pTeb
= NtCurrentTeb();
274 Win32Thread
->pClientInfo
= (PCLIENTINFO
)pTeb
->Win32ClientInfo
;
275 Win32Thread
->pClientInfo
->pClientThreadInfo
= NULL
;
277 Win32Thread
->MessageQueue
= MsqCreateMessageQueue(Thread
);
278 Win32Thread
->KeyboardLayout
= W32kGetDefaultKeyLayout();
282 PSINGLE_LIST_ENTRY e
;
284 DPRINT("Destroying W32 thread TID:%d at IRQ level: %lu\n", Thread
->Cid
.UniqueThread
, KeGetCurrentIrql());
286 Win32Thread
->IsExiting
= TRUE
;
287 HOOK_DestroyThreadHooks(Thread
);
288 UnregisterThreadHotKeys(Thread
);
289 /* what if this co_ func crash in umode? what will clean us up then? */
290 co_DestroyThreadWindows(Thread
);
291 IntBlockInput(Win32Thread
, FALSE
);
292 MsqDestroyMessageQueue(Win32Thread
->MessageQueue
);
293 IntCleanupThreadCallbacks(Win32Thread
);
295 /* cleanup user object references stack */
296 e
= PopEntryList(&Win32Thread
->ReferencesList
);
299 PUSER_REFERENCE_ENTRY ref
= CONTAINING_RECORD(e
, USER_REFERENCE_ENTRY
, Entry
);
300 DPRINT("thread clean: remove reference obj 0x%x\n",ref
->obj
);
301 UserDereferenceObject(ref
->obj
);
303 e
= PopEntryList(&Win32Thread
->ReferencesList
);
306 IntSetThreadDesktop(NULL
,
309 PsSetThreadWin32Thread(Thread
, NULL
);
312 RETURN( STATUS_SUCCESS
);
316 DPRINT("Leave Win32kThreadCallback, ret=%i\n",_ret_
);
320 /* Only used in ntuser/input.c KeyboardThreadMain(). If it's
321 not called there anymore, please delete */
323 Win32kInitWin32Thread(PETHREAD Thread
)
327 Process
= Thread
->ThreadsProcess
;
329 if (Process
->Win32Process
== NULL
)
331 /* FIXME - lock the process */
332 Process
->Win32Process
= ExAllocatePool(NonPagedPool
, sizeof(PROCESSINFO
));
334 if (Process
->Win32Process
== NULL
)
335 return STATUS_NO_MEMORY
;
337 RtlZeroMemory(Process
->Win32Process
, sizeof(PROCESSINFO
));
338 /* FIXME - unlock the process */
340 Win32kProcessCallback(Process
, TRUE
);
343 if (Thread
->Tcb
.Win32Thread
== NULL
)
345 Thread
->Tcb
.Win32Thread
= ExAllocatePool (NonPagedPool
, sizeof(THREADINFO
));
346 if (Thread
->Tcb
.Win32Thread
== NULL
)
347 return STATUS_NO_MEMORY
;
349 RtlZeroMemory(Thread
->Tcb
.Win32Thread
, sizeof(THREADINFO
));
351 Win32kThreadCallback(Thread
, PsW32ThreadCalloutInitialize
);
354 return(STATUS_SUCCESS
);
359 * This definition doesn't work
363 IN PDRIVER_OBJECT DriverObject
,
364 IN PUNICODE_STRING RegistryPath
)
368 WIN32_CALLOUTS_FPNS CalloutData
= {0};
369 PVOID GlobalUserHeapBase
= NULL
;
372 * Register user mode call interface
373 * (system service table index = 1)
375 Result
= KeAddSystemServiceTable (Win32kSSDT
,
377 Win32kNumberOfSysCalls
,
382 DPRINT1("Adding system services failed!\n");
383 return STATUS_UNSUCCESSFUL
;
386 hModuleWin
= MmPageEntireDriver(DriverEntry
);
389 * Register Object Manager Callbacks
391 CalloutData
.WindowStationParseProcedure
= IntWinStaObjectParse
;
392 CalloutData
.WindowStationDeleteProcedure
= IntWinStaObjectDelete
;
393 CalloutData
.DesktopDeleteProcedure
= IntDesktopObjectDelete
;
394 CalloutData
.ProcessCallout
= Win32kProcessCallback
;
395 CalloutData
.ThreadCallout
= Win32kThreadCallback
;
396 CalloutData
.BatchFlushRoutine
= NtGdiFlushUserBatch
;
399 * Register our per-process and per-thread structures.
401 PsEstablishWin32Callouts((PWIN32_CALLOUTS_FPNS
)&CalloutData
);
403 GlobalUserHeap
= UserCreateHeap(&GlobalUserHeapSection
,
405 1 * 1024 * 1024); /* FIXME - 1 MB for now... */
406 if (GlobalUserHeap
== NULL
)
408 DPRINT1("Failed to initialize the global heap!\n");
409 return STATUS_UNSUCCESSFUL
;
412 /* Initialize a list of loaded drivers in Win32 subsystem */
413 InitializeListHead(&GlobalDriverListHead
);
415 if(!hsemDriverMgmt
) hsemDriverMgmt
= EngCreateSemaphore();
417 GdiHandleTable
= GDIOBJ_iAllocHandleTable(&GdiTableSection
);
418 if (GdiHandleTable
== NULL
)
420 DPRINT1("Failed to initialize the GDI handle table.\n");
421 return STATUS_UNSUCCESSFUL
;
424 Status
= InitUserImpl();
425 if (!NT_SUCCESS(Status
))
427 DPRINT1("Failed to initialize user implementation!\n");
428 return STATUS_UNSUCCESSFUL
;
431 Status
= InitHotkeyImpl();
432 if (!NT_SUCCESS(Status
))
434 DPRINT1("Failed to initialize hotkey implementation!\n");
435 return STATUS_UNSUCCESSFUL
;
438 Status
= InitWindowStationImpl();
439 if (!NT_SUCCESS(Status
))
441 DPRINT1("Failed to initialize window station implementation!\n");
442 return STATUS_UNSUCCESSFUL
;
445 Status
= InitDesktopImpl();
446 if (!NT_SUCCESS(Status
))
448 DPRINT1("Failed to initialize desktop implementation!\n");
449 return STATUS_UNSUCCESSFUL
;
452 Status
= InitWindowImpl();
453 if (!NT_SUCCESS(Status
))
455 DPRINT1("Failed to initialize window implementation!\n");
456 return STATUS_UNSUCCESSFUL
;
459 Status
= InitMenuImpl();
460 if (!NT_SUCCESS(Status
))
462 DPRINT1("Failed to initialize menu implementation!\n");
463 return STATUS_UNSUCCESSFUL
;
466 Status
= InitInputImpl();
467 if (!NT_SUCCESS(Status
))
469 DPRINT1("Failed to initialize input implementation.\n");
473 Status
= InitKeyboardImpl();
474 if (!NT_SUCCESS(Status
))
476 DPRINT1("Failed to initialize keyboard implementation.\n");
480 Status
= InitMonitorImpl();
481 if (!NT_SUCCESS(Status
))
483 DbgPrint("Failed to initialize monitor implementation!\n");
484 return STATUS_UNSUCCESSFUL
;
487 Status
= MsqInitializeImpl();
488 if (!NT_SUCCESS(Status
))
490 DPRINT1("Failed to initialize message queue implementation.\n");
494 Status
= InitTimerImpl();
495 if (!NT_SUCCESS(Status
))
497 DPRINT1("Failed to initialize timer implementation.\n");
501 Status
= InitAcceleratorImpl();
502 if (!NT_SUCCESS(Status
))
504 DPRINT1("Failed to initialize accelerator implementation.\n");
508 Status
= InitGuiCheckImpl();
509 if (!NT_SUCCESS(Status
))
511 DPRINT1("Failed to initialize GUI check implementation.\n");
515 Status
= InitDcImpl();
516 if (!NT_SUCCESS(Status
))
518 DPRINT1("Failed to initialize Device context implementation!\n");
519 return STATUS_UNSUCCESSFUL
;
522 /* Initialize FreeType library */
523 if (! InitFontSupport())
525 DPRINT1("Unable to initialize font support\n");
526 return STATUS_UNSUCCESSFUL
;
531 /* Create stock objects, ie. precreated objects commonly
532 used by win32 applications */
533 CreateStockObjects();
534 CreateSysColorObjects();
536 gusLanguageID
= IntGdiGetLanguageID();
538 return STATUS_SUCCESS
;