main.c revision 9ace9065
1/***********************************************************
2
3Copyright 1987, 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 in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21Except as contained in this notice, the name of The Open Group shall not be
22used in advertising or otherwise to promote the sale, use or other dealings
23in this Software without prior written authorization from The Open Group.
24
25
26Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
27
28                        All Rights Reserved
29
30Permission to use, copy, modify, and distribute this software and its
31documentation for any purpose and without fee is hereby granted,
32provided that the above copyright notice appear in all copies and that
33both that copyright notice and this permission notice appear in
34supporting documentation, and that the name of Digital not be
35used in advertising or publicity pertaining to distribution of the
36software without specific, written prior permission.
37
38DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
39ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
40DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
41ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
42WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
43ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
44SOFTWARE.
45
46******************************************************************/
47
48/* The panoramix components contained the following notice */
49/*****************************************************************
50
51Copyright (c) 1991, 1997 Digital Equipment Corporation, Maynard, Massachusetts.
52
53Permission is hereby granted, free of charge, to any person obtaining a copy
54of this software and associated documentation files (the "Software"), to deal
55in the Software without restriction, including without limitation the rights
56to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
57copies of the Software.
58
59The above copyright notice and this permission notice shall be included in
60all copies or substantial portions of the Software.
61
62THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
63IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
64FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
65DIGITAL EQUIPMENT CORPORATION BE LIABLE FOR ANY CLAIM, DAMAGES, INCLUDING,
66BUT NOT LIMITED TO CONSEQUENTIAL OR INCIDENTAL DAMAGES, OR OTHER LIABILITY,
67WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
68IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
69
70Except as contained in this notice, the name of Digital Equipment Corporation
71shall not be used in advertising or otherwise to promote the sale, use or other
72dealings in this Software without prior written authorization from Digital
73Equipment Corporation.
74
75******************************************************************/
76
77#ifdef HAVE_DIX_CONFIG_H
78#include <dix-config.h>
79#include <version-config.h>
80#endif
81
82#include <X11/X.h>
83#include <X11/Xos.h>   /* for unistd.h  */
84#include <X11/Xproto.h>
85#include <pixman.h>
86#include "scrnintstr.h"
87#include "misc.h"
88#include "os.h"
89#include "windowstr.h"
90#include "resource.h"
91#include "dixstruct.h"
92#include "gcstruct.h"
93#include "extension.h"
94#include "colormap.h"
95#include "colormapst.h"
96#include "cursorstr.h"
97#include "selection.h"
98#include <X11/fonts/font.h>
99#include "opaque.h"
100#include "servermd.h"
101#include "hotplug.h"
102#include "site.h"
103#include "dixfont.h"
104#include "extnsionst.h"
105#include "privates.h"
106#include "registry.h"
107#ifdef PANORAMIX
108#include "panoramiXsrv.h"
109#else
110#include "dixevents.h"		/* InitEvents() */
111#endif
112
113#ifdef DPMSExtension
114#include <X11/extensions/dpmsconst.h>
115#include "dpmsproc.h"
116#endif
117
118extern void Dispatch(void);
119
120#ifdef XQUARTZ
121#include <pthread.h>
122
123BOOL serverRunning = FALSE;
124pthread_mutex_t serverRunningMutex = PTHREAD_MUTEX_INITIALIZER;
125pthread_cond_t serverRunningCond = PTHREAD_COND_INITIALIZER;
126
127int dix_main(int argc, char *argv[], char *envp[]);
128
129int dix_main(int argc, char *argv[], char *envp[])
130#else
131int main(int argc, char *argv[], char *envp[])
132#endif
133{
134    int		i;
135    HWEventQueueType	alwaysCheckForInput[2];
136
137    display = "0";
138
139    InitRegions();
140
141    pixman_disable_out_of_bounds_workaround();
142
143    CheckUserParameters(argc, argv, envp);
144
145    CheckUserAuthorization();
146
147    InitConnectionLimits();
148
149    ProcessCommandLine(argc, argv);
150
151    alwaysCheckForInput[0] = 0;
152    alwaysCheckForInput[1] = 1;
153    while(1)
154    {
155	serverGeneration++;
156	ScreenSaverTime = defaultScreenSaverTime;
157	ScreenSaverInterval = defaultScreenSaverInterval;
158	ScreenSaverBlanking = defaultScreenSaverBlanking;
159	ScreenSaverAllowExposures = defaultScreenSaverAllowExposures;
160#ifdef DPMSExtension
161	DPMSStandbyTime = DPMSSuspendTime = DPMSOffTime = ScreenSaverTime;
162	DPMSEnabled = TRUE;
163	DPMSPowerLevel = 0;
164#endif
165	InitBlockAndWakeupHandlers();
166	/* Perform any operating system dependent initializations you'd like */
167	OsInit();
168	if(serverGeneration == 1)
169	{
170	    CreateWellKnownSockets();
171	    for (i=1; i<MAXCLIENTS; i++)
172		clients[i] = NullClient;
173	    serverClient = calloc(sizeof(ClientRec), 1);
174	    if (!serverClient)
175		FatalError("couldn't create server client");
176	    InitClient(serverClient, 0, (pointer)NULL);
177	}
178	else
179	    ResetWellKnownSockets ();
180	clients[0] = serverClient;
181	currentMaxClients = 1;
182
183	/* Initialize privates before first allocation */
184	dixResetPrivates();
185
186	/* Initialize server client devPrivates, to be reallocated as
187	 * more client privates are registered
188	 */
189	if (!dixAllocatePrivates(&serverClient->devPrivates, PRIVATE_CLIENT))
190	    FatalError("failed to create server client privates");
191
192	if (!InitClientResources(serverClient))      /* for root resources */
193	    FatalError("couldn't init server resources");
194
195	SetInputCheck(&alwaysCheckForInput[0], &alwaysCheckForInput[1]);
196	screenInfo.numScreens = 0;
197
198	InitAtoms();
199	InitEvents();
200	InitSelections();
201	InitGlyphCaching();
202	dixResetRegistry();
203	ResetFontPrivateIndex();
204	InitCallbackManager();
205	InitOutput(&screenInfo, argc, argv);
206
207	if (screenInfo.numScreens < 1)
208	    FatalError("no screens found");
209	InitExtensions(argc, argv);
210
211	for (i = 0; i < screenInfo.numScreens; i++)
212	{
213	    ScreenPtr pScreen = screenInfo.screens[i];
214	    if (!CreateScratchPixmapsForScreen(i))
215		FatalError("failed to create scratch pixmaps");
216	    if (pScreen->CreateScreenResources &&
217		!(*pScreen->CreateScreenResources)(pScreen))
218		FatalError("failed to create screen resources");
219	    if (!CreateGCperDepth(i))
220		FatalError("failed to create scratch GCs");
221	    if (!CreateDefaultStipple(i))
222		FatalError("failed to create default stipple");
223	    if (!CreateRootWindow(pScreen))
224		FatalError("failed to create root window");
225	}
226
227	InitFonts();
228	if (SetDefaultFontPath(defaultFontPath) != Success) {
229	    ErrorF("[dix] failed to set default font path '%s'", defaultFontPath);
230	}
231	if (!SetDefaultFont(defaultTextFont)) {
232	    FatalError("could not open default font '%s'", defaultTextFont);
233	}
234
235	if (!(rootCursor = CreateRootCursor(NULL, 0))) {
236	    FatalError("could not open default cursor font '%s'",
237		       defaultCursorFont);
238	}
239
240#ifdef DPMSExtension
241 	/* check all screens, looking for DPMS Capabilities */
242 	DPMSCapableFlag = DPMSSupported();
243	if (!DPMSCapableFlag)
244     	    DPMSEnabled = FALSE;
245#endif
246
247#ifdef PANORAMIX
248	/*
249	 * Consolidate window and colourmap information for each screen
250	 */
251	if (!noPanoramiXExtension)
252	    PanoramiXConsolidate();
253#endif
254
255	for (i = 0; i < screenInfo.numScreens; i++)
256	    InitRootWindow(screenInfo.screens[i]->root);
257
258        InitCoreDevices();
259	InitInput(argc, argv);
260	InitAndStartDevices();
261
262	dixSaveScreens(serverClient, SCREEN_SAVER_FORCER, ScreenSaverReset);
263
264#ifdef PANORAMIX
265	if (!noPanoramiXExtension) {
266	    if (!PanoramiXCreateConnectionBlock()) {
267		FatalError("could not create connection block info");
268	    }
269	} else
270#endif
271	{
272	    if (!CreateConnectionBlock()) {
273	    	FatalError("could not create connection block info");
274	    }
275	}
276
277#ifdef XQUARTZ
278	/* Let the other threads know the server is done with its init */
279	pthread_mutex_lock(&serverRunningMutex);
280	serverRunning = TRUE;
281	pthread_cond_broadcast(&serverRunningCond);
282	pthread_mutex_unlock(&serverRunningMutex);
283#endif
284
285	NotifyParentProcess();
286
287	Dispatch();
288
289#ifdef XQUARTZ
290	/* Let the other threads know the server is no longer running */
291	pthread_mutex_lock(&serverRunningMutex);
292	serverRunning = FALSE;
293	pthread_mutex_unlock(&serverRunningMutex);
294#endif
295
296	UndisplayDevices();
297
298	/* Now free up whatever must be freed */
299	if (screenIsSaved == SCREEN_SAVER_ON)
300	    dixSaveScreens(serverClient, SCREEN_SAVER_OFF, ScreenSaverReset);
301	FreeScreenSaverTimer();
302	CloseDownExtensions();
303
304#ifdef PANORAMIX
305	{
306	    Bool remember_it = noPanoramiXExtension;
307	    noPanoramiXExtension = TRUE;
308	    FreeAllResources();
309	    noPanoramiXExtension = remember_it;
310	}
311#else
312	FreeAllResources();
313#endif
314
315        CloseInput();
316
317	for (i = 0; i < screenInfo.numScreens; i++)
318	    screenInfo.screens[i]->root = NullWindow;
319	CloseDownDevices();
320	CloseDownEvents();
321
322	for (i = screenInfo.numScreens - 1; i >= 0; i--)
323	{
324	    FreeScratchPixmapsForScreen(i);
325	    FreeGCperDepth(i);
326	    FreeDefaultStipple(i);
327	    (* screenInfo.screens[i]->CloseScreen)(i, screenInfo.screens[i]);
328	    dixFreePrivates(screenInfo.screens[i]->devPrivates, PRIVATE_SCREEN);
329	    free(screenInfo.screens[i]);
330	    screenInfo.numScreens = i;
331	}
332
333	dixFreePrivates(serverClient->devPrivates, PRIVATE_CLIENT);
334	serverClient->devPrivates = NULL;
335
336	FreeFonts();
337
338	FreeAuditTimer();
339
340	if (dispatchException & DE_TERMINATE)
341	{
342	    CloseWellKnownConnections();
343	}
344
345	OsCleanup((dispatchException & DE_TERMINATE) != 0);
346
347	if (dispatchException & DE_TERMINATE)
348	{
349	    ddxGiveUp();
350	    break;
351	}
352
353	free(ConnectionInfo);
354	ConnectionInfo = NULL;
355    }
356    return 0;
357}
358
359