Thread: Xlib perversity

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    Xlib perversity

    Anyone used Xlib much? I just want to ring the speaker/bell/beeeeep with it.

    Code:
    #include <stdio.h>
    #include <X11/Xlib.h>
    
    int main(void) {
    	Display *display = NULL;
    	
    	perror("Now");
    	if ((display = XOpenDisplay(":0.0")) == NULL) {
    		perror("Can't connect to server");
    	}
    	printf("%p\n",display);
    
    	perror("Now");
    	XBell(display,100);
    	perror("Now");
    	return 0;
    }
    Complile -lX11.

    What's weird to my mind here is this output:

    Now: Success
    0xf3a010
    Now: Resource temporarily unavailable
    Now: Resource temporarily unavailable


    Apparently errno is getting set before the XBell call, but XOpenDisplay returned a valid pointer...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    It might seem odd, but this is normal behavior for errno. You are never expected to clear errno upon success, only to set it upon failure.

    The general rule is to rely on the API's method for reporting error status, and ONLY if an error occurred can you examine errno. For the scant few functions where errno is the only way of indicating failure, you should actually set errno = 0 yourself before calling those functions.

    Probably, inside XOpenDisplay(), the client tries several different strategies to find the server, and one or more of these resulted in an error.

    Ask yourself, have you made it a habit to continuously set errno to 0 whenever you try something and it succeeds? Nobody else does that either, including library code.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by brewbuck View Post
    Probably, inside XOpenDisplay(), the client tries several different strategies to find the server, and one or more of these resulted in an error.
    Okay, that makes sense. I did actually try setting errno before I posted, the outcome is much the same obviously...but I was not aware of this explanation. Thanks brewbuck. Seems strange to me that functions that could set errno would not themselves clear it first, as you say.

    Anyway my bell still doesn't ring!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    I would guess that XOpenDisplay() doesn't set errno. I would think it would be more on the lines of errno is set by a function that XOpenDisplay() uses. I'm basing this on the following
    Quote Originally Posted by man XOpenDisplay
    If successful, XOpenDisplay() returns a pointer to a Display structure, which is defined in X11/Xlib.h. If XOpenDisplay() does not succeed, it returns NULL. After a successful call to XOpenDisplay() all of the screens in the display can be used by the client. The screen number specified in the display_name argument is returned by the DefaultScreen() macro (or the XDefaultScreen() function). You can access elements of the Display and Screen structures only by using the information macros or functions. For information about using macros and functions to obtain information from the Display structure, see Display Macros.
    Note that it says on failure it returns NULL and never mentions errno. Probably there is a couple calls to open(), ioctl(), bind() etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-11-2008, 11:24 AM
  2. Xlib Fullscreen and/or borderless
    By IsmAvatar2 in forum Linux Programming
    Replies: 0
    Last Post: 06-17-2008, 11:25 AM
  3. XLib documentation
    By Series X4 1.0 in forum Linux Programming
    Replies: 1
    Last Post: 03-20-2005, 06:31 PM
  4. problems with gtk+, and xlib
    By Ian in forum Linux Programming
    Replies: 3
    Last Post: 12-02-2001, 06:43 AM