update for HEAD-2003091401
[reactos.git] / subsys / win32k / main / dllmain.c
1 /*
2  *  ReactOS W32 Subsystem
3  *  Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 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 #undef WIN32_LEAN_AND_MEAN
25 #define WIN32_NO_STATUS
26 #include <windows.h>
27 #define NTOS_MODE_KERNEL
28 #include <ntos.h>
29 #include <ddk/winddi.h>
30
31 #include <win32k/win32k.h>
32
33 #include <include/winsta.h>
34 #include <include/class.h>
35 #include <include/window.h>
36 #include <include/menu.h>
37 #include <include/object.h>
38 #include <include/input.h>
39 #include <include/timer.h>
40 #include <include/text.h>
41
42 #define NDEBUG
43 #include <win32k/debug1.h>
44
45 extern SSDT Win32kSSDT[];
46 extern SSPT Win32kSSPT[];
47 extern ULONG Win32kNumberOfSysCalls;
48
49 PEPROCESS Win32kDeviceProcess;
50
51
52 NTSTATUS STDCALL
53 Win32kProcessCallback (struct _EPROCESS *Process,
54                      BOOLEAN Create)
55 {
56   PW32PROCESS Win32Process;
57   NTSTATUS Status;
58
59 #if 0
60   DbgPrint ("Win32kProcessCallback() called\n");
61 #endif
62
63   Win32Process = Process->Win32Process;
64   if (Create)
65     {
66 #if 0
67       DbgPrint ("  Create process\n");
68 #endif
69
70       InitializeListHead(&Win32Process->ClassListHead);
71       ExInitializeFastMutex(&Win32Process->ClassListLock);
72       
73       InitializeListHead(&Win32Process->MenuListHead);
74       ExInitializeFastMutex(&Win32Process->MenuListLock);      
75
76       Win32Process->WindowStation = NULL;
77       if (Process->Win32WindowStation != NULL)
78         {
79           Status = 
80             ValidateWindowStationHandle(Process->Win32WindowStation,
81                                         UserMode,
82                                         GENERIC_ALL,
83                                         &Win32Process->WindowStation);
84           if (!NT_SUCCESS(Status))
85             {
86               DbgPrint("Win32K: Failed to reference a window station for "
87                        "process.\n");
88             }
89         }
90     }
91   else
92     {
93 #if 0
94       DbgPrint ("  Destroy process\n");
95       DbgPrint ("  IRQ level: %lu\n", KeGetCurrentIrql ());
96 #endif
97
98       IntCleanupMenus(Process, Win32Process);
99
100       CleanupForProcess(Process, Process->UniqueProcessId);
101     }
102
103   return STATUS_SUCCESS;
104 }
105
106
107 NTSTATUS STDCALL
108 Win32kThreadCallback (struct _ETHREAD *Thread,
109                     BOOLEAN Create)
110 {
111   struct _EPROCESS *Process;
112   PW32THREAD Win32Thread;
113   NTSTATUS Status;
114
115 #if 0
116   DbgPrint ("Win32kThreadCallback() called\n");
117 #endif
118
119   Process = Thread->ThreadsProcess;
120   Win32Thread = Thread->Win32Thread;
121   if (Create)
122     {
123 #if 0
124       DbgPrint ("  Create thread\n");
125 #endif
126
127       Win32Thread->MessageQueue = MsqCreateMessageQueue();
128       InitializeListHead(&Win32Thread->WindowListHead);
129       ExInitializeFastMutex(&Win32Thread->WindowListLock);
130
131       /* By default threads get assigned their process's desktop. */
132       Win32Thread->Desktop = NULL;
133       if (Process->Win32Desktop != NULL)
134         {
135           Status = ObReferenceObjectByHandle(Process->Win32Desktop,
136                                              GENERIC_ALL,
137                                              ExDesktopObjectType,
138                                              UserMode,
139                                              (PVOID*)&Win32Thread->Desktop,
140                                              NULL);
141           if (!NT_SUCCESS(Status))
142             {
143               DbgPrint("Win32K: Failed to reference a desktop for thread.\n");
144             }
145         }
146     }
147   else
148     {
149 #if 0
150       DbgPrint ("  Destroy thread\n");
151 #endif
152
153       RemoveTimersThread(Thread->Cid.UniqueThread);
154       DestroyThreadWindows(Thread);
155     }
156
157   return STATUS_SUCCESS;
158 }
159
160
161 /*
162  * This definition doesn't work
163  */
164 // WINBOOL STDCALL DllMain(VOID)
165 NTSTATUS
166 STDCALL
167 DllMain (
168   IN    PDRIVER_OBJECT  DriverObject,
169   IN    PUNICODE_STRING RegistryPath)
170 {
171   NTSTATUS Status;
172   BOOLEAN Result;
173
174   IntInitializeWinLock();
175
176   /*
177    * Register user mode call interface
178    * (system service table index = 1)
179    */
180   Result = KeAddSystemServiceTable (Win32kSSDT,
181                                     NULL,
182                                     Win32kNumberOfSysCalls,
183                                     Win32kSSPT,
184                                     1);
185   if (Result == FALSE)
186     {
187       DbgPrint("Adding system services failed!\n");
188       return STATUS_UNSUCCESSFUL;
189     }
190
191   /*
192    * Register our per-process and per-thread structures.
193    */
194   PsEstablishWin32Callouts (Win32kProcessCallback,
195                             Win32kThreadCallback,
196                             0,
197                             0,
198                             sizeof(W32THREAD),
199                             sizeof(W32PROCESS));
200
201   WinPosSetupInternalPos();
202
203   Status = InitWindowStationImpl();
204   if (!NT_SUCCESS(Status))
205   {
206     DbgPrint("Failed to initialize window station implementation!\n");
207     return STATUS_UNSUCCESSFUL;
208   }
209
210   Status = InitClassImpl();
211   if (!NT_SUCCESS(Status))
212   {
213     DbgPrint("Failed to initialize window class implementation!\n");
214     return STATUS_UNSUCCESSFUL;
215   }
216
217   Status = InitWindowImpl();
218   if (!NT_SUCCESS(Status))
219   {
220     DbgPrint("Failed to initialize window implementation!\n");
221     return STATUS_UNSUCCESSFUL;
222   }
223   
224   Status = InitMenuImpl();
225   if (!NT_SUCCESS(Status))
226   {
227     DbgPrint("Failed to initialize menu implementation!\n");
228     return STATUS_UNSUCCESSFUL;
229   }  
230
231   Status = InitInputImpl();
232   if (!NT_SUCCESS(Status))
233     {
234       DbgPrint("Failed to initialize input implementation.\n");
235       return(Status);
236     }
237
238   Status = MsqInitializeImpl();
239   if (!NT_SUCCESS(Status))
240     {
241       DbgPrint("Failed to initialize message queue implementation.\n");
242       return(Status);
243     }
244
245   Status = InitTimerImpl();
246   if (!NT_SUCCESS(Status))
247     {
248       DbgPrint("Failed to initialize timer implementation.\n");
249       return(Status);
250     }
251
252   return STATUS_SUCCESS;
253 }
254
255
256 BOOLEAN
257 STDCALL
258 Win32kInitialize (VOID)
259 {
260   DPRINT("in Win32kInitialize\n");
261
262   Win32kDeviceProcess = PsGetCurrentProcess();
263
264   InitGdiObjectHandleTable ();
265
266   // Initialize FreeType library
267   if(!InitFontSupport()) return FALSE;
268
269   // Create stock objects, ie. precreated objects commonly used by win32 applications
270   CreateStockObjects();
271
272   return TRUE;
273 }
274
275 /* EOF */