Code:
#include<stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>
int main (int ArgC, char **ArgV)
{
Display *cdisDisplay;
int ciScreenNum;
Screen *cscrScreenPtr;
XWMHints *xwmhHints;
XClassHint *xchClass;
XTextProperty xtpWinName, xtpIconName;
XSizeHints *xshSize;
char *pcIconName = "Icon Name?", *pcWinName = "Window Name?", *pcProgName = "Test App";
XEvent xeEvent;
Window Handle;
unsigned long Pixel;
Colormap cmap;
cdisDisplay = XOpenDisplay(NULL);
if (cdisDisplay == NULL)
{//Failed to connect to an X Server
printf("\nFailed to connect to the display\n");
return 0;
}
else
{
ciScreenNum = DefaultScreen(cdisDisplay);
cscrScreenPtr = DefaultScreenOfDisplay(cdisDisplay);
}
cmap=XDefaultColormap(cdisDisplay,ciScreenNum);
Handle = XCreateSimpleWindow(cdisDisplay, RootWindow(cdisDisplay, ciScreenNum), 0, 0, 200, 200, 0,
BlackPixel(cdisDisplay, ciScreenNum), WhitePixel(cdisDisplay, ciScreenNum));
//Allocate space for the hints
xshSize = XAllocSizeHints();
xwmhHints = XAllocWMHints();
xchClass = XAllocClassHint();
xshSize->flags = PPosition | PSize;
XStringListToTextProperty(&pcWinName, 1, &xtpWinName);
XStringListToTextProperty(&pcIconName, 1, &xtpIconName);
xwmhHints->initial_state = NormalState;
xwmhHints->input = True;
xwmhHints->flags = StateHint | InputHint;
xchClass->res_name = pcProgName;
xchClass->res_class = "Base Win";
XSetWMProperties(cdisDisplay, Handle, &xtpWinName, &xtpIconName, 0, 0, xshSize, xwmhHints, xchClass);
XSelectInput(cdisDisplay, Handle, ExposureMask | KeyPressMask | ButtonPressMask | StructureNotifyMask);
XMapWindow(cdisDisplay, Handle); //Show the window
while (1)
{
XNextEvent(cdisDisplay, &xeEvent);
switch (xeEvent.type)
{
case (ButtonPress) :
{
//How can I set Pixel to any color?
/*Pixel = BlackPixel(cdisDisplay, ciScreenNum);
printf("Mouse button %d pressed!\n", xeEvent.xbutton.button);
XSetWindowBackground(cdisDisplay, Handle, Pixel);
XClearWindow(cdisDisplay, Handle);
XFlush(cdisDisplay);
break;*/
XColor colour_bk;
printf("Mouse button %d pressed!\n", xeEvent.xbutton.button);
colour_bk.flags=DoRed|DoGreen|DoBlue;
colour_bk.red=10000;
colour_bk.green=40000;
colour_bk.blue=0;
if (XAllocColor(cdisDisplay,cmap,&colour_bk)==0)
{
printf("XAllocColor failure.\n");
}
if (XSetWindowBackground(cdisDisplay,Handle, colour_bk.pixel)==BadGC)
{
printf("XSetBackground failure.\n");
}
XClearWindow(cdisDisplay, Handle);
XFlush(cdisDisplay);
break;
}
default :
{
printf("Unknown event: %d (discarding)\n", xeEvent.type);
break;
}
}
}
}