Search code examples
linuxx11

X11 FocusIn is not working


As long as I understand it, the X11 FocusIn event is triggered whenever the window comes into focused. It the the window that the keyboard input is sent to. I am having trouble triggering this event. I have made sure to give it the FocusChangeMask when creating the window. I have created a breakpoint inside my event handler where the FocusIn event is supposed to happen and it is not stopping.

I have 2 separate window, a transparent one and one non-transparent. Currently I have it so the transparent window is always on top of the non transparent window. Whenever I switch focus and then switch back to the transparent window, the non-transparent window is directly underneath. This causes other windows to be stuck in 'between' the transparent and non-transparent window.

I have noticed that whenever I focus on the non-transparent window that is underneath that triggers the FocusIn event. I can not get the transparent window to trigger the event. Does this have something to do with the window being in 32-bit color?

What am I missing?

while(!renderer->stop)
    {
        XNextEvent(renderer->x_display, &event);
        switch(event.type)
        {
            case Expose:
            if (event.xexpose.window == gstreamer_window)
            {
                XRaiseWindow(renderer->x_display, renderer->opengl_window);
            }
            break;

            case FocusIn:
            if (event.xfocus.window == renderer->opengl_window)
            {
                XRaiseWindow(renderer->x_display, gstreamer_window);
            }
            break;

            case ConfigureNotify:
            if (event.xconfigure.window == renderer->opengl_window)
            {
                XMoveWindow(renderer->x_display, gstreamer_window,
                            event.xconfigure.x, event.xconfigure.y - top_border_offset);
            }
            break;
        }
    }

Here is how I created the window.

XSetWindowAttributes  swa;
    swa.event_mask = ExposureMask | PointerMotionMask | KeyPressMask | FocusChangeMask;
    swa.colormap = XCreateColormap(x_display, XDefaultRootWindow(x_display), visual, AllocNone);
    swa.background_pixel = 0;
    swa.border_pixel = 0;

    /* Create a window */
    opengl_window = XCreateWindow (
              x_display, parent,
              0, 0, m_plane_width, m_plane_height, 0,
              depth, InputOutput,
              visual, CWEventMask | CWBackPixel | CWColormap | CWBorderPixel,
              &swa );

Solution

  • It appears as though I set the FocusChangeMask at the wrong place. By adding the line XSelectInput(x_display, opengl_window, FocusChangeMask) , it now triggers the FocusIn event. It was triggering the other display because it had the mask but this one didn't.