main.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /***********************************************************
  2. Copyright 1987, 1998 The Open Group
  3. Permission to use, copy, modify, distribute, and sell this software and its
  4. documentation for any purpose is hereby granted without fee, provided that
  5. the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation.
  8. The above copyright notice and this permission notice shall be included in
  9. all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  14. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. Except as contained in this notice, the name of The Open Group shall not be
  17. used in advertising or otherwise to promote the sale, use or other dealings
  18. in this Software without prior written authorization from The Open Group.
  19. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
  20. All Rights Reserved
  21. Permission to use, copy, modify, and distribute this software and its
  22. documentation for any purpose and without fee is hereby granted,
  23. provided that the above copyright notice appear in all copies and that
  24. both that copyright notice and this permission notice appear in
  25. supporting documentation, and that the name of Digital not be
  26. used in advertising or publicity pertaining to distribution of the
  27. software without specific, written prior permission.
  28. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  29. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  30. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  31. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  32. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  33. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  34. SOFTWARE.
  35. ******************************************************************/
  36. /* The panoramix components contained the following notice */
  37. /*****************************************************************
  38. Copyright (c) 1991, 1997 Digital Equipment Corporation, Maynard, Massachusetts.
  39. Permission is hereby granted, free of charge, to any person obtaining a copy
  40. of this software and associated documentation files (the "Software"), to deal
  41. in the Software without restriction, including without limitation the rights
  42. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  43. copies of the Software.
  44. The above copyright notice and this permission notice shall be included in
  45. all copies or substantial portions of the Software.
  46. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  47. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  48. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  49. DIGITAL EQUIPMENT CORPORATION BE LIABLE FOR ANY CLAIM, DAMAGES, INCLUDING,
  50. BUT NOT LIMITED TO CONSEQUENTIAL OR INCIDENTAL DAMAGES, OR OTHER LIABILITY,
  51. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
  52. IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  53. Except as contained in this notice, the name of Digital Equipment Corporation
  54. shall not be used in advertising or otherwise to promote the sale, use or other
  55. dealings in this Software without prior written authorization from Digital
  56. Equipment Corporation.
  57. ******************************************************************/
  58. #ifdef HAVE_DIX_CONFIG_H
  59. #include <dix-config.h>
  60. #endif
  61. #include <X11/X.h>
  62. #include <X11/Xos.h> /* for unistd.h */
  63. #include <X11/Xproto.h>
  64. #include "scrnintstr.h"
  65. #include "misc.h"
  66. #include "os.h"
  67. #include "windowstr.h"
  68. #include "resource.h"
  69. #include "dixstruct.h"
  70. #include "gcstruct.h"
  71. #include "extension.h"
  72. #include "colormap.h"
  73. #include "colormapst.h"
  74. #include "cursorstr.h"
  75. #include <X11/fonts/font.h>
  76. #include "opaque.h"
  77. #include "servermd.h"
  78. #include "site.h"
  79. #include "dixfont.h"
  80. #include "extnsionst.h"
  81. #include "dixevents.h" /* InitEvents() */
  82. #ifdef DPMSExtension
  83. #define DPMS_SERVER
  84. #include <X11/extensions/dpms.h>
  85. #include "dpmsproc.h"
  86. #endif
  87. extern int InitClientPrivates(ClientPtr client);
  88. extern void Dispatch(void);
  89. char *ConnectionInfo;
  90. xConnSetupPrefix connSetupPrefix;
  91. extern FontPtr defaultFont;
  92. extern int screenPrivateCount;
  93. extern Bool CreateGCperDepthArray(void);
  94. static
  95. Bool CreateConnectionBlock(void);
  96. static void FreeScreen(ScreenPtr);
  97. _X_EXPORT PaddingInfo PixmapWidthPaddingInfo[33];
  98. int connBlockScreenStart;
  99. _X_EXPORT void
  100. NotImplemented(xEvent *from, xEvent *to)
  101. {
  102. FatalError("Not implemented");
  103. }
  104. /*
  105. * Dummy entry for ReplySwapVector[]
  106. */
  107. void
  108. ReplyNotSwappd(ClientPtr pClient, int size, void *pbuf)
  109. {
  110. FatalError("Not implemented");
  111. }
  112. /*
  113. * This array encodes the answer to the question "what is the log base 2
  114. * of the number of pixels that fit in a scanline pad unit?"
  115. * Note that ~0 is an invalid entry (mostly for the benefit of the reader).
  116. */
  117. static int answer[6][4] = {
  118. /* pad pad pad pad */
  119. /* 8 16 32 64 */
  120. {3, 4, 5, 6}, /* 1 bit per pixel */
  121. {1, 2, 3, 4}, /* 4 bits per pixel */
  122. {0, 1, 2, 3}, /* 8 bits per pixel */
  123. {~0, 0, 1, 2}, /* 16 bits per pixel */
  124. {~0, ~0, 0, 1}, /* 24 bits per pixel */
  125. {~0, ~0, 0, 1} /* 32 bits per pixel */
  126. };
  127. /*
  128. * This array gives the answer to the question "what is the first index for
  129. * the answer array above given the number of bits per pixel?"
  130. * Note that ~0 is an invalid entry (mostly for the benefit of the reader).
  131. */
  132. static int indexForBitsPerPixel[33] = {
  133. ~0, 0, ~0, ~0, /* 1 bit per pixel */
  134. 1, ~0, ~0, ~0, /* 4 bits per pixel */
  135. 2, ~0, ~0, ~0, /* 8 bits per pixel */
  136. ~0, ~0, ~0, ~0,
  137. 3, ~0, ~0, ~0, /* 16 bits per pixel */
  138. ~0, ~0, ~0, ~0,
  139. 4, ~0, ~0, ~0, /* 24 bits per pixel */
  140. ~0, ~0, ~0, ~0,
  141. 5 /* 32 bits per pixel */
  142. };
  143. /*
  144. * This array gives the bytesperPixel value for cases where the number
  145. * of bits per pixel is a multiple of 8 but not a power of 2.
  146. */
  147. static int answerBytesPerPixel[33] = {
  148. ~0, 0, ~0, ~0, /* 1 bit per pixel */
  149. 0, ~0, ~0, ~0, /* 4 bits per pixel */
  150. 0, ~0, ~0, ~0, /* 8 bits per pixel */
  151. ~0, ~0, ~0, ~0,
  152. 0, ~0, ~0, ~0, /* 16 bits per pixel */
  153. ~0, ~0, ~0, ~0,
  154. 3, ~0, ~0, ~0, /* 24 bits per pixel */
  155. ~0, ~0, ~0, ~0,
  156. 0 /* 32 bits per pixel */
  157. };
  158. /*
  159. * This array gives the answer to the question "what is the second index for
  160. * the answer array above given the number of bits per scanline pad unit?"
  161. * Note that ~0 is an invalid entry (mostly for the benefit of the reader).
  162. */
  163. static int indexForScanlinePad[65] = {
  164. ~0, ~0, ~0, ~0,
  165. ~0, ~0, ~0, ~0,
  166. 0, ~0, ~0, ~0, /* 8 bits per scanline pad unit */
  167. ~0, ~0, ~0, ~0,
  168. 1, ~0, ~0, ~0, /* 16 bits per scanline pad unit */
  169. ~0, ~0, ~0, ~0,
  170. ~0, ~0, ~0, ~0,
  171. ~0, ~0, ~0, ~0,
  172. 2, ~0, ~0, ~0, /* 32 bits per scanline pad unit */
  173. ~0, ~0, ~0, ~0,
  174. ~0, ~0, ~0, ~0,
  175. ~0, ~0, ~0, ~0,
  176. ~0, ~0, ~0, ~0,
  177. ~0, ~0, ~0, ~0,
  178. ~0, ~0, ~0, ~0,
  179. ~0, ~0, ~0, ~0,
  180. 3 /* 64 bits per scanline pad unit */
  181. };
  182. #ifndef MIN
  183. #define MIN(a,b) (((a) < (b)) ? (a) : (b))
  184. #endif
  185. int
  186. main(int argc, char *argv[], char *envp[])
  187. {
  188. int i;
  189. char *xauthfile;
  190. HWEventQueueType alwaysCheckForInput[2];
  191. display = "0";
  192. /* Quartz support on Mac OS X requires that the Cocoa event loop be in
  193. * the main thread. This allows the X server main to be called again
  194. * from another thread. */
  195. CheckUserParameters(argc, argv, envp);
  196. CheckUserAuthorization();
  197. InitConnectionLimits();
  198. /* prep X authority file from environment; this can be overriden by a
  199. * command line option */
  200. xauthfile = getenv("XAUTHORITY");
  201. if (xauthfile)
  202. InitAuthorization(xauthfile);
  203. ProcessCommandLine(argc, argv);
  204. alwaysCheckForInput[0] = 0;
  205. alwaysCheckForInput[1] = 1;
  206. while (1) {
  207. serverGeneration++;
  208. ScreenSaverTime = defaultScreenSaverTime;
  209. ScreenSaverInterval = defaultScreenSaverInterval;
  210. ScreenSaverBlanking = defaultScreenSaverBlanking;
  211. ScreenSaverAllowExposures = defaultScreenSaverAllowExposures;
  212. #ifdef DPMSExtension
  213. DPMSStandbyTime = DEFAULT_SCREEN_SAVER_TIME;
  214. DPMSSuspendTime = DEFAULT_SCREEN_SAVER_TIME;
  215. DPMSOffTime = DEFAULT_SCREEN_SAVER_TIME;
  216. DPMSEnabled = TRUE;
  217. DPMSPowerLevel = 0;
  218. #endif
  219. InitBlockAndWakeupHandlers();
  220. /* Perform any operating system dependent initializations you'd like */
  221. OsInit();
  222. if (serverGeneration == 1) {
  223. CreateWellKnownSockets();
  224. for (i = 1; i < MAXCLIENTS; i++)
  225. clients[i] = NullClient;
  226. serverClient = malloc(sizeof(ClientRec));
  227. if (!serverClient)
  228. FatalError("couldn't create server client");
  229. InitClient(serverClient, 0, (pointer) NULL);
  230. }
  231. else
  232. ResetWellKnownSockets();
  233. clients[0] = serverClient;
  234. currentMaxClients = 1;
  235. if (!InitClientResources(serverClient)) /* for root resources */
  236. FatalError("couldn't init server resources");
  237. SetInputCheck(&alwaysCheckForInput[0], &alwaysCheckForInput[1]);
  238. screenInfo.numScreens = 0;
  239. InitAtoms();
  240. InitEvents();
  241. InitGlyphCaching();
  242. ResetExtensionPrivates();
  243. ResetClientPrivates();
  244. ResetScreenPrivates();
  245. ResetWindowPrivates();
  246. ResetGCPrivates();
  247. ResetPixmapPrivates();
  248. ResetColormapPrivates();
  249. ResetFontPrivateIndex();
  250. ResetDevicePrivateIndex();
  251. InitCallbackManager();
  252. InitVisualWrap();
  253. InitOutput(&screenInfo, argc, argv);
  254. if (screenInfo.numScreens < 1)
  255. FatalError("no screens found");
  256. InitExtensions(argc, argv);
  257. if (!InitClientPrivates(serverClient))
  258. FatalError("failed to allocate serverClient devprivates");
  259. for (i = 0; i < screenInfo.numScreens; i++) {
  260. ScreenPtr pScreen = screenInfo.screens[i];
  261. if (!CreateScratchPixmapsForScreen(i))
  262. FatalError("failed to create scratch pixmaps");
  263. if (pScreen->CreateScreenResources &&
  264. !(*pScreen->CreateScreenResources) (pScreen))
  265. FatalError("failed to create screen resources");
  266. if (!CreateGCperDepth(i))
  267. FatalError("failed to create scratch GCs");
  268. if (!CreateDefaultStipple(i))
  269. FatalError("failed to create default stipple");
  270. if (!CreateRootWindow(pScreen))
  271. FatalError("failed to create root window");
  272. }
  273. InitInput(argc, argv);
  274. if (InitAndStartDevices() != Success)
  275. FatalError("failed to initialize core devices");
  276. InitFonts();
  277. if (SetDefaultFontPath(defaultFontPath) != Success)
  278. ErrorF("failed to set default font path '%s'", defaultFontPath);
  279. if (!SetDefaultFont(defaultTextFont))
  280. FatalError("could not open default font '%s'", defaultTextFont);
  281. if (!(rootCursor = CreateRootCursor(defaultCursorFont, 0)))
  282. FatalError("could not open default cursor font '%s'",
  283. defaultCursorFont);
  284. #ifdef DPMSExtension
  285. /* check all screens, looking for DPMS Capabilities */
  286. DPMSCapableFlag = DPMSSupported();
  287. if (!DPMSCapableFlag)
  288. DPMSEnabled = FALSE;
  289. #endif
  290. for (i = 0; i < screenInfo.numScreens; i++)
  291. InitRootWindow(WindowTable[i]);
  292. DefineInitialRootWindow(WindowTable[0]);
  293. SaveScreens(SCREEN_SAVER_FORCER, ScreenSaverReset);
  294. {
  295. if (!CreateConnectionBlock())
  296. FatalError("could not create connection block info");
  297. }
  298. Dispatch();
  299. /* Now free up whatever must be freed */
  300. if (screenIsSaved == SCREEN_SAVER_ON)
  301. SaveScreens(SCREEN_SAVER_OFF, ScreenSaverReset);
  302. FreeScreenSaverTimer();
  303. CloseDownExtensions();
  304. FreeAllResources();
  305. memset(WindowTable, 0, sizeof(WindowTable));
  306. CloseDownDevices();
  307. for (i = screenInfo.numScreens - 1; i >= 0; i--) {
  308. FreeScratchPixmapsForScreen(i);
  309. FreeGCperDepth(i);
  310. FreeDefaultStipple(i);
  311. (*screenInfo.screens[i]->CloseScreen) (i, screenInfo.screens[i]);
  312. FreeScreen(screenInfo.screens[i]);
  313. screenInfo.numScreens = i;
  314. }
  315. CloseDownEvents();
  316. FreeFonts();
  317. free(serverClient->devPrivates);
  318. serverClient->devPrivates = NULL;
  319. if (dispatchException & DE_TERMINATE) {
  320. CloseWellKnownConnections();
  321. }
  322. OsCleanup((dispatchException & DE_TERMINATE) != 0);
  323. if (dispatchException & DE_TERMINATE) {
  324. ddxGiveUp();
  325. break;
  326. }
  327. free(ConnectionInfo);
  328. ConnectionInfo = NULL;
  329. }
  330. return (0);
  331. }
  332. static const int VendorRelease = VENDOR_RELEASE;
  333. static const char * const VendorString = VENDOR_STRING;
  334. static const int padlength[4] = { 0, 3, 2, 1 };
  335. static
  336. Bool
  337. CreateConnectionBlock()
  338. {
  339. xConnSetup setup;
  340. xWindowRoot root;
  341. xDepth depth;
  342. xVisualType visual;
  343. xPixmapFormat format;
  344. unsigned long vid;
  345. int i, j, k, lenofblock, sizesofar = 0;
  346. char *pBuf;
  347. memset(&setup, 0, sizeof(xConnSetup));
  348. /* Leave off the ridBase and ridMask, these must be sent with
  349. connection */
  350. setup.release = VendorRelease;
  351. /*
  352. * per-server image and bitmap parameters are defined in Xmd.h
  353. */
  354. setup.imageByteOrder = screenInfo.imageByteOrder;
  355. setup.bitmapScanlineUnit = screenInfo.bitmapScanlineUnit;
  356. setup.bitmapScanlinePad = screenInfo.bitmapScanlinePad;
  357. setup.bitmapBitOrder = screenInfo.bitmapBitOrder;
  358. setup.motionBufferSize = NumMotionEvents();
  359. setup.numRoots = screenInfo.numScreens;
  360. setup.nbytesVendor = strlen(VendorString);
  361. setup.numFormats = screenInfo.numPixmapFormats;
  362. setup.maxRequestSize = MAX_REQUEST_SIZE;
  363. QueryMinMaxKeyCodes(&setup.minKeyCode, &setup.maxKeyCode);
  364. lenofblock = sizeof(xConnSetup) +
  365. ((setup.nbytesVendor + 3) & ~3) +
  366. (setup.numFormats * sizeof(xPixmapFormat)) +
  367. (setup.numRoots * sizeof(xWindowRoot));
  368. ConnectionInfo = malloc(lenofblock);
  369. if (!ConnectionInfo)
  370. return FALSE;
  371. memmove(ConnectionInfo, (char *) &setup, sizeof(xConnSetup));
  372. sizesofar = sizeof(xConnSetup);
  373. pBuf = ConnectionInfo + sizeof(xConnSetup);
  374. memmove(pBuf, VendorString, (int) setup.nbytesVendor);
  375. sizesofar += setup.nbytesVendor;
  376. pBuf += setup.nbytesVendor;
  377. i = padlength[setup.nbytesVendor & 3];
  378. sizesofar += i;
  379. while (--i >= 0)
  380. *pBuf++ = 0;
  381. memset(&format, 0, sizeof(xPixmapFormat));
  382. for (i = 0; i < screenInfo.numPixmapFormats; i++) {
  383. format.depth = screenInfo.formats[i].depth;
  384. format.bitsPerPixel = screenInfo.formats[i].bitsPerPixel;
  385. format.scanLinePad = screenInfo.formats[i].scanlinePad;
  386. memmove(pBuf, (char *) &format, sizeof(xPixmapFormat));
  387. pBuf += sizeof(xPixmapFormat);
  388. sizesofar += sizeof(xPixmapFormat);
  389. }
  390. connBlockScreenStart = sizesofar;
  391. memset(&depth, 0, sizeof(xDepth));
  392. memset(&visual, 0, sizeof(xVisualType));
  393. for (i = 0; i < screenInfo.numScreens; i++) {
  394. ScreenPtr pScreen;
  395. DepthPtr pDepth;
  396. VisualPtr pVisual;
  397. pScreen = screenInfo.screens[i];
  398. root.windowId = WindowTable[i]->drawable.id;
  399. root.defaultColormap = pScreen->defColormap;
  400. root.whitePixel = pScreen->whitePixel;
  401. root.blackPixel = pScreen->blackPixel;
  402. root.currentInputMask = 0; /* filled in when sent */
  403. root.pixWidth = pScreen->width;
  404. root.pixHeight = pScreen->height;
  405. root.mmWidth = pScreen->mmWidth;
  406. root.mmHeight = pScreen->mmHeight;
  407. root.minInstalledMaps = pScreen->minInstalledCmaps;
  408. root.maxInstalledMaps = pScreen->maxInstalledCmaps;
  409. root.rootVisualID = pScreen->rootVisual;
  410. root.backingStore = pScreen->backingStoreSupport;
  411. root.saveUnders = FALSE;
  412. root.rootDepth = pScreen->rootDepth;
  413. root.nDepths = pScreen->numDepths;
  414. memmove(pBuf, (char *) &root, sizeof(xWindowRoot));
  415. sizesofar += sizeof(xWindowRoot);
  416. pBuf += sizeof(xWindowRoot);
  417. pDepth = pScreen->allowedDepths;
  418. for (j = 0; j < pScreen->numDepths; j++, pDepth++) {
  419. lenofblock += sizeof(xDepth) +
  420. (pDepth->numVids * sizeof(xVisualType));
  421. pBuf = (char *) realloc(ConnectionInfo, lenofblock);
  422. if (!pBuf) {
  423. free(ConnectionInfo);
  424. return FALSE;
  425. }
  426. ConnectionInfo = pBuf;
  427. pBuf += sizesofar;
  428. depth.depth = pDepth->depth;
  429. depth.nVisuals = pDepth->numVids;
  430. memmove(pBuf, (char *) &depth, sizeof(xDepth));
  431. pBuf += sizeof(xDepth);
  432. sizesofar += sizeof(xDepth);
  433. for (k = 0; k < pDepth->numVids; k++) {
  434. vid = pDepth->vids[k];
  435. for (pVisual = pScreen->visuals;
  436. pVisual->vid != vid; pVisual++);
  437. visual.visualID = vid;
  438. visual.class = pVisual->class;
  439. visual.bitsPerRGB = pVisual->bitsPerRGBValue;
  440. visual.colormapEntries = pVisual->ColormapEntries;
  441. visual.redMask = pVisual->redMask;
  442. visual.greenMask = pVisual->greenMask;
  443. visual.blueMask = pVisual->blueMask;
  444. memmove(pBuf, (char *) &visual, sizeof(xVisualType));
  445. pBuf += sizeof(xVisualType);
  446. sizesofar += sizeof(xVisualType);
  447. }
  448. }
  449. }
  450. connSetupPrefix.success = xTrue;
  451. connSetupPrefix.length = lenofblock / 4;
  452. connSetupPrefix.majorVersion = X_PROTOCOL;
  453. connSetupPrefix.minorVersion = X_PROTOCOL_REVISION;
  454. return TRUE;
  455. }
  456. /*
  457. grow the array of screenRecs if necessary.
  458. call the device-supplied initialization procedure
  459. with its screen number, a pointer to its ScreenRec, argc, and argv.
  460. return the number of successfully installed screens.
  461. */
  462. int
  463. AddScreen(Bool (*pfnInit) (int /*index */ ,
  464. ScreenPtr /*pScreen */ ,
  465. int /*argc */ ,
  466. char ** /*argv */
  467. ), int argc, char **argv)
  468. {
  469. int i;
  470. int scanlinepad, format, depth, bitsPerPixel, j, k;
  471. ScreenPtr pScreen;
  472. #ifdef DEBUG
  473. void (**jNI) ();
  474. #endif /* DEBUG */
  475. i = screenInfo.numScreens;
  476. if (i == MAXSCREENS)
  477. return -1;
  478. pScreen = calloc(1, sizeof(ScreenRec));
  479. if (!pScreen)
  480. return -1;
  481. pScreen->devPrivates = calloc(sizeof(DevUnion),
  482. screenPrivateCount);
  483. if (!pScreen->devPrivates && screenPrivateCount) {
  484. free(pScreen);
  485. return -1;
  486. }
  487. pScreen->myNum = i;
  488. pScreen->WindowPrivateLen = 0;
  489. pScreen->WindowPrivateSizes = (unsigned *) NULL;
  490. pScreen->totalWindowSize =
  491. ((sizeof(WindowRec) + sizeof(long) - 1) / sizeof(long)) * sizeof(long);
  492. pScreen->GCPrivateLen = 0;
  493. pScreen->GCPrivateSizes = (unsigned *) NULL;
  494. pScreen->totalGCSize =
  495. ((sizeof(GC) + sizeof(long) - 1) / sizeof(long)) * sizeof(long);
  496. pScreen->PixmapPrivateLen = 0;
  497. pScreen->PixmapPrivateSizes = (unsigned *) NULL;
  498. pScreen->totalPixmapSize = BitmapBytePad(sizeof(PixmapRec) * 8);
  499. pScreen->ClipNotify = 0; /* for R4 ddx compatibility */
  500. pScreen->CreateScreenResources = 0;
  501. #ifdef DEBUG
  502. for (jNI = &pScreen->QueryBestSize;
  503. jNI < (void (**)()) &pScreen->SendGraphicsExpose; jNI++)
  504. *jNI = NotImplemented;
  505. #endif /* DEBUG */
  506. /*
  507. * This loop gets run once for every Screen that gets added,
  508. * but thats ok. If the ddx layer initializes the formats
  509. * one at a time calling AddScreen() after each, then each
  510. * iteration will make it a little more accurate. Worst case
  511. * we do this loop N * numPixmapFormats where N is # of screens.
  512. * Anyway, this must be called after InitOutput and before the
  513. * screen init routine is called.
  514. */
  515. for (format = 0; format < screenInfo.numPixmapFormats; format++) {
  516. depth = screenInfo.formats[format].depth;
  517. bitsPerPixel = screenInfo.formats[format].bitsPerPixel;
  518. scanlinepad = screenInfo.formats[format].scanlinePad;
  519. j = indexForBitsPerPixel[bitsPerPixel];
  520. k = indexForScanlinePad[scanlinepad];
  521. PixmapWidthPaddingInfo[depth].padPixelsLog2 = answer[j][k];
  522. PixmapWidthPaddingInfo[depth].padRoundUp =
  523. (scanlinepad / bitsPerPixel) - 1;
  524. j = indexForBitsPerPixel[8]; /* bits per byte */
  525. PixmapWidthPaddingInfo[depth].padBytesLog2 = answer[j][k];
  526. PixmapWidthPaddingInfo[depth].bitsPerPixel = bitsPerPixel;
  527. if (answerBytesPerPixel[bitsPerPixel]) {
  528. PixmapWidthPaddingInfo[depth].notPower2 = 1;
  529. PixmapWidthPaddingInfo[depth].bytesPerPixel =
  530. answerBytesPerPixel[bitsPerPixel];
  531. }
  532. else {
  533. PixmapWidthPaddingInfo[depth].notPower2 = 0;
  534. }
  535. }
  536. /* This is where screen specific stuff gets initialized. Load the
  537. screen structure, call the hardware, whatever.
  538. This is also where the default colormap should be allocated and
  539. also pixel values for blackPixel, whitePixel, and the cursor
  540. Note that InitScreen is NOT allowed to modify argc, argv, or
  541. any of the strings pointed to by argv. They may be passed to
  542. multiple screens.
  543. */
  544. pScreen->rgf = ~0L; /* there are no scratch GCs yet */
  545. WindowTable[i] = NullWindow;
  546. screenInfo.screens[i] = pScreen;
  547. screenInfo.numScreens++;
  548. if (!(*pfnInit) (i, pScreen, argc, argv)) {
  549. FreeScreen(pScreen);
  550. screenInfo.numScreens--;
  551. return -1;
  552. }
  553. return i;
  554. }
  555. static void
  556. FreeScreen(ScreenPtr pScreen)
  557. {
  558. free(pScreen->WindowPrivateSizes);
  559. free(pScreen->GCPrivateSizes);
  560. free(pScreen->PixmapPrivateSizes);
  561. free(pScreen->devPrivates);
  562. free(pScreen);
  563. }