Thursday, June 26, 2008

XGetWindowAttributes and You

Right at the end of a project I've been working on, I run into a very weird issue. As per the title of this post, it's with XGetWindowAttributes and the XWindowAttributes struct.

Reference page:
XGetWindowAttributes

The main reason I'm looking to use this function is for XWindowAttributes.class, which will tell me if the window I have is InputOutput (visible) or InputOnly (not visible). If there's a simpler way to detect a window's visibility, I couldn't find it.

Anyway, in my coding I have this:


XWindowAttributes attr;
XGetWindowAttributes(display, hwnd, &attr);
if(attr.class == InputOutput){
[...]
}else{
[...]
}


Looks fine, yes? Well, it won't compile.

error: expected unqualified-id before class
error: expected `)' before class

Some research on the error turns up next to nothing; just a bunch of people saying that switching header include orders fixes it. Didn't fix it for me.

So then I figure, perhaps the documentation is old, and "class" has been removed. Changing "class" to a random string returns a different error, so that's not it. So I looked at the newest documentation I could find - the source. Turns out that it WAS changed, and here it is (abbridged):


typedef struct {
int x, y; /* location of window */
[...]
Window root; /* root of screen containing window */
#if defined(__cplusplus) || defined(c_plusplus)
int c_class; /* C++ InputOutput, InputOnly*/
#else
int class; /* InputOutput, InputOnly*/
#endif
int bit_gravity; /* one of bit gravity values */
[...]
Screen *screen; /* back pointer to correct screen */
} XWindowAttributes;


As it turns out, my code is C++, and apparently there's a different member for C and C++ (whoever heard of doing that? I fail to see the reasoning - though I'm sure there is some).

Anyway, a simple change of variable name to c_class fixed the errors, and it compiles successfully. Mark another one up for non-deterministic error messages.

4 comments:

Anonymous said...

FYI:

Quite a few of the XLib structures were created before C++, which means they were straight C. Well, since class is a reserved word in C++, but not in C, there were two options:
Break all existing code by renaming the "class" member.
Rename the "class" member to something else when compiling in C++.

Too bad the creators of the X libraries were not psychic and couldn't see into the future that "class" would be a reserved word in C++...

Solnus said...

Oh, well that makes sense! I should have known that. Thanks!

Unknown said...

How do you add code blocks to blogger???? they look so pretty!

TELL ME.....

Solnus said...

Blogger Syntax Highlighter Widget

Usage