home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / ddx / macII / macIIInitS.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-31  |  25.6 KB  |  964 lines

  1. /************************************************************ 
  2. Copyright 1988 by Apple Computer, Inc, Cupertino, California
  3.             All Rights Reserved
  4.  
  5. Permission to use, copy, modify, and distribute this software
  6. for any purpose and without fee is hereby granted, provided
  7. that the above copyright notice appear in all copies.
  8.  
  9. APPLE MAKES NO WARRANTY OR REPRESENTATION, EITHER EXPRESS,
  10. OR IMPLIED, WITH RESPECT TO THIS SOFTWARE, ITS QUALITY,
  11. PERFORMANCE, MERCHANABILITY, OR FITNESS FOR A PARTICULAR
  12. PURPOSE. AS A RESULT, THIS SOFTWARE IS PROVIDED "AS IS,"
  13. AND YOU THE USER ARE ASSUMING THE ENTIRE RISK AS TO ITS
  14. QUALITY AND PERFORMANCE. IN NO EVENT WILL APPLE BE LIABLE 
  15. FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
  16. DAMAGES RESULTING FROM ANY DEFECT IN THE SOFTWARE.
  17.  
  18. THE WARRANTY AND REMEDIES SET FORTH ABOVE ARE EXCLUSIVE
  19. AND IN LIEU OF ALL OTHERS, ORAL OR WRITTEN, EXPRESS OR
  20. IMPLIED.
  21.  
  22. ************************************************************/
  23. /*-
  24.  * macIIInit.c --
  25.  *    Initialization functions for screen/keyboard/mouse, etc.
  26.  *
  27.  * Copyright (c) 1987 by the Regents of the University of California
  28.  *
  29.  * Permission to use, copy, modify, and distribute this
  30.  * software and its documentation for any purpose and without
  31.  * fee is hereby granted, provided that the above copyright
  32.  * notice appear in all copies.  The University of California
  33.  * makes no representations about the suitability of this
  34.  * software for any purpose.  It is provided "as is" without
  35.  * express or implied warranty.
  36.  *
  37.  *
  38.  */
  39.  
  40. /************************************************************
  41. Copyright 1987 by Sun Microsystems, Inc. Mountain View, CA.
  42.  
  43.                     All Rights Reserved
  44.  
  45. Permission  to  use,  copy,  modify,  and  distribute   this
  46. software  and  its documentation for any purpose and without
  47. fee is hereby granted, provided that the above copyright no-
  48. tice  appear  in all copies and that both that copyright no-
  49. tice and this permission notice appear in  supporting  docu-
  50. mentation,  and  that the names of Sun or MIT not be used in
  51. advertising or publicity pertaining to distribution  of  the
  52. software  without specific prior written permission. Sun and
  53. M.I.T. make no representations about the suitability of this
  54. software for any purpose. It is provided "as is" without any
  55. express or implied warranty.
  56.  
  57. SUN DISCLAIMS ALL WARRANTIES WITH REGARD TO  THIS  SOFTWARE,
  58. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-
  59. NESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SUN BE  LI-
  60. ABLE  FOR  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  61. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,  DATA  OR
  62. PROFITS,  WHETHER  IN  AN  ACTION OF CONTRACT, NEGLIGENCE OR
  63. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
  64. THE USE OR PERFORMANCE OF THIS SOFTWARE.
  65.  
  66. ********************************************************/
  67.  
  68. #include    "macII.h"
  69. #include    <servermd.h>
  70. #include    "dixstruct.h"
  71. #include    "dix.h"
  72. #include    "opaque.h"
  73. #include    "mipointer.h"
  74. #include    <compat.h>
  75.  
  76. extern int macIIMouseProc();
  77. extern void macIIKbdProc();
  78. extern int macIIKbdSetUp();
  79.  
  80. extern Bool macIIMonoProbe();
  81. extern Bool macIIMonoInit();
  82.  
  83. extern Bool macIIColorInit();
  84.  
  85. extern Bool macIISlotProbe();
  86. extern void ProcessInputEvents();
  87.  
  88. extern void SetInputCheck();
  89.  
  90. static int video_find();
  91. static int get_video_data();
  92. static int get_all_video_data();
  93.  
  94. DevicePtr pPointerDevice, pKeyboardDevice;
  95.  
  96. static int autoRepeatHandlersInstalled; /* FALSE each time InitOutput called */
  97.  
  98. /*-
  99.  *-----------------------------------------------------------------------
  100.  * SigIOHandler --
  101.  *    Signal handler for SIGIO - input is available.
  102.  *
  103.  * Results:
  104.  *    SigIO is set - ProcessInputEvents() will be called soon.
  105.  *
  106.  * Side Effects:
  107.  *    None
  108.  *
  109.  *-----------------------------------------------------------------------
  110.  */
  111. /*ARGSUSED*/
  112. static void
  113. SigIOHandler(sig, code, scp)
  114.     int        code;
  115.     int        sig;
  116.     struct sigcontext *scp;
  117. {
  118.     macIIEnqueueEvents ();
  119. }
  120.  
  121. macIIFbDataRec macIIFbData[] = {
  122.     macIISlotProbe,      "slot 0",        neverProbed,
  123.     macIISlotProbe,      "slot 1",        neverProbed,
  124.     macIISlotProbe,      "slot 2",        neverProbed,
  125.     macIISlotProbe,      "slot 3",        neverProbed,
  126.     macIISlotProbe,      "slot 4",        neverProbed,
  127.     macIISlotProbe,      "slot 5",        neverProbed,
  128.     macIISlotProbe,      "slot 6",        neverProbed,
  129.     macIISlotProbe,      "slot 7",        neverProbed,
  130.     macIISlotProbe,      "slot 8",        neverProbed,
  131.     macIISlotProbe,      "slot 9",        neverProbed,
  132.     macIISlotProbe,      "slot A",        neverProbed,
  133.     macIISlotProbe,      "slot B",        neverProbed,
  134.     macIISlotProbe,      "slot C",        neverProbed,
  135.     macIISlotProbe,      "slot D",        neverProbed,
  136.     macIISlotProbe,      "slot E",        neverProbed,
  137.     /*
  138.      * The following entry provides support for A/UX 1.0 where no
  139.      * slot manager calls were available. After failing to probe
  140.      * all the slots above, InitOutput falls through to this entry
  141.      * identifying a single monochrome screen. It must be last in
  142.      * this table!
  143.      */
  144.      macIIMonoProbe,     "/dev/console",        neverProbed,
  145. };
  146.  
  147. /*
  148.  * NUMSCREENS is the number of supported frame buffers (i.e. the number of
  149.  * structures in macIIFbData which have an actual probeProc).
  150.  */
  151. #define NUMSCREENS (sizeof(macIIFbData)/sizeof(macIIFbData[0]))
  152. #define NUMDEVICES 2
  153.  
  154. fbFd    macIIFbs[NUMSCREENS];  /* Space for descriptors of open frame buffers */
  155.  
  156. static PixmapFormatRec    formats[] = {
  157.     1, 1, BITMAP_SCANLINE_PAD,    /* 1-bit deep */
  158.     8, 8, BITMAP_SCANLINE_PAD,    /* 8-bit deep */
  159. };
  160. #define NUMFORMATS    (sizeof formats)/(sizeof formats[0])
  161.  
  162. #define NUMSLOTS 16
  163. struct video *video_index[NUMSLOTS];     /* how to find it by slot number */
  164. static struct video video[NUMSLOTS];    /* their attributes */
  165.  
  166. #define NUMMODES    7        /* 1,2,4,8,16,24,32 */
  167. static struct legal_mode_struct {    /* only 1 and 8 bits are supported */
  168.     int depth;
  169.     int legal;
  170. } legal_modes[NUMMODES] = {{1,1},{2,0},{4,0},{8,1},{16,0},{24,0},{32,0}};
  171.  
  172. static int screen_depth[MAXSCREENS] = {0,0,0};
  173.  
  174. static struct video_mode_struct{
  175.     int depth[NUMMODES];
  176.         int mode[NUMMODES];   
  177.     struct video_data  info[NUMMODES];
  178. } video_modes[NUMSLOTS] = {
  179.     {{0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}},
  180.     {{0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}},
  181.     {{0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}},
  182.     {{0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}},
  183.     {{0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}},
  184.     {{0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}},
  185.     {{0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}},
  186.     {{0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}},
  187.     {{0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}},
  188.     {{0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}},
  189.     {{0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}},
  190.     {{0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}},
  191.     {{0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}},
  192.     {{0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}},
  193.     {{0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}},
  194.     {{0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}};
  195.  
  196. /*-
  197.  *-----------------------------------------------------------------------
  198.  * InitOutput --
  199.  *    Initialize screenInfo for all actually accessible framebuffers.
  200.  *
  201.  * Results:
  202.  *    screenInfo init proc field set
  203.  *
  204.  * Side Effects:
  205.  *    None
  206.  *
  207.  *-----------------------------------------------------------------------
  208.  */
  209.  
  210. InitOutput(pScreenInfo, argc, argv)
  211.     ScreenInfo       *pScreenInfo;
  212.     int           argc;
  213.     char          **argv;
  214. {
  215.     int           i, index, ac = argc;
  216.     char      **av = argv;
  217.  
  218.     pScreenInfo->imageByteOrder = IMAGE_BYTE_ORDER;
  219.     pScreenInfo->bitmapScanlineUnit = BITMAP_SCANLINE_UNIT;
  220.     pScreenInfo->bitmapScanlinePad = BITMAP_SCANLINE_PAD;
  221.     pScreenInfo->bitmapBitOrder = BITMAP_BIT_ORDER;
  222.  
  223.     pScreenInfo->numPixmapFormats = NUMFORMATS;
  224.     for (i=0; i< NUMFORMATS; i++)
  225.     {
  226.         pScreenInfo->formats[i] = formats[i];
  227.     }
  228.  
  229.     autoRepeatHandlersInstalled = FALSE;
  230.  
  231.     ParseDepths(argc, argv);
  232.  
  233. #define SLOT_LO 0x09
  234.     for (i = SLOT_LO, index = 0; i < NUMSCREENS - 1; i++) {
  235.     if ((* macIIFbData[i].probeProc) (pScreenInfo, index, i, argc, argv)) {
  236.         /* This display exists OK */
  237.         index++;
  238.     } 
  239.     }
  240.  
  241.     if (index == 0) {
  242.     if (macIIMonoProbe(pScreenInfo, index, NUMSCREENS - 1, argc, argv)) {
  243.         index++;
  244.     } 
  245.     }
  246.     if (index == 0)
  247.     FatalError("Can't find any displays\n");
  248.  
  249.     pScreenInfo->numScreens = index;
  250. }
  251.  
  252. ParseDepths(argc, argv)
  253.     int        argc;
  254.     char     **argv;
  255. {
  256. #define SCREEN_LO     0
  257. #define SCREEN_HI     (MAXSCREENS - 1)
  258. #define EQSTRING(s1,s2)    (!(strcmp((s1), (s2))))
  259.  
  260.     int i;
  261.     int j;
  262.     int screen = -1;
  263.     int depth = -1;
  264.     int supported_depth = 0;
  265.  
  266.     i = 0;
  267.     while (i < argc) {
  268.     if (EQSTRING(argv[i], "-screen")) {
  269.         if (i <= argc - 4) {
  270.             screen = atoi(argv[i+1]);
  271.                 if ((screen < SCREEN_LO) || (screen > SCREEN_HI)) {
  272.             ErrorF("Invalid screen number %d; a legal number is %d < screen < %d\n", 
  273.                 screen, SCREEN_LO, SCREEN_HI);
  274.                 }
  275.  
  276.         if (!EQSTRING(argv[i+2], "-depth")) {
  277.             goto usage;
  278.         }
  279.  
  280.             /* is this a depth supported by the server? */
  281.         depth = atoi(argv[i+3]);
  282.         for (j = 0; j < NUMMODES; j++) {
  283.             if ((legal_modes[j].depth == depth) &&
  284.             (legal_modes[j].legal)) {
  285.             screen_depth[screen] = depth;
  286.             supported_depth = 1;
  287.             break;
  288.             }
  289.         }
  290.         if (!supported_depth) {
  291.             ErrorF("Invalid screen depth specified\n", depth);
  292.         }
  293.         }
  294.         else {
  295. usage:
  296.         ErrorF("Usage: -screen screennum -depth depthnum\n");
  297.         }
  298.         i += 4;
  299.     }
  300.     else {
  301.             i++;
  302.     }
  303.     }
  304. }
  305.  
  306. /*-
  307.  *-----------------------------------------------------------------------
  308.  * InitInput --
  309.  *    Initialize all supported input devices...what else is there
  310.  *    besides pointer and keyboard?
  311.  *
  312.  * Results:
  313.  *    None.
  314.  *
  315.  * Side Effects:
  316.  *    Two DeviceRec's are allocated and registered as the system pointer
  317.  *    and keyboard devices.
  318.  *
  319.  *-----------------------------------------------------------------------
  320.  */
  321. /*ARGSUSED*/
  322. InitInput(argc, argv)
  323.     int           argc;
  324.     char          **argv;
  325. {
  326.     DevicePtr p, k;
  327.     static int  zero = 0;
  328.     
  329.     p = AddInputDevice(macIIMouseProc, TRUE);
  330.     k = AddInputDevice(macIIKbdProc, TRUE);
  331.  
  332.     pPointerDevice = p;
  333.     pKeyboardDevice = k;
  334.  
  335.     RegisterPointerDevice(p);
  336.     RegisterKeyboardDevice(k);
  337.     miRegisterPointerDevice(screenInfo.screens[0], p);
  338.  
  339.     setcompat (getcompat() | COMPAT_BSDSIGNALS);
  340.     if (!mieqInit (k, p))
  341.     return FALSE;
  342.     signal(SIGIO, SigIOHandler);
  343.     SetTimeSinceLastInputEvent ();
  344. }
  345.  
  346. /*-
  347.  *-----------------------------------------------------------------------
  348.  * macIISlotProbe --
  349.  *    Attempt to find and initialize a framebuffer. 
  350.  *
  351.  * Results:
  352.  *    TRUE if everything went ok. FALSE if not.
  353.  *
  354.  * Side Effects:
  355.  *    Memory is allocated for the frame buffer and the buffer is mapped.
  356.  *
  357.  *-----------------------------------------------------------------------
  358.  */
  359. Bool
  360. macIISlotProbe (pScreenInfo, index, fbNum, argc, argv)
  361.     ScreenInfo      *pScreenInfo;    /* The screenInfo struct */
  362.     int              index;        /* The index of pScreen in the ScreenInfo */
  363.     int              fbNum;        /* Index into the macIIFbData array */
  364.     int              argc;            /* The number of the Server's arguments. */
  365.     char          **argv;       /* The arguments themselves. Don't change! */
  366. {
  367.     int         i, j, oldNumScreens;
  368.     int        depth;
  369.     int        depth_supported;
  370.     char     *video_physaddr;
  371.     static char *video_virtaddr = (char *)(120 * 1024 * 1024);
  372.     static long csTable[] =
  373.     {
  374.         0x0000ffff, 0xffffffff,
  375.         0x00000000, 0x00000000
  376.     };
  377.  
  378.     static struct VDEntryRecord vde =
  379.     {
  380.         (char *) csTable,
  381.         0,              /* start = 0 */
  382.         1               /* count = 1 (2 entries) */
  383.     };
  384.     struct VDPgInfo vdp;
  385.     struct CntrlParam pb;
  386.     struct strioctl ctl; /* Streams ioctl control structure */
  387.     int mode_index;
  388.     int fd;
  389.  
  390.  
  391.     if (macIIFbData[fbNum].probeStatus == probedAndFailed) {
  392.     return FALSE;
  393.     }
  394.  
  395.     if (macIIFbData[fbNum].probeStatus == neverProbed) {
  396.  
  397.     if ((depth = video_find(fbNum)) < 0) {
  398.         macIIFbData[fbNum].probeStatus = probedAndFailed;
  399.         return FALSE;
  400.     }
  401.  
  402.     /* this slot is a video screen */
  403.  
  404.     /*
  405.      * we need to ensure that the video board supports a depth that the 
  406.      * server supports before physing in the board.
  407.      */
  408.     depth_supported = 0;
  409.     for (i = 0; i < NUMMODES; i++) {
  410.         for (j = 0; j < NUMMODES; j++) {
  411.         if ((video_modes[fbNum].depth[i] == legal_modes[j].depth) &&
  412.                 (legal_modes[j].legal)) {
  413.             depth_supported = 1;
  414.         }
  415.         }
  416.     }
  417.     if (!depth_supported) {
  418.         macIIFbData[fbNum].probeStatus = probedAndFailed;
  419.         ErrorF("Video board in slot %d does not offer a supported depth\n",
  420.         (fbNum - SLOT_LO));
  421.         return FALSE;
  422.     }
  423.  
  424. #ifdef VIDEO_MAP_SLOT
  425.     {
  426.         struct video_map_slot vmap;
  427.         struct strioctl ctl; /* Streams ioctl control structure */
  428.  
  429.         /* map frame buffer to next 16MB segment boundary above 128M */
  430.         video_virtaddr = video_virtaddr + (16 * 1024 * 1024); 
  431.         vmap.map_slotnum = fbNum;
  432.             vmap.map_physnum = index;
  433.             vmap.map_virtaddr = video_virtaddr;
  434.  
  435.         ctl.ic_cmd = VIDEO_MAP_SLOT;
  436.         ctl.ic_timout = -1;
  437.         ctl.ic_len = sizeof(vmap);
  438.         ctl.ic_dp = (char *)&vmap;
  439.         fd = open("/dev/console", O_RDWR, 0);
  440.         if (fd < 0) {
  441.             FatalError ("could not open /dev/console");
  442.         } 
  443.         if (ioctl(fd, I_STR, &ctl) == -1) {
  444.             FatalError ("ioctl I_STR VIDEO_MAP_SLOT failed");
  445.             (void) close (fd);
  446.             return (FALSE);
  447.         }
  448.         (void) close(fd);
  449.     }
  450. #else
  451.     video_physaddr = video[fbNum].video_base;
  452.     video_virtaddr = video_virtaddr + (16 * 1024 * 1024);
  453.  
  454.     /*
  455.      * Note that a unique reference number is provided as the
  456.      * first argument to phys. School of hard knocks ...
  457.      */
  458.     ErrorF("macIIInitS.c -- need to take out temp phys() hack.\n");
  459.     if (phys(index, video_virtaddr, 16*1024*1024, video_physaddr) == -1) {
  460.         FatalError ("phys failed, server must run suid root");
  461.         return (FALSE);
  462.     }
  463. #endif /* VIDEO_MAP_SLOT */
  464.  
  465.         macIIFbs[index].fb = (pointer)(video_virtaddr + 
  466.         video[fbNum].video_data.v_baseoffset); 
  467.  
  468.     macIIFbs[index].slot = fbNum;
  469.     macIIFbs[index].installedMap = NULL;
  470.  
  471.     /* set things up for default mode */
  472.     macIIFbs[index].default_depth = depth;
  473.         macIIFbs[index].info = video[fbNum].video_data;
  474.  
  475.     /* check if user asks for a specific depth */
  476.         if (screen_depth[index]) {
  477.         int i;
  478.         for (i = 0; i < NUMMODES; i++) {
  479.         if (video_modes[fbNum].depth[i] == screen_depth[index]) {
  480.             macIIFbs[index].default_depth = screen_depth[index];
  481.                 macIIFbs[index].info = video_modes[fbNum].info[i];
  482.             break;
  483.         }
  484.         }
  485.         if (i == NUMMODES) {
  486.         ErrorF("Screen %d does not support %d bits per pixel\n",
  487.             index, screen_depth[index]);
  488.         }
  489.     }
  490.  
  491.     macIIFbData[fbNum].probeStatus = probedAndSucceeded;
  492.  
  493.     }
  494.  
  495.     /*
  496.      * If we've ever successfully probed this device, do the following. 
  497.      */
  498.  
  499.     oldNumScreens = pScreenInfo->numScreens;
  500.  
  501.     {
  502.     /* poke the video board */
  503.     ctl.ic_cmd = VIDEO_CONTROL;
  504.     ctl.ic_timout = -1;
  505.     ctl.ic_len = sizeof(pb);
  506.     ctl.ic_dp = (char *)&pb;
  507.  
  508.     fd = open("/dev/console", O_RDWR, 0);
  509.     if (fd < 0) {
  510.         FatalError ("could not open /dev/console");
  511.     } 
  512.     for (mode_index = 0; mode_index < NUMMODES; ) {
  513.         if (video_modes[fbNum].depth[mode_index] == 
  514.             macIIFbs[index].default_depth) {
  515.         break;
  516.         }
  517.         mode_index++;
  518.     }
  519.  
  520. #define noQueueBit 0x0200
  521. #define SetMode 0x2
  522. #define SetEntries 0x3 
  523.  
  524.     vdp.csMode = video_modes[fbNum].mode[mode_index];
  525.     vdp.csData = 0;
  526.     vdp.csPage = 0;
  527.     vdp.csBaseAddr = (char *) NULL;
  528.  
  529.     pb.qType = fbNum;
  530.     pb.ioTrap = noQueueBit;
  531.     pb.ioCmdAddr = (char *) -1;
  532.     pb.csCode = SetMode;
  533.     * (char **) pb.csParam = (char *) &vdp;
  534.  
  535.     if (ioctl(fd, I_STR, &ctl) == -1) {
  536.         FatalError ("ioctl I_STR VIDEO_CONTROL failed");
  537.         (void) close (fd);
  538.         return (FALSE);
  539.     }
  540.  
  541.     (void) close (fd);
  542.     if (pb.qType != 0) {
  543.         FatalError ("ioctl I_STR VIDEO_CONTROL CMD failed");
  544.         return (FALSE);
  545.     }
  546.  
  547.     }
  548.  
  549.     macIIBlackScreen(index);
  550.  
  551.     switch (macIIFbs[index].default_depth) {
  552.     case 1:
  553.         /* install the black & white color map */
  554.         fd = open("/dev/console", O_RDWR, 0);
  555.         if (fd < 0) {
  556.         FatalError ("could not open /dev/console");
  557.         } 
  558.         pb.qType = fbNum;
  559.         pb.ioTrap = noQueueBit;
  560.         pb.ioCmdAddr = (char *) -1;
  561.         pb.csCode = SetEntries;
  562.         * (char **) pb.csParam = (char *) &vde;
  563.  
  564.         if (ioctl(fd, I_STR, &ctl) == -1) {
  565.         FatalError ("ioctl I_STR VIDEO_CONTROL failed");
  566.         (void) close (fd);
  567.         return(FALSE);
  568.         }  
  569.         (void) close (fd);
  570.  
  571.             i = AddScreen(macIIMonoInit, argc, argv);
  572.         break;
  573.  
  574.     case 8:
  575.             i = AddScreen(macIIColorInit, argc, argv);
  576.         break;
  577.  
  578.     default:
  579.         FatalError("Encountered bogus depth: %d", fbNum);
  580.         break;
  581.     }
  582.  
  583.     return (i >= oldNumScreens);
  584. }
  585.  
  586. static int
  587. video_find(slot)
  588. int slot;
  589. {
  590.     register struct video *vp;
  591.     register struct video_data *vdp;
  592.     int depth;
  593.  
  594.     vp = &video[slot];
  595.  
  596.  
  597.     /*
  598.      *    If it isn't a video card, ignore it.  Otherwise, get
  599.      *        the driver and video parameter block from the
  600.      *        slot ROM.
  601.      */
  602.     
  603.     vp->video_base = (char *)(0xf0000000 | (slot<<24));
  604.     vp->dce.dCtlSlot = slot;
  605.     vp->dce.dCtlDevBase = (long) vp->video_base;
  606.     vp->dce.dCtlExtDev = 0;
  607.     if ((depth = get_video_data(vp)) < 0)
  608.         return(-1);
  609.     vdp = &vp->video_data;
  610.     vp->video_mem_x = 8*vdp->v_rowbytes;
  611.     vp->video_mem_y = vdp->v_bottom - vdp->v_top;
  612.     vp->video_scr_x = vdp->v_right - vdp->v_left;
  613.     vp->video_scr_y = vdp->v_bottom - vdp->v_top;
  614.     vp->video_addr = vp->video_base + vdp->v_baseoffset;
  615.     video_index[slot] = vp;
  616.     vp->video_slot = slot;
  617.  
  618.     return(depth);
  619. }
  620.  
  621. /*
  622.     This routine uses the Slot Manager to find a video devices default
  623. mode and corresponding video parameter block.  It returns zero upon
  624. success and a slot manager error code upon failure.  This code is
  625. pretty much stolen from the Monitors Desk Accessory.
  626.     We search through the list of video parameter blocks for the one
  627. with the smallest bits/pixel.  For most devices, this will be the
  628. first on in the list.
  629.     This routine fills in the video_data and video_def_mode fields of the
  630. video structure.  It also fills in the dCtlSlotId field of the dce which
  631. is a magic number understood by only a few people on earth.  These people
  632. have gained this knowledge only by promising to remove their tongues.
  633. */
  634.  
  635. #include "sys/slotmgr.h"
  636. static int noSlotMgr;
  637.  
  638. static void
  639. SigSYSHandler(sig, code, scp)
  640.     int        code;
  641.     int        sig;
  642.     struct sigcontext *scp;
  643. {
  644.     noSlotMgr++;
  645. }
  646.  
  647. static int get_video_data(vp)
  648. register struct video *vp;
  649. {
  650.     int depth = 1024;
  651.     int default_mode = 0x80;/* video modes normally default to 0x80 */
  652.     int err;        /* last slot manager result */
  653.     int success = 0;    /* assume failure */
  654.     struct SpBlock pb;
  655.     struct video_data *vd;
  656.     caddr_t slotModesPointer;
  657.     int nextMode;
  658.  
  659.     pb.spSlot = vp->dce.dCtlSlot;
  660.     pb.spID = 0;
  661.     pb.spCategory = 3;    /* catDisplay */
  662.     pb.spCType = 1;        /* typeVideo */
  663.     pb.spDrvrSW = 1;    /* drSwApple */
  664.     pb.spTBMask = 1;
  665.  
  666.     noSlotMgr = 0;
  667.     signal(SIGSYS, SigSYSHandler);
  668.     err = slotmanager(_sNextTypesRsrc,&pb);
  669.     signal(SIGSYS, SIG_DFL);
  670.     if (noSlotMgr) return(-1);
  671.  
  672.     if (err == 0 && pb.spSlot != vp->dce.dCtlSlot)
  673.         err = smNoMoresRsrcs;
  674.     else if (err == 0) {
  675.         vp->dce.dCtlSlotId = pb.spID;
  676.         slotModesPointer = pb.spsPointer;
  677.         for (nextMode = 0x80; depth != 1 && !err; nextMode++) {
  678.             pb.spID = nextMode;
  679.             pb.spsPointer = slotModesPointer;
  680.             err = slotmanager(_sFindStruct,&pb);
  681.             if (err == 0) {
  682.                 pb.spID = 1;    /* mVidParams */
  683.                 pb.spResult = 
  684.                     (long)malloc(sizeof(struct video_data));
  685.                 err = slotmanager(_sGetBlock,&pb);
  686.                 if (err == 0) {
  687.                     vd = (struct video_data *) pb.spResult;
  688.                     if (vd->v_pixelsize < depth) {
  689.                         depth = vd->v_pixelsize;
  690.                         default_mode = nextMode;
  691.                         vp->video_data = *vd;
  692.                         success = 1;
  693.                     }
  694.                     else free(pb.spResult);
  695.                 }
  696.                 else free(pb.spResult);
  697.             }
  698.         }
  699.     }
  700.     vp->video_def_mode = default_mode;
  701.     get_all_video_data(vp);
  702.     return success? depth: -abs(err);
  703. }
  704.  
  705. static int get_all_video_data(vp)
  706. register struct video *vp;
  707. {
  708.     int depth = 1024;
  709.     int default_mode = 0x80;/* video modes normally default to 0x80 */
  710.     int err;        /* last slot manager result */
  711.     int success = 0;    /* assume failure */
  712.     struct SpBlock pb;
  713.     struct video_data *vd;
  714.     caddr_t slotModesPointer;
  715.     int nextMode;
  716.     int i;
  717.     int slot;
  718.  
  719.     pb.spSlot = vp->dce.dCtlSlot;
  720.     pb.spID = 0;
  721.     pb.spCategory = 3;    /* catDisplay */
  722.     pb.spCType = 1;        /* typeVideo */
  723.     pb.spDrvrSW = 1;    /* drSwApple */
  724.     pb.spTBMask = 1;
  725.  
  726.         slot = vp->dce.dCtlSlot;
  727.  
  728.     noSlotMgr = 0;
  729.     signal(SIGSYS, SigSYSHandler);
  730.     err = slotmanager(_sNextTypesRsrc,&pb);
  731.     signal(SIGSYS, SIG_DFL);
  732.     if (noSlotMgr) return(-1);
  733.  
  734.     if (err == 0 && pb.spSlot != vp->dce.dCtlSlot)
  735.         err = smNoMoresRsrcs;
  736.     else if (err == 0) {
  737.         vp->dce.dCtlSlotId = pb.spID;
  738.         slotModesPointer = pb.spsPointer;
  739.         for (nextMode = 0x80, i=0; ((!err) && (i < NUMMODES)); nextMode++) {
  740.         pb.spID = nextMode;
  741.         pb.spsPointer = slotModesPointer;
  742.         err = slotmanager(_sFindStruct,&pb);
  743.         if (err == 0) {
  744.             pb.spID = 1;    /* mVidParams */
  745.             pb.spResult = (long)(&video_modes[slot].info[i]);
  746.             err = slotmanager(_sGetBlock,&pb);
  747.             if (err == 0) {
  748.                 vd = (struct video_data *) pb.spResult;
  749.                 video_modes[slot].depth[i] = vd->v_pixelsize;
  750.                 video_modes[slot].mode[i] = nextMode;
  751.             i++;
  752.             }
  753.         }
  754.         }
  755.     }
  756.     return 0;
  757. }
  758.  
  759. /*-
  760.  *-------------------------------------------------------------%---------
  761.  * macIIScreenInit --
  762.  *    Things which must be done for all types of frame buffers...
  763.  *    Should be called last of all.
  764.  *
  765.  * Results:
  766.  *    None.
  767.  *
  768.  * Side Effects:
  769.  *    The graphics context for the screen is created. The CreateGC,
  770.  *    CreateWindow and ChangeWindowAttributes vectors are changed in
  771.  *    the screen structure.
  772.  *
  773.  *    Both a BlockHandler and a WakeupHandler are installed for the
  774.  *    first screen.  Together, these handlers implement autorepeat
  775.  *    keystrokes on the macII.
  776.  *
  777.  *-----------------------------------------------------------------------
  778.  */
  779. Bool
  780. macIIScreenInit (pScreen)
  781.     ScreenPtr      pScreen;
  782. {
  783.     extern void   macIIBlockHandler();
  784.     extern void   macIIWakeupHandler();
  785.     static ScreenPtr autoRepeatScreen;
  786.     extern miPointerScreenFuncRec   macIIPointerCursorFuncs;
  787.  
  788.     /*
  789.      *    Block/Unblock handlers
  790.      */
  791.     if (autoRepeatHandlersInstalled == FALSE) {
  792.     autoRepeatScreen = pScreen;
  793.     autoRepeatHandlersInstalled = TRUE;
  794.     }
  795.  
  796.     if (pScreen == autoRepeatScreen) {
  797.         pScreen->BlockHandler = macIIBlockHandler;
  798.         pScreen->WakeupHandler = macIIWakeupHandler;
  799.     }
  800.  
  801.     miDCInitialize (pScreen, &macIIPointerCursorFuncs);
  802.  
  803.     return TRUE; 
  804. }
  805.  
  806. /*-
  807.  *-----------------------------------------------------------------------
  808.  * macIIOpenFrameBuffer --
  809.  *    Open a frame buffer  through the /dev/console interface.
  810.  *
  811.  * Results:
  812.  *    The fd of the framebuffer.
  813.  *
  814.  * Side Effects:
  815.  *
  816.  *-----------------------------------------------------------------------
  817.  */
  818. int
  819. macIIOpenFrameBuffer(expect, pfbType, index, fbNum, argc, argv)
  820.     int              expect;       /* The expected type of framebuffer */
  821.     fbtype       *pfbType;     /* Place to store the fb info */
  822.     int              fbNum;        /* Index into the macIIFbData array */
  823.     int              index;        /* Screen index */
  824.     int              argc;            /* Command-line arguments... */
  825.     char      **argv;       /* ... */
  826. {
  827.     int           fd = -1;            /* Descriptor to device */
  828.     struct strioctl ctl;
  829.  
  830.     fd = open("/dev/console", O_RDWR, 0);
  831.     if (fd < 0) {
  832.     return (-1);
  833.     } 
  834.  
  835.     ctl.ic_cmd = VIDEO_DATA;
  836.     ctl.ic_timout = -1;
  837.     ctl.ic_len = sizeof(fbtype);
  838.     ctl.ic_dp = (char *)pfbType;
  839.     if (ioctl(fd, I_STR, &ctl) < 0) {
  840.         FatalError("Failed to ioctl I_STR VIDEO_DATA.\n");
  841.     (void) close(fd);
  842.         return(!Success);
  843.     }
  844.  
  845.     return (fd);
  846. }
  847.  
  848. /*-
  849.  *-----------------------------------------------------------------------
  850.  * macIIBlackScreen --
  851.  *    Fill a frame buffer with the black pixel.
  852.  *
  853.  * Results:
  854.  *    None
  855.  *
  856.  * Side Effects:
  857.  *
  858.  *-----------------------------------------------------------------------
  859.  */
  860. int
  861. macIIBlackScreen(index)
  862.       int index;
  863. {
  864.     fbFd *pf;
  865.     register unsigned char* fb;
  866.     register int fbinc, line, lw;
  867.     register unsigned int *fbt;
  868.  
  869.     pf = &macIIFbs[index];
  870.     fb = pf->fb; /* Assumed longword aligned! */
  871.  
  872.     switch (pf->info.v_pixelsize) {
  873.     case 1:
  874.     {
  875.       fbinc = pf->info.v_rowbytes;
  876.         for (line = pf->info.v_top; line < pf->info.v_bottom; line++) {
  877.           fbt = (unsigned int *)fb;
  878.           lw = ((pf->info.v_right - pf->info.v_left) + 31) >> 5;
  879.           do {
  880.               *fbt++ = 0xffffffff;
  881.           } while (--lw);
  882.           fb += fbinc;
  883.       }
  884.       break;
  885.     }
  886.     case 8:
  887.     {
  888.       fbinc = pf->info.v_rowbytes;
  889.         for (line = pf->info.v_top; line < pf->info.v_bottom; line++) {
  890.           fbt = (unsigned int *)fb;
  891.           lw = ((pf->info.v_right - pf->info.v_left) + 3) >> 2;
  892.           do {
  893.               *fbt++ = 0x01010101;
  894.           } while (--lw);
  895.           fb += fbinc;
  896.       }
  897.       break;
  898.     }
  899.     default:
  900.       ErrorF("Bad depth in macIIBlackScreen.");
  901.       break;
  902.     }
  903. }
  904.  
  905. void
  906. AbortDDX()
  907. {
  908.     extern int consoleFd, devosmFd;
  909.  
  910.     if (devosmFd > 0) close(devosmFd);
  911.     if (consoleFd > 0) {
  912.     macIIKbdSetUp(consoleFd, FALSE); /* Must NOT FatalError() anywhere! */
  913.         close(consoleFd);
  914.     consoleFd = 0;
  915.     }
  916. }
  917.  
  918. /* Called by GiveUp(). */
  919. void
  920. ddxGiveUp()
  921. {
  922. }
  923.  
  924. int
  925. ddxProcessArgument (argc, argv, i)
  926.     int       argc;
  927.     char *argv[];
  928.     int       i;
  929. {
  930.     if ( strcmp ( argv[i], "-screen" ) == 0)
  931.     return 4;
  932.     else return 0;
  933. }
  934.  
  935. void
  936. ddxUseMsg()
  937. {
  938.     ErrorF("-screen # -depth #      run screen (0-5) at depth {1,8}\n");
  939. }
  940.  
  941. void
  942. MessageF(s)
  943. char *s;
  944. {
  945.     ErrorF(s);
  946. }
  947.  
  948. /* AUX version of ffs does bits in the wrong order, silly them */
  949.  
  950. int
  951. ffs(mask)
  952. unsigned int    mask;
  953. {
  954.     register i;
  955.  
  956.     if ( ! mask ) return 0;
  957.     i = 1;
  958.     while (! (mask & 1)) {
  959.     i++;
  960.     mask = mask >> 1;
  961.     }
  962.     return i;
  963. }
  964.