Author Topic: SLEEP not working in Dev version of QB64 with CONSOLE output  (Read 3816 times)

0 Members and 1 Guest are viewing this topic.

Offline hanness

  • Forum Regular
  • Posts: 210
    • View Profile
SLEEP not working in Dev version of QB64 with CONSOLE output
« on: October 29, 2019, 03:37:45 pm »
I think I must be missing something here:

I am finding that if output is set to display on a Windows command console only, then the SLEEP command does not seem to always work.

Take the following code sample:

REM $DYNAMIC
$CONSOLE:ONLY
_DEST _CONSOLE: _SOURCE _CONSOLE

CLS
SHELL "type SomeTextFile.txt | more"

PRINT "press any key to continue"
SLEEP

In the above example, we are displaying the contents of a text file to the console. You should use a text file that has more than one screen of data for this test.

When you run the program, text is displayed to the screen one screen at a time. You press ENTER to display the next line or SPACEBAR to display the next screen full of data.

Tap the SPACEBAR to display each page of the text file.

Once the file has been completely displayed, you will see the message "The operation completed successfully" and "Press any key to continue". At this point the program runs the SLEEP command. I would expect to be able to press any key to continue (in this case it just takes you to the end of the program). However, for some reason, the only key that works here is the SPACEBAR.

Now, repeat the above operation, but press the ENTER key to display the file one line at a time. This time, you will observe the same behavior EXCEPT that the only key that will advance past the SLEEP command is the ENTER key.

It seems as if the SLEEP command is somehow being affected by the operation of piping the text file through MORE but I can't make sense of how this is happening.

Any ideas?

Offline hanness

  • Forum Regular
  • Posts: 210
    • View Profile
Re: SLEEP not working in Dev version of QB64 with CONSOLE output
« Reply #1 on: October 29, 2019, 04:38:42 pm »
One more thing to add:

I have already worked around the issue by writing code in QB64 to display a file to the screen one screen at a time. It's more just a curiosity now about what it is that is affecting the behavior of the SLEEP command in such an odd way.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: SLEEP not working in Dev version of QB64 with CONSOLE output
« Reply #2 on: October 29, 2019, 05:53:55 pm »
_KEYCLEAR might help clear last keypress, use just before SLEEP so it has to wait for next key press.
« Last Edit: October 29, 2019, 05:57:30 pm by bplus »

Offline hanness

  • Forum Regular
  • Posts: 210
    • View Profile
Re: SLEEP not working in Dev version of QB64 with CONSOLE output
« Reply #3 on: October 29, 2019, 06:04:41 pm »
Unfortunately, _KEYCLEAR is not helping. However, I have some more information:

After testing further I found the following:

Once a command is run via SHELL that requires user input, that seems to be when SLEEP breaks.

In the below code example, I perform a SHELL "pause". The user has to hit a key to continue from the pause command. From now on, the SLEEP command will no longer continue after hitting any key EXCEPT for the same key that was hit when resuming from the PAUSE.



$CONSOLE:ONLY
_DEST _CONSOLE: _SOURCE _CONSOLE
OPTION BASE 1

CLS
PRINT "We are going to run the command PAUSE via a SHELL command. You will be asked"
PRINT "to press any key to continue. Take note of what key you press!"
PRINT

SHELL "pause"

PRINT
PRINT "You have pressed a key. Now, we are running the QB64 command SLEEP. Note that"
PRINT "you are supposed to be able to continue by pressing any key but the program"
PRINT "will NOT continue unless you press the same key that you pressed earlier!"
PRINT

SLEEP
PRINT
PRINT "Done sleeping. You must have pressed the same key you pressed earlier."
PRINT "We are going to run the SLEEP command again. Once again, you will not"
PRINT "be able to continue unless you press that same key again."
PRINT
SLEEP

END

Offline hanness

  • Forum Regular
  • Posts: 210
    • View Profile
Re: SLEEP not working in Dev version of QB64 with CONSOLE output
« Reply #4 on: October 30, 2019, 04:01:10 pm »
I'm just wondering if anyone could take the sample code that I posted in the message just prior to this one to see if they can confirm that it is not just me seeing this behavior.

That post condenses down nicely where it seems the issue is coming from.

I was using QB64 Dev. Release from Aug 28 2019.

Thanks much!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: SLEEP not working in Dev version of QB64 with CONSOLE output
« Reply #5 on: October 30, 2019, 04:34:49 pm »
Hi. I don't know at what stage is it now, but I think Steve McNeill was also doing something with the SLEEP statement (and more) for the console. Perhaps try use his version of the IDE, which can be found on his github, which also points his signature.

