Sync with trunk (aka 'I want my virtualbox mouse integration too')
[reactos.git] / subsystems / win32 / win32k / objects / dclife.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * PURPOSE: Functions for creation and destruction of DCs
5 * FILE: subsystem/win32/win32k/objects/dclife.c
6 * PROGRAMER: Timo Kreuzer (timo.kreuzer@rectos.org)
7 */
8
9 #include <w32k.h>
10 #include <bugcodes.h>
11
12 #define NDEBUG
13 #include <debug.h>
14
15 //FIXME: windows uses 0x0012009f
16 #define DIRTY_DEFAULT DIRTY_CHARSET|DIRTY_BACKGROUND|DIRTY_TEXT|DIRTY_LINE|DIRTY_FILL
17
18 PSURFACE psurfDefaultBitmap = NULL;
19 PBRUSH pbrDefaultBrush = NULL;
20
21 // FIXME: these should go to floatobj.h or something
22 #define FLOATOBJ_0 {0x00000000, 0x00000000}
23 #define FLOATOBJ_1 {0x40000000, 0x00000002}
24 #define FLOATOBJ_16 {0x40000000, 0x00000006}
25 #define FLOATOBJ_1_16 {0x40000000, 0xfffffffe}
26
27 static const FLOATOBJ gef0 = FLOATOBJ_0;
28 static const FLOATOBJ gef1 = FLOATOBJ_1;
29 static const FLOATOBJ gef16 = FLOATOBJ_16;
30
31 static const MATRIX gmxWorldToDeviceDefault =
32 {
33 FLOATOBJ_16, FLOATOBJ_0,
34 FLOATOBJ_0, FLOATOBJ_16,
35 FLOATOBJ_0, FLOATOBJ_0,
36 0, 0, 0x4b
37 };
38
39 static const MATRIX gmxDeviceToWorldDefault =
40 {
41 FLOATOBJ_1_16, FLOATOBJ_0,
42 FLOATOBJ_0, FLOATOBJ_1_16,
43 FLOATOBJ_0, FLOATOBJ_0,
44 0, 0, 0x53
45 };
46
47 static const MATRIX gmxWorldToPageDefault =
48 {
49 FLOATOBJ_1, FLOATOBJ_0,
50 FLOATOBJ_0, FLOATOBJ_1,
51 FLOATOBJ_0, FLOATOBJ_0,
52 0, 0, 0x63
53 };
54
55 // HACK!! Fix XFORMOBJ then use 1:16 / 16:1
56 #define gmxWorldToDeviceDefault gmxWorldToPageDefault
57 #define gmxDeviceToWorldDefault gmxWorldToPageDefault
58
59 /** Internal functions ********************************************************/
60
61 NTSTATUS
62 InitDcImpl()
63 {
64 psurfDefaultBitmap = SURFACE_ShareLockSurface(StockObjects[DEFAULT_BITMAP]);
65 if (!psurfDefaultBitmap)
66 return STATUS_UNSUCCESSFUL;
67
68 pbrDefaultBrush = BRUSH_ShareLockBrush(StockObjects[BLACK_BRUSH]);
69 if (!pbrDefaultBrush)
70 return STATUS_UNSUCCESSFUL;
71
72 return STATUS_SUCCESS;
73 }
74
75
76 PDC
77 NTAPI
78 DC_AllocDcWithHandle()
79 {
80 PDC pdc;
81 pdc = (PDC)GDIOBJ_AllocObjWithHandle(GDILoObjType_LO_DC_TYPE);
82
83 pdc->pdcattr = &pdc->dcattr;
84
85 return pdc;
86 }
87
88
89 void
90 DC_InitHack(PDC pdc)
91 {
92 HRGN hVisRgn;
93
94 TextIntRealizeFont(pdc->pdcattr->hlfntNew,NULL);
95 pdc->pdcattr->iCS_CP = ftGdiGetTextCharsetInfo(pdc,NULL,0);
96
97 /* This should never fail */
98 ASSERT(pdc->dclevel.ppal);
99
100 /* Select regions */
101 // FIXME: too complicated, broken error handling
102 pdc->rosdc.hClipRgn = NULL;
103 pdc->rosdc.hGCClipRgn = NULL;
104 hVisRgn = NtGdiCreateRectRgn(0, 0, pdc->dclevel.sizl.cx, pdc->dclevel.sizl.cy);
105 ASSERT(hVisRgn);
106 GdiSelectVisRgn(pdc->BaseObject.hHmgr, hVisRgn);
107 GreDeleteObject(hVisRgn);
108 ASSERT(pdc->prgnVis);
109 pdc->rosdc.bitsPerPixel = pdc->ppdev->gdiinfo.cBitsPixel *
110 pdc->ppdev->gdiinfo.cPlanes;
111 }
112
113 VOID
114 NTAPI
115 DC_vInitDc(
116 PDC pdc,
117 DCTYPE dctype,
118 PPDEVOBJ ppdev)
119 {
120 if (dctype == DCTYPE_DIRECT)
121 {
122 /* Lock ppdev */
123 EngAcquireSemaphoreShared(ppdev->hsemDevLock);
124 }
125
126 /* Setup some basic fields */
127 pdc->dctype = dctype;
128 pdc->ppdev = ppdev;
129 pdc->dhpdev = ppdev->dhpdev;
130 pdc->hsem = ppdev->hsemDevLock;
131 pdc->flGraphicsCaps = ppdev->devinfo.flGraphicsCaps;
132 pdc->flGraphicsCaps2 = ppdev->devinfo.flGraphicsCaps2;
133 pdc->fs = DC_DIRTY_RAO;
134
135 /* Setup dc attribute */
136 pdc->pdcattr = &pdc->dcattr;
137 pdc->dcattr.pvLDC = NULL;
138 pdc->dcattr.ulDirty_ = DIRTY_DEFAULT;
139 if (ppdev == gppdevPrimary)
140 pdc->dcattr.ulDirty_ |= DC_PRIMARY_DISPLAY;
141
142 /* Setup the DC size */
143 if (dctype == DCTYPE_MEMORY)
144 {
145 /* Memory DCs have a 1 x 1 bitmap by default */
146 pdc->dclevel.sizl.cx = 1;
147 pdc->dclevel.sizl.cy = 1;
148 }
149 else
150 {
151 /* Other DC's are as big as the related PDEV */
152 pdc->dclevel.sizl.cx = ppdev->gdiinfo.ulHorzRes;
153 pdc->dclevel.sizl.cy = ppdev->gdiinfo.ulVertRes;
154 }
155
156 /* Setup Window rect based on DC size */
157 pdc->erclWindow.left = 0;
158 pdc->erclWindow.top = 0;
159 pdc->erclWindow.right = pdc->dclevel.sizl.cx;
160 pdc->erclWindow.bottom = pdc->dclevel.sizl.cy;
161
162 if (dctype == DCTYPE_DIRECT)
163 {
164 /* Direct DCs get the surface from the PDEV */
165 pdc->dclevel.pSurface = PDEVOBJ_pSurface(ppdev);
166
167 pdc->erclBounds.left = 0x7fffffff;
168 pdc->erclBounds.top = 0x7fffffff;
169 pdc->erclBounds.right = 0x80000000;
170 pdc->erclBounds.bottom = 0x80000000;
171 pdc->erclBoundsApp.left = 0xffffffff;
172 pdc->erclBoundsApp.top = 0xfffffffc;
173 pdc->erclBoundsApp.right = 0x00007ffc; // FIXME
174 pdc->erclBoundsApp.bottom = 0x00000333; // FIXME
175 pdc->erclClip = pdc->erclBounds;
176 // pdc->co
177
178 pdc->fs |= DC_SYNCHRONIZEACCESS | DC_ACCUM_APP | DC_PERMANANT | DC_DISPLAY;
179 }
180 else
181 {
182 /* Non-direct DCs don't have a surface by default */
183 pdc->dclevel.pSurface = NULL;
184
185 // FIXME: HACK, because our code expects a surface
186 pdc->dclevel.pSurface = SURFACE_ShareLockSurface(StockObjects[DEFAULT_BITMAP]);
187
188 pdc->erclBounds.left = 0;
189 pdc->erclBounds.top = 0;
190 pdc->erclBounds.right = 0;
191 pdc->erclBounds.bottom = 0;
192 pdc->erclBoundsApp = pdc->erclBounds;
193 pdc->erclClip = pdc->erclWindow;
194 // pdc->co = NULL
195 }
196
197 // pdc->dcattr.VisRectRegion:
198
199 /* Setup coordinate transformation data */
200 pdc->dclevel.mxWorldToDevice = gmxWorldToDeviceDefault;
201 pdc->dclevel.mxDeviceToWorld = gmxDeviceToWorldDefault;
202 pdc->dclevel.mxWorldToPage = gmxWorldToPageDefault;
203 pdc->dclevel.efM11PtoD = gef16;
204 pdc->dclevel.efM22PtoD = gef16;
205 pdc->dclevel.efDxPtoD = gef0;
206 pdc->dclevel.efDyPtoD = gef0;
207 pdc->dclevel.efM11_TWIPS = gef0;
208 pdc->dclevel.efM22_TWIPS = gef0;
209 pdc->dclevel.efPr11 = gef0;
210 pdc->dclevel.efPr22 = gef0;
211 pdc->dcattr.mxWorldToDevice = pdc->dclevel.mxWorldToDevice;
212 pdc->dcattr.mxDeviceToWorld = pdc->dclevel.mxDeviceToWorld;
213 pdc->dcattr.mxWorldToPage = pdc->dclevel.mxWorldToPage;
214 pdc->dcattr.efM11PtoD = pdc->dclevel.efM11PtoD;
215 pdc->dcattr.efM22PtoD = pdc->dclevel.efM22PtoD;
216 pdc->dcattr.efDxPtoD = pdc->dclevel.efDxPtoD;
217 pdc->dcattr.efDyPtoD = pdc->dclevel.efDyPtoD;
218 pdc->dcattr.iMapMode = MM_TEXT;
219 pdc->dcattr.dwLayout = 0;
220 pdc->dcattr.flXform = PAGE_TO_DEVICE_SCALE_IDENTITY |
221 PAGE_TO_DEVICE_IDENTITY |
222 WORLD_TO_PAGE_IDENTITY;
223
224 /* Setup more coordinates */
225 pdc->ptlDCOrig.x = 0;
226 pdc->ptlDCOrig.y = 0;
227 pdc->dcattr.lWindowOrgx = 0;
228 pdc->dcattr.ptlWindowOrg.x = 0;
229 pdc->dcattr.ptlWindowOrg.y = 0;
230 pdc->dcattr.szlWindowExt.cx = 1;
231 pdc->dcattr.szlWindowExt.cy = 1;
232 pdc->dcattr.ptlViewportOrg.x = 0;
233 pdc->dcattr.ptlViewportOrg.y = 0;
234 pdc->dcattr.szlViewportExt.cx = 1;
235 pdc->dcattr.szlViewportExt.cy = 1;
236 pdc->dcattr.szlVirtualDevicePixel.cx = 0;
237 pdc->dcattr.szlVirtualDevicePixel.cy = 0;
238 pdc->dcattr.szlVirtualDeviceMm.cx = 0;
239 pdc->dcattr.szlVirtualDeviceMm.cy = 0;
240 pdc->dcattr.szlVirtualDeviceSize.cx = 0;
241 pdc->dcattr.szlVirtualDeviceSize.cy = 0;
242
243 /* Setup regions */
244 pdc->prgnAPI = NULL;
245 pdc->prgnVis = NULL; // FIXME
246 pdc->prgnRao = NULL;
247
248 /* Setup palette */
249 pdc->dclevel.hpal = StockObjects[DEFAULT_PALETTE];
250 pdc->dclevel.ppal = PALETTE_ShareLockPalette(pdc->dclevel.hpal);
251
252 /* Setup path */
253 pdc->dclevel.hPath = NULL;
254 pdc->dclevel.flPath = 0;
255 // pdc->dclevel.lapath:
256
257 /* Setup colors */
258 pdc->dcattr.crBackgroundClr = RGB(0xff, 0xff, 0xff);
259 pdc->dcattr.ulBackgroundClr = RGB(0xff, 0xff, 0xff);
260 pdc->dcattr.crForegroundClr = RGB(0, 0, 0);
261 pdc->dcattr.ulForegroundClr = RGB(0, 0, 0);
262 pdc->dcattr.crBrushClr = RGB(0xff, 0xff, 0xff);
263 pdc->dcattr.ulBrushClr = RGB(0xff, 0xff, 0xff);
264 pdc->dcattr.crPenClr = RGB(0, 0, 0);
265 pdc->dcattr.ulPenClr = RGB(0, 0, 0);
266
267 /* Select the default fill and line brush */
268 pdc->dcattr.hbrush = StockObjects[WHITE_BRUSH];
269 pdc->dcattr.hpen = StockObjects[BLACK_PEN];
270 pdc->dclevel.pbrFill = BRUSH_ShareLockBrush(pdc->pdcattr->hbrush);
271 pdc->dclevel.pbrLine = PEN_ShareLockPen(pdc->pdcattr->hpen);
272 pdc->dclevel.ptlBrushOrigin.x = 0;
273 pdc->dclevel.ptlBrushOrigin.y = 0;
274 pdc->dcattr.ptlBrushOrigin = pdc->dclevel.ptlBrushOrigin;
275
276 /* Initialize EBRUSHOBJs */
277 EBRUSHOBJ_vInit(&pdc->eboFill, pdc->dclevel.pbrFill, pdc);
278 EBRUSHOBJ_vInit(&pdc->eboLine, pdc->dclevel.pbrLine, pdc);
279 EBRUSHOBJ_vInit(&pdc->eboText, pbrDefaultBrush, pdc);
280 EBRUSHOBJ_vInit(&pdc->eboBackground, pbrDefaultBrush, pdc);
281
282 /* Setup fill data */
283 pdc->dcattr.jROP2 = R2_COPYPEN;
284 pdc->dcattr.jBkMode = 2;
285 pdc->dcattr.lBkMode = 2;
286 pdc->dcattr.jFillMode = ALTERNATE;
287 pdc->dcattr.lFillMode = 1;
288 pdc->dcattr.jStretchBltMode = 1;
289 pdc->dcattr.lStretchBltMode = 1;
290 pdc->ptlFillOrigin.x = 0;
291 pdc->ptlFillOrigin.y = 0;
292
293 /* Setup drawing position */
294 pdc->dcattr.ptlCurrent.x = 0;
295 pdc->dcattr.ptlCurrent.y = 0;
296 pdc->dcattr.ptfxCurrent.x = 0;
297 pdc->dcattr.ptfxCurrent.y = 0;
298
299 /* Setup ICM data */
300 pdc->dclevel.lIcmMode = 0;
301 pdc->dcattr.lIcmMode = 0;
302 pdc->dcattr.hcmXform = NULL;
303 pdc->dcattr.flIcmFlags = 0;
304 pdc->dcattr.IcmBrushColor = CLR_INVALID;
305 pdc->dcattr.IcmPenColor = CLR_INVALID;
306 pdc->dcattr.pvLIcm = NULL;
307 pdc->dcattr.hColorSpace = NULL; // FIXME: 0189001f
308 pdc->dclevel.pColorSpace = NULL; // FIXME
309 pdc->pClrxFormLnk = NULL;
310 // pdc->dclevel.ca =
311
312 /* Setup font data */
313 pdc->hlfntCur = NULL; // FIXME: 2f0a0cf8
314 pdc->pPFFList = NULL;
315 pdc->flSimulationFlags = 0;
316 pdc->lEscapement = 0;
317 pdc->prfnt = NULL;
318 pdc->dcattr.flFontMapper = 0;
319 pdc->dcattr.flTextAlign = 0;
320 pdc->dcattr.lTextAlign = 0;
321 pdc->dcattr.lTextExtra = 0;
322 pdc->dcattr.lRelAbs = 1;
323 pdc->dcattr.lBreakExtra = 0;
324 pdc->dcattr.cBreak = 0;
325 pdc->dcattr.hlfntNew = StockObjects[SYSTEM_FONT];
326 // pdc->dclevel.pFont = LFONT_ShareLockFont(pdc->dcattr.hlfntNew);
327
328 /* Other stuff */
329 pdc->hdcNext = NULL;
330 pdc->hdcPrev = NULL;
331 pdc->ipfdDevMax = 0x0000ffff;
332 pdc->ulCopyCount = -1;
333 pdc->ptlDoBanding.x = 0;
334 pdc->ptlDoBanding.y = 0;
335 pdc->dclevel.lSaveDepth = 1;
336 pdc->dclevel.hdcSave = NULL;
337 pdc->dcattr.iGraphicsMode = GM_COMPATIBLE;
338 pdc->dcattr.iCS_CP = 0;
339 pdc->pSurfInfo = NULL;
340
341 }
342
343 BOOL
344 INTERNAL_CALL
345 DC_Cleanup(PVOID ObjectBody)
346 {
347 PDC pdc = (PDC)ObjectBody;
348
349 /* Free DC_ATTR */
350 DC_vFreeDcAttr(pdc);
351
352 /* Delete saved DCs */
353 DC_vRestoreDC(pdc, 1);
354
355 /* Deselect dc objects */
356 DC_vSelectSurface(pdc, NULL);
357 DC_vSelectFillBrush(pdc, NULL);
358 DC_vSelectLineBrush(pdc, NULL);
359 DC_vSelectPalette(pdc, NULL);
360
361 /* Cleanup the dc brushes */
362 EBRUSHOBJ_vCleanup(&pdc->eboFill);
363 EBRUSHOBJ_vCleanup(&pdc->eboLine);
364 EBRUSHOBJ_vCleanup(&pdc->eboText);
365 EBRUSHOBJ_vCleanup(&pdc->eboBackground);
366
367 /* Free regions */
368 if (pdc->rosdc.hClipRgn)
369 GreDeleteObject(pdc->rosdc.hClipRgn);
370 if (pdc->prgnVis)
371 REGION_FreeRgnByHandle(pdc->prgnVis->BaseObject.hHmgr);
372 ASSERT(pdc->rosdc.hGCClipRgn);
373 if (pdc->rosdc.hGCClipRgn)
374 GreDeleteObject(pdc->rosdc.hGCClipRgn);
375 if (NULL != pdc->rosdc.CombinedClip)
376 IntEngDeleteClipRegion(pdc->rosdc.CombinedClip);
377
378 PATH_Delete(pdc->dclevel.hPath);
379
380 if(pdc->dclevel.pSurface)
381 SURFACE_ShareUnlockSurface(pdc->dclevel.pSurface);
382
383 PDEVOBJ_vRelease(pdc->ppdev) ;
384
385 return TRUE;
386 }
387
388 BOOL
389 FASTCALL
390 DC_SetOwnership(HDC hDC, PEPROCESS Owner)
391 {
392 INT Index;
393 PGDI_TABLE_ENTRY Entry;
394 PDC pDC;
395 BOOL ret = FALSE;
396
397 /* FIXME: This function has broken error handling */
398
399 if (!GDIOBJ_SetOwnership(hDC, Owner))
400 {
401 DPRINT1("GDIOBJ_SetOwnership failed\n");
402 return FALSE;
403 }
404
405 pDC = DC_LockDc(hDC);
406 if (!pDC)
407 {
408 DPRINT1("Could not lock DC\n");
409 return FALSE;
410 }
411
412 /*
413 System Regions:
414 These regions do not use attribute sections and when allocated, use
415 gdiobj level functions.
416 */
417 if (pDC->rosdc.hClipRgn)
418 { // FIXME! HAX!!!
419 Index = GDI_HANDLE_GET_INDEX(pDC->rosdc.hClipRgn);
420 Entry = &GdiHandleTable->Entries[Index];
421 if (Entry->UserData) FreeObjectAttr(Entry->UserData);
422 Entry->UserData = NULL;
423 //
424 if (!GDIOBJ_SetOwnership(pDC->rosdc.hClipRgn, Owner)) goto leave;
425 }
426 if (pDC->prgnVis)
427 { // FIXME! HAX!!!
428 Index = GDI_HANDLE_GET_INDEX(pDC->prgnVis->BaseObject.hHmgr);
429 Entry = &GdiHandleTable->Entries[Index];
430 if (Entry->UserData) FreeObjectAttr(Entry->UserData);
431 Entry->UserData = NULL;
432 //
433 if (!GDIOBJ_SetOwnership(pDC->prgnVis->BaseObject.hHmgr, Owner)) goto leave;
434 }
435 if (pDC->rosdc.hGCClipRgn)
436 { // FIXME! HAX!!!
437 Index = GDI_HANDLE_GET_INDEX(pDC->rosdc.hGCClipRgn);
438 Entry = &GdiHandleTable->Entries[Index];
439 if (Entry->UserData) FreeObjectAttr(Entry->UserData);
440 Entry->UserData = NULL;
441 //
442 if (!GDIOBJ_SetOwnership(pDC->rosdc.hGCClipRgn, Owner)) goto leave;
443 }
444 if (pDC->dclevel.hPath)
445 {
446 if (!GDIOBJ_SetOwnership(pDC->dclevel.hPath, Owner)) goto leave;
447 }
448 ret = TRUE;
449
450 leave:
451 DC_UnlockDc(pDC);
452
453 return ret;
454 }
455
456 HDC
457 NTAPI
458 GreOpenDCW(
459 PUNICODE_STRING pustrDevice,
460 DEVMODEW *pdmInit,
461 PUNICODE_STRING pustrLogAddr,
462 ULONG iType,
463 BOOL bDisplay,
464 HANDLE hspool,
465 VOID *pDriverInfo2,
466 VOID *pUMdhpdev)
467 {
468 PPDEVOBJ ppdev;
469 PDC pdc;
470 HDC hdc;
471
472 DPRINT("GreOpenDCW(%S, iType=%ld)\n",
473 pustrDevice ? pustrDevice->Buffer : NULL, iType);
474
475 /* Get a PDEVOBJ for the device */
476 ppdev = EngpGetPDEV(pustrDevice);
477 if (!ppdev)
478 {
479 DPRINT1("Didn't find a suitable PDEV\n");
480 return NULL;
481 }
482
483 DPRINT("GreOpenDCW - ppdev = %p\n", ppdev);
484
485 pdc = DC_AllocDcWithHandle();
486 if (!pdc)
487 {
488 DPRINT1("Could not Allocate a DC\n");
489 PDEVOBJ_vRelease(ppdev);
490 return NULL;
491 }
492 hdc = pdc->BaseObject.hHmgr;
493
494 /* Lock ppdev and initialize the new DC */
495 DC_vInitDc(pdc, iType, ppdev);
496 /* FIXME: HACK! */
497 DC_InitHack(pdc);
498
499 DC_AllocDcAttr(pdc);
500
501 DC_UnlockDc(pdc);
502
503 DPRINT("returning hdc = %p\n", hdc);
504
505 return hdc;
506 }
507
508 HDC
509 APIENTRY
510 NtGdiOpenDCW(
511 PUNICODE_STRING pustrDevice,
512 DEVMODEW *pdmInit,
513 PUNICODE_STRING pustrLogAddr,
514 ULONG iType,
515 BOOL bDisplay,
516 HANDLE hspool,
517 VOID *pDriverInfo2,
518 VOID *pUMdhpdev)
519 {
520 UNICODE_STRING ustrDevice;
521 WCHAR awcDevice[CCHDEVICENAME];
522 DEVMODEW dmInit;
523 PVOID dhpdev;
524 HDC hdc;
525
526 /* Only if a devicename is given, we need any data */
527 if (pustrDevice)
528 {
529 /* Initialize destination string */
530 RtlInitEmptyUnicodeString(&ustrDevice, awcDevice, sizeof(awcDevice));
531
532 _SEH2_TRY
533 {
534 /* Probe the UNICODE_STRING and the buffer */
535 ProbeForRead(pustrDevice, sizeof(UNICODE_STRING), 1);
536 ProbeForRead(pustrDevice->Buffer, pustrDevice->Length, 1);
537
538 /* Copy the string */
539 RtlCopyUnicodeString(&ustrDevice, pustrDevice);
540
541 if (pdmInit)
542 {
543 /* FIXME: could be larger */
544 ProbeForRead(pdmInit, sizeof(DEVMODEW), 1);
545 RtlCopyMemory(&dmInit, pdmInit, sizeof(DEVMODEW));
546 }
547
548 if (pUMdhpdev)
549 {
550 ProbeForWrite(pUMdhpdev, sizeof(HANDLE), 1);
551 }
552 }
553 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
554 {
555 SetLastNtError(_SEH2_GetExceptionCode());
556 _SEH2_YIELD(return NULL);
557 }
558 _SEH2_END
559 }
560 else
561 {
562 pdmInit = NULL;
563 pUMdhpdev = NULL;
564 }
565
566 /* FIXME: HACK! */
567 if (pustrDevice)
568 {
569 UNICODE_STRING ustrDISPLAY = RTL_CONSTANT_STRING(L"DISPLAY");
570 if (RtlEqualUnicodeString(&ustrDevice, &ustrDISPLAY, TRUE))
571 {
572 pustrDevice = NULL;
573 }
574 }
575
576 /* Call the internal function */
577 hdc = GreOpenDCW(pustrDevice ? &ustrDevice : NULL,
578 pdmInit ? &dmInit : NULL,
579 NULL, // fixme pwszLogAddress
580 iType,
581 bDisplay,
582 hspool,
583 NULL, //FIXME: pDriverInfo2
584 pUMdhpdev ? &dhpdev : NULL);
585
586 /* If we got a HDC and a UM dhpdev is requested,... */
587 if (hdc && pUMdhpdev)
588 {
589 /* Copy dhpdev to caller (FIXME: use dhpdev?? */
590 _SEH2_TRY
591 {
592 /* Pointer was already probed */
593 *(HANDLE*)pUMdhpdev = dhpdev;
594 }
595 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
596 {
597 /* Ignore error */
598 }
599 _SEH2_END
600 }
601
602 return hdc;
603 }
604
605
606 HDC
607 APIENTRY
608 NtGdiCreateCompatibleDC(HDC hdc)
609 {
610 HDC hdcNew;
611 PPDEVOBJ ppdev;
612 PDC pdc, pdcNew;
613
614 DPRINT("NtGdiCreateCompatibleDC(0x%p)\n", hdc);
615
616 /* Did the caller provide a DC? */
617 if (hdc)
618 {
619 /* Yes, try to lock it */
620 pdc = DC_LockDc(hdc);
621 if (!pdc)
622 {
623 DPRINT1("Could not lock source DC %p\n", hdc);
624 return NULL;
625 }
626
627 /* Get the pdev from the DC */
628 ppdev = pdc->ppdev;
629 InterlockedIncrement(&ppdev->cPdevRefs);
630
631 /* Unlock the source DC */
632 DC_UnlockDc(pdc);
633 }
634 else
635 {
636 /* No DC given, get default device */
637 ppdev = EngpGetPDEV(NULL);
638 }
639
640 if (!ppdev)
641 {
642 DPRINT1("Didn't find a suitable PDEV\n");
643 return NULL;
644 }
645
646 /* Allocate a new DC */
647 pdcNew = DC_AllocDcWithHandle();
648 if (!pdcNew)
649 {
650 DPRINT1("Could not allocate a new DC\n");
651 PDEVOBJ_vRelease(ppdev);
652 return NULL;
653 }
654 hdcNew = pdcNew->BaseObject.hHmgr;
655
656 /* Lock ppdev and initialize the new DC */
657 DC_vInitDc(pdcNew, DCTYPE_MEMORY, ppdev);
658 /* FIXME: HACK! */
659 DC_InitHack(pdcNew);
660
661 /* Allocate a dc attribute */
662 DC_AllocDcAttr(pdcNew);
663
664 // HACK!
665 DC_vSelectSurface(pdcNew, psurfDefaultBitmap);
666
667 DC_UnlockDc(pdcNew);
668
669 DPRINT("Leave NtGdiCreateCompatibleDC hdcNew = %p\n", hdcNew);
670
671 return hdcNew;
672 }
673
674 BOOL
675 FASTCALL
676 IntGdiDeleteDC(HDC hDC, BOOL Force)
677 {
678 PDC DCToDelete = DC_LockDc(hDC);
679
680 if (DCToDelete == NULL)
681 {
682 SetLastWin32Error(ERROR_INVALID_HANDLE);
683 return FALSE;
684 }
685
686 if (!Force)
687 {
688 if (DCToDelete->fs & DC_FLAG_PERMANENT)
689 {
690 DPRINT1("No! You Naughty Application!\n");
691 DC_UnlockDc(DCToDelete);
692 return UserReleaseDC(NULL, hDC, FALSE);
693 }
694 }
695
696 DC_UnlockDc(DCToDelete);
697
698 if (!IsObjectDead(hDC))
699 {
700 if (!GDIOBJ_FreeObjByHandle(hDC, GDI_OBJECT_TYPE_DC))
701 {
702 DPRINT1("DC_FreeDC failed\n");
703 }
704 }
705 else
706 {
707 DPRINT1("Attempted to Delete 0x%x currently being destroyed!!!\n", hDC);
708 }
709
710 return TRUE;
711 }
712
713 BOOL
714 APIENTRY
715 NtGdiDeleteObjectApp(HANDLE DCHandle)
716 {
717 /* Complete all pending operations */
718 NtGdiFlushUserBatch();
719
720 if (GDI_HANDLE_IS_STOCKOBJ(DCHandle)) return TRUE;
721
722 if (GDI_HANDLE_GET_TYPE(DCHandle) != GDI_OBJECT_TYPE_DC)
723 return GreDeleteObject((HGDIOBJ) DCHandle);
724
725 if (IsObjectDead((HGDIOBJ)DCHandle)) return TRUE;
726
727 if (!GDIOBJ_OwnedByCurrentProcess(DCHandle))
728 {
729 SetLastWin32Error(ERROR_INVALID_HANDLE);
730 return FALSE;
731 }
732
733 return IntGdiDeleteDC(DCHandle, FALSE);
734 }
735
736 BOOL
737 FASTCALL
738 MakeInfoDC(PDC pdc, BOOL bSet)
739 {
740 PSURFACE pSurface;
741 SIZEL sizl;
742
743 /* Can not be a display DC. */
744 if (pdc->fs & DC_FLAG_DISPLAY) return FALSE;
745 if (bSet)
746 {
747 if (pdc->fs & DC_FLAG_TEMPINFODC || pdc->dctype == DC_TYPE_DIRECT)
748 return FALSE;
749
750 pSurface = pdc->dclevel.pSurface;
751 pdc->fs |= DC_FLAG_TEMPINFODC;
752 pdc->pSurfInfo = pSurface;
753 pdc->dctype = DC_TYPE_INFO;
754 pdc->dclevel.pSurface = NULL;
755
756 PDEVOBJ_sizl(pdc->ppdev, &sizl);
757
758 if ( sizl.cx == pdc->dclevel.sizl.cx &&
759 sizl.cy == pdc->dclevel.sizl.cy )
760 return TRUE;
761
762 pdc->dclevel.sizl.cx = sizl.cx;
763 pdc->dclevel.sizl.cy = sizl.cy;
764 }
765 else
766 {
767 if (!(pdc->fs & DC_FLAG_TEMPINFODC) || pdc->dctype != DC_TYPE_INFO)
768 return FALSE;
769
770 pSurface = pdc->pSurfInfo;
771 pdc->fs &= ~DC_FLAG_TEMPINFODC;
772 pdc->dclevel.pSurface = pSurface;
773 pdc->dctype = DC_TYPE_DIRECT;
774 pdc->pSurfInfo = NULL;
775
776 if ( !pSurface ||
777 (pSurface->SurfObj.sizlBitmap.cx == pdc->dclevel.sizl.cx &&
778 pSurface->SurfObj.sizlBitmap.cy == pdc->dclevel.sizl.cy) )
779 return TRUE;
780
781 pdc->dclevel.sizl.cx = pSurface->SurfObj.sizlBitmap.cx;
782 pdc->dclevel.sizl.cy = pSurface->SurfObj.sizlBitmap.cy;
783 }
784 return IntSetDefaultRegion(pdc);
785 }
786
787 /*
788 * @implemented
789 */
790 BOOL
791 APIENTRY
792 NtGdiMakeInfoDC(
793 IN HDC hdc,
794 IN BOOL bSet)
795 {
796 BOOL Ret;
797 PDC pdc = DC_LockDc(hdc);
798 if (pdc)
799 {
800 Ret = MakeInfoDC(pdc, bSet);
801 DC_UnlockDc(pdc);
802 return Ret;
803 }
804 return FALSE;
805 }
806
807
808 HDC FASTCALL
809 IntGdiCreateDC(
810 PUNICODE_STRING Driver,
811 PUNICODE_STRING pustrDevice,
812 PVOID pUMdhpdev,
813 CONST PDEVMODEW pdmInit,
814 BOOL CreateAsIC)
815 {
816 HDC hdc;
817
818 hdc = GreOpenDCW(pustrDevice,
819 pdmInit,
820 NULL,
821 CreateAsIC ? DCTYPE_INFO :
822 (Driver ? DC_TYPE_DIRECT : DC_TYPE_DIRECT),
823 TRUE,
824 NULL,
825 NULL,
826 pUMdhpdev);
827
828 return hdc;
829 }
830
831 HDC FASTCALL
832 IntGdiCreateDisplayDC(HDEV hDev, ULONG DcType, BOOL EmptyDC)
833 {
834 HDC hDC;
835 UNIMPLEMENTED;
836 ASSERT(FALSE);
837
838 if (DcType == DC_TYPE_MEMORY)
839 hDC = NtGdiCreateCompatibleDC(NULL); // OH~ Yuck! I think I taste vomit in my mouth!
840 else
841 hDC = IntGdiCreateDC(NULL, NULL, NULL, NULL, (DcType == DC_TYPE_INFO));
842
843 return hDC;
844 }
845