Update PDEVOBJ and GRAPHICS_DEVICE fields
[reactos.git] / reactos / subsystems / win32 / win32k / eng / driverobj.c
1 /*
2 * COPYRIGHT: GPL, see COPYING in the top level directory
3 * PROJECT: ReactOS win32 kernel mode sunsystem
4 * PURPOSE: GDI DRIVEROBJ Functions
5 * FILE: subsystems/win32k/eng/driverobj.c
6 * PROGRAMER: Timo Kreuzer
7 */
8
9 /** Includes ******************************************************************/
10
11 #include <w32k.h>
12
13 #define NDEBUG
14 #include <debug.h>
15
16
17 /** Internal interface ********************************************************/
18
19 /*!
20 * \brief DRIVEROBJ cleanup function
21 */
22 BOOL INTERNAL_CALL
23 DRIVEROBJ_Cleanup(PVOID pObject)
24 {
25 PEDRIVEROBJ pedo = pObject;
26 FREEOBJPROC pFreeProc;
27
28 pFreeProc = pedo->drvobj.pFreeProc;
29 if (pFreeProc)
30 {
31 return pFreeProc(pedo->drvobj.pvObj);
32 }
33
34 return TRUE;
35 }
36
37 /** Public interface **********************************************************/
38
39 HDRVOBJ
40 APIENTRY
41 EngCreateDriverObj(
42 IN PVOID pvObj,
43 IN FREEOBJPROC pFreeObjProc,
44 IN HDEV hdev)
45 {
46 PEDRIVEROBJ pedo;
47 HDRVOBJ hdo;
48 PDEVOBJ *ppdev = (PDEVOBJ*)hdev;
49
50 /* Allocate a new DRIVEROBJ */
51 pedo = DRIVEROBJ_AllocObjectWithHandle();
52 if (!pedo)
53 {
54 return NULL;
55 }
56 hdo = pedo->baseobj.hHmgr;
57
58 /* Fill in fields */
59 pedo->drvobj.pvObj = pvObj;
60 pedo->drvobj.pFreeProc = pFreeObjProc;
61 pedo->drvobj.hdev = hdev;
62 pedo->drvobj.dhpdev = ppdev->dhpdev;
63
64 /* Unlock the object */
65 DRIVEROBJ_UnlockObject(pedo);
66
67 /* Return the handle */
68 return hdo;
69 }
70
71
72 BOOL
73 APIENTRY
74 EngDeleteDriverObj(
75 IN HDRVOBJ hdo,
76 IN BOOL bCallBack,
77 IN BOOL bLocked)
78 {
79 PEDRIVEROBJ pedo;
80
81 /* Lock the object */
82 pedo = DRIVEROBJ_LockObject(hdo);
83 if (!pedo)
84 {
85 return FALSE;
86 }
87
88 /* Manually call cleanup callback */
89 if (bCallBack)
90 {
91 if (!pedo->drvobj.pFreeProc(pedo->drvobj.pvObj))
92 {
93 /* Callback failed */
94 DRIVEROBJ_UnlockObject(pedo);
95 return FALSE;
96 }
97 }
98
99 /* Prevent cleanup callback from being called again */
100 pedo->drvobj.pFreeProc = NULL;
101
102 /* NOTE: We don't care about the bLocked param, as our handle manager
103 allows freeing the object, while we hold any number of locks. */
104
105 /* Free the object */
106 return DRIVEROBJ_FreeObjectByHandle(hdo);
107 }
108
109
110 PDRIVEROBJ
111 APIENTRY
112 EngLockDriverObj(
113 IN HDRVOBJ hdo)
114 {
115 PEDRIVEROBJ pedo;
116
117 /* Lock the object */
118 pedo = DRIVEROBJ_LockObject(hdo);
119
120 /* Return pointer to the DRIVEROBJ structure */
121 return &pedo->drvobj;
122 }
123
124
125 BOOL
126 APIENTRY
127 EngUnlockDriverObj(
128 IN HDRVOBJ hdo)
129 {
130 PEDRIVEROBJ pedo;
131 ULONG cLocks;
132
133 /* First lock to get a pointer to the object */
134 pedo = DRIVEROBJ_LockObject(hdo);
135 if(!pedo)
136 {
137 /* Object could not be locked, fail. */
138 return FALSE;
139 }
140
141 /* Unlock object */
142 cLocks = DRIVEROBJ_UnlockObject(pedo);
143
144 /* Check if we still hold a lock */
145 if (cLocks < 1)
146 {
147 /* Object wasn't locked before, fail. */
148 return FALSE;
149 }
150
151 /* Unlock again */
152 DRIVEROBJ_UnlockObject(pedo);
153
154 /* Success */
155 return TRUE;
156 }
157