142542f5fSchristos/*
242542f5fSchristos * Copyright (c) 2014 Intel Corporation
342542f5fSchristos *
442542f5fSchristos * Permission is hereby granted, free of charge, to any person obtaining a
542542f5fSchristos * copy of this software and associated documentation files (the "Software"),
642542f5fSchristos * to deal in the Software without restriction, including without limitation
742542f5fSchristos * the rights to use, copy, modify, merge, publish, distribute, sublicense,
842542f5fSchristos * and/or sell copies of the Software, and to permit persons to whom the
942542f5fSchristos * Software is furnished to do so, subject to the following conditions:
1042542f5fSchristos *
1142542f5fSchristos * The above copyright notice and this permission notice (including the next
1242542f5fSchristos * paragraph) shall be included in all copies or substantial portions of the
1342542f5fSchristos * Software.
1442542f5fSchristos *
1542542f5fSchristos * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1642542f5fSchristos * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1742542f5fSchristos * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1842542f5fSchristos * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1942542f5fSchristos * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2042542f5fSchristos * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2142542f5fSchristos * SOFTWARE.
2242542f5fSchristos *
2342542f5fSchristos */
2442542f5fSchristos
2542542f5fSchristos#ifdef HAVE_CONFIG_H
2642542f5fSchristos#include "config.h"
2742542f5fSchristos#endif
2842542f5fSchristos
2942542f5fSchristos#include <stdio.h>
3042542f5fSchristos#include <stdlib.h>
3142542f5fSchristos#include <unistd.h>
3242542f5fSchristos#include <cairo.h>
3342542f5fSchristos#include <cairo-xlib.h>
3442542f5fSchristos#include <cairo-xlib-xrender.h>
3542542f5fSchristos#include <X11/Xlib.h>
3642542f5fSchristos#include <X11/extensions/Xrender.h>
3742542f5fSchristos
3842542f5fSchristos#include <string.h>
3942542f5fSchristos
4042542f5fSchristosstatic void core_cursor(Display *dpy, int width, int height)
4142542f5fSchristos{
4242542f5fSchristos	char text[256];
4342542f5fSchristos	cairo_surface_t *surface;
4442542f5fSchristos	cairo_text_extents_t extents;
4542542f5fSchristos	cairo_t *cr;
4642542f5fSchristos	Pixmap bitmap;
4742542f5fSchristos	int scr = DefaultScreen(dpy);
4842542f5fSchristos	Window root = RootWindow(dpy, scr);
4942542f5fSchristos	XColor bg, fg;
5042542f5fSchristos	int pitch;
5142542f5fSchristos	char *data;
5242542f5fSchristos	GC gc;
5342542f5fSchristos
5442542f5fSchristos	sprintf(text, "%dx%d", width, height);
5542542f5fSchristos
5642542f5fSchristos	pitch = (width + 31) >> 5 << 2;
5742542f5fSchristos	data = calloc(pitch, height);
5842542f5fSchristos
5942542f5fSchristos	surface = cairo_image_surface_create_for_data((unsigned char *)data, CAIRO_FORMAT_A1, width, height, pitch);
6042542f5fSchristos	cr = cairo_create(surface);
6142542f5fSchristos	cairo_text_extents(cr, text, &extents);
6242542f5fSchristos	cairo_move_to(cr, 0, extents.height);
6342542f5fSchristos	cairo_show_text(cr, text);
6442542f5fSchristos	cairo_destroy(cr);
6542542f5fSchristos	cairo_surface_destroy(surface);
6642542f5fSchristos
6742542f5fSchristos	bitmap = XCreatePixmap(dpy, root, width, height, 1);
6842542f5fSchristos	gc = XCreateGC(dpy, bitmap, 0, NULL);
6942542f5fSchristos	if (gc != NULL) {
7042542f5fSchristos		XImage ximage = {
7142542f5fSchristos			.height = height,
7242542f5fSchristos			.width = width,
7342542f5fSchristos			.depth = 1,
7442542f5fSchristos			.bits_per_pixel = 1,
7542542f5fSchristos			.xoffset = 0,
7642542f5fSchristos			.format = XYPixmap,
7742542f5fSchristos			.data = data,
7842542f5fSchristos			.byte_order = LSBFirst,
7942542f5fSchristos			.bitmap_unit = 32,
8042542f5fSchristos			.bitmap_bit_order = LSBFirst,
8142542f5fSchristos			.bitmap_pad = 32,
8242542f5fSchristos			.bytes_per_line = pitch,
8342542f5fSchristos		};
8442542f5fSchristos		XPutImage(dpy, bitmap, gc, &ximage, 0, 0, 0, 0, width, height);
8542542f5fSchristos		XFreeGC(dpy, gc);
8642542f5fSchristos	}
8742542f5fSchristos	free(data);
8842542f5fSchristos
8942542f5fSchristos	XParseColor(dpy, DefaultColormap(dpy, scr), "green", &fg);
9042542f5fSchristos	XParseColor(dpy, DefaultColormap(dpy, scr), "black", &bg);
9142542f5fSchristos
9242542f5fSchristos	XDefineCursor(dpy, root, XCreatePixmapCursor(dpy, bitmap, bitmap, &fg, &bg, 0, 0));
9342542f5fSchristos	XFreePixmap(dpy, bitmap);
9442542f5fSchristos}
9542542f5fSchristos
9642542f5fSchristosstatic void render_cursor(Display *dpy, int width, int height)
9742542f5fSchristos{
9842542f5fSchristos	char text[256];
9942542f5fSchristos	cairo_surface_t *surface;
10042542f5fSchristos	cairo_text_extents_t extents;
10142542f5fSchristos	cairo_t *cr;
10242542f5fSchristos	Pixmap pixmap;
10342542f5fSchristos	Picture picture;
10442542f5fSchristos
10542542f5fSchristos	sprintf(text, "%dx%d", width, height);
10642542f5fSchristos
10742542f5fSchristos	pixmap = XCreatePixmap(dpy, DefaultRootWindow(dpy), width, height, 32);
10842542f5fSchristos	surface = cairo_xlib_surface_create_with_xrender_format(dpy, pixmap,
10942542f5fSchristos								DefaultScreenOfDisplay(dpy),
11042542f5fSchristos								XRenderFindStandardFormat(dpy, PictStandardARGB32),
11142542f5fSchristos								width, height);
11242542f5fSchristos	cr = cairo_create(surface);
11342542f5fSchristos	cairo_surface_destroy(surface);
11442542f5fSchristos
11542542f5fSchristos	cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
11642542f5fSchristos	cairo_paint(cr);
11742542f5fSchristos	cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
11842542f5fSchristos
11942542f5fSchristos	cairo_text_extents(cr, text, &extents);
12042542f5fSchristos	cairo_move_to(cr, 0, extents.height);
12142542f5fSchristos	cairo_set_source_rgb(cr, 1, 0, 1);
12242542f5fSchristos	cairo_show_text(cr, text);
12342542f5fSchristos	cairo_destroy(cr);
12442542f5fSchristos
12542542f5fSchristos	picture = XRenderCreatePicture(dpy, pixmap, XRenderFindStandardFormat(dpy, PictStandardARGB32), 0, NULL);
12642542f5fSchristos	XFreePixmap(dpy, pixmap);
12742542f5fSchristos
12842542f5fSchristos	XDefineCursor(dpy, DefaultRootWindow(dpy), XRenderCreateCursor(dpy, picture, 0, 0));
12942542f5fSchristos	XRenderFreePicture(dpy, picture);
13042542f5fSchristos}
13142542f5fSchristos
13242542f5fSchristosint main(void)
13342542f5fSchristos{
13442542f5fSchristos	Display *dpy;
13542542f5fSchristos	int sizes[] = { 24, 32, 48, 64, 72, 128, 160, 256 };
13642542f5fSchristos	int x, y;
13742542f5fSchristos
13842542f5fSchristos	dpy = XOpenDisplay(NULL);
13942542f5fSchristos	if (dpy == NULL)
14042542f5fSchristos		dpy = XOpenDisplay(":0"); /* lazy */
14142542f5fSchristos	if (dpy == NULL)
14242542f5fSchristos		return 77;
14342542f5fSchristos
14442542f5fSchristos	for (x = 0; x < sizeof(sizes)/sizeof(sizes[0]); x++) {
14542542f5fSchristos		for (y = 0; y < sizeof(sizes)/sizeof(sizes[0]); y++) {
14642542f5fSchristos			printf("Testing %dx%d (core)\n", sizes[x], sizes[y]);
14742542f5fSchristos			core_cursor(dpy, sizes[x], sizes[y]);
14842542f5fSchristos			XSync(dpy, True);
14942542f5fSchristos			sleep(2);
15042542f5fSchristos
15142542f5fSchristos			printf("Testing %dx%d (render)\n", sizes[x], sizes[y]);
15242542f5fSchristos			render_cursor(dpy, sizes[x], sizes[y]);
15342542f5fSchristos			XSync(dpy, True);
15442542f5fSchristos			sleep(2);
15542542f5fSchristos		}
15642542f5fSchristos	}
15742542f5fSchristos
15842542f5fSchristos	return 0;
15942542f5fSchristos}
160