summaryrefslogtreecommitdiff
path: root/xc/programs/Xserver/hw/svga/svgaFb.c
blob: 74d587713685b6a37c870c9d919d6d9c55be8d40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* $XConsortium: svgaFb.c,v 1.3 93/09/20 14:05:32 rws Exp $ */
/*
 * Copyright 1990,91,92,93 by Thomas Roell, Germany.
 * Copyright 1991,92,93    by SGCS (Snitily Graphics Consulting Services), USA.
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this  permission notice appear
 * in supporting documentation, and that the name of Thomas Roell nor
 * SGCS be used in advertising or publicity pertaining to distribution
 * of the software without specific, written prior permission.
 * Thomas Roell nor SGCS makes no representations about the suitability
 * of this software for any purpose. It is provided "as is" without
 * express or implied warranty.
 *
 * THOMAS ROELL AND SGCS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL THOMAS ROELL OR SGCS BE LIABLE FOR ANY
 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "svga.h"
#include "cfb.h"
#include "mi.h"
#include "mibstore.h"
#include "mipointer.h"
#include "misprite.h"
#include "colormapst.h"
#include "resource.h"
#include "pixmapstr.h"
#include "rgb.h"
#include "windowstr.h"
#ifdef MITSHM
 extern void ShmRegisterFbFuncs();
#endif

extern miBSFuncRec svgaBankBSFuncRec;

static Bool ScreenIsSaved = FALSE;
static ColormapPtr pInstalledMap;
static RGB clut[1<<PSZ];


/* ARGSUSED */
static Bool
svgaSVGACursorOffScreen(
    ScreenPtr *pScreen,
    int       *x,
    int       *y
)
{
  return FALSE;
}

/* ARGSUSED */
static void
svgaSVGACrossScreen(
    ScreenPtr pScreen,
    Bool      entering
)
{
}

static void
svgaSVGAWrapCursor(
    ScreenPtr pScreen,
    int       x,
    int       y
)
{
  miPointerWarpCursor(pScreen, x, y);
}

static miPointerScreenFuncRec svgaScreenFuncsRec = {
  svgaSVGACursorOffScreen,
  svgaSVGACrossScreen,
  svgaSVGAWrapCursor,
};

Bool
svgaSVGABlankScreen(
    ScreenPtr pScreen,
    int       on
)
{
  int i;

  if (!svgaVTActive) return TRUE;

  switch(on) {

  case SCREEN_SAVER_FORCER:
    break;

  case SCREEN_SAVER_OFF:
    ScreenIsSaved = FALSE;

    for (i = 0; i < (1<<PSZ); i++)
      svgaSVPMISetColor(pScreen, i, clut[i].red, clut[i].green, clut[i].blue);
    break;

  case SCREEN_SAVER_ON:
    ScreenIsSaved = TRUE;

    for (i = 0; i < (1<<PSZ); i++)
      svgaSVPMISetColor(pScreen, i, 0, 0, 0);
    break;
  }

  return TRUE;
}

static void
svgaSVGAStoreColors(
    ColormapPtr pmap,
    int         ndef,
    xColorItem  *pdefs
)
{
  int         idef, shift, ndx;
  xColorItem  directDefs[1<<PSZ];

  if (pmap != pInstalledMap)
    return;

  if ((pmap->pVisual->class | DynamicClass) == DirectColor)
    {
      ndef = cfbExpandDirectColors(pmap, ndef, pdefs, directDefs);
      pdefs = directDefs;
    }

  shift = 16 - pmap->pVisual->bitsPerRGBValue;

  for(idef = 0; idef < ndef; idef++) {
    ndx = pdefs[idef].pixel;
    clut[ndx].red = pdefs[idef].red >> shift;
    clut[ndx].green = pdefs[idef].green >> shift;
    clut[ndx].blue = pdefs[idef].blue >> shift;

    if (svgaVTActive && !ScreenIsSaved)
      svgaSVPMISetColor(pmap->pScreen,
			ndx, clut[ndx].red, clut[ndx].green, clut[ndx].blue);
  }
}

static void
svgaSVGAInstallColormap(
    ColormapPtr pmap
)
{
  int         entries;
  Pixel *     ppix;
  xrgb *      prgb;
  xColorItem *defs;
  int         i;

  if (pmap == pInstalledMap)
    return;

  if ((pmap->pVisual->class | DynamicClass) == DirectColor)
    entries = (pmap->pVisual->redMask |
	       pmap->pVisual->greenMask |
	       pmap->pVisual->blueMask) + 1;
  else
    entries = pmap->pVisual->ColormapEntries;

  ppix = (Pixel*)ALLOCATE_LOCAL(entries * sizeof(Pixel));
  prgb = (xrgb*)ALLOCATE_LOCAL(entries * sizeof(xrgb));
  defs = (xColorItem*)ALLOCATE_LOCAL(entries * sizeof(xColorItem));

  if (pInstalledMap != NULL)
    WalkTree( pmap->pScreen, TellLostMap, &pInstalledMap->mid);
  pInstalledMap = pmap;

  for (i = 0; i < entries; i++ ) ppix[i] = i;
  QueryColors( pmap, entries, ppix, prgb);

  for (i = 0; i < entries; i++)
    {
      defs[i].pixel = ppix[i];
      defs[i].red = prgb[i].red;
      defs[i].green = prgb[i].green;
      defs[i].blue = prgb[i].blue;
      defs[i].flags =  DoRed|DoGreen|DoBlue;
    }
  svgaSVGAStoreColors(pmap, entries, defs);
  WalkTree(pmap->pScreen, TellGainedMap, &pmap->mid);

  DEALLOCATE_LOCAL(ppix);
  DEALLOCATE_LOCAL(prgb);
  DEALLOCATE_LOCAL(defs);
}

