Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I've just started working with xlib.
Can anyone tell me if there's any nonblocking equivalent to XNextEvent() ?
XCheckWindowEvent() and others are not what i really want.
I'm writing a programme which draws to a window, after every time interval 't'. But i also want to receive user input events. If i use XNextEvent(), it blocks until there's some input, so it'll not allow me to draw to screen continously. On the other hand, if i use XCheckWindowEvent() in a loop like:
while(1)
{
if(time interval 't' has elapsed)
draw something
if(XCheckWindowEvent())
{
handle input event
}
}
this eats up cpu time like anything.
So what i really require is a function, which blocks for a period of time 't', or until an input event is received from XServer, whichever is earlier. Thus, when it doesn't need to do anything, the programme will simply block, rather than keep on looping!
I might be able to use select() system call to block for time 't', but then it'll block even if an event is received, which will make the user wait, making the programme less responsive. Not something desirable.
The following code snippet is not blocking where it should! Please take a look...
struct timespec tv; //used to manage update intervals
while(1)
{
printf("\nLoop"); //this is where is actually blocks!! Strange.
//if any input event occured while the process was blocked for time
'updateInterval' microseconds, retrive & process it
if(XCheckWindowEvent(this->display, this->window, ExposureMask |
ButtonPressMask, &anEvent))
{
printf("\nGot an event");
switch(anEvent.type)
{
case Expose:
{
//code to be added later
break;
}
case ButtonPress:
{
if(anEvent.xbutton.button == Button3)
{
XCloseDisplay(this->display);
printf("\nClosed X server connection.\n\n");
this->cleanUpBodies(); //delete all bodies
return (1); //successful
completion of simulation
}
}
default: break;
}//end of event switch
}
//now draw the simulation window to depict updated state.
this->updateSimulationScreen(DRAW);
printf("\nDraw");
//wait for a time of 'updateInterval' microseconds
tv.tv_sec = 0;
tv.tv_usec = updateInterval;
select(0, NULL, NULL, NULL, &tv); //this is where it should
block
printf("\nSelect");
//erase current simulation screen
this->updateSimulationScreen(ERASE);
printf("\nErase");
//update state of all 'bodies'
this->updateState();
printf("\nUpdate State");
}
The Idea is, in the while loop, the code begins by checking is a mouse 3rd button click
event has occured or no. If yes, quit. Else, draw something to the window (
updateSimulationScreen(DRAW)), and block for 'updateInterval' microseconds. For blocking, i
have used select(). This is where the programme should block. After 'updateInterval'
microseconds, whatever was drawn to window previously should be erased (
updateSimulationScreen(ERASE)), and a call should be made updateState(). This updateState()
tells what's new to be drawn (doesn't actually draw).
Repeat loop.
The problem is that programme blocks at beginning of while loop, after executing printf("
\nLoop");
Not immediately after call to select()!!!
So there's no gap between calls to updateSimulationScreen(DRAW) and updateSimulationScreen(
ERASE), resulting in blank screen!!
Instead of select(), i tried using sleep() and nanosleep(). But the problem remains!
Please help.
Following is other relevant code:
void nature::updateSimulationScreen(enum UPDATE_MODE action)
//! Draws/erases all 'bodies' to/from the simulation
window and returns.
/*! Each 'body' is represented by a circle in the
simulation window.
* If 'action' is DRAW, all bodies contained in
'bodyList' are drawn to simulation window.
* This is done by drawing the 'bodies' using
foreground colour.
*
* If 'action' is ERASE, all bodies contained in
'bodyList' are erased from simulation window.
* This is done by drawing the 'bodies' using
background colour.
*/
{
if(action == ERASE)
{
//make background and foreground colours same
XSetForeground(this->display, this->gc, this->whitePixelID);
}
//for every 'body' in 'bodyList', compute x and y pixel coordinates, from it's x and
windowHeight/2) )
//if the body lies outside drawable area, then no need to draw/erase it!
{
temp = temp->next;
}
else
{
this->translateCoordinates(&screenX, &screenY);
XDrawArc(this->display, this->window, this->gc, (int)screenX -
if(action == ERASE)
{
//set foreground colour back to black
XSetForeground(this->display, this->gc, this->blackPixelID);
}
XFlush(this->display);
}
Sample output:
kapil@linux:~/Kopera/n-body-simulation> ./test
Loop <--output pauses after printing this!
Draw <--not after printing this!!
Select
Erase
Update State
Loop
Draw
Select
Erase
Update State
.
.
.
Loop
Draw
Select
Erase
Update State
Loop
Got an event
Closed X server connection.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.