1/*
2
3Copyright (c) 1991  X Consortium
4
5Permission is hereby granted, free of charge, to any person obtaining
6a copy of this software and associated documentation files (the
7"Software"), to deal in the Software without restriction, including
8without limitation the rights to use, copy, modify, merge, publish,
9distribute, sublicense, and/or sell copies of the Software, and to
10permit persons to whom the Software is furnished to do so, subject to
11the following conditions:
12
13The above copyright notice and this permission notice shall be included
14in all copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
20OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22OTHER DEALINGS IN THE SOFTWARE.
23
24Except as contained in this notice, the name of the X Consortium shall
25not be used in advertising or otherwise to promote the sale, use or
26other dealings in this Software without prior written authorization
27from the X Consortium.
28
29*/
30
31#ifdef HAVE_CONFIG_H
32# include "config.h"
33#endif
34
35#include <X11/Intrinsic.h>
36#include <X11/StringDefs.h>
37#include <X11/Shell.h>
38#include "Eyes.h"
39#include <stdio.h>
40#include <stdlib.h>
41#include "eyes.bit"
42#include "eyesmask.bit"
43
44/* Exit with message describing command line format */
45
46static void _X_NORETURN
47usage(int exitval)
48{
49    fprintf(stderr,
50	    "usage: xeyes [-display [{host}]:{vs}]\n"
51	    "             [-geometry [{width}][x{height}][{+-}{xoff}[{+-}{yoff}]]]\n"
52	    "             [-fg {color}] [-bg {color}] [-bd {color}] [-bw {pixels}]\n"
53	    "             [-shape | +shape] [-outline {color}] [-center {color}]\n"
54	    "             [-backing {backing-store}] [-distance]\n"
55	    "             [-biblicallyAccurate]\n"
56#ifdef XRENDER
57	    "             [-render | +render]\n"
58#endif
59#ifdef PRESENT
60	    "             [-present | +present]\n"
61#endif
62            "       xeyes -help\n"
63            "       xeyes -version\n");
64    exit(exitval);
65}
66
67/* Command line options table.  Only resources are entered here...there is a
68   pass over the remaining options after XtParseCommand is let loose. */
69
70static XrmOptionDescRec options[] = {
71{(char *)"-outline",	(char *)"*eyes.outline",	XrmoptionSepArg,	NULL},
72{(char *)"-center",	(char *)"*eyes.center",		XrmoptionSepArg,	NULL},
73{(char *)"-backing",	(char *)"*eyes.backingStore",	XrmoptionSepArg,	NULL},
74{(char *)"-shape",	(char *)"*eyes.shapeWindow",	XrmoptionNoArg,		(char *)"TRUE"},
75{(char *)"+shape",	(char *)"*eyes.shapeWindow",	XrmoptionNoArg,		(char *)"FALSE"},
76#ifdef XRENDER
77{(char *)"-render",	(char *)"*eyes.render",		XrmoptionNoArg,		(char *)"TRUE"},
78{(char *)"+render",	(char *)"*eyes.render",		XrmoptionNoArg,		(char *)"FALSE"},
79#endif
80#ifdef PRESENT
81{(char *)"-present",	(char *)"*eyes.present",	XrmoptionNoArg,		(char *)"TRUE"},
82{(char *)"+present",	(char *)"*eyes.present",	XrmoptionNoArg,		(char *)"FALSE"},
83#endif
84{(char *)"-distance",	(char *)"*eyes.distance",	XrmoptionNoArg,		(char *)"TRUE"},
85{(char *)"-biblicallyAccurate",	(char *)"*eyes.biblicallyAccurate",	XrmoptionNoArg,		(char *)"TRUE"},
86};
87
88static Atom wm_delete_window;
89
90/*ARGSUSED*/
91static void
92quit(Widget w, XEvent *event, String *params, Cardinal *num_params)
93{
94    if (event->type == ClientMessage &&
95	event->xclient.data.l[0] != wm_delete_window) {
96	XBell(XtDisplay(w), 0);
97    } else {
98	XtDestroyApplicationContext(XtWidgetToApplicationContext(w));
99	exit(0);
100    }
101}
102
103static XtActionsRec actions[] = {
104    {(char *) "quit",	quit}
105};
106
107int
108main(int argc, char **argv)
109{
110    XtAppContext app_context;
111    Widget toplevel;
112    Arg arg[2];
113    Cardinal i;
114
115    XtSetLanguageProc(NULL, (XtLanguageProc) NULL, NULL);
116
117    /* Handle args that don't require opening a display */
118    for (int n = 1; n < argc; n++) {
119	const char *argn = argv[n];
120	/* accept single or double dash for -help & -version */
121	if (argn[0] == '-' && argn[1] == '-') {
122	    argn++;
123	}
124	if (strcmp(argn, "-help") == 0) {
125	    usage(0);
126	}
127	if (strcmp(argn, "-version") == 0) {
128	    puts(PACKAGE_STRING);
129	    exit(0);
130	}
131    }
132
133    toplevel = XtAppInitialize(&app_context, "XEyes",
134			       options, XtNumber(options), &argc, argv,
135			       NULL, arg, (Cardinal) 0);
136
137    if (argc != 1) {
138	fputs("Unknown argument(s):", stderr);
139	for (int n = 1; n < argc; n++) {
140	    if ((n < (argc -1)) || (argv[n][0] == '-')) {
141		fprintf(stderr, " %s", argv[n]);
142	    }
143	}
144	fputs("\n\n", stderr);
145	usage(1);
146    }
147
148    wm_delete_window = XInternAtom(XtDisplay(toplevel), "WM_DELETE_WINDOW",
149				   False);
150    XtAppAddActions(app_context, actions, XtNumber(actions));
151    XtOverrideTranslations
152	(toplevel, XtParseTranslationTable ("<Message>WM_PROTOCOLS: quit()"));
153
154    i = 0;
155    XtSetArg (arg[i], XtNiconPixmap,
156	      XCreateBitmapFromData (XtDisplay(toplevel),
157				     XtScreen(toplevel)->root,
158				     (char *)eyes_bits, eyes_width, eyes_height));
159    i++;
160    XtSetArg (arg[i], XtNiconMask,
161	      XCreateBitmapFromData (XtDisplay(toplevel),
162				     XtScreen(toplevel)->root,
163				     (char *)eyesmask_bits,
164				     eyesmask_width, eyesmask_height));
165    i++;
166    XtSetValues (toplevel, arg, i);
167
168    (void) XtCreateManagedWidget ("eyes", eyesWidgetClass, toplevel, NULL, 0);
169    XtRealizeWidget (toplevel);
170    (void) XSetWMProtocols (XtDisplay(toplevel), XtWindow(toplevel),
171                            &wm_delete_window, 1);
172    XtAppMainLoop(app_context);
173
174    return 0;
175}
176