summaryrefslogtreecommitdiffstats
path: root/libfreerdp/gdi/dc.c
blob: 94562b1f717e9a7b54dc23fd0547b0d42e4e12c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 * FreeRDP: A Remote Desktop Protocol Implementation
 * GDI Device Context Functions
 *
 * Copyright 2010-2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
 * Copyright 2016 Armin Novak <armin.novak@thincast.com>
 * Copyright 2016 Thincast Technologies GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* Device Context Functions: http://msdn.microsoft.com/en-us/library/dd183554 */

#include <freerdp/config.h>

#include <stdio.h>
#include <stdlib.h>

#include <freerdp/freerdp.h>
#include <freerdp/gdi/gdi.h>

#include <freerdp/gdi/region.h>

#include <freerdp/gdi/dc.h>

/**
 * @brief Get the current device context (a new one is created each time).
 * msdn{dd144871}
 *
 * @return current device context
 */

HGDI_DC gdi_GetDC(void)
{
	HGDI_DC hDC = (HGDI_DC)calloc(1, sizeof(GDI_DC));

	if (!hDC)
		return NULL;

	hDC->format = PIXEL_FORMAT_XRGB32;
	hDC->drawMode = GDI_R2_BLACK;
	hDC->clip = gdi_CreateRectRgn(0, 0, 0, 0);

	if (!hDC->clip)
	{
		free(hDC);
		return NULL;
	}

	hDC->clip->null = TRUE;
	hDC->hwnd = NULL;
	return hDC;
}

/**
 * @brief Create a device context.
 * msdn{dd144871}
 *
 * @return new device context
 */

HGDI_DC gdi_CreateDC(UINT32 format)
{
	HGDI_DC hDC = NULL;

	if (!(hDC = (HGDI_DC)calloc(1, sizeof(GDI_DC))))
		return NULL;

	hDC->drawMode = GDI_R2_BLACK;

	if (!(hDC->clip = gdi_CreateRectRgn(0, 0, 0, 0)))
		goto fail;

	hDC->clip->null = TRUE;
	hDC->hwnd = NULL;
	hDC->format = format;

	if (!(hDC->hwnd = (HGDI_WND)calloc(1, sizeof(GDI_WND))))
		goto fail;

	if (!(hDC->hwnd->invalid = gdi_CreateRectRgn(0, 0, 0, 0)))
		goto fail;

	hDC->hwnd->invalid->null = TRUE;
	hDC->hwnd->count = 32;

	if (!(hDC->hwnd->cinvalid = (HGDI_RGN)calloc(hDC->hwnd->count, sizeof(GDI_RGN))))
		goto fail;

	hDC->hwnd->ninvalid = 0;
	return hDC;
fail:
	gdi_DeleteDC(hDC);
	return NULL;
}

/**
 * @brief Create a new device context compatible with the given device context.
 * msdn{dd183489}
 * @param hdc device context
 * @return new compatible device context
 */

HGDI_DC gdi_CreateCompatibleDC(HGDI_DC hdc)
{
	HGDI_DC hDC = (HGDI_DC)calloc(1, sizeof(GDI_DC));

	if (!hDC)
		return NULL;

	if (!(hDC->clip = gdi_CreateRectRgn(0, 0, 0, 0)))
	{
		free(hDC);
		return NULL;
	}

	hDC->clip->null = TRUE;
	hDC->format = hdc->format;
	hDC->drawMode = hdc->drawMode;
	hDC->hwnd = NULL;
	return hDC;
}

/**
 * @brief Select a GDI object in the current device context.
 * msdn{dd162957}
 *
 * @param hdc device context
 * @param hgdiobject new selected GDI object
 * @return previous selected GDI object
 */

HGDIOBJECT gdi_SelectObject(HGDI_DC hdc, HGDIOBJECT hgdiobject)
{
	HGDIOBJECT previousSelectedObject = hdc->selectedObject;

	if (hgdiobject == NULL)
		return NULL;

	if (hgdiobject->objectType == GDIOBJECT_BITMAP)
	{
		hdc->selectedObject = hgdiobject;
	}
	else if (hgdiobject->objectType == GDIOBJECT_PEN)
	{
		previousSelectedObject = (HGDIOBJECT)hdc->pen;
		hdc->pen = (HGDI_PEN)hgdiobject;
	}
	else if (hgdiobject->objectType == GDIOBJECT_BRUSH)
	{
		previousSelectedObject = (HGDIOBJECT)hdc->brush;
		hdc->brush = (HGDI_BRUSH)hgdiobject;
	}
	else if (hgdiobject->objectType == GDIOBJECT_REGION)
	{
		hdc->selectedObject = hgdiobject;
		previousSelectedObject = (HGDIOBJECT)COMPLEXREGION;
	}
	else if (hgdiobject->objectType == GDIOBJECT_RECT)
	{
		hdc->selectedObject = hgdiobject;
		previousSelectedObject = (HGDIOBJECT)SIMPLEREGION;
	}
	else
	{
		/* Unknown GDI Object Type */
		return NULL;
	}

	return previousSelectedObject;
}

/**
 * @brief Delete a GDI object.
 * msdn{dd183539}
 * @param hgdiobject GDI object
 * @return nonzero if successful, 0 otherwise
 */

BOOL gdi_DeleteObject(HGDIOBJECT hgdiobject)
{
	if (!hgdiobject)
		return FALSE;

	if (hgdiobject->objectType == GDIOBJECT_BITMAP)
	{
		HGDI_BITMAP hBitmap = (HGDI_BITMAP)hgdiobject;

		if (hBitmap->data && hBitmap->free)
		{
			hBitmap->free(hBitmap->data);
			hBitmap->data = NULL;
		}

		free(hBitmap);
	}
	else if (hgdiobject->objectType == GDIOBJECT_PEN)
	{
		HGDI_PEN hPen = (HGDI_PEN)hgdiobject;
		free(hPen);
	}
	else if (hgdiobject->objectType == GDIOBJECT_BRUSH)
	{
		HGDI_BRUSH hBrush = (HGDI_BRUSH)hgdiobject;
		free(hBrush);
	}
	else if (hgdiobject->objectType == GDIOBJECT_REGION)
	{
		free(hgdiobject);
	}
	else if (hgdiobject->objectType == GDIOBJECT_RECT)
	{
		free(hgdiobject);
	}
	else
	{
		/* Unknown GDI Object Type */
		free(hgdiobject);
		return FALSE;
	}

	return TRUE;
}

/**
 * @brief Delete device context.
 * msdn{dd183533}
 * @param hdc device context
 * @return nonzero if successful, 0 otherwise
 */

BOOL gdi_DeleteDC(HGDI_DC hdc)
{
	if (hdc)
	{
		if (hdc->hwnd)
		{
			free(hdc->hwnd->cinvalid);
			free(hdc->hwnd->invalid);
			free(hdc->hwnd);
		}

		free(hdc->clip);
		free(hdc);
	}

	return TRUE;
}