static void
svgaSVGAUninstallColormap(
    ColormapPtr pmap
)
{
  if (pmap == pInstalledMap) {
    pmap = (ColormapPtr)LookupIDByType(pmap->pScreen->defColormap,
				       RT_COLORMAP);
    (*pmap->pScreen->InstallColormap)(pmap);
  }    
}

/* ARGSUSED */
static int
svgaSVGAListInstalledColormaps(
    ScreenPtr pScreen,
    Colormap  *pmaps
)
{
  *pmaps = pInstalledMap->mid;

  return 1;
}

static Bool
svgaSVGACreateScreenResources(
    ScreenPtr pScreen
)
{
  PixmapPtr pPixmap;
  ulong     bytesPerScanLine = (ulong)pScreen->devPrivate;

  pPixmap = (*pScreen->CreatePixmap)(pScreen, 0, 0, pScreen->rootDepth);
  if (!pPixmap)
    return FALSE;

  if (!(*pScreen->ModifyPixmapHeader)(pPixmap,
				      pScreen->width, pScreen->height,
				      pScreen->rootDepth, pScreen->rootDepth,
				      bytesPerScanLine,
				      pScreen->devPrivate))
    return FALSE;

  pPixmap->drawable.class = 1; /* ### */
  pScreen->devPrivate = (pointer)pPixmap;

  return TRUE;
}

/* ARGSUSED */
static Bool
svgaSVGACloseScreen(
    int         index,
    ScreenPtr   pScreen
)
{
  int      d;
  DepthPtr depths = pScreen->allowedDepths;

  for (d = 0; d < pScreen->numDepths; d++) Xfree(depths[d].vids);
  Xfree(depths);
  Xfree(pScreen->visuals);

  svgaSVPMISetText(pScreen);

  return TRUE;
}

/* ARGSUSED */
Bool
svgaSVGAScreenInit(
    int        index,
    ScreenPtr  pScreen,
    int        argc,
    char       **argv
)
{
  ulong       winAAttributes, winBAttributes, winABase, winBBase;
  int         winGranularity, winSize, bytesPerScanLine, bitsRGB;
  int         XResolution, YResolution;
  VisualPtr   visuals, pVisual;
  DepthPtr    depths;
  int         nvisuals, ndepths, rootdepth;
  VisualID    defaultVisual;
  ColormapPtr pCmap;
  ushort      zeros = 0x0000;
  ushort      ones  = 0xffff; 

  if (!svgaSVPMIOpen(pScreen, argc, argv,
		     &winAAttributes, &winBAttributes,
		     &winGranularity,
		     &winSize,
		     &winABase, &winBBase,
		     &bytesPerScanLine,
		     &XResolution,
		     &YResolution,
		     &bitsRGB))
    return FALSE;

  if (!cfbSetupScreen(pScreen,
		      NULL,
		      XResolution, YResolution, 
		      75, 75,
		      bytesPerScanLine))
    return FALSE;

  rootdepth = 0;

  if (!cfbInitVisuals(&visuals, &depths, &nvisuals, &ndepths, &rootdepth,
		       &defaultVisual, 1<<(PSZ-1), bitsRGB))
        return FALSE;

  pScreen->SaveScreen = svgaSVGABlankScreen;
  pScreen->InstallColormap = svgaSVGAInstallColormap;
  pScreen->UninstallColormap = svgaSVGAUninstallColormap;
  pScreen->ListInstalledColormaps = svgaSVGAListInstalledColormaps;
  pScreen->StoreColors = svgaSVGAStoreColors;

  if (!miScreenInit(pScreen, NULL,
		    XResolution, YResolution,
		    75, 75,
		    bytesPerScanLine,
		    rootdepth, ndepths, depths,
		    defaultVisual, nvisuals, visuals,
		    NULL))
    return FALSE;

  pScreen->CloseScreen = svgaSVGACloseScreen;
  pScreen->CreateScreenResources = svgaSVGACreateScreenResources;

  svgaSVPMISetGraphics(pScreen);

  if (!svgaBankScreenInit(pScreen,
			  winAAttributes, winBAttributes,
			  winGranularity,
			  winSize,
			  winABase, winBBase,
			  bytesPerScanLine,
			  XResolution, YResolution,
			  svgaSVPMISetWindow))
    return FALSE;

#ifdef MITSHM
  ShmRegisterFbFuncs(pScreen);
#endif

  miInitializeBackingStore(pScreen, &svgaBankBSFuncRec);

  if (!miDCInitialize(pScreen, &svgaScreenFuncsRec))
    return FALSE;

  pScreen->whitePixel = 1;
  pScreen->blackPixel = 0;

  for (pVisual = pScreen->visuals;
       pVisual->vid != pScreen->rootVisual;
       pVisual++)
    ;

  if (CreateColormap(pScreen->defColormap, pScreen, pVisual, &pCmap,
		     (pVisual->class & DynamicClass) ? AllocNone : AllocAll,
		     0)
      != Success)

    return FALSE;

  if ((AllocColor(pCmap, &ones, &ones, &ones,
		  &(pScreen->whitePixel), 0) != Success) ||
      (AllocColor(pCmap, &zeros, &zeros, &zeros,
		  &(pScreen->blackPixel), 0) != Success))
    
      return FALSE;

  (*pScreen->InstallColormap)(pCmap);

  pScreen->devPrivate = (pointer)bytesPerScanLine;

  return TRUE;
}