Code:
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define X 100
#define Y 100
#define WIDTH 200
#define HEIGHT 200
#define WIDTH_MIN 50
#define HEIGHT_MIN 50
#define BORDER_WIDTH 5
#define TITLE "Example"
#define ICON_TITLE "Example"
#define PRG_CLASS "Example"
static void SetWindowManagerHints (
Display * display,
char * PClass,
char * argv[],
int argc,
Window window,
int x,
int y,
int win_wdt,
int win_hgt,
int win_wdt_min,
int win_hgt_min,
char * ptrTitle,
char * ptrITitle,
Pixmap pixmap
)
{
XSizeHints size_hints;
XWMHints wm_hints;
XClassHint class_hint;
XTextProperty windowname, iconname;
if ( !XStringListToTextProperty (&ptrTitle, 1, &windowname ) ||
!XStringListToTextProperty (&ptrITitle, 1, &iconname ) ) {
puts ( "No memory!\n");
_exit ( 1 );
}
size_hints.flags = PPosition | PSize | PMinSize;
size_hints.min_width = win_wdt_min;
size_hints.min_height = win_hgt_min;
wm_hints.flags = StateHint | IconPixmapHint | InputHint;
wm_hints.initial_state = NormalState;
wm_hints.input = True;
wm_hints.icon_pixmap= pixmap;
class_hint.res_name = argv[0];
class_hint.res_class = PClass;
XSetWMProperties ( display, window, &windowname,
&iconname, argv, argc, &size_hints, &wm_hints,
&class_hint );
}
Display * display;
unsigned long getColor(char color[]) {
XColor hex;
Colormap colormap = DefaultColormap(display, 0);
XParseColor(display, colormap, color, &hex);
XAllocColor(display, colormap, &hex);
return hex.pixel;
}
int main(int argc, char *argv[])
{
int ScreenNumber;
XEvent report;
Window window;
if ( ( display = XOpenDisplay ( NULL ) ) == NULL ) {
puts ("Can not connect to the X server!\n");
_exit ( 1 );
}
ScreenNumber = DefaultScreen ( display );
window = XCreateSimpleWindow ( display,
RootWindow ( display, ScreenNumber ),
X, Y, WIDTH, HEIGHT, BORDER_WIDTH,
BlackPixel ( display, ScreenNumber ),
WhitePixel ( display, ScreenNumber ) );
// register interest in the delete window message
Atom wmDeleteMessage = XInternAtom(display, "WM_DELETE_WINDOW", False);
XSetWMProtocols(display, window, &wmDeleteMessage, 1);
SetWindowManagerHints ( display, PRG_CLASS, argv, argc,
window, X, Y, WIDTH, HEIGHT, WIDTH_MIN,
HEIGHT_MIN, TITLE, ICON_TITLE, 0 );
XSelectInput ( display, window, ExposureMask | KeyPressMask);
XMapWindow ( display, window );
GC gc = XCreateGC(display, window, 0, NULL);
while ( 1 ) {
XNextEvent ( display, &report );
if(report.type == ClientMessage && report.xclient.data.l[0] == wmDeleteMessage) {
printf("Shutdown!\n");
break;
}
switch ( report.type ) {
case Expose :
{
if(report.xexpose.count != 0)
break;
XSetForeground ( display, gc, getColor("#ff00ff"));
XDrawString ( display, window, gc, 20,50,
"First example", strlen ( "First example" ) );
XFreeGC ( display, gc );
XFlush(display);
break;
}
case KeyPress:
{
XGCValues values;
values.background = getColor("#00ff00");
int result = XChangeGC(display, gc, GCBackground, &values);
printf("%i:%i\n", result, BadAlloc);
char buff[200];
XGetErrorText(display, result, buff, sizeof(buff));
printf("%s\n", buff);
// XSetWindowBackground(display, window, getColor("#000000"));
// XClearArea(display, window, 0, 0, 0, 0, True);
}
}
}
}
I need to change the background through the function XChangeGC, another method does not work in my case