LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 05-17-2018, 03:14 PM   #1
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro
Posts: 6,616

Rep: Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333
How to, XEvent, while loops and response time


does any one know how to increase response time for a button click within these loops?

the sleep is for when the time is up it runs, loads the next image then changes the display to the new image. ( dockapp )

I've been trying to figure out how to put an onclick event in so it fires as soon as it has been click, and not wait until the next cycle of sleep. I have had no luck in it. the command call works it is the time delay that is has between the time clicked and time executed being governed more by the seep then a queue.



Code:
    //while (1)
    while (!sleep(( doc.getStime())))
    {
         // here systems sleep time set in seconds
        // user input it waits until the sleep time
        // is up

        while (XPending(doc.getDisplay()))
        {
            XNextEvent(doc.getDisplay(), &doc.event);
            switch (doc.event.type)
            {
                case ButtonPress:
                {
                    if (doc.event.xbutton.button == Button1)
                    cout<<"Button pressed"<<endl;
                    std::string cmd = doc.getCommandOne();
                    system( cmd.c_str() );
                    XSync(doc.getDisplay(), doc.getScreen());
                break;
                }
                default:
                break;
            }
              // here systems sleep time set in seconds
            // button time never regitures.
            //
        //    if (!sleep(doc.getStime()))
         //   {
          //      doc.DisplayImage();
         //   }
        }// end inner while loop

         // here systems sleep time set in seconds
        // user input it waits until the sleep time
        // is up


     doc.DisplayImage();

    }// end control while loop
 
Old 05-18-2018, 09:06 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,058
Blog Entries: 12

Rep: Reputation: 2593Reputation: 2593Reputation: 2593Reputation: 2593Reputation: 2593Reputation: 2593Reputation: 2593Reputation: 2593Reputation: 2593Reputation: 2593Reputation: 2593
Is that event coming from an integer handle or file handle?

select(2)

If yes, check my LQ blog, there's one about using that function.

Don't use sleep, use pure event driven to have great performance.
 
1 members found this post helpful.
Old 05-18-2018, 09:39 AM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro
Posts: 6,616

Original Poster
Rep: Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333
Quote:
Originally Posted by rtmistler View Post
Is that event coming from an integer handle or file handle?

select(2)

If yes, check my LQ blog, there's one about using that function.

Don't use sleep, use pure event driven to have great performance.
Yeah, that sleep is holding me up, no pun intended. I'm sitting here right now being baffled about which new path to take because of sleep.
it was originally just a simple while loop to just keep the Window showing until its next update with a different image. Redraw is taken care of by default behavior for the window manager.
Code:
while ( !sleep( doc.getStime() )) { doc.DisplayImage(); }
As soon as time is up, it pulls a different image from a list, loads it, sets it to the drawable ,then displays it then goes back to sleep again holding the loop in suspension.

Now adding this XEvent:

I think I'd be more of an int handle then file (io read write) handle. Because it is more time related in waiting for a time to elapse before before pulling a different image to display, then

while waiting on a set time to elapse to do something, if anything else was to take place within the time it is waiting then check to see what it is (XEvent) , if the XEvent is what it's looking for then act on it in between the time it is just "standing around waiting for the time to run out to do the other thing".

so it needs to keep looking for other events to be taking place then preform an action on them if they meet the requirements. else do not bother with them.

As neither of these event are read write actions pertaining to a file, then I'd have to say they are a int handle.

Forking came to mind, just before this post of yours did.

Thanks for the lead.

This program only has an io event that only happens after a set time elapses.

Last edited by BW-userx; 05-18-2018 at 10:39 AM.
 
Old 05-18-2018, 12:05 PM   #4
GazL
Senior Member
 
Registered: May 2008
Posts: 4,754
Blog Entries: 14

Rep: Reputation: Disabled
Timers tend to be done by the higher level toolkits like Xt/Motif/Gtk and so on. Xlib itself doesn't provide a way to do it. However, there's a well known, down and dirty approach that uses select() on Xlibs internal file descriptor.

Here's a function you can use in place of XNextEvent:
Code:
#include <unistd.h>
#include <sys/time.h>
#include <X11/Xlib.h>

