sunFbs.c revision b9325ec5
1
2/* $Xorg: sunFbs.c,v 1.4 2001/02/09 02:04:43 xorgcvs Exp $ */
3
4/*
5Copyright 1990, 1993, 1998  The Open Group
6
7Permission to use, copy, modify, distribute, and sell this software and its
8documentation for any purpose is hereby granted without fee, provided that
9the above copyright notice appear in all copies and that both that
10copyright notice and this permission notice appear in supporting
11documentation.
12
13The above copyright notice and this permission notice shall be included in
14all copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
19OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23Except as contained in this notice, the name of The Open Group shall not be
24used in advertising or otherwise to promote the sale, use or other dealings
25in this Software without prior written authorization from The Open Group.
26 */
27
28/************************************************************
29Copyright 1987 by Sun Microsystems, Inc. Mountain View, CA.
30
31                    All Rights Reserved
32
33Permission  to  use,  copy,  modify,  and  distribute   this
34software  and  its documentation for any purpose and without
35fee is hereby granted, provided that the above copyright no-
36tice  appear  in all copies and that both that copyright no-
37tice and this permission notice appear in  supporting  docu-
38mentation,  and  that the names of Sun or The Open Group
39not be used in advertising or publicity pertaining to
40distribution  of  the software  without specific prior
41written permission. Sun and The Open Group make no
42representations about the suitability of this software for
43any purpose. It is provided "as is" without any express or
44implied warranty.
45
46SUN DISCLAIMS ALL WARRANTIES WITH REGARD TO  THIS  SOFTWARE,
47INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-
48NESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SUN BE  LI-
49ABLE  FOR  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
50ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,  DATA  OR
51PROFITS,  WHETHER  IN  AN  ACTION OF CONTRACT, NEGLIGENCE OR
52OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
53THE USE OR PERFORMANCE OF THIS SOFTWARE.
54
55********************************************************/
56
57/*
58 * Copyright 1987 by the Regents of the University of California
59 * Copyright 1987 by Adam de Boor, UC Berkeley
60 *
61 * Permission to use, copy, modify, and distribute this
62 * software and its documentation for any purpose and without
63 * fee is hereby granted, provided that the above copyright
64 * notice appear in all copies.  The University of California
65 * makes no representations about the suitability of this
66 * software for any purpose.  It is provided "as is" without
67 * express or implied warranty.
68 */
69
70/* $XFree86: xc/programs/Xserver/hw/sun/sunFbs.c,v 1.8 2003/11/17 22:20:36 dawes Exp $ */
71
72/****************************************************************/
73/* Modified from  sunCG4C.c for X11R3 by Tom Jarmolowski	*/
74/****************************************************************/
75
76#include "sun.h"
77#include <sys/mman.h>
78
79static Bool closeScreen(int, ScreenPtr pScreen);
80
81int sunScreenIndex;
82
83DevPrivateKeyRec sunScreenPrivateKeyRec;
84
85pointer
86sunMemoryMap(size_t len, off_t off, int fd)
87{
88    int		pagemask, mapsize;
89    caddr_t	addr;
90    pointer	mapaddr;
91
92#ifdef SVR4
93    pagemask = sysconf(_SC_PAGESIZE) - 1;
94#else
95    pagemask = getpagesize() - 1;
96#endif
97    mapsize = ((int) len + pagemask) & ~pagemask;
98    addr = 0;
99
100#if !defined(__bsdi__) && !defined(_MAP_NEW) && !defined(__NetBSD__) && !defined(__OpenBSD__)
101    if ((addr = (caddr_t) valloc (mapsize)) == NULL) {
102	Error ("Couldn't allocate frame buffer memory");
103	(void) close (fd);
104	return NULL;
105    }
106#endif
107
108#if !defined(__NetBSD__) && !defined(__OpenBSD__)
109    /*
110     * try and make it private first, that way once we get it, an
111     * interloper, e.g. another server, can't get this frame buffer,
112     * and if another server already has it, this one won't.
113     */
114    if ((int)(mapaddr = (pointer) mmap (addr,
115		mapsize,
116		PROT_READ | PROT_WRITE, MAP_PRIVATE,
117		fd, off)) == -1)
118#endif
119	mapaddr = (pointer) mmap (addr,
120		    mapsize,
121		    PROT_READ | PROT_WRITE, MAP_SHARED,
122		    fd, off);
123    if (mapaddr == (pointer) -1) {
124	Error ("mapping frame buffer memory");
125	(void) close (fd);
126	mapaddr = (pointer) NULL;
127    }
128    return mapaddr;
129}
130
131Bool
132sunScreenAllocate(ScreenPtr pScreen)
133{
134    sunScreenPtr    pPrivate;
135
136    if (!dixRegisterPrivateKey(&sunScreenPrivateKeyRec, PRIVATE_SCREEN, 0)) {
137	Error("dixRegisterPrivateKey failed");
138	return FALSE;
139    }
140    pPrivate = (sunScreenPtr) xalloc (sizeof (sunScreenRec));
141    if (!pPrivate)
142	return FALSE;
143
144    sunSetScreenPrivate(pScreen, pPrivate);
145    return TRUE;
146}
147
148Bool
149sunSaveScreen(ScreenPtr pScreen, int on)
150{
151    int		state;
152
153    if (on != SCREEN_SAVER_FORCER)
154    {
155	if (on == SCREEN_SAVER_ON)
156	    state = 0;
157	else
158	    state = 1;
159	(void) ioctl(sunFbs[pScreen->myNum].fd, FBIOSVIDEO, &state);
160    }
161    return( TRUE );
162}
163
164static Bool
165closeScreen(int i, ScreenPtr pScreen)
166{
167    sunScreenPtr pPrivate = sunGetScreenPrivate(pScreen);
168    Bool    ret;
169
170    (void) OsSignal (SIGIO, SIG_IGN);
171#if 0	/* XXX GX is disabled for now */
172    sunDisableCursor (pScreen);
173#endif
174    pScreen->CloseScreen = pPrivate->CloseScreen;
175    ret = (*pScreen->CloseScreen) (i, pScreen);
176    (void) (*pScreen->SaveScreen) (pScreen, SCREEN_SAVER_OFF);
177    xfree ((pointer) pPrivate);
178    return ret;
179}
180
181Bool
182sunScreenInit(ScreenPtr pScreen)
183{
184    sunScreenPtr pPrivate = sunGetScreenPrivate(pScreen);
185
186    pPrivate->installedMap = 0;
187    pPrivate->CloseScreen = pScreen->CloseScreen;
188    pScreen->CloseScreen = closeScreen;
189    pScreen->SaveScreen = sunSaveScreen;
190#if 0	/* XXX GX is disabled for now */
191    if (!sunCursorInitialize (pScreen))
192#endif
193	miDCInitialize (pScreen, &sunPointerScreenFuncs);
194    return TRUE;
195}
196
197Bool
198sunInitCommon(
199    int		scrn,
200    ScreenPtr	pScrn,
201    off_t	offset,
202    Bool	(*init1)(ScreenPtr, pointer, int, int, int, int, int, int),
203    void	(*init2)(ScreenPtr),
204    Bool	(*cr_cm)(ScreenPtr),
205    Bool	(*save)(ScreenPtr, int),
206    int		fb_off
207)
208{
209    unsigned char*	fb = sunFbs[scrn].fb;
210
211    if (!sunScreenAllocate (pScrn))
212	return FALSE;
213    if (!fb) {
214	if ((fb = sunMemoryMap ((size_t) sunFbs[scrn].info.fb_size,
215			     offset,
216			     sunFbs[scrn].fd)) == NULL)
217	    return FALSE;
218	sunFbs[scrn].fb = fb;
219    }
220    /* mfbScreenInit() or cfbScreenInit() */
221    if (!(*init1)(pScrn, fb + fb_off,
222	    sunFbs[scrn].info.fb_width,
223	    sunFbs[scrn].info.fb_height,
224	    monitorResolution, monitorResolution,
225	    sunFbs[scrn].info.fb_width,
226	    sunFbs[scrn].info.fb_depth))
227	    return FALSE;
228    /* sunCGScreenInit() if cfb... */
229    if (init2)
230	(*init2)(pScrn);
231    if (!sunScreenInit(pScrn))
232	return FALSE;
233    (void) (*save) (pScrn, SCREEN_SAVER_OFF);
234    return (*cr_cm)(pScrn);
235}
236
237