LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-12-2009, 11:34 PM   #1
tron_thomas
Member
 
Registered: May 2004
Posts: 221

Rep: Reputation: 30
Can't get Xlib window to display


I am having trouble writing an Xlib application that displays a window. The most frustrating thing is that I've written applications like this before, and never had any problems. For some reason I cannot get this program to work properly. I'm running KDE and when I launch the program, a "button" for the application will appear on the application panel, however, no window will display. It is possible to right click on the panel button and select the Close item which will successfully close the window.

The following is source code that when built will exhibit the behaviour described above:

Code:
#include <cstring>
#include <X11/Xlib.h>
#include <GL/glx.h>

int main()
{
    Display* display = ::XOpenDisplay(NULL);

    int glXAttributes[] = 
    {
        GLX_RGBA,
        GLX_RED_SIZE, 1,
        GLX_GREEN_SIZE, 1,
        GLX_BLUE_SIZE, 1,
        GLX_DEPTH_SIZE, 1,
        GLX_DOUBLEBUFFER,
        None
    };

    XVisualInfo* visualInfo = ::glXChooseVisual(display, 0, glXAttributes);
    
    Window rootWindow = DefaultRootWindow(display);
    
    XSetWindowAttributes attributes;
    ::memset(&attributes, 0, sizeof(attributes));
    attributes.colormap = ::XCreateColormap(display, rootWindow,
        visualInfo->visual, AllocNone);

    int x = 0;
    int y = 0;
    
    unsigned int width = 640;
    unsigned int height = 480;

    Window window = ::XCreateWindow(display, rootWindow, x, y, width, height,
        0, visualInfo->depth, InputOutput, visualInfo->visual,
        CWBorderPixel | CWColormap, &attributes);
        
    ::XFree(visualInfo);
    
    Atom atom = ::XInternAtom(display, "WM_DELETE_WINDOW", False);
    ::XSetWMProtocols(display, window, &atom, 1);
    
    ::XSelectInput(display, window, ExposureMask | KeyPressMask |
        ButtonPressMask | ButtonReleaseMask | StructureNotifyMask);
    
    ::XMapWindow(display, window);
    
    bool loop = true;
    XEvent event;
    do{
        ::XNextEvent(display, &event);
        if(ClientMessage == event.type){
            if(event.xclient.data.l[0] == static_cast<long>(atom)){
                loop = false;
            }
        }
    }while(loop);
    
    ::XDeleteProperty(display, window, atom);
    ::XDestroyWindow(display, window);
    ::XFlush(display);
    ::XCloseDisplay(display);

    return 0;
}
What is needed so this program will successfully display a window?
 
Old 03-13-2009, 08:24 AM   #2
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
Does it give you any error messages (i.e. when running your application from a terminal) ?
First thing I'd check in an OpenGL app is if OpenGL is generally supported (glXQueryExtension) and then deciding what to do next.
 
Old 03-13-2009, 09:53 AM   #3
tron_thomas
Member
 
Registered: May 2004
Posts: 221

Original Poster
Rep: Reputation: 30
I have always been launching the program from GDB, and I have never noticed the program emitting any error information. This is just another aspect that adds to the frustration of the problem.

OpenGL is definitely supported. Other application I've written like this use OpenGL and they work fine. I cannot determine what the difference is between this more recent application which doesn't work and the former applications what work just fine.
 
Old 03-13-2009, 10:25 AM   #4
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
Could it be because of you specifying the bordersize as null but adding the CWBorderPixel mask ? Weird, but stranger things do happen sometimes.
Otherwise, did you try yet to replace the gl-specific arguments to XCreateWindow with "CopyFromParent" to rule out that it's something gl-specific ?

Well, that's at least the two next thoughts I had about your code. If they don't give any more hints I guess it's time to check all return values etc., and see where it all goes wrong.
 
Old 03-13-2009, 12:09 PM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

It builds and runs fine for me:

SuSE Linux 9.3, X11 R6.8.2

Are you sure $DISPLAY is set correctly? Are you able to run your program directly from the command prompt? Do standard X clients (e.g. "xclock") run OK from the same command prompt?

Sorry I can't be of more help :-(

Your .. PSM
 
Old 03-13-2009, 12:12 PM   #6
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
Could it be your window manager? Your program works on my computer, showing a window with screen garabage inside it. (fvwm on gentoo)

Maybe try adding a flush after mapping the window?
 
Old 03-13-2009, 11:40 PM   #7
tron_thomas
Member
 
Registered: May 2004
Posts: 221

Original Poster
Rep: Reputation: 30
I changed:

Code:
Window window = ::XCreateWindow(display, rootWindow, x, y, width, height,
    0, visualInfo->depth, InputOutput, visualInfo->visual,
    CWBorderPixel | CWColormap, &attributes);
to

Code:
Window window = ::XCreateWindow(display, rootWindow, x, y, width, height,
    1, visualInfo->depth, InputOutput, visualInfo->visual,
    CWBorderPixel | CWColormap, &attributes);
and now the window is displaying. Thanks everyone for you help
 
Old 03-14-2009, 01:41 PM   #8
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
So my first guess was right. Glad that it works now.
 
Old 03-14-2009, 01:43 PM   #9
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Wow - that's odd!

I know that border_width must be 0 for an InputOnly window, but I wasn't aware of any restriction where it *couldn't* be zero...

SUGGESTION:
I think maybe fantas hit it on the head:
Quote:
Could it be because of you specifying the bordersize as null but adding the CWBorderPixel mask?
Try *removing* CWBorderPixel, change border_width back from 1 to 0, and see if it works now.

QUESTION:
I'm also curious about your version info:
a) platform (e.g. Ubuntu 8.04),
b) X Windows version,
... and ...
c) desktop Window Manager (e.g. KDE xy.z)

Thanx in advance .. PSM

Last edited by paulsm4; 03-14-2009 at 01:44 PM.
 
Old 03-14-2009, 02:14 PM   #10
tron_thomas
Member
 
Registered: May 2004
Posts: 221

Original Poster
Rep: Reputation: 30
I removed the CWBorderPixel flag from the XCreateWindow and changed the border_width back to zero. The window failed to display.

I'm running Fedora 9 on an Intel Core2 Duo system.
X-Server version 11
KDE 4.2.0
NVIDIA GeForce 7600 GT/PCI/SSE2
Compiz Window Manager with Slate Horn Emerald theme.
 
Old 03-14-2009, 02:24 PM   #11
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
Less weird now (at least to me as I would have been surprised that X11 could choke on such a situation), but still enough weird that a zero-size border should give such a result.
I have no ideas about KDE or Compiz though, but in this light it sounds like that's where the bug (if we can call it like that) originates from.

Last edited by fantas; 03-14-2009 at 02:26 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to draw a bitmap in the window using xlib mirsoft Programming 1 02-10-2008 09:38 AM
xlib to create window mirsoft Programming 0 01-24-2008 01:52 AM
how to draw a bitmap in the window using xlib mirsoft Programming 4 01-23-2008 06:56 PM
topmost window with xlib CarzyIvan Programming 0 03-21-2006 03:18 PM
window without a title bar using xlib CarzyIvan Programming 1 02-11-2006 04:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration