FreeRDP
dc.c
1 /*
2  * FreeRDP: A Remote Desktop Protocol Implementation
3  * GDI Device Context Functions
4  *
5  * Copyright 2010-2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6  * Copyright 2016 Armin Novak <armin.novak@thincast.com>
7  * Copyright 2016 Thincast Technologies GmbH
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 /* Device Context Functions: http://msdn.microsoft.com/en-us/library/dd183554 */
23 
24 #include <freerdp/config.h>
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 
29 #include <freerdp/freerdp.h>
30 #include <freerdp/gdi/gdi.h>
31 
32 #include <freerdp/gdi/region.h>
33 
34 #include <freerdp/gdi/dc.h>
35 
43 HGDI_DC gdi_GetDC(void)
44 {
45  HGDI_DC hDC = (HGDI_DC)calloc(1, sizeof(GDI_DC));
46 
47  if (!hDC)
48  return NULL;
49 
50  hDC->format = PIXEL_FORMAT_XRGB32;
51  hDC->drawMode = GDI_R2_BLACK;
52  hDC->clip = gdi_CreateRectRgn(0, 0, 0, 0);
53 
54  if (!hDC->clip)
55  {
56  free(hDC);
57  return NULL;
58  }
59 
60  hDC->clip->null = TRUE;
61  hDC->hwnd = NULL;
62  return hDC;
63 }
64 
72 HGDI_DC gdi_CreateDC(UINT32 format)
73 {
74  HGDI_DC hDC = NULL;
75 
76  if (!(hDC = (HGDI_DC)calloc(1, sizeof(GDI_DC))))
77  return NULL;
78 
79  hDC->drawMode = GDI_R2_BLACK;
80 
81  if (!(hDC->clip = gdi_CreateRectRgn(0, 0, 0, 0)))
82  goto fail;
83 
84  hDC->clip->null = TRUE;
85  hDC->hwnd = NULL;
86  hDC->format = format;
87 
88  if (!(hDC->hwnd = (HGDI_WND)calloc(1, sizeof(GDI_WND))))
89  goto fail;
90 
91  if (!(hDC->hwnd->invalid = gdi_CreateRectRgn(0, 0, 0, 0)))
92  goto fail;
93 
94  hDC->hwnd->invalid->null = TRUE;
95  hDC->hwnd->count = 32;
96 
97  if (!(hDC->hwnd->cinvalid = (HGDI_RGN)calloc(hDC->hwnd->count, sizeof(GDI_RGN))))
98  goto fail;
99 
100  hDC->hwnd->ninvalid = 0;
101  return hDC;
102 fail:
103  gdi_DeleteDC(hDC);
104  return NULL;
105 }
106 
114 HGDI_DC gdi_CreateCompatibleDC(HGDI_DC hdc)
115 {
116  HGDI_DC hDC = (HGDI_DC)calloc(1, sizeof(GDI_DC));
117 
118  if (!hDC)
119  return NULL;
120 
121  if (!(hDC->clip = gdi_CreateRectRgn(0, 0, 0, 0)))
122  {
123  free(hDC);
124  return NULL;
125  }
126 
127  hDC->clip->null = TRUE;
128  hDC->format = hdc->format;
129  hDC->drawMode = hdc->drawMode;
130  hDC->hwnd = NULL;
131  return hDC;
132 }
133 
143 HGDIOBJECT gdi_SelectObject(HGDI_DC hdc, HGDIOBJECT hgdiobject)
144 {
145  HGDIOBJECT previousSelectedObject = hdc->selectedObject;
146 
147  if (hgdiobject == NULL)
148  return NULL;
149 
150  if (hgdiobject->objectType == GDIOBJECT_BITMAP)
151  {
152  hdc->selectedObject = hgdiobject;
153  }
154  else if (hgdiobject->objectType == GDIOBJECT_PEN)
155  {
156  previousSelectedObject = (HGDIOBJECT)hdc->pen;
157  hdc->pen = (HGDI_PEN)hgdiobject;
158  }
159  else if (hgdiobject->objectType == GDIOBJECT_BRUSH)
160  {
161  previousSelectedObject = (HGDIOBJECT)hdc->brush;
162  hdc->brush = (HGDI_BRUSH)hgdiobject;
163  }
164  else if (hgdiobject->objectType == GDIOBJECT_REGION)
165  {
166  hdc->selectedObject = hgdiobject;
167  previousSelectedObject = (HGDIOBJECT)COMPLEXREGION;
168  }
169  else if (hgdiobject->objectType == GDIOBJECT_RECT)
170  {
171  hdc->selectedObject = hgdiobject;
172  previousSelectedObject = (HGDIOBJECT)SIMPLEREGION;
173  }
174  else
175  {
176  /* Unknown GDI Object Type */
177  return NULL;
178  }
179 
180  return previousSelectedObject;
181 }
182 
190 BOOL gdi_DeleteObject(HGDIOBJECT hgdiobject)
191 {
192  if (!hgdiobject)
193  return FALSE;
194 
195  if (hgdiobject->objectType == GDIOBJECT_BITMAP)
196  {
197  HGDI_BITMAP hBitmap = (HGDI_BITMAP)hgdiobject;
198 
199  if (hBitmap->data && hBitmap->free)
200  {
201  hBitmap->free(hBitmap->data);
202  hBitmap->data = NULL;
203  }
204 
205  free(hBitmap);
206  }
207  else if (hgdiobject->objectType == GDIOBJECT_PEN)
208  {
209  HGDI_PEN hPen = (HGDI_PEN)hgdiobject;
210  free(hPen);
211  }
212  else if (hgdiobject->objectType == GDIOBJECT_BRUSH)
213  {
214  HGDI_BRUSH hBrush = (HGDI_BRUSH)hgdiobject;
215  free(hBrush);
216  }
217  else if (hgdiobject->objectType == GDIOBJECT_REGION)
218  {
219  free(hgdiobject);
220  }
221  else if (hgdiobject->objectType == GDIOBJECT_RECT)
222  {
223  free(hgdiobject);
224  }
225  else
226  {
227  /* Unknown GDI Object Type */
228  free(hgdiobject);
229  return FALSE;
230  }
231 
232  return TRUE;
233 }
234 
242 BOOL gdi_DeleteDC(HGDI_DC hdc)
243 {
244  if (hdc)
245  {
246  if (hdc->hwnd)
247  {
248  free(hdc->hwnd->cinvalid);
249  free(hdc->hwnd->invalid);
250  free(hdc->hwnd);
251  }
252 
253  free(hdc->clip);
254  free(hdc);
255  }
256 
257  return TRUE;
258 }