firefox: update to 135.0
[oi-userland.git] / components / x11 / winsysck / src / main.c
blob31f469126c13d012edf378945208900f829e6106
1 /*
2 * Copyright (c) 1990, 2015, Oracle and/or its affiliates. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
26 * winsysck - determine which window system protocols are available
28 * usage: winsysck [ -va ] [ -display display_name ] protocol [...]
30 * options:
31 * -a check every protocol listed, rather than stopping after
32 * the first one.
33 * -v print the first available protocol (or every available
34 * protocol, with the -a option) to stdout.
35 * -display display_name
36 * use the display display_name for X11 connections instead of
37 * the one specified by the $DISPLAY environment variable.
38 * -timeout seconds
39 * The timeout option sets the number of seconds before
40 * the winsysck will time out.
42 * protocols:
43 * x11
44 * The X11 protocol.
45 * news
46 * The NeWS protocol.
47 * x11news
48 * Both the X11 and the NeWS protocols, and both connect to
49 * the same server.
50 * sunview
51 * The SunView protocol.
53 * NOTE: Since news, x11news, and sunview have been unsupported for over
54 * a decade, we no longer actually check for them, but always return false.
56 * exit status:
57 * 0 if any listed protocols are available
58 * 1 if no listed protocols are available
59 * 2 usage error
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <sys/types.h>
66 #include <sys/time.h>
67 #include <signal.h>
68 #include <unistd.h>
70 static char* cmdname;
72 static void
73 usage (void) {
74 fprintf(stderr, "usage: %s %s\n\t%s\n", cmdname,
75 "[-va] [-display display_name] [-timeout seconds] protocol [...]",
76 "protocol can be one of: x11 news x11news sunview");
77 exit(2);
81 * Keep track of what protocols we're checking, and in what order.
84 #define PROTO_NONE 0
85 #define PROTO_X11 1
86 #define PROTO_NeWS 2
87 #define PROTO_X11NeWS 3
88 #define PROTO_SunView 4
89 #define N_PROTO 4
91 static int proto_list[N_PROTO], n_proto;
93 static void
94 AddProto(int proto)
96 int i;
97 for (i=0; i<N_PROTO; i++) {
98 if (proto_list[i]==proto) {
99 return;
102 proto_list[n_proto++]=proto;
106 * Keep track of the options specified.
108 static int verbose, showall, timeout = 0;
110 static char* display_name=NULL;
113 * Parse the options.
116 static void
117 Parse (int argc, char* argv[])
119 for (;argc > 1; argv++, argc--) {
120 if (argv[1][0] == '-') {
121 if (!strcmp(argv[1], "-display")) {
122 display_name=argv[2];
123 argv++;
124 argc--;
125 continue;
127 if (!strcmp(argv[1], "-timeout")) {
128 timeout = atoi(argv[2]);
129 argv++;
130 argc--;
131 continue;
133 for (; argv[1][1]; argv[1]++) {
134 switch (argv[1][1]) {
135 case 'a':
136 showall++;
137 break;
138 case 'v':
139 verbose++;
140 break;
141 default:
142 fprintf(stderr, "%s: -%c: unknown option\n",
143 cmdname,argv[1][1]);
144 usage();
148 else {
149 if (!strcmp(argv[1], "x11")) {
150 AddProto(PROTO_X11);
151 } else if (!strcmp(argv[1], "news")) {
152 AddProto(PROTO_NeWS);
153 } else if (!strcmp(argv[1], "x11news")) {
154 AddProto(PROTO_X11NeWS);
155 } else if (!strcmp(argv[1], "sunview")) {
156 AddProto(PROTO_SunView);
157 } else {
158 fprintf(stderr, "%s: %s: unknown protocol\n",
159 cmdname,argv[1]);
160 usage();
164 if (!n_proto) {
165 fprintf(stderr, "%s: no protocols specified\n",
166 cmdname);
167 usage();
171 /* Last remnants of x11-procs.c */
172 #include <X11/Xlib.h>
173 #include <X11/Xos.h>
175 static Display *display=NULL;
177 static void OnAlarm(int i)
179 fprintf(stderr, "winsysck timeout\n");
180 exit(1);
183 static int
184 ConnectToX (const char* display_name)
186 if(timeout > 0){
187 signal(SIGALRM, OnAlarm);
188 alarm(timeout);
190 display=XOpenDisplay(display_name);
191 if(timeout > 0)
192 alarm(0);
193 return((display != NULL));
196 static int
197 DisconnectFromX (void) {
198 XCloseDisplay(display);
199 return(1);
204 * Try connecting via the specified protocols.
206 static void
207 Probe(void)
209 int i, found=0;
210 for(i=0; (i < N_PROTO) && (proto_list[i] != PROTO_NONE); i++) {
211 switch (proto_list[i]) {
212 case PROTO_X11:
213 if (ConnectToX(display_name)) {
214 found++;
215 if (verbose) printf("%s\n","x11");
216 DisconnectFromX();
217 if (!showall) exit(0);
219 break;
220 case PROTO_NeWS:
221 case PROTO_X11NeWS:
222 case PROTO_SunView:
223 /* Don't even bother */
224 break;
225 default:
226 fprintf(stderr, "%s: WSGO: proto_list[%d]==%d\n",
227 cmdname,i,proto_list[i]);
230 exit(!found);
234 main (int argc, char* argv[])
236 cmdname=argv[0];
237 Parse(argc,argv);
238 Probe();
239 exit(0);