home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / ddx / x386 / vga / vga.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-26  |  9.7 KB  |  358 lines

  1. /*
  2.  * Copyright 1990,91 by Thomas Roell, Dinkelscherben, Germany.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and its
  5.  * documentation for any purpose is hereby granted without fee, provided that
  6.  * the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of Thomas Roell not be used in
  9.  * advertising or publicity pertaining to distribution of the software without
  10.  * specific, written prior permission.  Thomas Roell makes no representations
  11.  * about the suitability of this software for any purpose.  It is provided
  12.  * "as is" without express or implied warranty.
  13.  *
  14.  * THOMAS ROELL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16.  * EVENT SHALL THOMAS ROELL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  19.  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  20.  * PERFORMANCE OF THIS SOFTWARE.
  21.  *
  22.  * Author:  Thomas Roell, roell@informatik.tu-muenchen.de
  23.  *
  24.  * $Header: /proj/X11/mit/server/ddx/x386/vga/RCS/vga.c,v 1.2 1991/06/27 00:02:49 root Exp $
  25.  */
  26.  
  27.  
  28. #include "X.h"
  29. #include "input.h"
  30. #include "scrnintstr.h"
  31. #include "mipointer.h"
  32. #include "cursorstr.h"
  33.  
  34. #include "compiler.h"
  35.  
  36. #include "x386.h"
  37. #include "x386Priv.h"
  38. #include "x386OSD.h"
  39. #include "vga.h"
  40. #include "cfb.h"
  41.  
  42. extern void NoopDDA();
  43.  
  44. ScrnInfoRec vga256InfoRec = {
  45.   FALSE,        /* Bool configured */
  46.   -1,            /* int index */
  47.   vgaProbe,        /* Bool (* Probe)() */
  48.   vgaScreenInit,    /* Bool (* Init)() */
  49.   vgaEnterLeaveVT,    /* void (* EnterLeaveVT)() */
  50.   NoopDDA,        /* void (* EnterLeaveMonitor)() */
  51.   NoopDDA,        /* void (* EnterLeaveCursor)() */
  52.   vgaAdjustFrame,    /* void (* AdjustFrame)() */
  53.   vgaSwitchMode,    /* void (* SwitchMode)() */
  54.   8,            /* int depth */
  55.   8,            /* int bitsPerPixel */
  56.   PseudoColor,        /* int defaultVisual */
  57.   -1, -1,        /* int virtualX,virtualY */
  58.   -1, -1, -1, -1,    /* int frameX0, frameY0, frameX1, frameY1 */
  59.   NULL,            /* char *vendor */
  60.   NULL,            /* char *chipset */
  61.   0,            /* int clocks */
  62.   {0, },        /* int clock[MAXCLOCKS] */
  63.   0,            /* int videoRam */
  64.   240, 180,        /* int width, height */
  65.   NULL,            /* DisplayModePtr modes */
  66. };
  67.  
  68. pointer vgaOrigVideoState = NULL;
  69. pointer vgaNewVideoState = NULL;
  70. pointer vgaBase = NULL;
  71. struct kd_memloc vgaDSC;
  72.  
  73. void (* vgaEnterLeaveFunc)();
  74. void (* vgaInitFunc)();
  75. void * (* vgaSaveFunc)();
  76. void (* vgaRestoreFunc)();
  77. void (* vgaAdjustFunc)();
  78. void (* vgaSetReadFunc)();
  79. void (* vgaSetWriteFunc)();
  80. void (* vgaSetReadWriteFunc)();
  81. int vgaMapSize;
  82. int vgaSegmentSize;
  83. int vgaSegmentShift;
  84. int vgaSegmentMask;
  85. void *vgaReadBottom;
  86. void *vgaReadTop;
  87. void *vgaWriteBottom;
  88. void *vgaWriteTop =    (pointer)&writeseg; /* dummy for linking */
  89. Bool vgaReadFlag;
  90. Bool vgaWriteFlag;
  91.  
  92. int vgaIOBase;
  93.  
  94. static Bool saveFuncs = FALSE;
  95. static void (* saveInitFunc)();
  96. static void * (* saveSaveFunc)();
  97. static void (* saveRestoreFunc)();
  98. static void (* saveAdjustFunc)();
  99. static void (* saveSetReadFunc)();
  100. static void (* saveSetWriteFunc)();
  101. static void (* saveSetReadWriteFunc)();
  102.  
  103. extern miPointerScreenFuncRec x386PointerScreenFuncs;
  104.  
  105. static vgaVideoChipPtr Drivers[] = {
  106.   &PVGA1,
  107.   &GVGA,
  108.   &ET3000,
  109.   &ET4000,
  110. };
  111.  
  112. #define MAXDRIVERS (sizeof(Drivers)/sizeof(vgaVideoChipPtr))
  113.  
  114. /*
  115.  * vgaProbe --
  116.  *     probe and initialize the hardware driver
  117.  */
  118. Bool
  119. vgaProbe()
  120. {
  121.   int            i, j;
  122.   DisplayModePtr pMode, pEnd;
  123.  
  124.   for (i=0; i < MAXDRIVERS; i++)
  125.  
  126.     if ((Drivers[i]->ChipProbe)())
  127.       {
  128.     ErrorF("VGA256: %s (mem: %dk clocks:",
  129.            vga256InfoRec.chipset,
  130.            vga256InfoRec.videoRam);
  131.     for (j=0; j < vga256InfoRec.clocks; j++)
  132.       ErrorF(" %2d", vga256InfoRec.clock[j]);
  133.     ErrorF(")\n");
  134.  
  135.     vgaEnterLeaveFunc = Drivers[i]->ChipEnterLeave;
  136.     vgaInitFunc = Drivers[i]->ChipInit;
  137.     vgaSaveFunc = Drivers[i]->ChipSave;
  138.     vgaRestoreFunc = Drivers[i]->ChipRestore;
  139.     vgaAdjustFunc = Drivers[i]->ChipAdjust;
  140.     vgaSetReadFunc = Drivers[i]->ChipSetRead;
  141.     vgaSetWriteFunc = Drivers[i]->ChipSetWrite;
  142.     vgaSetReadWriteFunc = Drivers[i]->ChipSetReadWrite;
  143.     vgaMapSize = Drivers[i]->ChipMapSize;
  144.     vgaSegmentSize = Drivers[i]->ChipSegmentSize;
  145.     vgaSegmentShift = Drivers[i]->ChipSegmentShift;
  146.     vgaSegmentMask = Drivers[i]->ChipSegmentMask;
  147.     vgaReadBottom = (pointer)Drivers[i]->ChipReadBottom;
  148.     vgaReadTop = (pointer)Drivers[i]->ChipReadTop;
  149.     vgaWriteBottom = (pointer)Drivers[i]->ChipWriteBottom;
  150.     vgaWriteTop = (pointer)Drivers[i]->ChipWriteTop;
  151.  
  152.     if (vga256InfoRec.virtualX > 0 &&
  153.         vga256InfoRec.virtualX * vga256InfoRec.virtualY >
  154.         vga256InfoRec.videoRam * 1024)
  155.       {
  156.         ErrorF("Too less memory for virtual resolution\n");
  157.         return(FALSE);
  158.       }
  159.  
  160.  
  161.     pMode = pEnd = vga256InfoRec.modes;
  162.     do {
  163.       x386LookupMode(pMode, &vga256InfoRec);
  164.       if (pMode->HDisplay * pMode->VDisplay > vga256InfoRec.videoRam*1024)
  165.         {
  166.           ErrorF("Too less memory for all resolutions\n");
  167.           return(FALSE);
  168.         }
  169.       pMode = pMode->next;
  170.     }
  171.     while (pMode != pEnd);
  172.     
  173.     return TRUE;
  174.       }
  175.   
  176.   return FALSE;
  177. }
  178.  
  179.  
  180. /*
  181.  * vgaScreenInit --
  182.  *      Attempt to find and initialize a VGA framebuffer
  183.  *      Most of the elements of the ScreenRec are filled in.  The
  184.  *      video is enabled for the frame buffer...
  185.  */
  186.  
  187. Bool
  188. vgaScreenInit (index, pScreen, argc, argv)
  189.     int            index;        /* The index of pScreen in the ScreenInfo */
  190.     ScreenPtr      pScreen;      /* The Screen to initialize */
  191.     int            argc;         /* The number of the Server's arguments. */
  192.     char           **argv;       /* The arguments themselves. Don't change! */
  193. {
  194.   if (vgaBase == NULL) {
  195. #ifdef SCO
  196.     /*
  197.      * To map the video-memory, we use the MAPCONS ioctl. First the screen
  198.      * must be in graphics mode, hence the SW_CG640x350 (older SVR3.2 have
  199.      * no VGA support, thus a EGA mode here !!!
  200.      */
  201.     if (ioctl(x386Info.consoleFd, SW_CG640x350, 0) != 0 ||
  202.          (int)(vgaBase=(pointer)ioctl(x386Info.consoleFd, MAPCONS, 0)) == -1 )
  203.       FatalError("failed to map the video memory\n");
  204. #else
  205.     vgaBase = (pointer)(((uint)xalloc(0x11000) & ~0xFFF) + 0x1000);
  206.     vgaDSC.vaddr    = (char*)vgaBase;
  207.     vgaDSC.physaddr = (char*)0xA0000;
  208.     vgaDSC.length   = 0x10000;
  209.     vgaDSC.ioflg   = 1;
  210.     if (ioctl(x386Info.consoleFd, KDMAPDISP, &vgaDSC) < 0)
  211.       FatalError("failed to map the video memory\n");
  212. #endif
  213.     vgaReadBottom  = (void *)((uint)vgaReadBottom + (uint)vgaBase); 
  214.     vgaReadTop     = (void *)((uint)vgaReadTop + (uint)vgaBase); 
  215.     vgaWriteBottom = (void *)((uint)vgaWriteBottom + (uint)vgaBase); 
  216.     vgaWriteTop    = (void *)((uint)vgaWriteTop + (uint)vgaBase); 
  217.   }
  218.  
  219.   (vgaInitFunc)(vga256InfoRec.modes);
  220.   vgaOrigVideoState = (pointer)(vgaSaveFunc)(vgaOrigVideoState);
  221.   (vgaRestoreFunc)(vgaNewVideoState);
  222.   (vgaAdjustFunc)(vga256InfoRec.frameX0, vga256InfoRec.frameY0);
  223.  
  224.   /*
  225.    * Inititalize the dragon to color display
  226.    */
  227.   if (!cfbScreenInit(pScreen,
  228.              (pointer) VGABASE,
  229.              vga256InfoRec.virtualX,
  230.              vga256InfoRec.virtualY,
  231.              75, 75,
  232.              vga256InfoRec.virtualX))
  233.     return(FALSE);
  234.  
  235.     pScreen->CloseScreen = vgaCloseScreen;
  236.     pScreen->SaveScreen = vgaSaveScreen;
  237.     pScreen->InstallColormap = vgaInstallColormap;
  238.     pScreen->UninstallColormap = vgaUninstallColormap;
  239.     pScreen->ListInstalledColormaps = vgaListInstalledColormaps;
  240.     pScreen->StoreColors = vgaStoreColors;
  241.   
  242.   miDCInitialize (pScreen, &x386PointerScreenFuncs);
  243.   return (cfbCreateDefColormap(pScreen));
  244.  
  245. }
  246.  
  247.  
  248.  
  249. static void saveDummy() {}
  250.  
  251. /*
  252.  * vgaEnterLeaveVT -- 
  253.  *      grab/ungrab the current VT completely.
  254.  */
  255.  
  256. void
  257. vgaEnterLeaveVT(enter)
  258.      Bool enter;
  259. {
  260.   if (enter)
  261.     {
  262.       vgaInitFunc = saveInitFunc;
  263.       vgaSaveFunc = saveSaveFunc;
  264.       vgaRestoreFunc = saveRestoreFunc;
  265.       vgaAdjustFunc = saveAdjustFunc;
  266.       vgaSetReadFunc = saveSetReadFunc;
  267.       vgaSetWriteFunc = saveSetWriteFunc;
  268.       vgaSetReadWriteFunc = saveSetReadWriteFunc;
  269.       
  270. #ifndef SCO
  271.       ioctl(x386Info.consoleFd, KDMAPDISP, &vgaDSC);
  272. #endif
  273.       (vgaEnterLeaveFunc)(ENTER);
  274.       vgaOrigVideoState = (pointer)(vgaSaveFunc)(vgaOrigVideoState);
  275.       (vgaRestoreFunc)(vgaNewVideoState);
  276.  
  277.       saveFuncs = FALSE;
  278.     }
  279.   else
  280.     {
  281.       (vgaSaveFunc)(vgaNewVideoState);
  282.       /*
  283.        * We come here in many cases, but one is special: When the server aborts
  284.        * abnormaly. Therefore there MUST be a check whether vgaOrigVideoState
  285.        * is valid or not.
  286.        */
  287.       if (vgaOrigVideoState)
  288.     (vgaRestoreFunc)(vgaOrigVideoState);
  289.  
  290.       (vgaEnterLeaveFunc)(LEAVE);
  291. #ifndef SCO
  292.       ioctl(x386Info.consoleFd, KDUNMAPDISP, 0);
  293. #endif
  294.  
  295.       saveInitFunc = vgaInitFunc;
  296.       saveSaveFunc = vgaSaveFunc;
  297.       saveRestoreFunc = vgaRestoreFunc;
  298.       saveAdjustFunc = vgaAdjustFunc;
  299.       saveSetReadFunc = vgaSetReadFunc;
  300.       saveSetWriteFunc = vgaSetWriteFunc;
  301.       saveSetReadWriteFunc = vgaSetReadWriteFunc;
  302.       
  303.       vgaInitFunc = saveDummy;
  304.       vgaSaveFunc = (void * (*)())saveDummy;
  305.       vgaRestoreFunc = saveDummy;
  306.       vgaAdjustFunc = saveDummy;
  307.       vgaSetReadFunc = saveDummy;
  308.       vgaSetWriteFunc = saveDummy;
  309.       vgaSetReadWriteFunc = saveDummy;
  310.       
  311.       saveFuncs = TRUE;
  312.     }
  313. }
  314.  
  315.  
  316.  
  317. /*
  318.  * vgaCloseScreen --
  319.  *      called to ensure video is enabled when server exits.
  320.  */
  321.  
  322. Bool
  323. vgaCloseScreen()
  324. {
  325.   /*
  326.    * Hmm... The server may shut down even if it is not running on the
  327.    * current vt. Let's catch this case here.
  328.    */
  329.   if (x386VTSema) vgaEnterLeaveVT(LEAVE);
  330.   return(TRUE);
  331. }
  332.  
  333.  
  334.  
  335. /*
  336.  * vgaAdjustFrame --
  337.  *      Set a new viewport
  338.  */
  339. void
  340. vgaAdjustFrame(x, y)
  341.      int x, y;
  342. {
  343.   (vgaAdjustFunc)(x, y);
  344. }
  345.  
  346.  
  347. /*
  348.  * vgaSwitchMode --
  349.  *     Set a new display mode
  350.  */
  351. void
  352. vgaSwitchMode(mode)
  353.      DisplayModePtr mode;
  354. {
  355.   (vgaInitFunc)(mode);
  356.   (vgaRestoreFunc)(vgaNewVideoState);
  357. }
  358.