C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-25-2009, 11:52 AM   #1
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
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...
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 09-25-2009, 12:17 PM   #2
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
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.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 09-25-2009, 12:42 PM   #3
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
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!
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 09-25-2009, 01:02 PM   #4
Jaxom's & Imriel's Dad
 
Kennedy's Avatar
 
Join Date: Aug 2006
Location: Alabama
Posts: 801
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.
Kennedy is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Is XLib included in all Linux Distros? What about Solaris/OSX? 6tr6tr Linux Programming 1 11-11-2008 11:24 AM
Xlib Fullscreen and/or borderless IsmAvatar2 Linux Programming 0 06-17-2008 11:25 AM
XLib documentation Series X4 1.0 Linux Programming 1 03-20-2005 06:31 PM
problems with gtk+, and xlib Ian Linux Programming 3 12-02-2001 06:43 AM


All times are GMT -6. The time now is 01:36 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22