File: FImage.c

package info (click to toggle)
fvwm 1:2.6.8-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 15,804 kB
  • sloc: ansic: 145,770; xml: 17,093; perl: 7,302; sh: 4,921; makefile: 1,094; yacc: 688; lex: 187; sed: 11
file content (266 lines) | stat: -rw-r--r-- 5,675 bytes parent folder | download
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
259
260
261
262
263
264
265
266
/* -*-c-*- */
/* This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see: <http://www.gnu.org/licenses/>
 */

/* ---------------------------- included header files ---------------------- */

#include "config.h"

#include <stdio.h>

#include <X11/Xlib.h>

#include "libs/fvwmlib.h"
#include "libs/PictureBase.h"
#include "libs/FImage.h"

/* ---------------------------- local definitions -------------------------- */

/* ---------------------------- local macros ------------------------------- */

/* ---------------------------- imports ------------------------------------ */

/* ---------------------------- included code files ------------------------ */

/* ---------------------------- local types -------------------------------- */

/* ---------------------------- forward declarations ----------------------- */

/* ---------------------------- local variables ---------------------------- */

int FShmMajorOpCode = -10000;
int FShmEventBase = -10000;
int FShmErrorBase = -10000;
Bool FShmInitialized = False;
Bool FShmImagesSupported = False;

/* ---------------------------- exported variables (globals) --------------- */

/* ---------------------------- local functions ---------------------------- */

static int FShmErrorHandler(Display *dpy, XErrorEvent *ev)
{
	FShmImagesSupported = False;
	return 0;
}

static void FShmInit(Display *dpy)
{
	if (FShmInitialized)
	{
		return;
	}

	FShmInitialized = True;

	if (!XShmSupport)
	{
		return;
	}
	FShmImagesSupported = XQueryExtension(
		dpy, "MIT-SHM", &FShmMajorOpCode, &FShmEventBase,
		&FShmErrorBase);
}

static void FShmSafeCreateImage(
	Display *dpy, FImage *fim, Visual *visual, unsigned int depth,
	int format, unsigned int width, unsigned int height)
{
	Bool error = False;
	XErrorHandler save_handler;

	if (!XShmSupport)
	{
		return;
	}
	fim->shminfo = (FShmSegmentInfo *)safecalloc(
		1, sizeof(FShmSegmentInfo));
	if (!(fim->im = FShmCreateImage(
		dpy, visual, depth, format, NULL, fim->shminfo,
		width, height)))
	{
		error = True;
		goto bail;
	}
	fim->shminfo->shmid = Fshmget(
		IPC_PRIVATE,
		fim->im->bytes_per_line * fim->im->height,
		IPC_CREAT|0777);
	if (fim->shminfo->shmid <= 0)
	{
		error = True;
		goto bail;
	}
	fim->shminfo->shmaddr = fim->im->data = Fshmat(
		fim->shminfo->shmid, 0, 0);
	if (fim->shminfo->shmaddr == (char *)(-1))
	{
		error = True;
		goto bail;
	}
	fim->shminfo->readOnly = False;

	/* use the error handler for a definitive error */
	save_handler = XSetErrorHandler(FShmErrorHandler);
	if (!FShmAttach(dpy, fim->shminfo))
	{
		error = True;
	}
	else
	{
		XSync(dpy, False);
	}

	if (!error && !FShmImagesSupported)
	{
		/* get an X error: we are a remote client */
		if (FShmDetach(dpy, fim->shminfo))
		{
			XSync(dpy, False);
		}
		error = True;
	}
	XSetErrorHandler(save_handler);

 bail:
	if (error)
	{

		if (fim->im)
		{
			XDestroyImage (fim->im);
			fim->im = NULL;
		}
		if (fim->shminfo->shmaddr)
		{
			Fshmdt(fim->shminfo->shmaddr);
		}
		if (fim->shminfo->shmid > 0)
		{
			Fshmctl(fim->shminfo->shmid, IPC_RMID, 0);
		}
		free(fim->shminfo);
		fim->shminfo = NULL;
	}
}

/* ---------------------------- interface functions ------------------------ */

FImage *FCreateFImage (
	Display *dpy, Visual *visual, unsigned int depth, int format,
	unsigned int width, unsigned int height)
{
	FImage *fim;

	FShmInit(dpy);

	fim = (FImage *)safemalloc(sizeof(FImage));
	fim->im = NULL;
	fim->shminfo = NULL;

	if (XShmSupport && FShmImagesSupported)
	{
		FShmSafeCreateImage(
			dpy, fim, visual, depth, format, width, height);
	}

	if(!fim->im )
	{
		if ((fim->im = XCreateImage(
			dpy, visual, depth, ZPixmap, 0, 0, width, height,
			Pdepth > 16 ? 32 : (Pdepth > 8 ? 16 : 8), 0)))
		{
			fim->im->data = safemalloc(
				fim->im->bytes_per_line * height);
		}
		else
		{
			free(fim);
		}
	}

	return fim;
}

FImage *FGetFImage(
	Display *dpy, Drawable d, Visual *visual,
	unsigned int depth, int x, int y, unsigned int width,
	unsigned int height, unsigned long plane_mask, int format)
{
	FImage *fim;

	FShmInit(dpy);

	fim = (FImage *)safemalloc(sizeof(FImage));
	fim->im = NULL;
	fim->shminfo = NULL;

	if (XShmSupport && FShmImagesSupported)
	{
		FShmSafeCreateImage(
			dpy, fim, visual, depth, format, width, height);
		if (fim->im)
		{
			(void)FShmGetImage(dpy, d, fim->im, x, y, plane_mask);
		}
	}

	if (!fim->im)
	{
		fim->im = XGetImage(
			dpy, d, x, y, width, height, plane_mask, format);
	}

	return fim;
}

void FPutFImage(
	Display *dpy, Drawable d, GC gc, FImage *fim, int src_x, int src_y,
	int dest_x, int dest_y, unsigned int width, unsigned int height)
{

	if (fim->shminfo)
	{
		if (FShmPutImage(
			dpy, d, gc, fim->im, src_x, src_y, dest_x, dest_y, width,
			height, False))
		{
		}
	}
	else
	{
		XPutImage(
			dpy, d, gc, fim->im, src_x, src_y, dest_x, dest_y, width,
			height);
	}
}

void FDestroyFImage(Display *dpy, FImage *fim)
{
	if (fim->shminfo)
	{
		if (FShmDetach(dpy, fim->shminfo))
		{
		}
	}
	XDestroyImage (fim->im);
	if (fim->shminfo)
	{
		Fshmdt(fim->shminfo->shmaddr);
		Fshmctl(fim->shminfo->shmid, IPC_RMID, 0);
		free(fim->shminfo);
	}
	free(fim);
}