summaryrefslogtreecommitdiff
path: root/lpimage.c
blob: 852e33acf10d111401aa20a8205d26a4c7995aa2 (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
/*
 * $Id$
 *
 * Copyright © 2004 Keith Packard
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Keith Packard not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Keith Packard makes no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

#include "lpint.h"

Bool
_xlightpipe_create_window_image (light_pipe_window *lpw)
{
    int		image_size;
    XGCValues	gcv;
    
    lpw->public.image = XCreateImage (lpw->display,
				      lpw->visual,
				      lpw->depth,
				      ZPixmap,
				      0,
				      0,
				      lpw->public.geometry.width,
				      lpw->public.geometry.height,
				      32,
				      0);
    if (!lpw->public.image)
	return False;
    image_size = (lpw->public.image->bytes_per_line *
		  lpw->public.geometry.height);
    if (!image_size)
    {
	XDestroyImage (lpw->public.image);
	return False;
    }
    lpw->shm_info.shmid = shmget (IPC_PRIVATE, image_size, IPC_CREAT|0600);
    if (lpw->shm_info.shmid < 0)
    {
	XDestroyImage (lpw->public.image);
	lpw->public.image = 0;
	return False;
    }
    lpw->shm_info.shmaddr = (char *) shmat (lpw->shm_info.shmid, 0, 0);
    if (lpw->shm_info.shmaddr == ((char *) -1))
    {
	XDestroyImage (lpw->public.image);
	lpw->public.image = 0;
	shmctl (lpw->shm_info.shmid, IPC_RMID, 0);
	return False;
    }
    lpw->shm_info.readOnly = False;
    XShmAttach (lpw->display, &lpw->shm_info);
    lpw->public.image->data = lpw->shm_info.shmaddr;
    lpw->public.image->obdata = (char *) &lpw->shm_info;
    lpw->shm_pixmap = XShmCreatePixmap (lpw->display,
					lpw->window,
					lpw->public.image->data,
					&lpw->shm_info,
					lpw->public.geometry.width,
					lpw->public.geometry.height,
					lpw->depth);
    gcv.graphics_exposures = False;
    gcv.subwindow_mode = IncludeInferiors;
    lpw->shm_gc = XCreateGC (lpw->display,
			     lpw->window,
			     GCGraphicsExposures|GCSubwindowMode,
			     &gcv);
    return True;
}

void
_xlightpipe_destroy_window_image (light_pipe_window *lpw)
{
    if (lpw->public.image)
    {
	XShmDetach (lpw->display, &lpw->shm_info);
	XSync (lpw->display, False);
	(void) shmdt (&lpw->shm_info.shmaddr);
	(void) shmctl (lpw->shm_info.shmid, IPC_RMID, 0);
	lpw->public.image->data = 0;
	lpw->public.image->obdata = 0;
	XDestroyImage (lpw->public.image);
	XFreePixmap (lpw->display, lpw->shm_pixmap);
	XFreeGC (lpw->display, lpw->shm_gc);
	lpw->public.image = 0;
    }
}

void
_xlightpipe_update_region (light_pipe_window *lpw, int x, int y, int width, int height)
{
    XCopyArea (lpw->display,
	       lpw->window,
	       lpw->shm_pixmap,
	       lpw->shm_gc,
	       x, y, width, height, x, y);
}