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