some more video code
[reactos.git] / reactos / subsys / win32k / misc / driver.c
1 /* $Id: driver.c,v 1.6 1999/12/09 02:45:05 rex Exp $
2 *
3 * GDI Driver support routines
4 * (mostly swiped from Wine)
5 *
6 */
7
8 #undef WIN32_LEAN_AND_MEAN
9 #include <ddk/ntddk.h>
10 #include <windows.h>
11 #include <win32k/driver.h>
12 #include <wchar.h>
13
14 //#define NDEBUG
15 #include <internal/debug.h>
16
17 typedef struct _GRAPHICS_DRIVER
18 {
19 PWSTR Name;
20 PGD_ENABLEDRIVER EnableDriver;
21 int ReferenceCount;
22 struct _GRAPHICS_DRIVER *Next;
23 } GRAPHICS_DRIVER, *PGRAPHICS_DRIVER;
24
25 static PGRAPHICS_DRIVER DriverList;
26 static PGRAPHICS_DRIVER GenericDriver;
27
28 BOOL DRIVER_RegisterDriver(LPCWSTR Name, PGD_ENABLEDRIVER EnableDriver)
29 {
30 PGRAPHICS_DRIVER Driver = ExAllocatePool(NonPagedPool, sizeof(*Driver));
31 if (!Driver)
32 {
33 return FALSE;
34 }
35 Driver->ReferenceCount = 0;
36 Driver->EnableDriver = EnableDriver;
37 if (Name)
38 {
39 Driver->Name = ExAllocatePool(NonPagedPool,
40 (wcslen(Name) + 1) * sizeof(WCHAR));
41 wcscpy(Driver->Name, Name);
42 Driver->Next = DriverList;
43 DriverList = Driver;
44 return TRUE;
45 }
46
47 if (GenericDriver != NULL)
48 {
49 ExFreePool(Driver);
50
51 return FALSE;
52 }
53
54 Driver->Name = NULL;
55 GenericDriver = Driver;
56 return TRUE;
57 }
58
59 PGD_ENABLEDRIVER DRIVER_FindDDIDriver(LPCWSTR Name)
60 {
61 GRAPHICS_DRIVER *Driver = DriverList;
62
63 while (Driver && Name)
64 {
65 if (!_wcsicmp( Driver->Name, Name))
66 {
67 return Driver->EnableDriver;
68 }
69 Driver = Driver->Next;
70 }
71
72 return GenericDriver ? GenericDriver->EnableDriver : NULL;
73 }
74
75 BOOL DRIVER_BuildDDIFunctions(PDRVENABLEDATA DED,
76 PDRIVER_FUNCTIONS DF)
77 {
78 UNIMPLEMENTED;
79 }
80
81 HANDLE DRIVER_FindMPDriver(LPCWSTR Name)
82 {
83 PWSTR lName;
84 HANDLE DriverHandle;
85 NTSTATUS Status;
86 UNICODE_STRING DeviceName;
87 OBJECT_ATTRIBUTES ObjectAttributes;
88
89 if (Name[0] != '\\')
90 {
91 lName = ExAllocatePool(NonPagedPool, wcslen(Name) * sizeof(WCHAR) +
92 10 * sizeof(WCHAR));
93 wcscpy(lName, L"\\Devices\\");
94 wcscat(lName, Name);
95 }
96 else
97 {
98 lName = ExAllocatePool(NonPagedPool, wcslen(Name) * sizeof(WCHAR));
99 wcscpy(lName, Name);
100 }
101
102 RtlInitUnicodeString(&DeviceName, lName);
103 InitializeObjectAttributes(&ObjectAttributes,
104 &DeviceName,
105 0,
106 NULL,
107 NULL);
108 Status = ZwOpenFile(&DriverHandle,
109 FILE_ALL_ACCESS,
110 &ObjectAttributes,
111 NULL,
112 0,
113 FILE_SYNCHRONOUS_IO_ALERT);
114 if (!NT_SUCCESS(Status))
115 {
116 DbgPrint("Failed to open display device\n");
117 DbgPrint("%08lx\n", Status);
118 if (Name[0] != '\\')
119 {
120 ExFreePool(lName);
121 }
122
123 return NULL;
124 }
125
126 if (Name[0] != '\\')
127 {
128 ExFreePool(lName);
129 }
130
131 return DriverHandle;
132 }
133
134 BOOL DRIVER_UnregisterDriver(LPCWSTR Name)
135 {
136 PGRAPHICS_DRIVER Driver = NULL;
137
138 if (Name)
139 {
140 if (DriverList != NULL)
141 {
142 if (!_wcsicmp(DriverList->Name, Name))
143 {
144 Driver = DriverList;
145 DriverList = DriverList->Next;
146 }
147 else
148 {
149 Driver = DriverList;
150 while (Driver->Next && _wcsicmp(Driver->Name, Name))
151 {
152 Driver = Driver->Next;
153 }
154 }
155 }
156 }
157 else
158 {
159 if (GenericDriver != NULL)
160 {
161 Driver = GenericDriver;
162 GenericDriver = NULL;
163 }
164 }
165
166 if (Driver != NULL)
167 {
168 ExFreePool(Driver->Name);
169 ExFreePool(Driver);
170
171 return TRUE;
172 }
173 else
174 {
175 return FALSE;
176 }
177 }
178
179 INT DRIVER_ReferenceDriver (LPCWSTR Name)
180 {
181 GRAPHICS_DRIVER *Driver = DriverList;
182
183 while (Driver && Name)
184 {
185 if (!_wcsicmp( Driver->Name, Name))
186 {
187 return ++Driver->ReferenceCount;
188 }
189 Driver = Driver->Next;
190 }
191
192 return GenericDriver ? ++GenericDriver->ReferenceCount : 0;
193 }
194
195 INT DRIVER_UnreferenceDriver (LPCWSTR Name)
196 {
197 GRAPHICS_DRIVER *Driver = DriverList;
198
199 while (Driver && Name)
200 {
201 if (!_wcsicmp( Driver->Name, Name))
202 {
203 return --Driver->ReferenceCount;
204 }
205 Driver = Driver->Next;
206 }
207
208 return GenericDriver ? --GenericDriver->ReferenceCount : 0;
209 }
210