View source with formatted comments or as raw
    1/*  Part of XPCE --- The SWI-Prolog GUI toolkit
    2
    3    Author:        Jan Wielemaker and Anjo Anjewierden
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org/packages/xpce/
    6    Copyright (c)  1995-2013, University of Amsterdam
    7                              VU University Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(pce_main,
   37          [ pce_loop/2,                 % :Goal, +Argv
   38            pce_loop/1,                 % :Goal
   39            pce_main_loop/1,            % :Goal
   40            dispatch_for_frames/1       % +FrameList
   41          ]).   42
   43:- meta_predicate
   44    pce_loop(:),
   45    pce_loop(:, +),
   46    pce_main_loop(:).   47
   48:- use_module(library(pce)).   49:- use_module(library(pce_util)).   50:- require([ append/3
   51           , call/2
   52           , ignore/1
   53           , unix/1
   54           , chain_list/2
   55           ]).   56
   57%!  pce_main_loop(+Goal)
   58%
   59%   Simple XPCE runtime toplevel loop.  This goal extracts the command
   60%   line arguments, calls `call(Goal, CmdLineArgs)' and waits for all
   61%   frames created by this call to be invisible.  Then it will halt/0.
   62
   63pce_main_loop(Goal) :-
   64    setup_runtime,
   65    current_prolog_flag(argv, Argv),
   66    pce_loop(Goal, Argv),
   67    halt.
   68
   69%!  pce_loop(+Goal).
   70%!  pce_loop(+Goal, +Argv:list).
   71%
   72%   Runs `Goal', finds all toplevel frames created and then dispatches
   73%   events untill the last frame is destroyed.
   74
   75pce_loop(Goal) :-
   76    pce_loop(Goal, []).
   77pce_loop(Goal, Argv) :-
   78    get(@display?frames, find_all, @arg1?kind == toplevel, FramesOld),
   79    call(Goal, Argv),
   80    get(@display?frames, find_all, @arg1?kind == toplevel, FramesNew),
   81    get(FramesNew, copy, FrameChain),
   82    send(FrameChain, subtract, FramesOld),
   83    chain_list(FrameChain, Frames),
   84    dispatch_for_frames(Frames).
   85
   86dispatch_for_frames([]) :- !.
   87dispatch_for_frames(Frames) :-
   88    (   catch(send(@display, dispatch), E,
   89              (   message_to_string(E, Msg),
   90                  send(@display, inform, Msg),
   91                  (   E == '$aborted'
   92                  ->  throw(E)
   93                  ;   true
   94                  )
   95              ))
   96    ->  true
   97    ;   true
   98    ),
   99    existing_frames(Frames, Existing),
  100    dispatch_for_frames(Existing).
  101
  102existing_frames([], []).
  103existing_frames([H|T0], [H|T]) :-
  104    object(H),
  105    send(H, instance_of, frame),
  106    get(H, status, Status),
  107    Status \== unmapped,
  108    !,
  109    existing_frames(T0, T).
  110existing_frames([_|T0], T) :-
  111    existing_frames(T0, T).
  112
  113setup_runtime :-
  114    (   get(@pce, is_runtime_system, @on)
  115    ->  true
  116    ;   send(@pce, trap_errors, @off)
  117    ),
  118    catch(set_prolog_flag(debug_on_error, false), _, true)