/*  Part of XPCE --- The SWI-Prolog GUI toolkit

    Author:        Jan Wielemaker and Anjo Anjewierden
    E-mail:        jan@swi-prolog.org
    WWW:           http://www.swi-prolog.nl/projects/xpce/
    Copyright (c)  2011-2025, University of Amsterdam
                              VU University Amsterdam
                              SWI-Prolog Solutions b.v.
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in
       the documentation and/or other materials provided with the
       distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/

:- module(link_xpce, []).
:- if(\+current_prolog_flag(xpce,false)).
:- set_prolog_flag(generate_debug_info, true).

/** <module> Associate XPCE with SWI-Prolog

This  file  initialises  XPCE,  the  SWI-Prolog   native  GUI.  XPCE  is
initialised if one of these conditions is true.

  - The Prolog flag `xpce` is `true`
  - There is a GUI.  See has_display/0 below.  In this case, XPCE
    is loaded if the Prolog flag `xpce` is `true`.  Otherwise it is
    setup to be loaded lazily if one of the GUI IDE tools is invoked.

The source-location of this file  is packages/xpce/swipl/swipl-rc. It is
installed as <plbase>/<exe-base>.rc, where   <exe-base> is ``swipl-win``
to  associate  with  the  SWI-Prolog  gui  application  on  Windows  and
``swipl`` on Unix/X11 platforms.
*/

:- multifile
    user:file_search_path/2.
:- dynamic
    pce_setup_done/1.

has_xpce_library :-
    pce_setup_done(How),
    How \== false.

user:file_search_path(pce, swi(xpce)) :-
    has_xpce_library.
user:file_search_path(library, pce('prolog/lib')) :-
    has_xpce_library.
user:file_search_path(foreign, pce(ArchLib)) :-
    has_xpce_library,
    current_prolog_flag(arch, Arch),
    atom_concat('lib/', Arch, ArchLib).

%!  gui_setup_
%
%   We have a GUI. Prepare it. This loads  hooks that may enable the gui
%   and set the editor to PceEmacs if it is still `default`.

gui_setup_ :-
    current_prolog_flag(gui, true),
    !.
gui_setup_ :-
    create_prolog_flag(gui, true, []),
    editor_setup,
    use_module(user:library(swi_hooks)).

editor_setup :-
    current_prolog_flag(editor, default),
    !,
    set_prolog_flag(editor, pce_emacs).
editor_setup.

%!  has_display is semidet.
%
%   True when we can run graphics.  Graphics are always available on
%   Windows or MacOS (although they may   appear on the main console
%   when invoked from a  remote  login).   On  Linux,  we can detect
%   ``DISPLAY`` (X11) or  ``WAYLAND_DISPLAY``   (Wayland).  We  also
%   assume we can run graphics if the ``SDL_VIDEO_DRIVER`` is set to
%   `dummy`, implying we try to run _headless_.

has_display :- current_prolog_flag(windows, true), !.
has_display :- current_prolog_flag(apple, true), !.
has_display :- getenv('WAYLAND_DISPLAY', D), D \== '', !.
has_display :- getenv('DISPLAY', D), D \== '', !.
has_display :- getenv('SDL_VIDEODRIVER', dummy).
has_display :- current_prolog_flag('SDL_VIDEODRIVER', dummy).

%!  pce_setup_ is det.
%
%   Setup xpce.  There are these cases:
%
%     - The flag `xpce` is `false`: do not setup xpce.
%     - The flag `xpce` is `true`: load xpce and set it up.
%     - The flag `xpce` is not present.  Now
%       - If we seem to have a gui, prepare xpce to be loaded lazily
%       - If there is no gui, do not setup xpce.

pce_setup_ :-
    pce_setup_done(_),
    !.
pce_setup_ :-
    (   current_prolog_flag(xpce, true)
    ->  Preload = true,
        How = true
    ;   has_display,                % no explicit xpce flag
        Preload = false,
        How = lazy
    ),
    !,
    set_prolog_flag(xpce, true),
    asserta(pce_setup_done(How)),
    reload_library_index,
    gui_setup_,
    (   Preload == true
    ->  ensure_loaded(user:library(pce))
    ;   true
    ).
pce_setup_ :-                       % Implicit and no display
    asserta(pce_setup_done(false)).

:- initialization pce_setup_.
:- endif. % \+ current_prolog_flag(xpce,false)
