1/*
2 * Copyright (c) 2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 */
24
25#ifdef HAVE_CONFIG_H
26#include "config.h"
27#endif
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <unistd.h>
32#include <cairo.h>
33#include <cairo-xlib.h>
34#include <cairo-xlib-xrender.h>
35#include <X11/Xlib.h>
36#include <X11/extensions/Xrender.h>
37
38#include <string.h>
39
40static void core_cursor(Display *dpy, int width, int height)
41{
42	char text[256];
43	cairo_surface_t *surface;
44	cairo_text_extents_t extents;
45	cairo_t *cr;
46	Pixmap bitmap;
47	int scr = DefaultScreen(dpy);
48	Window root = RootWindow(dpy, scr);
49	XColor bg, fg;
50	int pitch;
51	char *data;
52	GC gc;
53
54	sprintf(text, "%dx%d", width, height);
55
56	pitch = (width + 31) >> 5 << 2;
57	data = calloc(pitch, height);
58
59	surface = cairo_image_surface_create_for_data((unsigned char *)data, CAIRO_FORMAT_A1, width, height, pitch);
60	cr = cairo_create(surface);
61	cairo_text_extents(cr, text, &extents);
62	cairo_move_to(cr, 0, extents.height);
63	cairo_show_text(cr, text);
64	cairo_destroy(cr);
65	cairo_surface_destroy(surface);
66
67	bitmap = XCreatePixmap(dpy, root, width, height, 1);
68	gc = XCreateGC(dpy, bitmap, 0, NULL);
69	if (gc != NULL) {
70		XImage ximage = {
71			.height = height,
72			.width = width,
73			.depth = 1,
74			.bits_per_pixel = 1,
75			.xoffset = 0,
76			.format = XYPixmap,
77			.data = data,
78			.byte_order = LSBFirst,
79			.bitmap_unit = 32,
80			.bitmap_bit_order = LSBFirst,
81			.bitmap_pad = 32,
82			.bytes_per_line = pitch,
83		};
84		XPutImage(dpy, bitmap, gc, &ximage, 0, 0, 0, 0, width, height);
85		XFreeGC(dpy, gc);
86	}
87	free(data);
88
89	XParseColor(dpy, DefaultColormap(dpy, scr), "green", &fg);
90	XParseColor(dpy, DefaultColormap(dpy, scr), "black", &bg);
91
92	XDefineCursor(dpy, root, XCreatePixmapCursor(dpy, bitmap, bitmap, &fg, &bg, 0, 0));
93	XFreePixmap(dpy, bitmap);
94}
95
96static void render_cursor(Display *dpy, int width, int height)
97{
98	char text[256];
99	cairo_surface_t *surface;
100	cairo_text_extents_t extents;
101	cairo_t *cr;
102	Pixmap pixmap;
103	Picture picture;
104
105	sprintf(text, "%dx%d", width, height);
106
107	pixmap = XCreatePixmap(dpy, DefaultRootWindow(dpy), width, height, 32);
108	surface = cairo_xlib_surface_create_with_xrender_format(dpy, pixmap,
109								DefaultScreenOfDisplay(dpy),
110								XRenderFindStandardFormat(dpy, PictStandardARGB32),
111								width, height);
112	cr = cairo_create(surface);
113	cairo_surface_destroy(surface);
114
115	cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
116	cairo_paint(cr);
117	cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
118
119	cairo_text_extents(cr, text, &extents);
120	cairo_move_to(cr, 0, extents.height);
121	cairo_set_source_rgb(cr, 1, 0, 1);
122	cairo_show_text(cr, text);
123	cairo_destroy(cr);
124
125	picture = XRenderCreatePicture(dpy, pixmap, XRenderFindStandardFormat(dpy, PictStandardARGB32), 0, NULL);
126	XFreePixmap(dpy, pixmap);
127
128	XDefineCursor(dpy, DefaultRootWindow(dpy), XRenderCreateCursor(dpy, picture, 0, 0));
129	XRenderFreePicture(dpy, picture);
130}
131
132int main(void)
133{
134	Display *dpy;
135	int sizes[] = { 24, 32, 48, 64, 72, 128, 160, 256 };
136	int x, y;
137
138	dpy = XOpenDisplay(NULL);
139	if (dpy == NULL)
140		dpy = XOpenDisplay(":0"); /* lazy */
141	if (dpy == NULL)
142		return 77;
143
144	for (x = 0; x < sizeof(sizes)/sizeof(sizes[0]); x++) {
145		for (y = 0; y < sizeof(sizes)/sizeof(sizes[0]); y++) {
146			printf("Testing %dx%d (core)\n", sizes[x], sizes[y]);
147			core_cursor(dpy, sizes[x], sizes[y]);
148			XSync(dpy, True);
149			sleep(2);
150
151			printf("Testing %dx%d (render)\n", sizes[x], sizes[y]);
152			render_cursor(dpy, sizes[x], sizes[y]);
153			XSync(dpy, True);
154			sleep(2);
155		}
156	}
157
158	return 0;
159}
160