Bool XNextEventTimed(Display *display, XEvent *event_return, struct timeval *timeout)
{

    /*
     *  Usage:
     *    struct timeval = { .tv_sec = 5, .tv_usec = 0 };
     *    if (XNextEventTimed(display, &event, &timeout) == True) {
     *        //.. do your event processing switch
     *    } else {
     *        //.. do your timeout thing
     *    }
     *
     *  Return:  
     *    True if an event was selected, or False when no event
     *    was found prior to the timeout or when select returned
     *    an error (most likely EINTR).
    */

    if (timeout == NULL) {
        XNextEvent(display, event_return);
        return True;
    }

    if (XPending(display) == 0) {
        int fd = ConnectionNumber(display);
        fd_set readset;
        FD_ZERO(&readset);
        FD_SET(fd, &readset);
        if (select(fd + 1, &readset, NULL, NULL, timeout) > 0) {
            XNextEvent(display, event_return);
            return True;
        } else {
            return False;
        }
    } else {
        XNextEvent(display, event_return);
        return True;
    }
}
And you can use it in your event loop, something like this:
Code:
XEvent report ;
struct timeval interval = { .tv_sec = 5, .tv_usec = 0 } ;
struct timeval next_tick = interval ;

while ( status ) {
    if (XNextEventTimed(d, &report, &next_tick)) {
        dispatch(&report);
    } else {
        fprintf(stdout,"tick...\n") ;
        next_tick  = interval ;
    }
}

fprintf(stdout, "Exiting...\n") ;
You probably should read up on select(), especially the comments relating to timeval and how it is handled on return (linux does it differently to other UNIXes).

I can't take credit for the function. I've modified it slightly and added the comments, but I found the original out there on usenet way back when.

Anyway, that might give you something to work with.

Last edited by GazL; 05-18-2018 at 12:38 PM. Reason: fixed whitespace
 
1 members found this post helpful.
Old 05-18-2018, 12:40 PM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro
Posts: 6,616

Original Poster
Rep: Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333
Quote:
Originally Posted by GazL View Post
Timers tend to be done by the higher level toolkits like Xt/Motif/Gtk and so on. Xlib itself doesn't provide a way to do it. However, there's a well known, down and dirty approach that uses select() on Xlibs internal file descriptor.

Here's a function you can use in place of XNextEvent:
Code:
removed due to its already posted ....
You probably should read up on select(), especially the comments relating to timeval and how it is handled on return (linux does it differently to other UNIXes).

I can't take credit for the function. I've modified it slightly and added the comments, but I found the original out there on usenet way back when.

Anyway, that might give you something to work with.
Thanks or this code, as I have seen exactly what you are talking about in, "Xlib itself doesn't provide a way to do it" I have been mulling over how to use that to call it when it looks to be only for a I/O event that has to take place. synchronous I/O multiplexing
Quote:
Description

select() and pselect() allow a program to monitor multiple file descriptors, waiting until one or more of the
file descriptor
s become "ready" for some class of I/O operation (e.g., input possible). A file descriptor is
considered ready if it is possible to perform the corresponding I/O operation (e.g., read(2)) without blocking.
which leads me to this which I just now googled to.

https://en.wikipedia.org/wiki/File_descriptor

the timer thing is what i need/needed to figure out how to use part.

Last edited by BW-userx; 05-18-2018 at 01:18 PM.
 
Old 05-18-2018, 04:48 PM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro
Posts: 6,616

Original Poster
Rep: Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333Reputation: 1333
thanks to the two of you:
Code:
   interval.tv_sec = doc.getStime();
    interval.tv_usec = 0;

    while (1)
    {
        if (XNextEventTimed( doc.getDisplay(), &report, &next_tick ) )
        {
            switch (report.type)
            {
                case KeyPress:
                    break;
                case ButtonPress:
                    if (doc.event.xbutton.button == Button1)
                    {
                        cout<<"Button pressed"<<endl;
                      
                        std::string cmd = doc.getCommandOne();
                        system( cmd.c_str() );
                    }
                    break;
                default:
                    break;
            }
        }
        else
        {
            fprintf(stdout,"tick...\n") ;
            doc.DisplayImage();
            next_tick  = interval ;
        }
    }
still a work in progress. Just needs a little twerking.
thanks.

Last edited by BW-userx; 05-18-2018 at 05:56 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
syntax error near unexpected token 'done' (Bash scripting, first time, loops) Longeron Linux - Newbie 3 01-19-2013 06:52 AM
[SOLVED] Xevent ButtonReleaseMask in Xlib not working? kalleanka Programming 1 11-11-2011 12:10 PM
Xlib: How to convert an XEvent's window to an object in constant time JoeyAdams Programming 3 01-23-2008 04:52 AM
response time tommytomato Ubuntu 3 07-08-2007 05:49 AM
Time required for loops redhatrosh Programming 4 03-25-2005 11:20 AM

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

All times are GMT -5. The time now is 02:19 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration