Matthew Hoggan
Matthew Hoggan

Reputation: 7594

XNextEvent Blocking At Start of Application

I have the following application.

#include <FWWindow.h>
#include <FWApplication.h>

int main(int /*argc*/, char */*argv*/[])
{
    FWApplication::Initialize();
    FWWindow *win = new FWWindow(800, 600);
    win->Show();
    FWApplication::Run();
    delete win;
}

When I run it it gets stuck on the XNextEvent() because it blocks until it gets the next event from the XServer. What I would like to know, is based on the code below, why is the XNextEvent not getting the ConfigureNotify or Expose events after I am calling XMapWindow(); I have checked to make sure my application provides the right Display based on the address in the watch window of my IDE. What am I missing to get the window to appear?


Initialize() does the following

-

FWApplication *FWApplication::Initialize()
{
    if (!_instance)
    {
        _xDisplay = XOpenDisplay(NULL);
        if (_xDisplay == NULL)
            throw "Failed to get XDisplay";
        _initialized = true;
        _instance = new FWApplication(); // Calls an empty ctor
    }
    return _instance;
}

FWWindow *win = new FWWindow(800, 600); does the following

-

FWWindow::FWWindow(int width, int height) :
        clientWidth(width),
        clientHeight(height)
{
    // These are all member variables
    xDisplay = FWApplication::GetMainDisplay();
    xScreen = DefaultScreen(xDisplay);
    xDepth  = DefaultDepth(xDisplay, xScreen);
    xVisual = DefaultVisual(xDisplay,xScreen);
    xAttributes.background_pixel = XWhitePixel(xDisplay, xScreen);
    xAttributes.border_pixel = XBlackPixel(xDisplay, xScreen);
    xAttributes.override_redirect = 0;
    xWindow = XCreateWindow(
                             xDisplay,
                             RootWindow(xDisplay, xScreen),
                             0, 0,
                             width, height,
                             0,
                             xDepth,
                             InputOutput,
                             xVisual,
                             CWBorderPixel | CWColormap | CWEventMask,
                             &xAttributes
                           );

      XSetStandardProperties(
                             xDisplay,
                             xWindow,
                             "glxsimple",
                             "glxsimple",
                             None,
                             NULL,
                             0,
                             NULL
                            );
}

win->Show(); does the following

-

void FWWindow::Show()
{                                   
    XMapWindow(xDisplay, xWindow); // xWindow and xDisplay defined in ctor above
}

And finaly FWApplication::Run(); does the following

-

int FWApplication::Run()
{
    if (!_initialized)
        return -1;
    static bool run = true;
    static Display *lDisplay = _xDisplay;
    XEvent xEvent;
    while (run)
    {
        do
        {
            XNextEvent(lDisplay, &xEvent);
            switch (xEvent.type)
            {
            case ConfigureNotify:
                {
                    unsigned int w = xEvent.xconfigure.width;
                    unsigned int h = xEvent.xconfigure.height;
                    // Do something to main widget
                }
            case Expose:
                break;
            }
        } while (XPending(GetMainDisplay()));
    }
    return EXIT_SUCCESS;
}

Upvotes: 3

Views: 1512

Answers (1)

Fr&#233;d&#233;ric Hamidi
Fr&#233;d&#233;ric Hamidi

Reputation: 262919

You're not specifying an event mask in your window's attributes, so XNextEvent() won't report any event. You should write something like:

xAttributes.background_pixel = XWhitePixel(xDisplay, xScreen);
xAttributes.border_pixel = XBlackPixel(xDisplay, xScreen);
xAttributes.override_redirect = 0;
xAttributes.event_mask = StructureNotifyMask  // for ConfigureNotify
                       | ExposureMask;        // for Expose

Upvotes: 3

Related Questions