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