fbscreen.c revision b2450a3a
1/*
2 * Copyright © 1998 Keith Packard
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 Keith Packard not be used in
9 * advertising or publicity pertaining to distribution of the software without
10 * specific, written prior permission.  Keith Packard makes no
11 * representations about the suitability of this software for any purpose.  It
12 * is provided "as is" without express or implied warranty.
13 *
14 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL KEITH PACKARD 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
23#ifdef HAVE_DIX_CONFIG_H
24#include <dix-config.h>
25#endif
26
27#include "fb.h"
28
29Bool
30fbCloseScreen(ScreenPtr pScreen)
31{
32    int d;
33    DepthPtr depths = pScreen->allowedDepths;
34
35    fbDestroyGlyphCache();
36    for (d = 0; d < pScreen->numDepths; d++)
37        free(depths[d].vids);
38    free(depths);
39    free(pScreen->visuals);
40    if (pScreen->devPrivate)
41        FreePixmap((PixmapPtr)pScreen->devPrivate);
42    return TRUE;
43}
44
45Bool
46fbRealizeFont(ScreenPtr pScreen, FontPtr pFont)
47{
48    return TRUE;
49}
50
51Bool
52fbUnrealizeFont(ScreenPtr pScreen, FontPtr pFont)
53{
54    return TRUE;
55}
56
57void
58fbQueryBestSize(int class,
59                unsigned short *width, unsigned short *height,
60                ScreenPtr pScreen)
61{
62    unsigned short w;
63
64    switch (class) {
65    case CursorShape:
66        if (*width > pScreen->width)
67            *width = pScreen->width;
68        if (*height > pScreen->height)
69            *height = pScreen->height;
70        break;
71    case TileShape:
72    case StippleShape:
73        w = *width;
74        if ((w & (w - 1)) && w < FB_UNIT) {
75            for (w = 1; w < *width; w <<= 1);
76            *width = w;
77        }
78    }
79}
80
81PixmapPtr
82_fbGetWindowPixmap(WindowPtr pWindow)
83{
84    return fbGetWindowPixmap(pWindow);
85}
86
87void
88_fbSetWindowPixmap(WindowPtr pWindow, PixmapPtr pPixmap)
89{
90    dixSetPrivate(&pWindow->devPrivates, fbGetWinPrivateKey(pWindow), pPixmap);
91}
92
93Bool
94fbSetupScreen(ScreenPtr pScreen, void *pbits, /* pointer to screen bitmap */
95              int xsize,        /* in pixels */
96              int ysize, int dpix,      /* dots per inch */
97              int dpiy, int width,      /* pixel width of frame buffer */
98              int bpp)
99{                               /* bits per pixel for screen */
100    if (!fbAllocatePrivates(pScreen))
101        return FALSE;
102    pScreen->defColormap = FakeClientID(0);
103    if (bpp > 1) {
104	/* let CreateDefColormap do whatever it wants for pixels */
105	pScreen->blackPixel = pScreen->whitePixel = (Pixel) 0;
106    }
107    pScreen->QueryBestSize = fbQueryBestSize;
108    /* SaveScreen */
109    pScreen->GetImage = fbGetImage;
110    pScreen->GetSpans = fbGetSpans;
111    pScreen->CreateWindow = fbCreateWindow;
112    pScreen->DestroyWindow = fbDestroyWindow;
113    pScreen->PositionWindow = fbPositionWindow;
114    pScreen->ChangeWindowAttributes = fbChangeWindowAttributes;
115    pScreen->RealizeWindow = fbRealizeWindow;
116    pScreen->UnrealizeWindow = fbUnrealizeWindow;
117    pScreen->CopyWindow = fbCopyWindow;
118    pScreen->CreatePixmap = fbCreatePixmap;
119    pScreen->DestroyPixmap = fbDestroyPixmap;
120    pScreen->RealizeFont = fbRealizeFont;
121    pScreen->UnrealizeFont = fbUnrealizeFont;
122    pScreen->CreateGC = fbCreateGC;
123    if (bpp == 1) {
124	pScreen->CreateColormap = mfbCreateColormap;
125    } else {
126	pScreen->CreateColormap = fbInitializeColormap;
127    }
128    pScreen->DestroyColormap = (void (*)(ColormapPtr)) NoopDDA;
129    pScreen->InstallColormap = fbInstallColormap;
130    pScreen->UninstallColormap = fbUninstallColormap;
131    pScreen->ListInstalledColormaps = fbListInstalledColormaps;
132    pScreen->StoreColors = (void (*)(ColormapPtr, int, xColorItem *)) NoopDDA;
133    pScreen->ResolveColor = fbResolveColor;
134    pScreen->BitmapToRegion = fbPixmapToRegion;
135
136    pScreen->GetWindowPixmap = _fbGetWindowPixmap;
137    pScreen->SetWindowPixmap = _fbSetWindowPixmap;
138
139    return TRUE;
140}
141
142#ifdef FB_ACCESS_WRAPPER
143Bool
144wfbFinishScreenInit(ScreenPtr pScreen, void *pbits, int xsize, int ysize,
145                    int dpix, int dpiy, int width, int bpp,
146                    SetupWrapProcPtr setupWrap, FinishWrapProcPtr finishWrap)
147#else
148Bool
149fbFinishScreenInit(ScreenPtr pScreen, void *pbits, int xsize, int ysize,
150                   int dpix, int dpiy, int width, int bpp)
151#endif
152{
153    VisualPtr visuals;
154    DepthPtr depths;
155    int nvisuals;
156    int ndepths;
157    int rootdepth;
158    VisualID defaultVisual;
159
160#ifdef FB_DEBUG
161    int stride;
162
163    ysize -= 2;
164    stride = (width * bpp) / 8;
165    fbSetBits((FbStip *) pbits, stride / sizeof(FbStip), FB_HEAD_BITS);
166    pbits = (void *) ((char *) pbits + stride);
167    fbSetBits((FbStip *) ((char *) pbits + stride * ysize),
168              stride / sizeof(FbStip), FB_TAIL_BITS);
169#endif
170    /* fb requires power-of-two bpp */
171    if (Ones(bpp) != 1)
172        return FALSE;
173#ifdef FB_ACCESS_WRAPPER
174    fbGetScreenPrivate(pScreen)->setupWrap = setupWrap;
175    fbGetScreenPrivate(pScreen)->finishWrap = finishWrap;
176#endif
177    rootdepth = 0;
178    if (!fbInitVisuals(&visuals, &depths, &nvisuals, &ndepths, &rootdepth,
179                       &defaultVisual, ((unsigned long) 1 << (bpp - 1)),
180                       8))
181        return FALSE;
182    if (!miScreenInit(pScreen, pbits, xsize, ysize, dpix, dpiy, width,
183                      rootdepth, ndepths, depths,
184                      defaultVisual, nvisuals, visuals))
185        return FALSE;
186    /* overwrite miCloseScreen with our own */
187    pScreen->CloseScreen = fbCloseScreen;
188    return TRUE;
189}
190
191/* dts * (inch/dot) * (25.4 mm / inch) = mm */
192#ifdef FB_ACCESS_WRAPPER
193Bool
194wfbScreenInit(ScreenPtr pScreen, void *pbits, int xsize, int ysize,
195              int dpix, int dpiy, int width, int bpp,
196              SetupWrapProcPtr setupWrap, FinishWrapProcPtr finishWrap)
197{
198    if (!fbSetupScreen(pScreen, pbits, xsize, ysize, dpix, dpiy, width, bpp))
199        return FALSE;
200    if (!wfbFinishScreenInit(pScreen, pbits, xsize, ysize, dpix, dpiy,
201                             width, bpp, setupWrap, finishWrap))
202        return FALSE;
203    return TRUE;
204}
205#else
206Bool
207fbScreenInit(ScreenPtr pScreen, void *pbits, int xsize, int ysize,
208             int dpix, int dpiy, int width, int bpp)
209{
210    if (!fbSetupScreen(pScreen, pbits, xsize, ysize, dpix, dpiy, width, bpp))
211        return FALSE;
212    if (!fbFinishScreenInit(pScreen, pbits, xsize, ysize, dpix, dpiy,
213                            width, bpp))
214        return FALSE;
215    return TRUE;
216}
217#endif
218