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 300
#define Y 200
#define WIDTH 200
#define HEIGHT 200
#define WIDTH_MIN 50
#define HEIGHT_MIN 50
#define BORDER_WIDTH 1
#define TITLE "Prog"
#define ICON_TITLE "Prog"
#define PRG_CLASS "Prog"
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));
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;
char str[] = "Hello!";
XDrawString(display, window, gc, 50, 50, str, strlen(str));
XFreeGC(display, gc);
XFlush(display);
break;
}
case KeyPress:
{
XSetWindowBackground(display, window, getColor("#00ff00"));
XClearWindow(display, window);
}
}
}
}
When I change the size or minimize the window to full screen, it writes and closes