Raise/Map and Focus a window: BadMatch error

Peter Harris peter.harris at hummingbird.com
Tue Dec 18 11:19:15 PST 2007


Andrew Troschinetz wrote:
> Here's what I've got thus far:
> 
> void map_raise_focus(Display *display, Window window) {
> 	XEvent e;
> 	XSelectInput (display, windw, ExposureMask);

(Are you sure you want to overwrite the window's event mask? I don't see
you restoring the previous event mask anywhere.)

> 	XMapRaised (display, window);
> 	while (e.type != Expose) {
> 		XNextEvent (display, &e);
> 	}
> 	XSelectInputFocus (display, window, RevertToParent, CurrentTime);
> }
> 
> Sometimes (not always) I get a BadMatch error (Details: serial 2738  
> error_code 8 request_code 42 minor_code 0) which I'm sure is being  
> caused by my XSelectInputFocus() call. I have a feeling that I'm doing  
> something very wrong here but looking at the docs I can't seem to  
> figure out how one would go about writing a function like  
> map_raise_focus().
> 
> Can anyone shed some light on this problem for me please?

The problem is that you can't set focus to a window that isn't viewable.
Either the window's parent isn't mapped, or you haven't waited long
enough for the window manager to map your window.

First, you should make sure that this function is only called on
toplevel windows (or that all the window's parents are already mapped).

XNextEvent isn't polite, as you are consuming events that the rest of
the application may need. You probably want to use XWindowEvent or
XPeekIfEvent instead of XNextEvent. That aside, checking for an Expose
event (on any window) is not a reliable indicator that this window has
been mapped. Even if there are no other windows that this application
has created, you may receive an early expose event if BackingStores (or
maybe Composite) is enabled. Instead, you should check for a MapNotify
event on only the window you are interested in. Select
StructureNotifyMask instead of ExposureNotifyMask to get this event.

Note that you might be waiting for a long time if the window manager
decides to start your window minimized, or (in the case of twm) if the
user is away from the screen. It might be better to set a flag, and
check the flag inside the main loop's MapNotify handler.

Peter Harris
-- 
     Hummingbird Connectivity - A Division of Open Text
Peter Harris                    http://connectivity.hummingbird.com
Research and Development        Phone: +1 905 762 6001
peter.harris at hummingbird.com    Toll Free: 1 877 359 4866




More information about the xorg mailing list