update for HEAD-2003091401
[reactos.git] / subsys / csrss / init.c
1 /* $Id$
2  * 
3  * reactos/subsys/csrss/init.c
4  *
5  * Initialize the CSRSS subsystem server process.
6  *
7  * ReactOS Operating System
8  *
9  */
10
11 /* INCLUDES ******************************************************************/
12
13 #include <ddk/ntddk.h>
14 #include <ntdll/rtl.h>
15 #include <csrss/csrss.h>
16 #include <win32k/win32k.h>
17
18 #include "api.h"
19
20 #define NDEBUG
21 #include <debug.h>
22
23 /* GLOBALS ******************************************************************/
24
25 /*
26  * Server's named ports.
27  */
28 static HANDLE ApiPortHandle;
29
30
31 HANDLE CsrInitEvent = INVALID_HANDLE_VALUE;
32 HANDLE CsrHeap = INVALID_HANDLE_VALUE;
33
34 HANDLE CsrObjectDirectory = INVALID_HANDLE_VALUE;
35 HANDLE CsrApiPort = INVALID_HANDLE_VALUE;
36 HANDLE CsrSbApiPort = INVALID_HANDLE_VALUE;
37
38 UNICODE_STRING CsrDirectoryName;
39
40 extern HANDLE CsrssApiHeap;
41
42 ULONG
43 InitializeVideoAddressSpace(VOID);
44
45 static NTSTATUS
46 CsrParseCommandLine (
47         ULONG ArgumentCount,
48         PWSTR *ArgumentArray
49         )
50 {
51    NTSTATUS Status;
52    OBJECT_ATTRIBUTES Attributes;
53    ANSI_STRING       AnsiString;
54
55    ULONG i;
56
57    /*   DbgPrint ("Arguments: %ld\n", ArgumentCount);
58    for (i = 0; i < ArgumentCount; i++)
59      {
60         DbgPrint ("Argument %ld: %S\n", i, ArgumentArray[i]);
61         }*/
62
63
64         /* create object directory ('\Windows') */
65         RtlCreateUnicodeString (&CsrDirectoryName,
66                                 L"\\Windows");
67
68         InitializeObjectAttributes (&Attributes,
69                                     &CsrDirectoryName,
70                                     0,
71                                     NULL,
72                                     NULL);
73
74         Status = NtCreateDirectoryObject(&CsrObjectDirectory,
75                                          0xF000F,
76                                          &Attributes);
77
78         return Status;
79 }
80
81
82 static VOID
83 CsrInitVideo(VOID)
84 {
85   OBJECT_ATTRIBUTES ObjectAttributes;
86   UNICODE_STRING DeviceName;
87   IO_STATUS_BLOCK Iosb;
88   HANDLE VideoHandle;
89   NTSTATUS Status;
90
91   InitializeVideoAddressSpace();
92
93   RtlInitUnicodeStringFromLiteral(&DeviceName, L"\\??\\DISPLAY1");
94   InitializeObjectAttributes(&ObjectAttributes,
95                              &DeviceName,
96                              0,
97                              NULL,
98                              NULL);
99   Status = NtOpenFile(&VideoHandle,
100                       FILE_ALL_ACCESS,
101                       &ObjectAttributes,
102                       &Iosb,
103                       0,
104                       0);
105   if (NT_SUCCESS(Status))
106     {
107       NtClose(VideoHandle);
108     }
109 }
110
111
112 /**********************************************************************
113  * NAME
114  *      CsrServerInitialization
115  *
116  * DESCRIPTION
117  *      Create a directory object (\windows) and two named LPC ports:
118  *
119  *      1. \windows\ApiPort
120  *      2. \windows\SbApiPort
121  *
122  * RETURN VALUE
123  *      TRUE: Initialization OK; otherwise FALSE.
124  */
125 BOOL
126 STDCALL
127 CsrServerInitialization (
128         ULONG ArgumentCount,
129         PWSTR *ArgumentArray
130         )
131 {
132    NTSTATUS             Status;
133    OBJECT_ATTRIBUTES    ObAttributes;
134    UNICODE_STRING PortName;
135    OBJECT_ATTRIBUTES RefreshEventAttr;
136    UNICODE_STRING RefreshEventName;
137    HANDLE RefreshEventHandle;
138
139    Status = CsrParseCommandLine (ArgumentCount, ArgumentArray);
140    if (!NT_SUCCESS(Status))
141      {
142         DPRINT1("CSR: Unable to parse the command line (Status: %x)\n", Status);
143         return(FALSE);
144      }
145
146    CsrInitVideo();
147
148    /* NEW NAMED PORT: \ApiPort */
149    RtlInitUnicodeStringFromLiteral(&PortName, L"\\Windows\\ApiPort");
150    InitializeObjectAttributes(&ObAttributes,
151                               &PortName,
152                               0,
153                               NULL,
154                               NULL);
155
156    Status = NtCreatePort(&ApiPortHandle,
157                          &ObAttributes,
158                          260,
159                          328,
160                          0);
161    if (!NT_SUCCESS(Status))
162      {
163         DPRINT1("CSR: Unable to create \\ApiPort (Status %x)\n", Status);
164         return(FALSE);
165      }
166    CsrssApiHeap = RtlCreateHeap(HEAP_GROWABLE,
167                                 NULL,
168                                 65536,
169                                 65536,
170                                 NULL,
171                                 NULL);
172    if (CsrssApiHeap == NULL)
173      {
174         DPRINT1("CSR: Failed to create private heap, aborting\n");
175         return FALSE;
176      }
177
178    CsrInitConsoleSupport();
179    Status = RtlCreateUserThread(NtCurrentProcess(),
180                                 NULL,
181                                 FALSE,
182                                 0,
183                                 NULL,
184                                 NULL,
185                                 (PTHREAD_START_ROUTINE)Thread_Api,
186                                 ApiPortHandle,
187                                 NULL,
188                                 NULL);
189    if (!NT_SUCCESS(Status))
190      {
191         DPRINT1("CSR: Unable to create server thread\n");
192         NtClose(ApiPortHandle);
193         return FALSE;
194      }
195    RtlInitUnicodeStringFromLiteral( &RefreshEventName, L"\\TextConsoleRefreshEvent" );
196    InitializeObjectAttributes( &RefreshEventAttr, &RefreshEventName, 0, NULL, NULL );
197    Status = NtCreateEvent( &RefreshEventHandle, STANDARD_RIGHTS_ALL, &RefreshEventAttr, FALSE, FALSE );
198    if( !NT_SUCCESS( Status ) )
199      {
200        DPRINT1( "CSR: Unable to create refresh event!\n" );
201        return FALSE;
202      }
203    Status = RtlCreateUserThread( NtCurrentProcess(), NULL, FALSE, 0, NULL, NULL, (PTHREAD_START_ROUTINE)Console_Api, (PVOID) RefreshEventHandle, NULL, NULL );
204    if( !NT_SUCCESS( Status ) )
205      {
206        DPRINT1( "CSR: Unable to create console thread\n" );
207        return FALSE;
208      }
209
210    Win32kInitialize();
211
212    return TRUE;
213 }
214
215 /* EOF */