1 /*
2  * Loosely based on code bearing the following copyright:
3  *
4  *   Copyright 1990,91 by Thomas Roell, Dinkelscherben, Germany.
5  */
6 /*
7  * Copyright (c) 1992-2003 by The XFree86 Project, Inc.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
22  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *
27  * Except as contained in this notice, the name of the copyright holder(s)
28  * and author(s) shall not be used in advertising or otherwise to promote
29  * the sale, use or other dealings in this Software without prior written
30  * authorization from the copyright holder(s) and author(s).
31  */
32 
33 #ifdef HAVE_XORG_CONFIG_H
34 #include <xorg-config.h>
35 #endif
36 
37 #include <stdlib.h>
38 #include <errno.h>
39 
40 #undef HAS_UTSNAME
41 #if !defined(WIN32)
42 #define HAS_UTSNAME 1
43 #include <sys/utsname.h>
44 #endif
45 
46 #include <X11/X.h>
47 #include <X11/Xmd.h>
48 #include <X11/Xproto.h>
49 #include <X11/Xatom.h>
50 #include "input.h"
51 #include "servermd.h"
52 #include "windowstr.h"
53 #include "scrnintstr.h"
54 #include "site.h"
55 #include "mi.h"
56 #include "dbus-core.h"
57 #include "systemd-logind.h"
58 
59 #include "loaderProcs.h"
60 #ifdef XFreeXDGA
61 #include "dgaproc.h"
62 #endif
63 
64 #define XF86_OS_PRIVS
65 #include "xf86.h"
66 #include "xf86Priv.h"
67 #include "xf86Config.h"
68 #include "xf86_OSlib.h"
69 #include "xf86cmap.h"
70 #include "xorgVersion.h"
71 #include "xf86Build.h"
72 #include "mipointer.h"
73 #include <X11/extensions/XI.h>
74 #include <X11/extensions/XIproto.h>
75 #include "xf86Extensions.h"
76 #include "xf86DDC.h"
77 #include "xf86Xinput.h"
78 #include "xf86InPriv.h"
79 #include "picturestr.h"
80 #include "randrstr.h"
81 #include "glxvndabi.h"
82 #include "xf86Bus.h"
83 #ifdef XSERVER_LIBPCIACCESS
84 #include "xf86VGAarbiter.h"
85 #endif
86 #include "globals.h"
87 #include "xserver-properties.h"
88 
89 #ifdef DPMSExtension
90 #include <X11/extensions/dpmsconst.h>
91 #include "dpmsproc.h"
92 #endif
93 #include <hotplug.h>
94 
95 #ifdef XF86PM
96 void (*xf86OSPMClose) (void) = NULL;
97 #endif
98 static Bool xorgHWOpenConsole = FALSE;
99 
100 /* Common pixmap formats */
101 
102 static PixmapFormatRec formats[MAXFORMATS] = {
103     {1, 1, BITMAP_SCANLINE_PAD},
104     {4, 8, BITMAP_SCANLINE_PAD},
105     {8, 8, BITMAP_SCANLINE_PAD},
106     {15, 16, BITMAP_SCANLINE_PAD},
107     {16, 16, BITMAP_SCANLINE_PAD},
108     {24, 32, BITMAP_SCANLINE_PAD},
109     {32, 32, BITMAP_SCANLINE_PAD},
110 };
111 
112 static int numFormats = 7;
113 static Bool formatsDone = FALSE;
114 
115 #ifndef OSNAME
116 #define OSNAME " unknown"
117 #endif
118 #ifndef OSVENDOR
119 #define OSVENDOR ""
120 #endif
121 #ifndef PRE_RELEASE
122 #define PRE_RELEASE XORG_VERSION_SNAP
123 #endif
124 
125 static void
xf86PrintBanner(void)126 xf86PrintBanner(void)
127 {
128 #if PRE_RELEASE
129     xf86ErrorFVerb(0, "\n"
130                    "This is a pre-release version of the X server from "
131                    XVENDORNAME ".\n" "It is not supported in any way.\n"
132                    "Bugs may be filed in the bugzilla at http://bugs.freedesktop.org/.\n"
133                    "Select the \"xorg\" product for bugs you find in this release.\n"
134                    "Before reporting bugs in pre-release versions please check the\n"
135                    "latest version in the X.Org Foundation git repository.\n"
136                    "See http://wiki.x.org/wiki/GitPage for git access instructions.\n");
137 #endif
138     xf86ErrorFVerb(0, "\nX.Org X Server %d.%d.%d",
139                    XORG_VERSION_MAJOR, XORG_VERSION_MINOR, XORG_VERSION_PATCH);
140 #if XORG_VERSION_SNAP > 0
141     xf86ErrorFVerb(0, ".%d", XORG_VERSION_SNAP);
142 #endif
143 
144 #if XORG_VERSION_SNAP >= 900
145     /* When the minor number is 99, that signifies that the we are making
146      * a release candidate for a major version.  (X.0.0)
147      * When the patch number is 99, that signifies that the we are making
148      * a release candidate for a minor version.  (X.Y.0)
149      * When the patch number is < 99, then we are making a release
150      * candidate for the next point release.  (X.Y.Z)
151      */
152 #if XORG_VERSION_MINOR >= 99
153     xf86ErrorFVerb(0, " (%d.0.0 RC %d)", XORG_VERSION_MAJOR + 1,
154                    XORG_VERSION_SNAP - 900);
155 #elif XORG_VERSION_PATCH == 99
156     xf86ErrorFVerb(0, " (%d.%d.0 RC %d)", XORG_VERSION_MAJOR,
157                    XORG_VERSION_MINOR + 1, XORG_VERSION_SNAP - 900);
158 #else
159     xf86ErrorFVerb(0, " (%d.%d.%d RC %d)", XORG_VERSION_MAJOR,
160                    XORG_VERSION_MINOR, XORG_VERSION_PATCH + 1,
161                    XORG_VERSION_SNAP - 900);
162 #endif
163 #endif
164 
165 #ifdef XORG_CUSTOM_VERSION
166     xf86ErrorFVerb(0, " (%s)", XORG_CUSTOM_VERSION);
167 #endif
168     xf86ErrorFVerb(0, "\nX Protocol Version %d, Revision %d\n",
169                    X_PROTOCOL, X_PROTOCOL_REVISION);
170     xf86ErrorFVerb(0, "Build Operating System: %s %s\n", OSNAME, OSVENDOR);
171 #ifdef HAS_UTSNAME
172     {
173         struct utsname name;
174 
175         /* Linux & BSD state that 0 is success, SysV (including Solaris, HP-UX,
176            and Irix) and Single Unix Spec 3 just say that non-negative is success.
177            All agree that failure is represented by a negative number.
178          */
179         if (uname(&name) >= 0) {
180             xf86ErrorFVerb(0, "Current Operating System: %s %s %s %s %s\n",
181                            name.sysname, name.nodename, name.release,
182                            name.version, name.machine);
183 #ifdef __linux__
184             do {
185                 char buf[80];
186                 int fd = open("/proc/cmdline", O_RDONLY);
187 
188                 if (fd != -1) {
189                     xf86ErrorFVerb(0, "Kernel command line: ");
190                     memset(buf, 0, 80);
191                     while (read(fd, buf, 80) > 0) {
192                         xf86ErrorFVerb(0, "%.80s", buf);
193                         memset(buf, 0, 80);
194                     }
195                     close(fd);
196                 }
197             } while (0);
198 #endif
199         }
200     }
201 #endif
202 #if defined(BUILD_DATE) && (BUILD_DATE > 19000000)
203     {
204         struct tm t;
205         char buf[100];
206 
207         memset(&t, 0, sizeof(t));
208         memset(buf, 0, sizeof(buf));
209         t.tm_mday = BUILD_DATE % 100;
210         t.tm_mon = (BUILD_DATE / 100) % 100 - 1;
211         t.tm_year = BUILD_DATE / 10000 - 1900;
212 #if defined(BUILD_TIME)
213         t.tm_sec = BUILD_TIME % 100;
214         t.tm_min = (BUILD_TIME / 100) % 100;
215         t.tm_hour = (BUILD_TIME / 10000) % 100;
216         if (strftime(buf, sizeof(buf), "%d %B %Y  %I:%M:%S%p", &t))
217             xf86ErrorFVerb(0, "Build Date: %s\n", buf);
218 #else
219         if (strftime(buf, sizeof(buf), "%d %B %Y", &t))
220             xf86ErrorFVerb(0, "Build Date: %s\n", buf);
221 #endif
222     }
223 #endif
224 #if defined(BUILDERSTRING)
225     xf86ErrorFVerb(0, "%s \n", BUILDERSTRING);
226 #endif
227     xf86ErrorFVerb(0, "Current version of pixman: %s\n",
228                    pixman_version_string());
229     xf86ErrorFVerb(0, "\tBefore reporting problems, check "
230                    "" __VENDORDWEBSUPPORT__ "\n"
231                    "\tto make sure that you have the latest version.\n");
232 }
233 
234 Bool
xf86PrivsElevated(void)235 xf86PrivsElevated(void)
236 {
237     return PrivsElevated();
238 }
239 
240 static void
TrapSignals(void)241 TrapSignals(void)
242 {
243     if (xf86Info.notrapSignals) {
244         OsSignal(SIGSEGV, SIG_DFL);
245         OsSignal(SIGABRT, SIG_DFL);
246         OsSignal(SIGILL, SIG_DFL);
247 #ifdef SIGEMT
248         OsSignal(SIGEMT, SIG_DFL);
249 #endif
250         OsSignal(SIGFPE, SIG_DFL);
251         OsSignal(SIGBUS, SIG_DFL);
252         OsSignal(SIGSYS, SIG_DFL);
253         OsSignal(SIGXCPU, SIG_DFL);
254         OsSignal(SIGXFSZ, SIG_DFL);
255     }
256 }
257 
258 static void
AddSeatId(CallbackListPtr * pcbl,void * data,void * screen)259 AddSeatId(CallbackListPtr *pcbl, void *data, void *screen)
260 {
261     ScreenPtr pScreen = screen;
262     Atom SeatAtom = MakeAtom(SEAT_ATOM_NAME, sizeof(SEAT_ATOM_NAME) - 1, TRUE);
263     int err;
264 
265     err = dixChangeWindowProperty(serverClient, pScreen->root, SeatAtom,
266                                   XA_STRING, 8, PropModeReplace,
267                                   strlen(data) + 1, data, FALSE);
268 
269     if (err != Success)
270         xf86DrvMsg(pScreen->myNum, X_WARNING,
271                    "Failed to register seat property\n");
272 }
273 
274 static void
AddVTAtoms(CallbackListPtr * pcbl,void * data,void * screen)275 AddVTAtoms(CallbackListPtr *pcbl, void *data, void *screen)
276 {
277 #define VT_ATOM_NAME         "XFree86_VT"
278     int err, HasVT = 1;
279     ScreenPtr pScreen = screen;
280     Atom VTAtom = MakeAtom(VT_ATOM_NAME, sizeof(VT_ATOM_NAME) - 1, TRUE);
281     Atom HasVTAtom = MakeAtom(HAS_VT_ATOM_NAME, sizeof(HAS_VT_ATOM_NAME) - 1,
282                               TRUE);
283 
284     err = dixChangeWindowProperty(serverClient, pScreen->root, VTAtom,
285                                   XA_INTEGER, 32, PropModeReplace, 1,
286                                   &xf86Info.vtno, FALSE);
287 
288     err |= dixChangeWindowProperty(serverClient, pScreen->root, HasVTAtom,
289                                    XA_INTEGER, 32, PropModeReplace, 1,
290                                    &HasVT, FALSE);
291 
292     if (err != Success)
293         xf86DrvMsg(pScreen->myNum, X_WARNING,
294                    "Failed to register VT properties\n");
295 }
296 
297 static Bool
xf86ScreenInit(ScreenPtr pScreen,int argc,char ** argv)298 xf86ScreenInit(ScreenPtr pScreen, int argc, char **argv)
299 {
300     ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
301 
302     pScrn->pScreen = pScreen;
303     return pScrn->ScreenInit (pScreen, argc, argv);
304 }
305 
306 static void
xf86EnsureRANDR(ScreenPtr pScreen)307 xf86EnsureRANDR(ScreenPtr pScreen)
308 {
309 #ifdef RANDR
310         if (!dixPrivateKeyRegistered(rrPrivKey) ||
311             !rrGetScrPriv(pScreen))
312             xf86RandRInit(pScreen);
313 #endif
314 }
315 
316 /*
317  * InitOutput --
318  *	Initialize screenInfo for all actually accessible framebuffers.
319  *      That includes vt-manager setup, querying all possible devices and
320  *      collecting the pixmap formats.
321  */
322 void
InitOutput(ScreenInfo * pScreenInfo,int argc,char ** argv)323 InitOutput(ScreenInfo * pScreenInfo, int argc, char **argv)
324 {
325     int i, j, k, scr_index;
326     const char **modulelist;
327     void **optionlist;
328     Bool autoconfig = FALSE;
329     Bool sigio_blocked = FALSE;
330     Bool want_hw_access = FALSE;
331     GDevPtr configured_device;
332 
333     xf86Initialising = TRUE;
334 
335     config_pre_init();
336 
337     if (serverGeneration == 1) {
338         if ((xf86ServerName = strrchr(argv[0], '/')) != 0)
339             xf86ServerName++;
340         else
341             xf86ServerName = argv[0];
342 
343         xf86PrintBanner();
344         LogPrintMarkers();
345         if (xf86LogFile) {
346             time_t t;
347             const char *ct;
348 
349             t = time(NULL);
350             ct = ctime(&t);
351             xf86MsgVerb(xf86LogFileFrom, 0, "Log file: \"%s\", Time: %s",
352                         xf86LogFile, ct);
353         }
354 
355         /* Read and parse the config file */
356         if (!xf86DoConfigure && !xf86DoShowOptions) {
357             switch (xf86HandleConfigFile(FALSE)) {
358             case CONFIG_OK:
359                 break;
360             case CONFIG_PARSE_ERROR:
361                 xf86Msg(X_ERROR, "Error parsing the config file\n");
362                 return;
363             case CONFIG_NOFILE:
364                 autoconfig = TRUE;
365                 break;
366             }
367         }
368 
369         TrapSignals();
370 
371         /* Initialise the loader */
372         LoaderInit();
373 
374         /* Tell the loader the default module search path */
375         LoaderSetPath(xf86ModulePath);
376 
377         if (xf86Info.ignoreABI) {
378             LoaderSetOptions(LDR_OPT_ABI_MISMATCH_NONFATAL);
379         }
380 
381         if (xf86DoShowOptions)
382             DoShowOptions();
383 
384         dbus_core_init();
385         systemd_logind_init();
386 
387         /* Do a general bus probe.  This will be a PCI probe for x86 platforms */
388         xf86BusProbe();
389 
390         if (xf86DoConfigure)
391             DoConfigure();
392 
393         if (autoconfig) {
394             if (!xf86AutoConfig()) {
395                 xf86Msg(X_ERROR, "Auto configuration failed\n");
396                 return;
397             }
398         }
399 
400 #ifdef XF86PM
401         xf86OSPMClose = xf86OSPMOpen();
402 #endif
403 
404         xf86ExtensionInit();
405 
406         /* Load all modules specified explicitly in the config file */
407         if ((modulelist = xf86ModulelistFromConfig(&optionlist))) {
408             xf86LoadModules(modulelist, optionlist);
409             free(modulelist);
410             free(optionlist);
411         }
412 
413         /* Load all driver modules specified in the config file */
414         /* If there aren't any specified in the config file, autoconfig them */
415         /* FIXME: Does not handle multiple active screen sections, but I'm not
416          * sure if we really want to handle that case*/
417         configured_device = xf86ConfigLayout.screens->screen->device;
418         if ((!configured_device) || (!configured_device->driver)) {
419             if (!autoConfigDevice(configured_device)) {
420                 xf86Msg(X_ERROR, "Automatic driver configuration failed\n");
421                 return;
422             }
423         }
424         if ((modulelist = xf86DriverlistFromConfig())) {
425             xf86LoadModules(modulelist, NULL);
426             free(modulelist);
427         }
428 
429         /* Load all input driver modules specified in the config file. */
430         if ((modulelist = xf86InputDriverlistFromConfig())) {
431             xf86LoadModules(modulelist, NULL);
432             free(modulelist);
433         }
434 
435         /*
436          * It is expected that xf86AddDriver()/xf86AddInputDriver will be
437          * called for each driver as it is loaded.  Those functions save the
438          * module pointers for drivers.
439          * XXX Nothing keeps track of them for other modules.
440          */
441         /* XXX What do we do if not all of these could be loaded? */
442 
443         /*
444          * At this point, xf86DriverList[] is all filled in with entries for
445          * each of the drivers to try and xf86NumDrivers has the number of
446          * drivers.  If there are none, return now.
447          */
448 
449         if (xf86NumDrivers == 0) {
450             xf86Msg(X_ERROR, "No drivers available.\n");
451             return;
452         }
453 
454         /*
455          * Call each of the Identify functions and call the driverFunc to check
456          * if HW access is required.  The Identify functions print out some
457          * identifying information, and anything else that might be
458          * needed at this early stage.
459          */
460 
461         for (i = 0; i < xf86NumDrivers; i++) {
462             xorgHWFlags flags = HW_IO;
463 
464             if (xf86DriverList[i]->Identify != NULL)
465                 xf86DriverList[i]->Identify(0);
466 
467             if (xf86DriverList[i]->driverFunc)
468                 xf86DriverList[i]->driverFunc(NULL,
469                                               GET_REQUIRED_HW_INTERFACES,
470                                               &flags);
471 
472             if (NEED_IO_ENABLED(flags))
473                 want_hw_access = TRUE;
474 
475             /* Non-seat0 X servers should not open console */
476             if (!(flags & HW_SKIP_CONSOLE) && !ServerIsNotSeat0())
477                 xorgHWOpenConsole = TRUE;
478         }
479 
480         if (xorgHWOpenConsole)
481             xf86OpenConsole();
482         else
483             xf86Info.dontVTSwitch = TRUE;
484 
485 	/* Enable full I/O access */
486 	if (want_hw_access)
487 	    xorgHWAccess = xf86EnableIO();
488 
489         if (xf86BusConfig() == FALSE)
490             return;
491 
492         xf86PostProbe();
493 
494         /*
495          * Sort the drivers to match the requested ording.  Using a slow
496          * bubble sort.
497          */
498         for (j = 0; j < xf86NumScreens - 1; j++) {
499             for (i = 0; i < xf86NumScreens - j - 1; i++) {
500                 if (xf86Screens[i + 1]->confScreen->screennum <
501                     xf86Screens[i]->confScreen->screennum) {
502                     ScrnInfoPtr tmpScrn = xf86Screens[i + 1];
503 
504                     xf86Screens[i + 1] = xf86Screens[i];
505                     xf86Screens[i] = tmpScrn;
506                 }
507             }
508         }
509         /* Fix up the indexes */
510         for (i = 0; i < xf86NumScreens; i++) {
511             xf86Screens[i]->scrnIndex = i;
512         }
513 
514         /*
515          * Call the driver's PreInit()'s to complete initialisation for the first
516          * generation.
517          */
518 
519         for (i = 0; i < xf86NumScreens; i++) {
520             xf86VGAarbiterScrnInit(xf86Screens[i]);
521             xf86VGAarbiterLock(xf86Screens[i]);
522             if (xf86Screens[i]->PreInit &&
523                 xf86Screens[i]->PreInit(xf86Screens[i], 0))
524                 xf86Screens[i]->configured = TRUE;
525             xf86VGAarbiterUnlock(xf86Screens[i]);
526         }
527         for (i = 0; i < xf86NumScreens; i++)
528             if (!xf86Screens[i]->configured)
529                 xf86DeleteScreen(xf86Screens[i--]);
530 
531         for (i = 0; i < xf86NumGPUScreens; i++) {
532             xf86VGAarbiterScrnInit(xf86GPUScreens[i]);
533             xf86VGAarbiterLock(xf86GPUScreens[i]);
534             if (xf86GPUScreens[i]->PreInit &&
535                 xf86GPUScreens[i]->PreInit(xf86GPUScreens[i], 0))
536                 xf86GPUScreens[i]->configured = TRUE;
537             xf86VGAarbiterUnlock(xf86GPUScreens[i]);
538         }
539         for (i = 0; i < xf86NumGPUScreens; i++)
540             if (!xf86GPUScreens[i]->configured)
541                 xf86DeleteScreen(xf86GPUScreens[i--]);
542 
543         /*
544          * If no screens left, return now.
545          */
546 
547         if (xf86NumScreens == 0) {
548             xf86Msg(X_ERROR,
549                     "Screen(s) found, but none have a usable configuration.\n");
550             return;
551         }
552 
553         for (i = 0; i < xf86NumScreens; i++) {
554             if (xf86Screens[i]->name == NULL) {
555                 char *tmp;
556                 XNFasprintf(&tmp, "screen%d", i);
557                 xf86Screens[i]->name = tmp;
558                 xf86MsgVerb(X_WARNING, 0,
559                             "Screen driver %d has no name set, using `%s'.\n",
560                             i, xf86Screens[i]->name);
561             }
562         }
563 
564         /* Remove (unload) drivers that are not required */
565         for (i = 0; i < xf86NumDrivers; i++)
566             if (xf86DriverList[i] && xf86DriverList[i]->refCount <= 0)
567                 xf86DeleteDriver(i);
568 
569         /*
570          * At this stage we know how many screens there are.
571          */
572 
573         for (i = 0; i < xf86NumScreens; i++)
574             xf86InitViewport(xf86Screens[i]);
575 
576         /*
577          * Collect all pixmap formats and check for conflicts at the display
578          * level.  Should we die here?  Or just delete the offending screens?
579          */
580         for (i = 0; i < xf86NumScreens; i++) {
581             if (xf86Screens[i]->imageByteOrder !=
582                 xf86Screens[0]->imageByteOrder)
583                 FatalError("Inconsistent display bitmapBitOrder.  Exiting\n");
584             if (xf86Screens[i]->bitmapScanlinePad !=
585                 xf86Screens[0]->bitmapScanlinePad)
586                 FatalError
587                     ("Inconsistent display bitmapScanlinePad.  Exiting\n");
588             if (xf86Screens[i]->bitmapScanlineUnit !=
589                 xf86Screens[0]->bitmapScanlineUnit)
590                 FatalError
591                     ("Inconsistent display bitmapScanlineUnit.  Exiting\n");
592             if (xf86Screens[i]->bitmapBitOrder !=
593                 xf86Screens[0]->bitmapBitOrder)
594                 FatalError("Inconsistent display bitmapBitOrder.  Exiting\n");
595         }
596 
597         /* Collect additional formats */
598         for (i = 0; i < xf86NumScreens; i++) {
599             for (j = 0; j < xf86Screens[i]->numFormats; j++) {
600                 for (k = 0;; k++) {
601                     if (k >= numFormats) {
602                         if (k >= MAXFORMATS)
603                             FatalError("Too many pixmap formats!  Exiting\n");
604                         formats[k] = xf86Screens[i]->formats[j];
605                         numFormats++;
606                         break;
607                     }
608                     if (formats[k].depth == xf86Screens[i]->formats[j].depth) {
609                         if ((formats[k].bitsPerPixel ==
610                              xf86Screens[i]->formats[j].bitsPerPixel) &&
611                             (formats[k].scanlinePad ==
612                              xf86Screens[i]->formats[j].scanlinePad))
613                             break;
614                         FatalError("Inconsistent pixmap format for depth %d."
615                                    "  Exiting\n", formats[k].depth);
616                     }
617                 }
618             }
619         }
620         formatsDone = TRUE;
621     }
622     else {
623         /*
624          * serverGeneration != 1; some OSs have to do things here, too.
625          */
626         if (xorgHWOpenConsole)
627             xf86OpenConsole();
628 
629 #ifdef XF86PM
630         /*
631            should we reopen it here? We need to deal with an already opened
632            device. We could leave this to the OS layer. For now we simply
633            close it here
634          */
635         if (xf86OSPMClose)
636             xf86OSPMClose();
637         if ((xf86OSPMClose = xf86OSPMOpen()) != NULL)
638             xf86MsgVerb(X_INFO, 3, "APM registered successfully\n");
639 #endif
640 
641         /* Make sure full I/O access is enabled */
642         if (xorgHWAccess)
643             xf86EnableIO();
644     }
645 
646     if (xf86Info.vtno >= 0)
647         AddCallback(&RootWindowFinalizeCallback, AddVTAtoms, NULL);
648 
649     if (SeatId)
650         AddCallback(&RootWindowFinalizeCallback, AddSeatId, SeatId);
651 
652     /*
653      * Use the previously collected parts to setup pScreenInfo
654      */
655 
656     pScreenInfo->imageByteOrder = xf86Screens[0]->imageByteOrder;
657     pScreenInfo->bitmapScanlinePad = xf86Screens[0]->bitmapScanlinePad;
658     pScreenInfo->bitmapScanlineUnit = xf86Screens[0]->bitmapScanlineUnit;
659     pScreenInfo->bitmapBitOrder = xf86Screens[0]->bitmapBitOrder;
660     pScreenInfo->numPixmapFormats = numFormats;
661     for (i = 0; i < numFormats; i++)
662         pScreenInfo->formats[i] = formats[i];
663 
664     /* Make sure the server's VT is active */
665 
666     if (serverGeneration != 1) {
667         xf86Resetting = TRUE;
668         /* All screens are in the same state, so just check the first */
669         if (!xf86VTOwner()) {
670 #ifdef HAS_USL_VTS
671             ioctl(xf86Info.consoleFd, VT_RELDISP, VT_ACKACQ);
672 #endif
673             input_lock();
674             sigio_blocked = TRUE;
675         }
676     }
677 
678     for (i = 0; i < xf86NumScreens; i++)
679         if (!xf86ColormapAllocatePrivates(xf86Screens[i]))
680             FatalError("Cannot register DDX private keys");
681 
682     if (!dixRegisterPrivateKey(&xf86ScreenKeyRec, PRIVATE_SCREEN, 0))
683         FatalError("Cannot register DDX private keys");
684 
685     for (i = 0; i < xf86NumScreens; i++) {
686         xf86VGAarbiterLock(xf86Screens[i]);
687         /*
688          * Almost everything uses these defaults, and many of those that
689          * don't, will wrap them.
690          */
691         xf86Screens[i]->EnableDisableFBAccess = xf86EnableDisableFBAccess;
692 #ifdef XFreeXDGA
693         xf86Screens[i]->SetDGAMode = xf86SetDGAMode;
694 #endif
695         xf86Screens[i]->DPMSSet = NULL;
696         xf86Screens[i]->LoadPalette = NULL;
697         xf86Screens[i]->SetOverscan = NULL;
698         xf86Screens[i]->DriverFunc = NULL;
699         xf86Screens[i]->pScreen = NULL;
700         scr_index = AddScreen(xf86ScreenInit, argc, argv);
701         xf86VGAarbiterUnlock(xf86Screens[i]);
702         if (scr_index == i) {
703             /*
704              * Hook in our ScrnInfoRec, and initialise some other pScreen
705              * fields.
706              */
707             dixSetPrivate(&screenInfo.screens[scr_index]->devPrivates,
708                           xf86ScreenKey, xf86Screens[i]);
709             xf86Screens[i]->pScreen = screenInfo.screens[scr_index];
710             /* The driver should set this, but make sure it is set anyway */
711             xf86Screens[i]->vtSema = TRUE;
712         }
713         else {
714             /* This shouldn't normally happen */
715             FatalError("AddScreen/ScreenInit failed for driver %d\n", i);
716         }
717 
718         DebugF("InitOutput - xf86Screens[%d]->pScreen = %p\n",
719                i, xf86Screens[i]->pScreen);
720         DebugF("xf86Screens[%d]->pScreen->CreateWindow = %p\n",
721                i, xf86Screens[i]->pScreen->CreateWindow);
722 
723         if (PictureGetSubpixelOrder(xf86Screens[i]->pScreen) == SubPixelUnknown) {
724             xf86MonPtr DDC = (xf86MonPtr) (xf86Screens[i]->monitor->DDC);
725 
726             PictureSetSubpixelOrder(xf86Screens[i]->pScreen,
727                                     DDC ?
728                                     (DDC->features.input_type ?
729                                      SubPixelHorizontalRGB : SubPixelNone) :
730                                     SubPixelUnknown);
731         }
732 
733         /*
734          * If the driver hasn't set up its own RANDR support, install the
735          * fallback support.
736          */
737         xf86EnsureRANDR(xf86Screens[i]->pScreen);
738     }
739 
740     for (i = 0; i < xf86NumGPUScreens; i++) {
741         ScrnInfoPtr pScrn = xf86GPUScreens[i];
742         xf86VGAarbiterLock(pScrn);
743 
744         /*
745          * Almost everything uses these defaults, and many of those that
746          * don't, will wrap them.
747          */
748         pScrn->EnableDisableFBAccess = xf86EnableDisableFBAccess;
749 #ifdef XFreeXDGA
750         pScrn->SetDGAMode = xf86SetDGAMode;
751 #endif
752         pScrn->DPMSSet = NULL;
753         pScrn->LoadPalette = NULL;
754         pScrn->SetOverscan = NULL;
755         pScrn->DriverFunc = NULL;
756         pScrn->pScreen = NULL;
757         scr_index = AddGPUScreen(xf86ScreenInit, argc, argv);
758         xf86VGAarbiterUnlock(pScrn);
759         if (scr_index == i) {
760             dixSetPrivate(&screenInfo.gpuscreens[scr_index]->devPrivates,
761                           xf86ScreenKey, xf86GPUScreens[i]);
762             pScrn->pScreen = screenInfo.gpuscreens[scr_index];
763             /* The driver should set this, but make sure it is set anyway */
764             pScrn->vtSema = TRUE;
765         } else {
766             FatalError("AddScreen/ScreenInit failed for gpu driver %d %d\n", i, scr_index);
767         }
768     }
769 
770     for (i = 0; i < xf86NumGPUScreens; i++)
771         AttachUnboundGPU(xf86Screens[0]->pScreen, xf86GPUScreens[i]->pScreen);
772 
773     xf86VGAarbiterWrapFunctions();
774     if (sigio_blocked)
775         input_unlock();
776 
777     xf86InitOrigins();
778 
779     xf86Resetting = FALSE;
780     xf86Initialising = FALSE;
781 
782     RegisterBlockAndWakeupHandlers((ServerBlockHandlerProcPtr) NoopDDA, xf86Wakeup,
783                                    NULL);
784 }
785 
786 /**
787  * Initialize all supported input devices present and referenced in the
788  * xorg.conf.
789  */
790 void
InitInput(int argc,char ** argv)791 InitInput(int argc, char **argv)
792 {
793     InputInfoPtr *pInfo;
794     DeviceIntPtr dev;
795 
796     xf86Info.vtRequestsPending = FALSE;
797 
798     /* Enable threaded input */
799     InputThreadPreInit();
800 
801     mieqInit();
802 
803     /* Initialize all configured input devices */
804     for (pInfo = xf86ConfigLayout.inputs; pInfo && *pInfo; pInfo++) {
805         (*pInfo)->options =
806             xf86AddNewOption((*pInfo)->options, "driver", (*pInfo)->driver);
807         (*pInfo)->options =
808             xf86AddNewOption((*pInfo)->options, "identifier", (*pInfo)->name);
809         /* If one fails, the others will too */
810         if (NewInputDeviceRequest((*pInfo)->options, NULL, &dev) == BadAlloc)
811             break;
812     }
813 
814     config_init();
815 }
816 
817 void
CloseInput(void)818 CloseInput(void)
819 {
820     config_fini();
821     mieqFini();
822 }
823 
824 /*
825  * OsVendorInit --
826  *      OS/Vendor-specific initialisations.  Called from OsInit(), which
827  *      is called by dix before establishing the well known sockets.
828  */
829 
830 void
OsVendorInit(void)831 OsVendorInit(void)
832 {
833     static Bool beenHere = FALSE;
834 
835     OsSignal(SIGCHLD, SIG_DFL);   /* Need to wait for child processes */
836 
837     if (!beenHere) {
838         umask(022);
839         xf86LogInit();
840     }
841 
842     /* Set stderr to non-blocking. */
843 #ifndef O_NONBLOCK
844 #if defined(FNDELAY)
845 #define O_NONBLOCK FNDELAY
846 #elif defined(O_NDELAY)
847 #define O_NONBLOCK O_NDELAY
848 #endif
849 
850 #ifdef O_NONBLOCK
851     if (!beenHere) {
852         if (PrivsElevated()) {
853             int status;
854 
855             status = fcntl(fileno(stderr), F_GETFL, 0);
856             if (status != -1) {
857                 fcntl(fileno(stderr), F_SETFL, status | O_NONBLOCK);
858             }
859         }
860     }
861 #endif
862 #endif
863 
864     beenHere = TRUE;
865 }
866 
867 /*
868  * ddxGiveUp --
869  *      Device dependent cleanup. Called by by dix before normal server death.
870  *      For SYSV386 we must switch the terminal back to normal mode. No error-
871  *      checking here, since there should be restored as much as possible.
872  */
873 
874 void
ddxGiveUp(enum ExitCode error)875 ddxGiveUp(enum ExitCode error)
876 {
877     int i;
878 
879     xf86VGAarbiterFini();
880 
881 #ifdef XF86PM
882     if (xf86OSPMClose)
883         xf86OSPMClose();
884     xf86OSPMClose = NULL;
885 #endif
886 
887     for (i = 0; i < xf86NumScreens; i++) {
888         /*
889          * zero all access functions to
890          * trap calls when switched away.
891          */
892         xf86Screens[i]->vtSema = FALSE;
893     }
894 
895 #ifdef XFreeXDGA
896     DGAShutdown();
897 #endif
898 
899     if (xorgHWOpenConsole)
900         xf86CloseConsole();
901 
902     systemd_logind_fini();
903     dbus_core_fini();
904 
905     xf86CloseLog(error);
906 }
907 
908 /*
909  * AbortDDX --
910  *      DDX - specific abort routine.  Called by AbortServer(). The attempt is
911  *      made to restore all original setting of the displays. Also all devices
912  *      are closed.
913  */
914 
915 void
AbortDDX(enum ExitCode error)916 AbortDDX(enum ExitCode error)
917 {
918     int i;
919 
920     input_lock();
921 
922     /*
923      * try to restore the original video state
924      */
925 #ifdef DPMSExtension            /* Turn screens back on */
926     if (DPMSPowerLevel != DPMSModeOn)
927         DPMSSet(serverClient, DPMSModeOn);
928 #endif
929     if (xf86Screens) {
930         for (i = 0; i < xf86NumScreens; i++)
931             if (xf86Screens[i]->vtSema) {
932                 /*
933                  * if we are aborting before ScreenInit() has finished
934                  * we might not have been wrapped yet. Therefore enable
935                  * screen explicitely.
936                  */
937                 xf86VGAarbiterLock(xf86Screens[i]);
938                 (xf86Screens[i]->LeaveVT) (xf86Screens[i]);
939                 xf86VGAarbiterUnlock(xf86Screens[i]);
940             }
941     }
942 
943     /*
944      * This is needed for an abnormal server exit, since the normal exit stuff
945      * MUST also be performed (i.e. the vt must be left in a defined state)
946      */
947     ddxGiveUp(error);
948 }
949 
950 void
OsVendorFatalError(const char * f,va_list args)951 OsVendorFatalError(const char *f, va_list args)
952 {
953 #ifdef VENDORSUPPORT
954     ErrorFSigSafe("\nPlease refer to your Operating System Vendor support "
955                  "pages\nat %s for support on this crash.\n", VENDORSUPPORT);
956 #else
957     ErrorFSigSafe("\nPlease consult the " XVENDORNAME " support \n\t at "
958                  __VENDORDWEBSUPPORT__ "\n for help. \n");
959 #endif
960     if (xf86LogFile && xf86LogFileWasOpened)
961         ErrorFSigSafe("Please also check the log file at \"%s\" for additional "
962                      "information.\n", xf86LogFile);
963     ErrorFSigSafe("\n");
964 }
965 
966 int
xf86SetVerbosity(int verb)967 xf86SetVerbosity(int verb)
968 {
969     int save = xf86Verbose;
970 
971     xf86Verbose = verb;
972     LogSetParameter(XLOG_VERBOSITY, verb);
973     return save;
974 }
975 
976 int
xf86SetLogVerbosity(int verb)977 xf86SetLogVerbosity(int verb)
978 {
979     int save = xf86LogVerbose;
980 
981     xf86LogVerbose = verb;
982     LogSetParameter(XLOG_FILE_VERBOSITY, verb);
983     return save;
984 }
985 
986 static void
xf86PrintDefaultModulePath(void)987 xf86PrintDefaultModulePath(void)
988 {
989     ErrorF("%s\n", DEFAULT_MODULE_PATH);
990 }
991 
992 static void
xf86PrintDefaultLibraryPath(void)993 xf86PrintDefaultLibraryPath(void)
994 {
995     ErrorF("%s\n", DEFAULT_LIBRARY_PATH);
996 }
997 
998 static void
xf86CheckPrivs(const char * option,const char * arg)999 xf86CheckPrivs(const char *option, const char *arg)
1000 {
1001     if (PrivsElevated() && !xf86PathIsSafe(arg)) {
1002         FatalError("\nInvalid argument for %s - \"%s\"\n"
1003                     "\tWith elevated privileges %s must specify a relative path\n"
1004                     "\twithout any \"..\" elements.\n\n", option, arg, option);
1005     }
1006 }
1007 
1008 /*
1009  * ddxProcessArgument --
1010  *	Process device-dependent command line args. Returns 0 if argument is
1011  *      not device dependent, otherwise Count of number of elements of argv
1012  *      that are part of a device dependent commandline option.
1013  *
1014  */
1015 
1016 /* ARGSUSED */
1017 int
ddxProcessArgument(int argc,char ** argv,int i)1018 ddxProcessArgument(int argc, char **argv, int i)
1019 {
1020 #define CHECK_FOR_REQUIRED_ARGUMENT() \
1021     if (((i + 1) >= argc) || (!argv[i + 1])) { 				\
1022       ErrorF("Required argument to %s not specified\n", argv[i]); 	\
1023       UseMsg(); 							\
1024       FatalError("Required argument to %s not specified\n", argv[i]);	\
1025     }
1026 
1027     /* First the options that are not allowed with elevated privileges */
1028     if (!strcmp(argv[i], "-modulepath")) {
1029         CHECK_FOR_REQUIRED_ARGUMENT();
1030         if (xf86PrivsElevated())
1031               FatalError("\nInvalid argument -modulepath "
1032                 "with elevated privileges\n");
1033         xf86ModulePath = argv[i + 1];
1034         xf86ModPathFrom = X_CMDLINE;
1035         return 2;
1036     }
1037     if (!strcmp(argv[i], "-logfile")) {
1038         CHECK_FOR_REQUIRED_ARGUMENT();
1039         if (xf86PrivsElevated())
1040               FatalError("\nInvalid argument -logfile "
1041                 "with elevated privileges\n");
1042         xf86LogFile = argv[i + 1];
1043         xf86LogFileFrom = X_CMDLINE;
1044         return 2;
1045     }
1046     if (!strcmp(argv[i], "-config") || !strcmp(argv[i], "-xf86config")) {
1047         CHECK_FOR_REQUIRED_ARGUMENT();
1048         xf86CheckPrivs(argv[i], argv[i + 1]);
1049         xf86ConfigFile = argv[i + 1];
1050         return 2;
1051     }
1052     if (!strcmp(argv[i], "-configdir")) {
1053         CHECK_FOR_REQUIRED_ARGUMENT();
1054         xf86CheckPrivs(argv[i], argv[i + 1]);
1055         xf86ConfigDir = argv[i + 1];
1056         return 2;
1057     }
1058     if (!strcmp(argv[i], "-flipPixels")) {
1059         xf86FlipPixels = TRUE;
1060         return 1;
1061     }
1062 #ifdef XF86VIDMODE
1063     if (!strcmp(argv[i], "-disableVidMode")) {
1064         xf86VidModeDisabled = TRUE;
1065         return 1;
1066     }
1067     if (!strcmp(argv[i], "-allowNonLocalXvidtune")) {
1068         xf86VidModeAllowNonLocal = TRUE;
1069         return 1;
1070     }
1071 #endif
1072     if (!strcmp(argv[i], "-allowMouseOpenFail")) {
1073         xf86AllowMouseOpenFail = TRUE;
1074         return 1;
1075     }
1076     if (!strcmp(argv[i], "-ignoreABI")) {
1077         LoaderSetOptions(LDR_OPT_ABI_MISMATCH_NONFATAL);
1078         return 1;
1079     }
1080     if (!strcmp(argv[i], "-verbose")) {
1081         if (++i < argc && argv[i]) {
1082             char *end;
1083             long val;
1084 
1085             val = strtol(argv[i], &end, 0);
1086             if (*end == '\0') {
1087                 xf86SetVerbosity(val);
1088                 return 2;
1089             }
1090         }
1091         xf86SetVerbosity(++xf86Verbose);
1092         return 1;
1093     }
1094     if (!strcmp(argv[i], "-logverbose")) {
1095         if (++i < argc && argv[i]) {
1096             char *end;
1097             long val;
1098 
1099             val = strtol(argv[i], &end, 0);
1100             if (*end == '\0') {
1101                 xf86SetLogVerbosity(val);
1102                 return 2;
1103             }
1104         }
1105         xf86SetLogVerbosity(++xf86LogVerbose);
1106         return 1;
1107     }
1108     if (!strcmp(argv[i], "-quiet")) {
1109         xf86SetVerbosity(-1);
1110         return 1;
1111     }
1112     if (!strcmp(argv[i], "-showconfig") || !strcmp(argv[i], "-version")) {
1113         xf86PrintBanner();
1114         exit(0);
1115     }
1116     if (!strcmp(argv[i], "-showDefaultModulePath")) {
1117         xf86PrintDefaultModulePath();
1118         exit(0);
1119     }
1120     if (!strcmp(argv[i], "-showDefaultLibPath")) {
1121         xf86PrintDefaultLibraryPath();
1122         exit(0);
1123     }
1124     /* Notice the -fp flag, but allow it to pass to the dix layer */
1125     if (!strcmp(argv[i], "-fp")) {
1126         xf86fpFlag = TRUE;
1127         return 0;
1128     }
1129     /* Notice the -bs flag, but allow it to pass to the dix layer */
1130     if (!strcmp(argv[i], "-bs")) {
1131         xf86bsDisableFlag = TRUE;
1132         return 0;
1133     }
1134     /* Notice the +bs flag, but allow it to pass to the dix layer */
1135     if (!strcmp(argv[i], "+bs")) {
1136         xf86bsEnableFlag = TRUE;
1137         return 0;
1138     }
1139     /* Notice the -s flag, but allow it to pass to the dix layer */
1140     if (!strcmp(argv[i], "-s")) {
1141         xf86sFlag = TRUE;
1142         return 0;
1143     }
1144     if (!strcmp(argv[i], "-pixmap32") || !strcmp(argv[i], "-pixmap24")) {
1145         /* silently accept */
1146         return 1;
1147     }
1148     if (!strcmp(argv[i], "-fbbpp")) {
1149         int bpp;
1150 
1151         CHECK_FOR_REQUIRED_ARGUMENT();
1152         if (sscanf(argv[++i], "%d", &bpp) == 1) {
1153             xf86FbBpp = bpp;
1154             return 2;
1155         }
1156         else {
1157             ErrorF("Invalid fbbpp\n");
1158             return 0;
1159         }
1160     }
1161     if (!strcmp(argv[i], "-depth")) {
1162         int depth;
1163 
1164         CHECK_FOR_REQUIRED_ARGUMENT();
1165         if (sscanf(argv[++i], "%d", &depth) == 1) {
1166             xf86Depth = depth;
1167             return 2;
1168         }
1169         else {
1170             ErrorF("Invalid depth\n");
1171             return 0;
1172         }
1173     }
1174     if (!strcmp(argv[i], "-weight")) {
1175         int red, green, blue;
1176 
1177         CHECK_FOR_REQUIRED_ARGUMENT();
1178         if (sscanf(argv[++i], "%1d%1d%1d", &red, &green, &blue) == 3) {
1179             xf86Weight.red = red;
1180             xf86Weight.green = green;
1181             xf86Weight.blue = blue;
1182             return 2;
1183         }
1184         else {
1185             ErrorF("Invalid weighting\n");
1186             return 0;
1187         }
1188     }
1189     if (!strcmp(argv[i], "-gamma") || !strcmp(argv[i], "-rgamma") ||
1190         !strcmp(argv[i], "-ggamma") || !strcmp(argv[i], "-bgamma")) {
1191         double gamma;
1192 
1193         CHECK_FOR_REQUIRED_ARGUMENT();
1194         if (sscanf(argv[++i], "%lf", &gamma) == 1) {
1195             if (gamma < GAMMA_MIN || gamma > GAMMA_MAX) {
1196                 ErrorF("gamma out of range, only  %.2f <= gamma_value <= %.1f"
1197                        " is valid\n", GAMMA_MIN, GAMMA_MAX);
1198                 return 0;
1199             }
1200             if (!strcmp(argv[i - 1], "-gamma"))
1201                 xf86Gamma.red = xf86Gamma.green = xf86Gamma.blue = gamma;
1202             else if (!strcmp(argv[i - 1], "-rgamma"))
1203                 xf86Gamma.red = gamma;
1204             else if (!strcmp(argv[i - 1], "-ggamma"))
1205                 xf86Gamma.green = gamma;
1206             else if (!strcmp(argv[i - 1], "-bgamma"))
1207                 xf86Gamma.blue = gamma;
1208             return 2;
1209         }
1210     }
1211     if (!strcmp(argv[i], "-layout")) {
1212         CHECK_FOR_REQUIRED_ARGUMENT();
1213         xf86LayoutName = argv[++i];
1214         return 2;
1215     }
1216     if (!strcmp(argv[i], "-screen")) {
1217         CHECK_FOR_REQUIRED_ARGUMENT();
1218         xf86ScreenName = argv[++i];
1219         return 2;
1220     }
1221     if (!strcmp(argv[i], "-pointer")) {
1222         CHECK_FOR_REQUIRED_ARGUMENT();
1223         xf86PointerName = argv[++i];
1224         return 2;
1225     }
1226     if (!strcmp(argv[i], "-keyboard")) {
1227         CHECK_FOR_REQUIRED_ARGUMENT();
1228         xf86KeyboardName = argv[++i];
1229         return 2;
1230     }
1231     if (!strcmp(argv[i], "-nosilk")) {
1232         xf86silkenMouseDisableFlag = TRUE;
1233         return 1;
1234     }
1235 #ifdef HAVE_ACPI
1236     if (!strcmp(argv[i], "-noacpi")) {
1237         xf86acpiDisableFlag = TRUE;
1238         return 1;
1239     }
1240 #endif
1241     if (!strcmp(argv[i], "-configure")) {
1242         if (getuid() != 0 && geteuid() == 0) {
1243             ErrorF("The '-configure' option can only be used by root.\n");
1244             exit(1);
1245         }
1246         xf86DoConfigure = TRUE;
1247         xf86AllowMouseOpenFail = TRUE;
1248         return 1;
1249     }
1250     if (!strcmp(argv[i], "-showopts")) {
1251         if (getuid() != 0 && geteuid() == 0) {
1252             ErrorF("The '-showopts' option can only be used by root.\n");
1253             exit(1);
1254         }
1255         xf86DoShowOptions = TRUE;
1256         return 1;
1257     }
1258 #ifdef XSERVER_LIBPCIACCESS
1259     if (!strcmp(argv[i], "-isolateDevice")) {
1260         CHECK_FOR_REQUIRED_ARGUMENT();
1261         if (strncmp(argv[++i], "PCI:", 4)) {
1262             FatalError("Bus types other than PCI not yet isolable\n");
1263         }
1264         xf86PciIsolateDevice(argv[i]);
1265         return 2;
1266     }
1267 #endif
1268     /* Notice cmdline xkbdir, but pass to dix as well */
1269     if (!strcmp(argv[i], "-xkbdir")) {
1270         xf86xkbdirFlag = TRUE;
1271         return 0;
1272     }
1273     if (!strcmp(argv[i], "-novtswitch")) {
1274         xf86Info.autoVTSwitch = FALSE;
1275         return 1;
1276     }
1277     if (!strcmp(argv[i], "-sharevts")) {
1278         xf86Info.ShareVTs = TRUE;
1279         return 1;
1280     }
1281     if (!strcmp(argv[i], "-iglx") || !strcmp(argv[i], "+iglx")) {
1282         xf86Info.iglxFrom = X_CMDLINE;
1283         return 0;
1284     }
1285 
1286     /* OS-specific processing */
1287     return xf86ProcessArgument(argc, argv, i);
1288 }
1289 
1290 /*
1291  * ddxUseMsg --
1292  *	Print out correct use of device dependent commandline options.
1293  *      Maybe the user now knows what really to do ...
1294  */
1295 
1296 void
ddxUseMsg(void)1297 ddxUseMsg(void)
1298 {
1299     ErrorF("\n");
1300     ErrorF("\n");
1301     ErrorF("Device Dependent Usage\n");
1302     if (!PrivsElevated()) {
1303         ErrorF("-modulepath paths      specify the module search path\n");
1304         ErrorF("-logfile file          specify a log file name\n");
1305         ErrorF("-configure             probe for devices and write an "
1306                XCONFIGFILE "\n");
1307         ErrorF
1308             ("-showopts              print available options for all installed drivers\n");
1309     }
1310     ErrorF
1311         ("-config file           specify a configuration file, relative to the\n");
1312     ErrorF("                       " XCONFIGFILE
1313            " search path, only root can use absolute\n");
1314     ErrorF
1315         ("-configdir dir         specify a configuration directory, relative to the\n");
1316     ErrorF("                       " XCONFIGDIR
1317            " search path, only root can use absolute\n");
1318     ErrorF("-verbose [n]           verbose startup messages\n");
1319     ErrorF("-logverbose [n]        verbose log messages\n");
1320     ErrorF("-quiet                 minimal startup messages\n");
1321     ErrorF("-fbbpp n               set bpp for the framebuffer. Default: 8\n");
1322     ErrorF("-depth n               set colour depth. Default: 8\n");
1323     ErrorF
1324         ("-gamma f               set gamma value (0.1 < f < 10.0) Default: 1.0\n");
1325     ErrorF("-rgamma f              set gamma value for red phase\n");
1326     ErrorF("-ggamma f              set gamma value for green phase\n");
1327     ErrorF("-bgamma f              set gamma value for blue phase\n");
1328     ErrorF
1329         ("-weight nnn            set RGB weighting at 16 bpp.  Default: 565\n");
1330     ErrorF("-layout name           specify the ServerLayout section name\n");
1331     ErrorF("-screen name           specify the Screen section name\n");
1332     ErrorF
1333         ("-keyboard name         specify the core keyboard InputDevice name\n");
1334     ErrorF
1335         ("-pointer name          specify the core pointer InputDevice name\n");
1336     ErrorF("-nosilk                disable Silken Mouse\n");
1337     ErrorF("-flipPixels            swap default black/white Pixel values\n");
1338 #ifdef XF86VIDMODE
1339     ErrorF("-disableVidMode        disable mode adjustments with xvidtune\n");
1340     ErrorF
1341         ("-allowNonLocalXvidtune allow xvidtune to be run as a non-local client\n");
1342 #endif
1343     ErrorF
1344         ("-allowMouseOpenFail    start server even if the mouse can't be initialized\n");
1345     ErrorF("-ignoreABI             make module ABI mismatches non-fatal\n");
1346 #ifdef XSERVER_LIBPCIACCESS
1347     ErrorF
1348         ("-isolateDevice bus_id  restrict device resets to bus_id (PCI only)\n");
1349 #endif
1350     ErrorF("-version               show the server version\n");
1351     ErrorF("-showDefaultModulePath show the server default module path\n");
1352     ErrorF("-showDefaultLibPath    show the server default library path\n");
1353     ErrorF
1354         ("-novtswitch            don't automatically switch VT at reset & exit\n");
1355     ErrorF("-sharevts              share VTs with another X server\n");
1356     /* OS-specific usage */
1357     xf86UseMsg();
1358     ErrorF("\n");
1359 }
1360 
1361 /*
1362  * xf86LoadModules iterates over a list that is being passed in.
1363  */
1364 Bool
xf86LoadModules(const char ** list,void ** optlist)1365 xf86LoadModules(const char **list, void **optlist)
1366 {
1367     int errmaj;
1368     void *opt;
1369     int i;
1370     char *name;
1371     Bool failed = FALSE;
1372 
1373     if (!list)
1374         return TRUE;
1375 
1376     for (i = 0; list[i] != NULL; i++) {
1377 
1378         /* Normalise the module name */
1379         name = xf86NormalizeName(list[i]);
1380 
1381         /* Skip empty names */
1382         if (name == NULL || *name == '\0') {
1383             free(name);
1384             continue;
1385         }
1386 
1387         /* Replace obsolete keyboard driver with kbd */
1388         if (!xf86NameCmp(name, "keyboard")) {
1389             strcpy(name, "kbd");
1390         }
1391 
1392         if (optlist)
1393             opt = optlist[i];
1394         else
1395             opt = NULL;
1396 
1397         if (!LoadModule(name, opt, NULL, &errmaj)) {
1398             LoaderErrorMsg(NULL, name, errmaj, 0);
1399             failed = TRUE;
1400         }
1401         free(name);
1402     }
1403     return !failed;
1404 }
1405 
1406 /* Pixmap format stuff */
1407 
1408 PixmapFormatPtr
xf86GetPixFormat(ScrnInfoPtr pScrn,int depth)1409 xf86GetPixFormat(ScrnInfoPtr pScrn, int depth)
1410 {
1411     int i;
1412 
1413     for (i = 0; i < numFormats; i++)
1414         if (formats[i].depth == depth)
1415             break;
1416     if (i != numFormats)
1417         return &formats[i];
1418     else if (!formatsDone) {
1419         /* Check for screen-specified formats */
1420         for (i = 0; i < pScrn->numFormats; i++)
1421             if (pScrn->formats[i].depth == depth)
1422                 break;
1423         if (i != pScrn->numFormats)
1424             return &pScrn->formats[i];
1425     }
1426     return NULL;
1427 }
1428 
1429 int
xf86GetBppFromDepth(ScrnInfoPtr pScrn,int depth)1430 xf86GetBppFromDepth(ScrnInfoPtr pScrn, int depth)
1431 {
1432     PixmapFormatPtr format;
1433 
1434     format = xf86GetPixFormat(pScrn, depth);
1435     if (format)
1436         return format->bitsPerPixel;
1437     else
1438         return 0;
1439 }
1440 
1441 #ifdef DDXBEFORERESET
1442 void
ddxBeforeReset(void)1443 ddxBeforeReset(void)
1444 {
1445 }
1446 #endif
1447 
1448 #if INPUTTHREAD
1449 /** This function is called in Xserver/os/inputthread.c when starting
1450     the input thread. */
1451 void
ddxInputThreadInit(void)1452 ddxInputThreadInit(void)
1453 {
1454     xf86OSInputThreadInit();
1455 }
1456 #endif
1457