ali
ali

Reputation: 11045

Get X11 Window inside the main loop for an event

I come from Windows, where, inside WndProc you can find out what window handler is related to a specific message. I want to know if this is also possible with X11

while (!done) {
    XNextEvent(dis, &xev);

    if(xev.type == Expose) {
        // I want to know what window is being exposed here
    }

    if (xev.type == KeyPress) {
        // I want to know what window has received a key press here
    }
}

How could I achieve it? Really couldn't find anything so far

Also, in Win32, you can store an object pointer for a class you create to represent your window, using SetWindowLong, which you can later get in the WndProc callback. Is there a way to store an object pointer in the X11 case, so that it can be later retrieved in the same way, when processing the events?

Upvotes: 0

Views: 1275

Answers (2)

Stephen Brickles
Stephen Brickles

Reputation: 41

You don't need to retrieve the Window from each event type, you can use

Window w = event.xany.window;

at the top of your event loop, before you even detect what kind of event it is. You can use

XContext ClassID = XUniqueContext();

as a global variable to use with the XSaveContext function. Then you can use

XSaveContext( display, w, ClassID, (XPointer)myclass );

to store the Class pointer on the X Window itself. So once you have the Window from the event, you can retrieve the Class from the Window using

XPointer return_class;
XFindContext( display, w, &return_class );
MyClass myclass = (MyClass *)return_class;

and so on...

Upvotes: 4

JvO
JvO

Reputation: 3106

For those events that are related to X windows, their 'overloaded' event structure has a Window parameter.

XEvent is a union, a collection of message specific structures mapped into one structure. So, to get to the proper event structure, you use this:

   if (xev.type == KeyPress)
   {
      Window w = xev.xkey.window;
   }
   if (xev.type == Expose)
   {
      Window w = xev.xexpose.window;
   }

Et cetera. Each event structure has only the parameters it needs.

I don't know about an object pointer for an X window; however, you could use a std::map to keep a list from Window ID that maps to a pointer, struct or class and keep track of it globally.

Upvotes: 2

Related Questions