1/*
2
3Copyright 1993, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included
12in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20OTHER DEALINGS IN THE SOFTWARE.
21
22Except as contained in this notice, the name of The Open Group shall
23not be used in advertising or otherwise to promote the sale, use or
24other dealings in this Software without prior written authorization
25from The Open Group.
26
27*/
28
29#include <X11/Xos.h>
30#include <X11/Xlib.h>
31#include <X11/Xutil.h>
32#include <X11/cursorfont.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <stdarg.h>
36/*
37 * Other_stuff.h: Definitions of routines in other_stuff.
38 *
39 * Written by Mark Lillibridge.   Last updated 7/1/87
40 */
41
42#include "dsimple.h"
43
44/*
45 * Just_display: A group of routines designed to make the writing of simple
46 *               X11 applications which open a display but do not open
47 *               any windows much faster and easier.  Unless a routine says
48 *               otherwise, it may be assumed to require program_name, dpy,
49 *               and screen already defined on entry.
50 *
51 * Written by Mark Lillibridge.   Last updated 7/1/87
52 */
53
54/* This stuff is defined in the calling program by just_display.h */
55const char *program_name = "unknown_program";
56Display *dpy = NULL;
57int screen = 0;
58
59/*
60 * Get_Display_Name (argc, argv) Look for -display, -d, or host:dpy (obsolete)
61 * If found, remove it from command line.  Don't go past a lone -.
62 */
63static char *
64Get_Display_Name(int *pargc /* MODIFIED */, char **argv /* MODIFIED */)
65{
66    int argc = *pargc;
67    char **pargv = argv + 1;
68    char *displayname = NULL;
69    int i;
70
71    for (i = 1; i < argc; i++) {
72        char *arg = argv[i];
73
74        if (!strcmp(arg, "-display") || !strcmp(arg, "-d")) {
75            if (++i >= argc)
76                usage("-display requires an argument");
77
78            displayname = argv[i];
79            *pargc -= 2;
80            continue;
81        }
82        if (!strcmp(arg, "-")) {
83            while (i < argc)
84                *pargv++ = argv[i++];
85            break;
86        }
87        *pargv++ = arg;
88    }
89
90    *pargv = NULL;
91    return (displayname);
92}
93
94
95/*
96 * Open_Display: Routine to open a display with correct error handling.
97 *               Does not require dpy or screen defined on entry.
98 */
99static Display *
100Open_Display(char *display_name)
101{
102    Display *d;
103
104    d = XOpenDisplay(display_name);
105    if (d == NULL) {
106        fprintf(stderr, "%s:  unable to open display '%s'\n",
107                program_name, XDisplayName(display_name));
108        exit(1);
109    }
110
111    return (d);
112}
113
114
115/*
116 * Setup_Display_And_Screen: This routine opens up the correct display (i.e.,
117 *                           it calls Get_Display_Name) and then stores a
118 *                           pointer to it in dpy.  The default screen
119 *                           for this display is then stored in screen.
120 *                           Does not require dpy or screen defined.
121 */
122void
123Setup_Display_And_Screen(int *argc /* MODIFIED */, char **argv /* MODIFIED */)
124{
125    char *displayname = Get_Display_Name(argc, argv);
126
127    dpy = Open_Display(displayname);
128    screen = XDefaultScreen(dpy);
129}
130
131/*
132 * Close_Display: Close display
133 */
134void
135Close_Display(void)
136{
137    if (dpy == NULL)
138        return;
139
140    XCloseDisplay(dpy);
141    dpy = NULL;
142}
143
144/*
145 * Standard fatal error routine - call like printf but maximum of 7 arguments.
146 * Does not require dpy or screen defined.
147 */
148void
149Fatal_Error(const char *msg, ...)
150{
151    va_list args;
152
153    fflush(stdout);
154    fflush(stderr);
155    fprintf(stderr, "%s: error: ", program_name);
156    va_start(args, msg);
157    vfprintf(stderr, msg, args);
158    va_end(args);
159    fprintf(stderr, "\n");
160    Close_Display();
161    exit(EXIT_FAILURE);
162}
163