View source with raw comments or as raw
    1/*  Part of XPCE --- The SWI-Prolog GUI toolkit
    2
    3    Author:        Jan Wielemaker and Anjo Anjewierden
    4    E-mail:        jan@swi.psy.uva.nl
    5    WWW:           http://www.swi.psy.uva.nl/projects/xpce/
    6    Copyright (c)  2002-2011, University of Amsterdam
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module('Xserver',
   36          [ ensure_x_server/2           % +Display, +Depth
   37          ]).   38:- use_module(library(pce)).
 ensure_x_server(+Display, +Depth)
Ensure the existence of a graphics environment for XPCE. This library uses the `head-less' server Xvfb if there is no X-server in the environment.

Currently this library deals with Windows (where no explicit server is required) and Xfree using the Xfree socket naming conventions. Please send platform-specific code to info@swi-prolog.org

   52ensure_x_server(_Display, _) :-
   53    current_prolog_flag(windows, true),
   54    !. % we have a display
   55ensure_x_server(_Display, _) :-
   56    getenv('DISPLAY', _),
   57    !.               % Existing X11 display
   58ensure_x_server(Display, _) :-
   59    atom_concat('/tmp/.X11-unix/X', Display, Socket),
   60    catch(open(Socket, read, In), _, fail),
   61    close(In),
   62    export_environment(Display).
   63ensure_x_server(Display, Depth) :-
   64    print_message(informational, pce(xserver(start_Xvfb(Display, Depth)))),
   65    xauthority(Display, Auth),
   66    xlog(Display, Log),
   67    mcookie(Cookie),
   68    ignore(catch(delete_file(Auth), _, true)),
   69    sformat(Cmd1, '/usr/bin/X11/xauth -f ~w add :~w . ~s > ~w 2>&1',
   70            [ Auth, Display, Cookie, Log ]),
   71    shell(Cmd1),
   72    sformat(Cmd2, 'nohup /usr/bin/X11/Xvfb :~w -auth ~w -screen 0 640x480x~w >> ~w &',
   73            [ Display, Auth, Depth, Log ]),
   74    shell(Cmd2),
   75    sleep(5),
   76    export_environment(Display).
   77
   78xauthority(Display, Auth) :-
   79    atomic_list_concat(['/tmp/.X', Display, 'Authority'], Auth).
   80xlog(Display, Log) :-
   81    atomic_list_concat(['/tmp/.X', Display, 'Log'], Log).
   82
   83mcookie(Cookie) :-
   84    open(pipe(mcookie), read, Stream),
   85    read_line_to_codes(Stream, Cookie).
   86
   87export_environment(Display) :-
   88    xauthority(Display, Auth),
   89    atom_concat(':', Display, Address),
   90    setenv('DISPLAY', Address),
   91    setenv('XAUTHORITY', Auth).
   92
   93                 /*******************************
   94                 *            MESSAGES          *
   95                 *******************************/
   96:- multifile
   97    prolog:message/3.   98
   99%       Catch messages.  sgml/4 is generated by the SGML2PL binding.
  100
  101prolog:message(pce(xserver(start_Xvfb(Display, Depth)))) -->
  102    [ 'XPCE: Starting Xvfb on display ~w using depth ~w'-[Display, Depth]
  103    ]