[Date Prev] [Date Next] [Thread Prev] [Thread Next] Date Index Thread Index Search archive:
Date:Mon Sep 25 23:24:50 1994 
Subject:Re: Pause 
From:Aaron Sloman 
Volume-ID:940927.01 

yan@uk.ac.keele.cs ("M. Sapiyan") writes:

> Date: 15 Sep 1994 11:46:07 +0100
>
> Hi,
>    I am running a simulation of a motion. At certain stages of the simulation,
> I need to interrupt the motion. This is to enable the user to investigate the
> details of the motion at that particular stage. My question is:
>
>   In Pop11, is it possible to pause from whatever the program is doing, by
>   pressing a key, say space bar, and pressing the same key for the
>   second time,
>   lets the program resume its process.
>
> Thanks in advance for any pointers.

If you wish to associate an asynchronous event handler with a
keypress then it is possible via the X window facilities but you
would have to do a lot of reading in the files in

    $usepop/pop/x/pop/ref/*

However, many of them are very hard to follow as they assume you
have already mastered all the general X documentation.

Another possibility is to redefine the interrupt handler in your
program, and then use CTRL-c to interrupt and pause, until
you type another character.

Here is an illustration of how to do this:
-------------------------------------------------------------------
define silly_program(limit);
    ;;; limit specifies number of times to iterate, below.

    lvars limit;

    ;;; Save the previous version of interrupt, in case of disasters
    lvars oldinterrupt = interrupt;

    ;;; Now define a local version of interrupt, invoked by CTRL-c
    define dlocal interrupt();
        ;;; as a precaution, make sure that this is interruptable
        dlocal interrupt = oldinterrupt;

        ;;; In case it's running in VED do this
        if vedediting then
            ;;; Ensure current file (e.g. for output) is properly aligned
            vedcheck();
            ;;; Ensure that all output so far is visible
            vedscr_flush_output();
            ;;; ensure printing goes into VED output buffer.
            ;;; next line is optional: switch to an interaction file
            vedselect('interact');
            ;;; Make printing during pause to into VED buffer
            dlocal cucharout = vedcharinsert;
            vedcheck();
            ;;; Make sure printing occurs in correct place on screen
            vedsetcursor();
            vedscr_flush_output();
        endif;
        pr(newline);
        pr('PAUSING: press RETURN key to continue, CTRL-C to abort.');
        pr(newline);
        if vedediting then
            vedscr_read_ascii() ->;
        else
            charin() ->;
        endif;
        'CONTINUING' =>
        pr(newline);
    enddefine;

    ;;; now a program that endlessly prints things
    lvars x;
    for x from 1 to limit do spr(x); endfor;
enddefine;

;;; Run this and test interrupting by typing CTRL-C, then RETURN to
;;; continue.
silly_program(3000);
-------------------------------------------------------------------

Another way to handle this is to "poll" from time to time, to see
whether there is any input waiting on the terminal, and if so to
read a character and pause until another character is typed.

Here's an example of how to do this:
-------------------------------------------------------------------
define silly_program_2(limit);
    ;;; limit specifies number of times to iterate, below.

    lvars limit;

    ;;; Now define a pause_handler
    define lconstant pause();
        ;;; as a precaution, make sure that this is interruptable

        ;;; In case it's running in VED do this
        if vedediting then
            ;;; Ensure current file (e.g. for output) is properly aligned
            vedcheck();
            ;;; Ensure that all output so far is visible
            vedscr_flush_output();
            ;;; ensure printing goes into VED output buffer.
            ;;; next line is optional: switch to an interaction file
            vedselect('interact');
            ;;; Make printing during pause to into VED buffer
            dlocal cucharout = vedcharinsert;
            vedcheck();
            ;;; Make sure printing occurs in correct place on screen
            vedsetcursor();
            vedscr_flush_output();
        endif;
        pr(newline);
        pr('PAUSING: press RETURN key to continue, CTRL-C to abort.');
        pr(newline);
        if vedediting then
            vedscr_read_ascii() ->;
        else
            charin() ->;
        endif;
        'CONTINUING' =>
        pr(newline);
    enddefine;

    ;;; now a program that endlessly prints things, but polls for
    ;;; input
    lvars x;
    for x from 1 to limit do
        spr(x);
        ;;; check for input every 10 characters
        if x mod 10 == 0
        and ((vedediting and vedscr_input_waiting())
            or sys_input_waiting(popdevin))
        then
            ;;; read the character
            if vedediting then
                vedscr_read_ascii() ->;
            else
                charin() ->;
            endif;
            pause();
        endif;
    endfor;
enddefine;


;;; Warning to make this pause if run outside of VED press RETURN
;;; (It must be a "break" character.)
silly_program_2(3000);

-------------------------------------------------------------------

I hope this all makes sense. There may be more elegant methods.
For example you could use sys_timer to set up a procedure to check
every N seconds whether something has been typed at the keyboard and
if so to set a flag that makes the current program pause.

Alternatively you could use consproc to create a "lightweight"
process and runproc to run it, and then the pausing procedure can
run suspend to make it pause.

Aaron
---
--
Aaron Sloman,
School of Computer Science, The University of Birmingham, B15 2TT, England
EMAIL   A.Sloman@cs.bham.ac.uk  OR A.Sloman@bham.ac.uk
Phone: +44-(0)21-414-4775       Fax:   +44-(0)21-414-4281