/*
 *      hide_cursor.c
 *
 *      Copyright 2005-2007 Enrico Troeger <enrico.troeger(at)uvena(dot)de>
 *		(original from Nevrax (http://www.nevrax.com))
 *
 * 		Thanks to Broeisi Rast <broeisito(at)gmail(dot)com>
 *
 *      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, write to the Free Software
 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *
 *		little program to hide or show the mouse cursor on a X display
 *
 *		compile with
 *		gcc -lXft hide_cursor.c -o hide_cursor
 *
 *		run ./hide_cursor hide to hide the cursor
 *		and ./hide_cursor show to show the cursor
 */


#include <X11/Xlib.h>
#include <X11/cursorfont.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

Display *dpy;
Window   win;
static Cursor pointer = None;


void show_cursor(void)
{
	if (None != pointer)
	{
		XFreeCursor(dpy, pointer);
		pointer = None;
	}
	XUndefineCursor(dpy, win);
	XSync(dpy, False); /* optional */
}


void hide_cursor(void)
{
	if (None == pointer)
	{
		char bm[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
		Pixmap pix = XCreateBitmapFromData(dpy, win, bm, 8, 8);
		XColor black;
		memset(&black, 0, sizeof(XColor));
		black.flags = DoRed | DoGreen | DoBlue;
		pointer = XCreatePixmapCursor(dpy, pix, pix, &black, &black, 0, 0);
		XFreePixmap(dpy, pix);
	}
	XDefineCursor(dpy, win, pointer);
	XSync(dpy, False); /* again, optional */
}


void usage(void)
{
	fprintf(stderr,"Usage: hide_cursor [hide | show]\n");
	exit(1);
}


int main(int argc, char **argv)
{
	Screen *screen;
	dpy = XOpenDisplay(":0.0");
	screen = XScreenOfDisplay(dpy, 0);
	win = screen->root;

	if (argc != 2)
	        usage();

	if (!strcmp(argv[1],"hide"))
		hide_cursor();
	else if (!strcmp(argv[1],"show"))
		show_cursor();
	else
		usage();

	return 0;
}