Your program, you ask for,  is really stuck and doesn't respond to anything. The SLEEP statement acts as an infinite loop with the normal version of the IDE.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: SLEEP not working in Dev version of QB64 with CONSOLE output
« Reply #6 on: October 30, 2019, 04:53:44 pm »
Hi. I don't know at what stage is it now, but I think Steve McNeill was also doing something with the SLEEP statement (and more) for the console. Perhaps try use his version of the IDE, which can be found on his github, which also points his signature.

Your program, you ask for,  is really stuck and doesn't respond to anything. The SLEEP statement acts as an infinite loop with the normal version of the IDE.

This does work for me, with my own repo version.  I thought I'd pushed all the changes for the console from my repo into the development repo, but I may have missed updating sleep so it works for us.  I'll dig into it as soon as Halloween time is over.  This has always been one of the busiest times here on the farm, with us hosting a haunted house (barn), corn maze, tractor rides, pumpkin carving events, and other such festivities for the kids and their families.

If you don't mind downloading the version from my repo, it should work as intended for you.  If you don't want to bother to grab all those files and extract them (I use my repo not just as a development spot for changes to QB641, but also as a means to store and archive programs/examples/past versions which I want to hold on to), then give me about a week or so and I'll see what the difference is between the two versions.  Chances are, I simply forgot to push the changes to SLEEP so that it now works with the console in Windows, so the "fix" won't be any more complex than just copy/pasting from one source to another and then pushing the changes into the development build.  ;)

Edit: Further testing shows that this was also broke in my own personal repo, but the fix has been found and is posted below.  I'll push changes into the development build ASAP, and they should be fine for downloading in about a day or so.
« Last Edit: October 30, 2019, 05:40:50 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: SLEEP not working in Dev version of QB64 with CONSOLE output
« Reply #7 on: October 30, 2019, 05:39:08 pm »
Easy fix for this issue:

1) Go into internal/c/ and open libqb.cpp.
2) Search for "void sub_sleep(int32 seconds,int32 passed){"  -- this is where our c-code for the SLEEP command is located.
3) Scroll down just a few lines (it's line 15,308 for me, but those line numbers change with each edit of the source).

Look for: 
Code: [Select]
                do{ //ignore all console input
                    junk=func__getconsoleinput();
                    junk2=consolekey;
                }while(junk!=1); //until we have a key down event

This segment is the problem, as it's too vague for what it considers a proper "key down" event.  When we press any key in the console, we generate TWO key events -- one for "key down" and one for "key up".  By using the PAUSE statement via SHELL, we clear the "key down" event and then the SLEEP command reads the "key up" as our key event -- which is why it's then trapped waiting for us to press that same key back down once again.

The change here is rather simple -- replace that code segment with the following:
Code: [Select]
                do{ //ignore all console input unless it's a keydown event
                    do{ //ignore all console input unless it's a keyboard event
                        junk=func__getconsoleinput();
                        junk2=consolekey;
                    }while(junk!=1); //only when junk = 1 do we have a keyboard event
                }while(junk2<=0); //only when junk2 > 0 do we have a key down event. (values less than 0 are key up events; 0 is a non event) 

The above says, "Let's ignore any key up events, and only work with SLEEP with a keydown event."  (junk2 is the actual keypress, whereas junk is the *source* of the keypress -- in this case, it needs to be 1 to be the keyboard and not the mouse or some other console input event).

4) Once you've made that edit, save libqb.cpp.
5) Close libqb.cpp.
6) Inside the internal/c/ folder (the same one where you found libqb.cpp, you'll see a batch file called "purge_libqb_only.bat".
7) Run that batch file to remove the old library file.  Don't worry.  QB64 will rebuild it automatically for you.

At this point, you can now open QB64.exe, paste in your test program, and have it run as you'd expect.

A simple glitch, and simple to correct.  Many thanks for helping to locate this one for me.  It's one of those little things that's easy to overlook, until people start actually testing the code and find the issue for you.   I don't use the console enough to usually catch any of these small problems, so it's much appreciated when someone does and then takes time to point them out as eloquently as you did.  The second example of yours really helped highlight to issue for me, so I could zoom in on the problem and correct it quickly.  :)

EDIT: A minor change to make certain it works as intended and doesn't respond to other external non-keyboard events.
« Last Edit: October 30, 2019, 05:56:33 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline hanness

  • Forum Regular
  • Posts: 210
    • View Profile
Re: SLEEP not working in Dev version of QB64 with CONSOLE output
« Reply #8 on: October 30, 2019, 06:55:03 pm »
Thanks. The efforts are greatly appreciated.