summaryrefslogtreecommitdiff
path: root/ximage.c
blob: afabfcff441328bdb211603f113c7db7daaa986b (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
#include <cairo-xlib.h>

#include "demo.h"

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

struct ximage_device {
    struct device base;
    struct framebuffer fb[2];
    int q;
};

static void
destroy (struct framebuffer *abstract_framebuffer)
{
}

static void flush (struct ximage_device *device)
{
    cairo_surface_flush (device->base.scanout);
    XFlush (cairo_xlib_surface_get_display (device->base.scanout));
}

static void
show (struct framebuffer *fb)
{
    struct ximage_device *device = (struct ximage_device *) fb->device;
    cairo_t *cr;

    cr = cairo_create (device->base.scanout);
    cairo_set_source_surface (cr, fb->surface, 0, 0);
    cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
    cairo_paint (cr);
    cairo_destroy (cr);

    flush (device);
}

static struct framebuffer *
get_fb (struct device *abstract_device)
{
    struct ximage_device *device = (struct ximage_device *) abstract_device;
    int q = ++device->q & 1;
    return &device->fb[q];
}

static void
inplace_show (struct framebuffer *fb)
{
    struct ximage_device *device = (struct ximage_device *) fb->device;
    cairo_surface_unmap_image (device->base.scanout, fb->surface);

    flush (device);
}

static struct framebuffer *
get_inplace_fb (struct device *abstract_device)
{
    struct ximage_device *device = (struct ximage_device *) abstract_device;
    device->fb[0].surface = cairo_surface_map_to_image (device->base.scanout, NULL);
    return &device->fb[0];
}

struct device *
ximage_open (int argc, char **argv)
{
    struct ximage_device *device;
    Display *dpy;
    Screen *scr;
    Window win;
    int screen;
    XSetWindowAttributes attr;
    enum split split;
    int i, similar, map;
    int x, y;

    similar = 0;
    map = 0;
    for (i = 1; i < argc; i++) {
	    if (strcmp (argv[i], "--similar") == 0)
		    similar = cairo_version () >= CAIRO_VERSION_ENCODE (1, 12, 0);
	    else if (strcmp (argv[i], "--map") == 0)
		    map = cairo_version () >= CAIRO_VERSION_ENCODE (1, 12, 0);
    }

    dpy = XOpenDisplay (NULL);
    if (dpy == NULL)
	return NULL;

    device = (struct ximage_device *) malloc (sizeof (struct ximage_device));
    device->base.name = "ximage";

    screen = DefaultScreen (dpy);
    scr = XScreenOfDisplay (dpy, screen);
    device->base.width = WidthOfScreen (scr);
    device->base.height = HeightOfScreen (scr);
    device_get_size (argc, argv, &device->base.width, &device->base.height);
    x = y = 0;
    switch ((split = device_get_split(argc, argv))) {
    case SPLIT_NONE:
	    break;
    case SPLIT_LEFT:
	    device->base.width /= 2;
	    break;
    case SPLIT_RIGHT:
	    x = device->base.width /= 2;
	    break;
    case SPLIT_TOP:
	    device->base.height /= 2;
	    break;
    case SPLIT_BOTTOM:
	    y = device->base.height /= 2;
	    break;

    case SPLIT_BOTTOM_LEFT:
	    device->base.width /= 2;
	    y = device->base.height /= 2;
	    break;
    case SPLIT_BOTTOM_RIGHT:
	    x = device->base.width /= 2;
	    y = device->base.height /= 2;
	    break;

    case SPLIT_TOP_LEFT:
	    device->base.width /= 2;
	    device->base.height /= 2;
	    break;
    case SPLIT_TOP_RIGHT:
	    x = device->base.width /= 2;
	    device->base.height /= 2;
	    break;

    case SPLIT_1_16...SPLIT_16_16:
	    split -= SPLIT_1_16;
	    x = split & 3;
	    y = (split >> 2) & 3;
	    device->base.width /= 4;
	    device->base.height /= 4;
	    x *= device->base.width;
	    y *= device->base.height;
	    break;
    }

    attr.override_redirect = True;
    win = XCreateWindow (dpy, DefaultRootWindow (dpy),
			 x, y,
			 device->base.width, device->base.height, 0,
			 DefaultDepth (dpy, screen),
			 InputOutput,
			 DefaultVisual (dpy, screen),
			 CWOverrideRedirect, &attr);
    XMapWindow (dpy, win);
    XFlush (dpy);

    device->base.scanout =
	    cairo_xlib_surface_create (dpy, win,
				       DefaultVisual (dpy, screen),
				       device->base.width, device->base.height);

    if (map) {
	    device->fb[0].show = inplace_show;
	    device->base.get_framebuffer = get_inplace_fb;
    } else {
	    if (similar) {
		    device->fb[0].surface =
			    cairo_surface_create_similar_image (device->base.scanout,
								CAIRO_FORMAT_RGB24,
								device->base.width, device->base.height);
		    device->fb[1].surface =
			    cairo_surface_create_similar_image (device->base.scanout,
								CAIRO_FORMAT_RGB24,
								device->base.width, device->base.height);
	    } else
		    device->fb[1].surface = device->fb[0].surface =
			    cairo_image_surface_create (CAIRO_FORMAT_RGB24,
							device->base.width, device->base.height);

	    device->fb[1].show = device->fb[0].show = show;
	    device->base.get_framebuffer = get_fb;
    }
    device->fb[1].destroy = device->fb[0].destroy = destroy;
    device->fb[1].device = device->fb[0].device = &device->base;

    return &device->base;
}