main.c revision 1b5d61b8
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 <X11/fonts/fontstruct.h>
99#include <X11/fonts/libxfont2.h>
100#include "opaque.h"
101#include "servermd.h"
102#include "hotplug.h"
103#include "site.h"
104#include "dixfont.h"
105#include "extnsionst.h"
106#include "privates.h"
107#include "registry.h"
108#include "client.h"
109#include "exevents.h"
110#ifdef PANORAMIX
111#include "panoramiXsrv.h"
112#else
113#include "dixevents.h"          /* InitEvents() */
114#endif
115
116#ifdef DPMSExtension
117#include <X11/extensions/dpmsconst.h>
118#include "dpmsproc.h"
119#endif
120
121extern void Dispatch(void);
122
123CallbackListPtr RootWindowFinalizeCallback = NULL;
124
125int
126dix_main(int argc, char *argv[], char *envp[])
127{
128    int i;
129    HWEventQueueType alwaysCheckForInput[2];
130
131    display = "0";
132
133    InitRegions();
134
135    CheckUserParameters(argc, argv, envp);
136
137    CheckUserAuthorization();
138
139    InitConnectionLimits();
140
141    ProcessCommandLine(argc, argv);
142
143    alwaysCheckForInput[0] = 0;
144    alwaysCheckForInput[1] = 1;
145    while (1) {
146        serverGeneration++;
147        ScreenSaverTime = defaultScreenSaverTime;
148        ScreenSaverInterval = defaultScreenSaverInterval;
149        ScreenSaverBlanking = defaultScreenSaverBlanking;
150        ScreenSaverAllowExposures = defaultScreenSaverAllowExposures;
151
152        InitBlockAndWakeupHandlers();
153        /* Perform any operating system dependent initializations you'd like */
154        OsInit();
155        if (serverGeneration == 1) {
156            CreateWellKnownSockets();
157            for (i = 1; i < LimitClients; i++)
158                clients[i] = NullClient;
159            serverClient = calloc(sizeof(ClientRec), 1);
160            if (!serverClient)
161                FatalError("couldn't create server client");
162            InitClient(serverClient, 0, (void *) NULL);
163        }
164        else
165            ResetWellKnownSockets();
166        clients[0] = serverClient;
167        currentMaxClients = 1;
168
169        /* clear any existing selections */
170        InitSelections();
171
172        /* Initialize privates before first allocation */
173        dixResetPrivates();
174
175        /* Initialize server client devPrivates, to be reallocated as
176         * more client privates are registered
177         */
178        if (!dixAllocatePrivates(&serverClient->devPrivates, PRIVATE_CLIENT))
179            FatalError("failed to create server client privates");
180
181        if (!InitClientResources(serverClient)) /* for root resources */
182            FatalError("couldn't init server resources");
183
184        SetInputCheck(&alwaysCheckForInput[0], &alwaysCheckForInput[1]);
185        screenInfo.numScreens = 0;
186
187        InitAtoms();
188        InitEvents();
189        xfont2_init_glyph_caching();
190        dixResetRegistry();
191        InitFonts();
192        InitCallbackManager();
193        InitOutput(&screenInfo, argc, argv);
194
195        if (screenInfo.numScreens < 1)
196            FatalError("no screens found");
197        InitExtensions(argc, argv);
198
199        for (i = 0; i < screenInfo.numGPUScreens; i++) {
200            ScreenPtr pScreen = screenInfo.gpuscreens[i];
201            if (!CreateScratchPixmapsForScreen(pScreen))
202                FatalError("failed to create scratch pixmaps");
203            if (pScreen->CreateScreenResources &&
204                !(*pScreen->CreateScreenResources) (pScreen))
205                FatalError("failed to create screen resources");
206        }
207
208        for (i = 0; i < screenInfo.numScreens; i++) {
209            ScreenPtr pScreen = screenInfo.screens[i];
210
211            if (!CreateScratchPixmapsForScreen(pScreen))
212                FatalError("failed to create scratch pixmaps");
213            if (pScreen->CreateScreenResources &&
214                !(*pScreen->CreateScreenResources) (pScreen))
215                FatalError("failed to create screen resources");
216            if (!CreateGCperDepth(i))
217                FatalError("failed to create scratch GCs");
218            if (!CreateDefaultStipple(i))
219                FatalError("failed to create default stipple");
220            if (!CreateRootWindow(pScreen))
221                FatalError("failed to create root window");
222            CallCallbacks(&RootWindowFinalizeCallback, pScreen);
223        }
224
225        if (SetDefaultFontPath(defaultFontPath) != Success) {
226            ErrorF("[dix] failed to set default font path '%s'",
227                   defaultFontPath);
228        }
229        if (!SetDefaultFont(defaultTextFont)) {
230            FatalError("could not open default font '%s'", defaultTextFont);
231        }
232
233        if (!(rootCursor = CreateRootCursor(NULL, 0))) {
234            FatalError("could not open default cursor font '%s'",
235                       defaultCursorFont);
236        }
237
238#ifdef PANORAMIX
239        /*
240         * Consolidate window and colourmap information for each screen
241         */
242        if (!noPanoramiXExtension)
243            PanoramiXConsolidate();
244#endif
245
246        for (i = 0; i < screenInfo.numScreens; i++)
247            InitRootWindow(screenInfo.screens[i]->root);
248
249        InitCoreDevices();
250        InitInput(argc, argv);
251        InitAndStartDevices();
252        ReserveClientIds(serverClient);
253
254        dixSaveScreens(serverClient, SCREEN_SAVER_FORCER, ScreenSaverReset);
255
256        dixCloseRegistry();
257
258#ifdef PANORAMIX
259        if (!noPanoramiXExtension) {
260            if (!PanoramiXCreateConnectionBlock()) {
261                FatalError("could not create connection block info");
262            }
263        }
264        else
265#endif
266        {
267            if (!CreateConnectionBlock()) {
268                FatalError("could not create connection block info");
269            }
270        }
271
272        NotifyParentProcess();
273
274        InputThreadInit();
275
276        Dispatch();
277
278        UndisplayDevices();
279        DisableAllDevices();
280
281        /* Now free up whatever must be freed */
282        if (screenIsSaved == SCREEN_SAVER_ON)
283            dixSaveScreens(serverClient, SCREEN_SAVER_OFF, ScreenSaverReset);
284        FreeScreenSaverTimer();
285        CloseDownExtensions();
286
287#ifdef PANORAMIX
288        {
289            Bool remember_it = noPanoramiXExtension;
290
291            noPanoramiXExtension = TRUE;
292            FreeAllResources();
293            noPanoramiXExtension = remember_it;
294        }
295#else
296        FreeAllResources();
297#endif
298
299        CloseInput();
300
301        InputThreadFini();
302
303        for (i = 0; i < screenInfo.numScreens; i++)
304            screenInfo.screens[i]->root = NullWindow;
305
306        CloseDownDevices();
307
308        CloseDownEvents();
309
310        for (i = screenInfo.numGPUScreens - 1; i >= 0; i--) {
311            ScreenPtr pScreen = screenInfo.gpuscreens[i];
312            FreeScratchPixmapsForScreen(pScreen);
313            dixFreeScreenSpecificPrivates(pScreen);
314            (*pScreen->CloseScreen) (pScreen);
315            dixFreePrivates(pScreen->devPrivates, PRIVATE_SCREEN);
316            free(pScreen);
317            screenInfo.numGPUScreens = i;
318        }
319
320        for (i = screenInfo.numScreens - 1; i >= 0; i--) {
321            FreeScratchPixmapsForScreen(screenInfo.screens[i]);
322            FreeGCperDepth(i);
323            FreeDefaultStipple(i);
324            dixFreeScreenSpecificPrivates(screenInfo.screens[i]);
325            (*screenInfo.screens[i]->CloseScreen) (screenInfo.screens[i]);
326            dixFreePrivates(screenInfo.screens[i]->devPrivates, PRIVATE_SCREEN);
327            free(screenInfo.screens[i]);
328            screenInfo.numScreens = i;
329        }
330
331        ReleaseClientIds(serverClient);
332        dixFreePrivates(serverClient->devPrivates, PRIVATE_CLIENT);
333        serverClient->devPrivates = NULL;
334
335	dixFreeRegistry();
336
337        FreeFonts();
338
339        FreeAllAtoms();
340
341        FreeAuditTimer();
342
343        DeleteCallbackManager();
344
345        if (dispatchException & DE_TERMINATE) {
346            CloseWellKnownConnections();
347        }
348
349        OsCleanup((dispatchException & DE_TERMINATE) != 0);
350
351        if (dispatchException & DE_TERMINATE) {
352            ddxGiveUp(EXIT_NO_ERROR);
353            break;
354        }
355
356        free(ConnectionInfo);
357        ConnectionInfo = NULL;
358    }
359    return 0;
360}
361