Thread: X11 application closes with an error when changing its size

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    2

    Unhappy X11 application closes with an error when changing its size

    Problem!Here is the my code:
    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:
    X Error of failed request: BadGC (invalid GC parameter)
    Major opcode of failed request: 74 (X_PolyText8)
    Resource id in failed request: 0x5e00002
    Serial number of failed request: 25
    Current serial number in output stream: 26

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    This looks very similar to The xlib program does not want to change the background color, even down to the same function and variable names. Are you taking the same class as the OP in that other thread?

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    2

    Exclamation

    Quote Originally Posted by christop View Post
    This looks very similar to The xlib program does not want to change the background color, even down to the same function and variable names. Are you taking the same class as the OP in that other thread?
    I took from him in order not to write the most tedious code.
    Help solve this bug, I'm new and don’t even know what to read!

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    I would start by following the advice that john.c gave in that thread.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 06-23-2015, 02:50 AM
  2. Changing a program to an application?
    By ratrising in forum C Programming
    Replies: 6
    Last Post: 08-26-2014, 04:22 AM
  3. Replies: 13
    Last Post: 08-30-2012, 08:51 PM
  4. Program closes when startup form closes
    By dcboy in forum C# Programming
    Replies: 1
    Last Post: 07-01-2006, 02:40 AM
  5. Changing focus in an MFC Application
    By rangalo in forum Windows Programming
    Replies: 2
    Last Post: 06-20-2005, 06:21 AM

Tags for this Thread