home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / ddx / sun / sunInit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-23  |  20.8 KB  |  778 lines

  1. /* $XConsortium: sunInit.c,v 5.24 91/08/23 16:14:53 keith Exp $ */
  2. /*-
  3.  * sunInit.c --
  4.  *    Initialization functions for screen/keyboard/mouse, etc.
  5.  *
  6.  * Copyright (c) 1987 by the Regents of the University of California
  7.  *
  8.  * Permission to use, copy, modify, and distribute this
  9.  * software and its documentation for any purpose and without
  10.  * fee is hereby granted, provided that the above copyright
  11.  * notice appear in all copies.  The University of California
  12.  * makes no representations about the suitability of this
  13.  * software for any purpose.  It is provided "as is" without
  14.  * express or implied warranty.
  15.  *
  16.  *
  17.  */
  18.  
  19. /************************************************************
  20. Copyright 1987 by Sun Microsystems, Inc. Mountain View, CA.
  21.  
  22.                     All Rights Reserved
  23.  
  24. Permission  to  use,  copy,  modify,  and  distribute   this
  25. software  and  its documentation for any purpose and without
  26. fee is hereby granted, provided that the above copyright no-
  27. tice  appear  in all copies and that both that copyright no-
  28. tice and this permission notice appear in  supporting  docu-
  29. mentation,  and  that the names of Sun or MIT not be used in
  30. advertising or publicity pertaining to distribution  of  the
  31. software  without specific prior written permission. Sun and
  32. M.I.T. make no representations about the suitability of this
  33. software for any purpose. It is provided "as is" without any
  34. express or implied warranty.
  35.  
  36. SUN DISCLAIMS ALL WARRANTIES WITH REGARD TO  THIS  SOFTWARE,
  37. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-
  38. NESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SUN BE  LI-
  39. ABLE  FOR  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  40. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,  DATA  OR
  41. PROFITS,  WHETHER  IN  AN  ACTION OF CONTRACT, NEGLIGENCE OR
  42. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
  43. THE USE OR PERFORMANCE OF THIS SOFTWARE.
  44.  
  45. ********************************************************/
  46.  
  47. #ifndef    lint
  48. static char sccsid[] = "%W %G Copyright 1987 Sun Micro";
  49. #endif
  50.  
  51. #include    "sun.h"
  52. #include    <servermd.h>
  53. #include    "dixstruct.h"
  54. #include    "dix.h"
  55. #include    "opaque.h"
  56. #include    "mipointer.h"
  57.  
  58. extern int sunMouseProc();
  59. extern int sunKbdProc();
  60. extern Bool sunBW2Probe(), sunBW2Create();
  61. #ifndef MONO_ONLY
  62. extern Bool sunCG2CProbe(), sunCG2CCreate();
  63. extern Bool sunCG3CProbe(), sunCG3CCreate();
  64. extern Bool sunCG4CProbe(), sunCG4CCreate();
  65. #ifdef FBTYPE_SUNFAST_COLOR /* doesn't exist in sunos3.x */
  66. extern Bool sunCG6CProbe(), sunCG6CCreate();
  67. #endif
  68. #endif
  69. extern void ProcessInputEvents();
  70.  
  71. extern void SetInputCheck();
  72. extern char *strncpy();
  73. extern GCPtr CreateScratchGC();
  74.  
  75. #define    XDEVICE    "XDEVICE"
  76. #define    PARENT    "WINDOW_GFX"
  77.  
  78. static int autoRepeatHandlersInstalled;    /* FALSE each time InitOutput called */
  79.  
  80. static Bool sunDevsProbed = FALSE;
  81. Bool sunSupportsDepth8 = FALSE;
  82. unsigned long sunGeneration = 0;
  83. int sunScreenIndex;
  84. Bool FlipPixels = FALSE;
  85.  
  86.  
  87. /*-
  88.  *-----------------------------------------------------------------------
  89.  * SigIOHandler --
  90.  *    Signal handler for SIGIO - input is available.
  91.  *
  92.  * Results:
  93.  *    sunSigIO is set - ProcessInputEvents() will be called soon.
  94.  *
  95.  * Side Effects:
  96.  *    None
  97.  *
  98.  *-----------------------------------------------------------------------
  99.  */
  100. /*ARGSUSED*/
  101. static void
  102. SigIOHandler(sig, code, scp)
  103.     int        code;
  104.     int        sig;
  105.     struct sigcontext *scp;
  106. {
  107.     sunEnqueueEvents ();
  108. }
  109.  
  110. sunFbDataRec sunFbData[] = {
  111. #ifndef MONO_ONLY
  112.     sunBW2Probe,      "/dev/bwtwo0",        sunBW2Create,
  113.     sunCG3CProbe,      "/dev/cgthree0",    sunCG3CCreate,
  114. #ifdef FBTYPE_SUNFAST_COLOR
  115.     sunCG6CProbe,    "/dev/cgsix0",        sunCG6CCreate,
  116. #endif
  117.     sunCG2CProbe,      "/dev/cgtwo0",        sunCG2CCreate,
  118.     sunCG4CProbe,      "/dev/cgfour0",        sunCG4CCreate,
  119. #endif
  120.     sunBW2Probe,      "/dev/bwtwo0",        sunBW2Create,
  121. };
  122.  
  123. #ifdef MONO_ONLY
  124. #define DEV_START   0
  125. #define DEV_END        0
  126. #else
  127. #define DEV_START   1
  128. #define DEV_END        1
  129. #endif
  130.  
  131. /*
  132.  * NUMSCREENS is the number of supported frame buffers (i.e. the number of
  133.  * structures in sunFbData which have an actual probeProc).
  134.  */
  135. #define NUMSCREENS (sizeof(sunFbData)/sizeof(sunFbData[0]))
  136. #define NUMDEVICES  2
  137.  
  138. fbFd sunFbs[NUMDEVICES];
  139.  
  140. static PixmapFormatRec    formats[] = {
  141.     1, 1, BITMAP_SCANLINE_PAD,    /* 1-bit deep */
  142.     8, 8, BITMAP_SCANLINE_PAD,    /* 8-bit deep */
  143. };
  144. #define NUMFORMATS    (sizeof formats)/(sizeof formats[0])
  145.  
  146. /*-
  147.  *-----------------------------------------------------------------------
  148.  * sunNonBlockConsoleOff --
  149.  *    Turn non-blocking mode on the console off, so you don't get logged
  150.  *    out when the server exits.
  151.  *
  152.  * Results:
  153.  *    None.
  154.  *
  155.  * Side Effects:
  156.  *    None.
  157.  *
  158.  *-----------------------------------------------------------------------
  159.  */
  160. /*ARGSUSED*/
  161. sunNonBlockConsoleOff(arg)
  162.     char    *arg;
  163. {
  164.     register int i;
  165.  
  166.     i = fcntl(2, F_GETFL, 0);
  167.     if (i >= 0)
  168.     (void) fcntl(2, F_SETFL, i & ~FNDELAY);
  169. }
  170.  
  171. /*-
  172.  *-----------------------------------------------------------------------
  173.  * InitOutput --
  174.  *    Initialize screenInfo for all actually accessible framebuffers.
  175.  *    The
  176.  *
  177.  * Results:
  178.  *    screenInfo init proc field set
  179.  *
  180.  * Side Effects:
  181.  *    None
  182.  *
  183.  *-----------------------------------------------------------------------
  184.  */
  185.  
  186. InitOutput(pScreenInfo, argc, argv)
  187.     ScreenInfo       *pScreenInfo;
  188.     int           argc;
  189.     char          **argv;
  190. {
  191.     int           i, n, dev;
  192.     int          nonBlockConsole = 0;
  193.     static int      setup_on_exit = 0;
  194.     int          devStart = DEV_START;
  195.     extern Bool      RunFromSmartParent;
  196.  
  197.     if (!monitorResolution)
  198.     monitorResolution = 90;
  199.     if (RunFromSmartParent)
  200.     nonBlockConsole = 1;
  201.     for (i = 1; i < argc; i++) {
  202.     if (!strcmp(argv[i],"-debug"))
  203.         nonBlockConsole = 0;
  204.     else if (!strcmp(argv[i],"-mono"))
  205.         devStart = 0;
  206.     }
  207.     /*
  208.      *    Writes to /dev/console can block - causing an
  209.      *    excess of error messages to hang the server in
  210.      *    deadlock.  So.......
  211.      */
  212.     if (nonBlockConsole) {
  213.     if (!setup_on_exit) {
  214.         if (on_exit(sunNonBlockConsoleOff, (char *)0))
  215.         ErrorF("InitOutput: can't register NBIO exit handler\n");
  216.         setup_on_exit = 1;
  217.     }
  218.     i = fcntl(2, F_GETFL, 0);
  219.     if (i >= 0)
  220.         i = fcntl(2, F_SETFL, i | FNDELAY);
  221.     if (i < 0) {
  222.         perror("fcntl");
  223.         ErrorF("InitOutput: can't put stderr in non-block mode\n");
  224.     }
  225.     }
  226.     pScreenInfo->imageByteOrder = IMAGE_BYTE_ORDER;
  227.     pScreenInfo->bitmapScanlineUnit = BITMAP_SCANLINE_UNIT;
  228.     pScreenInfo->bitmapScanlinePad = BITMAP_SCANLINE_PAD;
  229.     pScreenInfo->bitmapBitOrder = BITMAP_BIT_ORDER;
  230.  
  231.     pScreenInfo->numPixmapFormats = NUMFORMATS;
  232.     for (i=0; i< NUMFORMATS; i++)
  233.         pScreenInfo->formats[i] = formats[i];
  234.  
  235.     autoRepeatHandlersInstalled = FALSE;
  236.  
  237.     if (!sunDevsProbed)
  238.     {
  239.     n = 0;
  240.     for (i = NUMSCREENS, dev = devStart; --i >= DEV_END; dev++) {
  241.         if ((*sunFbData[dev].probeProc)(pScreenInfo, n, dev, argc, argv))
  242.         n++;
  243.         else
  244.         sunFbData[dev].createProc = NULL;
  245.     }
  246.     sunDevsProbed = TRUE;
  247.     if (n == 0)
  248.         return;
  249.     }
  250.     if (!sunSupportsDepth8)
  251.     pScreenInfo->numPixmapFormats--;
  252.     for (i = NUMSCREENS, dev = devStart; --i >= DEV_END; dev++) {
  253.     if (sunFbData[dev].createProc)
  254.         (*sunFbData[dev].createProc)(pScreenInfo, argc, argv);
  255.     }
  256.     sunInitCursor();
  257.     signal(SIGWINCH, SIG_IGN);
  258. }
  259.  
  260. /*-
  261.  *-----------------------------------------------------------------------
  262.  * InitInput --
  263.  *    Initialize all supported input devices...what else is there
  264.  *    besides pointer and keyboard?
  265.  *
  266.  * Results:
  267.  *    None.
  268.  *
  269.  * Side Effects:
  270.  *    Two DeviceRec's are allocated and registered as the system pointer
  271.  *    and keyboard devices.
  272.  *
  273.  *-----------------------------------------------------------------------
  274.  */
  275. /*ARGSUSED*/
  276. InitInput(argc, argv)
  277.     int           argc;
  278.     char          **argv;
  279. {
  280.     DevicePtr p, k;
  281.     static int  zero = 0;
  282.     
  283.     p = AddInputDevice(sunMouseProc, TRUE);
  284.     k = AddInputDevice(sunKbdProc, TRUE);
  285.     if (!p || !k)
  286.     FatalError("failed to create input devices in InitInput");
  287.  
  288.     SetTimeSinceLastInputEvent();
  289.     RegisterPointerDevice(p);
  290.     RegisterKeyboardDevice(k);
  291.     miRegisterPointerDevice(screenInfo.screens[0], p);
  292.     if (!mieqInit (k, p))
  293.     return FALSE;
  294.     signal(SIGIO, SigIOHandler);
  295. }
  296.  
  297.  
  298. static Bool
  299. sunCloseScreen (i, pScreen)
  300.     int        i;
  301.     ScreenPtr    pScreen;
  302. {
  303.     SetupScreen(pScreen);
  304.     Bool    ret;
  305.  
  306.     signal (SIGIO, SIG_IGN);
  307.     sunDisableCursor (pScreen);
  308.     pScreen->CloseScreen = pPrivate->CloseScreen;
  309.     ret = (*pScreen->CloseScreen) (i, pScreen);
  310.     (void) (*pScreen->SaveScreen) (pScreen, SCREEN_SAVER_OFF);
  311.     xfree ((pointer) pPrivate);
  312.     return ret;
  313. }
  314.  
  315. Bool
  316. sunSaveScreen (pScreen, on)
  317.     ScreenPtr    pScreen;
  318.     int        on;
  319. {
  320.     int        state;
  321.  
  322.     if (on == SCREEN_SAVER_FORCER)
  323.     SetTimeSinceLastInputEvent();
  324.     else
  325.     {
  326.     if (on == SCREEN_SAVER_ON)
  327.         state = 0;
  328.     else
  329.         state = 1;
  330.     (void) ioctl(sunFbs[pScreen->myNum].fd, FBIOSVIDEO, &state);
  331.     }
  332.     return( TRUE );
  333. }
  334.  
  335. /*-
  336.  *-----------------------------------------------------------------------
  337.  * sunScreenInit --
  338.  *    Things which must be done for all types of frame buffers...
  339.  *    Should be called last of all.
  340.  *
  341.  * Results:
  342.  *    TRUE if successful, else FALSE
  343.  *
  344.  * Side Effects:
  345.  *    Both a BlockHandler and a WakeupHandler are installed for the
  346.  *    first screen.  Together, these handlers implement autorepeat
  347.  *    keystrokes on the Sun.
  348.  *
  349.  *-----------------------------------------------------------------------
  350.  */
  351.  
  352. Bool
  353. sunScreenAllocate (pScreen)
  354.     ScreenPtr    pScreen;
  355. {
  356.     sunScreenPtr    pPrivate;
  357.  
  358.     if (sunGeneration != serverGeneration)
  359.     {
  360.     sunScreenIndex = AllocateScreenPrivateIndex();
  361.     if (sunScreenIndex < 0)
  362.         return FALSE;
  363.     sunGeneration = serverGeneration;
  364.     }
  365.     pPrivate = (sunScreenPtr) xalloc (sizeof (sunScreenRec));
  366.     if (!pPrivate)
  367.     return FALSE;
  368.  
  369.     pScreen->devPrivates[sunScreenIndex].ptr = (pointer) pPrivate;
  370.     return TRUE;
  371. }
  372.  
  373. Bool
  374. sunScreenInit (pScreen)
  375.     ScreenPtr    pScreen;
  376. {
  377.     SetupScreen(pScreen);
  378.     extern void   sunBlockHandler();
  379.     extern void   sunWakeupHandler();
  380.     static ScreenPtr autoRepeatScreen;
  381.     extern miPointerScreenFuncRec   sunPointerScreenFuncs;
  382.  
  383.     pPrivate->installedMap = 0;
  384.     pPrivate->CloseScreen = pScreen->CloseScreen;
  385.     pScreen->CloseScreen = sunCloseScreen;
  386.     pScreen->SaveScreen = sunSaveScreen;
  387.  
  388.     /*
  389.      *    Block/Unblock handlers
  390.      */
  391.     if (autoRepeatHandlersInstalled == FALSE) {
  392.     autoRepeatScreen = pScreen;
  393.     autoRepeatHandlersInstalled = TRUE;
  394.     }
  395.  
  396.     if (pScreen == autoRepeatScreen) {
  397.         pScreen->BlockHandler = sunBlockHandler;
  398.         pScreen->WakeupHandler = sunWakeupHandler;
  399.     }
  400.  
  401.     if (!sunCursorInitialize (pScreen))
  402.     miDCInitialize (pScreen, &sunPointerScreenFuncs);
  403.  
  404. #ifdef SUN_WINDOWS
  405.     /*
  406.      * We need to wrap one of the cursor functions so that
  407.      * we can keep the SunWindows cursor position in sync
  408.      * with X. We still call the mi version to do the real
  409.      * work.
  410.      */
  411.     pPrivate->SetCursorPosition = pScreen->SetCursorPosition;
  412.     pScreen->SetCursorPosition = sunSetCursorPosition;
  413. #endif
  414.     sunInitCursor ();
  415.  
  416.     return TRUE;
  417. }
  418.  
  419. extern char *getenv();
  420.  
  421. /*-
  422.  *-----------------------------------------------------------------------
  423.  * nthdev --
  424.  *    Return the nth device in a colon-separated list of devices.
  425.  *    n is 0-origin.
  426.  *
  427.  * Results:
  428.  *    A pointer to a STATIC string which is the device name.
  429.  *
  430.  * Side Effects:
  431.  *    None.
  432.  *
  433.  *-----------------------------------------------------------------------
  434.  */
  435. static char *
  436. nthdev (dList, n)
  437.     register char    *dList;        /* Colon-separated device names */
  438.     int        n;              /* Device number wanted */
  439. {
  440.     char *result;
  441.     static char returnstring[100];
  442.  
  443.     while (n--) {
  444.     while (*dList && *dList != ':') {
  445.         dList++;
  446.     }
  447.     if (*dList)
  448.         ++dList;
  449.     }
  450.     if (*dList) {
  451.     register char *cp = dList;
  452.  
  453.     while (*cp && *cp != ':') {
  454.         cp++;
  455.     }
  456.     result = returnstring;
  457.     strncpy (result, dList, cp - dList);
  458.     result[cp - dList] = '\0';
  459.     } else {
  460.     result = (char *)0;
  461.     }
  462.     return (result);
  463. }
  464.  
  465. /*-
  466.  *-----------------------------------------------------------------------
  467.  * sunOpenFrameBuffer --
  468.  *    Open a frame buffer according to several rules. If running under
  469.  *    overview and we're set up for it, use the device given in the
  470.  *    PARENT envariable and note that the screen is under overview.
  471.  *    Else find the device to use by looking in the sunFbData table,
  472.  *    an XDEVICE envariable, a -dev switch or using /dev/fb if trying
  473.  *    to open screen 0 and all else has failed.
  474.  *
  475.  * Results:
  476.  *    The fd of the framebuffer.
  477.  *
  478.  * Side Effects:
  479.  *
  480.  *-----------------------------------------------------------------------
  481.  */
  482. int
  483. sunOpenFrameBuffer(expect, pfbType, index, fbNum, argc, argv)
  484.     int              expect;       /* The expected type of framebuffer */
  485.     struct fbtype *pfbType;     /* Place to store the fb info */
  486.     int              fbNum;        /* Index into the sunFbData array */
  487.     int              index;        /* Screen index */
  488.     int              argc;            /* Command-line arguments... */
  489.     char      **argv;       /* ... */
  490. {
  491.     char             *name=(char *)0;
  492.     int           i;            /* Index into argument list */
  493.     int           fd = -1;            /* Descriptor to device */
  494.     static int      devFbUsed=FALSE;  /* true if /dev/fb has been used for a */
  495.                                   /* screen already */
  496.     static Bool      inited = FALSE;
  497.     static char      *xdevice;     /* string of devices to use from environ */
  498.     static char      *devsw;       /* string of devices from args */
  499.     struct fbgattr  fbattr;
  500.     int          type;
  501.  
  502.     sunFbs[index].parent = FALSE;
  503.  
  504.     if (!inited) {
  505.     xdevice = devsw = (char *)NULL;
  506.  
  507.     xdevice = getenv (XDEVICE);
  508.     /*
  509.      * Look for an argument of the form -dev <device-string>
  510.      * If such a one is found place the <device-string> in devsw.
  511.      */
  512.     for (i = 1; i < argc; i++) {
  513.         if ((strcmp(argv[i], "-dev") == 0) && (i + 1 < argc)) {
  514.         devsw = argv[i+1];
  515.         break;
  516.         }
  517.     }
  518.     inited = TRUE;
  519.     }
  520.  
  521.     /*
  522.      * Attempt to find a file name for the frame buffer 
  523.      */
  524.  
  525.     /*
  526.      * First see if any device was given on the command line.
  527.      * If one was and the device is both readable and writeable,
  528.      * set 'name' to it, else set it to NULL.
  529.      */
  530.     if (devsw == (char *)NULL ||
  531.     (name = nthdev (devsw, index)) == (char *)NULL ||
  532.     (access (name, R_OK | W_OK) != 0) ||
  533.     (strcmp(name, sunFbData[fbNum].devName) != 0)) {
  534.         name = (char *)NULL;
  535.     }
  536.         
  537.     /*
  538.      * If we still don't have a device for this screen, check the
  539.      * environment variable for one. If one was given, stick its
  540.      * path in name and check its accessibility. If it's not
  541.      * properly accessible, then reset the name to NULL to force the
  542.      * checking of the sunFbData array.
  543.      */
  544.     if (devsw == (char *)NULL && name == (char *)NULL &&
  545.     xdevice != (char *)NULL &&
  546.     (name = nthdev(xdevice, index)) != (char *)NULL &&
  547.     (access (name, R_OK | W_OK) != 0)) {
  548.         name = (char *)NULL;
  549.     }
  550.  
  551.     /*
  552.      * Take the device given in the frame buffer description
  553.      * and see if it exists and is accessible. If it does/is,
  554.      * we will use it, as long as no other device was given.
  555.      */
  556.     if (devsw == (char *)NULL && name == (char *)NULL &&
  557.     access(sunFbData[fbNum].devName, (R_OK | W_OK)) == 0) {
  558.         name = sunFbData[fbNum].devName;
  559.     }
  560.  
  561.     /*
  562.      * If we still have nothing and have yet to use "/dev/fb" for
  563.      * a screen, default the name to be "/dev/fb"
  564.      */
  565.     if (devsw == (char *)NULL && name == (char *)NULL && !devFbUsed) {
  566.     name = "/dev/fb";
  567.     }
  568.  
  569.  
  570.     if (name != (char *) NULL && sunUseSunWindows()) {
  571. #ifdef    SUN_WINDOWS
  572.  
  573.     /*
  574.      * Running X in coexistence with SunWindows.
  575.      *
  576.      * This section of code enables X to run with SunWindows.  This is 
  577.      * accomplished by opening up screens and windows in the SunWindows 
  578.      * style and accepting input events from them.  However, since the X 
  579.      * graphics libraries want a raw framebuffer, we open the 
  580.      * framebuffer of the current screen and return that.  So we get 
  581.      * input from SunWindows but send output to the raw framebuffer.
  582.      */
  583.  
  584.     char       *parent = getenv("WINDOW_PARENT");
  585.     struct screen sc;
  586.     int        winFd;
  587.     int        parentFd;
  588.     int        framebufferFd;
  589.     Rect        r;
  590.     static struct screen newScreen;
  591.     struct inputmask inputMask;
  592.     Bool        keepParent = FALSE;
  593.     static int  sunFbFound = 0;    /* True if FB found under SunWindows */
  594.  
  595.     /*
  596.      * If no device was specified on the command line, open the window 
  597.      * specified in WINDOW_PARENT.  If a device was specified, open a 
  598.      * new screen on that device and use it as a parent window.
  599.      */
  600.  
  601.     if ( devsw ) {
  602.         bzero( (caddr_t)&newScreen, sizeof(newScreen) );
  603.         strcpy( newScreen.scr_fbname, name );
  604.         newScreen.scr_flags |= SCR_TOGGLEENABLE;
  605.         if ( (parentFd = win_screennew( &newScreen )) < 0 ) {
  606.         ErrorF( "sunOpenFrameBuffer: Can't open new screen on %s.\n",
  607.             name );
  608.         return( -1 );
  609.         }
  610.         keepParent = TRUE;
  611.     } else {
  612.         if ((parentFd = open(parent, 2, 0)) < 0) {
  613.         ErrorF("sunOpenFrameBuffer: Can't open parent %s.\n", parent);
  614.         return (-1);
  615.         }
  616.     }
  617.  
  618.     if ((winFd = win_getnewwindow()) < 0) {
  619.         ErrorF("sunOpenFrameBuffer: Can't open a new window.\n");
  620.         close( parentFd );
  621.         return (-1);
  622.     }
  623.  
  624.     /* link the new window into the window hierarchy */
  625.     bzero((caddr_t) & r, sizeof r);
  626.     win_getrect(parentFd, &r);
  627.     win_setrect(winFd, &r);
  628.     win_setlink(winFd, WL_PARENT, win_fdtonumber(parentFd));
  629.     win_setlink(winFd, WL_OLDERSIB, win_getlink(parentFd, WL_TOPCHILD));
  630.     sunFbs[index].parent = TRUE;
  631.  
  632.     /*
  633.      * We would like to close the parent window here, since it's no 
  634.      * longer needed.  However, in the case where the parent is really a 
  635.      * screen, we have to keep it open, because closing a screen closes 
  636.      * all the windows on that screen.
  637.      */
  638.  
  639.     if ( ! keepParent )
  640.         close(parentFd);
  641.     
  642.     /*
  643.      * Express interest in SunView mouse events.  Note: the win_insert 
  644.      * must be AFTER setting the input mask in order to receive the 
  645.      * initial LOC_WINENTER event.
  646.      */
  647.  
  648.     input_imnull(&inputMask);
  649.     inputMask.im_flags = IM_ASCII | IM_META | IM_NEGEVENT | IM_INTRANSIT;
  650.  
  651.     win_setinputcodebit(&inputMask, LOC_MOVE);
  652.     win_setinputcodebit(&inputMask, LOC_WINEXIT);
  653.     win_setinputcodebit(&inputMask, LOC_WINENTER);
  654.     win_setinputcodebit(&inputMask, MS_LEFT);
  655.     win_setinputcodebit(&inputMask, MS_MIDDLE);
  656.     win_setinputcodebit(&inputMask, MS_RIGHT);
  657.     win_set_pick_mask(winFd, &inputMask);
  658.  
  659.     input_imnull(&inputMask);
  660.     win_setinputcodebit(&inputMask, KBD_USE);
  661.     win_setinputcodebit(&inputMask, KBD_DONE);
  662.     win_set_kbd_mask(winFd, &inputMask);
  663.  
  664.     win_insert(winFd);
  665.  
  666.     /*
  667.      * Determine the framebuffer name from the window's screen, then 
  668.      * open it.  Then do an FBIOGTYPE ioctl to determine its type.
  669.      */
  670.  
  671.     win_screenget( winFd, &sc );
  672.     if ((framebufferFd = open(sc.scr_fbname, O_RDWR, 0)) < 0) {
  673.         ErrorF("sunOpenFrameBuffer: can't open %s\n",sc.scr_fbname);
  674.         (void) close(winFd);
  675.         (void) close(parentFd);
  676.         return (-1);
  677.     }
  678.  
  679.     if (ioctl(framebufferFd, FBIOGTYPE, pfbType) < 0) {
  680.         perror("sunOpenFrameBuffer: FBIOGTYPE");
  681. badfb:
  682.         (void) close(framebufferFd);
  683.         (void) close(winFd);
  684.         (void) close(parentFd);
  685.         return (-1);
  686.     }
  687.  
  688.     if (pfbType->fb_type != expect) {
  689.         struct fbgattr fbgattr;
  690.         int i;
  691.  
  692.         /*
  693.          * On a 3/110 we only want to open one display
  694.          * on /dev/fb (or /dev/cgfour0) if no device is
  695.          * specified in the command line.  If not, we won't
  696.          * be able to slide back and forth between X displays
  697.          * since only one SunWindows framebuffer is open.
  698.          * The screen that is opened is /dev/bwtwo0.
  699.          */
  700.         if (sunFbFound && (devsw == (char *) NULL))
  701.         goto badfb;
  702.  
  703.         if (ioctl(framebufferFd, FBIOGATTR, &fbgattr) < 0)
  704.         goto badfb;
  705.         
  706.         for (i=0; i<FB_ATTR_NEMUTYPES; i++)
  707.         if (fbgattr.emu_types[i] == expect)
  708.             break;
  709.         else if (fbgattr.emu_types[i] == -1)
  710.             goto badfb;
  711.     }
  712.         /*
  713.      * NDELAY only applies to "input" fds, or fds that can be
  714.      * read.  sunwindows fds are read, while frame buffer fds aren't.
  715.      * That's why this fcntl is in the conditional compilation section.
  716.      */
  717.  
  718.     if (fcntl(winFd, F_SETFL, FNDELAY|FASYNC) < 0) {
  719.         ErrorF("Can't set FNDELAY|FASYNC on %s\n",name);
  720.         perror("sunOpenFrameBuffer");
  721.         (void) close(winFd);
  722.         return (-1);
  723.     }
  724.     
  725.     fd = framebufferFd;
  726.     windowFd = winFd;
  727.     sunFbFound++;
  728. #else
  729.     ErrorF("Not configured to run inside SunWindows\n");
  730.     fd = -1;
  731. #endif    SUN_WINDOWS
  732.     } else if (name) {
  733.     fd = open(name, O_RDWR, 0);
  734.         if (fd < 0) {
  735.         return (-1);
  736.     } 
  737.     if (ioctl(fd, FBIOGATTR, &fbattr) < 0)
  738.     {
  739.         if (ioctl(fd, FBIOGTYPE, pfbType) < 0) {
  740.             perror("sunOpenFrameBuffer");
  741.             (void) close(fd);
  742.             return (-1);
  743.         }
  744.         type = pfbType->fb_type;
  745.     }
  746.     else
  747.     {
  748.         int    i;
  749.  
  750.         type = fbattr.real_type;
  751.         *pfbType = fbattr.fbtype;
  752.         if (expect == FBTYPE_SUN2BW && expect != type)
  753.         {
  754.             for (i = 0; i < FB_ATTR_NEMUTYPES; i++)
  755.             {
  756.             if (expect == fbattr.emu_types[i])
  757.             {
  758.                 type = fbattr.emu_types[i];
  759.             break;
  760.             }
  761.             }
  762.         }
  763.     }
  764.     /* XXX - this is temporary 'cos the CG4 pretends its a BW2 */
  765.     if (strcmp(name, sunFbData[fbNum].devName) != 0 && type != expect)
  766.     {
  767.         (void) close(fd);
  768.         return (-1);
  769.     }
  770.     }
  771.  
  772.     if (name && strcmp (name, "/dev/fb") == 0) {
  773.     devFbUsed = TRUE;
  774.     }
  775.  
  776.     return (fd);
  777. }
  778.