a464fede37951b723013d73c005b2b4ab71b0454
[reactos.git] / reactos / subsys / win32k / objects / gdiobj.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 /*
20 * GDIOBJ.C - GDI object manipulation routines
21 *
22 * $Id: gdiobj.c,v 1.45 2003/10/04 21:09:29 gvg Exp $
23 *
24 */
25
26 #undef WIN32_LEAN_AND_MEAN
27 #define WIN32_NO_STATUS
28 #include <windows.h>
29 #include <ddk/ntddk.h>
30 #include <include/dce.h>
31 #include <include/object.h>
32 #include <win32k/gdiobj.h>
33 #include <win32k/brush.h>
34 #include <win32k/pen.h>
35 #include <win32k/text.h>
36 #include <win32k/dc.h>
37 #include <win32k/bitmaps.h>
38 #include <win32k/region.h>
39 #include <win32k/cursoricon.h>
40 #include <include/palette.h>
41 #define NDEBUG
42 #include <win32k/debug1.h>
43
44 /*! Size of the GDI handle table
45 * http://www.windevnet.com/documents/s=7290/wdj9902b/9902b.htm
46 * gdi handle table can hold 0x4000 handles
47 */
48 #define GDI_HANDLE_COUNT 0x4000
49
50 #define GDI_GLOBAL_PROCESS ((HANDLE) 0xffffffff)
51
52 #define GDI_HANDLE_INDEX_MASK (GDI_HANDLE_COUNT - 1)
53 #define GDI_HANDLE_TYPE_MASK 0x007f0000
54 #define GDI_HANDLE_STOCK_MASK 0x00800000
55
56 #define GDI_HANDLE_CREATE(i, t) ((HANDLE)(((i) & GDI_HANDLE_INDEX_MASK) | ((t) & GDI_HANDLE_TYPE_MASK)))
57 #define GDI_HANDLE_GET_INDEX(h) (((DWORD)(h)) & GDI_HANDLE_INDEX_MASK)
58 #define GDI_HANDLE_GET_TYPE(h) (((DWORD)(h)) & GDI_HANDLE_TYPE_MASK)
59 #define GDI_HANDLE_IS_TYPE(h, t) ((t) == (((DWORD)(h)) & GDI_HANDLE_TYPE_MASK))
60 #define GDI_HANDLE_IS_STOCKOBJ(h) (0 != (((DWORD)(h)) & GDI_HANDLE_STOCK_MASK))
61 #define GDI_HANDLE_SET_STOCKOBJ(h) ((h) = (HANDLE)(((DWORD)(h)) | GDI_HANDLE_STOCK_MASK))
62
63 #define GDI_TYPE_TO_MAGIC(t) ((WORD) ((t) >> 16))
64 #define GDI_MAGIC_TO_TYPE(m) ((DWORD)(m) << 16)
65
66 #define GDI_VALID_OBJECT(h, obj, t, f) \
67 (NULL != (obj) \
68 && (GDI_MAGIC_TO_TYPE((obj)->Magic) == (t) || GDI_OBJECT_TYPE_DONTCARE == (t)) \
69 && (GDI_HANDLE_GET_TYPE((h)) == GDI_MAGIC_TO_TYPE((obj)->Magic)) \
70 && (((obj)->hProcessId == PsGetCurrentProcessId()) \
71 || (GDI_GLOBAL_PROCESS == (obj)->hProcessId) \
72 || ((f) & GDIOBJFLAG_IGNOREPID)))
73
74 typedef struct _GDI_HANDLE_TABLE
75 {
76 WORD wTableSize;
77 PGDIOBJHDR Handles[1];
78 } GDI_HANDLE_TABLE, *PGDI_HANDLE_TABLE;
79
80 /* GDI stock objects */
81
82 static LOGBRUSH WhiteBrush =
83 { BS_SOLID, RGB(255,255,255), 0 };
84
85 static LOGBRUSH LtGrayBrush =
86 /* FIXME : this should perhaps be BS_HATCHED, at least for 1 bitperpixel */
87 { BS_SOLID, RGB(192,192,192), 0 };
88
89 static LOGBRUSH GrayBrush =
90 /* FIXME : this should perhaps be BS_HATCHED, at least for 1 bitperpixel */
91 { BS_SOLID, RGB(128,128,128), 0 };
92
93 static LOGBRUSH DkGrayBrush =
94 /* This is BS_HATCHED, for 1 bitperpixel. This makes the spray work in pbrush */
95 /* NB_HATCH_STYLES is an index into HatchBrushes */
96 { BS_HATCHED, RGB(0,0,0), NB_HATCH_STYLES };
97
98 static LOGBRUSH BlackBrush =
99 { BS_SOLID, RGB(0,0,0), 0 };
100
101 static LOGBRUSH NullBrush =
102 { BS_NULL, 0, 0 };
103
104 static LOGPEN WhitePen =
105 { PS_SOLID, { 0, 0 }, RGB(255,255,255) };
106
107 static LOGPEN BlackPen =
108 { PS_SOLID, { 0, 0 }, RGB(0,0,0) };
109
110 static LOGPEN NullPen =
111 { PS_NULL, { 0, 0 }, 0 };
112
113 static LOGFONTW OEMFixedFont =
114 { 14, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, OEM_CHARSET,
115 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, L"" };
116
117 static LOGFONTW AnsiFixedFont =
118 { 14, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
119 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, L"" };
120
121 /*static LOGFONTW AnsiVarFont =
122 *{ 14, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
123 * 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS, L"MS Sans Serif" }; */
124
125 static LOGFONTW SystemFont =
126 { 14, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
127 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS, L"System" };
128
129 static LOGFONTW DeviceDefaultFont =
130 { 14, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
131 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS, L"" };
132
133 static LOGFONTW SystemFixedFont =
134 { 14, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
135 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, L"" };
136
137 /* FIXME: Is this correct? */
138 static LOGFONTW DefaultGuiFont =
139 { 14, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
140 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS, L"MS Sans Serif" };
141
142 #define NB_STOCK_OBJECTS (DEFAULT_GUI_FONT + 1)
143
144 static HGDIOBJ *StockObjects[NB_STOCK_OBJECTS];
145 static PGDI_HANDLE_TABLE HandleTable = 0;
146 static FAST_MUTEX HandleTableMutex;
147 static FAST_MUTEX RefCountHandling;
148
149 /*!
150 * Allocate GDI object table.
151 * \param Size - number of entries in the object table.
152 */
153 static PGDI_HANDLE_TABLE FASTCALL
154 GDIOBJ_iAllocHandleTable (WORD Size)
155 {
156 PGDI_HANDLE_TABLE handleTable;
157
158 ExAcquireFastMutexUnsafe (&HandleTableMutex);
159 handleTable = ExAllocatePool(PagedPool,
160 sizeof(GDI_HANDLE_TABLE) +
161 sizeof(PGDIOBJ) * Size);
162 ASSERT( handleTable );
163 memset (handleTable,
164 0,
165 sizeof(GDI_HANDLE_TABLE) + sizeof(PGDIOBJ) * Size);
166 handleTable->wTableSize = Size;
167 ExReleaseFastMutexUnsafe (&HandleTableMutex);
168
169 return handleTable;
170 }
171
172 /*!
173 * Returns the entry into the handle table by index.
174 */
175 static PGDIOBJHDR FASTCALL
176 GDIOBJ_iGetObjectForIndex(WORD TableIndex)
177 {
178 if (0 == TableIndex || HandleTable->wTableSize < TableIndex)
179 {
180 DPRINT1("Invalid TableIndex %u\n", (unsigned) TableIndex);
181 return NULL;
182 }
183
184 return HandleTable->Handles[TableIndex];
185 }
186
187 /*!
188 * Finds next free entry in the GDI handle table.
189 * \return index into the table is successful, zero otherwise.
190 */
191 static WORD FASTCALL
192 GDIOBJ_iGetNextOpenHandleIndex (void)
193 {
194 WORD tableIndex;
195
196 ExAcquireFastMutexUnsafe (&HandleTableMutex);
197 for (tableIndex = 1; tableIndex < HandleTable->wTableSize; tableIndex++)
198 {
199 if (NULL == HandleTable->Handles[tableIndex])
200 {
201 HandleTable->Handles[tableIndex] = (PGDIOBJHDR) -1;
202 break;
203 }
204 }
205 ExReleaseFastMutexUnsafe (&HandleTableMutex);
206
207 return (tableIndex < HandleTable->wTableSize) ? tableIndex : 0;
208 }
209
210 /*!
211 * Allocate memory for GDI object and return handle to it.
212 *
213 * \param Size - size of the GDI object. This shouldn't to include the size of GDIOBJHDR.
214 * The actual amount of allocated memory is sizeof(GDIOBJHDR)+Size
215 * \param ObjectType - type of object \ref GDI object types
216 * \param CleanupProcPtr - Routine to be called on destruction of object
217 *
218 * \return Handle of the allocated object.
219 *
220 * \note Use GDIOBJ_Lock() to obtain pointer to the new object.
221 */
222 HGDIOBJ FASTCALL
223 GDIOBJ_AllocObj(WORD Size, DWORD ObjectType, GDICLEANUPPROC CleanupProc)
224 {
225 PGDIOBJHDR newObject;
226 WORD Index;
227
228 Index = GDIOBJ_iGetNextOpenHandleIndex ();
229 if (0 == Index)
230 {
231 DPRINT1("Out of GDI handles\n");
232 return NULL;
233 }
234
235 DPRINT("GDIOBJ_AllocObj: handle: %d, size: %d, type: 0x%08x\n", Index, Size, ObjectType);
236 newObject = ExAllocatePool(PagedPool, Size + sizeof (GDIOBJHDR));
237 if (newObject == NULL)
238 {
239 DPRINT1("GDIOBJ_AllocObj: failed\n");
240 return NULL;
241 }
242 RtlZeroMemory (newObject, Size + sizeof(GDIOBJHDR));
243
244 newObject->wTableIndex = Index;
245
246 newObject->dwCount = 0;
247 newObject->hProcessId = PsGetCurrentProcessId ();
248 newObject->CleanupProc = CleanupProc;
249 newObject->Magic = GDI_TYPE_TO_MAGIC(ObjectType);
250 newObject->lockfile = NULL;
251 newObject->lockline = 0;
252 HandleTable->Handles[Index] = newObject;
253
254 return GDI_HANDLE_CREATE(Index, ObjectType);
255 }
256
257 /*!
258 * Free memory allocated for the GDI object. For each object type this function calls the
259 * appropriate cleanup routine.
260 *
261 * \param hObj - handle of the object to be deleted.
262 * \param ObjectType - one of the \ref GDI object types
263 * or GDI_OBJECT_TYPE_DONTCARE.
264 * \param Flag - if set to GDIOBJFLAG_IGNOREPID then the routine doesn't check if the process that
265 * tries to delete the object is the same one that created it.
266 *
267 * \return Returns TRUE if succesful.
268 *
269 * \note You should only use GDIOBJFLAG_IGNOREPID if you are cleaning up after the process that terminated.
270 * \note This function deferres object deletion if it is still in use.
271 */
272 BOOL STDCALL
273 GDIOBJ_FreeObj(HGDIOBJ hObj, DWORD ObjectType, DWORD Flag)
274 {
275 PGDIOBJHDR objectHeader;
276 PGDIOBJ Obj;
277 BOOL bRet = TRUE;
278
279 objectHeader = GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(hObj));
280 DPRINT("GDIOBJ_FreeObj: hObj: 0x%08x, object: %x\n", hObj, objectHeader);
281
282 if (! GDI_VALID_OBJECT(hObj, objectHeader, ObjectType, Flag)
283 || GDI_GLOBAL_PROCESS == objectHeader->hProcessId)
284
285 {
286 DPRINT1("Can't delete hObj:0x%08x, type:0x%08x, flag:%d\n", hObj, ObjectType, Flag);
287 return FALSE;
288 }
289
290 DPRINT("FreeObj: locks: %x\n", objectHeader->dwCount );
291 if (!(Flag & GDIOBJFLAG_IGNORELOCK))
292 {
293 /* check that the reference count is zero. if not then set flag
294 * and delete object when releaseobj is called */
295 ExAcquireFastMutex(&RefCountHandling);
296 if ((objectHeader->dwCount & ~0x80000000) > 0 )
297 {
298 DPRINT("GDIOBJ_FreeObj: delayed object deletion: count %d\n", objectHeader->dwCount);
299 objectHeader->dwCount |= 0x80000000;
300 ExReleaseFastMutex(&RefCountHandling);
301 return TRUE;
302 }
303 ExReleaseFastMutex(&RefCountHandling);
304 }
305
306 /* allow object to delete internal data */
307 if (NULL != objectHeader->CleanupProc)
308 {
309 Obj = (PGDIOBJ)((PCHAR)objectHeader + sizeof(GDIOBJHDR));
310 bRet = (*(objectHeader->CleanupProc))(Obj);
311 }
312
313 ExFreePool(objectHeader);
314 HandleTable->Handles[GDI_HANDLE_GET_INDEX(hObj)] = NULL;
315
316 return bRet;
317 }
318
319 /*!
320 * Lock multiple objects. Use this function when you need to lock multiple objects and some of them may be
321 * duplicates. You should use this function to avoid trying to lock the same object twice!
322 *
323 * \param pList pointer to the list that contains handles to the objects. You should set hObj and ObjectType fields.
324 * \param nObj number of objects to lock
325 * \return for each entry in pList this function sets pObj field to point to the object.
326 *
327 * \note this function uses an O(n^2) algoritm because we shouldn't need to call it with more than 3 or 4 objects.
328 */
329 BOOL FASTCALL
330 GDIOBJ_LockMultipleObj(PGDIMULTILOCK pList, INT nObj)
331 {
332 INT i, j;
333 ASSERT( pList );
334 /* FIXME - check for "invalid" handles */
335 /* go through the list checking for duplicate objects */
336 for (i = 0; i < nObj; i++)
337 {
338 pList[i].pObj = NULL;
339 for (j = 0; j < i; j++)
340 {
341 if (pList[i].hObj == pList[j].hObj)
342 {
343 /* already locked, so just copy the pointer to the object */
344 pList[i].pObj = pList[j].pObj;
345 break;
346 }
347 }
348
349 if (NULL == pList[i].pObj)
350 {
351 /* object hasn't been locked, so lock it. */
352 if (NULL != pList[i].hObj)
353 {
354 pList[i].pObj = GDIOBJ_LockObj(pList[i].hObj, pList[i].ObjectType);
355 }
356 }
357 }
358
359 return TRUE;
360 }
361
362 /*!
363 * Unlock multiple objects. Use this function when you need to unlock multiple objects and some of them may be
364 * duplicates.
365 *
366 * \param pList pointer to the list that contains handles to the objects. You should set hObj and ObjectType fields.
367 * \param nObj number of objects to lock
368 *
369 * \note this function uses O(n^2) algoritm because we shouldn't need to call it with more than 3 or 4 objects.
370 */
371 BOOL FASTCALL
372 GDIOBJ_UnlockMultipleObj(PGDIMULTILOCK pList, INT nObj)
373 {
374 INT i, j;
375 ASSERT(pList);
376
377 /* go through the list checking for duplicate objects */
378 for (i = 0; i < nObj; i++)
379 {
380 if (NULL != pList[i].pObj)
381 {
382 for (j = i + 1; j < nObj; j++)
383 {
384 if ((pList[i].pObj == pList[j].pObj))
385 {
386 /* set the pointer to zero for all duplicates */
387 pList[j].pObj = NULL;
388 }
389 }
390 GDIOBJ_UnlockObj(pList[i].hObj, pList[i].ObjectType);
391 pList[i].pObj = NULL;
392 }
393 }
394
395 return TRUE;
396 }
397
398 /*!
399 * Marks the object as global. (Creator process ID is set to GDI_GLOBAL_PROCESS). Global objects may be
400 * accessed by any process.
401 * \param ObjectHandle - handle of the object to make global.
402 *
403 * \note Only stock objects should be marked global.
404 */
405 VOID FASTCALL
406 GDIOBJ_MarkObjectGlobal(HGDIOBJ ObjectHandle)
407 {
408 PGDIOBJHDR ObjHdr;
409
410 DPRINT("GDIOBJ_MarkObjectGlobal handle 0x%08x\n", ObjectHandle);
411 ObjHdr = GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(ObjectHandle));
412 if (NULL == ObjHdr)
413 {
414 return;
415 }
416
417 ObjHdr->hProcessId = GDI_GLOBAL_PROCESS;
418 }
419
420 /*!
421 * Removes the global mark from the object. Global objects may be
422 * accessed by any process.
423 * \param ObjectHandle - handle of the object to make local.
424 *
425 * \note Only stock objects should be marked global.
426 */
427 VOID FASTCALL
428 GDIOBJ_UnmarkObjectGlobal(HGDIOBJ ObjectHandle)
429 {
430 PGDIOBJHDR ObjHdr;
431
432 DPRINT("GDIOBJ_MarkObjectGlobal handle 0x%08x\n", ObjectHandle);
433 ObjHdr = GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(ObjectHandle));
434 if (NULL == ObjHdr || GDI_GLOBAL_PROCESS != ObjHdr->hProcessId)
435 {
436 return;
437 }
438
439 ObjHdr->hProcessId = PsGetCurrentProcessId();
440 }
441
442 /*!
443 * Get the type of the object.
444 * \param ObjectHandle - handle of the object.
445 * \return One of the \ref GDI object types
446 */
447 DWORD FASTCALL
448 GDIOBJ_GetObjectType(HGDIOBJ ObjectHandle)
449 {
450 PGDIOBJHDR ObjHdr;
451
452 ObjHdr = GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(ObjectHandle));
453 if (NULL == ObjHdr
454 || ! GDI_VALID_OBJECT(ObjectHandle, ObjHdr, GDI_MAGIC_TO_TYPE(ObjHdr->Magic), 0))
455 {
456 DPRINT1("Invalid ObjectHandle 0x%08x\n", ObjectHandle);
457 return 0;
458 }
459 DPRINT("GDIOBJ_GetObjectType for handle 0x%08x returns 0x%08x\n", ObjectHandle,
460 GDI_MAGIC_TO_TYPE(ObjHdr->Magic));
461
462 return GDI_MAGIC_TO_TYPE(ObjHdr->Magic);
463 }
464
465 /*!
466 * Initialization of the GDI object engine.
467 */
468 VOID FASTCALL
469 InitGdiObjectHandleTable (VOID)
470 {
471 DPRINT("InitGdiObjectHandleTable\n");
472 ExInitializeFastMutex (&HandleTableMutex);
473 ExInitializeFastMutex (&RefCountHandling);
474
475 HandleTable = GDIOBJ_iAllocHandleTable (GDI_HANDLE_COUNT);
476 DPRINT("HandleTable: %x\n", HandleTable );
477
478 InitEngHandleTable();
479 }
480
481 /*!
482 * Creates a bunch of stock objects: brushes, pens, fonts.
483 */
484 VOID FASTCALL
485 CreateStockObjects(void)
486 {
487 unsigned Object;
488
489 DPRINT("Beginning creation of stock objects\n");
490
491 /* Create GDI Stock Objects from the logical structures we've defined */
492
493 StockObjects[WHITE_BRUSH] = NtGdiCreateBrushIndirect(&WhiteBrush);
494 StockObjects[LTGRAY_BRUSH] = NtGdiCreateBrushIndirect(&LtGrayBrush);
495 StockObjects[GRAY_BRUSH] = NtGdiCreateBrushIndirect(&GrayBrush);
496 StockObjects[DKGRAY_BRUSH] = NtGdiCreateBrushIndirect(&DkGrayBrush);
497 StockObjects[BLACK_BRUSH] = NtGdiCreateBrushIndirect(&BlackBrush);
498 StockObjects[NULL_BRUSH] = NtGdiCreateBrushIndirect(&NullBrush);
499
500 StockObjects[WHITE_PEN] = NtGdiCreatePenIndirect(&WhitePen);
501 StockObjects[BLACK_PEN] = NtGdiCreatePenIndirect(&BlackPen);
502 StockObjects[NULL_PEN] = NtGdiCreatePenIndirect(&NullPen);
503
504 (void) TextIntCreateFontIndirect(&OEMFixedFont, (HFONT*)&StockObjects[OEM_FIXED_FONT]);
505 (void) TextIntCreateFontIndirect(&AnsiFixedFont, (HFONT*)&StockObjects[ANSI_FIXED_FONT]);
506 (void) TextIntCreateFontIndirect(&SystemFont, (HFONT*)&StockObjects[SYSTEM_FONT]);
507 (void) TextIntCreateFontIndirect(&DeviceDefaultFont, (HFONT*)&StockObjects[DEVICE_DEFAULT_FONT]);
508 (void) TextIntCreateFontIndirect(&SystemFixedFont, (HFONT*)&StockObjects[SYSTEM_FIXED_FONT]);
509 (void) TextIntCreateFontIndirect(&DefaultGuiFont, (HFONT*)&StockObjects[DEFAULT_GUI_FONT]);
510
511 StockObjects[DEFAULT_PALETTE] = (HGDIOBJ*)PALETTE_Init();
512
513 for (Object = 0; Object < NB_STOCK_OBJECTS; Object++)
514 {
515 if (NULL != StockObjects[Object])
516 {
517 GDIOBJ_MarkObjectGlobal(StockObjects[Object]);
518 /* GDI_HANDLE_SET_STOCKOBJ(StockObjects[Object]);*/
519 }
520 }
521
522 DPRINT("Completed creation of stock objects\n");
523 }
524
525 /*!
526 * Return stock object.
527 * \param Object - stock object id.
528 * \return Handle to the object.
529 */
530 HGDIOBJ STDCALL
531 NtGdiGetStockObject(INT Object)
532 {
533 DPRINT("NtGdiGetStockObject index %d\n", Object);
534
535 return ((Object < 0) || (NB_STOCK_OBJECTS <= Object)) ? NULL : StockObjects[Object];
536 }
537
538 /*!
539 * Delete GDI object
540 * \param hObject object handle
541 * \return if the function fails the returned value is FALSE.
542 */
543 BOOL STDCALL
544 NtGdiDeleteObject(HGDIOBJ hObject)
545 {
546 DPRINT("NtGdiDeleteObject handle 0x%08x\n", hObject);
547
548 return GDIOBJ_FreeObj(hObject, GDI_OBJECT_TYPE_DONTCARE, GDIOBJFLAG_DEFAULT);
549 }
550
551 /*!
552 * Internal function. Called when the process is destroyed to free the remaining GDI handles.
553 * \param Process - PID of the process that will be destroyed.
554 */
555 BOOL FASTCALL
556 CleanupForProcess (struct _EPROCESS *Process, INT Pid)
557 {
558 DWORD i;
559 PGDIOBJHDR objectHeader;
560 PEPROCESS CurrentProcess;
561
562 DPRINT("Starting CleanupForProcess prochandle %x Pid %d\n", Process, Pid);
563 CurrentProcess = PsGetCurrentProcess();
564 if (CurrentProcess != Process)
565 {
566 KeAttachProcess(Process);
567 }
568
569 for(i = 1; i < HandleTable->wTableSize; i++)
570 {
571 objectHeader = GDIOBJ_iGetObjectForIndex(i);
572 if (NULL != objectHeader &&
573 (INT) objectHeader->hProcessId == Pid)
574 {
575 DPRINT("CleanupForProcess: %d, process: %d, locks: %d, magic: 0x%x", i, objectHeader->hProcessId, objectHeader->dwCount, objectHeader->Magic);
576 GDIOBJ_FreeObj(GDI_HANDLE_CREATE(i, GDI_MAGIC_TO_TYPE(objectHeader->Magic)),
577 GDI_MAGIC_TO_TYPE(objectHeader->Magic),
578 GDIOBJFLAG_IGNOREPID | GDIOBJFLAG_IGNORELOCK);
579 }
580 }
581
582 if (CurrentProcess != Process)
583 {
584 KeDetachProcess();
585 }
586
587 DPRINT("Completed cleanup for process %d\n", Pid);
588
589 return TRUE;
590 }
591
592 #define GDIOBJ_TRACKLOCKS
593
594 #ifdef GDIOBJ_LockObj
595 #undef GDIOBJ_LockObj
596 PGDIOBJ FASTCALL
597 GDIOBJ_LockObjDbg (const char* file, int line, HGDIOBJ hObj, DWORD ObjectType)
598 {
599 PGDIOBJ rc;
600 PGDIOBJHDR ObjHdr = GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(hObj));
601
602 if (! GDI_VALID_OBJECT(hObj, ObjHdr, ObjectType, GDIOBJFLAG_DEFAULT))
603 {
604 int reason = 0;
605 if (NULL == ObjHdr)
606 {
607 reason = 1;
608 }
609 else if (GDI_MAGIC_TO_TYPE(ObjHdr->Magic) != ObjectType && ObjectType != GDI_OBJECT_TYPE_DONTCARE)
610 {
611 reason = 2;
612 }
613 else if (ObjHdr->hProcessId != GDI_GLOBAL_PROCESS
614 && ObjHdr->hProcessId != PsGetCurrentProcessId())
615 {
616 reason = 3;
617 }
618 else if (GDI_HANDLE_GET_TYPE(hObj) != ObjectType && ObjectType != GDI_OBJECT_TYPE_DONTCARE)
619 {
620 reason = 4;
621 }
622 DPRINT1("GDIOBJ_LockObj failed for 0x%08x, reqtype 0x%08x reason %d\n",
623 hObj, ObjectType, reason );
624 DPRINT1("\tcalled from: %s:%i\n", file, line );
625 return NULL;
626 }
627 if (NULL != ObjHdr->lockfile)
628 {
629 DPRINT1("Caution! GDIOBJ_LockObj trying to lock object (0x%x) second time\n", hObj );
630 DPRINT1("\tcalled from: %s:%i\n", file, line );
631 DPRINT1("\tpreviously locked from: %s:%i\n", ObjHdr->lockfile, ObjHdr->lockline );
632 }
633 DPRINT("(%s:%i) GDIOBJ_LockObj(0x%08x,0x%08x)\n", file, line, hObj, ObjectType);
634 rc = GDIOBJ_LockObj(hObj, ObjectType);
635 if (rc && NULL == ObjHdr->lockfile)
636 {
637 ObjHdr->lockfile = file;
638 ObjHdr->lockline = line;
639 }
640
641 return rc;
642 }
643 #endif//GDIOBJ_LockObj
644
645 #ifdef GDIOBJ_UnlockObj
646 #undef GDIOBJ_UnlockObj
647 BOOL FASTCALL
648 GDIOBJ_UnlockObjDbg (const char* file, int line, HGDIOBJ hObj, DWORD ObjectType)
649 {
650 PGDIOBJHDR ObjHdr = GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(hObj));
651
652 if (! GDI_VALID_OBJECT(hObj, ObjHdr, ObjectType, GDIOBJFLAG_DEFAULT))
653 {
654 DPRINT1("GDIBOJ_UnlockObj failed for 0x%08x, reqtype 0x%08x\n",
655 hObj, ObjectType);
656 DPRINT1("\tcalled from: %s:%i\n", file, line);
657 return FALSE;
658 }
659 DPRINT("(%s:%i) GDIOBJ_UnlockObj(0x%08x,0x%08x)\n", file, line, hObj, ObjectType);
660 ObjHdr->lockfile = NULL;
661 ObjHdr->lockline = 0;
662
663 return GDIOBJ_UnlockObj(hObj, ObjectType);
664 }
665 #endif//GDIOBJ_LockObj
666
667 /*!
668 * Return pointer to the object by handle.
669 *
670 * \param hObj Object handle
671 * \param ObjectType one of the object types defined in \ref GDI object types
672 * \return Pointer to the object.
673 *
674 * \note Process can only get pointer to the objects it created or global objects.
675 *
676 * \todo Don't allow to lock the objects twice! Synchronization!
677 */
678 PGDIOBJ FASTCALL
679 GDIOBJ_LockObj(HGDIOBJ hObj, DWORD ObjectType)
680 {
681 PGDIOBJHDR ObjHdr = GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(hObj));
682
683 DPRINT("GDIOBJ_LockObj: hObj: 0x%08x, type: 0x%08x, objhdr: %x\n", hObj, ObjectType, ObjHdr);
684 if (! GDI_VALID_OBJECT(hObj, ObjHdr, ObjectType, GDIOBJFLAG_DEFAULT))
685 {
686 DPRINT1("GDIBOJ_LockObj failed for 0x%08x, type 0x%08x\n",
687 hObj, ObjectType);
688 return NULL;
689 }
690
691 if(0 < ObjHdr->dwCount)
692 {
693 DPRINT1("Caution! GDIOBJ_LockObj trying to lock object (0x%x) second time\n", hObj);
694 DPRINT1("\t called from: %x\n", __builtin_return_address(0));
695 }
696
697 ExAcquireFastMutex(&RefCountHandling);
698 ObjHdr->dwCount++;
699 ExReleaseFastMutex(&RefCountHandling);
700 return (PGDIOBJ)((PCHAR)ObjHdr + sizeof(GDIOBJHDR));
701 }
702
703 /*!
704 * Release GDI object. Every object locked by GDIOBJ_LockObj() must be unlocked. You should unlock the object
705 * as soon as you don't need to have access to it's data.
706
707 * \param hObj Object handle
708 * \param ObjectType one of the object types defined in \ref GDI object types
709 *
710 * \note This function performs delayed cleanup. If the object is locked when GDI_FreeObj() is called
711 * then \em this function frees the object when reference count is zero.
712 *
713 * \todo Change synchronization algorithm.
714 */
715 #undef GDIOBJ_UnlockObj
716 BOOL FASTCALL
717 GDIOBJ_UnlockObj(HGDIOBJ hObj, DWORD ObjectType)
718 {
719 PGDIOBJHDR ObjHdr = GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(hObj));
720
721 DPRINT("GDIOBJ_UnlockObj: hObj: 0x%08x, type: 0x%08x, objhdr: %x\n", hObj, ObjectType, ObjHdr);
722 if (! GDI_VALID_OBJECT(hObj, ObjHdr, ObjectType, GDIOBJFLAG_DEFAULT))
723 {
724 DPRINT1( "GDIOBJ_UnLockObj: failed\n");
725 return FALSE;
726 }
727
728 ExAcquireFastMutex(&RefCountHandling);
729 if (0 == (ObjHdr->dwCount & ~0x80000000))
730 {
731 ExReleaseFastMutex(&RefCountHandling);
732 DPRINT1( "GDIOBJ_UnLockObj: unlock object (0x%x) that is not locked\n", hObj );
733 return FALSE;
734 }
735
736 ObjHdr->dwCount--;
737
738 if (ObjHdr->dwCount == 0x80000000)
739 {
740 //delayed object release
741 ObjHdr->dwCount = 0;
742 ExReleaseFastMutex(&RefCountHandling);
743 DPRINT("GDIOBJ_UnlockObj: delayed delete\n");
744 return GDIOBJ_FreeObj(hObj, ObjectType, GDIOBJFLAG_DEFAULT);
745 }
746 ExReleaseFastMutex(&RefCountHandling);
747
748 return TRUE;
749 }
750
751 /* EOF */