Question

I'm writing a simple window class which is failing to return a display. Here's the short version:

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

class WindowImpl
{
    public:
        WindowImpl()
        {
            open = true;
        }

        WindowImpl(float width, float height)
        {
            if(!create(width, height))
            {
                fprintf(stderr, "Could not open display\n");
                exit(1);
            }

            open = true;
        }

        ~WindowImpl()
        {
            XCloseDisplay(display);
        };

        bool create(float width, float height)
        {
            display = XOpenDisplay(NULL);
            if(display == NULL)
                return false;

            int displayID = DefaultScreen(display);

            window = XCreateSimpleWindow(display, RootWindow(display, displayID), 10, 10, width, height, 1, BlackPixel(display, displayID), WhitePixel(display, displayID));
            XMapWindow(display, window);

            return true;
        }

        bool isOpen()
        {
            return open;
        }

        void close()
        {
            open == false;
        }

    private:
        Display* display;
        Window window;
        bool open;
};


int main()
{
    WindowImpl myWindow(1920, 1080);
    char* cmd;

    while(myWindow.isOpen())
    {
        if(gets(cmd) == "close")
            myWindow.close();
    }

    return 0;
}

WindowImpl::create fails, XOpenDisplay is returning NULL but I'm not sure why. Hopefully someone could shed some light on the problem here.

Edit: Changing WindowImpl::create to return true and false instead of 0 and 1 causes it to go through but the window still doesn't open;

For clarification:

#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    Display* display;
    Window window;
    XEvent event;
    char* message = "Hello";
    int screenSize;

    display = XOpenDisplay(NULL);
    if(display == NULL)
    {
        fprintf(stderr, "Cannot open display\n");
        exit(1);
    }

    screenSize = DefaultScreen(display);
    window = XCreateSimpleWindow(display, RootWindow(display, screenSize), 10, 10, 1920, 1080, 1, BlackPixel(display, screenSize), WhitePixel(display, screenSize));
    XSelectInput(display, window, ExposureMask | KeyPressMask);
    XMapWindow(display, window);

    KeySym keysym = XK_Escape;
    KeyCode keycode = XKeysymToKeycode(display, keysym);

    while(true)
    {
        XNextEvent(display, &event);
        if(event.type == KeyPress && event.xkey.keycode == keycode)
            break;
    }

    XCloseDisplay(display);
    return 0;
}

Compiles and runs just fine.

Was it helpful?

Solution

When you create a windows, you need to let the X server to handle output buffer. Normally this is done when you enter into the window event loop, i.e. calling any function that has EVENT in its name.

Once you are not dealing with events in your code, another way to flush the output buffer is calling XFlush, as said in its manual:

The XFlush function flushes the output buffer. Most client applications need not use this function because the output buffer is automatically flushed as needed by calls to XPending, XNextEvent, and XWindowEvent. Events generated by the server may be enqueued into the library's event queue.

The XSync function flushes the output buffer and then waits until all requests have been received and processed by the X server.

So, to solve your issue, I suggest to put this line of code:

        window = XCreateSimpleWindow(display, RootWindow(display, displayID), 10, 10, width, height, 1, BlackPixel(display, displayID), WhitePixel(display, displayID));
        XMapWindow(display, window);

        XFlush(display);   // <------------

        return true;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top