1/* Part of XPCE --- The SWI-Prolog GUI toolkit 2 3 Author: Jan Wielemaker and Anjo Anjewierden 4 E-mail: jan@swi-prolog.org 5 WWW: http://www.swi-prolog.nl/projects/xpce/ 6 Copyright (c) 2011-2025, University of Amsterdam 7 VU University Amsterdam 8 SWI-Prolog Solutions b.v. 9 All rights reserved. 10 11 Redistribution and use in source and binary forms, with or without 12 modification, are permitted provided that the following conditions 13 are met: 14 15 1. Redistributions of source code must retain the above copyright 16 notice, this list of conditions and the following disclaimer. 17 18 2. Redistributions in binary form must reproduce the above copyright 19 notice, this list of conditions and the following disclaimer in 20 the documentation and/or other materials provided with the 21 distribution. 22 23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 33 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 POSSIBILITY OF SUCH DAMAGE. 35*/ 36 37:- module(link_xpce, []). 38:- if(\+current_prolog_flag(xpce,false)). 39:- set_prolog_flag(generate_debug_info, true). 40 41/** <module> Associate XPCE with SWI-Prolog 42 43This file initialises XPCE, the SWI-Prolog native GUI. XPCE is 44initialised if one of these conditions is true. 45 46 - The Prolog flag `xpce` is `true` 47 - There is a GUI. See has_display/0 below. In this case, XPCE 48 is loaded if the Prolog flag `xpce` is `true`. Otherwise it is 49 setup to be loaded lazily if one of the GUI IDE tools is invoked. 50 51The source-location of this file is packages/xpce/swipl/swipl-rc. It is 52installed as <plbase>/<exe-base>.rc, where <exe-base> is ``swipl-win`` 53to associate with the SWI-Prolog gui application on Windows and 54``swipl`` on Unix/X11 platforms. 55*/ 56 57:- multifile 58 user:file_search_path/2. 59:- dynamic 60 pce_setup_done/1. 61 62has_xpce_library :- 63 pce_setup_done(How), 64 How \== false. 65 66user:file_search_path(pce, swi(xpce)) :- 67 has_xpce_library. 68user:file_search_path(library, pce('prolog/lib')) :- 69 has_xpce_library. 70user:file_search_path(foreign, pce(ArchLib)) :- 71 has_xpce_library, 72 current_prolog_flag(arch, Arch), 73 atom_concat('lib/', Arch, ArchLib). 74 75%! gui_setup_ 76% 77% We have a GUI. Prepare it. This loads hooks that may enable the gui 78% and set the editor to PceEmacs if it is still `default`. 79 80gui_setup_ :- 81 current_prolog_flag(gui, true), 82 !. 83gui_setup_ :- 84 create_prolog_flag(gui, true, []), 85 editor_setup, 86 use_module(user:library(swi_hooks)). 87 88editor_setup :- 89 current_prolog_flag(editor, default), 90 !, 91 set_prolog_flag(editor, pce_emacs). 92editor_setup. 93 94%! has_display is semidet. 95% 96% True when we can run graphics. Graphics are always available on 97% Windows or MacOS (although they may appear on the main console 98% when invoked from a remote login). On Linux, we can detect 99% ``DISPLAY`` (X11) or ``WAYLAND_DISPLAY`` (Wayland). We also 100% assume we can run graphics if the ``SDL_VIDEO_DRIVER`` is set to 101% `dummy`, implying we try to run _headless_. 102 103has_display :- current_prolog_flag(windows, true), !. 104has_display :- current_prolog_flag(apple, true), !. 105has_display :- getenv('WAYLAND_DISPLAY', D), D \== '', !. 106has_display :- getenv('DISPLAY', D), D \== '', !. 107has_display :- getenv('SDL_VIDEODRIVER', dummy). 108has_display :- current_prolog_flag('SDL_VIDEODRIVER', dummy). 109 110%! pce_setup_ is det. 111% 112% Setup xpce. There are these cases: 113% 114% - The flag `xpce` is `false`: do not setup xpce. 115% - The flag `xpce` is `true`: load xpce and set it up. 116% - The flag `xpce` is not present. Now 117% - If we seem to have a gui, prepare xpce to be loaded lazily 118% - If there is no gui, do not setup xpce. 119 120pce_setup_ :- 121 pce_setup_done(_), 122 !. 123pce_setup_ :- 124 ( current_prolog_flag(xpce, true) 125 -> Preload = true, 126 How = true 127 ; has_display, % no explicit xpce flag 128 Preload = false, 129 How = lazy 130 ), 131 !, 132 set_prolog_flag(xpce, true), 133 asserta(pce_setup_done(How)), 134 reload_library_index, 135 gui_setup_, 136 ( Preload == true 137 -> ensure_loaded(user:library(pce)) 138 ; true 139 ). 140pce_setup_ :- % Implicit and no display 141 asserta(pce_setup_done(false)). 142 143:- initialization pce_setup_. 144:- endif. % \+ current_prolog_flag(xpce,false)